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