blob: 534c8f685d5a939c11ebf359f5b9294ce0207873 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000021#include "llvm/DataLayout.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
Micah Villmow25a6a842012-10-08 16:25:52 +000054const llvm::DataLayout &ABIInfo::getDataLayout() const {
55 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000056}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
98 return 32;
99}
100
John McCallde5d3c72012-02-17 03:33:10 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000103 // The following conventions are known to require this to be false:
104 // x86_stdcall
105 // MIPS
106 // For everything else, we just prefer false unless we opt out.
107 return false;
108}
109
Daniel Dunbar98303b92009-09-13 08:03:58 +0000110static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000112/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000113/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000114static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
115 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000116 if (FD->isUnnamedBitfield())
117 return true;
118
119 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000121 // Constant arrays of empty records count as empty, strip them off.
122 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
125 if (AT->getSize() == 0)
126 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000127 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000128 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000129
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130 const RecordType *RT = FT->getAs<RecordType>();
131 if (!RT)
132 return false;
133
134 // C++ record fields are never empty, at least in the Itanium ABI.
135 //
136 // FIXME: We should use a predicate for whether this behavior is true in the
137 // current ABI.
138 if (isa<CXXRecordDecl>(RT->getDecl()))
139 return false;
140
Daniel Dunbar98303b92009-09-13 08:03:58 +0000141 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000142}
143
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000144/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000145/// fields. Note that a structure with a flexible array member is not
146/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000147static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000154
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000155 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000157 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158 e = CXXRD->bases_end(); i != e; ++i)
159 if (!isEmptyRecord(Context, i->getType(), true))
160 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000161
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
163 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000164 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166 return true;
167}
168
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000169/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
170/// a non-trivial destructor or a non-trivial copy constructor.
171static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
172 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
173 if (!RD)
174 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000175
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000176 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
177}
178
179/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
180/// a record type with either a non-trivial destructor or a non-trivial copy
181/// constructor.
182static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
183 const RecordType *RT = T->getAs<RecordType>();
184 if (!RT)
185 return false;
186
187 return hasNonTrivialDestructorOrCopyConstructor(RT);
188}
189
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000190/// isSingleElementStruct - Determine if a structure is a "single
191/// element struct", i.e. it has exactly one non-empty field or
192/// exactly one field which is itself a single element
193/// struct. Structures with flexible array members are never
194/// considered single element structs.
195///
196/// \return The field declaration for the single non-empty field, if
197/// it exists.
198static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
199 const RecordType *RT = T->getAsStructureType();
200 if (!RT)
201 return 0;
202
203 const RecordDecl *RD = RT->getDecl();
204 if (RD->hasFlexibleArrayMember())
205 return 0;
206
207 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000208
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000209 // If this is a C++ record, check the bases first.
210 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
211 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
212 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000213 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000214 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000215 continue;
216
217 // If we already found an element then this isn't a single-element struct.
218 if (Found)
219 return 0;
220
221 // If this is non-empty and not a single element struct, the composite
222 // cannot be a single element struct.
223 Found = isSingleElementStruct(i->getType(), Context);
224 if (!Found)
225 return 0;
226 }
227 }
228
229 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000232 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000233 QualType FT = FD->getType();
234
235 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000236 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000237 continue;
238
239 // If we already found an element then this isn't a single-element
240 // struct.
241 if (Found)
242 return 0;
243
244 // Treat single element arrays as the element.
245 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
246 if (AT->getSize().getZExtValue() != 1)
247 break;
248 FT = AT->getElementType();
249 }
250
John McCalld608cdb2010-08-22 10:59:02 +0000251 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000252 Found = FT.getTypePtr();
253 } else {
254 Found = isSingleElementStruct(FT, Context);
255 if (!Found)
256 return 0;
257 }
258 }
259
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000260 // We don't consider a struct a single-element struct if it has
261 // padding beyond the element type.
262 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
263 return 0;
264
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000265 return Found;
266}
267
268static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000269 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000270 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
271 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
Daniel Dunbar53012f42009-11-09 01:33:53 +0000278/// canExpandIndirectArgument - Test whether an argument type which is to be
279/// passed indirectly (on the stack) would have the equivalent layout if it was
280/// expanded into separate arguments. If so, we prefer to do the latter to avoid
281/// inhibiting optimizations.
282///
283// FIXME: This predicate is missing many cases, currently it just follows
284// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
285// should probably make this smarter, or better yet make the LLVM backend
286// capable of handling it.
287static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
288 // We can only expand structure types.
289 const RecordType *RT = Ty->getAs<RecordType>();
290 if (!RT)
291 return false;
292
293 // We can only expand (C) structures.
294 //
295 // FIXME: This needs to be generalized to handle classes as well.
296 const RecordDecl *RD = RT->getDecl();
297 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
298 return false;
299
Eli Friedman506d4e32011-11-18 01:32:26 +0000300 uint64_t Size = 0;
301
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
303 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000304 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305
306 if (!is32Or64BitBasicType(FD->getType(), Context))
307 return false;
308
309 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
310 // how to expand them yet, and the predicate for telling if a bitfield still
311 // counts as "basic" is more complicated than what we were doing previously.
312 if (FD->isBitField())
313 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000314
315 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000316 }
317
Eli Friedman506d4e32011-11-18 01:32:26 +0000318 // Make sure there are not any holes in the struct.
319 if (Size != Context.getTypeSize(Ty))
320 return false;
321
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322 return true;
323}
324
325namespace {
326/// DefaultABIInfo - The default implementation for ABI specific
327/// details. This implementation provides information which results in
328/// self-consistent and sensible LLVM IR generation, but does not
329/// conform to any particular ABI.
330class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000331public:
332 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000333
Chris Lattnera3c109b2010-07-29 02:16:43 +0000334 ABIArgInfo classifyReturnType(QualType RetTy) const;
335 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
Chris Lattneree5dcd02010-07-29 02:31:05 +0000337 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000342 }
343
344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345 CodeGenFunction &CGF) const;
346};
347
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000348class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
349public:
Chris Lattnerea044322010-07-29 02:01:43 +0000350 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000352};
353
354llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
355 CodeGenFunction &CGF) const {
356 return 0;
357}
358
Chris Lattnera3c109b2010-07-29 02:16:43 +0000359ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000360 if (isAggregateTypeForABI(Ty)) {
361 // Records with non trivial destructors/constructors should not be passed
362 // by value.
363 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
364 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000367 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000368
Chris Lattnera14db752010-03-11 18:19:55 +0000369 // Treat an enum type as its underlying type.
370 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
371 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000372
Chris Lattnera14db752010-03-11 18:19:55 +0000373 return (Ty->isPromotableIntegerType() ?
374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000375}
376
Bob Wilson0024f942011-01-10 23:54:17 +0000377ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
378 if (RetTy->isVoidType())
379 return ABIArgInfo::getIgnore();
380
381 if (isAggregateTypeForABI(RetTy))
382 return ABIArgInfo::getIndirect(0);
383
384 // Treat an enum type as its underlying type.
385 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
386 RetTy = EnumTy->getDecl()->getIntegerType();
387
388 return (RetTy->isPromotableIntegerType() ?
389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
390}
391
Derek Schuff9ed63f82012-09-06 17:37:28 +0000392//===----------------------------------------------------------------------===//
393// le32/PNaCl bitcode ABI Implementation
394//===----------------------------------------------------------------------===//
395
396class PNaClABIInfo : public ABIInfo {
397 public:
398 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
399
400 ABIArgInfo classifyReturnType(QualType RetTy) const;
401 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
402
403 virtual void computeInfo(CGFunctionInfo &FI) const;
404 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
405 CodeGenFunction &CGF) const;
406};
407
408class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
409 public:
410 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
411 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
412};
413
414void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
415 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
416
417 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
418
419 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
420 it != ie; ++it)
421 it->info = classifyArgumentType(it->type, FreeRegs);
422 }
423
424llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
425 CodeGenFunction &CGF) const {
426 return 0;
427}
428
429ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
430 unsigned &FreeRegs) const {
431 if (isAggregateTypeForABI(Ty)) {
432 // Records with non trivial destructors/constructors should not be passed
433 // by value.
434 FreeRegs = 0;
435 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
436 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
437
438 return ABIArgInfo::getIndirect(0);
439 }
440
441 // Treat an enum type as its underlying type.
442 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
443 Ty = EnumTy->getDecl()->getIntegerType();
444
445 ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
446 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
447
448 // Regparm regs hold 32 bits.
449 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
450 if (SizeInRegs == 0) return BaseInfo;
451 if (SizeInRegs > FreeRegs) {
452 FreeRegs = 0;
453 return BaseInfo;
454 }
455 FreeRegs -= SizeInRegs;
456 return BaseInfo.isDirect() ?
457 ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
458 ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
459}
460
461ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
462 if (RetTy->isVoidType())
463 return ABIArgInfo::getIgnore();
464
465 if (isAggregateTypeForABI(RetTy))
466 return ABIArgInfo::getIndirect(0);
467
468 // Treat an enum type as its underlying type.
469 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
470 RetTy = EnumTy->getDecl()->getIntegerType();
471
472 return (RetTy->isPromotableIntegerType() ?
473 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
474}
475
Eli Friedman55fc7e22012-01-25 22:46:34 +0000476/// UseX86_MMXType - Return true if this is an MMX type that should use the
477/// special x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000478bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000479 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
480 // special x86_mmx type.
481 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
482 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
483 IRType->getScalarSizeInBits() != 64;
484}
485
Jay Foadef6de3d2011-07-11 09:56:20 +0000486static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000488 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000489 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000490 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
491 return Ty;
492}
493
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000494//===----------------------------------------------------------------------===//
495// X86-32 ABI Implementation
496//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000497
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498/// X86_32ABIInfo - The X86-32 ABI information.
499class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000500 enum Class {
501 Integer,
502 Float
503 };
504
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000505 static const unsigned MinABIStackAlignInBytes = 4;
506
David Chisnall1e4249c2009-08-17 23:08:21 +0000507 bool IsDarwinVectorABI;
508 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000509 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000510 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000511 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000512
513 static bool isRegisterSize(unsigned Size) {
514 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
515 }
516
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000517 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
518 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000520 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
521 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000522 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
523 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000524
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000525 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000526 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000527
Rafael Espindolab48280b2012-07-31 02:44:24 +0000528 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000529 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000530 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000531 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
532 bool IsFastCall) const;
533 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
534 bool IsFastCall) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000536public:
537
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000538 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000539 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
540 CodeGenFunction &CGF) const;
541
Rafael Espindolab48280b2012-07-31 02:44:24 +0000542 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
543 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000544 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000545 IsMMXDisabled(m), IsWin32FloatStructABI(w),
546 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000547};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000549class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
550public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000551 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000552 bool d, bool p, bool m, bool w, unsigned r)
553 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000554
555 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
556 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000557
558 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
559 // Darwin uses different dwarf register numbers for EH.
560 if (CGM.isTargetDarwin()) return 5;
561
562 return 4;
563 }
564
565 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
566 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000567
Jay Foadef6de3d2011-07-11 09:56:20 +0000568 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000569 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000570 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000571 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
572 }
573
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000574};
575
576}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000577
578/// shouldReturnTypeInRegister - Determine if the given type should be
579/// passed in a register (for the Darwin ABI).
580bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000581 ASTContext &Context,
582 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583 uint64_t Size = Context.getTypeSize(Ty);
584
585 // Type must be register sized.
586 if (!isRegisterSize(Size))
587 return false;
588
589 if (Ty->isVectorType()) {
590 // 64- and 128- bit vectors inside structures are not returned in
591 // registers.
592 if (Size == 64 || Size == 128)
593 return false;
594
595 return true;
596 }
597
Daniel Dunbar77115232010-05-15 00:00:30 +0000598 // If this is a builtin, pointer, enum, complex type, member pointer, or
599 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000600 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000601 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000602 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000603 return true;
604
605 // Arrays are treated like records.
606 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000607 return shouldReturnTypeInRegister(AT->getElementType(), Context,
608 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000609
610 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000611 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000612 if (!RT) return false;
613
Anders Carlssona8874232010-01-27 03:25:19 +0000614 // FIXME: Traverse bases here too.
615
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000616 // For thiscall conventions, structures will never be returned in
617 // a register. This is for compatibility with the MSVC ABI
618 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
619 RT->isStructureType()) {
620 return false;
621 }
622
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000623 // Structure types are passed in register if all fields would be
624 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000625 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
626 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000627 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000628
629 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000630 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000631 continue;
632
633 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000634 if (!shouldReturnTypeInRegister(FD->getType(), Context,
635 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000636 return false;
637 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000638 return true;
639}
640
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000641ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
642 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000643 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000644 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000645
Chris Lattnera3c109b2010-07-29 02:16:43 +0000646 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000647 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000648 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000649 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650
651 // 128-bit vectors are a special case; they are returned in
652 // registers and we need to make sure to pick a type the LLVM
653 // backend will like.
654 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000655 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000656 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000657
658 // Always return in register if it fits in a general purpose
659 // register, or if it is 64 bits and has a single element.
660 if ((Size == 8 || Size == 16 || Size == 32) ||
661 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000662 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000663 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000664
665 return ABIArgInfo::getIndirect(0);
666 }
667
668 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000669 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000670
John McCalld608cdb2010-08-22 10:59:02 +0000671 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000672 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000673 // Structures with either a non-trivial destructor or a non-trivial
674 // copy constructor are always indirect.
675 if (hasNonTrivialDestructorOrCopyConstructor(RT))
676 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000677
Anders Carlsson40092972009-10-20 22:07:59 +0000678 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000679 if (RT->getDecl()->hasFlexibleArrayMember())
680 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000681 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000682
David Chisnall1e4249c2009-08-17 23:08:21 +0000683 // If specified, structs and unions are always indirect.
684 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 return ABIArgInfo::getIndirect(0);
686
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687 // Small structures which are register sized are generally returned
688 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000689 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
690 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000691 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000692
693 // As a special-case, if the struct is a "single-element" struct, and
694 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000695 // floating-point register. (MSVC does not apply this special case.)
696 // We apply a similar transformation for pointer types to improve the
697 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000698 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000699 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
700 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000701 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
702
703 // FIXME: We should be able to narrow this integer in cases with dead
704 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000705 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000706 }
707
708 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000709 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000710
Chris Lattnera3c109b2010-07-29 02:16:43 +0000711 // Treat an enum type as its underlying type.
712 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
713 RetTy = EnumTy->getDecl()->getIntegerType();
714
715 return (RetTy->isPromotableIntegerType() ?
716 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000717}
718
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000719static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
720 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
721}
722
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000723static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
724 const RecordType *RT = Ty->getAs<RecordType>();
725 if (!RT)
726 return 0;
727 const RecordDecl *RD = RT->getDecl();
728
729 // If this is a C++ record, check the bases first.
730 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
731 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
732 e = CXXRD->bases_end(); i != e; ++i)
733 if (!isRecordWithSSEVectorType(Context, i->getType()))
734 return false;
735
736 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
737 i != e; ++i) {
738 QualType FT = i->getType();
739
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000740 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000741 return true;
742
743 if (isRecordWithSSEVectorType(Context, FT))
744 return true;
745 }
746
747 return false;
748}
749
Daniel Dunbare59d8582010-09-16 20:42:06 +0000750unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
751 unsigned Align) const {
752 // Otherwise, if the alignment is less than or equal to the minimum ABI
753 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000754 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000755 return 0; // Use default alignment.
756
757 // On non-Darwin, the stack type alignment is always 4.
758 if (!IsDarwinVectorABI) {
759 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000760 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000761 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000762
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000763 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000764 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
765 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000766 return 16;
767
768 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000769}
770
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000771ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
772 unsigned &FreeRegs) const {
773 if (!ByVal) {
774 if (FreeRegs) {
775 --FreeRegs; // Non byval indirects just use one pointer.
776 return ABIArgInfo::getIndirectInReg(0, false);
777 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000778 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000779 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000780
Daniel Dunbare59d8582010-09-16 20:42:06 +0000781 // Compute the byval alignment.
782 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
783 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
784 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000785 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000786
787 // If the stack alignment is less than the type alignment, realign the
788 // argument.
789 if (StackAlign < TypeAlign)
790 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
791 /*Realign=*/true);
792
793 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000794}
795
Rafael Espindolab48280b2012-07-31 02:44:24 +0000796X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
797 const Type *T = isSingleElementStruct(Ty, getContext());
798 if (!T)
799 T = Ty.getTypePtr();
800
801 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
802 BuiltinType::Kind K = BT->getKind();
803 if (K == BuiltinType::Float || K == BuiltinType::Double)
804 return Float;
805 }
806 return Integer;
807}
808
Rafael Espindolab6932692012-10-24 01:58:58 +0000809bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
810 bool IsFastCall) const {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000811 Class C = classify(Ty);
812 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000813 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000814
Rafael Espindolab6932692012-10-24 01:58:58 +0000815 unsigned Size = getContext().getTypeSize(Ty);
816 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000817
818 if (SizeInRegs == 0)
819 return false;
820
Rafael Espindolab48280b2012-07-31 02:44:24 +0000821 if (SizeInRegs > FreeRegs) {
822 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000823 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000824 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000825
Rafael Espindolab48280b2012-07-31 02:44:24 +0000826 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000827
828 if (IsFastCall) {
829 if (Size > 32)
830 return false;
831
832 if (Ty->isIntegralOrEnumerationType())
833 return true;
834
835 if (Ty->isPointerType())
836 return true;
837
838 if (Ty->isReferenceType())
839 return true;
840
841 return false;
842 }
843
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000844 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000845}
846
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000847ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000848 unsigned &FreeRegs,
849 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000850 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000851 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000852 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000853 if (const RecordType *RT = Ty->getAs<RecordType>()) {
854 // Structures with either a non-trivial destructor or a non-trivial
855 // copy constructor are always indirect.
856 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000857 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000858
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000859 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000860 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000861 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862
Eli Friedman5a4d3522011-11-18 00:28:11 +0000863 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000864 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000865 return ABIArgInfo::getIgnore();
866
Rafael Espindolab6932692012-10-24 01:58:58 +0000867 if (shouldUseInReg(Ty, FreeRegs, IsFastCall)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000868 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
869 llvm::LLVMContext &LLVMContext = getVMContext();
870 llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
871 SmallVector<llvm::Type*, 3> Elements;
872 for (unsigned I = 0; I < SizeInRegs; ++I)
873 Elements.push_back(Int32);
874 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
875 return ABIArgInfo::getDirectInReg(Result);
876 }
877
Daniel Dunbar53012f42009-11-09 01:33:53 +0000878 // Expand small (<= 128-bit) record types when we know that the stack layout
879 // of those arguments will match the struct. This is important because the
880 // LLVM backend isn't smart enough to remove byval, which inhibits many
881 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000882 if (getContext().getTypeSize(Ty) <= 4*32 &&
883 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000884 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000885
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000886 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000887 }
888
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000889 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000890 // On Darwin, some vectors are passed in memory, we handle this by passing
891 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000892 if (IsDarwinVectorABI) {
893 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000894 if ((Size == 8 || Size == 16 || Size == 32) ||
895 (Size == 64 && VT->getNumElements() == 1))
896 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
897 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000898 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000899
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000900 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000901 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000902 if (IsMMXDisabled)
903 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
904 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000905 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
906 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
907 return AAI;
908 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000909
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000910 return ABIArgInfo::getDirect();
911 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000912
913
Chris Lattnera3c109b2010-07-29 02:16:43 +0000914 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
915 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000916
Rafael Espindolab6932692012-10-24 01:58:58 +0000917 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000918
919 if (Ty->isPromotableIntegerType()) {
920 if (InReg)
921 return ABIArgInfo::getExtendInReg();
922 return ABIArgInfo::getExtend();
923 }
924 if (InReg)
925 return ABIArgInfo::getDirectInReg();
926 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000927}
928
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000929void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
930 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
931 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000932
Rafael Espindolab6932692012-10-24 01:58:58 +0000933 unsigned CC = FI.getCallingConvention();
934 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
935 unsigned FreeRegs;
936 if (IsFastCall)
937 FreeRegs = 2;
938 else if (FI.getHasRegParm())
939 FreeRegs = FI.getRegParm();
940 else
941 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000942
943 // If the return value is indirect, then the hidden argument is consuming one
944 // integer register.
945 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
946 --FreeRegs;
947 ABIArgInfo &Old = FI.getReturnInfo();
948 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
949 Old.getIndirectByVal(),
950 Old.getIndirectRealign());
951 }
952
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000953 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
954 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000955 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000956}
957
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000958llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
959 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000960 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000961
962 CGBuilderTy &Builder = CGF.Builder;
963 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
964 "ap");
965 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000966
967 // Compute if the address needs to be aligned
968 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
969 Align = getTypeStackAlignInBytes(Ty, Align);
970 Align = std::max(Align, 4U);
971 if (Align > 4) {
972 // addr = (addr + align - 1) & -align;
973 llvm::Value *Offset =
974 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
975 Addr = CGF.Builder.CreateGEP(Addr, Offset);
976 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
977 CGF.Int32Ty);
978 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
979 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
980 Addr->getType(),
981 "ap.cur.aligned");
982 }
983
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000984 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000985 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000986 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
987
988 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000989 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000990 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000991 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000992 "ap.next");
993 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
994
995 return AddrTyped;
996}
997
Charles Davis74f72932010-02-13 15:54:06 +0000998void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
999 llvm::GlobalValue *GV,
1000 CodeGen::CodeGenModule &CGM) const {
1001 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1002 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1003 // Get the LLVM function.
1004 llvm::Function *Fn = cast<llvm::Function>(GV);
1005
1006 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001007 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001008 B.addStackAlignmentAttr(16);
Bill Wendling75d37b42012-10-15 07:31:59 +00001009 Fn->addAttribute(llvm::AttrListPtr::FunctionIndex,
1010 llvm::Attributes::get(CGM.getLLVMContext(), B));
Charles Davis74f72932010-02-13 15:54:06 +00001011 }
1012 }
1013}
1014
John McCall6374c332010-03-06 00:35:14 +00001015bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1016 CodeGen::CodeGenFunction &CGF,
1017 llvm::Value *Address) const {
1018 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001019
Chris Lattner8b418682012-02-07 00:39:47 +00001020 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001021
John McCall6374c332010-03-06 00:35:14 +00001022 // 0-7 are the eight integer registers; the order is different
1023 // on Darwin (for EH), but the range is the same.
1024 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001025 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001026
1027 if (CGF.CGM.isTargetDarwin()) {
1028 // 12-16 are st(0..4). Not sure why we stop at 4.
1029 // These have size 16, which is sizeof(long double) on
1030 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001031 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001032 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001033
John McCall6374c332010-03-06 00:35:14 +00001034 } else {
1035 // 9 is %eflags, which doesn't get a size on Darwin for some
1036 // reason.
1037 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1038
1039 // 11-16 are st(0..5). Not sure why we stop at 5.
1040 // These have size 12, which is sizeof(long double) on
1041 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001042 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001043 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1044 }
John McCall6374c332010-03-06 00:35:14 +00001045
1046 return false;
1047}
1048
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001049//===----------------------------------------------------------------------===//
1050// X86-64 ABI Implementation
1051//===----------------------------------------------------------------------===//
1052
1053
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001054namespace {
1055/// X86_64ABIInfo - The X86_64 ABI information.
1056class X86_64ABIInfo : public ABIInfo {
1057 enum Class {
1058 Integer = 0,
1059 SSE,
1060 SSEUp,
1061 X87,
1062 X87Up,
1063 ComplexX87,
1064 NoClass,
1065 Memory
1066 };
1067
1068 /// merge - Implement the X86_64 ABI merging algorithm.
1069 ///
1070 /// Merge an accumulating classification \arg Accum with a field
1071 /// classification \arg Field.
1072 ///
1073 /// \param Accum - The accumulating classification. This should
1074 /// always be either NoClass or the result of a previous merge
1075 /// call. In addition, this should never be Memory (the caller
1076 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001077 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001078
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001079 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1080 ///
1081 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1082 /// final MEMORY or SSE classes when necessary.
1083 ///
1084 /// \param AggregateSize - The size of the current aggregate in
1085 /// the classification process.
1086 ///
1087 /// \param Lo - The classification for the parts of the type
1088 /// residing in the low word of the containing object.
1089 ///
1090 /// \param Hi - The classification for the parts of the type
1091 /// residing in the higher words of the containing object.
1092 ///
1093 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1094
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001095 /// classify - Determine the x86_64 register classes in which the
1096 /// given type T should be passed.
1097 ///
1098 /// \param Lo - The classification for the parts of the type
1099 /// residing in the low word of the containing object.
1100 ///
1101 /// \param Hi - The classification for the parts of the type
1102 /// residing in the high word of the containing object.
1103 ///
1104 /// \param OffsetBase - The bit offset of this type in the
1105 /// containing object. Some parameters are classified different
1106 /// depending on whether they straddle an eightbyte boundary.
1107 ///
1108 /// If a word is unused its result will be NoClass; if a type should
1109 /// be passed in Memory then at least the classification of \arg Lo
1110 /// will be Memory.
1111 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001112 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001113 ///
1114 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1115 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001116 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001117
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001118 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001119 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1120 unsigned IROffset, QualType SourceTy,
1121 unsigned SourceOffset) const;
1122 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1123 unsigned IROffset, QualType SourceTy,
1124 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001125
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001126 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001127 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001128 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001129
1130 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001131 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001132 ///
1133 /// \param freeIntRegs - The number of free integer registers remaining
1134 /// available.
1135 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001136
Chris Lattnera3c109b2010-07-29 02:16:43 +00001137 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138
Bill Wendlingbb465d72010-10-18 03:41:31 +00001139 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001140 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001141 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001142 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001143
Eli Friedmanee1ad992011-12-02 00:11:43 +00001144 bool IsIllegalVectorType(QualType Ty) const;
1145
John McCall67a57732011-04-21 01:20:55 +00001146 /// The 0.98 ABI revision clarified a lot of ambiguities,
1147 /// unfortunately in ways that were not always consistent with
1148 /// certain previous compilers. In particular, platforms which
1149 /// required strict binary compatibility with older versions of GCC
1150 /// may need to exempt themselves.
1151 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001152 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001153 }
1154
Eli Friedmanee1ad992011-12-02 00:11:43 +00001155 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001156 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1157 // 64-bit hardware.
1158 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001159
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001160public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001161 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001162 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001163 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001164 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001165
John McCallde5d3c72012-02-17 03:33:10 +00001166 bool isPassedUsingAVXType(QualType type) const {
1167 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001168 // The freeIntRegs argument doesn't matter here.
1169 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001170 if (info.isDirect()) {
1171 llvm::Type *ty = info.getCoerceToType();
1172 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1173 return (vectorTy->getBitWidth() > 128);
1174 }
1175 return false;
1176 }
1177
Chris Lattneree5dcd02010-07-29 02:31:05 +00001178 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001179
1180 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1181 CodeGenFunction &CGF) const;
1182};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001183
Chris Lattnerf13721d2010-08-31 16:44:54 +00001184/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001185class WinX86_64ABIInfo : public ABIInfo {
1186
1187 ABIArgInfo classify(QualType Ty) const;
1188
Chris Lattnerf13721d2010-08-31 16:44:54 +00001189public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001190 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1191
1192 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001193
1194 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1195 CodeGenFunction &CGF) const;
1196};
1197
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001198class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1199public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001200 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001201 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001202
John McCallde5d3c72012-02-17 03:33:10 +00001203 const X86_64ABIInfo &getABIInfo() const {
1204 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1205 }
1206
John McCall6374c332010-03-06 00:35:14 +00001207 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1208 return 7;
1209 }
1210
1211 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1212 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001213 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001214
John McCallaeeb7012010-05-27 06:19:26 +00001215 // 0-15 are the 16 integer registers.
1216 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001217 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001218 return false;
1219 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001220
Jay Foadef6de3d2011-07-11 09:56:20 +00001221 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001222 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001223 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001224 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1225 }
1226
John McCallde5d3c72012-02-17 03:33:10 +00001227 bool isNoProtoCallVariadic(const CallArgList &args,
1228 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001229 // The default CC on x86-64 sets %al to the number of SSA
1230 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001231 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001232 // that when AVX types are involved: the ABI explicitly states it is
1233 // undefined, and it doesn't work in practice because of how the ABI
1234 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001235 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001236 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001237 for (CallArgList::const_iterator
1238 it = args.begin(), ie = args.end(); it != ie; ++it) {
1239 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1240 HasAVXType = true;
1241 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001242 }
1243 }
John McCallde5d3c72012-02-17 03:33:10 +00001244
Eli Friedman3ed79032011-12-01 04:53:19 +00001245 if (!HasAVXType)
1246 return true;
1247 }
John McCall01f151e2011-09-21 08:08:30 +00001248
John McCallde5d3c72012-02-17 03:33:10 +00001249 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001250 }
1251
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001252};
1253
Chris Lattnerf13721d2010-08-31 16:44:54 +00001254class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1255public:
1256 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1257 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1258
1259 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1260 return 7;
1261 }
1262
1263 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1264 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001265 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001266
Chris Lattnerf13721d2010-08-31 16:44:54 +00001267 // 0-15 are the 16 integer registers.
1268 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001269 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001270 return false;
1271 }
1272};
1273
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001274}
1275
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001276void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1277 Class &Hi) const {
1278 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1279 //
1280 // (a) If one of the classes is Memory, the whole argument is passed in
1281 // memory.
1282 //
1283 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1284 // memory.
1285 //
1286 // (c) If the size of the aggregate exceeds two eightbytes and the first
1287 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1288 // argument is passed in memory. NOTE: This is necessary to keep the
1289 // ABI working for processors that don't support the __m256 type.
1290 //
1291 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1292 //
1293 // Some of these are enforced by the merging logic. Others can arise
1294 // only with unions; for example:
1295 // union { _Complex double; unsigned; }
1296 //
1297 // Note that clauses (b) and (c) were added in 0.98.
1298 //
1299 if (Hi == Memory)
1300 Lo = Memory;
1301 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1302 Lo = Memory;
1303 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1304 Lo = Memory;
1305 if (Hi == SSEUp && Lo != SSE)
1306 Hi = SSE;
1307}
1308
Chris Lattner1090a9b2010-06-28 21:43:59 +00001309X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001310 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1311 // classified recursively so that always two fields are
1312 // considered. The resulting class is calculated according to
1313 // the classes of the fields in the eightbyte:
1314 //
1315 // (a) If both classes are equal, this is the resulting class.
1316 //
1317 // (b) If one of the classes is NO_CLASS, the resulting class is
1318 // the other class.
1319 //
1320 // (c) If one of the classes is MEMORY, the result is the MEMORY
1321 // class.
1322 //
1323 // (d) If one of the classes is INTEGER, the result is the
1324 // INTEGER.
1325 //
1326 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1327 // MEMORY is used as class.
1328 //
1329 // (f) Otherwise class SSE is used.
1330
1331 // Accum should never be memory (we should have returned) or
1332 // ComplexX87 (because this cannot be passed in a structure).
1333 assert((Accum != Memory && Accum != ComplexX87) &&
1334 "Invalid accumulated classification during merge.");
1335 if (Accum == Field || Field == NoClass)
1336 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001337 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001338 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001339 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001341 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001342 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001343 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1344 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001346 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347}
1348
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001349void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001350 Class &Lo, Class &Hi) const {
1351 // FIXME: This code can be simplified by introducing a simple value class for
1352 // Class pairs with appropriate constructor methods for the various
1353 // situations.
1354
1355 // FIXME: Some of the split computations are wrong; unaligned vectors
1356 // shouldn't be passed in registers for example, so there is no chance they
1357 // can straddle an eightbyte. Verify & simplify.
1358
1359 Lo = Hi = NoClass;
1360
1361 Class &Current = OffsetBase < 64 ? Lo : Hi;
1362 Current = Memory;
1363
John McCall183700f2009-09-21 23:43:11 +00001364 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001365 BuiltinType::Kind k = BT->getKind();
1366
1367 if (k == BuiltinType::Void) {
1368 Current = NoClass;
1369 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1370 Lo = Integer;
1371 Hi = Integer;
1372 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1373 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001374 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1375 (k == BuiltinType::LongDouble &&
1376 getContext().getTargetInfo().getTriple().getOS() ==
1377 llvm::Triple::NativeClient)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001378 Current = SSE;
1379 } else if (k == BuiltinType::LongDouble) {
1380 Lo = X87;
1381 Hi = X87Up;
1382 }
1383 // FIXME: _Decimal32 and _Decimal64 are SSE.
1384 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001385 return;
1386 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001387
Chris Lattner1090a9b2010-06-28 21:43:59 +00001388 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001389 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001390 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001391 return;
1392 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001393
Chris Lattner1090a9b2010-06-28 21:43:59 +00001394 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001395 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001396 return;
1397 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001398
Chris Lattner1090a9b2010-06-28 21:43:59 +00001399 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001400 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001401 Lo = Hi = Integer;
1402 else
1403 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001404 return;
1405 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001406
Chris Lattner1090a9b2010-06-28 21:43:59 +00001407 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001408 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001409 if (Size == 32) {
1410 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1411 // float> as integer.
1412 Current = Integer;
1413
1414 // If this type crosses an eightbyte boundary, it should be
1415 // split.
1416 uint64_t EB_Real = (OffsetBase) / 64;
1417 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1418 if (EB_Real != EB_Imag)
1419 Hi = Lo;
1420 } else if (Size == 64) {
1421 // gcc passes <1 x double> in memory. :(
1422 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1423 return;
1424
1425 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001426 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001427 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1428 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1429 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001430 Current = Integer;
1431 else
1432 Current = SSE;
1433
1434 // If this type crosses an eightbyte boundary, it should be
1435 // split.
1436 if (OffsetBase && OffsetBase != 64)
1437 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001438 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001439 // Arguments of 256-bits are split into four eightbyte chunks. The
1440 // least significant one belongs to class SSE and all the others to class
1441 // SSEUP. The original Lo and Hi design considers that types can't be
1442 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1443 // This design isn't correct for 256-bits, but since there're no cases
1444 // where the upper parts would need to be inspected, avoid adding
1445 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001446 Lo = SSE;
1447 Hi = SSEUp;
1448 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001449 return;
1450 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001451
Chris Lattner1090a9b2010-06-28 21:43:59 +00001452 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001453 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001454
Chris Lattnerea044322010-07-29 02:01:43 +00001455 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001456 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001457 if (Size <= 64)
1458 Current = Integer;
1459 else if (Size <= 128)
1460 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001461 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001462 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001463 else if (ET == getContext().DoubleTy ||
1464 (ET == getContext().LongDoubleTy &&
1465 getContext().getTargetInfo().getTriple().getOS() ==
1466 llvm::Triple::NativeClient))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001467 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001468 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469 Current = ComplexX87;
1470
1471 // If this complex type crosses an eightbyte boundary then it
1472 // should be split.
1473 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001474 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001475 if (Hi == NoClass && EB_Real != EB_Imag)
1476 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001477
Chris Lattner1090a9b2010-06-28 21:43:59 +00001478 return;
1479 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001480
Chris Lattnerea044322010-07-29 02:01:43 +00001481 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 // Arrays are treated like structures.
1483
Chris Lattnerea044322010-07-29 02:01:43 +00001484 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001485
1486 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001487 // than four eightbytes, ..., it has class MEMORY.
1488 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001489 return;
1490
1491 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1492 // fields, it has class MEMORY.
1493 //
1494 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001495 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001496 return;
1497
1498 // Otherwise implement simplified merge. We could be smarter about
1499 // this, but it isn't worth it and would be harder to verify.
1500 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001501 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001502 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001503
1504 // The only case a 256-bit wide vector could be used is when the array
1505 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1506 // to work for sizes wider than 128, early check and fallback to memory.
1507 if (Size > 128 && EltSize != 256)
1508 return;
1509
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001510 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1511 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001512 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001513 Lo = merge(Lo, FieldLo);
1514 Hi = merge(Hi, FieldHi);
1515 if (Lo == Memory || Hi == Memory)
1516 break;
1517 }
1518
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001519 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001520 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001521 return;
1522 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001523
Chris Lattner1090a9b2010-06-28 21:43:59 +00001524 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001525 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526
1527 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001528 // than four eightbytes, ..., it has class MEMORY.
1529 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001530 return;
1531
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001532 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1533 // copy constructor or a non-trivial destructor, it is passed by invisible
1534 // reference.
1535 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1536 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001537
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001538 const RecordDecl *RD = RT->getDecl();
1539
1540 // Assume variable sized types are passed in memory.
1541 if (RD->hasFlexibleArrayMember())
1542 return;
1543
Chris Lattnerea044322010-07-29 02:01:43 +00001544 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545
1546 // Reset Lo class, this will be recomputed.
1547 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001548
1549 // If this is a C++ record, classify the bases first.
1550 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1551 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1552 e = CXXRD->bases_end(); i != e; ++i) {
1553 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1554 "Unexpected base class!");
1555 const CXXRecordDecl *Base =
1556 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1557
1558 // Classify this field.
1559 //
1560 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1561 // single eightbyte, each is classified separately. Each eightbyte gets
1562 // initialized to class NO_CLASS.
1563 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001564 uint64_t Offset =
1565 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001566 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001567 Lo = merge(Lo, FieldLo);
1568 Hi = merge(Hi, FieldHi);
1569 if (Lo == Memory || Hi == Memory)
1570 break;
1571 }
1572 }
1573
1574 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001575 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001576 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001577 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001578 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1579 bool BitField = i->isBitField();
1580
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001581 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1582 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001583 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001584 // The only case a 256-bit wide vector could be used is when the struct
1585 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1586 // to work for sizes wider than 128, early check and fallback to memory.
1587 //
1588 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1589 Lo = Memory;
1590 return;
1591 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001592 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001593 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001594 Lo = Memory;
1595 return;
1596 }
1597
1598 // Classify this field.
1599 //
1600 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1601 // exceeds a single eightbyte, each is classified
1602 // separately. Each eightbyte gets initialized to class
1603 // NO_CLASS.
1604 Class FieldLo, FieldHi;
1605
1606 // Bit-fields require special handling, they do not force the
1607 // structure to be passed in memory even if unaligned, and
1608 // therefore they can straddle an eightbyte.
1609 if (BitField) {
1610 // Ignore padding bit-fields.
1611 if (i->isUnnamedBitfield())
1612 continue;
1613
1614 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001615 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001616
1617 uint64_t EB_Lo = Offset / 64;
1618 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1619 FieldLo = FieldHi = NoClass;
1620 if (EB_Lo) {
1621 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1622 FieldLo = NoClass;
1623 FieldHi = Integer;
1624 } else {
1625 FieldLo = Integer;
1626 FieldHi = EB_Hi ? Integer : NoClass;
1627 }
1628 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001629 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001630 Lo = merge(Lo, FieldLo);
1631 Hi = merge(Hi, FieldHi);
1632 if (Lo == Memory || Hi == Memory)
1633 break;
1634 }
1635
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001636 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001637 }
1638}
1639
Chris Lattner9c254f02010-06-29 06:01:59 +00001640ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001641 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1642 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001643 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001644 // Treat an enum type as its underlying type.
1645 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1646 Ty = EnumTy->getDecl()->getIntegerType();
1647
1648 return (Ty->isPromotableIntegerType() ?
1649 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1650 }
1651
1652 return ABIArgInfo::getIndirect(0);
1653}
1654
Eli Friedmanee1ad992011-12-02 00:11:43 +00001655bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1656 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1657 uint64_t Size = getContext().getTypeSize(VecTy);
1658 unsigned LargestVector = HasAVX ? 256 : 128;
1659 if (Size <= 64 || Size > LargestVector)
1660 return true;
1661 }
1662
1663 return false;
1664}
1665
Daniel Dunbaredfac032012-03-10 01:03:58 +00001666ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1667 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001668 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1669 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001670 //
1671 // This assumption is optimistic, as there could be free registers available
1672 // when we need to pass this argument in memory, and LLVM could try to pass
1673 // the argument in the free register. This does not seem to happen currently,
1674 // but this code would be much safer if we could mark the argument with
1675 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001676 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001677 // Treat an enum type as its underlying type.
1678 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1679 Ty = EnumTy->getDecl()->getIntegerType();
1680
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001681 return (Ty->isPromotableIntegerType() ?
1682 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001683 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001684
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001685 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1686 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001687
Chris Lattner855d2272011-05-22 23:21:23 +00001688 // Compute the byval alignment. We specify the alignment of the byval in all
1689 // cases so that the mid-level optimizer knows the alignment of the byval.
1690 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001691
1692 // Attempt to avoid passing indirect results using byval when possible. This
1693 // is important for good codegen.
1694 //
1695 // We do this by coercing the value into a scalar type which the backend can
1696 // handle naturally (i.e., without using byval).
1697 //
1698 // For simplicity, we currently only do this when we have exhausted all of the
1699 // free integer registers. Doing this when there are free integer registers
1700 // would require more care, as we would have to ensure that the coerced value
1701 // did not claim the unused register. That would require either reording the
1702 // arguments to the function (so that any subsequent inreg values came first),
1703 // or only doing this optimization when there were no following arguments that
1704 // might be inreg.
1705 //
1706 // We currently expect it to be rare (particularly in well written code) for
1707 // arguments to be passed on the stack when there are still free integer
1708 // registers available (this would typically imply large structs being passed
1709 // by value), so this seems like a fair tradeoff for now.
1710 //
1711 // We can revisit this if the backend grows support for 'onstack' parameter
1712 // attributes. See PR12193.
1713 if (freeIntRegs == 0) {
1714 uint64_t Size = getContext().getTypeSize(Ty);
1715
1716 // If this type fits in an eightbyte, coerce it into the matching integral
1717 // type, which will end up on the stack (with alignment 8).
1718 if (Align == 8 && Size <= 64)
1719 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1720 Size));
1721 }
1722
Chris Lattner855d2272011-05-22 23:21:23 +00001723 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001724}
1725
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001726/// GetByteVectorType - The ABI specifies that a value should be passed in an
1727/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001728/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001729llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001730 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001731
Chris Lattner15842bd2010-07-29 05:02:29 +00001732 // Wrapper structs that just contain vectors are passed just like vectors,
1733 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001734 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001735 while (STy && STy->getNumElements() == 1) {
1736 IRType = STy->getElementType(0);
1737 STy = dyn_cast<llvm::StructType>(IRType);
1738 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001739
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001740 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001741 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1742 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001743 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001744 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001745 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1746 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1747 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1748 EltTy->isIntegerTy(128)))
1749 return VT;
1750 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001751
Chris Lattner0f408f52010-07-29 04:56:46 +00001752 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1753}
1754
Chris Lattnere2962be2010-07-29 07:30:00 +00001755/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1756/// is known to either be off the end of the specified type or being in
1757/// alignment padding. The user type specified is known to be at most 128 bits
1758/// in size, and have passed through X86_64ABIInfo::classify with a successful
1759/// classification that put one of the two halves in the INTEGER class.
1760///
1761/// It is conservatively correct to return false.
1762static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1763 unsigned EndBit, ASTContext &Context) {
1764 // If the bytes being queried are off the end of the type, there is no user
1765 // data hiding here. This handles analysis of builtins, vectors and other
1766 // types that don't contain interesting padding.
1767 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1768 if (TySize <= StartBit)
1769 return true;
1770
Chris Lattner021c3a32010-07-29 07:43:55 +00001771 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1772 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1773 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1774
1775 // Check each element to see if the element overlaps with the queried range.
1776 for (unsigned i = 0; i != NumElts; ++i) {
1777 // If the element is after the span we care about, then we're done..
1778 unsigned EltOffset = i*EltSize;
1779 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001780
Chris Lattner021c3a32010-07-29 07:43:55 +00001781 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1782 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1783 EndBit-EltOffset, Context))
1784 return false;
1785 }
1786 // If it overlaps no elements, then it is safe to process as padding.
1787 return true;
1788 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001789
Chris Lattnere2962be2010-07-29 07:30:00 +00001790 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1791 const RecordDecl *RD = RT->getDecl();
1792 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
Chris Lattnere2962be2010-07-29 07:30:00 +00001794 // If this is a C++ record, check the bases first.
1795 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1796 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1797 e = CXXRD->bases_end(); i != e; ++i) {
1798 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1799 "Unexpected base class!");
1800 const CXXRecordDecl *Base =
1801 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001802
Chris Lattnere2962be2010-07-29 07:30:00 +00001803 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001804 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001805 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001806
Chris Lattnere2962be2010-07-29 07:30:00 +00001807 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1808 if (!BitsContainNoUserData(i->getType(), BaseStart,
1809 EndBit-BaseOffset, Context))
1810 return false;
1811 }
1812 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001813
Chris Lattnere2962be2010-07-29 07:30:00 +00001814 // Verify that no field has data that overlaps the region of interest. Yes
1815 // this could be sped up a lot by being smarter about queried fields,
1816 // however we're only looking at structs up to 16 bytes, so we don't care
1817 // much.
1818 unsigned idx = 0;
1819 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1820 i != e; ++i, ++idx) {
1821 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001822
Chris Lattnere2962be2010-07-29 07:30:00 +00001823 // If we found a field after the region we care about, then we're done.
1824 if (FieldOffset >= EndBit) break;
1825
1826 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1827 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1828 Context))
1829 return false;
1830 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001831
Chris Lattnere2962be2010-07-29 07:30:00 +00001832 // If nothing in this record overlapped the area of interest, then we're
1833 // clean.
1834 return true;
1835 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001836
Chris Lattnere2962be2010-07-29 07:30:00 +00001837 return false;
1838}
1839
Chris Lattner0b362002010-07-29 18:39:32 +00001840/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1841/// float member at the specified offset. For example, {int,{float}} has a
1842/// float at offset 4. It is conservatively correct for this routine to return
1843/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001844static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001845 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001846 // Base case if we find a float.
1847 if (IROffset == 0 && IRType->isFloatTy())
1848 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001849
Chris Lattner0b362002010-07-29 18:39:32 +00001850 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001851 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001852 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1853 unsigned Elt = SL->getElementContainingOffset(IROffset);
1854 IROffset -= SL->getElementOffset(Elt);
1855 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1856 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001857
Chris Lattner0b362002010-07-29 18:39:32 +00001858 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001859 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1860 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001861 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1862 IROffset -= IROffset/EltSize*EltSize;
1863 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1864 }
1865
1866 return false;
1867}
1868
Chris Lattnerf47c9442010-07-29 18:13:09 +00001869
1870/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1871/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001872llvm::Type *X86_64ABIInfo::
1873GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001874 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001875 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001876 // pass as float if the last 4 bytes is just padding. This happens for
1877 // structs that contain 3 floats.
1878 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1879 SourceOffset*8+64, getContext()))
1880 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001881
Chris Lattner0b362002010-07-29 18:39:32 +00001882 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1883 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1884 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001885 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1886 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001887 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001888
Chris Lattnerf47c9442010-07-29 18:13:09 +00001889 return llvm::Type::getDoubleTy(getVMContext());
1890}
1891
1892
Chris Lattner0d2656d2010-07-29 17:40:35 +00001893/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1894/// an 8-byte GPR. This means that we either have a scalar or we are talking
1895/// about the high or low part of an up-to-16-byte struct. This routine picks
1896/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001897/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1898/// etc).
1899///
1900/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1901/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1902/// the 8-byte value references. PrefType may be null.
1903///
1904/// SourceTy is the source level type for the entire argument. SourceOffset is
1905/// an offset into this that we're processing (which is always either 0 or 8).
1906///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001907llvm::Type *X86_64ABIInfo::
1908GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001909 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001910 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1911 // returning an 8-byte unit starting with it. See if we can safely use it.
1912 if (IROffset == 0) {
1913 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001914 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1915 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001916 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001917
Chris Lattnere2962be2010-07-29 07:30:00 +00001918 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1919 // goodness in the source type is just tail padding. This is allowed to
1920 // kick in for struct {double,int} on the int, but not on
1921 // struct{double,int,int} because we wouldn't return the second int. We
1922 // have to do this analysis on the source type because we can't depend on
1923 // unions being lowered a specific way etc.
1924 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001925 IRType->isIntegerTy(32) ||
1926 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1927 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1928 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001929
Chris Lattnere2962be2010-07-29 07:30:00 +00001930 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1931 SourceOffset*8+64, getContext()))
1932 return IRType;
1933 }
1934 }
Chris Lattner49382de2010-07-28 22:44:07 +00001935
Chris Lattner2acc6e32011-07-18 04:24:23 +00001936 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001937 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001938 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001939 if (IROffset < SL->getSizeInBytes()) {
1940 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1941 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001942
Chris Lattner0d2656d2010-07-29 17:40:35 +00001943 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1944 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001945 }
Chris Lattner49382de2010-07-28 22:44:07 +00001946 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001947
Chris Lattner2acc6e32011-07-18 04:24:23 +00001948 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001949 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001950 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001951 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001952 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1953 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001954 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001955
Chris Lattner49382de2010-07-28 22:44:07 +00001956 // Okay, we don't have any better idea of what to pass, so we pass this in an
1957 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001958 unsigned TySizeInBytes =
1959 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001960
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001961 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001962
Chris Lattner49382de2010-07-28 22:44:07 +00001963 // It is always safe to classify this as an integer type up to i64 that
1964 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001965 return llvm::IntegerType::get(getVMContext(),
1966 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001967}
1968
Chris Lattner66e7b682010-09-01 00:50:20 +00001969
1970/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1971/// be used as elements of a two register pair to pass or return, return a
1972/// first class aggregate to represent them. For example, if the low part of
1973/// a by-value argument should be passed as i32* and the high part as float,
1974/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001975static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001976GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001977 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001978 // In order to correctly satisfy the ABI, we need to the high part to start
1979 // at offset 8. If the high and low parts we inferred are both 4-byte types
1980 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1981 // the second element at offset 8. Check for this:
1982 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1983 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001984 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001985 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001986
Chris Lattner66e7b682010-09-01 00:50:20 +00001987 // To handle this, we have to increase the size of the low part so that the
1988 // second element will start at an 8 byte offset. We can't increase the size
1989 // of the second element because it might make us access off the end of the
1990 // struct.
1991 if (HiStart != 8) {
1992 // There are only two sorts of types the ABI generation code can produce for
1993 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1994 // Promote these to a larger type.
1995 if (Lo->isFloatTy())
1996 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1997 else {
1998 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1999 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2000 }
2001 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002002
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002003 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002004
2005
Chris Lattner66e7b682010-09-01 00:50:20 +00002006 // Verify that the second element is at an 8-byte offset.
2007 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2008 "Invalid x86-64 argument pair!");
2009 return Result;
2010}
2011
Chris Lattner519f68c2010-07-28 23:06:14 +00002012ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002013classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002014 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2015 // classification algorithm.
2016 X86_64ABIInfo::Class Lo, Hi;
2017 classify(RetTy, 0, Lo, Hi);
2018
2019 // Check some invariants.
2020 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002021 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2022
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002023 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002024 switch (Lo) {
2025 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002026 if (Hi == NoClass)
2027 return ABIArgInfo::getIgnore();
2028 // If the low part is just padding, it takes no register, leave ResType
2029 // null.
2030 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2031 "Unknown missing lo part");
2032 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002033
2034 case SSEUp:
2035 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002036 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002037
2038 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2039 // hidden argument.
2040 case Memory:
2041 return getIndirectReturnResult(RetTy);
2042
2043 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2044 // available register of the sequence %rax, %rdx is used.
2045 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002046 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002047
Chris Lattnereb518b42010-07-29 21:42:50 +00002048 // If we have a sign or zero extended integer, make sure to return Extend
2049 // so that the parameter gets the right LLVM IR attributes.
2050 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2051 // Treat an enum type as its underlying type.
2052 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2053 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002054
Chris Lattnereb518b42010-07-29 21:42:50 +00002055 if (RetTy->isIntegralOrEnumerationType() &&
2056 RetTy->isPromotableIntegerType())
2057 return ABIArgInfo::getExtend();
2058 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002059 break;
2060
2061 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2062 // available SSE register of the sequence %xmm0, %xmm1 is used.
2063 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002064 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002065 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002066
2067 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2068 // returned on the X87 stack in %st0 as 80-bit x87 number.
2069 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002070 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002071 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002072
2073 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2074 // part of the value is returned in %st0 and the imaginary part in
2075 // %st1.
2076 case ComplexX87:
2077 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002078 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002079 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002080 NULL);
2081 break;
2082 }
2083
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002084 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002085 switch (Hi) {
2086 // Memory was handled previously and X87 should
2087 // never occur as a hi class.
2088 case Memory:
2089 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002090 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002091
2092 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002093 case NoClass:
2094 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002095
Chris Lattner3db4dde2010-09-01 00:20:33 +00002096 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002097 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002098 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2099 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002100 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002101 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002102 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002103 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2104 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002105 break;
2106
2107 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002108 // is passed in the next available eightbyte chunk if the last used
2109 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002110 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002111 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002112 case SSEUp:
2113 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002114 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002115 break;
2116
2117 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2118 // returned together with the previous X87 value in %st0.
2119 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002120 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002121 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002122 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002123 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002124 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002125 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002126 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2127 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002128 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002129 break;
2130 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002131
Chris Lattner3db4dde2010-09-01 00:20:33 +00002132 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002133 // known to pass in the high eightbyte of the result. We do this by forming a
2134 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002135 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002136 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002137
Chris Lattnereb518b42010-07-29 21:42:50 +00002138 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002139}
2140
Daniel Dunbaredfac032012-03-10 01:03:58 +00002141ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2142 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2143 const
2144{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002145 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002146 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002147
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002148 // Check some invariants.
2149 // FIXME: Enforce these by construction.
2150 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002151 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2152
2153 neededInt = 0;
2154 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002155 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002156 switch (Lo) {
2157 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002158 if (Hi == NoClass)
2159 return ABIArgInfo::getIgnore();
2160 // If the low part is just padding, it takes no register, leave ResType
2161 // null.
2162 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2163 "Unknown missing lo part");
2164 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002165
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002166 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2167 // on the stack.
2168 case Memory:
2169
2170 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2171 // COMPLEX_X87, it is passed in memory.
2172 case X87:
2173 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002174 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2175 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002176 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002177
2178 case SSEUp:
2179 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002180 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181
2182 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2183 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2184 // and %r9 is used.
2185 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002186 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002187
Chris Lattner49382de2010-07-28 22:44:07 +00002188 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002189 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002190
2191 // If we have a sign or zero extended integer, make sure to return Extend
2192 // so that the parameter gets the right LLVM IR attributes.
2193 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2194 // Treat an enum type as its underlying type.
2195 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2196 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002197
Chris Lattnereb518b42010-07-29 21:42:50 +00002198 if (Ty->isIntegralOrEnumerationType() &&
2199 Ty->isPromotableIntegerType())
2200 return ABIArgInfo::getExtend();
2201 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002202
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002203 break;
2204
2205 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2206 // available SSE register is used, the registers are taken in the
2207 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002208 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002209 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002210 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002211 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002212 break;
2213 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002214 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002215
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002216 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002217 switch (Hi) {
2218 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002219 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002220 // which is passed in memory.
2221 case Memory:
2222 case X87:
2223 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002224 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225
2226 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002227
Chris Lattner645406a2010-09-01 00:24:35 +00002228 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002229 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002230 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002231 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002232
Chris Lattner645406a2010-09-01 00:24:35 +00002233 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2234 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002235 break;
2236
2237 // X87Up generally doesn't occur here (long double is passed in
2238 // memory), except in situations involving unions.
2239 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002240 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002241 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002242
Chris Lattner645406a2010-09-01 00:24:35 +00002243 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2244 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002245
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002246 ++neededSSE;
2247 break;
2248
2249 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2250 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002251 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002252 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002253 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002254 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002255 break;
2256 }
2257
Chris Lattner645406a2010-09-01 00:24:35 +00002258 // If a high part was specified, merge it together with the low part. It is
2259 // known to pass in the high eightbyte of the result. We do this by forming a
2260 // first class struct aggregate with the high and low part: {low, high}
2261 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002262 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002263
Chris Lattnereb518b42010-07-29 21:42:50 +00002264 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002265}
2266
Chris Lattneree5dcd02010-07-29 02:31:05 +00002267void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002268
Chris Lattnera3c109b2010-07-29 02:16:43 +00002269 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002270
2271 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002272 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002273
2274 // If the return value is indirect, then the hidden argument is consuming one
2275 // integer register.
2276 if (FI.getReturnInfo().isIndirect())
2277 --freeIntRegs;
2278
2279 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2280 // get assigned (in left-to-right order) for passing as follows...
2281 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2282 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002283 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002284 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2285 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002286
2287 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2288 // eightbyte of an argument, the whole argument is passed on the
2289 // stack. If registers have already been assigned for some
2290 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002291 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292 freeIntRegs -= neededInt;
2293 freeSSERegs -= neededSSE;
2294 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002295 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002296 }
2297 }
2298}
2299
2300static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2301 QualType Ty,
2302 CodeGenFunction &CGF) {
2303 llvm::Value *overflow_arg_area_p =
2304 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2305 llvm::Value *overflow_arg_area =
2306 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2307
2308 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2309 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002310 // It isn't stated explicitly in the standard, but in practice we use
2311 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002312 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2313 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002314 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002315 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002316 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002317 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2318 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002319 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002320 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002321 overflow_arg_area =
2322 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2323 overflow_arg_area->getType(),
2324 "overflow_arg_area.align");
2325 }
2326
2327 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002328 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002329 llvm::Value *Res =
2330 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002331 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002332
2333 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2334 // l->overflow_arg_area + sizeof(type).
2335 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2336 // an 8 byte boundary.
2337
2338 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002339 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002340 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002341 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2342 "overflow_arg_area.next");
2343 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2344
2345 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2346 return Res;
2347}
2348
2349llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2350 CodeGenFunction &CGF) const {
2351 // Assume that va_list type is correct; should be pointer to LLVM type:
2352 // struct {
2353 // i32 gp_offset;
2354 // i32 fp_offset;
2355 // i8* overflow_arg_area;
2356 // i8* reg_save_area;
2357 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002358 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002359
Chris Lattnera14db752010-03-11 18:19:55 +00002360 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002361 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002362
2363 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2364 // in the registers. If not go to step 7.
2365 if (!neededInt && !neededSSE)
2366 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2367
2368 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2369 // general purpose registers needed to pass type and num_fp to hold
2370 // the number of floating point registers needed.
2371
2372 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2373 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2374 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2375 //
2376 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2377 // register save space).
2378
2379 llvm::Value *InRegs = 0;
2380 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2381 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2382 if (neededInt) {
2383 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2384 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002385 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2386 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002387 }
2388
2389 if (neededSSE) {
2390 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2391 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2392 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002393 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2394 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002395 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2396 }
2397
2398 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2399 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2400 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2401 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2402
2403 // Emit code to load the value if it was passed in registers.
2404
2405 CGF.EmitBlock(InRegBlock);
2406
2407 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2408 // an offset of l->gp_offset and/or l->fp_offset. This may require
2409 // copying to a temporary location in case the parameter is passed
2410 // in different register classes or requires an alignment greater
2411 // than 8 for general purpose registers and 16 for XMM registers.
2412 //
2413 // FIXME: This really results in shameful code when we end up needing to
2414 // collect arguments from different places; often what should result in a
2415 // simple assembling of a structure from scattered addresses has many more
2416 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002417 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002418 llvm::Value *RegAddr =
2419 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2420 "reg_save_area");
2421 if (neededInt && neededSSE) {
2422 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002423 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002424 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002425 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2426 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002427 llvm::Type *TyLo = ST->getElementType(0);
2428 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002429 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002430 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002431 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2432 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002433 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2434 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002435 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2436 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002437 llvm::Value *V =
2438 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2439 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2440 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2441 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2442
Owen Andersona1cf15f2009-07-14 23:10:40 +00002443 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002444 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002445 } else if (neededInt) {
2446 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2447 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002448 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002449 } else if (neededSSE == 1) {
2450 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2451 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2452 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002453 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002454 assert(neededSSE == 2 && "Invalid number of needed registers!");
2455 // SSE registers are spaced 16 bytes apart in the register save
2456 // area, we need to collect the two eightbytes together.
2457 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002458 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002459 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002460 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002461 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002462 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002463 DoubleTy, NULL);
2464 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2465 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2466 DblPtrTy));
2467 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2468 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2469 DblPtrTy));
2470 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2471 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2472 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002473 }
2474
2475 // AMD64-ABI 3.5.7p5: Step 5. Set:
2476 // l->gp_offset = l->gp_offset + num_gp * 8
2477 // l->fp_offset = l->fp_offset + num_fp * 16.
2478 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002479 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002480 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2481 gp_offset_p);
2482 }
2483 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002484 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002485 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2486 fp_offset_p);
2487 }
2488 CGF.EmitBranch(ContBlock);
2489
2490 // Emit code to load the value if it was passed in memory.
2491
2492 CGF.EmitBlock(InMemBlock);
2493 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2494
2495 // Return the appropriate result.
2496
2497 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002498 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002499 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002500 ResAddr->addIncoming(RegAddr, InRegBlock);
2501 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002502 return ResAddr;
2503}
2504
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002505ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2506
2507 if (Ty->isVoidType())
2508 return ABIArgInfo::getIgnore();
2509
2510 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2511 Ty = EnumTy->getDecl()->getIntegerType();
2512
2513 uint64_t Size = getContext().getTypeSize(Ty);
2514
2515 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002516 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2517 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002518 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2519
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002520 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2521 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002522 getContext().getTargetInfo().getTriple().getOS()
2523 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002524 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2525 Size));
2526
2527 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2528 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2529 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002530 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002531 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2532 Size));
2533
2534 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2535 }
2536
2537 if (Ty->isPromotableIntegerType())
2538 return ABIArgInfo::getExtend();
2539
2540 return ABIArgInfo::getDirect();
2541}
2542
2543void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2544
2545 QualType RetTy = FI.getReturnType();
2546 FI.getReturnInfo() = classify(RetTy);
2547
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002548 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2549 it != ie; ++it)
2550 it->info = classify(it->type);
2551}
2552
Chris Lattnerf13721d2010-08-31 16:44:54 +00002553llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2554 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002555 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002556
Chris Lattnerf13721d2010-08-31 16:44:54 +00002557 CGBuilderTy &Builder = CGF.Builder;
2558 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2559 "ap");
2560 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2561 llvm::Type *PTy =
2562 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2563 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2564
2565 uint64_t Offset =
2566 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2567 llvm::Value *NextAddr =
2568 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2569 "ap.next");
2570 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2571
2572 return AddrTyped;
2573}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002574
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002575namespace {
2576
Derek Schuff263366f2012-10-16 22:30:41 +00002577class NaClX86_64ABIInfo : public ABIInfo {
2578 public:
2579 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2580 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2581 virtual void computeInfo(CGFunctionInfo &FI) const;
2582 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2583 CodeGenFunction &CGF) const;
2584 private:
2585 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2586 X86_64ABIInfo NInfo; // Used for everything else.
2587};
2588
2589class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2590 public:
2591 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2592 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2593};
2594
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002595}
2596
Derek Schuff263366f2012-10-16 22:30:41 +00002597void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2598 if (FI.getASTCallingConvention() == CC_PnaclCall)
2599 PInfo.computeInfo(FI);
2600 else
2601 NInfo.computeInfo(FI);
2602}
2603
2604llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2605 CodeGenFunction &CGF) const {
2606 // Always use the native convention; calling pnacl-style varargs functions
2607 // is unuspported.
2608 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2609}
2610
2611
John McCallec853ba2010-03-11 00:10:12 +00002612// PowerPC-32
2613
2614namespace {
2615class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2616public:
Chris Lattnerea044322010-07-29 02:01:43 +00002617 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002618
John McCallec853ba2010-03-11 00:10:12 +00002619 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2620 // This is recovered from gcc output.
2621 return 1; // r1 is the dedicated stack pointer
2622 }
2623
2624 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002625 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002626};
2627
2628}
2629
2630bool
2631PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2632 llvm::Value *Address) const {
2633 // This is calculated from the LLVM and GCC tables and verified
2634 // against gcc output. AFAIK all ABIs use the same encoding.
2635
2636 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002637
Chris Lattner8b418682012-02-07 00:39:47 +00002638 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002639 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2640 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2641 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2642
2643 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002644 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002645
2646 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002647 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002648
2649 // 64-76 are various 4-byte special-purpose registers:
2650 // 64: mq
2651 // 65: lr
2652 // 66: ctr
2653 // 67: ap
2654 // 68-75 cr0-7
2655 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002656 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002657
2658 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002659 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002660
2661 // 109: vrsave
2662 // 110: vscr
2663 // 111: spe_acc
2664 // 112: spefscr
2665 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002666 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002667
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002668 return false;
John McCallec853ba2010-03-11 00:10:12 +00002669}
2670
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002671// PowerPC-64
2672
2673namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002674/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2675class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2676
2677public:
2678 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2679
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002680 // TODO: We can add more logic to computeInfo to improve performance.
2681 // Example: For aggregate arguments that fit in a register, we could
2682 // use getDirectInReg (as is done below for structs containing a single
2683 // floating-point value) to avoid pushing them to memory on function
2684 // entry. This would require changing the logic in PPCISelLowering
2685 // when lowering the parameters in the caller and args in the callee.
2686 virtual void computeInfo(CGFunctionInfo &FI) const {
2687 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2688 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2689 it != ie; ++it) {
2690 // We rely on the default argument classification for the most part.
2691 // One exception: An aggregate containing a single floating-point
2692 // item must be passed in a register if one is available.
2693 const Type *T = isSingleElementStruct(it->type, getContext());
2694 if (T) {
2695 const BuiltinType *BT = T->getAs<BuiltinType>();
2696 if (BT && BT->isFloatingPoint()) {
2697 QualType QT(T, 0);
2698 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2699 continue;
2700 }
2701 }
2702 it->info = classifyArgumentType(it->type);
2703 }
2704 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002705
2706 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2707 QualType Ty,
2708 CodeGenFunction &CGF) const;
2709};
2710
2711class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2712public:
2713 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2714 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2715
2716 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2717 // This is recovered from gcc output.
2718 return 1; // r1 is the dedicated stack pointer
2719 }
2720
2721 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2722 llvm::Value *Address) const;
2723};
2724
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002725class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2726public:
2727 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2728
2729 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2730 // This is recovered from gcc output.
2731 return 1; // r1 is the dedicated stack pointer
2732 }
2733
2734 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2735 llvm::Value *Address) const;
2736};
2737
2738}
2739
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002740// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2741llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2742 QualType Ty,
2743 CodeGenFunction &CGF) const {
2744 llvm::Type *BP = CGF.Int8PtrTy;
2745 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2746
2747 CGBuilderTy &Builder = CGF.Builder;
2748 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2749 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2750
2751 // Handle address alignment for type alignment > 64 bits. Although
2752 // long double normally requires 16-byte alignment, this is not the
2753 // case when it is passed as an argument; so handle that special case.
2754 const BuiltinType *BT = Ty->getAs<BuiltinType>();
2755 unsigned TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2756
2757 if (TyAlign > 8 && (!BT || !BT->isFloatingPoint())) {
2758 assert((TyAlign & (TyAlign - 1)) == 0 &&
2759 "Alignment is not power of 2!");
2760 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2761 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(TyAlign - 1));
2762 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(~(TyAlign - 1)));
2763 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2764 }
2765
2766 // Update the va_list pointer.
2767 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2768 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2769 llvm::Value *NextAddr =
2770 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2771 "ap.next");
2772 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2773
2774 // If the argument is smaller than 8 bytes, it is right-adjusted in
2775 // its doubleword slot. Adjust the pointer to pick it up from the
2776 // correct offset.
2777 if (SizeInBytes < 8) {
2778 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2779 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2780 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2781 }
2782
2783 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2784 return Builder.CreateBitCast(Addr, PTy);
2785}
2786
2787static bool
2788PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2789 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002790 // This is calculated from the LLVM and GCC tables and verified
2791 // against gcc output. AFAIK all ABIs use the same encoding.
2792
2793 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2794
2795 llvm::IntegerType *i8 = CGF.Int8Ty;
2796 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2797 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2798 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2799
2800 // 0-31: r0-31, the 8-byte general-purpose registers
2801 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2802
2803 // 32-63: fp0-31, the 8-byte floating-point registers
2804 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2805
2806 // 64-76 are various 4-byte special-purpose registers:
2807 // 64: mq
2808 // 65: lr
2809 // 66: ctr
2810 // 67: ap
2811 // 68-75 cr0-7
2812 // 76: xer
2813 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2814
2815 // 77-108: v0-31, the 16-byte vector registers
2816 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2817
2818 // 109: vrsave
2819 // 110: vscr
2820 // 111: spe_acc
2821 // 112: spefscr
2822 // 113: sfp
2823 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2824
2825 return false;
2826}
John McCallec853ba2010-03-11 00:10:12 +00002827
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002828bool
2829PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2830 CodeGen::CodeGenFunction &CGF,
2831 llvm::Value *Address) const {
2832
2833 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2834}
2835
2836bool
2837PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2838 llvm::Value *Address) const {
2839
2840 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2841}
2842
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002843//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002844// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002845//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002846
2847namespace {
2848
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002849class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002850public:
2851 enum ABIKind {
2852 APCS = 0,
2853 AAPCS = 1,
2854 AAPCS_VFP
2855 };
2856
2857private:
2858 ABIKind Kind;
2859
2860public:
Chris Lattnerea044322010-07-29 02:01:43 +00002861 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002862
John McCall49e34be2011-08-30 01:42:09 +00002863 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002864 StringRef Env =
2865 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002866 return (Env == "gnueabi" || Env == "eabi" ||
2867 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002868 }
2869
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002870private:
2871 ABIKind getABIKind() const { return Kind; }
2872
Chris Lattnera3c109b2010-07-29 02:16:43 +00002873 ABIArgInfo classifyReturnType(QualType RetTy) const;
2874 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Manman Ren97f81572012-10-16 19:18:39 +00002875 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002876
Chris Lattneree5dcd02010-07-29 02:31:05 +00002877 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002878
2879 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2880 CodeGenFunction &CGF) const;
2881};
2882
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002883class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2884public:
Chris Lattnerea044322010-07-29 02:01:43 +00002885 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2886 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002887
John McCall49e34be2011-08-30 01:42:09 +00002888 const ARMABIInfo &getABIInfo() const {
2889 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2890 }
2891
John McCall6374c332010-03-06 00:35:14 +00002892 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2893 return 13;
2894 }
Roman Divacky09345d12011-05-18 19:36:54 +00002895
Chris Lattner5f9e2722011-07-23 10:55:15 +00002896 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002897 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2898 }
2899
Roman Divacky09345d12011-05-18 19:36:54 +00002900 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2901 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002902 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002903
2904 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002905 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002906 return false;
2907 }
John McCall49e34be2011-08-30 01:42:09 +00002908
2909 unsigned getSizeOfUnwindException() const {
2910 if (getABIInfo().isEABI()) return 88;
2911 return TargetCodeGenInfo::getSizeOfUnwindException();
2912 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002913};
2914
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002915}
2916
Chris Lattneree5dcd02010-07-29 02:31:05 +00002917void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002918 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002919 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002920 it != ie; ++it)
2921 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002922
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002923 // Always honor user-specified calling convention.
2924 if (FI.getCallingConvention() != llvm::CallingConv::C)
2925 return;
2926
2927 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002928 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002929 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002930 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002931 else
2932 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002933
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002934 // If user did not ask for specific calling convention explicitly (e.g. via
2935 // pcs attribute), set effective calling convention if it's different than ABI
2936 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002937 switch (getABIKind()) {
2938 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002939 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2940 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002941 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002942 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002943 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2944 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002945 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002946 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002947 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2948 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002949 break;
2950 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002951}
2952
Bob Wilson194f06a2011-08-03 05:58:22 +00002953/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2954/// aggregate. If HAMembers is non-null, the number of base elements
2955/// contained in the type is returned through it; this is used for the
2956/// recursive calls that check aggregate component types.
2957static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2958 ASTContext &Context,
2959 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002960 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002961 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2962 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2963 return false;
2964 Members *= AT->getSize().getZExtValue();
2965 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2966 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002967 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002968 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002969
Bob Wilson194f06a2011-08-03 05:58:22 +00002970 Members = 0;
2971 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2972 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002973 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002974 uint64_t FldMembers;
2975 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2976 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002977
2978 Members = (RD->isUnion() ?
2979 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002980 }
2981 } else {
2982 Members = 1;
2983 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2984 Members = 2;
2985 Ty = CT->getElementType();
2986 }
2987
2988 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2989 // double, or 64-bit or 128-bit vectors.
2990 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2991 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00002992 BT->getKind() != BuiltinType::Double &&
2993 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00002994 return false;
2995 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2996 unsigned VecSize = Context.getTypeSize(VT);
2997 if (VecSize != 64 && VecSize != 128)
2998 return false;
2999 } else {
3000 return false;
3001 }
3002
3003 // The base type must be the same for all members. Vector types of the
3004 // same total size are treated as being equivalent here.
3005 const Type *TyPtr = Ty.getTypePtr();
3006 if (!Base)
3007 Base = TyPtr;
3008 if (Base != TyPtr &&
3009 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3010 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3011 return false;
3012 }
3013
3014 // Homogeneous Aggregates can have at most 4 members of the base type.
3015 if (HAMembers)
3016 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003017
3018 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003019}
3020
Chris Lattnera3c109b2010-07-29 02:16:43 +00003021ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Manman Ren97f81572012-10-16 19:18:39 +00003022 // Handle illegal vector types here.
3023 if (isIllegalVectorType(Ty)) {
3024 uint64_t Size = getContext().getTypeSize(Ty);
3025 if (Size <= 32) {
3026 llvm::Type *ResType =
3027 llvm::Type::getInt32Ty(getVMContext());
3028 return ABIArgInfo::getDirect(ResType);
3029 }
3030 if (Size == 64) {
3031 llvm::Type *ResType = llvm::VectorType::get(
3032 llvm::Type::getInt32Ty(getVMContext()), 2);
3033 return ABIArgInfo::getDirect(ResType);
3034 }
3035 if (Size == 128) {
3036 llvm::Type *ResType = llvm::VectorType::get(
3037 llvm::Type::getInt32Ty(getVMContext()), 4);
3038 return ABIArgInfo::getDirect(ResType);
3039 }
3040 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3041 }
3042
John McCalld608cdb2010-08-22 10:59:02 +00003043 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003044 // Treat an enum type as its underlying type.
3045 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3046 Ty = EnumTy->getDecl()->getIntegerType();
3047
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003048 return (Ty->isPromotableIntegerType() ?
3049 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003050 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003051
Daniel Dunbar42025572009-09-14 21:54:03 +00003052 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003053 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003054 return ABIArgInfo::getIgnore();
3055
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003056 // Structures with either a non-trivial destructor or a non-trivial
3057 // copy constructor are always indirect.
3058 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3059 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3060
Bob Wilson194f06a2011-08-03 05:58:22 +00003061 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
3062 // Homogeneous Aggregates need to be expanded.
3063 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003064 if (isHomogeneousAggregate(Ty, Base, getContext())) {
3065 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson194f06a2011-08-03 05:58:22 +00003066 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003067 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003068 }
3069
Manman Ren634b3d22012-08-13 21:23:55 +00003070 // Support byval for ARM.
3071 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
3072 getContext().getTypeAlign(Ty) > 64) {
3073 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
Eli Friedman79f30982012-08-09 00:31:40 +00003074 }
3075
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003076 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003077 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003078 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003079 // FIXME: Try to match the types of the arguments more accurately where
3080 // we can.
3081 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003082 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3083 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003084 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003085 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3086 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003087 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003088
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003089 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003090 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003091 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003092}
3093
Chris Lattnera3c109b2010-07-29 02:16:43 +00003094static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003095 llvm::LLVMContext &VMContext) {
3096 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3097 // is called integer-like if its size is less than or equal to one word, and
3098 // the offset of each of its addressable sub-fields is zero.
3099
3100 uint64_t Size = Context.getTypeSize(Ty);
3101
3102 // Check that the type fits in a word.
3103 if (Size > 32)
3104 return false;
3105
3106 // FIXME: Handle vector types!
3107 if (Ty->isVectorType())
3108 return false;
3109
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003110 // Float types are never treated as "integer like".
3111 if (Ty->isRealFloatingType())
3112 return false;
3113
Daniel Dunbar98303b92009-09-13 08:03:58 +00003114 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003115 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003116 return true;
3117
Daniel Dunbar45815812010-02-01 23:31:26 +00003118 // Small complex integer types are "integer like".
3119 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3120 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003121
3122 // Single element and zero sized arrays should be allowed, by the definition
3123 // above, but they are not.
3124
3125 // Otherwise, it must be a record type.
3126 const RecordType *RT = Ty->getAs<RecordType>();
3127 if (!RT) return false;
3128
3129 // Ignore records with flexible arrays.
3130 const RecordDecl *RD = RT->getDecl();
3131 if (RD->hasFlexibleArrayMember())
3132 return false;
3133
3134 // Check that all sub-fields are at offset 0, and are themselves "integer
3135 // like".
3136 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3137
3138 bool HadField = false;
3139 unsigned idx = 0;
3140 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3141 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003142 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003143
Daniel Dunbar679855a2010-01-29 03:22:29 +00003144 // Bit-fields are not addressable, we only need to verify they are "integer
3145 // like". We still have to disallow a subsequent non-bitfield, for example:
3146 // struct { int : 0; int x }
3147 // is non-integer like according to gcc.
3148 if (FD->isBitField()) {
3149 if (!RD->isUnion())
3150 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003151
Daniel Dunbar679855a2010-01-29 03:22:29 +00003152 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3153 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003154
Daniel Dunbar679855a2010-01-29 03:22:29 +00003155 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003156 }
3157
Daniel Dunbar679855a2010-01-29 03:22:29 +00003158 // Check if this field is at offset 0.
3159 if (Layout.getFieldOffset(idx) != 0)
3160 return false;
3161
Daniel Dunbar98303b92009-09-13 08:03:58 +00003162 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3163 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003164
Daniel Dunbar679855a2010-01-29 03:22:29 +00003165 // Only allow at most one field in a structure. This doesn't match the
3166 // wording above, but follows gcc in situations with a field following an
3167 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003168 if (!RD->isUnion()) {
3169 if (HadField)
3170 return false;
3171
3172 HadField = true;
3173 }
3174 }
3175
3176 return true;
3177}
3178
Chris Lattnera3c109b2010-07-29 02:16:43 +00003179ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003180 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003181 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003182
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003183 // Large vector types should be returned via memory.
3184 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3185 return ABIArgInfo::getIndirect(0);
3186
John McCalld608cdb2010-08-22 10:59:02 +00003187 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003188 // Treat an enum type as its underlying type.
3189 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3190 RetTy = EnumTy->getDecl()->getIntegerType();
3191
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003192 return (RetTy->isPromotableIntegerType() ?
3193 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003194 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003195
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003196 // Structures with either a non-trivial destructor or a non-trivial
3197 // copy constructor are always indirect.
3198 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3199 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3200
Daniel Dunbar98303b92009-09-13 08:03:58 +00003201 // Are we following APCS?
3202 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003203 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003204 return ABIArgInfo::getIgnore();
3205
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003206 // Complex types are all returned as packed integers.
3207 //
3208 // FIXME: Consider using 2 x vector types if the back end handles them
3209 // correctly.
3210 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003211 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003212 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003213
Daniel Dunbar98303b92009-09-13 08:03:58 +00003214 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003215 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003216 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003217 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003218 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003219 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003220 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003221 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3222 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003223 }
3224
3225 // Otherwise return in memory.
3226 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003227 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003228
3229 // Otherwise this is an AAPCS variant.
3230
Chris Lattnera3c109b2010-07-29 02:16:43 +00003231 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003232 return ABIArgInfo::getIgnore();
3233
Bob Wilson3b694fa2011-11-02 04:51:36 +00003234 // Check for homogeneous aggregates with AAPCS-VFP.
3235 if (getABIKind() == AAPCS_VFP) {
3236 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003237 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3238 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003239 // Homogeneous Aggregates are returned directly.
3240 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003241 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003242 }
3243
Daniel Dunbar98303b92009-09-13 08:03:58 +00003244 // Aggregates <= 4 bytes are returned in r0; other aggregates
3245 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003246 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003247 if (Size <= 32) {
3248 // Return in the smallest viable integer type.
3249 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003250 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003251 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003252 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3253 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003254 }
3255
Daniel Dunbar98303b92009-09-13 08:03:58 +00003256 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003257}
3258
Manman Ren97f81572012-10-16 19:18:39 +00003259/// isIllegalVector - check whether Ty is an illegal vector type.
3260bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3261 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3262 // Check whether VT is legal.
3263 unsigned NumElements = VT->getNumElements();
3264 uint64_t Size = getContext().getTypeSize(VT);
3265 // NumElements should be power of 2.
3266 if ((NumElements & (NumElements - 1)) != 0)
3267 return true;
3268 // Size should be greater than 32 bits.
3269 return Size <= 32;
3270 }
3271 return false;
3272}
3273
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003274llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003275 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003276 llvm::Type *BP = CGF.Int8PtrTy;
3277 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003278
3279 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003280 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003281 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003282
3283 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003284 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003285 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003286
3287 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3288 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003289 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3290 getABIKind() == ARMABIInfo::AAPCS)
3291 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3292 else
3293 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003294 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3295 if (isIllegalVectorType(Ty) && Size > 16) {
3296 IsIndirect = true;
3297 Size = 4;
3298 TyAlign = 4;
3299 }
Manman Rend105e732012-10-16 19:01:37 +00003300
3301 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003302 if (TyAlign > 4) {
3303 assert((TyAlign & (TyAlign - 1)) == 0 &&
3304 "Alignment is not power of 2!");
3305 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3306 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3307 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003308 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003309 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003310
3311 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003312 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003313 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003314 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003315 "ap.next");
3316 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3317
Manman Ren97f81572012-10-16 19:18:39 +00003318 if (IsIndirect)
3319 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003320 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003321 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3322 // may not be correctly aligned for the vector type. We create an aligned
3323 // temporary space and copy the content over from ap.cur to the temporary
3324 // space. This is necessary if the natural alignment of the type is greater
3325 // than the ABI alignment.
3326 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3327 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3328 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3329 "var.align");
3330 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3331 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3332 Builder.CreateMemCpy(Dst, Src,
3333 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3334 TyAlign, false);
3335 Addr = AlignedTemp; //The content is in aligned location.
3336 }
3337 llvm::Type *PTy =
3338 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3339 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3340
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003341 return AddrTyped;
3342}
3343
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003344namespace {
3345
Derek Schuff263366f2012-10-16 22:30:41 +00003346class NaClARMABIInfo : public ABIInfo {
3347 public:
3348 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3349 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3350 virtual void computeInfo(CGFunctionInfo &FI) const;
3351 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3352 CodeGenFunction &CGF) const;
3353 private:
3354 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3355 ARMABIInfo NInfo; // Used for everything else.
3356};
3357
3358class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3359 public:
3360 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3361 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3362};
3363
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003364}
3365
Derek Schuff263366f2012-10-16 22:30:41 +00003366void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3367 if (FI.getASTCallingConvention() == CC_PnaclCall)
3368 PInfo.computeInfo(FI);
3369 else
3370 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3371}
3372
3373llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3374 CodeGenFunction &CGF) const {
3375 // Always use the native convention; calling pnacl-style varargs functions
3376 // is unsupported.
3377 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3378}
3379
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003380//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003381// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003382//===----------------------------------------------------------------------===//
3383
3384namespace {
3385
Justin Holewinski2c585b92012-05-24 17:43:12 +00003386class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003387public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003388 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003389
3390 ABIArgInfo classifyReturnType(QualType RetTy) const;
3391 ABIArgInfo classifyArgumentType(QualType Ty) const;
3392
3393 virtual void computeInfo(CGFunctionInfo &FI) const;
3394 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3395 CodeGenFunction &CFG) const;
3396};
3397
Justin Holewinski2c585b92012-05-24 17:43:12 +00003398class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003399public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003400 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3401 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003402
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003403 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3404 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003405};
3406
Justin Holewinski2c585b92012-05-24 17:43:12 +00003407ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003408 if (RetTy->isVoidType())
3409 return ABIArgInfo::getIgnore();
3410 if (isAggregateTypeForABI(RetTy))
3411 return ABIArgInfo::getIndirect(0);
3412 return ABIArgInfo::getDirect();
3413}
3414
Justin Holewinski2c585b92012-05-24 17:43:12 +00003415ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003416 if (isAggregateTypeForABI(Ty))
3417 return ABIArgInfo::getIndirect(0);
3418
3419 return ABIArgInfo::getDirect();
3420}
3421
Justin Holewinski2c585b92012-05-24 17:43:12 +00003422void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003423 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3424 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3425 it != ie; ++it)
3426 it->info = classifyArgumentType(it->type);
3427
3428 // Always honor user-specified calling convention.
3429 if (FI.getCallingConvention() != llvm::CallingConv::C)
3430 return;
3431
3432 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003433 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3434 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003435 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003436 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003437 if (LangOpts.OpenCL || LangOpts.CUDA) {
3438 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003439 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003440 } else {
3441 // If we are in standard C/C++ mode, use the triple to decide on the default
3442 StringRef Env =
3443 getContext().getTargetInfo().getTriple().getEnvironmentName();
3444 if (Env == "device")
3445 DefaultCC = llvm::CallingConv::PTX_Device;
3446 else
3447 DefaultCC = llvm::CallingConv::PTX_Kernel;
3448 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003449 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003450
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003451}
3452
Justin Holewinski2c585b92012-05-24 17:43:12 +00003453llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3454 CodeGenFunction &CFG) const {
3455 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003456}
3457
Justin Holewinski2c585b92012-05-24 17:43:12 +00003458void NVPTXTargetCodeGenInfo::
3459SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3460 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003461 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3462 if (!FD) return;
3463
3464 llvm::Function *F = cast<llvm::Function>(GV);
3465
3466 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003467 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003468 // Use OpenCL function attributes to set proper calling conventions
3469 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003470 if (FD->hasAttr<OpenCLKernelAttr>()) {
3471 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003472 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003473 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003474 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003475 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003476 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003477
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003478 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003479 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003480 // CUDA __global__ functions get a kernel calling convention. Since
3481 // __global__ functions cannot be called from the device, we do not
3482 // need to set the noinline attribute.
3483 if (FD->getAttr<CUDAGlobalAttr>())
3484 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003485 }
3486}
3487
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003488}
3489
3490//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003491// MBlaze ABI Implementation
3492//===----------------------------------------------------------------------===//
3493
3494namespace {
3495
3496class MBlazeABIInfo : public ABIInfo {
3497public:
3498 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3499
3500 bool isPromotableIntegerType(QualType Ty) const;
3501
3502 ABIArgInfo classifyReturnType(QualType RetTy) const;
3503 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3504
3505 virtual void computeInfo(CGFunctionInfo &FI) const {
3506 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3507 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3508 it != ie; ++it)
3509 it->info = classifyArgumentType(it->type);
3510 }
3511
3512 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3513 CodeGenFunction &CGF) const;
3514};
3515
3516class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3517public:
3518 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3519 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3520 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3521 CodeGen::CodeGenModule &M) const;
3522};
3523
3524}
3525
3526bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3527 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3528 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3529 switch (BT->getKind()) {
3530 case BuiltinType::Bool:
3531 case BuiltinType::Char_S:
3532 case BuiltinType::Char_U:
3533 case BuiltinType::SChar:
3534 case BuiltinType::UChar:
3535 case BuiltinType::Short:
3536 case BuiltinType::UShort:
3537 return true;
3538 default:
3539 return false;
3540 }
3541 return false;
3542}
3543
3544llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3545 CodeGenFunction &CGF) const {
3546 // FIXME: Implement
3547 return 0;
3548}
3549
3550
3551ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3552 if (RetTy->isVoidType())
3553 return ABIArgInfo::getIgnore();
3554 if (isAggregateTypeForABI(RetTy))
3555 return ABIArgInfo::getIndirect(0);
3556
3557 return (isPromotableIntegerType(RetTy) ?
3558 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3559}
3560
3561ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3562 if (isAggregateTypeForABI(Ty))
3563 return ABIArgInfo::getIndirect(0);
3564
3565 return (isPromotableIntegerType(Ty) ?
3566 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3567}
3568
3569void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3570 llvm::GlobalValue *GV,
3571 CodeGen::CodeGenModule &M)
3572 const {
3573 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3574 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003575
Wesley Peck276fdf42010-12-19 19:57:51 +00003576 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3577 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3578 CC = llvm::CallingConv::MBLAZE_INTR;
3579 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3580 CC = llvm::CallingConv::MBLAZE_SVOL;
3581
3582 if (CC != llvm::CallingConv::C) {
3583 // Handle 'interrupt_handler' attribute:
3584 llvm::Function *F = cast<llvm::Function>(GV);
3585
3586 // Step 1: Set ISR calling convention.
3587 F->setCallingConv(CC);
3588
3589 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003590 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003591 }
3592
3593 // Step 3: Emit _interrupt_handler alias.
3594 if (CC == llvm::CallingConv::MBLAZE_INTR)
3595 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3596 "_interrupt_handler", GV, &M.getModule());
3597}
3598
3599
3600//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003601// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003602//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003603
3604namespace {
3605
3606class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3607public:
Chris Lattnerea044322010-07-29 02:01:43 +00003608 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3609 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003610 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3611 CodeGen::CodeGenModule &M) const;
3612};
3613
3614}
3615
3616void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3617 llvm::GlobalValue *GV,
3618 CodeGen::CodeGenModule &M) const {
3619 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3620 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3621 // Handle 'interrupt' attribute:
3622 llvm::Function *F = cast<llvm::Function>(GV);
3623
3624 // Step 1: Set ISR calling convention.
3625 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3626
3627 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003628 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003629
3630 // Step 3: Emit ISR vector alias.
3631 unsigned Num = attr->getNumber() + 0xffe0;
3632 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003633 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003634 GV, &M.getModule());
3635 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003636 }
3637}
3638
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003639//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003640// MIPS ABI Implementation. This works for both little-endian and
3641// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003642//===----------------------------------------------------------------------===//
3643
John McCallaeeb7012010-05-27 06:19:26 +00003644namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003645class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003646 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003647 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3648 void CoerceToIntArgs(uint64_t TySize,
3649 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003650 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003651 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003652 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003653public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003654 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003655 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3656 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003657
3658 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003659 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003660 virtual void computeInfo(CGFunctionInfo &FI) const;
3661 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3662 CodeGenFunction &CGF) const;
3663};
3664
John McCallaeeb7012010-05-27 06:19:26 +00003665class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003666 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003667public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003668 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3669 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3670 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003671
3672 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3673 return 29;
3674 }
3675
3676 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003677 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003678
3679 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003680 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003681 }
John McCallaeeb7012010-05-27 06:19:26 +00003682};
3683}
3684
Akira Hatanakac359f202012-07-03 19:24:06 +00003685void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3686 SmallVector<llvm::Type*, 8> &ArgList) const {
3687 llvm::IntegerType *IntTy =
3688 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003689
3690 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3691 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3692 ArgList.push_back(IntTy);
3693
3694 // If necessary, add one more integer type to ArgList.
3695 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3696
3697 if (R)
3698 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003699}
3700
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003701// In N32/64, an aligned double precision floating point field is passed in
3702// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003703llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003704 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3705
3706 if (IsO32) {
3707 CoerceToIntArgs(TySize, ArgList);
3708 return llvm::StructType::get(getVMContext(), ArgList);
3709 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003710
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003711 if (Ty->isComplexType())
3712 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003713
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003714 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003715
Akira Hatanakac359f202012-07-03 19:24:06 +00003716 // Unions/vectors are passed in integer registers.
3717 if (!RT || !RT->isStructureOrClassType()) {
3718 CoerceToIntArgs(TySize, ArgList);
3719 return llvm::StructType::get(getVMContext(), ArgList);
3720 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003721
3722 const RecordDecl *RD = RT->getDecl();
3723 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003724 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003725
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003726 uint64_t LastOffset = 0;
3727 unsigned idx = 0;
3728 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3729
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003730 // Iterate over fields in the struct/class and check if there are any aligned
3731 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003732 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3733 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003734 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003735 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3736
3737 if (!BT || BT->getKind() != BuiltinType::Double)
3738 continue;
3739
3740 uint64_t Offset = Layout.getFieldOffset(idx);
3741 if (Offset % 64) // Ignore doubles that are not aligned.
3742 continue;
3743
3744 // Add ((Offset - LastOffset) / 64) args of type i64.
3745 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3746 ArgList.push_back(I64);
3747
3748 // Add double type.
3749 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3750 LastOffset = Offset + 64;
3751 }
3752
Akira Hatanakac359f202012-07-03 19:24:06 +00003753 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3754 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003755
3756 return llvm::StructType::get(getVMContext(), ArgList);
3757}
3758
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003759llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003760 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003761
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003762 if ((Align - 1) & Offset)
3763 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3764
3765 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003766}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003767
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003768ABIArgInfo
3769MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003770 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003771 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003772 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003773
Akira Hatanakac359f202012-07-03 19:24:06 +00003774 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3775 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003776 Offset = llvm::RoundUpToAlignment(Offset, Align);
3777 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003778
Akira Hatanakac359f202012-07-03 19:24:06 +00003779 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003780 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003781 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003782 return ABIArgInfo::getIgnore();
3783
Akira Hatanaka511949b2011-08-01 18:09:58 +00003784 // Records with non trivial destructors/constructors should not be passed
3785 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003786 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003787 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003788 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003789 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003790
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003791 // If we have reached here, aggregates are passed directly by coercing to
3792 // another structure type. Padding is inserted if the offset of the
3793 // aggregate is unaligned.
3794 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3795 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003796 }
3797
3798 // Treat an enum type as its underlying type.
3799 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3800 Ty = EnumTy->getDecl()->getIntegerType();
3801
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003802 if (Ty->isPromotableIntegerType())
3803 return ABIArgInfo::getExtend();
3804
3805 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003806}
3807
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003808llvm::Type*
3809MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003810 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003811 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003812
Akira Hatanakada54ff32012-02-09 18:49:26 +00003813 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003814 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003815 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3816 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003817
Akira Hatanakada54ff32012-02-09 18:49:26 +00003818 // N32/64 returns struct/classes in floating point registers if the
3819 // following conditions are met:
3820 // 1. The size of the struct/class is no larger than 128-bit.
3821 // 2. The struct/class has one or two fields all of which are floating
3822 // point types.
3823 // 3. The offset of the first field is zero (this follows what gcc does).
3824 //
3825 // Any other composite results are returned in integer registers.
3826 //
3827 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3828 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3829 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003830 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003831
Akira Hatanakada54ff32012-02-09 18:49:26 +00003832 if (!BT || !BT->isFloatingPoint())
3833 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003834
David Blaikie262bc182012-04-30 02:36:29 +00003835 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003836 }
3837
3838 if (b == e)
3839 return llvm::StructType::get(getVMContext(), RTList,
3840 RD->hasAttr<PackedAttr>());
3841
3842 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003843 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003844 }
3845
Akira Hatanakac359f202012-07-03 19:24:06 +00003846 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003847 return llvm::StructType::get(getVMContext(), RTList);
3848}
3849
Akira Hatanaka619e8872011-06-02 00:09:17 +00003850ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003851 uint64_t Size = getContext().getTypeSize(RetTy);
3852
3853 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003854 return ABIArgInfo::getIgnore();
3855
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003856 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003857 if (Size <= 128) {
3858 if (RetTy->isAnyComplexType())
3859 return ABIArgInfo::getDirect();
3860
Akira Hatanakac359f202012-07-03 19:24:06 +00003861 // O32 returns integer vectors in registers.
3862 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3863 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3864
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003865 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003866 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3867 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003868
3869 return ABIArgInfo::getIndirect(0);
3870 }
3871
3872 // Treat an enum type as its underlying type.
3873 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3874 RetTy = EnumTy->getDecl()->getIntegerType();
3875
3876 return (RetTy->isPromotableIntegerType() ?
3877 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3878}
3879
3880void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003881 ABIArgInfo &RetInfo = FI.getReturnInfo();
3882 RetInfo = classifyReturnType(FI.getReturnType());
3883
3884 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003885 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003886
Akira Hatanaka619e8872011-06-02 00:09:17 +00003887 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3888 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003889 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003890}
3891
3892llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3893 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003894 llvm::Type *BP = CGF.Int8PtrTy;
3895 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003896
3897 CGBuilderTy &Builder = CGF.Builder;
3898 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3899 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003900 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003901 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3902 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003903 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3904 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003905
3906 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003907 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3908 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3909 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3910 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003911 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3912 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3913 }
3914 else
3915 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3916
3917 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003918 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003919 uint64_t Offset =
3920 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3921 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003922 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003923 "ap.next");
3924 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3925
3926 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003927}
3928
John McCallaeeb7012010-05-27 06:19:26 +00003929bool
3930MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3931 llvm::Value *Address) const {
3932 // This information comes from gcc's implementation, which seems to
3933 // as canonical as it gets.
3934
John McCallaeeb7012010-05-27 06:19:26 +00003935 // Everything on MIPS is 4 bytes. Double-precision FP registers
3936 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003937 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003938
3939 // 0-31 are the general purpose registers, $0 - $31.
3940 // 32-63 are the floating-point registers, $f0 - $f31.
3941 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3942 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003943 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003944
3945 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3946 // They are one bit wide and ignored here.
3947
3948 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3949 // (coprocessor 1 is the FP unit)
3950 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3951 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3952 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003953 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003954 return false;
3955}
3956
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003957//===----------------------------------------------------------------------===//
3958// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3959// Currently subclassed only to implement custom OpenCL C function attribute
3960// handling.
3961//===----------------------------------------------------------------------===//
3962
3963namespace {
3964
3965class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3966public:
3967 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3968 : DefaultTargetCodeGenInfo(CGT) {}
3969
3970 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3971 CodeGen::CodeGenModule &M) const;
3972};
3973
3974void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3975 llvm::GlobalValue *GV,
3976 CodeGen::CodeGenModule &M) const {
3977 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3978 if (!FD) return;
3979
3980 llvm::Function *F = cast<llvm::Function>(GV);
3981
David Blaikie4e4d0842012-03-11 07:00:24 +00003982 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003983 if (FD->hasAttr<OpenCLKernelAttr>()) {
3984 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003985 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003986
3987 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3988
3989 // Convert the reqd_work_group_size() attributes to metadata.
3990 llvm::LLVMContext &Context = F->getContext();
3991 llvm::NamedMDNode *OpenCLMetadata =
3992 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3993
3994 SmallVector<llvm::Value*, 5> Operands;
3995 Operands.push_back(F);
3996
Chris Lattner8b418682012-02-07 00:39:47 +00003997 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3998 llvm::APInt(32,
3999 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4000 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4001 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004002 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004003 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4004 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004005 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4006
4007 // Add a boolean constant operand for "required" (true) or "hint" (false)
4008 // for implementing the work_group_size_hint attr later. Currently
4009 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004010 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004011 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4012 }
4013 }
4014 }
4015}
4016
4017}
John McCallaeeb7012010-05-27 06:19:26 +00004018
Tony Linthicum96319392011-12-12 21:14:55 +00004019//===----------------------------------------------------------------------===//
4020// Hexagon ABI Implementation
4021//===----------------------------------------------------------------------===//
4022
4023namespace {
4024
4025class HexagonABIInfo : public ABIInfo {
4026
4027
4028public:
4029 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4030
4031private:
4032
4033 ABIArgInfo classifyReturnType(QualType RetTy) const;
4034 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4035
4036 virtual void computeInfo(CGFunctionInfo &FI) const;
4037
4038 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4039 CodeGenFunction &CGF) const;
4040};
4041
4042class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4043public:
4044 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4045 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4046
4047 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4048 return 29;
4049 }
4050};
4051
4052}
4053
4054void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4055 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4056 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4057 it != ie; ++it)
4058 it->info = classifyArgumentType(it->type);
4059}
4060
4061ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4062 if (!isAggregateTypeForABI(Ty)) {
4063 // Treat an enum type as its underlying type.
4064 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4065 Ty = EnumTy->getDecl()->getIntegerType();
4066
4067 return (Ty->isPromotableIntegerType() ?
4068 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4069 }
4070
4071 // Ignore empty records.
4072 if (isEmptyRecord(getContext(), Ty, true))
4073 return ABIArgInfo::getIgnore();
4074
4075 // Structures with either a non-trivial destructor or a non-trivial
4076 // copy constructor are always indirect.
4077 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4078 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4079
4080 uint64_t Size = getContext().getTypeSize(Ty);
4081 if (Size > 64)
4082 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4083 // Pass in the smallest viable integer type.
4084 else if (Size > 32)
4085 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4086 else if (Size > 16)
4087 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4088 else if (Size > 8)
4089 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4090 else
4091 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4092}
4093
4094ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4095 if (RetTy->isVoidType())
4096 return ABIArgInfo::getIgnore();
4097
4098 // Large vector types should be returned via memory.
4099 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4100 return ABIArgInfo::getIndirect(0);
4101
4102 if (!isAggregateTypeForABI(RetTy)) {
4103 // Treat an enum type as its underlying type.
4104 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4105 RetTy = EnumTy->getDecl()->getIntegerType();
4106
4107 return (RetTy->isPromotableIntegerType() ?
4108 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4109 }
4110
4111 // Structures with either a non-trivial destructor or a non-trivial
4112 // copy constructor are always indirect.
4113 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4114 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4115
4116 if (isEmptyRecord(getContext(), RetTy, true))
4117 return ABIArgInfo::getIgnore();
4118
4119 // Aggregates <= 8 bytes are returned in r0; other aggregates
4120 // are returned indirectly.
4121 uint64_t Size = getContext().getTypeSize(RetTy);
4122 if (Size <= 64) {
4123 // Return in the smallest viable integer type.
4124 if (Size <= 8)
4125 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4126 if (Size <= 16)
4127 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4128 if (Size <= 32)
4129 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4130 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4131 }
4132
4133 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4134}
4135
4136llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004137 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004138 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004139 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004140
4141 CGBuilderTy &Builder = CGF.Builder;
4142 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4143 "ap");
4144 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4145 llvm::Type *PTy =
4146 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4147 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4148
4149 uint64_t Offset =
4150 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4151 llvm::Value *NextAddr =
4152 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4153 "ap.next");
4154 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4155
4156 return AddrTyped;
4157}
4158
4159
Chris Lattnerea044322010-07-29 02:01:43 +00004160const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004161 if (TheTargetCodeGenInfo)
4162 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004163
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004164 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004165 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004166 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004167 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004168
Derek Schuff9ed63f82012-09-06 17:37:28 +00004169 case llvm::Triple::le32:
4170 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004171 case llvm::Triple::mips:
4172 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004173 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004174
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004175 case llvm::Triple::mips64:
4176 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004177 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004178
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004179 case llvm::Triple::arm:
4180 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004181 {
4182 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004183
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004184 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004185 Kind = ARMABIInfo::APCS;
4186 else if (CodeGenOpts.FloatABI == "hard")
4187 Kind = ARMABIInfo::AAPCS_VFP;
4188
Derek Schuff263366f2012-10-16 22:30:41 +00004189 switch (Triple.getOS()) {
4190 case llvm::Triple::NativeClient:
4191 return *(TheTargetCodeGenInfo =
4192 new NaClARMTargetCodeGenInfo(Types, Kind));
4193 default:
4194 return *(TheTargetCodeGenInfo =
4195 new ARMTargetCodeGenInfo(Types, Kind));
4196 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004197 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004198
John McCallec853ba2010-03-11 00:10:12 +00004199 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004200 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004201 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004202 if (Triple.isOSBinFormatELF())
4203 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4204 else
4205 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004206
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004207 case llvm::Triple::nvptx:
4208 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004209 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004210
Wesley Peck276fdf42010-12-19 19:57:51 +00004211 case llvm::Triple::mblaze:
4212 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4213
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004214 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004215 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004216
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004217 case llvm::Triple::tce:
4218 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4219
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004220 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004221 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004222
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004223 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004224 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004225 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4226 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004227
4228 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004229 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004230 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004231 case llvm::Triple::AuroraUX:
4232 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004233 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004234 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004235 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004236 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004237 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4238 false,
4239 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004240
4241 case llvm::Triple::Win32:
4242 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004243 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4244 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004245
4246 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004247 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004248 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4249 false,
4250 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004251 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004252 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004253
Eli Friedmanee1ad992011-12-02 00:11:43 +00004254 case llvm::Triple::x86_64: {
4255 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4256
Chris Lattnerf13721d2010-08-31 16:44:54 +00004257 switch (Triple.getOS()) {
4258 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004259 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004260 case llvm::Triple::Cygwin:
4261 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Derek Schuff263366f2012-10-16 22:30:41 +00004262 case llvm::Triple::NativeClient:
4263 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004264 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004265 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4266 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004267 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004268 }
Tony Linthicum96319392011-12-12 21:14:55 +00004269 case llvm::Triple::hexagon:
4270 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004271 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004272}