blob: 02f451a42039af26fd716c855ae9aaafba75fdf9 [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"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000020#include "llvm/ADT/Triple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "llvm/DataLayout.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Type.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
Richard Smith426391c2012-11-16 00:53:38 +0000176 return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor();
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000177}
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) {
Eli Friedmandb748a32012-11-29 23:21:04 +0000269 // Treat complex types as the element type.
270 if (const ComplexType *CTy = Ty->getAs<ComplexType>())
271 Ty = CTy->getElementType();
272
273 // Check for a type which we know has a simple scalar argument-passing
274 // convention without any padding. (We're specifically looking for 32
275 // and 64-bit integer and integer-equivalents, float, and double.)
Daniel Dunbara1842d32010-05-14 03:40:53 +0000276 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Eli Friedmandb748a32012-11-29 23:21:04 +0000277 !Ty->isEnumeralType() && !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000278 return false;
279
280 uint64_t Size = Context.getTypeSize(Ty);
281 return Size == 32 || Size == 64;
282}
283
Daniel Dunbar53012f42009-11-09 01:33:53 +0000284/// canExpandIndirectArgument - Test whether an argument type which is to be
285/// passed indirectly (on the stack) would have the equivalent layout if it was
286/// expanded into separate arguments. If so, we prefer to do the latter to avoid
287/// inhibiting optimizations.
288///
289// FIXME: This predicate is missing many cases, currently it just follows
290// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
291// should probably make this smarter, or better yet make the LLVM backend
292// capable of handling it.
293static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
294 // We can only expand structure types.
295 const RecordType *RT = Ty->getAs<RecordType>();
296 if (!RT)
297 return false;
298
299 // We can only expand (C) structures.
300 //
301 // FIXME: This needs to be generalized to handle classes as well.
302 const RecordDecl *RD = RT->getDecl();
303 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
304 return false;
305
Eli Friedman506d4e32011-11-18 01:32:26 +0000306 uint64_t Size = 0;
307
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000308 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
309 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000310 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000311
312 if (!is32Or64BitBasicType(FD->getType(), Context))
313 return false;
314
315 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
316 // how to expand them yet, and the predicate for telling if a bitfield still
317 // counts as "basic" is more complicated than what we were doing previously.
318 if (FD->isBitField())
319 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000320
321 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322 }
323
Eli Friedman506d4e32011-11-18 01:32:26 +0000324 // Make sure there are not any holes in the struct.
325 if (Size != Context.getTypeSize(Ty))
326 return false;
327
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000328 return true;
329}
330
331namespace {
332/// DefaultABIInfo - The default implementation for ABI specific
333/// details. This implementation provides information which results in
334/// self-consistent and sensible LLVM IR generation, but does not
335/// conform to any particular ABI.
336class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000337public:
338 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000339
Chris Lattnera3c109b2010-07-29 02:16:43 +0000340 ABIArgInfo classifyReturnType(QualType RetTy) const;
341 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000342
Chris Lattneree5dcd02010-07-29 02:31:05 +0000343 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000344 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000345 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
346 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000347 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000348 }
349
350 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
351 CodeGenFunction &CGF) const;
352};
353
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000354class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
355public:
Chris Lattnerea044322010-07-29 02:01:43 +0000356 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
357 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000358};
359
360llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
361 CodeGenFunction &CGF) const {
362 return 0;
363}
364
Chris Lattnera3c109b2010-07-29 02:16:43 +0000365ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000366 if (isAggregateTypeForABI(Ty)) {
367 // Records with non trivial destructors/constructors should not be passed
368 // by value.
369 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
370 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
371
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000372 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000373 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000374
Chris Lattnera14db752010-03-11 18:19:55 +0000375 // Treat an enum type as its underlying type.
376 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
377 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000378
Chris Lattnera14db752010-03-11 18:19:55 +0000379 return (Ty->isPromotableIntegerType() ?
380 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000381}
382
Bob Wilson0024f942011-01-10 23:54:17 +0000383ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
384 if (RetTy->isVoidType())
385 return ABIArgInfo::getIgnore();
386
387 if (isAggregateTypeForABI(RetTy))
388 return ABIArgInfo::getIndirect(0);
389
390 // Treat an enum type as its underlying type.
391 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
392 RetTy = EnumTy->getDecl()->getIntegerType();
393
394 return (RetTy->isPromotableIntegerType() ?
395 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
396}
397
Derek Schuff9ed63f82012-09-06 17:37:28 +0000398//===----------------------------------------------------------------------===//
399// le32/PNaCl bitcode ABI Implementation
400//===----------------------------------------------------------------------===//
401
402class PNaClABIInfo : public ABIInfo {
403 public:
404 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
405
406 ABIArgInfo classifyReturnType(QualType RetTy) const;
407 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
408
409 virtual void computeInfo(CGFunctionInfo &FI) const;
410 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
411 CodeGenFunction &CGF) const;
412};
413
414class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
415 public:
416 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
417 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
418};
419
420void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
421 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
422
423 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
424
425 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
426 it != ie; ++it)
427 it->info = classifyArgumentType(it->type, FreeRegs);
428 }
429
430llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
431 CodeGenFunction &CGF) const {
432 return 0;
433}
434
435ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
436 unsigned &FreeRegs) const {
437 if (isAggregateTypeForABI(Ty)) {
438 // Records with non trivial destructors/constructors should not be passed
439 // by value.
440 FreeRegs = 0;
441 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
442 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
443
444 return ABIArgInfo::getIndirect(0);
445 }
446
447 // Treat an enum type as its underlying type.
448 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
449 Ty = EnumTy->getDecl()->getIntegerType();
450
451 ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
452 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
453
454 // Regparm regs hold 32 bits.
455 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
456 if (SizeInRegs == 0) return BaseInfo;
457 if (SizeInRegs > FreeRegs) {
458 FreeRegs = 0;
459 return BaseInfo;
460 }
461 FreeRegs -= SizeInRegs;
462 return BaseInfo.isDirect() ?
463 ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
464 ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
465}
466
467ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
468 if (RetTy->isVoidType())
469 return ABIArgInfo::getIgnore();
470
471 if (isAggregateTypeForABI(RetTy))
472 return ABIArgInfo::getIndirect(0);
473
474 // Treat an enum type as its underlying type.
475 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
476 RetTy = EnumTy->getDecl()->getIntegerType();
477
478 return (RetTy->isPromotableIntegerType() ?
479 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
480}
481
Eli Friedman55fc7e22012-01-25 22:46:34 +0000482/// UseX86_MMXType - Return true if this is an MMX type that should use the
483/// special x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000484bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000485 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
486 // special x86_mmx type.
487 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
488 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
489 IRType->getScalarSizeInBits() != 64;
490}
491
Jay Foadef6de3d2011-07-11 09:56:20 +0000492static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000493 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000494 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000495 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000496 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
497 return Ty;
498}
499
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000500//===----------------------------------------------------------------------===//
501// X86-32 ABI Implementation
502//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000503
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000504/// X86_32ABIInfo - The X86-32 ABI information.
505class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000506 enum Class {
507 Integer,
508 Float
509 };
510
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000511 static const unsigned MinABIStackAlignInBytes = 4;
512
David Chisnall1e4249c2009-08-17 23:08:21 +0000513 bool IsDarwinVectorABI;
514 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000515 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000516 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000517 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000518
519 static bool isRegisterSize(unsigned Size) {
520 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
521 }
522
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000523 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
524 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000525
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000526 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
527 /// such that the argument will be passed in memory.
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000528 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
529 unsigned &FreeRegs) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000530
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000531 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000532 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000533
Rafael Espindolab48280b2012-07-31 02:44:24 +0000534 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000535 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000536 unsigned callingConvention) const;
Rafael Espindolab6932692012-10-24 01:58:58 +0000537 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
538 bool IsFastCall) const;
539 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000540 bool IsFastCall, bool &NeedsPadding) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000542public:
543
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000544 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
546 CodeGenFunction &CGF) const;
547
Rafael Espindolab48280b2012-07-31 02:44:24 +0000548 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
549 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000550 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000551 IsMMXDisabled(m), IsWin32FloatStructABI(w),
552 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000553};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000554
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000555class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
556public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000557 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000558 bool d, bool p, bool m, bool w, unsigned r)
559 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000560
561 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
562 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000563
564 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
565 // Darwin uses different dwarf register numbers for EH.
566 if (CGM.isTargetDarwin()) return 5;
567
568 return 4;
569 }
570
571 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
572 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000573
Jay Foadef6de3d2011-07-11 09:56:20 +0000574 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000575 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000576 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000577 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
578 }
579
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000580};
581
582}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000583
584/// shouldReturnTypeInRegister - Determine if the given type should be
585/// passed in a register (for the Darwin ABI).
586bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000587 ASTContext &Context,
588 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000589 uint64_t Size = Context.getTypeSize(Ty);
590
591 // Type must be register sized.
592 if (!isRegisterSize(Size))
593 return false;
594
595 if (Ty->isVectorType()) {
596 // 64- and 128- bit vectors inside structures are not returned in
597 // registers.
598 if (Size == 64 || Size == 128)
599 return false;
600
601 return true;
602 }
603
Daniel Dunbar77115232010-05-15 00:00:30 +0000604 // If this is a builtin, pointer, enum, complex type, member pointer, or
605 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000606 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000607 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000608 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000609 return true;
610
611 // Arrays are treated like records.
612 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000613 return shouldReturnTypeInRegister(AT->getElementType(), Context,
614 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000615
616 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000617 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000618 if (!RT) return false;
619
Anders Carlssona8874232010-01-27 03:25:19 +0000620 // FIXME: Traverse bases here too.
621
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000622 // For thiscall conventions, structures will never be returned in
623 // a register. This is for compatibility with the MSVC ABI
624 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
625 RT->isStructureType()) {
626 return false;
627 }
628
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000629 // Structure types are passed in register if all fields would be
630 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000631 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
632 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000633 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634
635 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000636 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000637 continue;
638
639 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000640 if (!shouldReturnTypeInRegister(FD->getType(), Context,
641 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642 return false;
643 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000644 return true;
645}
646
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000647ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
648 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000649 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000650 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000651
Chris Lattnera3c109b2010-07-29 02:16:43 +0000652 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000653 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000654 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000655 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000656
657 // 128-bit vectors are a special case; they are returned in
658 // registers and we need to make sure to pick a type the LLVM
659 // backend will like.
660 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000661 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000662 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000663
664 // Always return in register if it fits in a general purpose
665 // register, or if it is 64 bits and has a single element.
666 if ((Size == 8 || Size == 16 || Size == 32) ||
667 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000668 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000669 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000670
671 return ABIArgInfo::getIndirect(0);
672 }
673
674 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000675 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000676
John McCalld608cdb2010-08-22 10:59:02 +0000677 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000678 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000679 // Structures with either a non-trivial destructor or a non-trivial
680 // copy constructor are always indirect.
681 if (hasNonTrivialDestructorOrCopyConstructor(RT))
682 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000683
Anders Carlsson40092972009-10-20 22:07:59 +0000684 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 if (RT->getDecl()->hasFlexibleArrayMember())
686 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000687 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000688
David Chisnall1e4249c2009-08-17 23:08:21 +0000689 // If specified, structs and unions are always indirect.
690 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000691 return ABIArgInfo::getIndirect(0);
692
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000693 // Small structures which are register sized are generally returned
694 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000695 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
696 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000697 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000698
699 // As a special-case, if the struct is a "single-element" struct, and
700 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000701 // floating-point register. (MSVC does not apply this special case.)
702 // We apply a similar transformation for pointer types to improve the
703 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000704 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000705 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
706 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000707 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
708
709 // FIXME: We should be able to narrow this integer in cases with dead
710 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000711 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000712 }
713
714 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000715 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000716
Chris Lattnera3c109b2010-07-29 02:16:43 +0000717 // Treat an enum type as its underlying type.
718 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
719 RetTy = EnumTy->getDecl()->getIntegerType();
720
721 return (RetTy->isPromotableIntegerType() ?
722 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000723}
724
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000725static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
726 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
727}
728
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000729static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
730 const RecordType *RT = Ty->getAs<RecordType>();
731 if (!RT)
732 return 0;
733 const RecordDecl *RD = RT->getDecl();
734
735 // If this is a C++ record, check the bases first.
736 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
737 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
738 e = CXXRD->bases_end(); i != e; ++i)
739 if (!isRecordWithSSEVectorType(Context, i->getType()))
740 return false;
741
742 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
743 i != e; ++i) {
744 QualType FT = i->getType();
745
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000746 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000747 return true;
748
749 if (isRecordWithSSEVectorType(Context, FT))
750 return true;
751 }
752
753 return false;
754}
755
Daniel Dunbare59d8582010-09-16 20:42:06 +0000756unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
757 unsigned Align) const {
758 // Otherwise, if the alignment is less than or equal to the minimum ABI
759 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000760 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000761 return 0; // Use default alignment.
762
763 // On non-Darwin, the stack type alignment is always 4.
764 if (!IsDarwinVectorABI) {
765 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000766 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000767 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000768
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000769 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000770 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
771 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000772 return 16;
773
774 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000775}
776
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000777ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
778 unsigned &FreeRegs) const {
779 if (!ByVal) {
780 if (FreeRegs) {
781 --FreeRegs; // Non byval indirects just use one pointer.
782 return ABIArgInfo::getIndirectInReg(0, false);
783 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000784 return ABIArgInfo::getIndirect(0, false);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000785 }
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000786
Daniel Dunbare59d8582010-09-16 20:42:06 +0000787 // Compute the byval alignment.
788 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
789 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
790 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000791 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000792
793 // If the stack alignment is less than the type alignment, realign the
794 // argument.
795 if (StackAlign < TypeAlign)
796 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
797 /*Realign=*/true);
798
799 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000800}
801
Rafael Espindolab48280b2012-07-31 02:44:24 +0000802X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
803 const Type *T = isSingleElementStruct(Ty, getContext());
804 if (!T)
805 T = Ty.getTypePtr();
806
807 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
808 BuiltinType::Kind K = BT->getKind();
809 if (K == BuiltinType::Float || K == BuiltinType::Double)
810 return Float;
811 }
812 return Integer;
813}
814
Rafael Espindolab6932692012-10-24 01:58:58 +0000815bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000816 bool IsFastCall, bool &NeedsPadding) const {
817 NeedsPadding = false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000818 Class C = classify(Ty);
819 if (C == Float)
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000820 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000821
Rafael Espindolab6932692012-10-24 01:58:58 +0000822 unsigned Size = getContext().getTypeSize(Ty);
823 unsigned SizeInRegs = (Size + 31) / 32;
Rafael Espindola5f14fcb2012-10-23 02:04:01 +0000824
825 if (SizeInRegs == 0)
826 return false;
827
Rafael Espindolab48280b2012-07-31 02:44:24 +0000828 if (SizeInRegs > FreeRegs) {
829 FreeRegs = 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000830 return false;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000831 }
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000832
Rafael Espindolab48280b2012-07-31 02:44:24 +0000833 FreeRegs -= SizeInRegs;
Rafael Espindolab6932692012-10-24 01:58:58 +0000834
835 if (IsFastCall) {
836 if (Size > 32)
837 return false;
838
839 if (Ty->isIntegralOrEnumerationType())
840 return true;
841
842 if (Ty->isPointerType())
843 return true;
844
845 if (Ty->isReferenceType())
846 return true;
847
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000848 if (FreeRegs)
849 NeedsPadding = true;
850
Rafael Espindolab6932692012-10-24 01:58:58 +0000851 return false;
852 }
853
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000854 return true;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000855}
856
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000857ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Rafael Espindolab6932692012-10-24 01:58:58 +0000858 unsigned &FreeRegs,
859 bool IsFastCall) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000860 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000861 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000863 if (const RecordType *RT = Ty->getAs<RecordType>()) {
864 // Structures with either a non-trivial destructor or a non-trivial
865 // copy constructor are always indirect.
866 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000867 return getIndirectResult(Ty, false, FreeRegs);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000868
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000869 if (RT->getDecl()->hasFlexibleArrayMember())
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000870 return getIndirectResult(Ty, true, FreeRegs);
Anders Carlssona8874232010-01-27 03:25:19 +0000871 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000872
Eli Friedman5a4d3522011-11-18 00:28:11 +0000873 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000874 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000875 return ABIArgInfo::getIgnore();
876
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000877 llvm::LLVMContext &LLVMContext = getVMContext();
878 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
879 bool NeedsPadding;
880 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000881 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000882 SmallVector<llvm::Type*, 3> Elements;
883 for (unsigned I = 0; I < SizeInRegs; ++I)
884 Elements.push_back(Int32);
885 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
886 return ABIArgInfo::getDirectInReg(Result);
887 }
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000888 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000889
Daniel Dunbar53012f42009-11-09 01:33:53 +0000890 // Expand small (<= 128-bit) record types when we know that the stack layout
891 // of those arguments will match the struct. This is important because the
892 // LLVM backend isn't smart enough to remove byval, which inhibits many
893 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000894 if (getContext().getTypeSize(Ty) <= 4*32 &&
895 canExpandIndirectArgument(Ty, getContext()))
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000896 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000897
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000898 return getIndirectResult(Ty, true, FreeRegs);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000899 }
900
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000901 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000902 // On Darwin, some vectors are passed in memory, we handle this by passing
903 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000904 if (IsDarwinVectorABI) {
905 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000906 if ((Size == 8 || Size == 16 || Size == 32) ||
907 (Size == 64 && VT->getNumElements() == 1))
908 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
909 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000910 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000911
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000912 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000913 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000914 if (IsMMXDisabled)
915 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
916 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000917 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
918 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
919 return AAI;
920 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000921
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000922 return ABIArgInfo::getDirect();
923 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000924
925
Chris Lattnera3c109b2010-07-29 02:16:43 +0000926 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
927 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000928
Rafael Espindolae4aeeaa2012-10-24 01:59:00 +0000929 bool NeedsPadding;
930 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
Rafael Espindola0b4cc952012-10-19 05:04:37 +0000931
932 if (Ty->isPromotableIntegerType()) {
933 if (InReg)
934 return ABIArgInfo::getExtendInReg();
935 return ABIArgInfo::getExtend();
936 }
937 if (InReg)
938 return ABIArgInfo::getDirectInReg();
939 return ABIArgInfo::getDirect();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000940}
941
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000942void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
943 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
944 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000945
Rafael Espindolab6932692012-10-24 01:58:58 +0000946 unsigned CC = FI.getCallingConvention();
947 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
948 unsigned FreeRegs;
949 if (IsFastCall)
950 FreeRegs = 2;
951 else if (FI.getHasRegParm())
952 FreeRegs = FI.getRegParm();
953 else
954 FreeRegs = DefaultNumRegisterParameters;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000955
956 // If the return value is indirect, then the hidden argument is consuming one
957 // integer register.
958 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
959 --FreeRegs;
960 ABIArgInfo &Old = FI.getReturnInfo();
961 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
962 Old.getIndirectByVal(),
963 Old.getIndirectRealign());
964 }
965
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000966 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
967 it != ie; ++it)
Rafael Espindolab6932692012-10-24 01:58:58 +0000968 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000969}
970
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000971llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
972 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000973 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000974
975 CGBuilderTy &Builder = CGF.Builder;
976 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
977 "ap");
978 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000979
980 // Compute if the address needs to be aligned
981 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
982 Align = getTypeStackAlignInBytes(Ty, Align);
983 Align = std::max(Align, 4U);
984 if (Align > 4) {
985 // addr = (addr + align - 1) & -align;
986 llvm::Value *Offset =
987 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
988 Addr = CGF.Builder.CreateGEP(Addr, Offset);
989 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
990 CGF.Int32Ty);
991 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
992 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
993 Addr->getType(),
994 "ap.cur.aligned");
995 }
996
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000997 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000998 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000999 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1000
1001 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +00001002 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001003 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00001004 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 "ap.next");
1006 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1007
1008 return AddrTyped;
1009}
1010
Charles Davis74f72932010-02-13 15:54:06 +00001011void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1012 llvm::GlobalValue *GV,
1013 CodeGen::CodeGenModule &CGM) const {
1014 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1015 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1016 // Get the LLVM function.
1017 llvm::Function *Fn = cast<llvm::Function>(GV);
1018
1019 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +00001020 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +00001021 B.addStackAlignmentAttr(16);
Bill Wendling75d37b42012-10-15 07:31:59 +00001022 Fn->addAttribute(llvm::AttrListPtr::FunctionIndex,
1023 llvm::Attributes::get(CGM.getLLVMContext(), B));
Charles Davis74f72932010-02-13 15:54:06 +00001024 }
1025 }
1026}
1027
John McCall6374c332010-03-06 00:35:14 +00001028bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1029 CodeGen::CodeGenFunction &CGF,
1030 llvm::Value *Address) const {
1031 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001032
Chris Lattner8b418682012-02-07 00:39:47 +00001033 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001034
John McCall6374c332010-03-06 00:35:14 +00001035 // 0-7 are the eight integer registers; the order is different
1036 // on Darwin (for EH), but the range is the same.
1037 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001038 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001039
1040 if (CGF.CGM.isTargetDarwin()) {
1041 // 12-16 are st(0..4). Not sure why we stop at 4.
1042 // These have size 16, which is sizeof(long double) on
1043 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001044 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001045 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001046
John McCall6374c332010-03-06 00:35:14 +00001047 } else {
1048 // 9 is %eflags, which doesn't get a size on Darwin for some
1049 // reason.
1050 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1051
1052 // 11-16 are st(0..5). Not sure why we stop at 5.
1053 // These have size 12, which is sizeof(long double) on
1054 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001055 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001056 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1057 }
John McCall6374c332010-03-06 00:35:14 +00001058
1059 return false;
1060}
1061
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001062//===----------------------------------------------------------------------===//
1063// X86-64 ABI Implementation
1064//===----------------------------------------------------------------------===//
1065
1066
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001067namespace {
1068/// X86_64ABIInfo - The X86_64 ABI information.
1069class X86_64ABIInfo : public ABIInfo {
1070 enum Class {
1071 Integer = 0,
1072 SSE,
1073 SSEUp,
1074 X87,
1075 X87Up,
1076 ComplexX87,
1077 NoClass,
1078 Memory
1079 };
1080
1081 /// merge - Implement the X86_64 ABI merging algorithm.
1082 ///
1083 /// Merge an accumulating classification \arg Accum with a field
1084 /// classification \arg Field.
1085 ///
1086 /// \param Accum - The accumulating classification. This should
1087 /// always be either NoClass or the result of a previous merge
1088 /// call. In addition, this should never be Memory (the caller
1089 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001090 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001091
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001092 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1093 ///
1094 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1095 /// final MEMORY or SSE classes when necessary.
1096 ///
1097 /// \param AggregateSize - The size of the current aggregate in
1098 /// the classification process.
1099 ///
1100 /// \param Lo - The classification for the parts of the type
1101 /// residing in the low word of the containing object.
1102 ///
1103 /// \param Hi - The classification for the parts of the type
1104 /// residing in the higher words of the containing object.
1105 ///
1106 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1107
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108 /// classify - Determine the x86_64 register classes in which the
1109 /// given type T should be passed.
1110 ///
1111 /// \param Lo - The classification for the parts of the type
1112 /// residing in the low word of the containing object.
1113 ///
1114 /// \param Hi - The classification for the parts of the type
1115 /// residing in the high word of the containing object.
1116 ///
1117 /// \param OffsetBase - The bit offset of this type in the
1118 /// containing object. Some parameters are classified different
1119 /// depending on whether they straddle an eightbyte boundary.
1120 ///
1121 /// If a word is unused its result will be NoClass; if a type should
1122 /// be passed in Memory then at least the classification of \arg Lo
1123 /// will be Memory.
1124 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001125 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001126 ///
1127 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1128 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001129 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001130
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001131 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001132 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1133 unsigned IROffset, QualType SourceTy,
1134 unsigned SourceOffset) const;
1135 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1136 unsigned IROffset, QualType SourceTy,
1137 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001138
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001139 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001140 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001141 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001142
1143 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001144 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001145 ///
1146 /// \param freeIntRegs - The number of free integer registers remaining
1147 /// available.
1148 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001149
Chris Lattnera3c109b2010-07-29 02:16:43 +00001150 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001151
Bill Wendlingbb465d72010-10-18 03:41:31 +00001152 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001153 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001154 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001155 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001156
Eli Friedmanee1ad992011-12-02 00:11:43 +00001157 bool IsIllegalVectorType(QualType Ty) const;
1158
John McCall67a57732011-04-21 01:20:55 +00001159 /// The 0.98 ABI revision clarified a lot of ambiguities,
1160 /// unfortunately in ways that were not always consistent with
1161 /// certain previous compilers. In particular, platforms which
1162 /// required strict binary compatibility with older versions of GCC
1163 /// may need to exempt themselves.
1164 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001165 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001166 }
1167
Eli Friedmanee1ad992011-12-02 00:11:43 +00001168 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001169 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1170 // 64-bit hardware.
1171 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001172
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001173public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001174 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001175 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001176 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001177 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001178
John McCallde5d3c72012-02-17 03:33:10 +00001179 bool isPassedUsingAVXType(QualType type) const {
1180 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001181 // The freeIntRegs argument doesn't matter here.
1182 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001183 if (info.isDirect()) {
1184 llvm::Type *ty = info.getCoerceToType();
1185 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1186 return (vectorTy->getBitWidth() > 128);
1187 }
1188 return false;
1189 }
1190
Chris Lattneree5dcd02010-07-29 02:31:05 +00001191 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001192
1193 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1194 CodeGenFunction &CGF) const;
1195};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001196
Chris Lattnerf13721d2010-08-31 16:44:54 +00001197/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001198class WinX86_64ABIInfo : public ABIInfo {
1199
1200 ABIArgInfo classify(QualType Ty) const;
1201
Chris Lattnerf13721d2010-08-31 16:44:54 +00001202public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001203 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1204
1205 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001206
1207 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1208 CodeGenFunction &CGF) const;
1209};
1210
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001211class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1212public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001213 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001214 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001215
John McCallde5d3c72012-02-17 03:33:10 +00001216 const X86_64ABIInfo &getABIInfo() const {
1217 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1218 }
1219
John McCall6374c332010-03-06 00:35:14 +00001220 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1221 return 7;
1222 }
1223
1224 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1225 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001226 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001227
John McCallaeeb7012010-05-27 06:19:26 +00001228 // 0-15 are the 16 integer registers.
1229 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001230 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001231 return false;
1232 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001233
Jay Foadef6de3d2011-07-11 09:56:20 +00001234 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001235 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001236 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001237 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1238 }
1239
John McCallde5d3c72012-02-17 03:33:10 +00001240 bool isNoProtoCallVariadic(const CallArgList &args,
1241 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001242 // The default CC on x86-64 sets %al to the number of SSA
1243 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001244 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001245 // that when AVX types are involved: the ABI explicitly states it is
1246 // undefined, and it doesn't work in practice because of how the ABI
1247 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001248 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001249 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001250 for (CallArgList::const_iterator
1251 it = args.begin(), ie = args.end(); it != ie; ++it) {
1252 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1253 HasAVXType = true;
1254 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001255 }
1256 }
John McCallde5d3c72012-02-17 03:33:10 +00001257
Eli Friedman3ed79032011-12-01 04:53:19 +00001258 if (!HasAVXType)
1259 return true;
1260 }
John McCall01f151e2011-09-21 08:08:30 +00001261
John McCallde5d3c72012-02-17 03:33:10 +00001262 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001263 }
1264
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001265};
1266
Chris Lattnerf13721d2010-08-31 16:44:54 +00001267class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1268public:
1269 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1270 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1271
1272 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1273 return 7;
1274 }
1275
1276 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1277 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001278 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001279
Chris Lattnerf13721d2010-08-31 16:44:54 +00001280 // 0-15 are the 16 integer registers.
1281 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001282 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001283 return false;
1284 }
1285};
1286
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001287}
1288
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001289void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1290 Class &Hi) const {
1291 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1292 //
1293 // (a) If one of the classes is Memory, the whole argument is passed in
1294 // memory.
1295 //
1296 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1297 // memory.
1298 //
1299 // (c) If the size of the aggregate exceeds two eightbytes and the first
1300 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1301 // argument is passed in memory. NOTE: This is necessary to keep the
1302 // ABI working for processors that don't support the __m256 type.
1303 //
1304 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1305 //
1306 // Some of these are enforced by the merging logic. Others can arise
1307 // only with unions; for example:
1308 // union { _Complex double; unsigned; }
1309 //
1310 // Note that clauses (b) and (c) were added in 0.98.
1311 //
1312 if (Hi == Memory)
1313 Lo = Memory;
1314 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1315 Lo = Memory;
1316 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1317 Lo = Memory;
1318 if (Hi == SSEUp && Lo != SSE)
1319 Hi = SSE;
1320}
1321
Chris Lattner1090a9b2010-06-28 21:43:59 +00001322X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001323 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1324 // classified recursively so that always two fields are
1325 // considered. The resulting class is calculated according to
1326 // the classes of the fields in the eightbyte:
1327 //
1328 // (a) If both classes are equal, this is the resulting class.
1329 //
1330 // (b) If one of the classes is NO_CLASS, the resulting class is
1331 // the other class.
1332 //
1333 // (c) If one of the classes is MEMORY, the result is the MEMORY
1334 // class.
1335 //
1336 // (d) If one of the classes is INTEGER, the result is the
1337 // INTEGER.
1338 //
1339 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1340 // MEMORY is used as class.
1341 //
1342 // (f) Otherwise class SSE is used.
1343
1344 // Accum should never be memory (we should have returned) or
1345 // ComplexX87 (because this cannot be passed in a structure).
1346 assert((Accum != Memory && Accum != ComplexX87) &&
1347 "Invalid accumulated classification during merge.");
1348 if (Accum == Field || Field == NoClass)
1349 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001350 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001351 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001352 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001353 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001354 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001355 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001356 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1357 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001358 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001359 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001360}
1361
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001362void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001363 Class &Lo, Class &Hi) const {
1364 // FIXME: This code can be simplified by introducing a simple value class for
1365 // Class pairs with appropriate constructor methods for the various
1366 // situations.
1367
1368 // FIXME: Some of the split computations are wrong; unaligned vectors
1369 // shouldn't be passed in registers for example, so there is no chance they
1370 // can straddle an eightbyte. Verify & simplify.
1371
1372 Lo = Hi = NoClass;
1373
1374 Class &Current = OffsetBase < 64 ? Lo : Hi;
1375 Current = Memory;
1376
John McCall183700f2009-09-21 23:43:11 +00001377 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001378 BuiltinType::Kind k = BT->getKind();
1379
1380 if (k == BuiltinType::Void) {
1381 Current = NoClass;
1382 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1383 Lo = Integer;
1384 Hi = Integer;
1385 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1386 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001387 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1388 (k == BuiltinType::LongDouble &&
1389 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001390 llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001391 Current = SSE;
1392 } else if (k == BuiltinType::LongDouble) {
1393 Lo = X87;
1394 Hi = X87Up;
1395 }
1396 // FIXME: _Decimal32 and _Decimal64 are SSE.
1397 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001398 return;
1399 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001400
Chris Lattner1090a9b2010-06-28 21:43:59 +00001401 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001402 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001403 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
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 (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001408 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001409 return;
1410 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001411
Chris Lattner1090a9b2010-06-28 21:43:59 +00001412 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001413 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001414 Lo = Hi = Integer;
1415 else
1416 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001417 return;
1418 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001419
Chris Lattner1090a9b2010-06-28 21:43:59 +00001420 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001421 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001422 if (Size == 32) {
1423 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1424 // float> as integer.
1425 Current = Integer;
1426
1427 // If this type crosses an eightbyte boundary, it should be
1428 // split.
1429 uint64_t EB_Real = (OffsetBase) / 64;
1430 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1431 if (EB_Real != EB_Imag)
1432 Hi = Lo;
1433 } else if (Size == 64) {
1434 // gcc passes <1 x double> in memory. :(
1435 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1436 return;
1437
1438 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001439 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001440 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1441 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1442 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001443 Current = Integer;
1444 else
1445 Current = SSE;
1446
1447 // If this type crosses an eightbyte boundary, it should be
1448 // split.
1449 if (OffsetBase && OffsetBase != 64)
1450 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001451 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001452 // Arguments of 256-bits are split into four eightbyte chunks. The
1453 // least significant one belongs to class SSE and all the others to class
1454 // SSEUP. The original Lo and Hi design considers that types can't be
1455 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1456 // This design isn't correct for 256-bits, but since there're no cases
1457 // where the upper parts would need to be inspected, avoid adding
1458 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001459 Lo = SSE;
1460 Hi = SSEUp;
1461 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001462 return;
1463 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001464
Chris Lattner1090a9b2010-06-28 21:43:59 +00001465 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001466 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001467
Chris Lattnerea044322010-07-29 02:01:43 +00001468 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001469 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001470 if (Size <= 64)
1471 Current = Integer;
1472 else if (Size <= 128)
1473 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001474 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001475 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001476 else if (ET == getContext().DoubleTy ||
1477 (ET == getContext().LongDoubleTy &&
1478 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001479 llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001480 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001481 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 Current = ComplexX87;
1483
1484 // If this complex type crosses an eightbyte boundary then it
1485 // should be split.
1486 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001487 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001488 if (Hi == NoClass && EB_Real != EB_Imag)
1489 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001490
Chris Lattner1090a9b2010-06-28 21:43:59 +00001491 return;
1492 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001493
Chris Lattnerea044322010-07-29 02:01:43 +00001494 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001495 // Arrays are treated like structures.
1496
Chris Lattnerea044322010-07-29 02:01:43 +00001497 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001498
1499 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001500 // than four eightbytes, ..., it has class MEMORY.
1501 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001502 return;
1503
1504 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1505 // fields, it has class MEMORY.
1506 //
1507 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001508 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509 return;
1510
1511 // Otherwise implement simplified merge. We could be smarter about
1512 // this, but it isn't worth it and would be harder to verify.
1513 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001514 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001515 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001516
1517 // The only case a 256-bit wide vector could be used is when the array
1518 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1519 // to work for sizes wider than 128, early check and fallback to memory.
1520 if (Size > 128 && EltSize != 256)
1521 return;
1522
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001523 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1524 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001525 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001526 Lo = merge(Lo, FieldLo);
1527 Hi = merge(Hi, FieldHi);
1528 if (Lo == Memory || Hi == Memory)
1529 break;
1530 }
1531
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001532 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001533 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001534 return;
1535 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001536
Chris Lattner1090a9b2010-06-28 21:43:59 +00001537 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001538 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001539
1540 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001541 // than four eightbytes, ..., it has class MEMORY.
1542 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001543 return;
1544
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001545 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1546 // copy constructor or a non-trivial destructor, it is passed by invisible
1547 // reference.
1548 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1549 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001550
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001551 const RecordDecl *RD = RT->getDecl();
1552
1553 // Assume variable sized types are passed in memory.
1554 if (RD->hasFlexibleArrayMember())
1555 return;
1556
Chris Lattnerea044322010-07-29 02:01:43 +00001557 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001558
1559 // Reset Lo class, this will be recomputed.
1560 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001561
1562 // If this is a C++ record, classify the bases first.
1563 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1564 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1565 e = CXXRD->bases_end(); i != e; ++i) {
1566 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1567 "Unexpected base class!");
1568 const CXXRecordDecl *Base =
1569 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1570
1571 // Classify this field.
1572 //
1573 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1574 // single eightbyte, each is classified separately. Each eightbyte gets
1575 // initialized to class NO_CLASS.
1576 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001577 uint64_t Offset =
1578 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001579 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001580 Lo = merge(Lo, FieldLo);
1581 Hi = merge(Hi, FieldHi);
1582 if (Lo == Memory || Hi == Memory)
1583 break;
1584 }
1585 }
1586
1587 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001588 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001589 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001590 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001591 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1592 bool BitField = i->isBitField();
1593
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001594 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1595 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001596 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001597 // The only case a 256-bit wide vector could be used is when the struct
1598 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1599 // to work for sizes wider than 128, early check and fallback to memory.
1600 //
1601 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1602 Lo = Memory;
1603 return;
1604 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001605 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001606 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607 Lo = Memory;
1608 return;
1609 }
1610
1611 // Classify this field.
1612 //
1613 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1614 // exceeds a single eightbyte, each is classified
1615 // separately. Each eightbyte gets initialized to class
1616 // NO_CLASS.
1617 Class FieldLo, FieldHi;
1618
1619 // Bit-fields require special handling, they do not force the
1620 // structure to be passed in memory even if unaligned, and
1621 // therefore they can straddle an eightbyte.
1622 if (BitField) {
1623 // Ignore padding bit-fields.
1624 if (i->isUnnamedBitfield())
1625 continue;
1626
1627 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001628 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001629
1630 uint64_t EB_Lo = Offset / 64;
1631 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1632 FieldLo = FieldHi = NoClass;
1633 if (EB_Lo) {
1634 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1635 FieldLo = NoClass;
1636 FieldHi = Integer;
1637 } else {
1638 FieldLo = Integer;
1639 FieldHi = EB_Hi ? Integer : NoClass;
1640 }
1641 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001642 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001643 Lo = merge(Lo, FieldLo);
1644 Hi = merge(Hi, FieldHi);
1645 if (Lo == Memory || Hi == Memory)
1646 break;
1647 }
1648
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001649 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001650 }
1651}
1652
Chris Lattner9c254f02010-06-29 06:01:59 +00001653ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001654 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1655 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001656 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001657 // Treat an enum type as its underlying type.
1658 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1659 Ty = EnumTy->getDecl()->getIntegerType();
1660
1661 return (Ty->isPromotableIntegerType() ?
1662 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1663 }
1664
1665 return ABIArgInfo::getIndirect(0);
1666}
1667
Eli Friedmanee1ad992011-12-02 00:11:43 +00001668bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1669 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1670 uint64_t Size = getContext().getTypeSize(VecTy);
1671 unsigned LargestVector = HasAVX ? 256 : 128;
1672 if (Size <= 64 || Size > LargestVector)
1673 return true;
1674 }
1675
1676 return false;
1677}
1678
Daniel Dunbaredfac032012-03-10 01:03:58 +00001679ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1680 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001681 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1682 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001683 //
1684 // This assumption is optimistic, as there could be free registers available
1685 // when we need to pass this argument in memory, and LLVM could try to pass
1686 // the argument in the free register. This does not seem to happen currently,
1687 // but this code would be much safer if we could mark the argument with
1688 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001689 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001690 // Treat an enum type as its underlying type.
1691 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1692 Ty = EnumTy->getDecl()->getIntegerType();
1693
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001694 return (Ty->isPromotableIntegerType() ?
1695 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001696 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001697
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001698 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1699 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001700
Chris Lattner855d2272011-05-22 23:21:23 +00001701 // Compute the byval alignment. We specify the alignment of the byval in all
1702 // cases so that the mid-level optimizer knows the alignment of the byval.
1703 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001704
1705 // Attempt to avoid passing indirect results using byval when possible. This
1706 // is important for good codegen.
1707 //
1708 // We do this by coercing the value into a scalar type which the backend can
1709 // handle naturally (i.e., without using byval).
1710 //
1711 // For simplicity, we currently only do this when we have exhausted all of the
1712 // free integer registers. Doing this when there are free integer registers
1713 // would require more care, as we would have to ensure that the coerced value
1714 // did not claim the unused register. That would require either reording the
1715 // arguments to the function (so that any subsequent inreg values came first),
1716 // or only doing this optimization when there were no following arguments that
1717 // might be inreg.
1718 //
1719 // We currently expect it to be rare (particularly in well written code) for
1720 // arguments to be passed on the stack when there are still free integer
1721 // registers available (this would typically imply large structs being passed
1722 // by value), so this seems like a fair tradeoff for now.
1723 //
1724 // We can revisit this if the backend grows support for 'onstack' parameter
1725 // attributes. See PR12193.
1726 if (freeIntRegs == 0) {
1727 uint64_t Size = getContext().getTypeSize(Ty);
1728
1729 // If this type fits in an eightbyte, coerce it into the matching integral
1730 // type, which will end up on the stack (with alignment 8).
1731 if (Align == 8 && Size <= 64)
1732 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1733 Size));
1734 }
1735
Chris Lattner855d2272011-05-22 23:21:23 +00001736 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001737}
1738
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001739/// GetByteVectorType - The ABI specifies that a value should be passed in an
1740/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001741/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001742llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001743 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001744
Chris Lattner15842bd2010-07-29 05:02:29 +00001745 // Wrapper structs that just contain vectors are passed just like vectors,
1746 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001747 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001748 while (STy && STy->getNumElements() == 1) {
1749 IRType = STy->getElementType(0);
1750 STy = dyn_cast<llvm::StructType>(IRType);
1751 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001752
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001753 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001754 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1755 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001756 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001757 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001758 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1759 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1760 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1761 EltTy->isIntegerTy(128)))
1762 return VT;
1763 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001764
Chris Lattner0f408f52010-07-29 04:56:46 +00001765 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1766}
1767
Chris Lattnere2962be2010-07-29 07:30:00 +00001768/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1769/// is known to either be off the end of the specified type or being in
1770/// alignment padding. The user type specified is known to be at most 128 bits
1771/// in size, and have passed through X86_64ABIInfo::classify with a successful
1772/// classification that put one of the two halves in the INTEGER class.
1773///
1774/// It is conservatively correct to return false.
1775static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1776 unsigned EndBit, ASTContext &Context) {
1777 // If the bytes being queried are off the end of the type, there is no user
1778 // data hiding here. This handles analysis of builtins, vectors and other
1779 // types that don't contain interesting padding.
1780 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1781 if (TySize <= StartBit)
1782 return true;
1783
Chris Lattner021c3a32010-07-29 07:43:55 +00001784 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1785 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1786 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1787
1788 // Check each element to see if the element overlaps with the queried range.
1789 for (unsigned i = 0; i != NumElts; ++i) {
1790 // If the element is after the span we care about, then we're done..
1791 unsigned EltOffset = i*EltSize;
1792 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
Chris Lattner021c3a32010-07-29 07:43:55 +00001794 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1795 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1796 EndBit-EltOffset, Context))
1797 return false;
1798 }
1799 // If it overlaps no elements, then it is safe to process as padding.
1800 return true;
1801 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001802
Chris Lattnere2962be2010-07-29 07:30:00 +00001803 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1804 const RecordDecl *RD = RT->getDecl();
1805 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001806
Chris Lattnere2962be2010-07-29 07:30:00 +00001807 // If this is a C++ record, check the bases first.
1808 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1809 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1810 e = CXXRD->bases_end(); i != e; ++i) {
1811 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1812 "Unexpected base class!");
1813 const CXXRecordDecl *Base =
1814 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001815
Chris Lattnere2962be2010-07-29 07:30:00 +00001816 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001817 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001818 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001819
Chris Lattnere2962be2010-07-29 07:30:00 +00001820 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1821 if (!BitsContainNoUserData(i->getType(), BaseStart,
1822 EndBit-BaseOffset, Context))
1823 return false;
1824 }
1825 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001826
Chris Lattnere2962be2010-07-29 07:30:00 +00001827 // Verify that no field has data that overlaps the region of interest. Yes
1828 // this could be sped up a lot by being smarter about queried fields,
1829 // however we're only looking at structs up to 16 bytes, so we don't care
1830 // much.
1831 unsigned idx = 0;
1832 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1833 i != e; ++i, ++idx) {
1834 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001835
Chris Lattnere2962be2010-07-29 07:30:00 +00001836 // If we found a field after the region we care about, then we're done.
1837 if (FieldOffset >= EndBit) break;
1838
1839 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1840 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1841 Context))
1842 return false;
1843 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001844
Chris Lattnere2962be2010-07-29 07:30:00 +00001845 // If nothing in this record overlapped the area of interest, then we're
1846 // clean.
1847 return true;
1848 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001849
Chris Lattnere2962be2010-07-29 07:30:00 +00001850 return false;
1851}
1852
Chris Lattner0b362002010-07-29 18:39:32 +00001853/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1854/// float member at the specified offset. For example, {int,{float}} has a
1855/// float at offset 4. It is conservatively correct for this routine to return
1856/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001857static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001858 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001859 // Base case if we find a float.
1860 if (IROffset == 0 && IRType->isFloatTy())
1861 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001862
Chris Lattner0b362002010-07-29 18:39:32 +00001863 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001864 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001865 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1866 unsigned Elt = SL->getElementContainingOffset(IROffset);
1867 IROffset -= SL->getElementOffset(Elt);
1868 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1869 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001870
Chris Lattner0b362002010-07-29 18:39:32 +00001871 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001872 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1873 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001874 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1875 IROffset -= IROffset/EltSize*EltSize;
1876 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1877 }
1878
1879 return false;
1880}
1881
Chris Lattnerf47c9442010-07-29 18:13:09 +00001882
1883/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1884/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001885llvm::Type *X86_64ABIInfo::
1886GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001887 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001888 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001889 // pass as float if the last 4 bytes is just padding. This happens for
1890 // structs that contain 3 floats.
1891 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1892 SourceOffset*8+64, getContext()))
1893 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001894
Chris Lattner0b362002010-07-29 18:39:32 +00001895 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1896 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1897 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001898 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1899 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001900 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001901
Chris Lattnerf47c9442010-07-29 18:13:09 +00001902 return llvm::Type::getDoubleTy(getVMContext());
1903}
1904
1905
Chris Lattner0d2656d2010-07-29 17:40:35 +00001906/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1907/// an 8-byte GPR. This means that we either have a scalar or we are talking
1908/// about the high or low part of an up-to-16-byte struct. This routine picks
1909/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001910/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1911/// etc).
1912///
1913/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1914/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1915/// the 8-byte value references. PrefType may be null.
1916///
1917/// SourceTy is the source level type for the entire argument. SourceOffset is
1918/// an offset into this that we're processing (which is always either 0 or 8).
1919///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001920llvm::Type *X86_64ABIInfo::
1921GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001922 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001923 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1924 // returning an 8-byte unit starting with it. See if we can safely use it.
1925 if (IROffset == 0) {
1926 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001927 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1928 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001929 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001930
Chris Lattnere2962be2010-07-29 07:30:00 +00001931 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1932 // goodness in the source type is just tail padding. This is allowed to
1933 // kick in for struct {double,int} on the int, but not on
1934 // struct{double,int,int} because we wouldn't return the second int. We
1935 // have to do this analysis on the source type because we can't depend on
1936 // unions being lowered a specific way etc.
1937 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001938 IRType->isIntegerTy(32) ||
1939 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1940 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1941 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001942
Chris Lattnere2962be2010-07-29 07:30:00 +00001943 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1944 SourceOffset*8+64, getContext()))
1945 return IRType;
1946 }
1947 }
Chris Lattner49382de2010-07-28 22:44:07 +00001948
Chris Lattner2acc6e32011-07-18 04:24:23 +00001949 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001950 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001951 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001952 if (IROffset < SL->getSizeInBytes()) {
1953 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1954 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001955
Chris Lattner0d2656d2010-07-29 17:40:35 +00001956 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1957 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001958 }
Chris Lattner49382de2010-07-28 22:44:07 +00001959 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001960
Chris Lattner2acc6e32011-07-18 04:24:23 +00001961 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001962 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001963 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001964 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001965 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1966 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001967 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001968
Chris Lattner49382de2010-07-28 22:44:07 +00001969 // Okay, we don't have any better idea of what to pass, so we pass this in an
1970 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001971 unsigned TySizeInBytes =
1972 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001973
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001974 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001975
Chris Lattner49382de2010-07-28 22:44:07 +00001976 // It is always safe to classify this as an integer type up to i64 that
1977 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001978 return llvm::IntegerType::get(getVMContext(),
1979 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001980}
1981
Chris Lattner66e7b682010-09-01 00:50:20 +00001982
1983/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1984/// be used as elements of a two register pair to pass or return, return a
1985/// first class aggregate to represent them. For example, if the low part of
1986/// a by-value argument should be passed as i32* and the high part as float,
1987/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001988static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001989GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001990 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001991 // In order to correctly satisfy the ABI, we need to the high part to start
1992 // at offset 8. If the high and low parts we inferred are both 4-byte types
1993 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1994 // the second element at offset 8. Check for this:
1995 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1996 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001997 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001998 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001999
Chris Lattner66e7b682010-09-01 00:50:20 +00002000 // To handle this, we have to increase the size of the low part so that the
2001 // second element will start at an 8 byte offset. We can't increase the size
2002 // of the second element because it might make us access off the end of the
2003 // struct.
2004 if (HiStart != 8) {
2005 // There are only two sorts of types the ABI generation code can produce for
2006 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2007 // Promote these to a larger type.
2008 if (Lo->isFloatTy())
2009 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2010 else {
2011 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2012 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2013 }
2014 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002015
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002016 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002017
2018
Chris Lattner66e7b682010-09-01 00:50:20 +00002019 // Verify that the second element is at an 8-byte offset.
2020 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2021 "Invalid x86-64 argument pair!");
2022 return Result;
2023}
2024
Chris Lattner519f68c2010-07-28 23:06:14 +00002025ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002026classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002027 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2028 // classification algorithm.
2029 X86_64ABIInfo::Class Lo, Hi;
2030 classify(RetTy, 0, Lo, Hi);
2031
2032 // Check some invariants.
2033 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002034 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2035
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002036 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002037 switch (Lo) {
2038 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002039 if (Hi == NoClass)
2040 return ABIArgInfo::getIgnore();
2041 // If the low part is just padding, it takes no register, leave ResType
2042 // null.
2043 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2044 "Unknown missing lo part");
2045 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002046
2047 case SSEUp:
2048 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002049 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002050
2051 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2052 // hidden argument.
2053 case Memory:
2054 return getIndirectReturnResult(RetTy);
2055
2056 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2057 // available register of the sequence %rax, %rdx is used.
2058 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002059 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002060
Chris Lattnereb518b42010-07-29 21:42:50 +00002061 // If we have a sign or zero extended integer, make sure to return Extend
2062 // so that the parameter gets the right LLVM IR attributes.
2063 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2064 // Treat an enum type as its underlying type.
2065 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2066 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002067
Chris Lattnereb518b42010-07-29 21:42:50 +00002068 if (RetTy->isIntegralOrEnumerationType() &&
2069 RetTy->isPromotableIntegerType())
2070 return ABIArgInfo::getExtend();
2071 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002072 break;
2073
2074 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2075 // available SSE register of the sequence %xmm0, %xmm1 is used.
2076 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002077 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002078 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002079
2080 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2081 // returned on the X87 stack in %st0 as 80-bit x87 number.
2082 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002083 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002084 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002085
2086 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2087 // part of the value is returned in %st0 and the imaginary part in
2088 // %st1.
2089 case ComplexX87:
2090 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002091 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002092 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002093 NULL);
2094 break;
2095 }
2096
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002097 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002098 switch (Hi) {
2099 // Memory was handled previously and X87 should
2100 // never occur as a hi class.
2101 case Memory:
2102 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002103 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002104
2105 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002106 case NoClass:
2107 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002108
Chris Lattner3db4dde2010-09-01 00:20:33 +00002109 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002110 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002111 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2112 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002113 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002114 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002115 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002116 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2117 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002118 break;
2119
2120 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002121 // is passed in the next available eightbyte chunk if the last used
2122 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002123 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002124 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002125 case SSEUp:
2126 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002127 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002128 break;
2129
2130 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2131 // returned together with the previous X87 value in %st0.
2132 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002133 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002134 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002135 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002136 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002137 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002138 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002139 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2140 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002141 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002142 break;
2143 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002144
Chris Lattner3db4dde2010-09-01 00:20:33 +00002145 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002146 // known to pass in the high eightbyte of the result. We do this by forming a
2147 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002148 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002149 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002150
Chris Lattnereb518b42010-07-29 21:42:50 +00002151 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002152}
2153
Daniel Dunbaredfac032012-03-10 01:03:58 +00002154ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2155 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2156 const
2157{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002158 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002159 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002160
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002161 // Check some invariants.
2162 // FIXME: Enforce these by construction.
2163 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002164 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2165
2166 neededInt = 0;
2167 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002168 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002169 switch (Lo) {
2170 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002171 if (Hi == NoClass)
2172 return ABIArgInfo::getIgnore();
2173 // If the low part is just padding, it takes no register, leave ResType
2174 // null.
2175 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2176 "Unknown missing lo part");
2177 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002178
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002179 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2180 // on the stack.
2181 case Memory:
2182
2183 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2184 // COMPLEX_X87, it is passed in memory.
2185 case X87:
2186 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002187 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2188 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002189 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002190
2191 case SSEUp:
2192 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002193 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002194
2195 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2196 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2197 // and %r9 is used.
2198 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002199 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002200
Chris Lattner49382de2010-07-28 22:44:07 +00002201 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002202 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002203
2204 // If we have a sign or zero extended integer, make sure to return Extend
2205 // so that the parameter gets the right LLVM IR attributes.
2206 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2207 // Treat an enum type as its underlying type.
2208 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2209 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002210
Chris Lattnereb518b42010-07-29 21:42:50 +00002211 if (Ty->isIntegralOrEnumerationType() &&
2212 Ty->isPromotableIntegerType())
2213 return ABIArgInfo::getExtend();
2214 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002215
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002216 break;
2217
2218 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2219 // available SSE register is used, the registers are taken in the
2220 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002221 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002222 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002223 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002224 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 break;
2226 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002227 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002228
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002229 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002230 switch (Hi) {
2231 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002232 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002233 // which is passed in memory.
2234 case Memory:
2235 case X87:
2236 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002237 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002238
2239 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002240
Chris Lattner645406a2010-09-01 00:24:35 +00002241 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002242 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002243 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002244 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002245
Chris Lattner645406a2010-09-01 00:24:35 +00002246 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2247 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002248 break;
2249
2250 // X87Up generally doesn't occur here (long double is passed in
2251 // memory), except in situations involving unions.
2252 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002253 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002254 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002255
Chris Lattner645406a2010-09-01 00:24:35 +00002256 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2257 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002258
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002259 ++neededSSE;
2260 break;
2261
2262 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2263 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002264 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002265 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002266 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002267 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002268 break;
2269 }
2270
Chris Lattner645406a2010-09-01 00:24:35 +00002271 // If a high part was specified, merge it together with the low part. It is
2272 // known to pass in the high eightbyte of the result. We do this by forming a
2273 // first class struct aggregate with the high and low part: {low, high}
2274 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002275 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002276
Chris Lattnereb518b42010-07-29 21:42:50 +00002277 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002278}
2279
Chris Lattneree5dcd02010-07-29 02:31:05 +00002280void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002281
Chris Lattnera3c109b2010-07-29 02:16:43 +00002282 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002283
2284 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002285 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002286
2287 // If the return value is indirect, then the hidden argument is consuming one
2288 // integer register.
2289 if (FI.getReturnInfo().isIndirect())
2290 --freeIntRegs;
2291
2292 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2293 // get assigned (in left-to-right order) for passing as follows...
2294 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2295 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002296 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002297 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2298 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002299
2300 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2301 // eightbyte of an argument, the whole argument is passed on the
2302 // stack. If registers have already been assigned for some
2303 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002304 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002305 freeIntRegs -= neededInt;
2306 freeSSERegs -= neededSSE;
2307 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002308 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002309 }
2310 }
2311}
2312
2313static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2314 QualType Ty,
2315 CodeGenFunction &CGF) {
2316 llvm::Value *overflow_arg_area_p =
2317 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2318 llvm::Value *overflow_arg_area =
2319 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2320
2321 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2322 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002323 // It isn't stated explicitly in the standard, but in practice we use
2324 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002325 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2326 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002327 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002328 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002329 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002330 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2331 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002332 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002333 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002334 overflow_arg_area =
2335 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2336 overflow_arg_area->getType(),
2337 "overflow_arg_area.align");
2338 }
2339
2340 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002341 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002342 llvm::Value *Res =
2343 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002344 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002345
2346 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2347 // l->overflow_arg_area + sizeof(type).
2348 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2349 // an 8 byte boundary.
2350
2351 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002352 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002353 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002354 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2355 "overflow_arg_area.next");
2356 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2357
2358 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2359 return Res;
2360}
2361
2362llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2363 CodeGenFunction &CGF) const {
2364 // Assume that va_list type is correct; should be pointer to LLVM type:
2365 // struct {
2366 // i32 gp_offset;
2367 // i32 fp_offset;
2368 // i8* overflow_arg_area;
2369 // i8* reg_save_area;
2370 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002371 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002372
Chris Lattnera14db752010-03-11 18:19:55 +00002373 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002374 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002375
2376 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2377 // in the registers. If not go to step 7.
2378 if (!neededInt && !neededSSE)
2379 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2380
2381 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2382 // general purpose registers needed to pass type and num_fp to hold
2383 // the number of floating point registers needed.
2384
2385 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2386 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2387 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2388 //
2389 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2390 // register save space).
2391
2392 llvm::Value *InRegs = 0;
2393 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2394 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2395 if (neededInt) {
2396 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2397 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002398 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2399 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002400 }
2401
2402 if (neededSSE) {
2403 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2404 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2405 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002406 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2407 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002408 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2409 }
2410
2411 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2412 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2413 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2414 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2415
2416 // Emit code to load the value if it was passed in registers.
2417
2418 CGF.EmitBlock(InRegBlock);
2419
2420 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2421 // an offset of l->gp_offset and/or l->fp_offset. This may require
2422 // copying to a temporary location in case the parameter is passed
2423 // in different register classes or requires an alignment greater
2424 // than 8 for general purpose registers and 16 for XMM registers.
2425 //
2426 // FIXME: This really results in shameful code when we end up needing to
2427 // collect arguments from different places; often what should result in a
2428 // simple assembling of a structure from scattered addresses has many more
2429 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002430 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002431 llvm::Value *RegAddr =
2432 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2433 "reg_save_area");
2434 if (neededInt && neededSSE) {
2435 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002436 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002437 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002438 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2439 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002440 llvm::Type *TyLo = ST->getElementType(0);
2441 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002442 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002443 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002444 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2445 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002446 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2447 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002448 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2449 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002450 llvm::Value *V =
2451 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2452 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2453 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2454 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2455
Owen Andersona1cf15f2009-07-14 23:10:40 +00002456 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002457 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002458 } else if (neededInt) {
2459 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2460 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002461 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002462 } else if (neededSSE == 1) {
2463 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2464 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2465 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002466 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002467 assert(neededSSE == 2 && "Invalid number of needed registers!");
2468 // SSE registers are spaced 16 bytes apart in the register save
2469 // area, we need to collect the two eightbytes together.
2470 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002471 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002472 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002473 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002474 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002475 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002476 DoubleTy, NULL);
2477 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2478 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2479 DblPtrTy));
2480 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2481 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2482 DblPtrTy));
2483 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2484 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2485 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002486 }
2487
2488 // AMD64-ABI 3.5.7p5: Step 5. Set:
2489 // l->gp_offset = l->gp_offset + num_gp * 8
2490 // l->fp_offset = l->fp_offset + num_fp * 16.
2491 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002492 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002493 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2494 gp_offset_p);
2495 }
2496 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002497 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002498 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2499 fp_offset_p);
2500 }
2501 CGF.EmitBranch(ContBlock);
2502
2503 // Emit code to load the value if it was passed in memory.
2504
2505 CGF.EmitBlock(InMemBlock);
2506 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2507
2508 // Return the appropriate result.
2509
2510 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002511 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002512 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002513 ResAddr->addIncoming(RegAddr, InRegBlock);
2514 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 return ResAddr;
2516}
2517
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002518ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2519
2520 if (Ty->isVoidType())
2521 return ABIArgInfo::getIgnore();
2522
2523 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2524 Ty = EnumTy->getDecl()->getIntegerType();
2525
2526 uint64_t Size = getContext().getTypeSize(Ty);
2527
2528 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002529 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2530 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002531 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2532
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002533 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2534 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002535 getContext().getTargetInfo().getTriple().getOS()
2536 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002537 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2538 Size));
2539
2540 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2541 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2542 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002543 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002544 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2545 Size));
2546
2547 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2548 }
2549
2550 if (Ty->isPromotableIntegerType())
2551 return ABIArgInfo::getExtend();
2552
2553 return ABIArgInfo::getDirect();
2554}
2555
2556void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2557
2558 QualType RetTy = FI.getReturnType();
2559 FI.getReturnInfo() = classify(RetTy);
2560
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002561 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2562 it != ie; ++it)
2563 it->info = classify(it->type);
2564}
2565
Chris Lattnerf13721d2010-08-31 16:44:54 +00002566llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2567 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002568 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002569
Chris Lattnerf13721d2010-08-31 16:44:54 +00002570 CGBuilderTy &Builder = CGF.Builder;
2571 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2572 "ap");
2573 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2574 llvm::Type *PTy =
2575 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2576 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2577
2578 uint64_t Offset =
2579 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2580 llvm::Value *NextAddr =
2581 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2582 "ap.next");
2583 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2584
2585 return AddrTyped;
2586}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002587
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002588namespace {
2589
Derek Schuff263366f2012-10-16 22:30:41 +00002590class NaClX86_64ABIInfo : public ABIInfo {
2591 public:
2592 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2593 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2594 virtual void computeInfo(CGFunctionInfo &FI) const;
2595 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2596 CodeGenFunction &CGF) const;
2597 private:
2598 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2599 X86_64ABIInfo NInfo; // Used for everything else.
2600};
2601
2602class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2603 public:
2604 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2605 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2606};
2607
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002608}
2609
Derek Schuff263366f2012-10-16 22:30:41 +00002610void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2611 if (FI.getASTCallingConvention() == CC_PnaclCall)
2612 PInfo.computeInfo(FI);
2613 else
2614 NInfo.computeInfo(FI);
2615}
2616
2617llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2618 CodeGenFunction &CGF) const {
2619 // Always use the native convention; calling pnacl-style varargs functions
2620 // is unuspported.
2621 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2622}
2623
2624
John McCallec853ba2010-03-11 00:10:12 +00002625// PowerPC-32
2626
2627namespace {
2628class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2629public:
Chris Lattnerea044322010-07-29 02:01:43 +00002630 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002631
John McCallec853ba2010-03-11 00:10:12 +00002632 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2633 // This is recovered from gcc output.
2634 return 1; // r1 is the dedicated stack pointer
2635 }
2636
2637 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002638 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002639};
2640
2641}
2642
2643bool
2644PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2645 llvm::Value *Address) const {
2646 // This is calculated from the LLVM and GCC tables and verified
2647 // against gcc output. AFAIK all ABIs use the same encoding.
2648
2649 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002650
Chris Lattner8b418682012-02-07 00:39:47 +00002651 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002652 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2653 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2654 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2655
2656 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002657 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002658
2659 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002660 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002661
2662 // 64-76 are various 4-byte special-purpose registers:
2663 // 64: mq
2664 // 65: lr
2665 // 66: ctr
2666 // 67: ap
2667 // 68-75 cr0-7
2668 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002669 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002670
2671 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002672 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002673
2674 // 109: vrsave
2675 // 110: vscr
2676 // 111: spe_acc
2677 // 112: spefscr
2678 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002679 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002680
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002681 return false;
John McCallec853ba2010-03-11 00:10:12 +00002682}
2683
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002684// PowerPC-64
2685
2686namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002687/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2688class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2689
2690public:
2691 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2692
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002693 bool isPromotableTypeForABI(QualType Ty) const;
2694
2695 ABIArgInfo classifyReturnType(QualType RetTy) const;
2696 ABIArgInfo classifyArgumentType(QualType Ty) const;
2697
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002698 // TODO: We can add more logic to computeInfo to improve performance.
2699 // Example: For aggregate arguments that fit in a register, we could
2700 // use getDirectInReg (as is done below for structs containing a single
2701 // floating-point value) to avoid pushing them to memory on function
2702 // entry. This would require changing the logic in PPCISelLowering
2703 // when lowering the parameters in the caller and args in the callee.
2704 virtual void computeInfo(CGFunctionInfo &FI) const {
2705 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2706 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2707 it != ie; ++it) {
2708 // We rely on the default argument classification for the most part.
2709 // One exception: An aggregate containing a single floating-point
2710 // item must be passed in a register if one is available.
2711 const Type *T = isSingleElementStruct(it->type, getContext());
2712 if (T) {
2713 const BuiltinType *BT = T->getAs<BuiltinType>();
2714 if (BT && BT->isFloatingPoint()) {
2715 QualType QT(T, 0);
2716 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2717 continue;
2718 }
2719 }
2720 it->info = classifyArgumentType(it->type);
2721 }
2722 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002723
2724 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2725 QualType Ty,
2726 CodeGenFunction &CGF) const;
2727};
2728
2729class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2730public:
2731 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2732 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2733
2734 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2735 // This is recovered from gcc output.
2736 return 1; // r1 is the dedicated stack pointer
2737 }
2738
2739 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2740 llvm::Value *Address) const;
2741};
2742
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002743class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2744public:
2745 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2746
2747 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2748 // This is recovered from gcc output.
2749 return 1; // r1 is the dedicated stack pointer
2750 }
2751
2752 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2753 llvm::Value *Address) const;
2754};
2755
2756}
2757
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002758// Return true if the ABI requires Ty to be passed sign- or zero-
2759// extended to 64 bits.
2760bool
2761PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2762 // Treat an enum type as its underlying type.
2763 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2764 Ty = EnumTy->getDecl()->getIntegerType();
2765
2766 // Promotable integer types are required to be promoted by the ABI.
2767 if (Ty->isPromotableIntegerType())
2768 return true;
2769
2770 // In addition to the usual promotable integer types, we also need to
2771 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2772 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2773 switch (BT->getKind()) {
2774 case BuiltinType::Int:
2775 case BuiltinType::UInt:
2776 return true;
2777 default:
2778 break;
2779 }
2780
2781 return false;
2782}
2783
2784ABIArgInfo
2785PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002786 if (Ty->isAnyComplexType())
2787 return ABIArgInfo::getDirect();
2788
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002789 if (isAggregateTypeForABI(Ty)) {
2790 // Records with non trivial destructors/constructors should not be passed
2791 // by value.
2792 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2793 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2794
2795 return ABIArgInfo::getIndirect(0);
2796 }
2797
2798 return (isPromotableTypeForABI(Ty) ?
2799 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2800}
2801
2802ABIArgInfo
2803PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2804 if (RetTy->isVoidType())
2805 return ABIArgInfo::getIgnore();
2806
2807 if (isAggregateTypeForABI(RetTy))
2808 return ABIArgInfo::getIndirect(0);
2809
2810 return (isPromotableTypeForABI(RetTy) ?
2811 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2812}
2813
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002814// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2815llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2816 QualType Ty,
2817 CodeGenFunction &CGF) const {
2818 llvm::Type *BP = CGF.Int8PtrTy;
2819 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2820
2821 CGBuilderTy &Builder = CGF.Builder;
2822 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2823 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2824
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002825 // Update the va_list pointer.
2826 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2827 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2828 llvm::Value *NextAddr =
2829 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2830 "ap.next");
2831 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2832
2833 // If the argument is smaller than 8 bytes, it is right-adjusted in
2834 // its doubleword slot. Adjust the pointer to pick it up from the
2835 // correct offset.
2836 if (SizeInBytes < 8) {
2837 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2838 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2839 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2840 }
2841
2842 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2843 return Builder.CreateBitCast(Addr, PTy);
2844}
2845
2846static bool
2847PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2848 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002849 // This is calculated from the LLVM and GCC tables and verified
2850 // against gcc output. AFAIK all ABIs use the same encoding.
2851
2852 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2853
2854 llvm::IntegerType *i8 = CGF.Int8Ty;
2855 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2856 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2857 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2858
2859 // 0-31: r0-31, the 8-byte general-purpose registers
2860 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2861
2862 // 32-63: fp0-31, the 8-byte floating-point registers
2863 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2864
2865 // 64-76 are various 4-byte special-purpose registers:
2866 // 64: mq
2867 // 65: lr
2868 // 66: ctr
2869 // 67: ap
2870 // 68-75 cr0-7
2871 // 76: xer
2872 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2873
2874 // 77-108: v0-31, the 16-byte vector registers
2875 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2876
2877 // 109: vrsave
2878 // 110: vscr
2879 // 111: spe_acc
2880 // 112: spefscr
2881 // 113: sfp
2882 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2883
2884 return false;
2885}
John McCallec853ba2010-03-11 00:10:12 +00002886
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002887bool
2888PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2889 CodeGen::CodeGenFunction &CGF,
2890 llvm::Value *Address) const {
2891
2892 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2893}
2894
2895bool
2896PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2897 llvm::Value *Address) const {
2898
2899 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2900}
2901
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002902//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002903// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002904//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002905
2906namespace {
2907
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002908class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002909public:
2910 enum ABIKind {
2911 APCS = 0,
2912 AAPCS = 1,
2913 AAPCS_VFP
2914 };
2915
2916private:
2917 ABIKind Kind;
2918
2919public:
Chris Lattnerea044322010-07-29 02:01:43 +00002920 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002921
John McCall49e34be2011-08-30 01:42:09 +00002922 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002923 StringRef Env =
2924 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002925 return (Env == "gnueabi" || Env == "eabi" ||
2926 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002927 }
2928
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002929private:
2930 ABIKind getABIKind() const { return Kind; }
2931
Chris Lattnera3c109b2010-07-29 02:16:43 +00002932 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002933 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2934 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002935 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002936 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002937
Chris Lattneree5dcd02010-07-29 02:31:05 +00002938 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002939
2940 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2941 CodeGenFunction &CGF) const;
2942};
2943
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002944class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2945public:
Chris Lattnerea044322010-07-29 02:01:43 +00002946 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2947 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002948
John McCall49e34be2011-08-30 01:42:09 +00002949 const ARMABIInfo &getABIInfo() const {
2950 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2951 }
2952
John McCall6374c332010-03-06 00:35:14 +00002953 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2954 return 13;
2955 }
Roman Divacky09345d12011-05-18 19:36:54 +00002956
Chris Lattner5f9e2722011-07-23 10:55:15 +00002957 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002958 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2959 }
2960
Roman Divacky09345d12011-05-18 19:36:54 +00002961 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2962 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002963 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002964
2965 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002966 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002967 return false;
2968 }
John McCall49e34be2011-08-30 01:42:09 +00002969
2970 unsigned getSizeOfUnwindException() const {
2971 if (getABIInfo().isEABI()) return 88;
2972 return TargetCodeGenInfo::getSizeOfUnwindException();
2973 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002974};
2975
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002976}
2977
Chris Lattneree5dcd02010-07-29 02:31:05 +00002978void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00002979 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00002980 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00002981 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
2982 // VFP registers of the appropriate type unallocated then the argument is
2983 // allocated to the lowest-numbered sequence of such registers.
2984 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
2985 // unallocated are marked as unavailable.
2986 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00002987 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00002988 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002989 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00002990 it != ie; ++it) {
2991 unsigned PreAllocation = AllocatedVFP;
2992 bool IsHA = false;
2993 // 6.1.2.3 There is one VFP co-processor register class using registers
2994 // s0-s15 (d0-d7) for passing arguments.
2995 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00002996 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00002997 // If we do not have enough VFP registers for the HA, any VFP registers
2998 // that are unallocated are marked as unavailable. To achieve this, we add
2999 // padding of (NumVFPs - PreAllocation) floats.
3000 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3001 llvm::Type *PaddingTy = llvm::ArrayType::get(
3002 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3003 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3004 }
3005 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003006
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003007 // Always honor user-specified calling convention.
3008 if (FI.getCallingConvention() != llvm::CallingConv::C)
3009 return;
3010
3011 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00003012 llvm::CallingConv::ID DefaultCC;
David Tweedb16abb12012-10-25 13:33:01 +00003013 if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf")
3014 DefaultCC = llvm::CallingConv::ARM_AAPCS_VFP;
3015 else if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00003016 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00003017 else
3018 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00003019
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003020 // If user did not ask for specific calling convention explicitly (e.g. via
3021 // pcs attribute), set effective calling convention if it's different than ABI
3022 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003023 switch (getABIKind()) {
3024 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00003025 if (DefaultCC != llvm::CallingConv::ARM_APCS)
3026 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003027 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003028 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00003029 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
3030 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003031 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003032 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003033 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
3034 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003035 break;
3036 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003037}
3038
Bob Wilson194f06a2011-08-03 05:58:22 +00003039/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3040/// aggregate. If HAMembers is non-null, the number of base elements
3041/// contained in the type is returned through it; this is used for the
3042/// recursive calls that check aggregate component types.
3043static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3044 ASTContext &Context,
3045 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003046 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003047 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3048 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3049 return false;
3050 Members *= AT->getSize().getZExtValue();
3051 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3052 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003053 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003054 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003055
Bob Wilson194f06a2011-08-03 05:58:22 +00003056 Members = 0;
3057 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3058 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003059 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003060 uint64_t FldMembers;
3061 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3062 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003063
3064 Members = (RD->isUnion() ?
3065 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003066 }
3067 } else {
3068 Members = 1;
3069 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3070 Members = 2;
3071 Ty = CT->getElementType();
3072 }
3073
3074 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3075 // double, or 64-bit or 128-bit vectors.
3076 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3077 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003078 BT->getKind() != BuiltinType::Double &&
3079 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003080 return false;
3081 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3082 unsigned VecSize = Context.getTypeSize(VT);
3083 if (VecSize != 64 && VecSize != 128)
3084 return false;
3085 } else {
3086 return false;
3087 }
3088
3089 // The base type must be the same for all members. Vector types of the
3090 // same total size are treated as being equivalent here.
3091 const Type *TyPtr = Ty.getTypePtr();
3092 if (!Base)
3093 Base = TyPtr;
3094 if (Base != TyPtr &&
3095 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3096 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3097 return false;
3098 }
3099
3100 // Homogeneous Aggregates can have at most 4 members of the base type.
3101 if (HAMembers)
3102 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003103
3104 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003105}
3106
Manman Ren710c5172012-10-31 19:02:26 +00003107/// markAllocatedVFPs - update VFPRegs according to the alignment and
3108/// number of VFP registers (unit is S register) requested.
3109static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3110 unsigned Alignment,
3111 unsigned NumRequired) {
3112 // Early Exit.
3113 if (AllocatedVFP >= 16)
3114 return;
3115 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3116 // VFP registers of the appropriate type unallocated then the argument is
3117 // allocated to the lowest-numbered sequence of such registers.
3118 for (unsigned I = 0; I < 16; I += Alignment) {
3119 bool FoundSlot = true;
3120 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3121 if (J >= 16 || VFPRegs[J]) {
3122 FoundSlot = false;
3123 break;
3124 }
3125 if (FoundSlot) {
3126 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3127 VFPRegs[J] = 1;
3128 AllocatedVFP += NumRequired;
3129 return;
3130 }
3131 }
3132 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3133 // unallocated are marked as unavailable.
3134 for (unsigned I = 0; I < 16; I++)
3135 VFPRegs[I] = 1;
3136 AllocatedVFP = 17; // We do not have enough VFP registers.
3137}
3138
3139ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3140 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003141 bool &IsHA) const {
3142 // We update number of allocated VFPs according to
3143 // 6.1.2.1 The following argument types are VFP CPRCs:
3144 // A single-precision floating-point type (including promoted
3145 // half-precision types); A double-precision floating-point type;
3146 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3147 // with a Base Type of a single- or double-precision floating-point type,
3148 // 64-bit containerized vectors or 128-bit containerized vectors with one
3149 // to four Elements.
3150
Manman Ren97f81572012-10-16 19:18:39 +00003151 // Handle illegal vector types here.
3152 if (isIllegalVectorType(Ty)) {
3153 uint64_t Size = getContext().getTypeSize(Ty);
3154 if (Size <= 32) {
3155 llvm::Type *ResType =
3156 llvm::Type::getInt32Ty(getVMContext());
3157 return ABIArgInfo::getDirect(ResType);
3158 }
3159 if (Size == 64) {
3160 llvm::Type *ResType = llvm::VectorType::get(
3161 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003162 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003163 return ABIArgInfo::getDirect(ResType);
3164 }
3165 if (Size == 128) {
3166 llvm::Type *ResType = llvm::VectorType::get(
3167 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003168 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003169 return ABIArgInfo::getDirect(ResType);
3170 }
3171 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3172 }
Manman Ren710c5172012-10-31 19:02:26 +00003173 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003174 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3175 uint64_t Size = getContext().getTypeSize(VT);
3176 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003177 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003178 }
Manman Ren710c5172012-10-31 19:02:26 +00003179 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003180 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3181 if (BT->getKind() == BuiltinType::Half ||
3182 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003183 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003184 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003185 BT->getKind() == BuiltinType::LongDouble)
3186 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003187 }
Manman Ren97f81572012-10-16 19:18:39 +00003188
John McCalld608cdb2010-08-22 10:59:02 +00003189 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003190 // Treat an enum type as its underlying type.
3191 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3192 Ty = EnumTy->getDecl()->getIntegerType();
3193
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003194 return (Ty->isPromotableIntegerType() ?
3195 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003196 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003197
Daniel Dunbar42025572009-09-14 21:54:03 +00003198 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003199 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003200 return ABIArgInfo::getIgnore();
3201
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003202 // Structures with either a non-trivial destructor or a non-trivial
3203 // copy constructor are always indirect.
3204 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3205 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3206
Bob Wilson194f06a2011-08-03 05:58:22 +00003207 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003208 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3209 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003210 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003211 uint64_t Members = 0;
3212 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003213 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003214 // Base can be a floating-point or a vector.
3215 if (Base->isVectorType()) {
3216 // ElementSize is in number of floats.
3217 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003218 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3219 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003220 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003221 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003222 else {
3223 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3224 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003225 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003226 }
3227 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003228 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003229 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003230 }
3231
Manman Ren634b3d22012-08-13 21:23:55 +00003232 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003233 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3234 // most 8-byte. We realign the indirect argument if type alignment is bigger
3235 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003236 uint64_t ABIAlign = 4;
3237 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3238 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3239 getABIKind() == ARMABIInfo::AAPCS)
3240 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003241 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3242 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003243 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003244 }
3245
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003246 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003247 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003248 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003249 // FIXME: Try to match the types of the arguments more accurately where
3250 // we can.
3251 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003252 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3253 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003254 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003255 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3256 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003257 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003258
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003259 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003260 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003261 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003262}
3263
Chris Lattnera3c109b2010-07-29 02:16:43 +00003264static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003265 llvm::LLVMContext &VMContext) {
3266 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3267 // is called integer-like if its size is less than or equal to one word, and
3268 // the offset of each of its addressable sub-fields is zero.
3269
3270 uint64_t Size = Context.getTypeSize(Ty);
3271
3272 // Check that the type fits in a word.
3273 if (Size > 32)
3274 return false;
3275
3276 // FIXME: Handle vector types!
3277 if (Ty->isVectorType())
3278 return false;
3279
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003280 // Float types are never treated as "integer like".
3281 if (Ty->isRealFloatingType())
3282 return false;
3283
Daniel Dunbar98303b92009-09-13 08:03:58 +00003284 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003285 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003286 return true;
3287
Daniel Dunbar45815812010-02-01 23:31:26 +00003288 // Small complex integer types are "integer like".
3289 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3290 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003291
3292 // Single element and zero sized arrays should be allowed, by the definition
3293 // above, but they are not.
3294
3295 // Otherwise, it must be a record type.
3296 const RecordType *RT = Ty->getAs<RecordType>();
3297 if (!RT) return false;
3298
3299 // Ignore records with flexible arrays.
3300 const RecordDecl *RD = RT->getDecl();
3301 if (RD->hasFlexibleArrayMember())
3302 return false;
3303
3304 // Check that all sub-fields are at offset 0, and are themselves "integer
3305 // like".
3306 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3307
3308 bool HadField = false;
3309 unsigned idx = 0;
3310 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3311 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003312 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003313
Daniel Dunbar679855a2010-01-29 03:22:29 +00003314 // Bit-fields are not addressable, we only need to verify they are "integer
3315 // like". We still have to disallow a subsequent non-bitfield, for example:
3316 // struct { int : 0; int x }
3317 // is non-integer like according to gcc.
3318 if (FD->isBitField()) {
3319 if (!RD->isUnion())
3320 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003321
Daniel Dunbar679855a2010-01-29 03:22:29 +00003322 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3323 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003324
Daniel Dunbar679855a2010-01-29 03:22:29 +00003325 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003326 }
3327
Daniel Dunbar679855a2010-01-29 03:22:29 +00003328 // Check if this field is at offset 0.
3329 if (Layout.getFieldOffset(idx) != 0)
3330 return false;
3331
Daniel Dunbar98303b92009-09-13 08:03:58 +00003332 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3333 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003334
Daniel Dunbar679855a2010-01-29 03:22:29 +00003335 // Only allow at most one field in a structure. This doesn't match the
3336 // wording above, but follows gcc in situations with a field following an
3337 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003338 if (!RD->isUnion()) {
3339 if (HadField)
3340 return false;
3341
3342 HadField = true;
3343 }
3344 }
3345
3346 return true;
3347}
3348
Chris Lattnera3c109b2010-07-29 02:16:43 +00003349ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003350 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003351 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003352
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003353 // Large vector types should be returned via memory.
3354 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3355 return ABIArgInfo::getIndirect(0);
3356
John McCalld608cdb2010-08-22 10:59:02 +00003357 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003358 // Treat an enum type as its underlying type.
3359 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3360 RetTy = EnumTy->getDecl()->getIntegerType();
3361
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003362 return (RetTy->isPromotableIntegerType() ?
3363 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003364 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003365
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003366 // Structures with either a non-trivial destructor or a non-trivial
3367 // copy constructor are always indirect.
3368 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3369 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3370
Daniel Dunbar98303b92009-09-13 08:03:58 +00003371 // Are we following APCS?
3372 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003373 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003374 return ABIArgInfo::getIgnore();
3375
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003376 // Complex types are all returned as packed integers.
3377 //
3378 // FIXME: Consider using 2 x vector types if the back end handles them
3379 // correctly.
3380 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003381 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003382 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003383
Daniel Dunbar98303b92009-09-13 08:03:58 +00003384 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003385 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003386 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003387 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003388 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003389 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003390 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003391 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3392 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003393 }
3394
3395 // Otherwise return in memory.
3396 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003397 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003398
3399 // Otherwise this is an AAPCS variant.
3400
Chris Lattnera3c109b2010-07-29 02:16:43 +00003401 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003402 return ABIArgInfo::getIgnore();
3403
Bob Wilson3b694fa2011-11-02 04:51:36 +00003404 // Check for homogeneous aggregates with AAPCS-VFP.
3405 if (getABIKind() == AAPCS_VFP) {
3406 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003407 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3408 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003409 // Homogeneous Aggregates are returned directly.
3410 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003411 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003412 }
3413
Daniel Dunbar98303b92009-09-13 08:03:58 +00003414 // Aggregates <= 4 bytes are returned in r0; other aggregates
3415 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003416 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003417 if (Size <= 32) {
3418 // Return in the smallest viable integer type.
3419 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003420 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003421 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003422 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3423 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003424 }
3425
Daniel Dunbar98303b92009-09-13 08:03:58 +00003426 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003427}
3428
Manman Ren97f81572012-10-16 19:18:39 +00003429/// isIllegalVector - check whether Ty is an illegal vector type.
3430bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3431 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3432 // Check whether VT is legal.
3433 unsigned NumElements = VT->getNumElements();
3434 uint64_t Size = getContext().getTypeSize(VT);
3435 // NumElements should be power of 2.
3436 if ((NumElements & (NumElements - 1)) != 0)
3437 return true;
3438 // Size should be greater than 32 bits.
3439 return Size <= 32;
3440 }
3441 return false;
3442}
3443
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003444llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003445 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003446 llvm::Type *BP = CGF.Int8PtrTy;
3447 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003448
3449 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003450 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003451 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003452
3453 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003454 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003455 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003456
3457 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3458 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003459 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3460 getABIKind() == ARMABIInfo::AAPCS)
3461 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3462 else
3463 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003464 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3465 if (isIllegalVectorType(Ty) && Size > 16) {
3466 IsIndirect = true;
3467 Size = 4;
3468 TyAlign = 4;
3469 }
Manman Rend105e732012-10-16 19:01:37 +00003470
3471 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003472 if (TyAlign > 4) {
3473 assert((TyAlign & (TyAlign - 1)) == 0 &&
3474 "Alignment is not power of 2!");
3475 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3476 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3477 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003478 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003479 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003480
3481 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003482 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003483 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003484 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003485 "ap.next");
3486 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3487
Manman Ren97f81572012-10-16 19:18:39 +00003488 if (IsIndirect)
3489 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003490 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003491 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3492 // may not be correctly aligned for the vector type. We create an aligned
3493 // temporary space and copy the content over from ap.cur to the temporary
3494 // space. This is necessary if the natural alignment of the type is greater
3495 // than the ABI alignment.
3496 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3497 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3498 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3499 "var.align");
3500 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3501 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3502 Builder.CreateMemCpy(Dst, Src,
3503 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3504 TyAlign, false);
3505 Addr = AlignedTemp; //The content is in aligned location.
3506 }
3507 llvm::Type *PTy =
3508 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3509 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3510
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003511 return AddrTyped;
3512}
3513
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003514namespace {
3515
Derek Schuff263366f2012-10-16 22:30:41 +00003516class NaClARMABIInfo : public ABIInfo {
3517 public:
3518 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3519 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3520 virtual void computeInfo(CGFunctionInfo &FI) const;
3521 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3522 CodeGenFunction &CGF) const;
3523 private:
3524 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3525 ARMABIInfo NInfo; // Used for everything else.
3526};
3527
3528class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3529 public:
3530 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3531 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3532};
3533
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003534}
3535
Derek Schuff263366f2012-10-16 22:30:41 +00003536void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3537 if (FI.getASTCallingConvention() == CC_PnaclCall)
3538 PInfo.computeInfo(FI);
3539 else
3540 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3541}
3542
3543llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3544 CodeGenFunction &CGF) const {
3545 // Always use the native convention; calling pnacl-style varargs functions
3546 // is unsupported.
3547 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3548}
3549
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003550//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003551// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003552//===----------------------------------------------------------------------===//
3553
3554namespace {
3555
Justin Holewinski2c585b92012-05-24 17:43:12 +00003556class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003557public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003558 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003559
3560 ABIArgInfo classifyReturnType(QualType RetTy) const;
3561 ABIArgInfo classifyArgumentType(QualType Ty) const;
3562
3563 virtual void computeInfo(CGFunctionInfo &FI) const;
3564 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3565 CodeGenFunction &CFG) const;
3566};
3567
Justin Holewinski2c585b92012-05-24 17:43:12 +00003568class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003569public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003570 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3571 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003572
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003573 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3574 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003575};
3576
Justin Holewinski2c585b92012-05-24 17:43:12 +00003577ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003578 if (RetTy->isVoidType())
3579 return ABIArgInfo::getIgnore();
3580 if (isAggregateTypeForABI(RetTy))
3581 return ABIArgInfo::getIndirect(0);
3582 return ABIArgInfo::getDirect();
3583}
3584
Justin Holewinski2c585b92012-05-24 17:43:12 +00003585ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003586 if (isAggregateTypeForABI(Ty))
3587 return ABIArgInfo::getIndirect(0);
3588
3589 return ABIArgInfo::getDirect();
3590}
3591
Justin Holewinski2c585b92012-05-24 17:43:12 +00003592void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003593 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3594 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3595 it != ie; ++it)
3596 it->info = classifyArgumentType(it->type);
3597
3598 // Always honor user-specified calling convention.
3599 if (FI.getCallingConvention() != llvm::CallingConv::C)
3600 return;
3601
3602 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003603 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3604 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003605 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003606 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003607 if (LangOpts.OpenCL || LangOpts.CUDA) {
3608 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003609 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003610 } else {
3611 // If we are in standard C/C++ mode, use the triple to decide on the default
3612 StringRef Env =
3613 getContext().getTargetInfo().getTriple().getEnvironmentName();
3614 if (Env == "device")
3615 DefaultCC = llvm::CallingConv::PTX_Device;
3616 else
3617 DefaultCC = llvm::CallingConv::PTX_Kernel;
3618 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003619 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003620
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003621}
3622
Justin Holewinski2c585b92012-05-24 17:43:12 +00003623llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3624 CodeGenFunction &CFG) const {
3625 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003626}
3627
Justin Holewinski2c585b92012-05-24 17:43:12 +00003628void NVPTXTargetCodeGenInfo::
3629SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3630 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003631 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3632 if (!FD) return;
3633
3634 llvm::Function *F = cast<llvm::Function>(GV);
3635
3636 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003637 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003638 // Use OpenCL function attributes to set proper calling conventions
3639 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003640 if (FD->hasAttr<OpenCLKernelAttr>()) {
3641 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003642 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003643 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003644 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003645 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003646 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003647
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003648 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003649 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003650 // CUDA __global__ functions get a kernel calling convention. Since
3651 // __global__ functions cannot be called from the device, we do not
3652 // need to set the noinline attribute.
3653 if (FD->getAttr<CUDAGlobalAttr>())
3654 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003655 }
3656}
3657
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003658}
3659
3660//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003661// MBlaze ABI Implementation
3662//===----------------------------------------------------------------------===//
3663
3664namespace {
3665
3666class MBlazeABIInfo : public ABIInfo {
3667public:
3668 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3669
3670 bool isPromotableIntegerType(QualType Ty) const;
3671
3672 ABIArgInfo classifyReturnType(QualType RetTy) const;
3673 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3674
3675 virtual void computeInfo(CGFunctionInfo &FI) const {
3676 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3677 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3678 it != ie; ++it)
3679 it->info = classifyArgumentType(it->type);
3680 }
3681
3682 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3683 CodeGenFunction &CGF) const;
3684};
3685
3686class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3687public:
3688 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3689 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3690 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3691 CodeGen::CodeGenModule &M) const;
3692};
3693
3694}
3695
3696bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3697 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3698 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3699 switch (BT->getKind()) {
3700 case BuiltinType::Bool:
3701 case BuiltinType::Char_S:
3702 case BuiltinType::Char_U:
3703 case BuiltinType::SChar:
3704 case BuiltinType::UChar:
3705 case BuiltinType::Short:
3706 case BuiltinType::UShort:
3707 return true;
3708 default:
3709 return false;
3710 }
3711 return false;
3712}
3713
3714llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3715 CodeGenFunction &CGF) const {
3716 // FIXME: Implement
3717 return 0;
3718}
3719
3720
3721ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3722 if (RetTy->isVoidType())
3723 return ABIArgInfo::getIgnore();
3724 if (isAggregateTypeForABI(RetTy))
3725 return ABIArgInfo::getIndirect(0);
3726
3727 return (isPromotableIntegerType(RetTy) ?
3728 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3729}
3730
3731ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3732 if (isAggregateTypeForABI(Ty))
3733 return ABIArgInfo::getIndirect(0);
3734
3735 return (isPromotableIntegerType(Ty) ?
3736 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3737}
3738
3739void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3740 llvm::GlobalValue *GV,
3741 CodeGen::CodeGenModule &M)
3742 const {
3743 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3744 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003745
Wesley Peck276fdf42010-12-19 19:57:51 +00003746 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3747 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3748 CC = llvm::CallingConv::MBLAZE_INTR;
3749 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3750 CC = llvm::CallingConv::MBLAZE_SVOL;
3751
3752 if (CC != llvm::CallingConv::C) {
3753 // Handle 'interrupt_handler' attribute:
3754 llvm::Function *F = cast<llvm::Function>(GV);
3755
3756 // Step 1: Set ISR calling convention.
3757 F->setCallingConv(CC);
3758
3759 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003760 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003761 }
3762
3763 // Step 3: Emit _interrupt_handler alias.
3764 if (CC == llvm::CallingConv::MBLAZE_INTR)
3765 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3766 "_interrupt_handler", GV, &M.getModule());
3767}
3768
3769
3770//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003771// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003772//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003773
3774namespace {
3775
3776class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3777public:
Chris Lattnerea044322010-07-29 02:01:43 +00003778 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3779 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003780 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3781 CodeGen::CodeGenModule &M) const;
3782};
3783
3784}
3785
3786void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3787 llvm::GlobalValue *GV,
3788 CodeGen::CodeGenModule &M) const {
3789 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3790 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3791 // Handle 'interrupt' attribute:
3792 llvm::Function *F = cast<llvm::Function>(GV);
3793
3794 // Step 1: Set ISR calling convention.
3795 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3796
3797 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003798 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003799
3800 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00003801 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003802 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00003803 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003804 GV, &M.getModule());
3805 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003806 }
3807}
3808
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003809//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003810// MIPS ABI Implementation. This works for both little-endian and
3811// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003812//===----------------------------------------------------------------------===//
3813
John McCallaeeb7012010-05-27 06:19:26 +00003814namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003815class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003816 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003817 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3818 void CoerceToIntArgs(uint64_t TySize,
3819 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003820 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003821 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003822 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003823public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003824 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003825 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3826 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003827
3828 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003829 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003830 virtual void computeInfo(CGFunctionInfo &FI) const;
3831 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3832 CodeGenFunction &CGF) const;
3833};
3834
John McCallaeeb7012010-05-27 06:19:26 +00003835class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003836 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003837public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003838 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3839 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3840 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003841
3842 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3843 return 29;
3844 }
3845
3846 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003847 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003848
3849 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003850 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003851 }
John McCallaeeb7012010-05-27 06:19:26 +00003852};
3853}
3854
Akira Hatanakac359f202012-07-03 19:24:06 +00003855void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3856 SmallVector<llvm::Type*, 8> &ArgList) const {
3857 llvm::IntegerType *IntTy =
3858 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003859
3860 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3861 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3862 ArgList.push_back(IntTy);
3863
3864 // If necessary, add one more integer type to ArgList.
3865 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3866
3867 if (R)
3868 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003869}
3870
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003871// In N32/64, an aligned double precision floating point field is passed in
3872// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003873llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003874 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3875
3876 if (IsO32) {
3877 CoerceToIntArgs(TySize, ArgList);
3878 return llvm::StructType::get(getVMContext(), ArgList);
3879 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003880
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003881 if (Ty->isComplexType())
3882 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003883
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003884 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003885
Akira Hatanakac359f202012-07-03 19:24:06 +00003886 // Unions/vectors are passed in integer registers.
3887 if (!RT || !RT->isStructureOrClassType()) {
3888 CoerceToIntArgs(TySize, ArgList);
3889 return llvm::StructType::get(getVMContext(), ArgList);
3890 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003891
3892 const RecordDecl *RD = RT->getDecl();
3893 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003894 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003895
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003896 uint64_t LastOffset = 0;
3897 unsigned idx = 0;
3898 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3899
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003900 // Iterate over fields in the struct/class and check if there are any aligned
3901 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003902 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3903 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003904 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003905 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3906
3907 if (!BT || BT->getKind() != BuiltinType::Double)
3908 continue;
3909
3910 uint64_t Offset = Layout.getFieldOffset(idx);
3911 if (Offset % 64) // Ignore doubles that are not aligned.
3912 continue;
3913
3914 // Add ((Offset - LastOffset) / 64) args of type i64.
3915 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3916 ArgList.push_back(I64);
3917
3918 // Add double type.
3919 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3920 LastOffset = Offset + 64;
3921 }
3922
Akira Hatanakac359f202012-07-03 19:24:06 +00003923 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3924 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003925
3926 return llvm::StructType::get(getVMContext(), ArgList);
3927}
3928
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003929llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003930 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003931
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003932 if ((Align - 1) & Offset)
3933 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3934
3935 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003936}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003937
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003938ABIArgInfo
3939MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003940 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003941 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003942 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003943
Akira Hatanakac359f202012-07-03 19:24:06 +00003944 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3945 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003946 Offset = llvm::RoundUpToAlignment(Offset, Align);
3947 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003948
Akira Hatanakac359f202012-07-03 19:24:06 +00003949 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003950 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003951 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003952 return ABIArgInfo::getIgnore();
3953
Akira Hatanaka511949b2011-08-01 18:09:58 +00003954 // Records with non trivial destructors/constructors should not be passed
3955 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003956 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003957 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003958 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003959 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003960
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003961 // If we have reached here, aggregates are passed directly by coercing to
3962 // another structure type. Padding is inserted if the offset of the
3963 // aggregate is unaligned.
3964 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3965 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003966 }
3967
3968 // Treat an enum type as its underlying type.
3969 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3970 Ty = EnumTy->getDecl()->getIntegerType();
3971
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003972 if (Ty->isPromotableIntegerType())
3973 return ABIArgInfo::getExtend();
3974
3975 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003976}
3977
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003978llvm::Type*
3979MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003980 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003981 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003982
Akira Hatanakada54ff32012-02-09 18:49:26 +00003983 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003984 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003985 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3986 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003987
Akira Hatanakada54ff32012-02-09 18:49:26 +00003988 // N32/64 returns struct/classes in floating point registers if the
3989 // following conditions are met:
3990 // 1. The size of the struct/class is no larger than 128-bit.
3991 // 2. The struct/class has one or two fields all of which are floating
3992 // point types.
3993 // 3. The offset of the first field is zero (this follows what gcc does).
3994 //
3995 // Any other composite results are returned in integer registers.
3996 //
3997 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3998 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3999 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004000 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004001
Akira Hatanakada54ff32012-02-09 18:49:26 +00004002 if (!BT || !BT->isFloatingPoint())
4003 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004004
David Blaikie262bc182012-04-30 02:36:29 +00004005 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004006 }
4007
4008 if (b == e)
4009 return llvm::StructType::get(getVMContext(), RTList,
4010 RD->hasAttr<PackedAttr>());
4011
4012 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004013 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004014 }
4015
Akira Hatanakac359f202012-07-03 19:24:06 +00004016 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004017 return llvm::StructType::get(getVMContext(), RTList);
4018}
4019
Akira Hatanaka619e8872011-06-02 00:09:17 +00004020ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004021 uint64_t Size = getContext().getTypeSize(RetTy);
4022
4023 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004024 return ABIArgInfo::getIgnore();
4025
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004026 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004027 if (Size <= 128) {
4028 if (RetTy->isAnyComplexType())
4029 return ABIArgInfo::getDirect();
4030
Akira Hatanakac359f202012-07-03 19:24:06 +00004031 // O32 returns integer vectors in registers.
4032 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4033 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4034
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00004035 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004036 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4037 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004038
4039 return ABIArgInfo::getIndirect(0);
4040 }
4041
4042 // Treat an enum type as its underlying type.
4043 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4044 RetTy = EnumTy->getDecl()->getIntegerType();
4045
4046 return (RetTy->isPromotableIntegerType() ?
4047 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4048}
4049
4050void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004051 ABIArgInfo &RetInfo = FI.getReturnInfo();
4052 RetInfo = classifyReturnType(FI.getReturnType());
4053
4054 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004055 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004056
Akira Hatanaka619e8872011-06-02 00:09:17 +00004057 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4058 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004059 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004060}
4061
4062llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4063 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004064 llvm::Type *BP = CGF.Int8PtrTy;
4065 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004066
4067 CGBuilderTy &Builder = CGF.Builder;
4068 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4069 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004070 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004071 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4072 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004073 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
4074 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004075
4076 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004077 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4078 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4079 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4080 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004081 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4082 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4083 }
4084 else
4085 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4086
4087 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004088 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004089 uint64_t Offset =
4090 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4091 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004092 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004093 "ap.next");
4094 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4095
4096 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004097}
4098
John McCallaeeb7012010-05-27 06:19:26 +00004099bool
4100MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4101 llvm::Value *Address) const {
4102 // This information comes from gcc's implementation, which seems to
4103 // as canonical as it gets.
4104
John McCallaeeb7012010-05-27 06:19:26 +00004105 // Everything on MIPS is 4 bytes. Double-precision FP registers
4106 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004107 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004108
4109 // 0-31 are the general purpose registers, $0 - $31.
4110 // 32-63 are the floating-point registers, $f0 - $f31.
4111 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4112 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004113 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004114
4115 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4116 // They are one bit wide and ignored here.
4117
4118 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4119 // (coprocessor 1 is the FP unit)
4120 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4121 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4122 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004123 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004124 return false;
4125}
4126
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004127//===----------------------------------------------------------------------===//
4128// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4129// Currently subclassed only to implement custom OpenCL C function attribute
4130// handling.
4131//===----------------------------------------------------------------------===//
4132
4133namespace {
4134
4135class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4136public:
4137 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4138 : DefaultTargetCodeGenInfo(CGT) {}
4139
4140 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4141 CodeGen::CodeGenModule &M) const;
4142};
4143
4144void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4145 llvm::GlobalValue *GV,
4146 CodeGen::CodeGenModule &M) const {
4147 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4148 if (!FD) return;
4149
4150 llvm::Function *F = cast<llvm::Function>(GV);
4151
David Blaikie4e4d0842012-03-11 07:00:24 +00004152 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004153 if (FD->hasAttr<OpenCLKernelAttr>()) {
4154 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00004155 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004156
4157 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4158
4159 // Convert the reqd_work_group_size() attributes to metadata.
4160 llvm::LLVMContext &Context = F->getContext();
4161 llvm::NamedMDNode *OpenCLMetadata =
4162 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4163
4164 SmallVector<llvm::Value*, 5> Operands;
4165 Operands.push_back(F);
4166
Chris Lattner8b418682012-02-07 00:39:47 +00004167 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4168 llvm::APInt(32,
4169 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4170 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4171 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004172 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004173 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4174 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004175 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4176
4177 // Add a boolean constant operand for "required" (true) or "hint" (false)
4178 // for implementing the work_group_size_hint attr later. Currently
4179 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004180 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004181 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4182 }
4183 }
4184 }
4185}
4186
4187}
John McCallaeeb7012010-05-27 06:19:26 +00004188
Tony Linthicum96319392011-12-12 21:14:55 +00004189//===----------------------------------------------------------------------===//
4190// Hexagon ABI Implementation
4191//===----------------------------------------------------------------------===//
4192
4193namespace {
4194
4195class HexagonABIInfo : public ABIInfo {
4196
4197
4198public:
4199 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4200
4201private:
4202
4203 ABIArgInfo classifyReturnType(QualType RetTy) const;
4204 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4205
4206 virtual void computeInfo(CGFunctionInfo &FI) const;
4207
4208 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4209 CodeGenFunction &CGF) const;
4210};
4211
4212class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4213public:
4214 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4215 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4216
4217 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4218 return 29;
4219 }
4220};
4221
4222}
4223
4224void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4225 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4226 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4227 it != ie; ++it)
4228 it->info = classifyArgumentType(it->type);
4229}
4230
4231ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4232 if (!isAggregateTypeForABI(Ty)) {
4233 // Treat an enum type as its underlying type.
4234 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4235 Ty = EnumTy->getDecl()->getIntegerType();
4236
4237 return (Ty->isPromotableIntegerType() ?
4238 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4239 }
4240
4241 // Ignore empty records.
4242 if (isEmptyRecord(getContext(), Ty, true))
4243 return ABIArgInfo::getIgnore();
4244
4245 // Structures with either a non-trivial destructor or a non-trivial
4246 // copy constructor are always indirect.
4247 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4248 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4249
4250 uint64_t Size = getContext().getTypeSize(Ty);
4251 if (Size > 64)
4252 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4253 // Pass in the smallest viable integer type.
4254 else if (Size > 32)
4255 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4256 else if (Size > 16)
4257 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4258 else if (Size > 8)
4259 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4260 else
4261 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4262}
4263
4264ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4265 if (RetTy->isVoidType())
4266 return ABIArgInfo::getIgnore();
4267
4268 // Large vector types should be returned via memory.
4269 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4270 return ABIArgInfo::getIndirect(0);
4271
4272 if (!isAggregateTypeForABI(RetTy)) {
4273 // Treat an enum type as its underlying type.
4274 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4275 RetTy = EnumTy->getDecl()->getIntegerType();
4276
4277 return (RetTy->isPromotableIntegerType() ?
4278 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4279 }
4280
4281 // Structures with either a non-trivial destructor or a non-trivial
4282 // copy constructor are always indirect.
4283 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4284 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4285
4286 if (isEmptyRecord(getContext(), RetTy, true))
4287 return ABIArgInfo::getIgnore();
4288
4289 // Aggregates <= 8 bytes are returned in r0; other aggregates
4290 // are returned indirectly.
4291 uint64_t Size = getContext().getTypeSize(RetTy);
4292 if (Size <= 64) {
4293 // Return in the smallest viable integer type.
4294 if (Size <= 8)
4295 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4296 if (Size <= 16)
4297 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4298 if (Size <= 32)
4299 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4300 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4301 }
4302
4303 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4304}
4305
4306llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004307 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004308 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004309 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004310
4311 CGBuilderTy &Builder = CGF.Builder;
4312 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4313 "ap");
4314 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4315 llvm::Type *PTy =
4316 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4317 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4318
4319 uint64_t Offset =
4320 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4321 llvm::Value *NextAddr =
4322 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4323 "ap.next");
4324 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4325
4326 return AddrTyped;
4327}
4328
4329
Chris Lattnerea044322010-07-29 02:01:43 +00004330const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004331 if (TheTargetCodeGenInfo)
4332 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004333
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004334 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004335 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004336 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004337 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004338
Derek Schuff9ed63f82012-09-06 17:37:28 +00004339 case llvm::Triple::le32:
4340 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004341 case llvm::Triple::mips:
4342 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004343 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004344
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004345 case llvm::Triple::mips64:
4346 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004347 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004348
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004349 case llvm::Triple::arm:
4350 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004351 {
4352 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004353 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004354 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004355 else if (CodeGenOpts.FloatABI == "hard" ||
4356 (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004357 Kind = ARMABIInfo::AAPCS_VFP;
4358
Derek Schuff263366f2012-10-16 22:30:41 +00004359 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00004360 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004361 return *(TheTargetCodeGenInfo =
4362 new NaClARMTargetCodeGenInfo(Types, Kind));
4363 default:
4364 return *(TheTargetCodeGenInfo =
4365 new ARMTargetCodeGenInfo(Types, Kind));
4366 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004367 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004368
John McCallec853ba2010-03-11 00:10:12 +00004369 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004370 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004371 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004372 if (Triple.isOSBinFormatELF())
4373 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4374 else
4375 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004376
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004377 case llvm::Triple::nvptx:
4378 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004379 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004380
Wesley Peck276fdf42010-12-19 19:57:51 +00004381 case llvm::Triple::mblaze:
4382 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4383
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004384 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004385 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004386
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004387 case llvm::Triple::tce:
4388 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4389
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004390 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004391 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004392
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004393 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004394 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004395 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4396 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004397
4398 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004399 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004400 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004401 case llvm::Triple::AuroraUX:
4402 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004403 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004404 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004405 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004406 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004407 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4408 false,
4409 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004410
4411 case llvm::Triple::Win32:
4412 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004413 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4414 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004415
4416 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004417 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004418 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4419 false,
4420 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004421 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004422 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004423
Eli Friedmanee1ad992011-12-02 00:11:43 +00004424 case llvm::Triple::x86_64: {
4425 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4426
Chris Lattnerf13721d2010-08-31 16:44:54 +00004427 switch (Triple.getOS()) {
4428 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004429 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004430 case llvm::Triple::Cygwin:
4431 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00004432 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004433 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004434 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004435 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4436 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004437 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004438 }
Tony Linthicum96319392011-12-12 21:14:55 +00004439 case llvm::Triple::hexagon:
4440 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004441 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004442}