blob: 680385357818631c9fdb6f6ad7a80e1e11e9d72b [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 Carruth3b844ba2013-01-02 11:45:17 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Type.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
Micah Villmow25a6a842012-10-08 16:25:52 +000054const llvm::DataLayout &ABIInfo::getDataLayout() const {
55 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000056}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
98 return 32;
99}
100
John McCallde5d3c72012-02-17 03:33:10 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +0000103 // The following conventions are known to require this to be false:
104 // x86_stdcall
105 // MIPS
106 // For everything else, we just prefer false unless we opt out.
107 return false;
108}
109
Daniel Dunbar98303b92009-09-13 08:03:58 +0000110static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000112/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000113/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000114static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
115 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000116 if (FD->isUnnamedBitfield())
117 return true;
118
119 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000121 // Constant arrays of empty records count as empty, strip them off.
122 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
125 if (AT->getSize() == 0)
126 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000127 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000128 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000129
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130 const RecordType *RT = FT->getAs<RecordType>();
131 if (!RT)
132 return false;
133
134 // C++ record fields are never empty, at least in the Itanium ABI.
135 //
136 // FIXME: We should use a predicate for whether this behavior is true in the
137 // current ABI.
138 if (isa<CXXRecordDecl>(RT->getDecl()))
139 return false;
140
Daniel Dunbar98303b92009-09-13 08:03:58 +0000141 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000142}
143
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000144/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000145/// fields. Note that a structure with a flexible array member is not
146/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000147static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000154
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000155 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000157 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158 e = CXXRD->bases_end(); i != e; ++i)
159 if (!isEmptyRecord(Context, i->getType(), true))
160 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000161
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
163 i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +0000164 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166 return true;
167}
168
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000169/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
170/// a non-trivial destructor or a non-trivial copy constructor.
171static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
172 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
173 if (!RD)
174 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000175
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 Wendling909b6de2013-01-23 00:21:06 +00001022 Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1023 llvm::AttributeSet::get(CGM.getLLVMContext(),
1024 llvm::AttributeSet::FunctionIndex,
1025 B));
Charles Davis74f72932010-02-13 15:54:06 +00001026 }
1027 }
1028}
1029
John McCall6374c332010-03-06 00:35:14 +00001030bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1031 CodeGen::CodeGenFunction &CGF,
1032 llvm::Value *Address) const {
1033 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +00001034
Chris Lattner8b418682012-02-07 00:39:47 +00001035 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001036
John McCall6374c332010-03-06 00:35:14 +00001037 // 0-7 are the eight integer registers; the order is different
1038 // on Darwin (for EH), but the range is the same.
1039 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +00001040 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +00001041
1042 if (CGF.CGM.isTargetDarwin()) {
1043 // 12-16 are st(0..4). Not sure why we stop at 4.
1044 // These have size 16, which is sizeof(long double) on
1045 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001046 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +00001047 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001048
John McCall6374c332010-03-06 00:35:14 +00001049 } else {
1050 // 9 is %eflags, which doesn't get a size on Darwin for some
1051 // reason.
1052 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1053
1054 // 11-16 are st(0..5). Not sure why we stop at 5.
1055 // These have size 12, which is sizeof(long double) on
1056 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001057 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001058 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1059 }
John McCall6374c332010-03-06 00:35:14 +00001060
1061 return false;
1062}
1063
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001064//===----------------------------------------------------------------------===//
1065// X86-64 ABI Implementation
1066//===----------------------------------------------------------------------===//
1067
1068
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001069namespace {
1070/// X86_64ABIInfo - The X86_64 ABI information.
1071class X86_64ABIInfo : public ABIInfo {
1072 enum Class {
1073 Integer = 0,
1074 SSE,
1075 SSEUp,
1076 X87,
1077 X87Up,
1078 ComplexX87,
1079 NoClass,
1080 Memory
1081 };
1082
1083 /// merge - Implement the X86_64 ABI merging algorithm.
1084 ///
1085 /// Merge an accumulating classification \arg Accum with a field
1086 /// classification \arg Field.
1087 ///
1088 /// \param Accum - The accumulating classification. This should
1089 /// always be either NoClass or the result of a previous merge
1090 /// call. In addition, this should never be Memory (the caller
1091 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001092 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001094 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1095 ///
1096 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1097 /// final MEMORY or SSE classes when necessary.
1098 ///
1099 /// \param AggregateSize - The size of the current aggregate in
1100 /// the classification process.
1101 ///
1102 /// \param Lo - The classification for the parts of the type
1103 /// residing in the low word of the containing object.
1104 ///
1105 /// \param Hi - The classification for the parts of the type
1106 /// residing in the higher words of the containing object.
1107 ///
1108 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1109
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001110 /// classify - Determine the x86_64 register classes in which the
1111 /// given type T should be passed.
1112 ///
1113 /// \param Lo - The classification for the parts of the type
1114 /// residing in the low word of the containing object.
1115 ///
1116 /// \param Hi - The classification for the parts of the type
1117 /// residing in the high word of the containing object.
1118 ///
1119 /// \param OffsetBase - The bit offset of this type in the
1120 /// containing object. Some parameters are classified different
1121 /// depending on whether they straddle an eightbyte boundary.
1122 ///
1123 /// If a word is unused its result will be NoClass; if a type should
1124 /// be passed in Memory then at least the classification of \arg Lo
1125 /// will be Memory.
1126 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001127 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001128 ///
1129 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1130 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001131 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001133 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001134 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1135 unsigned IROffset, QualType SourceTy,
1136 unsigned SourceOffset) const;
1137 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1138 unsigned IROffset, QualType SourceTy,
1139 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001140
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001142 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001143 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001144
1145 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001147 ///
1148 /// \param freeIntRegs - The number of free integer registers remaining
1149 /// available.
1150 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001151
Chris Lattnera3c109b2010-07-29 02:16:43 +00001152 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001153
Bill Wendlingbb465d72010-10-18 03:41:31 +00001154 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001155 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001156 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001157 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158
Eli Friedmanee1ad992011-12-02 00:11:43 +00001159 bool IsIllegalVectorType(QualType Ty) const;
1160
John McCall67a57732011-04-21 01:20:55 +00001161 /// The 0.98 ABI revision clarified a lot of ambiguities,
1162 /// unfortunately in ways that were not always consistent with
1163 /// certain previous compilers. In particular, platforms which
1164 /// required strict binary compatibility with older versions of GCC
1165 /// may need to exempt themselves.
1166 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001167 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001168 }
1169
Eli Friedmanee1ad992011-12-02 00:11:43 +00001170 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001171 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1172 // 64-bit hardware.
1173 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001174
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001176 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001177 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001178 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001179 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001180
John McCallde5d3c72012-02-17 03:33:10 +00001181 bool isPassedUsingAVXType(QualType type) const {
1182 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001183 // The freeIntRegs argument doesn't matter here.
1184 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001185 if (info.isDirect()) {
1186 llvm::Type *ty = info.getCoerceToType();
1187 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1188 return (vectorTy->getBitWidth() > 128);
1189 }
1190 return false;
1191 }
1192
Chris Lattneree5dcd02010-07-29 02:31:05 +00001193 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194
1195 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1196 CodeGenFunction &CGF) const;
1197};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001198
Chris Lattnerf13721d2010-08-31 16:44:54 +00001199/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001200class WinX86_64ABIInfo : public ABIInfo {
1201
1202 ABIArgInfo classify(QualType Ty) const;
1203
Chris Lattnerf13721d2010-08-31 16:44:54 +00001204public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001205 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1206
1207 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001208
1209 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1210 CodeGenFunction &CGF) const;
1211};
1212
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001213class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1214public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001215 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001216 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001217
John McCallde5d3c72012-02-17 03:33:10 +00001218 const X86_64ABIInfo &getABIInfo() const {
1219 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1220 }
1221
John McCall6374c332010-03-06 00:35:14 +00001222 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1223 return 7;
1224 }
1225
1226 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1227 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001228 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001229
John McCallaeeb7012010-05-27 06:19:26 +00001230 // 0-15 are the 16 integer registers.
1231 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001232 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001233 return false;
1234 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001235
Jay Foadef6de3d2011-07-11 09:56:20 +00001236 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001237 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001238 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001239 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1240 }
1241
John McCallde5d3c72012-02-17 03:33:10 +00001242 bool isNoProtoCallVariadic(const CallArgList &args,
1243 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001244 // The default CC on x86-64 sets %al to the number of SSA
1245 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001246 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001247 // that when AVX types are involved: the ABI explicitly states it is
1248 // undefined, and it doesn't work in practice because of how the ABI
1249 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001250 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001251 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001252 for (CallArgList::const_iterator
1253 it = args.begin(), ie = args.end(); it != ie; ++it) {
1254 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1255 HasAVXType = true;
1256 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001257 }
1258 }
John McCallde5d3c72012-02-17 03:33:10 +00001259
Eli Friedman3ed79032011-12-01 04:53:19 +00001260 if (!HasAVXType)
1261 return true;
1262 }
John McCall01f151e2011-09-21 08:08:30 +00001263
John McCallde5d3c72012-02-17 03:33:10 +00001264 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001265 }
1266
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001267};
1268
Chris Lattnerf13721d2010-08-31 16:44:54 +00001269class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1270public:
1271 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1272 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1273
1274 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1275 return 7;
1276 }
1277
1278 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1279 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001280 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001281
Chris Lattnerf13721d2010-08-31 16:44:54 +00001282 // 0-15 are the 16 integer registers.
1283 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001284 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001285 return false;
1286 }
1287};
1288
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001289}
1290
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001291void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1292 Class &Hi) const {
1293 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1294 //
1295 // (a) If one of the classes is Memory, the whole argument is passed in
1296 // memory.
1297 //
1298 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1299 // memory.
1300 //
1301 // (c) If the size of the aggregate exceeds two eightbytes and the first
1302 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1303 // argument is passed in memory. NOTE: This is necessary to keep the
1304 // ABI working for processors that don't support the __m256 type.
1305 //
1306 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1307 //
1308 // Some of these are enforced by the merging logic. Others can arise
1309 // only with unions; for example:
1310 // union { _Complex double; unsigned; }
1311 //
1312 // Note that clauses (b) and (c) were added in 0.98.
1313 //
1314 if (Hi == Memory)
1315 Lo = Memory;
1316 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1317 Lo = Memory;
1318 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1319 Lo = Memory;
1320 if (Hi == SSEUp && Lo != SSE)
1321 Hi = SSE;
1322}
1323
Chris Lattner1090a9b2010-06-28 21:43:59 +00001324X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001325 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1326 // classified recursively so that always two fields are
1327 // considered. The resulting class is calculated according to
1328 // the classes of the fields in the eightbyte:
1329 //
1330 // (a) If both classes are equal, this is the resulting class.
1331 //
1332 // (b) If one of the classes is NO_CLASS, the resulting class is
1333 // the other class.
1334 //
1335 // (c) If one of the classes is MEMORY, the result is the MEMORY
1336 // class.
1337 //
1338 // (d) If one of the classes is INTEGER, the result is the
1339 // INTEGER.
1340 //
1341 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1342 // MEMORY is used as class.
1343 //
1344 // (f) Otherwise class SSE is used.
1345
1346 // Accum should never be memory (we should have returned) or
1347 // ComplexX87 (because this cannot be passed in a structure).
1348 assert((Accum != Memory && Accum != ComplexX87) &&
1349 "Invalid accumulated classification during merge.");
1350 if (Accum == Field || Field == NoClass)
1351 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001352 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001353 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001354 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001355 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001356 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001357 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001358 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1359 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001360 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001361 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001362}
1363
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001364void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001365 Class &Lo, Class &Hi) const {
1366 // FIXME: This code can be simplified by introducing a simple value class for
1367 // Class pairs with appropriate constructor methods for the various
1368 // situations.
1369
1370 // FIXME: Some of the split computations are wrong; unaligned vectors
1371 // shouldn't be passed in registers for example, so there is no chance they
1372 // can straddle an eightbyte. Verify & simplify.
1373
1374 Lo = Hi = NoClass;
1375
1376 Class &Current = OffsetBase < 64 ? Lo : Hi;
1377 Current = Memory;
1378
John McCall183700f2009-09-21 23:43:11 +00001379 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001380 BuiltinType::Kind k = BT->getKind();
1381
1382 if (k == BuiltinType::Void) {
1383 Current = NoClass;
1384 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1385 Lo = Integer;
1386 Hi = Integer;
1387 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1388 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001389 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1390 (k == BuiltinType::LongDouble &&
1391 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001392 llvm::Triple::NaCl)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001393 Current = SSE;
1394 } else if (k == BuiltinType::LongDouble) {
1395 Lo = X87;
1396 Hi = X87Up;
1397 }
1398 // FIXME: _Decimal32 and _Decimal64 are SSE.
1399 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001400 return;
1401 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001402
Chris Lattner1090a9b2010-06-28 21:43:59 +00001403 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001404 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001405 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001406 return;
1407 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001408
Chris Lattner1090a9b2010-06-28 21:43:59 +00001409 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001411 return;
1412 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001413
Chris Lattner1090a9b2010-06-28 21:43:59 +00001414 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001415 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001416 Lo = Hi = Integer;
1417 else
1418 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001419 return;
1420 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001421
Chris Lattner1090a9b2010-06-28 21:43:59 +00001422 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001423 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001424 if (Size == 32) {
1425 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1426 // float> as integer.
1427 Current = Integer;
1428
1429 // If this type crosses an eightbyte boundary, it should be
1430 // split.
1431 uint64_t EB_Real = (OffsetBase) / 64;
1432 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1433 if (EB_Real != EB_Imag)
1434 Hi = Lo;
1435 } else if (Size == 64) {
1436 // gcc passes <1 x double> in memory. :(
1437 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1438 return;
1439
1440 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001441 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001442 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1443 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1444 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001445 Current = Integer;
1446 else
1447 Current = SSE;
1448
1449 // If this type crosses an eightbyte boundary, it should be
1450 // split.
1451 if (OffsetBase && OffsetBase != 64)
1452 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001453 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001454 // Arguments of 256-bits are split into four eightbyte chunks. The
1455 // least significant one belongs to class SSE and all the others to class
1456 // SSEUP. The original Lo and Hi design considers that types can't be
1457 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1458 // This design isn't correct for 256-bits, but since there're no cases
1459 // where the upper parts would need to be inspected, avoid adding
1460 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001461 Lo = SSE;
1462 Hi = SSEUp;
1463 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001464 return;
1465 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001466
Chris Lattner1090a9b2010-06-28 21:43:59 +00001467 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001468 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469
Chris Lattnerea044322010-07-29 02:01:43 +00001470 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001471 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001472 if (Size <= 64)
1473 Current = Integer;
1474 else if (Size <= 128)
1475 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001476 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001477 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001478 else if (ET == getContext().DoubleTy ||
1479 (ET == getContext().LongDoubleTy &&
1480 getContext().getTargetInfo().getTriple().getOS() ==
Eli Bendersky441d9f72012-12-04 18:38:10 +00001481 llvm::Triple::NaCl))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001483 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001484 Current = ComplexX87;
1485
1486 // If this complex type crosses an eightbyte boundary then it
1487 // should be split.
1488 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001489 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001490 if (Hi == NoClass && EB_Real != EB_Imag)
1491 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001492
Chris Lattner1090a9b2010-06-28 21:43:59 +00001493 return;
1494 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001495
Chris Lattnerea044322010-07-29 02:01:43 +00001496 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001497 // Arrays are treated like structures.
1498
Chris Lattnerea044322010-07-29 02:01:43 +00001499 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001500
1501 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001502 // than four eightbytes, ..., it has class MEMORY.
1503 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001504 return;
1505
1506 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1507 // fields, it has class MEMORY.
1508 //
1509 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001510 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001511 return;
1512
1513 // Otherwise implement simplified merge. We could be smarter about
1514 // this, but it isn't worth it and would be harder to verify.
1515 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001516 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001517 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001518
1519 // The only case a 256-bit wide vector could be used is when the array
1520 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1521 // to work for sizes wider than 128, early check and fallback to memory.
1522 if (Size > 128 && EltSize != 256)
1523 return;
1524
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001525 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1526 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001527 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001528 Lo = merge(Lo, FieldLo);
1529 Hi = merge(Hi, FieldHi);
1530 if (Lo == Memory || Hi == Memory)
1531 break;
1532 }
1533
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001534 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001535 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001536 return;
1537 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001538
Chris Lattner1090a9b2010-06-28 21:43:59 +00001539 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001540 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001541
1542 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001543 // than four eightbytes, ..., it has class MEMORY.
1544 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545 return;
1546
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001547 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1548 // copy constructor or a non-trivial destructor, it is passed by invisible
1549 // reference.
1550 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1551 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001552
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001553 const RecordDecl *RD = RT->getDecl();
1554
1555 // Assume variable sized types are passed in memory.
1556 if (RD->hasFlexibleArrayMember())
1557 return;
1558
Chris Lattnerea044322010-07-29 02:01:43 +00001559 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001560
1561 // Reset Lo class, this will be recomputed.
1562 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001563
1564 // If this is a C++ record, classify the bases first.
1565 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1566 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1567 e = CXXRD->bases_end(); i != e; ++i) {
1568 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1569 "Unexpected base class!");
1570 const CXXRecordDecl *Base =
1571 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1572
1573 // Classify this field.
1574 //
1575 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1576 // single eightbyte, each is classified separately. Each eightbyte gets
1577 // initialized to class NO_CLASS.
1578 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001579 uint64_t Offset =
1580 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001581 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001582 Lo = merge(Lo, FieldLo);
1583 Hi = merge(Hi, FieldHi);
1584 if (Lo == Memory || Hi == Memory)
1585 break;
1586 }
1587 }
1588
1589 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001590 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001591 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001592 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001593 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1594 bool BitField = i->isBitField();
1595
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001596 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1597 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001598 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001599 // The only case a 256-bit wide vector could be used is when the struct
1600 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1601 // to work for sizes wider than 128, early check and fallback to memory.
1602 //
1603 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1604 Lo = Memory;
1605 return;
1606 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001607 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001608 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001609 Lo = Memory;
1610 return;
1611 }
1612
1613 // Classify this field.
1614 //
1615 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1616 // exceeds a single eightbyte, each is classified
1617 // separately. Each eightbyte gets initialized to class
1618 // NO_CLASS.
1619 Class FieldLo, FieldHi;
1620
1621 // Bit-fields require special handling, they do not force the
1622 // structure to be passed in memory even if unaligned, and
1623 // therefore they can straddle an eightbyte.
1624 if (BitField) {
1625 // Ignore padding bit-fields.
1626 if (i->isUnnamedBitfield())
1627 continue;
1628
1629 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001630 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001631
1632 uint64_t EB_Lo = Offset / 64;
1633 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1634 FieldLo = FieldHi = NoClass;
1635 if (EB_Lo) {
1636 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1637 FieldLo = NoClass;
1638 FieldHi = Integer;
1639 } else {
1640 FieldLo = Integer;
1641 FieldHi = EB_Hi ? Integer : NoClass;
1642 }
1643 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001644 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001645 Lo = merge(Lo, FieldLo);
1646 Hi = merge(Hi, FieldHi);
1647 if (Lo == Memory || Hi == Memory)
1648 break;
1649 }
1650
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001651 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001652 }
1653}
1654
Chris Lattner9c254f02010-06-29 06:01:59 +00001655ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001656 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1657 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001658 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001659 // Treat an enum type as its underlying type.
1660 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1661 Ty = EnumTy->getDecl()->getIntegerType();
1662
1663 return (Ty->isPromotableIntegerType() ?
1664 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1665 }
1666
1667 return ABIArgInfo::getIndirect(0);
1668}
1669
Eli Friedmanee1ad992011-12-02 00:11:43 +00001670bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1671 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1672 uint64_t Size = getContext().getTypeSize(VecTy);
1673 unsigned LargestVector = HasAVX ? 256 : 128;
1674 if (Size <= 64 || Size > LargestVector)
1675 return true;
1676 }
1677
1678 return false;
1679}
1680
Daniel Dunbaredfac032012-03-10 01:03:58 +00001681ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1682 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001683 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1684 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001685 //
1686 // This assumption is optimistic, as there could be free registers available
1687 // when we need to pass this argument in memory, and LLVM could try to pass
1688 // the argument in the free register. This does not seem to happen currently,
1689 // but this code would be much safer if we could mark the argument with
1690 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001691 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001692 // Treat an enum type as its underlying type.
1693 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1694 Ty = EnumTy->getDecl()->getIntegerType();
1695
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001696 return (Ty->isPromotableIntegerType() ?
1697 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001698 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001699
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001700 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1701 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001702
Chris Lattner855d2272011-05-22 23:21:23 +00001703 // Compute the byval alignment. We specify the alignment of the byval in all
1704 // cases so that the mid-level optimizer knows the alignment of the byval.
1705 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001706
1707 // Attempt to avoid passing indirect results using byval when possible. This
1708 // is important for good codegen.
1709 //
1710 // We do this by coercing the value into a scalar type which the backend can
1711 // handle naturally (i.e., without using byval).
1712 //
1713 // For simplicity, we currently only do this when we have exhausted all of the
1714 // free integer registers. Doing this when there are free integer registers
1715 // would require more care, as we would have to ensure that the coerced value
1716 // did not claim the unused register. That would require either reording the
1717 // arguments to the function (so that any subsequent inreg values came first),
1718 // or only doing this optimization when there were no following arguments that
1719 // might be inreg.
1720 //
1721 // We currently expect it to be rare (particularly in well written code) for
1722 // arguments to be passed on the stack when there are still free integer
1723 // registers available (this would typically imply large structs being passed
1724 // by value), so this seems like a fair tradeoff for now.
1725 //
1726 // We can revisit this if the backend grows support for 'onstack' parameter
1727 // attributes. See PR12193.
1728 if (freeIntRegs == 0) {
1729 uint64_t Size = getContext().getTypeSize(Ty);
1730
1731 // If this type fits in an eightbyte, coerce it into the matching integral
1732 // type, which will end up on the stack (with alignment 8).
1733 if (Align == 8 && Size <= 64)
1734 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1735 Size));
1736 }
1737
Chris Lattner855d2272011-05-22 23:21:23 +00001738 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001739}
1740
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001741/// GetByteVectorType - The ABI specifies that a value should be passed in an
1742/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001743/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001744llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001745 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001746
Chris Lattner15842bd2010-07-29 05:02:29 +00001747 // Wrapper structs that just contain vectors are passed just like vectors,
1748 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001749 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001750 while (STy && STy->getNumElements() == 1) {
1751 IRType = STy->getElementType(0);
1752 STy = dyn_cast<llvm::StructType>(IRType);
1753 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001754
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001755 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001756 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1757 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001758 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001759 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001760 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1761 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1762 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1763 EltTy->isIntegerTy(128)))
1764 return VT;
1765 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001766
Chris Lattner0f408f52010-07-29 04:56:46 +00001767 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1768}
1769
Chris Lattnere2962be2010-07-29 07:30:00 +00001770/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1771/// is known to either be off the end of the specified type or being in
1772/// alignment padding. The user type specified is known to be at most 128 bits
1773/// in size, and have passed through X86_64ABIInfo::classify with a successful
1774/// classification that put one of the two halves in the INTEGER class.
1775///
1776/// It is conservatively correct to return false.
1777static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1778 unsigned EndBit, ASTContext &Context) {
1779 // If the bytes being queried are off the end of the type, there is no user
1780 // data hiding here. This handles analysis of builtins, vectors and other
1781 // types that don't contain interesting padding.
1782 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1783 if (TySize <= StartBit)
1784 return true;
1785
Chris Lattner021c3a32010-07-29 07:43:55 +00001786 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1787 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1788 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1789
1790 // Check each element to see if the element overlaps with the queried range.
1791 for (unsigned i = 0; i != NumElts; ++i) {
1792 // If the element is after the span we care about, then we're done..
1793 unsigned EltOffset = i*EltSize;
1794 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001795
Chris Lattner021c3a32010-07-29 07:43:55 +00001796 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1797 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1798 EndBit-EltOffset, Context))
1799 return false;
1800 }
1801 // If it overlaps no elements, then it is safe to process as padding.
1802 return true;
1803 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001804
Chris Lattnere2962be2010-07-29 07:30:00 +00001805 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1806 const RecordDecl *RD = RT->getDecl();
1807 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001808
Chris Lattnere2962be2010-07-29 07:30:00 +00001809 // If this is a C++ record, check the bases first.
1810 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1811 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1812 e = CXXRD->bases_end(); i != e; ++i) {
1813 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1814 "Unexpected base class!");
1815 const CXXRecordDecl *Base =
1816 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001817
Chris Lattnere2962be2010-07-29 07:30:00 +00001818 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001819 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001820 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001821
Chris Lattnere2962be2010-07-29 07:30:00 +00001822 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1823 if (!BitsContainNoUserData(i->getType(), BaseStart,
1824 EndBit-BaseOffset, Context))
1825 return false;
1826 }
1827 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001828
Chris Lattnere2962be2010-07-29 07:30:00 +00001829 // Verify that no field has data that overlaps the region of interest. Yes
1830 // this could be sped up a lot by being smarter about queried fields,
1831 // however we're only looking at structs up to 16 bytes, so we don't care
1832 // much.
1833 unsigned idx = 0;
1834 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1835 i != e; ++i, ++idx) {
1836 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001837
Chris Lattnere2962be2010-07-29 07:30:00 +00001838 // If we found a field after the region we care about, then we're done.
1839 if (FieldOffset >= EndBit) break;
1840
1841 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1842 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1843 Context))
1844 return false;
1845 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001846
Chris Lattnere2962be2010-07-29 07:30:00 +00001847 // If nothing in this record overlapped the area of interest, then we're
1848 // clean.
1849 return true;
1850 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001851
Chris Lattnere2962be2010-07-29 07:30:00 +00001852 return false;
1853}
1854
Chris Lattner0b362002010-07-29 18:39:32 +00001855/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1856/// float member at the specified offset. For example, {int,{float}} has a
1857/// float at offset 4. It is conservatively correct for this routine to return
1858/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001859static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001860 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001861 // Base case if we find a float.
1862 if (IROffset == 0 && IRType->isFloatTy())
1863 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001864
Chris Lattner0b362002010-07-29 18:39:32 +00001865 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001866 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001867 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1868 unsigned Elt = SL->getElementContainingOffset(IROffset);
1869 IROffset -= SL->getElementOffset(Elt);
1870 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1871 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001872
Chris Lattner0b362002010-07-29 18:39:32 +00001873 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001874 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1875 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001876 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1877 IROffset -= IROffset/EltSize*EltSize;
1878 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1879 }
1880
1881 return false;
1882}
1883
Chris Lattnerf47c9442010-07-29 18:13:09 +00001884
1885/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1886/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001887llvm::Type *X86_64ABIInfo::
1888GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001889 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001890 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001891 // pass as float if the last 4 bytes is just padding. This happens for
1892 // structs that contain 3 floats.
1893 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1894 SourceOffset*8+64, getContext()))
1895 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001896
Chris Lattner0b362002010-07-29 18:39:32 +00001897 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1898 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1899 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001900 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1901 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001902 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001903
Chris Lattnerf47c9442010-07-29 18:13:09 +00001904 return llvm::Type::getDoubleTy(getVMContext());
1905}
1906
1907
Chris Lattner0d2656d2010-07-29 17:40:35 +00001908/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1909/// an 8-byte GPR. This means that we either have a scalar or we are talking
1910/// about the high or low part of an up-to-16-byte struct. This routine picks
1911/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001912/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1913/// etc).
1914///
1915/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1916/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1917/// the 8-byte value references. PrefType may be null.
1918///
1919/// SourceTy is the source level type for the entire argument. SourceOffset is
1920/// an offset into this that we're processing (which is always either 0 or 8).
1921///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001922llvm::Type *X86_64ABIInfo::
1923GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001924 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001925 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1926 // returning an 8-byte unit starting with it. See if we can safely use it.
1927 if (IROffset == 0) {
1928 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001929 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1930 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001931 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001932
Chris Lattnere2962be2010-07-29 07:30:00 +00001933 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1934 // goodness in the source type is just tail padding. This is allowed to
1935 // kick in for struct {double,int} on the int, but not on
1936 // struct{double,int,int} because we wouldn't return the second int. We
1937 // have to do this analysis on the source type because we can't depend on
1938 // unions being lowered a specific way etc.
1939 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001940 IRType->isIntegerTy(32) ||
1941 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1942 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1943 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001944
Chris Lattnere2962be2010-07-29 07:30:00 +00001945 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1946 SourceOffset*8+64, getContext()))
1947 return IRType;
1948 }
1949 }
Chris Lattner49382de2010-07-28 22:44:07 +00001950
Chris Lattner2acc6e32011-07-18 04:24:23 +00001951 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001952 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001953 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001954 if (IROffset < SL->getSizeInBytes()) {
1955 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1956 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001957
Chris Lattner0d2656d2010-07-29 17:40:35 +00001958 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1959 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001960 }
Chris Lattner49382de2010-07-28 22:44:07 +00001961 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001962
Chris Lattner2acc6e32011-07-18 04:24:23 +00001963 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001964 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001965 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001966 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001967 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1968 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001969 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001970
Chris Lattner49382de2010-07-28 22:44:07 +00001971 // Okay, we don't have any better idea of what to pass, so we pass this in an
1972 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001973 unsigned TySizeInBytes =
1974 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001975
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001976 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001977
Chris Lattner49382de2010-07-28 22:44:07 +00001978 // It is always safe to classify this as an integer type up to i64 that
1979 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001980 return llvm::IntegerType::get(getVMContext(),
1981 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001982}
1983
Chris Lattner66e7b682010-09-01 00:50:20 +00001984
1985/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1986/// be used as elements of a two register pair to pass or return, return a
1987/// first class aggregate to represent them. For example, if the low part of
1988/// a by-value argument should be passed as i32* and the high part as float,
1989/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001990static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001991GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001992 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001993 // In order to correctly satisfy the ABI, we need to the high part to start
1994 // at offset 8. If the high and low parts we inferred are both 4-byte types
1995 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1996 // the second element at offset 8. Check for this:
1997 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1998 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001999 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00002000 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002001
Chris Lattner66e7b682010-09-01 00:50:20 +00002002 // To handle this, we have to increase the size of the low part so that the
2003 // second element will start at an 8 byte offset. We can't increase the size
2004 // of the second element because it might make us access off the end of the
2005 // struct.
2006 if (HiStart != 8) {
2007 // There are only two sorts of types the ABI generation code can produce for
2008 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2009 // Promote these to a larger type.
2010 if (Lo->isFloatTy())
2011 Lo = llvm::Type::getDoubleTy(Lo->getContext());
2012 else {
2013 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2014 Lo = llvm::Type::getInt64Ty(Lo->getContext());
2015 }
2016 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002017
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002018 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002019
2020
Chris Lattner66e7b682010-09-01 00:50:20 +00002021 // Verify that the second element is at an 8-byte offset.
2022 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2023 "Invalid x86-64 argument pair!");
2024 return Result;
2025}
2026
Chris Lattner519f68c2010-07-28 23:06:14 +00002027ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00002028classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00002029 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2030 // classification algorithm.
2031 X86_64ABIInfo::Class Lo, Hi;
2032 classify(RetTy, 0, Lo, Hi);
2033
2034 // Check some invariants.
2035 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002036 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2037
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002038 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002039 switch (Lo) {
2040 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002041 if (Hi == NoClass)
2042 return ABIArgInfo::getIgnore();
2043 // If the low part is just padding, it takes no register, leave ResType
2044 // null.
2045 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2046 "Unknown missing lo part");
2047 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002048
2049 case SSEUp:
2050 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002051 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002052
2053 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2054 // hidden argument.
2055 case Memory:
2056 return getIndirectReturnResult(RetTy);
2057
2058 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2059 // available register of the sequence %rax, %rdx is used.
2060 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002061 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002062
Chris Lattnereb518b42010-07-29 21:42:50 +00002063 // If we have a sign or zero extended integer, make sure to return Extend
2064 // so that the parameter gets the right LLVM IR attributes.
2065 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2066 // Treat an enum type as its underlying type.
2067 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2068 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002069
Chris Lattnereb518b42010-07-29 21:42:50 +00002070 if (RetTy->isIntegralOrEnumerationType() &&
2071 RetTy->isPromotableIntegerType())
2072 return ABIArgInfo::getExtend();
2073 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002074 break;
2075
2076 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2077 // available SSE register of the sequence %xmm0, %xmm1 is used.
2078 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002079 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002080 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002081
2082 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2083 // returned on the X87 stack in %st0 as 80-bit x87 number.
2084 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002085 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002086 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002087
2088 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2089 // part of the value is returned in %st0 and the imaginary part in
2090 // %st1.
2091 case ComplexX87:
2092 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002093 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002094 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002095 NULL);
2096 break;
2097 }
2098
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002099 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002100 switch (Hi) {
2101 // Memory was handled previously and X87 should
2102 // never occur as a hi class.
2103 case Memory:
2104 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002105 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002106
2107 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002108 case NoClass:
2109 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002110
Chris Lattner3db4dde2010-09-01 00:20:33 +00002111 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002112 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002113 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2114 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002115 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002116 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002117 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002118 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2119 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002120 break;
2121
2122 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002123 // is passed in the next available eightbyte chunk if the last used
2124 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002125 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002126 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002127 case SSEUp:
2128 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002129 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002130 break;
2131
2132 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2133 // returned together with the previous X87 value in %st0.
2134 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002135 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002136 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002137 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002138 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002139 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002140 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002141 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2142 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002143 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002144 break;
2145 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002146
Chris Lattner3db4dde2010-09-01 00:20:33 +00002147 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002148 // known to pass in the high eightbyte of the result. We do this by forming a
2149 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002150 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002151 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002152
Chris Lattnereb518b42010-07-29 21:42:50 +00002153 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002154}
2155
Daniel Dunbaredfac032012-03-10 01:03:58 +00002156ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2157 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2158 const
2159{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002160 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002161 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002162
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002163 // Check some invariants.
2164 // FIXME: Enforce these by construction.
2165 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002166 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2167
2168 neededInt = 0;
2169 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002170 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002171 switch (Lo) {
2172 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002173 if (Hi == NoClass)
2174 return ABIArgInfo::getIgnore();
2175 // If the low part is just padding, it takes no register, leave ResType
2176 // null.
2177 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2178 "Unknown missing lo part");
2179 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002180
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2182 // on the stack.
2183 case Memory:
2184
2185 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2186 // COMPLEX_X87, it is passed in memory.
2187 case X87:
2188 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002189 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2190 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002191 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002192
2193 case SSEUp:
2194 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002195 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002196
2197 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2198 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2199 // and %r9 is used.
2200 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002201 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002202
Chris Lattner49382de2010-07-28 22:44:07 +00002203 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002204 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002205
2206 // If we have a sign or zero extended integer, make sure to return Extend
2207 // so that the parameter gets the right LLVM IR attributes.
2208 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2209 // Treat an enum type as its underlying type.
2210 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2211 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002212
Chris Lattnereb518b42010-07-29 21:42:50 +00002213 if (Ty->isIntegralOrEnumerationType() &&
2214 Ty->isPromotableIntegerType())
2215 return ABIArgInfo::getExtend();
2216 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002217
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002218 break;
2219
2220 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2221 // available SSE register is used, the registers are taken in the
2222 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002223 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002224 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002225 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002226 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002227 break;
2228 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002229 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002230
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002231 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002232 switch (Hi) {
2233 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002234 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002235 // which is passed in memory.
2236 case Memory:
2237 case X87:
2238 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002239 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002240
2241 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002242
Chris Lattner645406a2010-09-01 00:24:35 +00002243 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002244 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002245 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002246 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002247
Chris Lattner645406a2010-09-01 00:24:35 +00002248 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2249 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002250 break;
2251
2252 // X87Up generally doesn't occur here (long double is passed in
2253 // memory), except in situations involving unions.
2254 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002255 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002256 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002257
Chris Lattner645406a2010-09-01 00:24:35 +00002258 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2259 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002260
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002261 ++neededSSE;
2262 break;
2263
2264 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2265 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002266 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002267 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002268 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002269 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002270 break;
2271 }
2272
Chris Lattner645406a2010-09-01 00:24:35 +00002273 // If a high part was specified, merge it together with the low part. It is
2274 // known to pass in the high eightbyte of the result. We do this by forming a
2275 // first class struct aggregate with the high and low part: {low, high}
2276 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002277 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002278
Chris Lattnereb518b42010-07-29 21:42:50 +00002279 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002280}
2281
Chris Lattneree5dcd02010-07-29 02:31:05 +00002282void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002283
Chris Lattnera3c109b2010-07-29 02:16:43 +00002284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002285
2286 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002287 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288
2289 // If the return value is indirect, then the hidden argument is consuming one
2290 // integer register.
2291 if (FI.getReturnInfo().isIndirect())
2292 --freeIntRegs;
2293
2294 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2295 // get assigned (in left-to-right order) for passing as follows...
2296 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2297 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002298 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002299 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2300 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002301
2302 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2303 // eightbyte of an argument, the whole argument is passed on the
2304 // stack. If registers have already been assigned for some
2305 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002306 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002307 freeIntRegs -= neededInt;
2308 freeSSERegs -= neededSSE;
2309 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002310 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002311 }
2312 }
2313}
2314
2315static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2316 QualType Ty,
2317 CodeGenFunction &CGF) {
2318 llvm::Value *overflow_arg_area_p =
2319 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2320 llvm::Value *overflow_arg_area =
2321 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2322
2323 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2324 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002325 // It isn't stated explicitly in the standard, but in practice we use
2326 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002327 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2328 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002329 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002330 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002331 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002332 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2333 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002334 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002335 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002336 overflow_arg_area =
2337 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2338 overflow_arg_area->getType(),
2339 "overflow_arg_area.align");
2340 }
2341
2342 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002343 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002344 llvm::Value *Res =
2345 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002346 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002347
2348 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2349 // l->overflow_arg_area + sizeof(type).
2350 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2351 // an 8 byte boundary.
2352
2353 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002354 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002355 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002356 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2357 "overflow_arg_area.next");
2358 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2359
2360 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2361 return Res;
2362}
2363
2364llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2365 CodeGenFunction &CGF) const {
2366 // Assume that va_list type is correct; should be pointer to LLVM type:
2367 // struct {
2368 // i32 gp_offset;
2369 // i32 fp_offset;
2370 // i8* overflow_arg_area;
2371 // i8* reg_save_area;
2372 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002373 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002374
Chris Lattnera14db752010-03-11 18:19:55 +00002375 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002376 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002377
2378 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2379 // in the registers. If not go to step 7.
2380 if (!neededInt && !neededSSE)
2381 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2382
2383 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2384 // general purpose registers needed to pass type and num_fp to hold
2385 // the number of floating point registers needed.
2386
2387 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2388 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2389 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2390 //
2391 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2392 // register save space).
2393
2394 llvm::Value *InRegs = 0;
2395 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2396 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2397 if (neededInt) {
2398 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2399 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002400 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2401 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002402 }
2403
2404 if (neededSSE) {
2405 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2406 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2407 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002408 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2409 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002410 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2411 }
2412
2413 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2414 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2415 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2416 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2417
2418 // Emit code to load the value if it was passed in registers.
2419
2420 CGF.EmitBlock(InRegBlock);
2421
2422 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2423 // an offset of l->gp_offset and/or l->fp_offset. This may require
2424 // copying to a temporary location in case the parameter is passed
2425 // in different register classes or requires an alignment greater
2426 // than 8 for general purpose registers and 16 for XMM registers.
2427 //
2428 // FIXME: This really results in shameful code when we end up needing to
2429 // collect arguments from different places; often what should result in a
2430 // simple assembling of a structure from scattered addresses has many more
2431 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002432 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002433 llvm::Value *RegAddr =
2434 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2435 "reg_save_area");
2436 if (neededInt && neededSSE) {
2437 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002438 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002439 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002440 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2441 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002442 llvm::Type *TyLo = ST->getElementType(0);
2443 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002444 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002445 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002446 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2447 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002448 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2449 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002450 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2451 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002452 llvm::Value *V =
2453 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2454 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2455 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2456 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2457
Owen Andersona1cf15f2009-07-14 23:10:40 +00002458 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002459 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002460 } else if (neededInt) {
2461 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2462 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002463 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002464 } else if (neededSSE == 1) {
2465 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2466 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2467 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002468 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002469 assert(neededSSE == 2 && "Invalid number of needed registers!");
2470 // SSE registers are spaced 16 bytes apart in the register save
2471 // area, we need to collect the two eightbytes together.
2472 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002473 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002474 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002475 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002476 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002477 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002478 DoubleTy, NULL);
2479 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2480 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2481 DblPtrTy));
2482 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2483 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2484 DblPtrTy));
2485 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2486 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2487 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002488 }
2489
2490 // AMD64-ABI 3.5.7p5: Step 5. Set:
2491 // l->gp_offset = l->gp_offset + num_gp * 8
2492 // l->fp_offset = l->fp_offset + num_fp * 16.
2493 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002494 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002495 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2496 gp_offset_p);
2497 }
2498 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002499 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002500 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2501 fp_offset_p);
2502 }
2503 CGF.EmitBranch(ContBlock);
2504
2505 // Emit code to load the value if it was passed in memory.
2506
2507 CGF.EmitBlock(InMemBlock);
2508 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2509
2510 // Return the appropriate result.
2511
2512 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002513 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002514 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002515 ResAddr->addIncoming(RegAddr, InRegBlock);
2516 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002517 return ResAddr;
2518}
2519
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002520ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2521
2522 if (Ty->isVoidType())
2523 return ABIArgInfo::getIgnore();
2524
2525 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2526 Ty = EnumTy->getDecl()->getIntegerType();
2527
2528 uint64_t Size = getContext().getTypeSize(Ty);
2529
2530 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002531 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2532 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002533 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2534
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002535 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2536 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002537 getContext().getTargetInfo().getTriple().getOS()
2538 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002539 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2540 Size));
2541
2542 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2543 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2544 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002545 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002546 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2547 Size));
2548
2549 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2550 }
2551
2552 if (Ty->isPromotableIntegerType())
2553 return ABIArgInfo::getExtend();
2554
2555 return ABIArgInfo::getDirect();
2556}
2557
2558void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2559
2560 QualType RetTy = FI.getReturnType();
2561 FI.getReturnInfo() = classify(RetTy);
2562
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002563 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2564 it != ie; ++it)
2565 it->info = classify(it->type);
2566}
2567
Chris Lattnerf13721d2010-08-31 16:44:54 +00002568llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2569 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002570 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002571
Chris Lattnerf13721d2010-08-31 16:44:54 +00002572 CGBuilderTy &Builder = CGF.Builder;
2573 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2574 "ap");
2575 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2576 llvm::Type *PTy =
2577 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2578 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2579
2580 uint64_t Offset =
2581 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2582 llvm::Value *NextAddr =
2583 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2584 "ap.next");
2585 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2586
2587 return AddrTyped;
2588}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002589
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002590namespace {
2591
Derek Schuff263366f2012-10-16 22:30:41 +00002592class NaClX86_64ABIInfo : public ABIInfo {
2593 public:
2594 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2595 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2596 virtual void computeInfo(CGFunctionInfo &FI) const;
2597 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2598 CodeGenFunction &CGF) const;
2599 private:
2600 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2601 X86_64ABIInfo NInfo; // Used for everything else.
2602};
2603
2604class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2605 public:
2606 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2607 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2608};
2609
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00002610}
2611
Derek Schuff263366f2012-10-16 22:30:41 +00002612void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2613 if (FI.getASTCallingConvention() == CC_PnaclCall)
2614 PInfo.computeInfo(FI);
2615 else
2616 NInfo.computeInfo(FI);
2617}
2618
2619llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2620 CodeGenFunction &CGF) const {
2621 // Always use the native convention; calling pnacl-style varargs functions
2622 // is unuspported.
2623 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2624}
2625
2626
John McCallec853ba2010-03-11 00:10:12 +00002627// PowerPC-32
2628
2629namespace {
2630class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2631public:
Chris Lattnerea044322010-07-29 02:01:43 +00002632 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002633
John McCallec853ba2010-03-11 00:10:12 +00002634 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2635 // This is recovered from gcc output.
2636 return 1; // r1 is the dedicated stack pointer
2637 }
2638
2639 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002640 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002641};
2642
2643}
2644
2645bool
2646PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2647 llvm::Value *Address) const {
2648 // This is calculated from the LLVM and GCC tables and verified
2649 // against gcc output. AFAIK all ABIs use the same encoding.
2650
2651 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002652
Chris Lattner8b418682012-02-07 00:39:47 +00002653 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002654 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2655 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2656 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2657
2658 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002659 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002660
2661 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002662 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002663
2664 // 64-76 are various 4-byte special-purpose registers:
2665 // 64: mq
2666 // 65: lr
2667 // 66: ctr
2668 // 67: ap
2669 // 68-75 cr0-7
2670 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002671 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002672
2673 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002674 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002675
2676 // 109: vrsave
2677 // 110: vscr
2678 // 111: spe_acc
2679 // 112: spefscr
2680 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002681 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002682
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002683 return false;
John McCallec853ba2010-03-11 00:10:12 +00002684}
2685
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002686// PowerPC-64
2687
2688namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002689/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2690class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2691
2692public:
2693 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2694
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002695 bool isPromotableTypeForABI(QualType Ty) const;
2696
2697 ABIArgInfo classifyReturnType(QualType RetTy) const;
2698 ABIArgInfo classifyArgumentType(QualType Ty) const;
2699
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002700 // TODO: We can add more logic to computeInfo to improve performance.
2701 // Example: For aggregate arguments that fit in a register, we could
2702 // use getDirectInReg (as is done below for structs containing a single
2703 // floating-point value) to avoid pushing them to memory on function
2704 // entry. This would require changing the logic in PPCISelLowering
2705 // when lowering the parameters in the caller and args in the callee.
2706 virtual void computeInfo(CGFunctionInfo &FI) const {
2707 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2708 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2709 it != ie; ++it) {
2710 // We rely on the default argument classification for the most part.
2711 // One exception: An aggregate containing a single floating-point
2712 // item must be passed in a register if one is available.
2713 const Type *T = isSingleElementStruct(it->type, getContext());
2714 if (T) {
2715 const BuiltinType *BT = T->getAs<BuiltinType>();
2716 if (BT && BT->isFloatingPoint()) {
2717 QualType QT(T, 0);
2718 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2719 continue;
2720 }
2721 }
2722 it->info = classifyArgumentType(it->type);
2723 }
2724 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002725
2726 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2727 QualType Ty,
2728 CodeGenFunction &CGF) const;
2729};
2730
2731class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2732public:
2733 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2734 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2735
2736 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2737 // This is recovered from gcc output.
2738 return 1; // r1 is the dedicated stack pointer
2739 }
2740
2741 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2742 llvm::Value *Address) const;
2743};
2744
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002745class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2746public:
2747 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2748
2749 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2750 // This is recovered from gcc output.
2751 return 1; // r1 is the dedicated stack pointer
2752 }
2753
2754 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2755 llvm::Value *Address) const;
2756};
2757
2758}
2759
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002760// Return true if the ABI requires Ty to be passed sign- or zero-
2761// extended to 64 bits.
2762bool
2763PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2764 // Treat an enum type as its underlying type.
2765 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2766 Ty = EnumTy->getDecl()->getIntegerType();
2767
2768 // Promotable integer types are required to be promoted by the ABI.
2769 if (Ty->isPromotableIntegerType())
2770 return true;
2771
2772 // In addition to the usual promotable integer types, we also need to
2773 // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2774 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2775 switch (BT->getKind()) {
2776 case BuiltinType::Int:
2777 case BuiltinType::UInt:
2778 return true;
2779 default:
2780 break;
2781 }
2782
2783 return false;
2784}
2785
2786ABIArgInfo
2787PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
Bill Schmidtc9715fc2012-11-27 02:46:43 +00002788 if (Ty->isAnyComplexType())
2789 return ABIArgInfo::getDirect();
2790
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002791 if (isAggregateTypeForABI(Ty)) {
2792 // Records with non trivial destructors/constructors should not be passed
2793 // by value.
2794 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2795 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2796
2797 return ABIArgInfo::getIndirect(0);
2798 }
2799
2800 return (isPromotableTypeForABI(Ty) ?
2801 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2802}
2803
2804ABIArgInfo
2805PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2806 if (RetTy->isVoidType())
2807 return ABIArgInfo::getIgnore();
2808
Bill Schmidt9e6111a2012-12-17 04:20:17 +00002809 if (RetTy->isAnyComplexType())
2810 return ABIArgInfo::getDirect();
2811
Ulrich Weigand71c0dcc2012-11-05 19:13:42 +00002812 if (isAggregateTypeForABI(RetTy))
2813 return ABIArgInfo::getIndirect(0);
2814
2815 return (isPromotableTypeForABI(RetTy) ?
2816 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2817}
2818
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002819// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2820llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2821 QualType Ty,
2822 CodeGenFunction &CGF) const {
2823 llvm::Type *BP = CGF.Int8PtrTy;
2824 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2825
2826 CGBuilderTy &Builder = CGF.Builder;
2827 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2828 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2829
Bill Schmidt19f8e852013-01-14 17:45:36 +00002830 // Update the va_list pointer. The pointer should be bumped by the
2831 // size of the object. We can trust getTypeSize() except for a complex
2832 // type whose base type is smaller than a doubleword. For these, the
2833 // size of the object is 16 bytes; see below for further explanation.
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002834 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
Bill Schmidt19f8e852013-01-14 17:45:36 +00002835 QualType BaseTy;
2836 unsigned CplxBaseSize = 0;
2837
2838 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2839 BaseTy = CTy->getElementType();
2840 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2841 if (CplxBaseSize < 8)
2842 SizeInBytes = 16;
2843 }
2844
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002845 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2846 llvm::Value *NextAddr =
2847 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2848 "ap.next");
2849 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2850
Bill Schmidt19f8e852013-01-14 17:45:36 +00002851 // If we have a complex type and the base type is smaller than 8 bytes,
2852 // the ABI calls for the real and imaginary parts to be right-adjusted
2853 // in separate doublewords. However, Clang expects us to produce a
2854 // pointer to a structure with the two parts packed tightly. So generate
2855 // loads of the real and imaginary parts relative to the va_list pointer,
2856 // and store them to a temporary structure.
2857 if (CplxBaseSize && CplxBaseSize < 8) {
2858 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2859 llvm::Value *ImagAddr = RealAddr;
2860 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2861 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2862 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2863 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2864 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2865 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2866 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2867 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2868 "vacplx");
2869 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2870 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2871 Builder.CreateStore(Real, RealPtr, false);
2872 Builder.CreateStore(Imag, ImagPtr, false);
2873 return Ptr;
2874 }
2875
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002876 // If the argument is smaller than 8 bytes, it is right-adjusted in
2877 // its doubleword slot. Adjust the pointer to pick it up from the
2878 // correct offset.
2879 if (SizeInBytes < 8) {
2880 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2881 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2882 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2883 }
2884
2885 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2886 return Builder.CreateBitCast(Addr, PTy);
2887}
2888
2889static bool
2890PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2891 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002892 // This is calculated from the LLVM and GCC tables and verified
2893 // against gcc output. AFAIK all ABIs use the same encoding.
2894
2895 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2896
2897 llvm::IntegerType *i8 = CGF.Int8Ty;
2898 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2899 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2900 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2901
2902 // 0-31: r0-31, the 8-byte general-purpose registers
2903 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2904
2905 // 32-63: fp0-31, the 8-byte floating-point registers
2906 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2907
2908 // 64-76 are various 4-byte special-purpose registers:
2909 // 64: mq
2910 // 65: lr
2911 // 66: ctr
2912 // 67: ap
2913 // 68-75 cr0-7
2914 // 76: xer
2915 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2916
2917 // 77-108: v0-31, the 16-byte vector registers
2918 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2919
2920 // 109: vrsave
2921 // 110: vscr
2922 // 111: spe_acc
2923 // 112: spefscr
2924 // 113: sfp
2925 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2926
2927 return false;
2928}
John McCallec853ba2010-03-11 00:10:12 +00002929
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002930bool
2931PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2932 CodeGen::CodeGenFunction &CGF,
2933 llvm::Value *Address) const {
2934
2935 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2936}
2937
2938bool
2939PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2940 llvm::Value *Address) const {
2941
2942 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2943}
2944
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002945//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002946// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002947//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002948
2949namespace {
2950
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002951class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002952public:
2953 enum ABIKind {
2954 APCS = 0,
2955 AAPCS = 1,
2956 AAPCS_VFP
2957 };
2958
2959private:
2960 ABIKind Kind;
2961
2962public:
Chris Lattnerea044322010-07-29 02:01:43 +00002963 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002964
John McCall49e34be2011-08-30 01:42:09 +00002965 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002966 StringRef Env =
2967 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002968 return (Env == "gnueabi" || Env == "eabi" ||
2969 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002970 }
2971
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002972private:
2973 ABIKind getABIKind() const { return Kind; }
2974
Chris Lattnera3c109b2010-07-29 02:16:43 +00002975 ABIArgInfo classifyReturnType(QualType RetTy) const;
Manman Ren710c5172012-10-31 19:02:26 +00002976 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
2977 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00002978 bool &IsHA) const;
Manman Ren97f81572012-10-16 19:18:39 +00002979 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002980
Chris Lattneree5dcd02010-07-29 02:31:05 +00002981 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002982
2983 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2984 CodeGenFunction &CGF) const;
2985};
2986
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002987class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2988public:
Chris Lattnerea044322010-07-29 02:01:43 +00002989 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2990 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002991
John McCall49e34be2011-08-30 01:42:09 +00002992 const ARMABIInfo &getABIInfo() const {
2993 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2994 }
2995
John McCall6374c332010-03-06 00:35:14 +00002996 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2997 return 13;
2998 }
Roman Divacky09345d12011-05-18 19:36:54 +00002999
Chris Lattner5f9e2722011-07-23 10:55:15 +00003000 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00003001 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3002 }
3003
Roman Divacky09345d12011-05-18 19:36:54 +00003004 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3005 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003006 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00003007
3008 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003009 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00003010 return false;
3011 }
John McCall49e34be2011-08-30 01:42:09 +00003012
3013 unsigned getSizeOfUnwindException() const {
3014 if (getABIInfo().isEABI()) return 88;
3015 return TargetCodeGenInfo::getSizeOfUnwindException();
3016 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003017};
3018
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003019}
3020
Chris Lattneree5dcd02010-07-29 02:31:05 +00003021void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Manman Renb3fa55f2012-10-30 23:21:41 +00003022 // To correctly handle Homogeneous Aggregate, we need to keep track of the
Manman Ren710c5172012-10-31 19:02:26 +00003023 // VFP registers allocated so far.
Manman Renb3fa55f2012-10-30 23:21:41 +00003024 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3025 // VFP registers of the appropriate type unallocated then the argument is
3026 // allocated to the lowest-numbered sequence of such registers.
3027 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3028 // unallocated are marked as unavailable.
3029 unsigned AllocatedVFP = 0;
Manman Ren710c5172012-10-31 19:02:26 +00003030 int VFPRegs[16] = { 0 };
Chris Lattnera3c109b2010-07-29 02:16:43 +00003031 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003032 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Manman Renb3fa55f2012-10-30 23:21:41 +00003033 it != ie; ++it) {
3034 unsigned PreAllocation = AllocatedVFP;
3035 bool IsHA = false;
3036 // 6.1.2.3 There is one VFP co-processor register class using registers
3037 // s0-s15 (d0-d7) for passing arguments.
3038 const unsigned NumVFPs = 16;
Manman Ren710c5172012-10-31 19:02:26 +00003039 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
Manman Renb3fa55f2012-10-30 23:21:41 +00003040 // If we do not have enough VFP registers for the HA, any VFP registers
3041 // that are unallocated are marked as unavailable. To achieve this, we add
3042 // padding of (NumVFPs - PreAllocation) floats.
3043 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3044 llvm::Type *PaddingTy = llvm::ArrayType::get(
3045 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3046 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3047 }
3048 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003049
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003050 // Always honor user-specified calling convention.
3051 if (FI.getCallingConvention() != llvm::CallingConv::C)
3052 return;
3053
3054 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00003055 llvm::CallingConv::ID DefaultCC;
David Tweedb16abb12012-10-25 13:33:01 +00003056 if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf")
3057 DefaultCC = llvm::CallingConv::ARM_AAPCS_VFP;
3058 else if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00003059 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00003060 else
3061 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00003062
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003063 // If user did not ask for specific calling convention explicitly (e.g. via
3064 // pcs attribute), set effective calling convention if it's different than ABI
3065 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003066 switch (getABIKind()) {
3067 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00003068 if (DefaultCC != llvm::CallingConv::ARM_APCS)
3069 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003070 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003071 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00003072 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
3073 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003074 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003075 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003076 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
3077 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003078 break;
3079 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003080}
3081
Bob Wilson194f06a2011-08-03 05:58:22 +00003082/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3083/// aggregate. If HAMembers is non-null, the number of base elements
3084/// contained in the type is returned through it; this is used for the
3085/// recursive calls that check aggregate component types.
3086static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3087 ASTContext &Context,
3088 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003089 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00003090 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3091 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3092 return false;
3093 Members *= AT->getSize().getZExtValue();
3094 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3095 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003096 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00003097 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003098
Bob Wilson194f06a2011-08-03 05:58:22 +00003099 Members = 0;
3100 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3101 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003102 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00003103 uint64_t FldMembers;
3104 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3105 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003106
3107 Members = (RD->isUnion() ?
3108 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00003109 }
3110 } else {
3111 Members = 1;
3112 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3113 Members = 2;
3114 Ty = CT->getElementType();
3115 }
3116
3117 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3118 // double, or 64-bit or 128-bit vectors.
3119 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3120 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00003121 BT->getKind() != BuiltinType::Double &&
3122 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00003123 return false;
3124 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3125 unsigned VecSize = Context.getTypeSize(VT);
3126 if (VecSize != 64 && VecSize != 128)
3127 return false;
3128 } else {
3129 return false;
3130 }
3131
3132 // The base type must be the same for all members. Vector types of the
3133 // same total size are treated as being equivalent here.
3134 const Type *TyPtr = Ty.getTypePtr();
3135 if (!Base)
3136 Base = TyPtr;
3137 if (Base != TyPtr &&
3138 (!Base->isVectorType() || !TyPtr->isVectorType() ||
3139 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3140 return false;
3141 }
3142
3143 // Homogeneous Aggregates can have at most 4 members of the base type.
3144 if (HAMembers)
3145 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003146
3147 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00003148}
3149
Manman Ren710c5172012-10-31 19:02:26 +00003150/// markAllocatedVFPs - update VFPRegs according to the alignment and
3151/// number of VFP registers (unit is S register) requested.
3152static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3153 unsigned Alignment,
3154 unsigned NumRequired) {
3155 // Early Exit.
3156 if (AllocatedVFP >= 16)
3157 return;
3158 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3159 // VFP registers of the appropriate type unallocated then the argument is
3160 // allocated to the lowest-numbered sequence of such registers.
3161 for (unsigned I = 0; I < 16; I += Alignment) {
3162 bool FoundSlot = true;
3163 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3164 if (J >= 16 || VFPRegs[J]) {
3165 FoundSlot = false;
3166 break;
3167 }
3168 if (FoundSlot) {
3169 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3170 VFPRegs[J] = 1;
3171 AllocatedVFP += NumRequired;
3172 return;
3173 }
3174 }
3175 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3176 // unallocated are marked as unavailable.
3177 for (unsigned I = 0; I < 16; I++)
3178 VFPRegs[I] = 1;
3179 AllocatedVFP = 17; // We do not have enough VFP registers.
3180}
3181
3182ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3183 unsigned &AllocatedVFP,
Manman Renb3fa55f2012-10-30 23:21:41 +00003184 bool &IsHA) const {
3185 // We update number of allocated VFPs according to
3186 // 6.1.2.1 The following argument types are VFP CPRCs:
3187 // A single-precision floating-point type (including promoted
3188 // half-precision types); A double-precision floating-point type;
3189 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3190 // with a Base Type of a single- or double-precision floating-point type,
3191 // 64-bit containerized vectors or 128-bit containerized vectors with one
3192 // to four Elements.
3193
Manman Ren97f81572012-10-16 19:18:39 +00003194 // Handle illegal vector types here.
3195 if (isIllegalVectorType(Ty)) {
3196 uint64_t Size = getContext().getTypeSize(Ty);
3197 if (Size <= 32) {
3198 llvm::Type *ResType =
3199 llvm::Type::getInt32Ty(getVMContext());
3200 return ABIArgInfo::getDirect(ResType);
3201 }
3202 if (Size == 64) {
3203 llvm::Type *ResType = llvm::VectorType::get(
3204 llvm::Type::getInt32Ty(getVMContext()), 2);
Manman Ren710c5172012-10-31 19:02:26 +00003205 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Ren97f81572012-10-16 19:18:39 +00003206 return ABIArgInfo::getDirect(ResType);
3207 }
3208 if (Size == 128) {
3209 llvm::Type *ResType = llvm::VectorType::get(
3210 llvm::Type::getInt32Ty(getVMContext()), 4);
Manman Ren710c5172012-10-31 19:02:26 +00003211 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
Manman Ren97f81572012-10-16 19:18:39 +00003212 return ABIArgInfo::getDirect(ResType);
3213 }
3214 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3215 }
Manman Ren710c5172012-10-31 19:02:26 +00003216 // Update VFPRegs for legal vector types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003217 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3218 uint64_t Size = getContext().getTypeSize(VT);
3219 // Size of a legal vector should be power of 2 and above 64.
Manman Ren710c5172012-10-31 19:02:26 +00003220 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
Manman Renb3fa55f2012-10-30 23:21:41 +00003221 }
Manman Ren710c5172012-10-31 19:02:26 +00003222 // Update VFPRegs for floating point types.
Manman Renb3fa55f2012-10-30 23:21:41 +00003223 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3224 if (BT->getKind() == BuiltinType::Half ||
3225 BT->getKind() == BuiltinType::Float)
Manman Ren710c5172012-10-31 19:02:26 +00003226 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
Manman Renb3fa55f2012-10-30 23:21:41 +00003227 if (BT->getKind() == BuiltinType::Double ||
Manman Ren710c5172012-10-31 19:02:26 +00003228 BT->getKind() == BuiltinType::LongDouble)
3229 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003230 }
Manman Ren97f81572012-10-16 19:18:39 +00003231
John McCalld608cdb2010-08-22 10:59:02 +00003232 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003233 // Treat an enum type as its underlying type.
3234 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3235 Ty = EnumTy->getDecl()->getIntegerType();
3236
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003237 return (Ty->isPromotableIntegerType() ?
3238 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003239 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003240
Daniel Dunbar42025572009-09-14 21:54:03 +00003241 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003242 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003243 return ABIArgInfo::getIgnore();
3244
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003245 // Structures with either a non-trivial destructor or a non-trivial
3246 // copy constructor are always indirect.
3247 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3248 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3249
Bob Wilson194f06a2011-08-03 05:58:22 +00003250 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
Manman Renb3fa55f2012-10-30 23:21:41 +00003251 // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3252 // into VFP registers.
Bob Wilson194f06a2011-08-03 05:58:22 +00003253 const Type *Base = 0;
Manman Renb3fa55f2012-10-30 23:21:41 +00003254 uint64_t Members = 0;
3255 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003256 assert(Base && "Base class should be set for homogeneous aggregate");
Manman Renb3fa55f2012-10-30 23:21:41 +00003257 // Base can be a floating-point or a vector.
3258 if (Base->isVectorType()) {
3259 // ElementSize is in number of floats.
3260 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
Manman Rencb489dd2012-11-06 19:05:29 +00003261 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3262 Members * ElementSize);
Manman Renb3fa55f2012-10-30 23:21:41 +00003263 } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
Manman Ren710c5172012-10-31 19:02:26 +00003264 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
Manman Renb3fa55f2012-10-30 23:21:41 +00003265 else {
3266 assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3267 Base->isSpecificBuiltinType(BuiltinType::LongDouble));
Manman Ren710c5172012-10-31 19:02:26 +00003268 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
Manman Renb3fa55f2012-10-30 23:21:41 +00003269 }
3270 IsHA = true;
Bob Wilson194f06a2011-08-03 05:58:22 +00003271 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003272 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003273 }
3274
Manman Ren634b3d22012-08-13 21:23:55 +00003275 // Support byval for ARM.
Manman Rencb489dd2012-11-06 19:05:29 +00003276 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3277 // most 8-byte. We realign the indirect argument if type alignment is bigger
3278 // than ABI alignment.
Manman Renfd1ba912012-11-05 22:42:46 +00003279 uint64_t ABIAlign = 4;
3280 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3281 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3282 getABIKind() == ARMABIInfo::AAPCS)
3283 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
Manman Ren885ad692012-11-06 04:58:01 +00003284 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3285 return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
Manman Rencb489dd2012-11-06 19:05:29 +00003286 /*Realign=*/TyAlign > ABIAlign);
Eli Friedman79f30982012-08-09 00:31:40 +00003287 }
3288
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003289 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003290 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003291 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003292 // FIXME: Try to match the types of the arguments more accurately where
3293 // we can.
3294 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003295 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3296 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003297 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003298 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3299 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003300 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003301
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003302 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003303 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003304 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003305}
3306
Chris Lattnera3c109b2010-07-29 02:16:43 +00003307static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003308 llvm::LLVMContext &VMContext) {
3309 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3310 // is called integer-like if its size is less than or equal to one word, and
3311 // the offset of each of its addressable sub-fields is zero.
3312
3313 uint64_t Size = Context.getTypeSize(Ty);
3314
3315 // Check that the type fits in a word.
3316 if (Size > 32)
3317 return false;
3318
3319 // FIXME: Handle vector types!
3320 if (Ty->isVectorType())
3321 return false;
3322
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003323 // Float types are never treated as "integer like".
3324 if (Ty->isRealFloatingType())
3325 return false;
3326
Daniel Dunbar98303b92009-09-13 08:03:58 +00003327 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003328 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003329 return true;
3330
Daniel Dunbar45815812010-02-01 23:31:26 +00003331 // Small complex integer types are "integer like".
3332 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3333 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003334
3335 // Single element and zero sized arrays should be allowed, by the definition
3336 // above, but they are not.
3337
3338 // Otherwise, it must be a record type.
3339 const RecordType *RT = Ty->getAs<RecordType>();
3340 if (!RT) return false;
3341
3342 // Ignore records with flexible arrays.
3343 const RecordDecl *RD = RT->getDecl();
3344 if (RD->hasFlexibleArrayMember())
3345 return false;
3346
3347 // Check that all sub-fields are at offset 0, and are themselves "integer
3348 // like".
3349 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3350
3351 bool HadField = false;
3352 unsigned idx = 0;
3353 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3354 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003355 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003356
Daniel Dunbar679855a2010-01-29 03:22:29 +00003357 // Bit-fields are not addressable, we only need to verify they are "integer
3358 // like". We still have to disallow a subsequent non-bitfield, for example:
3359 // struct { int : 0; int x }
3360 // is non-integer like according to gcc.
3361 if (FD->isBitField()) {
3362 if (!RD->isUnion())
3363 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003364
Daniel Dunbar679855a2010-01-29 03:22:29 +00003365 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3366 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003367
Daniel Dunbar679855a2010-01-29 03:22:29 +00003368 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003369 }
3370
Daniel Dunbar679855a2010-01-29 03:22:29 +00003371 // Check if this field is at offset 0.
3372 if (Layout.getFieldOffset(idx) != 0)
3373 return false;
3374
Daniel Dunbar98303b92009-09-13 08:03:58 +00003375 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3376 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003377
Daniel Dunbar679855a2010-01-29 03:22:29 +00003378 // Only allow at most one field in a structure. This doesn't match the
3379 // wording above, but follows gcc in situations with a field following an
3380 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003381 if (!RD->isUnion()) {
3382 if (HadField)
3383 return false;
3384
3385 HadField = true;
3386 }
3387 }
3388
3389 return true;
3390}
3391
Chris Lattnera3c109b2010-07-29 02:16:43 +00003392ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003393 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003394 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003395
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003396 // Large vector types should be returned via memory.
3397 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3398 return ABIArgInfo::getIndirect(0);
3399
John McCalld608cdb2010-08-22 10:59:02 +00003400 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003401 // Treat an enum type as its underlying type.
3402 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3403 RetTy = EnumTy->getDecl()->getIntegerType();
3404
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003405 return (RetTy->isPromotableIntegerType() ?
3406 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003407 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003408
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003409 // Structures with either a non-trivial destructor or a non-trivial
3410 // copy constructor are always indirect.
3411 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3412 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3413
Daniel Dunbar98303b92009-09-13 08:03:58 +00003414 // Are we following APCS?
3415 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003416 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003417 return ABIArgInfo::getIgnore();
3418
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003419 // Complex types are all returned as packed integers.
3420 //
3421 // FIXME: Consider using 2 x vector types if the back end handles them
3422 // correctly.
3423 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003424 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003425 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003426
Daniel Dunbar98303b92009-09-13 08:03:58 +00003427 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003428 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003429 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003430 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003431 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003432 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003433 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003434 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3435 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003436 }
3437
3438 // Otherwise return in memory.
3439 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003440 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003441
3442 // Otherwise this is an AAPCS variant.
3443
Chris Lattnera3c109b2010-07-29 02:16:43 +00003444 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003445 return ABIArgInfo::getIgnore();
3446
Bob Wilson3b694fa2011-11-02 04:51:36 +00003447 // Check for homogeneous aggregates with AAPCS-VFP.
3448 if (getABIKind() == AAPCS_VFP) {
3449 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003450 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3451 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003452 // Homogeneous Aggregates are returned directly.
3453 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003454 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003455 }
3456
Daniel Dunbar98303b92009-09-13 08:03:58 +00003457 // Aggregates <= 4 bytes are returned in r0; other aggregates
3458 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003459 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003460 if (Size <= 32) {
3461 // Return in the smallest viable integer type.
3462 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003463 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003464 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003465 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3466 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003467 }
3468
Daniel Dunbar98303b92009-09-13 08:03:58 +00003469 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003470}
3471
Manman Ren97f81572012-10-16 19:18:39 +00003472/// isIllegalVector - check whether Ty is an illegal vector type.
3473bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3474 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3475 // Check whether VT is legal.
3476 unsigned NumElements = VT->getNumElements();
3477 uint64_t Size = getContext().getTypeSize(VT);
3478 // NumElements should be power of 2.
3479 if ((NumElements & (NumElements - 1)) != 0)
3480 return true;
3481 // Size should be greater than 32 bits.
3482 return Size <= 32;
3483 }
3484 return false;
3485}
3486
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003487llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003488 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003489 llvm::Type *BP = CGF.Int8PtrTy;
3490 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003491
3492 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003493 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003494 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003495
3496 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003497 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003498 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003499
3500 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3501 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003502 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3503 getABIKind() == ARMABIInfo::AAPCS)
3504 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3505 else
3506 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003507 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3508 if (isIllegalVectorType(Ty) && Size > 16) {
3509 IsIndirect = true;
3510 Size = 4;
3511 TyAlign = 4;
3512 }
Manman Rend105e732012-10-16 19:01:37 +00003513
3514 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003515 if (TyAlign > 4) {
3516 assert((TyAlign & (TyAlign - 1)) == 0 &&
3517 "Alignment is not power of 2!");
3518 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3519 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3520 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003521 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003522 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003523
3524 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003525 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003526 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003527 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003528 "ap.next");
3529 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3530
Manman Ren97f81572012-10-16 19:18:39 +00003531 if (IsIndirect)
3532 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003533 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003534 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3535 // may not be correctly aligned for the vector type. We create an aligned
3536 // temporary space and copy the content over from ap.cur to the temporary
3537 // space. This is necessary if the natural alignment of the type is greater
3538 // than the ABI alignment.
3539 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3540 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3541 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3542 "var.align");
3543 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3544 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3545 Builder.CreateMemCpy(Dst, Src,
3546 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3547 TyAlign, false);
3548 Addr = AlignedTemp; //The content is in aligned location.
3549 }
3550 llvm::Type *PTy =
3551 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3552 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3553
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003554 return AddrTyped;
3555}
3556
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003557namespace {
3558
Derek Schuff263366f2012-10-16 22:30:41 +00003559class NaClARMABIInfo : public ABIInfo {
3560 public:
3561 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3562 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3563 virtual void computeInfo(CGFunctionInfo &FI) const;
3564 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3565 CodeGenFunction &CGF) const;
3566 private:
3567 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3568 ARMABIInfo NInfo; // Used for everything else.
3569};
3570
3571class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3572 public:
3573 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3574 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3575};
3576
Benjamin Kramerc6f84cf2012-10-20 13:02:06 +00003577}
3578
Derek Schuff263366f2012-10-16 22:30:41 +00003579void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3580 if (FI.getASTCallingConvention() == CC_PnaclCall)
3581 PInfo.computeInfo(FI);
3582 else
3583 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3584}
3585
3586llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3587 CodeGenFunction &CGF) const {
3588 // Always use the native convention; calling pnacl-style varargs functions
3589 // is unsupported.
3590 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3591}
3592
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003593//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003594// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003595//===----------------------------------------------------------------------===//
3596
3597namespace {
3598
Justin Holewinski2c585b92012-05-24 17:43:12 +00003599class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003600public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003601 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003602
3603 ABIArgInfo classifyReturnType(QualType RetTy) const;
3604 ABIArgInfo classifyArgumentType(QualType Ty) const;
3605
3606 virtual void computeInfo(CGFunctionInfo &FI) const;
3607 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3608 CodeGenFunction &CFG) const;
3609};
3610
Justin Holewinski2c585b92012-05-24 17:43:12 +00003611class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003612public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003613 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3614 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003615
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003616 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3617 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003618};
3619
Justin Holewinski2c585b92012-05-24 17:43:12 +00003620ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003621 if (RetTy->isVoidType())
3622 return ABIArgInfo::getIgnore();
3623 if (isAggregateTypeForABI(RetTy))
3624 return ABIArgInfo::getIndirect(0);
3625 return ABIArgInfo::getDirect();
3626}
3627
Justin Holewinski2c585b92012-05-24 17:43:12 +00003628ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003629 if (isAggregateTypeForABI(Ty))
3630 return ABIArgInfo::getIndirect(0);
3631
3632 return ABIArgInfo::getDirect();
3633}
3634
Justin Holewinski2c585b92012-05-24 17:43:12 +00003635void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003636 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3637 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3638 it != ie; ++it)
3639 it->info = classifyArgumentType(it->type);
3640
3641 // Always honor user-specified calling convention.
3642 if (FI.getCallingConvention() != llvm::CallingConv::C)
3643 return;
3644
3645 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003646 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3647 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003648 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003649 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003650 if (LangOpts.OpenCL || LangOpts.CUDA) {
3651 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003652 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003653 } else {
3654 // If we are in standard C/C++ mode, use the triple to decide on the default
3655 StringRef Env =
3656 getContext().getTargetInfo().getTriple().getEnvironmentName();
3657 if (Env == "device")
3658 DefaultCC = llvm::CallingConv::PTX_Device;
3659 else
3660 DefaultCC = llvm::CallingConv::PTX_Kernel;
3661 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003662 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003663
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003664}
3665
Justin Holewinski2c585b92012-05-24 17:43:12 +00003666llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3667 CodeGenFunction &CFG) const {
3668 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003669}
3670
Justin Holewinski2c585b92012-05-24 17:43:12 +00003671void NVPTXTargetCodeGenInfo::
3672SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3673 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003674 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3675 if (!FD) return;
3676
3677 llvm::Function *F = cast<llvm::Function>(GV);
3678
3679 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003680 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003681 // Use OpenCL function attributes to set proper calling conventions
3682 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003683 if (FD->hasAttr<OpenCLKernelAttr>()) {
3684 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003685 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003686 // And kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00003687 F->addFnAttr(llvm::Attribute::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003688 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003689 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003690
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003691 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003692 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003693 // CUDA __global__ functions get a kernel calling convention. Since
3694 // __global__ functions cannot be called from the device, we do not
3695 // need to set the noinline attribute.
3696 if (FD->getAttr<CUDAGlobalAttr>())
3697 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003698 }
3699}
3700
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003701}
3702
3703//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003704// MBlaze ABI Implementation
3705//===----------------------------------------------------------------------===//
3706
3707namespace {
3708
3709class MBlazeABIInfo : public ABIInfo {
3710public:
3711 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3712
3713 bool isPromotableIntegerType(QualType Ty) const;
3714
3715 ABIArgInfo classifyReturnType(QualType RetTy) const;
3716 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3717
3718 virtual void computeInfo(CGFunctionInfo &FI) const {
3719 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3720 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3721 it != ie; ++it)
3722 it->info = classifyArgumentType(it->type);
3723 }
3724
3725 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3726 CodeGenFunction &CGF) const;
3727};
3728
3729class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3730public:
3731 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3732 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3733 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3734 CodeGen::CodeGenModule &M) const;
3735};
3736
3737}
3738
3739bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3740 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3741 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3742 switch (BT->getKind()) {
3743 case BuiltinType::Bool:
3744 case BuiltinType::Char_S:
3745 case BuiltinType::Char_U:
3746 case BuiltinType::SChar:
3747 case BuiltinType::UChar:
3748 case BuiltinType::Short:
3749 case BuiltinType::UShort:
3750 return true;
3751 default:
3752 return false;
3753 }
3754 return false;
3755}
3756
3757llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3758 CodeGenFunction &CGF) const {
3759 // FIXME: Implement
3760 return 0;
3761}
3762
3763
3764ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3765 if (RetTy->isVoidType())
3766 return ABIArgInfo::getIgnore();
3767 if (isAggregateTypeForABI(RetTy))
3768 return ABIArgInfo::getIndirect(0);
3769
3770 return (isPromotableIntegerType(RetTy) ?
3771 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3772}
3773
3774ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3775 if (isAggregateTypeForABI(Ty))
3776 return ABIArgInfo::getIndirect(0);
3777
3778 return (isPromotableIntegerType(Ty) ?
3779 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3780}
3781
3782void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3783 llvm::GlobalValue *GV,
3784 CodeGen::CodeGenModule &M)
3785 const {
3786 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3787 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003788
Wesley Peck276fdf42010-12-19 19:57:51 +00003789 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3790 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3791 CC = llvm::CallingConv::MBLAZE_INTR;
3792 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3793 CC = llvm::CallingConv::MBLAZE_SVOL;
3794
3795 if (CC != llvm::CallingConv::C) {
3796 // Handle 'interrupt_handler' attribute:
3797 llvm::Function *F = cast<llvm::Function>(GV);
3798
3799 // Step 1: Set ISR calling convention.
3800 F->setCallingConv(CC);
3801
3802 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00003803 F->addFnAttr(llvm::Attribute::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003804 }
3805
3806 // Step 3: Emit _interrupt_handler alias.
3807 if (CC == llvm::CallingConv::MBLAZE_INTR)
3808 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3809 "_interrupt_handler", GV, &M.getModule());
3810}
3811
3812
3813//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003814// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003815//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003816
3817namespace {
3818
3819class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3820public:
Chris Lattnerea044322010-07-29 02:01:43 +00003821 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3822 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003823 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3824 CodeGen::CodeGenModule &M) const;
3825};
3826
3827}
3828
3829void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3830 llvm::GlobalValue *GV,
3831 CodeGen::CodeGenModule &M) const {
3832 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3833 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3834 // Handle 'interrupt' attribute:
3835 llvm::Function *F = cast<llvm::Function>(GV);
3836
3837 // Step 1: Set ISR calling convention.
3838 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3839
3840 // Step 2: Add attributes goodness.
Bill Wendling72390b32012-12-20 19:27:06 +00003841 F->addFnAttr(llvm::Attribute::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003842
3843 // Step 3: Emit ISR vector alias.
Anton Korobeynikovf419a852012-11-26 18:59:10 +00003844 unsigned Num = attr->getNumber() / 2;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003845 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Anton Korobeynikovf419a852012-11-26 18:59:10 +00003846 "__isr_" + Twine(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003847 GV, &M.getModule());
3848 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003849 }
3850}
3851
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003852//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003853// MIPS ABI Implementation. This works for both little-endian and
3854// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003855//===----------------------------------------------------------------------===//
3856
John McCallaeeb7012010-05-27 06:19:26 +00003857namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003858class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003859 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003860 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3861 void CoerceToIntArgs(uint64_t TySize,
3862 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003863 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003864 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003865 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003866public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003867 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003868 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3869 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003870
3871 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003872 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003873 virtual void computeInfo(CGFunctionInfo &FI) const;
3874 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3875 CodeGenFunction &CGF) const;
3876};
3877
John McCallaeeb7012010-05-27 06:19:26 +00003878class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003879 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003880public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003881 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3882 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3883 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003884
3885 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3886 return 29;
3887 }
3888
Reed Kotler7dfd1822013-01-16 17:10:28 +00003889 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3890 CodeGen::CodeGenModule &CGM) const {
3891 //
3892 // can fill this in when new attribute work in llvm is done.
3893 // attributes mips16 and nomips16 need to be handled here.
3894 //
3895 }
John McCallaeeb7012010-05-27 06:19:26 +00003896 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003897 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003898
3899 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003900 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003901 }
John McCallaeeb7012010-05-27 06:19:26 +00003902};
3903}
3904
Akira Hatanakac359f202012-07-03 19:24:06 +00003905void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3906 SmallVector<llvm::Type*, 8> &ArgList) const {
3907 llvm::IntegerType *IntTy =
3908 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003909
3910 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3911 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3912 ArgList.push_back(IntTy);
3913
3914 // If necessary, add one more integer type to ArgList.
3915 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3916
3917 if (R)
3918 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003919}
3920
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003921// In N32/64, an aligned double precision floating point field is passed in
3922// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003923llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003924 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3925
3926 if (IsO32) {
3927 CoerceToIntArgs(TySize, ArgList);
3928 return llvm::StructType::get(getVMContext(), ArgList);
3929 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003930
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003931 if (Ty->isComplexType())
3932 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003933
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003934 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003935
Akira Hatanakac359f202012-07-03 19:24:06 +00003936 // Unions/vectors are passed in integer registers.
3937 if (!RT || !RT->isStructureOrClassType()) {
3938 CoerceToIntArgs(TySize, ArgList);
3939 return llvm::StructType::get(getVMContext(), ArgList);
3940 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003941
3942 const RecordDecl *RD = RT->getDecl();
3943 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003944 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003945
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003946 uint64_t LastOffset = 0;
3947 unsigned idx = 0;
3948 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3949
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003950 // Iterate over fields in the struct/class and check if there are any aligned
3951 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003952 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3953 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003954 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003955 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3956
3957 if (!BT || BT->getKind() != BuiltinType::Double)
3958 continue;
3959
3960 uint64_t Offset = Layout.getFieldOffset(idx);
3961 if (Offset % 64) // Ignore doubles that are not aligned.
3962 continue;
3963
3964 // Add ((Offset - LastOffset) / 64) args of type i64.
3965 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3966 ArgList.push_back(I64);
3967
3968 // Add double type.
3969 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3970 LastOffset = Offset + 64;
3971 }
3972
Akira Hatanakac359f202012-07-03 19:24:06 +00003973 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3974 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003975
3976 return llvm::StructType::get(getVMContext(), ArgList);
3977}
3978
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003979llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003980 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003981
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003982 if ((Align - 1) & Offset)
3983 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3984
3985 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003986}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003987
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003988ABIArgInfo
3989MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003990 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003991 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003992 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003993
Akira Hatanakac359f202012-07-03 19:24:06 +00003994 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3995 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003996 Offset = llvm::RoundUpToAlignment(Offset, Align);
3997 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003998
Akira Hatanakac359f202012-07-03 19:24:06 +00003999 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00004000 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004001 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004002 return ABIArgInfo::getIgnore();
4003
Akira Hatanaka511949b2011-08-01 18:09:58 +00004004 // Records with non trivial destructors/constructors should not be passed
4005 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004006 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004007 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00004008 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004009 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00004010
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004011 // If we have reached here, aggregates are passed directly by coercing to
4012 // another structure type. Padding is inserted if the offset of the
4013 // aggregate is unaligned.
4014 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4015 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004016 }
4017
4018 // Treat an enum type as its underlying type.
4019 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4020 Ty = EnumTy->getDecl()->getIntegerType();
4021
Akira Hatanakaa33fd392012-01-09 19:31:25 +00004022 if (Ty->isPromotableIntegerType())
4023 return ABIArgInfo::getExtend();
4024
4025 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00004026}
4027
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004028llvm::Type*
4029MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00004030 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00004031 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004032
Akira Hatanakada54ff32012-02-09 18:49:26 +00004033 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004034 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00004035 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4036 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004037
Akira Hatanakada54ff32012-02-09 18:49:26 +00004038 // N32/64 returns struct/classes in floating point registers if the
4039 // following conditions are met:
4040 // 1. The size of the struct/class is no larger than 128-bit.
4041 // 2. The struct/class has one or two fields all of which are floating
4042 // point types.
4043 // 3. The offset of the first field is zero (this follows what gcc does).
4044 //
4045 // Any other composite results are returned in integer registers.
4046 //
4047 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4048 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4049 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00004050 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004051
Akira Hatanakada54ff32012-02-09 18:49:26 +00004052 if (!BT || !BT->isFloatingPoint())
4053 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004054
David Blaikie262bc182012-04-30 02:36:29 +00004055 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00004056 }
4057
4058 if (b == e)
4059 return llvm::StructType::get(getVMContext(), RTList,
4060 RD->hasAttr<PackedAttr>());
4061
4062 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004063 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004064 }
4065
Akira Hatanakac359f202012-07-03 19:24:06 +00004066 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004067 return llvm::StructType::get(getVMContext(), RTList);
4068}
4069
Akira Hatanaka619e8872011-06-02 00:09:17 +00004070ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00004071 uint64_t Size = getContext().getTypeSize(RetTy);
4072
4073 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00004074 return ABIArgInfo::getIgnore();
4075
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00004076 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004077 if (Size <= 128) {
4078 if (RetTy->isAnyComplexType())
4079 return ABIArgInfo::getDirect();
4080
Akira Hatanakac359f202012-07-03 19:24:06 +00004081 // O32 returns integer vectors in registers.
4082 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4083 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4084
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00004085 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00004086 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4087 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00004088
4089 return ABIArgInfo::getIndirect(0);
4090 }
4091
4092 // Treat an enum type as its underlying type.
4093 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4094 RetTy = EnumTy->getDecl()->getIntegerType();
4095
4096 return (RetTy->isPromotableIntegerType() ?
4097 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4098}
4099
4100void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00004101 ABIArgInfo &RetInfo = FI.getReturnInfo();
4102 RetInfo = classifyReturnType(FI.getReturnType());
4103
4104 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00004105 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00004106
Akira Hatanaka619e8872011-06-02 00:09:17 +00004107 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4108 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00004109 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00004110}
4111
4112llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4113 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00004114 llvm::Type *BP = CGF.Int8PtrTy;
4115 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004116
4117 CGBuilderTy &Builder = CGF.Builder;
4118 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4119 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004120 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004121 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4122 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004123 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
4124 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004125
4126 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004127 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4128 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4129 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4130 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004131 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4132 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4133 }
4134 else
4135 AddrTyped = Builder.CreateBitCast(Addr, PTy);
4136
4137 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004138 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004139 uint64_t Offset =
4140 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4141 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00004142 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00004143 "ap.next");
4144 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4145
4146 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00004147}
4148
John McCallaeeb7012010-05-27 06:19:26 +00004149bool
4150MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4151 llvm::Value *Address) const {
4152 // This information comes from gcc's implementation, which seems to
4153 // as canonical as it gets.
4154
John McCallaeeb7012010-05-27 06:19:26 +00004155 // Everything on MIPS is 4 bytes. Double-precision FP registers
4156 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004157 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00004158
4159 // 0-31 are the general purpose registers, $0 - $31.
4160 // 32-63 are the floating-point registers, $f0 - $f31.
4161 // 64 and 65 are the multiply/divide registers, $hi and $lo.
4162 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00004163 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00004164
4165 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4166 // They are one bit wide and ignored here.
4167
4168 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4169 // (coprocessor 1 is the FP unit)
4170 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4171 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4172 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00004173 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00004174 return false;
4175}
4176
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004177//===----------------------------------------------------------------------===//
4178// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4179// Currently subclassed only to implement custom OpenCL C function attribute
4180// handling.
4181//===----------------------------------------------------------------------===//
4182
4183namespace {
4184
4185class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4186public:
4187 TCETargetCodeGenInfo(CodeGenTypes &CGT)
4188 : DefaultTargetCodeGenInfo(CGT) {}
4189
4190 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4191 CodeGen::CodeGenModule &M) const;
4192};
4193
4194void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4195 llvm::GlobalValue *GV,
4196 CodeGen::CodeGenModule &M) const {
4197 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4198 if (!FD) return;
4199
4200 llvm::Function *F = cast<llvm::Function>(GV);
4201
David Blaikie4e4d0842012-03-11 07:00:24 +00004202 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004203 if (FD->hasAttr<OpenCLKernelAttr>()) {
4204 // OpenCL C Kernel functions are not subject to inlining
Bill Wendling72390b32012-12-20 19:27:06 +00004205 F->addFnAttr(llvm::Attribute::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004206
4207 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4208
4209 // Convert the reqd_work_group_size() attributes to metadata.
4210 llvm::LLVMContext &Context = F->getContext();
4211 llvm::NamedMDNode *OpenCLMetadata =
4212 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4213
4214 SmallVector<llvm::Value*, 5> Operands;
4215 Operands.push_back(F);
4216
Chris Lattner8b418682012-02-07 00:39:47 +00004217 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4218 llvm::APInt(32,
4219 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4220 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4221 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004222 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00004223 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4224 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004225 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4226
4227 // Add a boolean constant operand for "required" (true) or "hint" (false)
4228 // for implementing the work_group_size_hint attr later. Currently
4229 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00004230 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004231 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4232 }
4233 }
4234 }
4235}
4236
4237}
John McCallaeeb7012010-05-27 06:19:26 +00004238
Tony Linthicum96319392011-12-12 21:14:55 +00004239//===----------------------------------------------------------------------===//
4240// Hexagon ABI Implementation
4241//===----------------------------------------------------------------------===//
4242
4243namespace {
4244
4245class HexagonABIInfo : public ABIInfo {
4246
4247
4248public:
4249 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4250
4251private:
4252
4253 ABIArgInfo classifyReturnType(QualType RetTy) const;
4254 ABIArgInfo classifyArgumentType(QualType RetTy) const;
4255
4256 virtual void computeInfo(CGFunctionInfo &FI) const;
4257
4258 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4259 CodeGenFunction &CGF) const;
4260};
4261
4262class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4263public:
4264 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4265 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4266
4267 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4268 return 29;
4269 }
4270};
4271
4272}
4273
4274void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4275 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4276 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4277 it != ie; ++it)
4278 it->info = classifyArgumentType(it->type);
4279}
4280
4281ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4282 if (!isAggregateTypeForABI(Ty)) {
4283 // Treat an enum type as its underlying type.
4284 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4285 Ty = EnumTy->getDecl()->getIntegerType();
4286
4287 return (Ty->isPromotableIntegerType() ?
4288 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4289 }
4290
4291 // Ignore empty records.
4292 if (isEmptyRecord(getContext(), Ty, true))
4293 return ABIArgInfo::getIgnore();
4294
4295 // Structures with either a non-trivial destructor or a non-trivial
4296 // copy constructor are always indirect.
4297 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4298 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4299
4300 uint64_t Size = getContext().getTypeSize(Ty);
4301 if (Size > 64)
4302 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4303 // Pass in the smallest viable integer type.
4304 else if (Size > 32)
4305 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4306 else if (Size > 16)
4307 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4308 else if (Size > 8)
4309 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4310 else
4311 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4312}
4313
4314ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4315 if (RetTy->isVoidType())
4316 return ABIArgInfo::getIgnore();
4317
4318 // Large vector types should be returned via memory.
4319 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4320 return ABIArgInfo::getIndirect(0);
4321
4322 if (!isAggregateTypeForABI(RetTy)) {
4323 // Treat an enum type as its underlying type.
4324 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4325 RetTy = EnumTy->getDecl()->getIntegerType();
4326
4327 return (RetTy->isPromotableIntegerType() ?
4328 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4329 }
4330
4331 // Structures with either a non-trivial destructor or a non-trivial
4332 // copy constructor are always indirect.
4333 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4334 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4335
4336 if (isEmptyRecord(getContext(), RetTy, true))
4337 return ABIArgInfo::getIgnore();
4338
4339 // Aggregates <= 8 bytes are returned in r0; other aggregates
4340 // are returned indirectly.
4341 uint64_t Size = getContext().getTypeSize(RetTy);
4342 if (Size <= 64) {
4343 // Return in the smallest viable integer type.
4344 if (Size <= 8)
4345 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4346 if (Size <= 16)
4347 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4348 if (Size <= 32)
4349 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4350 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4351 }
4352
4353 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4354}
4355
4356llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004357 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004358 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004359 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004360
4361 CGBuilderTy &Builder = CGF.Builder;
4362 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4363 "ap");
4364 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4365 llvm::Type *PTy =
4366 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4367 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4368
4369 uint64_t Offset =
4370 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4371 llvm::Value *NextAddr =
4372 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4373 "ap.next");
4374 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4375
4376 return AddrTyped;
4377}
4378
4379
Chris Lattnerea044322010-07-29 02:01:43 +00004380const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004381 if (TheTargetCodeGenInfo)
4382 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004383
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004384 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004385 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004386 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004387 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004388
Derek Schuff9ed63f82012-09-06 17:37:28 +00004389 case llvm::Triple::le32:
4390 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004391 case llvm::Triple::mips:
4392 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004393 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004394
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004395 case llvm::Triple::mips64:
4396 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004397 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004398
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004399 case llvm::Triple::arm:
4400 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004401 {
4402 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004403 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004404 Kind = ARMABIInfo::APCS;
David Tweedb16abb12012-10-25 13:33:01 +00004405 else if (CodeGenOpts.FloatABI == "hard" ||
4406 (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF))
Sandeep Patel34c1af82011-04-05 00:23:47 +00004407 Kind = ARMABIInfo::AAPCS_VFP;
4408
Derek Schuff263366f2012-10-16 22:30:41 +00004409 switch (Triple.getOS()) {
Eli Bendersky441d9f72012-12-04 18:38:10 +00004410 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004411 return *(TheTargetCodeGenInfo =
4412 new NaClARMTargetCodeGenInfo(Types, Kind));
4413 default:
4414 return *(TheTargetCodeGenInfo =
4415 new ARMTargetCodeGenInfo(Types, Kind));
4416 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004417 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004418
John McCallec853ba2010-03-11 00:10:12 +00004419 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004420 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004421 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004422 if (Triple.isOSBinFormatELF())
4423 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4424 else
4425 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004426
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004427 case llvm::Triple::nvptx:
4428 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004429 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004430
Wesley Peck276fdf42010-12-19 19:57:51 +00004431 case llvm::Triple::mblaze:
4432 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4433
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004434 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004435 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004436
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004437 case llvm::Triple::tce:
4438 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4439
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004440 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004441 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004442
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004443 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004444 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004445 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4446 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004447
4448 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004449 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004450 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004451 case llvm::Triple::AuroraUX:
4452 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004453 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004454 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004455 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004456 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004457 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4458 false,
4459 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004460
4461 case llvm::Triple::Win32:
4462 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004463 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4464 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004465
4466 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004467 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004468 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4469 false,
4470 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004471 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004472 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004473
Eli Friedmanee1ad992011-12-02 00:11:43 +00004474 case llvm::Triple::x86_64: {
4475 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4476
Chris Lattnerf13721d2010-08-31 16:44:54 +00004477 switch (Triple.getOS()) {
4478 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004479 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004480 case llvm::Triple::Cygwin:
4481 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Eli Bendersky441d9f72012-12-04 18:38:10 +00004482 case llvm::Triple::NaCl:
Derek Schuff263366f2012-10-16 22:30:41 +00004483 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004484 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004485 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4486 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004487 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004488 }
Tony Linthicum96319392011-12-12 21:14:55 +00004489 case llvm::Triple::hexagon:
4490 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004491 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004492}