blob: 887ca890bce1b66985134e67a4bbb60ae88165c6 [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.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000522 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000523
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000524 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000525 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000526
Rafael Espindolab48280b2012-07-31 02:44:24 +0000527 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000528 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000529 unsigned callingConvention) const;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000530 ABIArgInfo classifyArgumentTypeWithReg(QualType RetTy,
531 unsigned &FreeRegs) const;
Chris Lattnera3c109b2010-07-29 02:16:43 +0000532 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000533
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000534public:
535
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000536 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
538 CodeGenFunction &CGF) const;
539
Rafael Espindolab48280b2012-07-31 02:44:24 +0000540 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
541 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000542 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000543 IsMMXDisabled(m), IsWin32FloatStructABI(w),
544 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000547class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
548public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000549 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000550 bool d, bool p, bool m, bool w, unsigned r)
551 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000552
553 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
554 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000555
556 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
557 // Darwin uses different dwarf register numbers for EH.
558 if (CGM.isTargetDarwin()) return 5;
559
560 return 4;
561 }
562
563 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
564 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000565
Jay Foadef6de3d2011-07-11 09:56:20 +0000566 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000567 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000568 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000569 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
570 }
571
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000572};
573
574}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000575
576/// shouldReturnTypeInRegister - Determine if the given type should be
577/// passed in a register (for the Darwin ABI).
578bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000579 ASTContext &Context,
580 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 uint64_t Size = Context.getTypeSize(Ty);
582
583 // Type must be register sized.
584 if (!isRegisterSize(Size))
585 return false;
586
587 if (Ty->isVectorType()) {
588 // 64- and 128- bit vectors inside structures are not returned in
589 // registers.
590 if (Size == 64 || Size == 128)
591 return false;
592
593 return true;
594 }
595
Daniel Dunbar77115232010-05-15 00:00:30 +0000596 // If this is a builtin, pointer, enum, complex type, member pointer, or
597 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000598 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000599 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000600 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 return true;
602
603 // Arrays are treated like records.
604 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000605 return shouldReturnTypeInRegister(AT->getElementType(), Context,
606 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607
608 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000609 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000610 if (!RT) return false;
611
Anders Carlssona8874232010-01-27 03:25:19 +0000612 // FIXME: Traverse bases here too.
613
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000614 // For thiscall conventions, structures will never be returned in
615 // a register. This is for compatibility with the MSVC ABI
616 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
617 RT->isStructureType()) {
618 return false;
619 }
620
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000621 // Structure types are passed in register if all fields would be
622 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000623 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
624 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000625 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000626
627 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000628 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000629 continue;
630
631 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000632 if (!shouldReturnTypeInRegister(FD->getType(), Context,
633 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 return false;
635 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000636 return true;
637}
638
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000639ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
640 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000641 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000643
Chris Lattnera3c109b2010-07-29 02:16:43 +0000644 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000646 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000647 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648
649 // 128-bit vectors are a special case; they are returned in
650 // registers and we need to make sure to pick a type the LLVM
651 // backend will like.
652 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000653 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000654 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000655
656 // Always return in register if it fits in a general purpose
657 // register, or if it is 64 bits and has a single element.
658 if ((Size == 8 || Size == 16 || Size == 32) ||
659 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000660 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000661 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000662
663 return ABIArgInfo::getIndirect(0);
664 }
665
666 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000667 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000668
John McCalld608cdb2010-08-22 10:59:02 +0000669 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000670 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000671 // Structures with either a non-trivial destructor or a non-trivial
672 // copy constructor are always indirect.
673 if (hasNonTrivialDestructorOrCopyConstructor(RT))
674 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000675
Anders Carlsson40092972009-10-20 22:07:59 +0000676 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677 if (RT->getDecl()->hasFlexibleArrayMember())
678 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000679 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000680
David Chisnall1e4249c2009-08-17 23:08:21 +0000681 // If specified, structs and unions are always indirect.
682 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000683 return ABIArgInfo::getIndirect(0);
684
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 // Small structures which are register sized are generally returned
686 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000687 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
688 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000689 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000690
691 // As a special-case, if the struct is a "single-element" struct, and
692 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000693 // floating-point register. (MSVC does not apply this special case.)
694 // We apply a similar transformation for pointer types to improve the
695 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000696 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000697 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
698 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000699 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
700
701 // FIXME: We should be able to narrow this integer in cases with dead
702 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000703 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000704 }
705
706 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000708
Chris Lattnera3c109b2010-07-29 02:16:43 +0000709 // Treat an enum type as its underlying type.
710 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
711 RetTy = EnumTy->getDecl()->getIntegerType();
712
713 return (RetTy->isPromotableIntegerType() ?
714 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000715}
716
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000717static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
718 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
719}
720
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000721static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
722 const RecordType *RT = Ty->getAs<RecordType>();
723 if (!RT)
724 return 0;
725 const RecordDecl *RD = RT->getDecl();
726
727 // If this is a C++ record, check the bases first.
728 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
729 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
730 e = CXXRD->bases_end(); i != e; ++i)
731 if (!isRecordWithSSEVectorType(Context, i->getType()))
732 return false;
733
734 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
735 i != e; ++i) {
736 QualType FT = i->getType();
737
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000738 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000739 return true;
740
741 if (isRecordWithSSEVectorType(Context, FT))
742 return true;
743 }
744
745 return false;
746}
747
Daniel Dunbare59d8582010-09-16 20:42:06 +0000748unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
749 unsigned Align) const {
750 // Otherwise, if the alignment is less than or equal to the minimum ABI
751 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000752 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000753 return 0; // Use default alignment.
754
755 // On non-Darwin, the stack type alignment is always 4.
756 if (!IsDarwinVectorABI) {
757 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000758 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000759 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000760
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000761 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000762 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
763 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000764 return 16;
765
766 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000767}
768
Chris Lattnera3c109b2010-07-29 02:16:43 +0000769ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000770 if (!ByVal)
771 return ABIArgInfo::getIndirect(0, false);
772
Daniel Dunbare59d8582010-09-16 20:42:06 +0000773 // Compute the byval alignment.
774 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
775 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
776 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000777 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000778
779 // If the stack alignment is less than the type alignment, realign the
780 // argument.
781 if (StackAlign < TypeAlign)
782 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
783 /*Realign=*/true);
784
785 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000786}
787
Rafael Espindolab48280b2012-07-31 02:44:24 +0000788X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
789 const Type *T = isSingleElementStruct(Ty, getContext());
790 if (!T)
791 T = Ty.getTypePtr();
792
793 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
794 BuiltinType::Kind K = BT->getKind();
795 if (K == BuiltinType::Float || K == BuiltinType::Double)
796 return Float;
797 }
798 return Integer;
799}
800
801ABIArgInfo
802X86_32ABIInfo::classifyArgumentTypeWithReg(QualType Ty,
803 unsigned &FreeRegs) const {
804 // Common case first.
805 if (FreeRegs == 0)
806 return classifyArgumentType(Ty);
807
808 Class C = classify(Ty);
809 if (C == Float)
810 return classifyArgumentType(Ty);
811
812 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
813 if (SizeInRegs == 0)
814 return classifyArgumentType(Ty);
815
816 if (SizeInRegs > FreeRegs) {
817 FreeRegs = 0;
818 return classifyArgumentType(Ty);
819 }
820 assert(SizeInRegs >= 1 && SizeInRegs <= 3);
821 FreeRegs -= SizeInRegs;
822
823 // If it is a simple scalar, keep the type so that we produce a cleaner IR.
824 ABIArgInfo Foo = classifyArgumentType(Ty);
825 if (Foo.isDirect() && !Foo.getDirectOffset() && !Foo.getPaddingType())
826 return ABIArgInfo::getDirectInReg(Foo.getCoerceToType());
827 if (Foo.isExtend())
828 return ABIArgInfo::getExtendInReg(Foo.getCoerceToType());
829
830 llvm::LLVMContext &LLVMContext = getVMContext();
831 llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
832 SmallVector<llvm::Type*, 3> Elements;
833 for (unsigned I = 0; I < SizeInRegs; ++I)
834 Elements.push_back(Int32);
835 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
836 return ABIArgInfo::getDirectInReg(Result);
837}
838
Chris Lattnera3c109b2010-07-29 02:16:43 +0000839ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000840 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000841 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000842 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000843 if (const RecordType *RT = Ty->getAs<RecordType>()) {
844 // Structures with either a non-trivial destructor or a non-trivial
845 // copy constructor are always indirect.
846 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000847 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000848
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000849 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000850 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000851 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000852
Eli Friedman5a4d3522011-11-18 00:28:11 +0000853 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000854 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000855 return ABIArgInfo::getIgnore();
856
Daniel Dunbar53012f42009-11-09 01:33:53 +0000857 // Expand small (<= 128-bit) record types when we know that the stack layout
858 // of those arguments will match the struct. This is important because the
859 // LLVM backend isn't smart enough to remove byval, which inhibits many
860 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000861 if (getContext().getTypeSize(Ty) <= 4*32 &&
862 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000863 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000864
Chris Lattnera3c109b2010-07-29 02:16:43 +0000865 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000866 }
867
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000868 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000869 // On Darwin, some vectors are passed in memory, we handle this by passing
870 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000871 if (IsDarwinVectorABI) {
872 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000873 if ((Size == 8 || Size == 16 || Size == 32) ||
874 (Size == 64 && VT->getNumElements() == 1))
875 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
876 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000877 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000878
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000879 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000880 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000881 if (IsMMXDisabled)
882 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
883 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000884 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
885 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
886 return AAI;
887 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000888
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000889 return ABIArgInfo::getDirect();
890 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000891
892
Chris Lattnera3c109b2010-07-29 02:16:43 +0000893 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
894 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000895
Chris Lattnera3c109b2010-07-29 02:16:43 +0000896 return (Ty->isPromotableIntegerType() ?
897 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000898}
899
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000900void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
901 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
902 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000903
904 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() :
905 DefaultNumRegisterParameters;
906
907 // If the return value is indirect, then the hidden argument is consuming one
908 // integer register.
909 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
910 --FreeRegs;
911 ABIArgInfo &Old = FI.getReturnInfo();
912 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
913 Old.getIndirectByVal(),
914 Old.getIndirectRealign());
915 }
916
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000917 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
918 it != ie; ++it)
Rafael Espindolab48280b2012-07-31 02:44:24 +0000919 it->info = classifyArgumentTypeWithReg(it->type, FreeRegs);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000920}
921
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000922llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
923 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000924 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000925
926 CGBuilderTy &Builder = CGF.Builder;
927 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
928 "ap");
929 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000930
931 // Compute if the address needs to be aligned
932 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
933 Align = getTypeStackAlignInBytes(Ty, Align);
934 Align = std::max(Align, 4U);
935 if (Align > 4) {
936 // addr = (addr + align - 1) & -align;
937 llvm::Value *Offset =
938 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
939 Addr = CGF.Builder.CreateGEP(Addr, Offset);
940 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
941 CGF.Int32Ty);
942 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
943 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
944 Addr->getType(),
945 "ap.cur.aligned");
946 }
947
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000949 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000950 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
951
952 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000953 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000954 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000955 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000956 "ap.next");
957 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
958
959 return AddrTyped;
960}
961
Charles Davis74f72932010-02-13 15:54:06 +0000962void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
963 llvm::GlobalValue *GV,
964 CodeGen::CodeGenModule &CGM) const {
965 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
966 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
967 // Get the LLVM function.
968 llvm::Function *Fn = cast<llvm::Function>(GV);
969
970 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendlingfac63102012-10-10 03:13:20 +0000971 Fn->addAttribute(~0U,
972 llvm::Attributes::constructStackAlignmentFromInt(16));
Charles Davis74f72932010-02-13 15:54:06 +0000973 }
974 }
975}
976
John McCall6374c332010-03-06 00:35:14 +0000977bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
978 CodeGen::CodeGenFunction &CGF,
979 llvm::Value *Address) const {
980 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000981
Chris Lattner8b418682012-02-07 00:39:47 +0000982 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000983
John McCall6374c332010-03-06 00:35:14 +0000984 // 0-7 are the eight integer registers; the order is different
985 // on Darwin (for EH), but the range is the same.
986 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000987 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000988
989 if (CGF.CGM.isTargetDarwin()) {
990 // 12-16 are st(0..4). Not sure why we stop at 4.
991 // These have size 16, which is sizeof(long double) on
992 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000993 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000994 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000995
John McCall6374c332010-03-06 00:35:14 +0000996 } else {
997 // 9 is %eflags, which doesn't get a size on Darwin for some
998 // reason.
999 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1000
1001 // 11-16 are st(0..5). Not sure why we stop at 5.
1002 // These have size 12, which is sizeof(long double) on
1003 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001004 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001005 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1006 }
John McCall6374c332010-03-06 00:35:14 +00001007
1008 return false;
1009}
1010
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001011//===----------------------------------------------------------------------===//
1012// X86-64 ABI Implementation
1013//===----------------------------------------------------------------------===//
1014
1015
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001016namespace {
1017/// X86_64ABIInfo - The X86_64 ABI information.
1018class X86_64ABIInfo : public ABIInfo {
1019 enum Class {
1020 Integer = 0,
1021 SSE,
1022 SSEUp,
1023 X87,
1024 X87Up,
1025 ComplexX87,
1026 NoClass,
1027 Memory
1028 };
1029
1030 /// merge - Implement the X86_64 ABI merging algorithm.
1031 ///
1032 /// Merge an accumulating classification \arg Accum with a field
1033 /// classification \arg Field.
1034 ///
1035 /// \param Accum - The accumulating classification. This should
1036 /// always be either NoClass or the result of a previous merge
1037 /// call. In addition, this should never be Memory (the caller
1038 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001039 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001040
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001041 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1042 ///
1043 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1044 /// final MEMORY or SSE classes when necessary.
1045 ///
1046 /// \param AggregateSize - The size of the current aggregate in
1047 /// the classification process.
1048 ///
1049 /// \param Lo - The classification for the parts of the type
1050 /// residing in the low word of the containing object.
1051 ///
1052 /// \param Hi - The classification for the parts of the type
1053 /// residing in the higher words of the containing object.
1054 ///
1055 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1056
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001057 /// classify - Determine the x86_64 register classes in which the
1058 /// given type T should be passed.
1059 ///
1060 /// \param Lo - The classification for the parts of the type
1061 /// residing in the low word of the containing object.
1062 ///
1063 /// \param Hi - The classification for the parts of the type
1064 /// residing in the high word of the containing object.
1065 ///
1066 /// \param OffsetBase - The bit offset of this type in the
1067 /// containing object. Some parameters are classified different
1068 /// depending on whether they straddle an eightbyte boundary.
1069 ///
1070 /// If a word is unused its result will be NoClass; if a type should
1071 /// be passed in Memory then at least the classification of \arg Lo
1072 /// will be Memory.
1073 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001074 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001075 ///
1076 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1077 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001078 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001079
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001080 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001081 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1082 unsigned IROffset, QualType SourceTy,
1083 unsigned SourceOffset) const;
1084 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1085 unsigned IROffset, QualType SourceTy,
1086 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001087
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001088 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001089 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001090 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001091
1092 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001094 ///
1095 /// \param freeIntRegs - The number of free integer registers remaining
1096 /// available.
1097 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001098
Chris Lattnera3c109b2010-07-29 02:16:43 +00001099 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100
Bill Wendlingbb465d72010-10-18 03:41:31 +00001101 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001102 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001103 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001104 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001105
Eli Friedmanee1ad992011-12-02 00:11:43 +00001106 bool IsIllegalVectorType(QualType Ty) const;
1107
John McCall67a57732011-04-21 01:20:55 +00001108 /// The 0.98 ABI revision clarified a lot of ambiguities,
1109 /// unfortunately in ways that were not always consistent with
1110 /// certain previous compilers. In particular, platforms which
1111 /// required strict binary compatibility with older versions of GCC
1112 /// may need to exempt themselves.
1113 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001114 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001115 }
1116
Eli Friedmanee1ad992011-12-02 00:11:43 +00001117 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001118 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1119 // 64-bit hardware.
1120 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001121
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001122public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001123 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001124 ABIInfo(CGT), HasAVX(hasavx),
1125 Has64BitPointers(CGT.getDataLayout().getPointerSize() == 8) {
1126 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001127
John McCallde5d3c72012-02-17 03:33:10 +00001128 bool isPassedUsingAVXType(QualType type) const {
1129 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001130 // The freeIntRegs argument doesn't matter here.
1131 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001132 if (info.isDirect()) {
1133 llvm::Type *ty = info.getCoerceToType();
1134 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1135 return (vectorTy->getBitWidth() > 128);
1136 }
1137 return false;
1138 }
1139
Chris Lattneree5dcd02010-07-29 02:31:05 +00001140 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141
1142 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1143 CodeGenFunction &CGF) const;
1144};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001145
Chris Lattnerf13721d2010-08-31 16:44:54 +00001146/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001147class WinX86_64ABIInfo : public ABIInfo {
1148
1149 ABIArgInfo classify(QualType Ty) const;
1150
Chris Lattnerf13721d2010-08-31 16:44:54 +00001151public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001152 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1153
1154 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001155
1156 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1157 CodeGenFunction &CGF) const;
1158};
1159
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001160class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1161public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001162 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001163 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001164
John McCallde5d3c72012-02-17 03:33:10 +00001165 const X86_64ABIInfo &getABIInfo() const {
1166 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1167 }
1168
John McCall6374c332010-03-06 00:35:14 +00001169 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1170 return 7;
1171 }
1172
1173 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1174 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001175 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001176
John McCallaeeb7012010-05-27 06:19:26 +00001177 // 0-15 are the 16 integer registers.
1178 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001179 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001180 return false;
1181 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001182
Jay Foadef6de3d2011-07-11 09:56:20 +00001183 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001184 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001185 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001186 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1187 }
1188
John McCallde5d3c72012-02-17 03:33:10 +00001189 bool isNoProtoCallVariadic(const CallArgList &args,
1190 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001191 // The default CC on x86-64 sets %al to the number of SSA
1192 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001193 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001194 // that when AVX types are involved: the ABI explicitly states it is
1195 // undefined, and it doesn't work in practice because of how the ABI
1196 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001197 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001198 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001199 for (CallArgList::const_iterator
1200 it = args.begin(), ie = args.end(); it != ie; ++it) {
1201 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1202 HasAVXType = true;
1203 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001204 }
1205 }
John McCallde5d3c72012-02-17 03:33:10 +00001206
Eli Friedman3ed79032011-12-01 04:53:19 +00001207 if (!HasAVXType)
1208 return true;
1209 }
John McCall01f151e2011-09-21 08:08:30 +00001210
John McCallde5d3c72012-02-17 03:33:10 +00001211 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001212 }
1213
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001214};
1215
Chris Lattnerf13721d2010-08-31 16:44:54 +00001216class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1217public:
1218 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1219 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1220
1221 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1222 return 7;
1223 }
1224
1225 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1226 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001227 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001228
Chris Lattnerf13721d2010-08-31 16:44:54 +00001229 // 0-15 are the 16 integer registers.
1230 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001231 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001232 return false;
1233 }
1234};
1235
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236}
1237
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001238void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1239 Class &Hi) const {
1240 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1241 //
1242 // (a) If one of the classes is Memory, the whole argument is passed in
1243 // memory.
1244 //
1245 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1246 // memory.
1247 //
1248 // (c) If the size of the aggregate exceeds two eightbytes and the first
1249 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1250 // argument is passed in memory. NOTE: This is necessary to keep the
1251 // ABI working for processors that don't support the __m256 type.
1252 //
1253 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1254 //
1255 // Some of these are enforced by the merging logic. Others can arise
1256 // only with unions; for example:
1257 // union { _Complex double; unsigned; }
1258 //
1259 // Note that clauses (b) and (c) were added in 0.98.
1260 //
1261 if (Hi == Memory)
1262 Lo = Memory;
1263 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1264 Lo = Memory;
1265 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1266 Lo = Memory;
1267 if (Hi == SSEUp && Lo != SSE)
1268 Hi = SSE;
1269}
1270
Chris Lattner1090a9b2010-06-28 21:43:59 +00001271X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001272 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1273 // classified recursively so that always two fields are
1274 // considered. The resulting class is calculated according to
1275 // the classes of the fields in the eightbyte:
1276 //
1277 // (a) If both classes are equal, this is the resulting class.
1278 //
1279 // (b) If one of the classes is NO_CLASS, the resulting class is
1280 // the other class.
1281 //
1282 // (c) If one of the classes is MEMORY, the result is the MEMORY
1283 // class.
1284 //
1285 // (d) If one of the classes is INTEGER, the result is the
1286 // INTEGER.
1287 //
1288 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1289 // MEMORY is used as class.
1290 //
1291 // (f) Otherwise class SSE is used.
1292
1293 // Accum should never be memory (we should have returned) or
1294 // ComplexX87 (because this cannot be passed in a structure).
1295 assert((Accum != Memory && Accum != ComplexX87) &&
1296 "Invalid accumulated classification during merge.");
1297 if (Accum == Field || Field == NoClass)
1298 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001299 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001300 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001301 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001302 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001303 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001304 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001305 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1306 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001307 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001308 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309}
1310
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001311void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001312 Class &Lo, Class &Hi) const {
1313 // FIXME: This code can be simplified by introducing a simple value class for
1314 // Class pairs with appropriate constructor methods for the various
1315 // situations.
1316
1317 // FIXME: Some of the split computations are wrong; unaligned vectors
1318 // shouldn't be passed in registers for example, so there is no chance they
1319 // can straddle an eightbyte. Verify & simplify.
1320
1321 Lo = Hi = NoClass;
1322
1323 Class &Current = OffsetBase < 64 ? Lo : Hi;
1324 Current = Memory;
1325
John McCall183700f2009-09-21 23:43:11 +00001326 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001327 BuiltinType::Kind k = BT->getKind();
1328
1329 if (k == BuiltinType::Void) {
1330 Current = NoClass;
1331 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1332 Lo = Integer;
1333 Hi = Integer;
1334 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1335 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001336 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1337 (k == BuiltinType::LongDouble &&
1338 getContext().getTargetInfo().getTriple().getOS() ==
1339 llvm::Triple::NativeClient)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 Current = SSE;
1341 } else if (k == BuiltinType::LongDouble) {
1342 Lo = X87;
1343 Hi = X87Up;
1344 }
1345 // FIXME: _Decimal32 and _Decimal64 are SSE.
1346 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001347 return;
1348 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001349
Chris Lattner1090a9b2010-06-28 21:43:59 +00001350 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001351 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001352 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001353 return;
1354 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001355
Chris Lattner1090a9b2010-06-28 21:43:59 +00001356 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001357 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001358 return;
1359 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001360
Chris Lattner1090a9b2010-06-28 21:43:59 +00001361 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001362 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001363 Lo = Hi = Integer;
1364 else
1365 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001366 return;
1367 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001368
Chris Lattner1090a9b2010-06-28 21:43:59 +00001369 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001370 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001371 if (Size == 32) {
1372 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1373 // float> as integer.
1374 Current = Integer;
1375
1376 // If this type crosses an eightbyte boundary, it should be
1377 // split.
1378 uint64_t EB_Real = (OffsetBase) / 64;
1379 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1380 if (EB_Real != EB_Imag)
1381 Hi = Lo;
1382 } else if (Size == 64) {
1383 // gcc passes <1 x double> in memory. :(
1384 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1385 return;
1386
1387 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001388 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001389 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1390 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1391 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 Current = Integer;
1393 else
1394 Current = SSE;
1395
1396 // If this type crosses an eightbyte boundary, it should be
1397 // split.
1398 if (OffsetBase && OffsetBase != 64)
1399 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001400 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001401 // Arguments of 256-bits are split into four eightbyte chunks. The
1402 // least significant one belongs to class SSE and all the others to class
1403 // SSEUP. The original Lo and Hi design considers that types can't be
1404 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1405 // This design isn't correct for 256-bits, but since there're no cases
1406 // where the upper parts would need to be inspected, avoid adding
1407 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001408 Lo = SSE;
1409 Hi = SSEUp;
1410 }
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 ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001415 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001416
Chris Lattnerea044322010-07-29 02:01:43 +00001417 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001418 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001419 if (Size <= 64)
1420 Current = Integer;
1421 else if (Size <= 128)
1422 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001423 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001424 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001425 else if (ET == getContext().DoubleTy ||
1426 (ET == getContext().LongDoubleTy &&
1427 getContext().getTargetInfo().getTriple().getOS() ==
1428 llvm::Triple::NativeClient))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001429 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001430 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001431 Current = ComplexX87;
1432
1433 // If this complex type crosses an eightbyte boundary then it
1434 // should be split.
1435 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001436 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001437 if (Hi == NoClass && EB_Real != EB_Imag)
1438 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001439
Chris Lattner1090a9b2010-06-28 21:43:59 +00001440 return;
1441 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001442
Chris Lattnerea044322010-07-29 02:01:43 +00001443 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001444 // Arrays are treated like structures.
1445
Chris Lattnerea044322010-07-29 02:01:43 +00001446 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001447
1448 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001449 // than four eightbytes, ..., it has class MEMORY.
1450 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451 return;
1452
1453 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1454 // fields, it has class MEMORY.
1455 //
1456 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001457 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 return;
1459
1460 // Otherwise implement simplified merge. We could be smarter about
1461 // this, but it isn't worth it and would be harder to verify.
1462 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001463 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001464 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001465
1466 // The only case a 256-bit wide vector could be used is when the array
1467 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1468 // to work for sizes wider than 128, early check and fallback to memory.
1469 if (Size > 128 && EltSize != 256)
1470 return;
1471
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001472 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1473 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001474 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001475 Lo = merge(Lo, FieldLo);
1476 Hi = merge(Hi, FieldHi);
1477 if (Lo == Memory || Hi == Memory)
1478 break;
1479 }
1480
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001481 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001483 return;
1484 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001485
Chris Lattner1090a9b2010-06-28 21:43:59 +00001486 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001487 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001488
1489 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001490 // than four eightbytes, ..., it has class MEMORY.
1491 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001492 return;
1493
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001494 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1495 // copy constructor or a non-trivial destructor, it is passed by invisible
1496 // reference.
1497 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1498 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001499
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001500 const RecordDecl *RD = RT->getDecl();
1501
1502 // Assume variable sized types are passed in memory.
1503 if (RD->hasFlexibleArrayMember())
1504 return;
1505
Chris Lattnerea044322010-07-29 02:01:43 +00001506 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001507
1508 // Reset Lo class, this will be recomputed.
1509 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001510
1511 // If this is a C++ record, classify the bases first.
1512 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1513 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1514 e = CXXRD->bases_end(); i != e; ++i) {
1515 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1516 "Unexpected base class!");
1517 const CXXRecordDecl *Base =
1518 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1519
1520 // Classify this field.
1521 //
1522 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1523 // single eightbyte, each is classified separately. Each eightbyte gets
1524 // initialized to class NO_CLASS.
1525 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001526 uint64_t Offset =
1527 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001528 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001529 Lo = merge(Lo, FieldLo);
1530 Hi = merge(Hi, FieldHi);
1531 if (Lo == Memory || Hi == Memory)
1532 break;
1533 }
1534 }
1535
1536 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001537 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001538 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001539 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001540 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1541 bool BitField = i->isBitField();
1542
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001543 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1544 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001545 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001546 // The only case a 256-bit wide vector could be used is when the struct
1547 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1548 // to work for sizes wider than 128, early check and fallback to memory.
1549 //
1550 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1551 Lo = Memory;
1552 return;
1553 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001554 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001555 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001556 Lo = Memory;
1557 return;
1558 }
1559
1560 // Classify this field.
1561 //
1562 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1563 // exceeds a single eightbyte, each is classified
1564 // separately. Each eightbyte gets initialized to class
1565 // NO_CLASS.
1566 Class FieldLo, FieldHi;
1567
1568 // Bit-fields require special handling, they do not force the
1569 // structure to be passed in memory even if unaligned, and
1570 // therefore they can straddle an eightbyte.
1571 if (BitField) {
1572 // Ignore padding bit-fields.
1573 if (i->isUnnamedBitfield())
1574 continue;
1575
1576 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001577 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001578
1579 uint64_t EB_Lo = Offset / 64;
1580 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1581 FieldLo = FieldHi = NoClass;
1582 if (EB_Lo) {
1583 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1584 FieldLo = NoClass;
1585 FieldHi = Integer;
1586 } else {
1587 FieldLo = Integer;
1588 FieldHi = EB_Hi ? Integer : NoClass;
1589 }
1590 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001591 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001592 Lo = merge(Lo, FieldLo);
1593 Hi = merge(Hi, FieldHi);
1594 if (Lo == Memory || Hi == Memory)
1595 break;
1596 }
1597
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001598 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001599 }
1600}
1601
Chris Lattner9c254f02010-06-29 06:01:59 +00001602ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001603 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1604 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001605 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001606 // Treat an enum type as its underlying type.
1607 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1608 Ty = EnumTy->getDecl()->getIntegerType();
1609
1610 return (Ty->isPromotableIntegerType() ?
1611 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1612 }
1613
1614 return ABIArgInfo::getIndirect(0);
1615}
1616
Eli Friedmanee1ad992011-12-02 00:11:43 +00001617bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1618 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1619 uint64_t Size = getContext().getTypeSize(VecTy);
1620 unsigned LargestVector = HasAVX ? 256 : 128;
1621 if (Size <= 64 || Size > LargestVector)
1622 return true;
1623 }
1624
1625 return false;
1626}
1627
Daniel Dunbaredfac032012-03-10 01:03:58 +00001628ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1629 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001630 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1631 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001632 //
1633 // This assumption is optimistic, as there could be free registers available
1634 // when we need to pass this argument in memory, and LLVM could try to pass
1635 // the argument in the free register. This does not seem to happen currently,
1636 // but this code would be much safer if we could mark the argument with
1637 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001638 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001639 // Treat an enum type as its underlying type.
1640 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1641 Ty = EnumTy->getDecl()->getIntegerType();
1642
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001643 return (Ty->isPromotableIntegerType() ?
1644 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001645 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001646
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001647 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1648 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001649
Chris Lattner855d2272011-05-22 23:21:23 +00001650 // Compute the byval alignment. We specify the alignment of the byval in all
1651 // cases so that the mid-level optimizer knows the alignment of the byval.
1652 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001653
1654 // Attempt to avoid passing indirect results using byval when possible. This
1655 // is important for good codegen.
1656 //
1657 // We do this by coercing the value into a scalar type which the backend can
1658 // handle naturally (i.e., without using byval).
1659 //
1660 // For simplicity, we currently only do this when we have exhausted all of the
1661 // free integer registers. Doing this when there are free integer registers
1662 // would require more care, as we would have to ensure that the coerced value
1663 // did not claim the unused register. That would require either reording the
1664 // arguments to the function (so that any subsequent inreg values came first),
1665 // or only doing this optimization when there were no following arguments that
1666 // might be inreg.
1667 //
1668 // We currently expect it to be rare (particularly in well written code) for
1669 // arguments to be passed on the stack when there are still free integer
1670 // registers available (this would typically imply large structs being passed
1671 // by value), so this seems like a fair tradeoff for now.
1672 //
1673 // We can revisit this if the backend grows support for 'onstack' parameter
1674 // attributes. See PR12193.
1675 if (freeIntRegs == 0) {
1676 uint64_t Size = getContext().getTypeSize(Ty);
1677
1678 // If this type fits in an eightbyte, coerce it into the matching integral
1679 // type, which will end up on the stack (with alignment 8).
1680 if (Align == 8 && Size <= 64)
1681 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1682 Size));
1683 }
1684
Chris Lattner855d2272011-05-22 23:21:23 +00001685 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001686}
1687
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001688/// GetByteVectorType - The ABI specifies that a value should be passed in an
1689/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001690/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001691llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001692 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001693
Chris Lattner15842bd2010-07-29 05:02:29 +00001694 // Wrapper structs that just contain vectors are passed just like vectors,
1695 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001696 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001697 while (STy && STy->getNumElements() == 1) {
1698 IRType = STy->getElementType(0);
1699 STy = dyn_cast<llvm::StructType>(IRType);
1700 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001701
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001702 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001703 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1704 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001705 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001706 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001707 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1708 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1709 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1710 EltTy->isIntegerTy(128)))
1711 return VT;
1712 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001713
Chris Lattner0f408f52010-07-29 04:56:46 +00001714 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1715}
1716
Chris Lattnere2962be2010-07-29 07:30:00 +00001717/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1718/// is known to either be off the end of the specified type or being in
1719/// alignment padding. The user type specified is known to be at most 128 bits
1720/// in size, and have passed through X86_64ABIInfo::classify with a successful
1721/// classification that put one of the two halves in the INTEGER class.
1722///
1723/// It is conservatively correct to return false.
1724static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1725 unsigned EndBit, ASTContext &Context) {
1726 // If the bytes being queried are off the end of the type, there is no user
1727 // data hiding here. This handles analysis of builtins, vectors and other
1728 // types that don't contain interesting padding.
1729 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1730 if (TySize <= StartBit)
1731 return true;
1732
Chris Lattner021c3a32010-07-29 07:43:55 +00001733 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1734 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1735 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1736
1737 // Check each element to see if the element overlaps with the queried range.
1738 for (unsigned i = 0; i != NumElts; ++i) {
1739 // If the element is after the span we care about, then we're done..
1740 unsigned EltOffset = i*EltSize;
1741 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001742
Chris Lattner021c3a32010-07-29 07:43:55 +00001743 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1744 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1745 EndBit-EltOffset, Context))
1746 return false;
1747 }
1748 // If it overlaps no elements, then it is safe to process as padding.
1749 return true;
1750 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001751
Chris Lattnere2962be2010-07-29 07:30:00 +00001752 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1753 const RecordDecl *RD = RT->getDecl();
1754 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001755
Chris Lattnere2962be2010-07-29 07:30:00 +00001756 // If this is a C++ record, check the bases first.
1757 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1758 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1759 e = CXXRD->bases_end(); i != e; ++i) {
1760 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1761 "Unexpected base class!");
1762 const CXXRecordDecl *Base =
1763 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001764
Chris Lattnere2962be2010-07-29 07:30:00 +00001765 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001766 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001767 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001768
Chris Lattnere2962be2010-07-29 07:30:00 +00001769 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1770 if (!BitsContainNoUserData(i->getType(), BaseStart,
1771 EndBit-BaseOffset, Context))
1772 return false;
1773 }
1774 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001775
Chris Lattnere2962be2010-07-29 07:30:00 +00001776 // Verify that no field has data that overlaps the region of interest. Yes
1777 // this could be sped up a lot by being smarter about queried fields,
1778 // however we're only looking at structs up to 16 bytes, so we don't care
1779 // much.
1780 unsigned idx = 0;
1781 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1782 i != e; ++i, ++idx) {
1783 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001784
Chris Lattnere2962be2010-07-29 07:30:00 +00001785 // If we found a field after the region we care about, then we're done.
1786 if (FieldOffset >= EndBit) break;
1787
1788 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1789 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1790 Context))
1791 return false;
1792 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
Chris Lattnere2962be2010-07-29 07:30:00 +00001794 // If nothing in this record overlapped the area of interest, then we're
1795 // clean.
1796 return true;
1797 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001798
Chris Lattnere2962be2010-07-29 07:30:00 +00001799 return false;
1800}
1801
Chris Lattner0b362002010-07-29 18:39:32 +00001802/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1803/// float member at the specified offset. For example, {int,{float}} has a
1804/// float at offset 4. It is conservatively correct for this routine to return
1805/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001806static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001807 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001808 // Base case if we find a float.
1809 if (IROffset == 0 && IRType->isFloatTy())
1810 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001811
Chris Lattner0b362002010-07-29 18:39:32 +00001812 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001813 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001814 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1815 unsigned Elt = SL->getElementContainingOffset(IROffset);
1816 IROffset -= SL->getElementOffset(Elt);
1817 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1818 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001819
Chris Lattner0b362002010-07-29 18:39:32 +00001820 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001821 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1822 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001823 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1824 IROffset -= IROffset/EltSize*EltSize;
1825 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1826 }
1827
1828 return false;
1829}
1830
Chris Lattnerf47c9442010-07-29 18:13:09 +00001831
1832/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1833/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001834llvm::Type *X86_64ABIInfo::
1835GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001836 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001837 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001838 // pass as float if the last 4 bytes is just padding. This happens for
1839 // structs that contain 3 floats.
1840 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1841 SourceOffset*8+64, getContext()))
1842 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001843
Chris Lattner0b362002010-07-29 18:39:32 +00001844 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1845 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1846 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001847 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1848 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001849 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001850
Chris Lattnerf47c9442010-07-29 18:13:09 +00001851 return llvm::Type::getDoubleTy(getVMContext());
1852}
1853
1854
Chris Lattner0d2656d2010-07-29 17:40:35 +00001855/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1856/// an 8-byte GPR. This means that we either have a scalar or we are talking
1857/// about the high or low part of an up-to-16-byte struct. This routine picks
1858/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001859/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1860/// etc).
1861///
1862/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1863/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1864/// the 8-byte value references. PrefType may be null.
1865///
1866/// SourceTy is the source level type for the entire argument. SourceOffset is
1867/// an offset into this that we're processing (which is always either 0 or 8).
1868///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001869llvm::Type *X86_64ABIInfo::
1870GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001871 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001872 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1873 // returning an 8-byte unit starting with it. See if we can safely use it.
1874 if (IROffset == 0) {
1875 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001876 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1877 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001878 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001879
Chris Lattnere2962be2010-07-29 07:30:00 +00001880 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1881 // goodness in the source type is just tail padding. This is allowed to
1882 // kick in for struct {double,int} on the int, but not on
1883 // struct{double,int,int} because we wouldn't return the second int. We
1884 // have to do this analysis on the source type because we can't depend on
1885 // unions being lowered a specific way etc.
1886 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001887 IRType->isIntegerTy(32) ||
1888 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1889 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1890 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001891
Chris Lattnere2962be2010-07-29 07:30:00 +00001892 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1893 SourceOffset*8+64, getContext()))
1894 return IRType;
1895 }
1896 }
Chris Lattner49382de2010-07-28 22:44:07 +00001897
Chris Lattner2acc6e32011-07-18 04:24:23 +00001898 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001899 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001900 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001901 if (IROffset < SL->getSizeInBytes()) {
1902 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1903 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001904
Chris Lattner0d2656d2010-07-29 17:40:35 +00001905 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1906 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001907 }
Chris Lattner49382de2010-07-28 22:44:07 +00001908 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001909
Chris Lattner2acc6e32011-07-18 04:24:23 +00001910 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001911 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001912 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001913 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001914 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1915 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001916 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001917
Chris Lattner49382de2010-07-28 22:44:07 +00001918 // Okay, we don't have any better idea of what to pass, so we pass this in an
1919 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001920 unsigned TySizeInBytes =
1921 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001922
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001923 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001924
Chris Lattner49382de2010-07-28 22:44:07 +00001925 // It is always safe to classify this as an integer type up to i64 that
1926 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001927 return llvm::IntegerType::get(getVMContext(),
1928 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001929}
1930
Chris Lattner66e7b682010-09-01 00:50:20 +00001931
1932/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1933/// be used as elements of a two register pair to pass or return, return a
1934/// first class aggregate to represent them. For example, if the low part of
1935/// a by-value argument should be passed as i32* and the high part as float,
1936/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001937static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001938GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001939 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001940 // In order to correctly satisfy the ABI, we need to the high part to start
1941 // at offset 8. If the high and low parts we inferred are both 4-byte types
1942 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1943 // the second element at offset 8. Check for this:
1944 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1945 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001946 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001947 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001948
Chris Lattner66e7b682010-09-01 00:50:20 +00001949 // To handle this, we have to increase the size of the low part so that the
1950 // second element will start at an 8 byte offset. We can't increase the size
1951 // of the second element because it might make us access off the end of the
1952 // struct.
1953 if (HiStart != 8) {
1954 // There are only two sorts of types the ABI generation code can produce for
1955 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1956 // Promote these to a larger type.
1957 if (Lo->isFloatTy())
1958 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1959 else {
1960 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1961 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1962 }
1963 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001964
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001965 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001966
1967
Chris Lattner66e7b682010-09-01 00:50:20 +00001968 // Verify that the second element is at an 8-byte offset.
1969 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1970 "Invalid x86-64 argument pair!");
1971 return Result;
1972}
1973
Chris Lattner519f68c2010-07-28 23:06:14 +00001974ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001975classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001976 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1977 // classification algorithm.
1978 X86_64ABIInfo::Class Lo, Hi;
1979 classify(RetTy, 0, Lo, Hi);
1980
1981 // Check some invariants.
1982 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001983 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1984
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001985 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001986 switch (Lo) {
1987 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001988 if (Hi == NoClass)
1989 return ABIArgInfo::getIgnore();
1990 // If the low part is just padding, it takes no register, leave ResType
1991 // null.
1992 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1993 "Unknown missing lo part");
1994 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001995
1996 case SSEUp:
1997 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001998 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001999
2000 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2001 // hidden argument.
2002 case Memory:
2003 return getIndirectReturnResult(RetTy);
2004
2005 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2006 // available register of the sequence %rax, %rdx is used.
2007 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002008 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002009
Chris Lattnereb518b42010-07-29 21:42:50 +00002010 // If we have a sign or zero extended integer, make sure to return Extend
2011 // so that the parameter gets the right LLVM IR attributes.
2012 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2013 // Treat an enum type as its underlying type.
2014 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2015 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002016
Chris Lattnereb518b42010-07-29 21:42:50 +00002017 if (RetTy->isIntegralOrEnumerationType() &&
2018 RetTy->isPromotableIntegerType())
2019 return ABIArgInfo::getExtend();
2020 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002021 break;
2022
2023 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2024 // available SSE register of the sequence %xmm0, %xmm1 is used.
2025 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002026 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002027 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002028
2029 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2030 // returned on the X87 stack in %st0 as 80-bit x87 number.
2031 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002032 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002033 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002034
2035 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2036 // part of the value is returned in %st0 and the imaginary part in
2037 // %st1.
2038 case ComplexX87:
2039 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002040 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002041 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002042 NULL);
2043 break;
2044 }
2045
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002046 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002047 switch (Hi) {
2048 // Memory was handled previously and X87 should
2049 // never occur as a hi class.
2050 case Memory:
2051 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002052 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002053
2054 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002055 case NoClass:
2056 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002057
Chris Lattner3db4dde2010-09-01 00:20:33 +00002058 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002059 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002060 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2061 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002062 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002063 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002064 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002065 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2066 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002067 break;
2068
2069 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002070 // is passed in the next available eightbyte chunk if the last used
2071 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002072 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002073 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002074 case SSEUp:
2075 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002076 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002077 break;
2078
2079 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2080 // returned together with the previous X87 value in %st0.
2081 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002082 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002083 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002084 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002085 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002086 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002087 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002088 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2089 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002090 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002091 break;
2092 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002093
Chris Lattner3db4dde2010-09-01 00:20:33 +00002094 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002095 // known to pass in the high eightbyte of the result. We do this by forming a
2096 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002097 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002098 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002099
Chris Lattnereb518b42010-07-29 21:42:50 +00002100 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002101}
2102
Daniel Dunbaredfac032012-03-10 01:03:58 +00002103ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2104 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2105 const
2106{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002107 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002108 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002109
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002110 // Check some invariants.
2111 // FIXME: Enforce these by construction.
2112 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002113 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2114
2115 neededInt = 0;
2116 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002117 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002118 switch (Lo) {
2119 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002120 if (Hi == NoClass)
2121 return ABIArgInfo::getIgnore();
2122 // If the low part is just padding, it takes no register, leave ResType
2123 // null.
2124 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2125 "Unknown missing lo part");
2126 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002127
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002128 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2129 // on the stack.
2130 case Memory:
2131
2132 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2133 // COMPLEX_X87, it is passed in memory.
2134 case X87:
2135 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002136 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2137 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002138 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002139
2140 case SSEUp:
2141 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002142 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002143
2144 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2145 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2146 // and %r9 is used.
2147 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002148 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002149
Chris Lattner49382de2010-07-28 22:44:07 +00002150 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002151 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002152
2153 // If we have a sign or zero extended integer, make sure to return Extend
2154 // so that the parameter gets the right LLVM IR attributes.
2155 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2156 // Treat an enum type as its underlying type.
2157 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2158 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002159
Chris Lattnereb518b42010-07-29 21:42:50 +00002160 if (Ty->isIntegralOrEnumerationType() &&
2161 Ty->isPromotableIntegerType())
2162 return ABIArgInfo::getExtend();
2163 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002164
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002165 break;
2166
2167 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2168 // available SSE register is used, the registers are taken in the
2169 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002170 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002171 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002172 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002173 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002174 break;
2175 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002176 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002177
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002178 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002179 switch (Hi) {
2180 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002181 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002182 // which is passed in memory.
2183 case Memory:
2184 case X87:
2185 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002186 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002187
2188 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002189
Chris Lattner645406a2010-09-01 00:24:35 +00002190 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002191 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002192 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002193 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002194
Chris Lattner645406a2010-09-01 00:24:35 +00002195 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2196 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002197 break;
2198
2199 // X87Up generally doesn't occur here (long double is passed in
2200 // memory), except in situations involving unions.
2201 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002202 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002203 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002204
Chris Lattner645406a2010-09-01 00:24:35 +00002205 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2206 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002207
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 ++neededSSE;
2209 break;
2210
2211 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2212 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002213 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002214 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002215 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002216 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002217 break;
2218 }
2219
Chris Lattner645406a2010-09-01 00:24:35 +00002220 // If a high part was specified, merge it together with the low part. It is
2221 // known to pass in the high eightbyte of the result. We do this by forming a
2222 // first class struct aggregate with the high and low part: {low, high}
2223 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002224 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002225
Chris Lattnereb518b42010-07-29 21:42:50 +00002226 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002227}
2228
Chris Lattneree5dcd02010-07-29 02:31:05 +00002229void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002230
Chris Lattnera3c109b2010-07-29 02:16:43 +00002231 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002232
2233 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002234 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002235
2236 // If the return value is indirect, then the hidden argument is consuming one
2237 // integer register.
2238 if (FI.getReturnInfo().isIndirect())
2239 --freeIntRegs;
2240
2241 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2242 // get assigned (in left-to-right order) for passing as follows...
2243 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2244 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002245 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002246 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2247 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002248
2249 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2250 // eightbyte of an argument, the whole argument is passed on the
2251 // stack. If registers have already been assigned for some
2252 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002253 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002254 freeIntRegs -= neededInt;
2255 freeSSERegs -= neededSSE;
2256 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002257 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002258 }
2259 }
2260}
2261
2262static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2263 QualType Ty,
2264 CodeGenFunction &CGF) {
2265 llvm::Value *overflow_arg_area_p =
2266 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2267 llvm::Value *overflow_arg_area =
2268 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2269
2270 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2271 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002272 // It isn't stated explicitly in the standard, but in practice we use
2273 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002274 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2275 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002276 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002277 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002278 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002279 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2280 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002281 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002282 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002283 overflow_arg_area =
2284 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2285 overflow_arg_area->getType(),
2286 "overflow_arg_area.align");
2287 }
2288
2289 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002290 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002291 llvm::Value *Res =
2292 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002293 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002294
2295 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2296 // l->overflow_arg_area + sizeof(type).
2297 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2298 // an 8 byte boundary.
2299
2300 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002301 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002302 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002303 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2304 "overflow_arg_area.next");
2305 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2306
2307 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2308 return Res;
2309}
2310
2311llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2312 CodeGenFunction &CGF) const {
2313 // Assume that va_list type is correct; should be pointer to LLVM type:
2314 // struct {
2315 // i32 gp_offset;
2316 // i32 fp_offset;
2317 // i8* overflow_arg_area;
2318 // i8* reg_save_area;
2319 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002320 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002321
Chris Lattnera14db752010-03-11 18:19:55 +00002322 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002323 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002324
2325 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2326 // in the registers. If not go to step 7.
2327 if (!neededInt && !neededSSE)
2328 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2329
2330 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2331 // general purpose registers needed to pass type and num_fp to hold
2332 // the number of floating point registers needed.
2333
2334 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2335 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2336 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2337 //
2338 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2339 // register save space).
2340
2341 llvm::Value *InRegs = 0;
2342 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2343 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2344 if (neededInt) {
2345 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2346 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002347 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2348 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002349 }
2350
2351 if (neededSSE) {
2352 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2353 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2354 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002355 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2356 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002357 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2358 }
2359
2360 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2361 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2362 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2363 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2364
2365 // Emit code to load the value if it was passed in registers.
2366
2367 CGF.EmitBlock(InRegBlock);
2368
2369 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2370 // an offset of l->gp_offset and/or l->fp_offset. This may require
2371 // copying to a temporary location in case the parameter is passed
2372 // in different register classes or requires an alignment greater
2373 // than 8 for general purpose registers and 16 for XMM registers.
2374 //
2375 // FIXME: This really results in shameful code when we end up needing to
2376 // collect arguments from different places; often what should result in a
2377 // simple assembling of a structure from scattered addresses has many more
2378 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002379 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002380 llvm::Value *RegAddr =
2381 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2382 "reg_save_area");
2383 if (neededInt && neededSSE) {
2384 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002385 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002386 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002387 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2388 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002389 llvm::Type *TyLo = ST->getElementType(0);
2390 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002391 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002392 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002393 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2394 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002395 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2396 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002397 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2398 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002399 llvm::Value *V =
2400 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2401 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2402 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2403 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2404
Owen Andersona1cf15f2009-07-14 23:10:40 +00002405 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002406 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002407 } else if (neededInt) {
2408 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2409 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002410 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002411 } else if (neededSSE == 1) {
2412 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2413 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2414 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002415 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002416 assert(neededSSE == 2 && "Invalid number of needed registers!");
2417 // SSE registers are spaced 16 bytes apart in the register save
2418 // area, we need to collect the two eightbytes together.
2419 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002420 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002421 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002422 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002423 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002424 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002425 DoubleTy, NULL);
2426 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2427 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2428 DblPtrTy));
2429 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2430 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2431 DblPtrTy));
2432 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2433 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2434 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002435 }
2436
2437 // AMD64-ABI 3.5.7p5: Step 5. Set:
2438 // l->gp_offset = l->gp_offset + num_gp * 8
2439 // l->fp_offset = l->fp_offset + num_fp * 16.
2440 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002441 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002442 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2443 gp_offset_p);
2444 }
2445 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002446 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002447 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2448 fp_offset_p);
2449 }
2450 CGF.EmitBranch(ContBlock);
2451
2452 // Emit code to load the value if it was passed in memory.
2453
2454 CGF.EmitBlock(InMemBlock);
2455 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2456
2457 // Return the appropriate result.
2458
2459 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002460 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002461 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002462 ResAddr->addIncoming(RegAddr, InRegBlock);
2463 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002464 return ResAddr;
2465}
2466
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002467ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2468
2469 if (Ty->isVoidType())
2470 return ABIArgInfo::getIgnore();
2471
2472 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2473 Ty = EnumTy->getDecl()->getIntegerType();
2474
2475 uint64_t Size = getContext().getTypeSize(Ty);
2476
2477 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002478 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2479 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002480 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2481
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002482 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2483 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002484 getContext().getTargetInfo().getTriple().getOS()
2485 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002486 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2487 Size));
2488
2489 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2490 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2491 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002492 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002493 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2494 Size));
2495
2496 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2497 }
2498
2499 if (Ty->isPromotableIntegerType())
2500 return ABIArgInfo::getExtend();
2501
2502 return ABIArgInfo::getDirect();
2503}
2504
2505void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2506
2507 QualType RetTy = FI.getReturnType();
2508 FI.getReturnInfo() = classify(RetTy);
2509
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002510 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2511 it != ie; ++it)
2512 it->info = classify(it->type);
2513}
2514
Chris Lattnerf13721d2010-08-31 16:44:54 +00002515llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2516 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002517 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002518
Chris Lattnerf13721d2010-08-31 16:44:54 +00002519 CGBuilderTy &Builder = CGF.Builder;
2520 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2521 "ap");
2522 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2523 llvm::Type *PTy =
2524 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2525 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2526
2527 uint64_t Offset =
2528 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2529 llvm::Value *NextAddr =
2530 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2531 "ap.next");
2532 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2533
2534 return AddrTyped;
2535}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002536
John McCallec853ba2010-03-11 00:10:12 +00002537// PowerPC-32
2538
2539namespace {
2540class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2541public:
Chris Lattnerea044322010-07-29 02:01:43 +00002542 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002543
John McCallec853ba2010-03-11 00:10:12 +00002544 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2545 // This is recovered from gcc output.
2546 return 1; // r1 is the dedicated stack pointer
2547 }
2548
2549 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002550 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002551};
2552
2553}
2554
2555bool
2556PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2557 llvm::Value *Address) const {
2558 // This is calculated from the LLVM and GCC tables and verified
2559 // against gcc output. AFAIK all ABIs use the same encoding.
2560
2561 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002562
Chris Lattner8b418682012-02-07 00:39:47 +00002563 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002564 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2565 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2566 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2567
2568 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002569 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002570
2571 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002572 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002573
2574 // 64-76 are various 4-byte special-purpose registers:
2575 // 64: mq
2576 // 65: lr
2577 // 66: ctr
2578 // 67: ap
2579 // 68-75 cr0-7
2580 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002581 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002582
2583 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002584 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002585
2586 // 109: vrsave
2587 // 110: vscr
2588 // 111: spe_acc
2589 // 112: spefscr
2590 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002591 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002592
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002593 return false;
John McCallec853ba2010-03-11 00:10:12 +00002594}
2595
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002596// PowerPC-64
2597
2598namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002599/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2600class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2601
2602public:
2603 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2604
2605 // TODO: Could override computeInfo to model the ABI more completely if
2606 // it would be helpful. Example: We might remove the byVal flag from
2607 // aggregate arguments that fit in a register to avoid pushing them to
2608 // memory on function entry. Note that this is a performance optimization,
2609 // not a compliance issue. In general we prefer to keep ABI details in
2610 // the back end where possible, but modifying an argument flag seems like
2611 // a good thing to do before invoking the back end.
2612
2613 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2614 QualType Ty,
2615 CodeGenFunction &CGF) const;
2616};
2617
2618class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2619public:
2620 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2621 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2622
2623 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2624 // This is recovered from gcc output.
2625 return 1; // r1 is the dedicated stack pointer
2626 }
2627
2628 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2629 llvm::Value *Address) const;
2630};
2631
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002632class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2633public:
2634 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2635
2636 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2637 // This is recovered from gcc output.
2638 return 1; // r1 is the dedicated stack pointer
2639 }
2640
2641 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2642 llvm::Value *Address) const;
2643};
2644
2645}
2646
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002647// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2648llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2649 QualType Ty,
2650 CodeGenFunction &CGF) const {
2651 llvm::Type *BP = CGF.Int8PtrTy;
2652 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2653
2654 CGBuilderTy &Builder = CGF.Builder;
2655 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2656 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2657
2658 // Handle address alignment for type alignment > 64 bits. Although
2659 // long double normally requires 16-byte alignment, this is not the
2660 // case when it is passed as an argument; so handle that special case.
2661 const BuiltinType *BT = Ty->getAs<BuiltinType>();
2662 unsigned TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2663
2664 if (TyAlign > 8 && (!BT || !BT->isFloatingPoint())) {
2665 assert((TyAlign & (TyAlign - 1)) == 0 &&
2666 "Alignment is not power of 2!");
2667 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2668 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(TyAlign - 1));
2669 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(~(TyAlign - 1)));
2670 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2671 }
2672
2673 // Update the va_list pointer.
2674 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2675 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2676 llvm::Value *NextAddr =
2677 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2678 "ap.next");
2679 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2680
2681 // If the argument is smaller than 8 bytes, it is right-adjusted in
2682 // its doubleword slot. Adjust the pointer to pick it up from the
2683 // correct offset.
2684 if (SizeInBytes < 8) {
2685 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2686 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2687 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2688 }
2689
2690 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2691 return Builder.CreateBitCast(Addr, PTy);
2692}
2693
2694static bool
2695PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2696 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002697 // This is calculated from the LLVM and GCC tables and verified
2698 // against gcc output. AFAIK all ABIs use the same encoding.
2699
2700 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2701
2702 llvm::IntegerType *i8 = CGF.Int8Ty;
2703 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2704 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2705 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2706
2707 // 0-31: r0-31, the 8-byte general-purpose registers
2708 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2709
2710 // 32-63: fp0-31, the 8-byte floating-point registers
2711 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2712
2713 // 64-76 are various 4-byte special-purpose registers:
2714 // 64: mq
2715 // 65: lr
2716 // 66: ctr
2717 // 67: ap
2718 // 68-75 cr0-7
2719 // 76: xer
2720 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2721
2722 // 77-108: v0-31, the 16-byte vector registers
2723 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2724
2725 // 109: vrsave
2726 // 110: vscr
2727 // 111: spe_acc
2728 // 112: spefscr
2729 // 113: sfp
2730 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2731
2732 return false;
2733}
John McCallec853ba2010-03-11 00:10:12 +00002734
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002735bool
2736PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2737 CodeGen::CodeGenFunction &CGF,
2738 llvm::Value *Address) const {
2739
2740 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2741}
2742
2743bool
2744PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2745 llvm::Value *Address) const {
2746
2747 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2748}
2749
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002750//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002751// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002752//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002753
2754namespace {
2755
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002756class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002757public:
2758 enum ABIKind {
2759 APCS = 0,
2760 AAPCS = 1,
2761 AAPCS_VFP
2762 };
2763
2764private:
2765 ABIKind Kind;
2766
2767public:
Chris Lattnerea044322010-07-29 02:01:43 +00002768 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002769
John McCall49e34be2011-08-30 01:42:09 +00002770 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002771 StringRef Env =
2772 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002773 return (Env == "gnueabi" || Env == "eabi" ||
2774 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002775 }
2776
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002777private:
2778 ABIKind getABIKind() const { return Kind; }
2779
Chris Lattnera3c109b2010-07-29 02:16:43 +00002780 ABIArgInfo classifyReturnType(QualType RetTy) const;
2781 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002782
Chris Lattneree5dcd02010-07-29 02:31:05 +00002783 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002784
2785 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2786 CodeGenFunction &CGF) const;
2787};
2788
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002789class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2790public:
Chris Lattnerea044322010-07-29 02:01:43 +00002791 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2792 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002793
John McCall49e34be2011-08-30 01:42:09 +00002794 const ARMABIInfo &getABIInfo() const {
2795 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2796 }
2797
John McCall6374c332010-03-06 00:35:14 +00002798 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2799 return 13;
2800 }
Roman Divacky09345d12011-05-18 19:36:54 +00002801
Chris Lattner5f9e2722011-07-23 10:55:15 +00002802 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002803 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2804 }
2805
Roman Divacky09345d12011-05-18 19:36:54 +00002806 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2807 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002808 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002809
2810 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002811 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002812 return false;
2813 }
John McCall49e34be2011-08-30 01:42:09 +00002814
2815 unsigned getSizeOfUnwindException() const {
2816 if (getABIInfo().isEABI()) return 88;
2817 return TargetCodeGenInfo::getSizeOfUnwindException();
2818 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002819};
2820
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002821}
2822
Chris Lattneree5dcd02010-07-29 02:31:05 +00002823void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002824 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002825 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002826 it != ie; ++it)
2827 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002828
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002829 // Always honor user-specified calling convention.
2830 if (FI.getCallingConvention() != llvm::CallingConv::C)
2831 return;
2832
2833 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002834 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002835 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002836 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002837 else
2838 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002839
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002840 // If user did not ask for specific calling convention explicitly (e.g. via
2841 // pcs attribute), set effective calling convention if it's different than ABI
2842 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002843 switch (getABIKind()) {
2844 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002845 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2846 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002847 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002848 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002849 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2850 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002851 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002852 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002853 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2854 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002855 break;
2856 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002857}
2858
Bob Wilson194f06a2011-08-03 05:58:22 +00002859/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2860/// aggregate. If HAMembers is non-null, the number of base elements
2861/// contained in the type is returned through it; this is used for the
2862/// recursive calls that check aggregate component types.
2863static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2864 ASTContext &Context,
2865 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002866 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002867 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2868 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2869 return false;
2870 Members *= AT->getSize().getZExtValue();
2871 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2872 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002873 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002874 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002875
Bob Wilson194f06a2011-08-03 05:58:22 +00002876 Members = 0;
2877 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2878 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002879 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002880 uint64_t FldMembers;
2881 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2882 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002883
2884 Members = (RD->isUnion() ?
2885 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002886 }
2887 } else {
2888 Members = 1;
2889 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2890 Members = 2;
2891 Ty = CT->getElementType();
2892 }
2893
2894 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2895 // double, or 64-bit or 128-bit vectors.
2896 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2897 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00002898 BT->getKind() != BuiltinType::Double &&
2899 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00002900 return false;
2901 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2902 unsigned VecSize = Context.getTypeSize(VT);
2903 if (VecSize != 64 && VecSize != 128)
2904 return false;
2905 } else {
2906 return false;
2907 }
2908
2909 // The base type must be the same for all members. Vector types of the
2910 // same total size are treated as being equivalent here.
2911 const Type *TyPtr = Ty.getTypePtr();
2912 if (!Base)
2913 Base = TyPtr;
2914 if (Base != TyPtr &&
2915 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2916 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2917 return false;
2918 }
2919
2920 // Homogeneous Aggregates can have at most 4 members of the base type.
2921 if (HAMembers)
2922 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002923
2924 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00002925}
2926
Chris Lattnera3c109b2010-07-29 02:16:43 +00002927ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002928 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002929 // Treat an enum type as its underlying type.
2930 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2931 Ty = EnumTy->getDecl()->getIntegerType();
2932
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002933 return (Ty->isPromotableIntegerType() ?
2934 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002935 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002936
Daniel Dunbar42025572009-09-14 21:54:03 +00002937 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002938 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002939 return ABIArgInfo::getIgnore();
2940
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002941 // Structures with either a non-trivial destructor or a non-trivial
2942 // copy constructor are always indirect.
2943 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2944 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2945
Bob Wilson194f06a2011-08-03 05:58:22 +00002946 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2947 // Homogeneous Aggregates need to be expanded.
2948 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002949 if (isHomogeneousAggregate(Ty, Base, getContext())) {
2950 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson194f06a2011-08-03 05:58:22 +00002951 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002952 }
Bob Wilson194f06a2011-08-03 05:58:22 +00002953 }
2954
Manman Ren634b3d22012-08-13 21:23:55 +00002955 // Support byval for ARM.
2956 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
2957 getContext().getTypeAlign(Ty) > 64) {
2958 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
Eli Friedman79f30982012-08-09 00:31:40 +00002959 }
2960
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002961 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002962 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002963 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00002964 // FIXME: Try to match the types of the arguments more accurately where
2965 // we can.
2966 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00002967 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2968 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00002969 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00002970 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2971 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002972 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002973
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002974 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002975 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002976 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002977}
2978
Chris Lattnera3c109b2010-07-29 02:16:43 +00002979static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002980 llvm::LLVMContext &VMContext) {
2981 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2982 // is called integer-like if its size is less than or equal to one word, and
2983 // the offset of each of its addressable sub-fields is zero.
2984
2985 uint64_t Size = Context.getTypeSize(Ty);
2986
2987 // Check that the type fits in a word.
2988 if (Size > 32)
2989 return false;
2990
2991 // FIXME: Handle vector types!
2992 if (Ty->isVectorType())
2993 return false;
2994
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002995 // Float types are never treated as "integer like".
2996 if (Ty->isRealFloatingType())
2997 return false;
2998
Daniel Dunbar98303b92009-09-13 08:03:58 +00002999 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003000 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003001 return true;
3002
Daniel Dunbar45815812010-02-01 23:31:26 +00003003 // Small complex integer types are "integer like".
3004 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3005 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003006
3007 // Single element and zero sized arrays should be allowed, by the definition
3008 // above, but they are not.
3009
3010 // Otherwise, it must be a record type.
3011 const RecordType *RT = Ty->getAs<RecordType>();
3012 if (!RT) return false;
3013
3014 // Ignore records with flexible arrays.
3015 const RecordDecl *RD = RT->getDecl();
3016 if (RD->hasFlexibleArrayMember())
3017 return false;
3018
3019 // Check that all sub-fields are at offset 0, and are themselves "integer
3020 // like".
3021 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3022
3023 bool HadField = false;
3024 unsigned idx = 0;
3025 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3026 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003027 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003028
Daniel Dunbar679855a2010-01-29 03:22:29 +00003029 // Bit-fields are not addressable, we only need to verify they are "integer
3030 // like". We still have to disallow a subsequent non-bitfield, for example:
3031 // struct { int : 0; int x }
3032 // is non-integer like according to gcc.
3033 if (FD->isBitField()) {
3034 if (!RD->isUnion())
3035 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003036
Daniel Dunbar679855a2010-01-29 03:22:29 +00003037 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3038 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003039
Daniel Dunbar679855a2010-01-29 03:22:29 +00003040 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003041 }
3042
Daniel Dunbar679855a2010-01-29 03:22:29 +00003043 // Check if this field is at offset 0.
3044 if (Layout.getFieldOffset(idx) != 0)
3045 return false;
3046
Daniel Dunbar98303b92009-09-13 08:03:58 +00003047 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3048 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003049
Daniel Dunbar679855a2010-01-29 03:22:29 +00003050 // Only allow at most one field in a structure. This doesn't match the
3051 // wording above, but follows gcc in situations with a field following an
3052 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003053 if (!RD->isUnion()) {
3054 if (HadField)
3055 return false;
3056
3057 HadField = true;
3058 }
3059 }
3060
3061 return true;
3062}
3063
Chris Lattnera3c109b2010-07-29 02:16:43 +00003064ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003065 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003066 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003067
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003068 // Large vector types should be returned via memory.
3069 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3070 return ABIArgInfo::getIndirect(0);
3071
John McCalld608cdb2010-08-22 10:59:02 +00003072 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003073 // Treat an enum type as its underlying type.
3074 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3075 RetTy = EnumTy->getDecl()->getIntegerType();
3076
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003077 return (RetTy->isPromotableIntegerType() ?
3078 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003079 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003080
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003081 // Structures with either a non-trivial destructor or a non-trivial
3082 // copy constructor are always indirect.
3083 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3084 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3085
Daniel Dunbar98303b92009-09-13 08:03:58 +00003086 // Are we following APCS?
3087 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003088 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003089 return ABIArgInfo::getIgnore();
3090
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003091 // Complex types are all returned as packed integers.
3092 //
3093 // FIXME: Consider using 2 x vector types if the back end handles them
3094 // correctly.
3095 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003096 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003097 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003098
Daniel Dunbar98303b92009-09-13 08:03:58 +00003099 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003100 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003101 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003102 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003103 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003104 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003105 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003106 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3107 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003108 }
3109
3110 // Otherwise return in memory.
3111 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003112 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003113
3114 // Otherwise this is an AAPCS variant.
3115
Chris Lattnera3c109b2010-07-29 02:16:43 +00003116 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003117 return ABIArgInfo::getIgnore();
3118
Bob Wilson3b694fa2011-11-02 04:51:36 +00003119 // Check for homogeneous aggregates with AAPCS-VFP.
3120 if (getABIKind() == AAPCS_VFP) {
3121 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003122 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3123 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003124 // Homogeneous Aggregates are returned directly.
3125 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003126 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003127 }
3128
Daniel Dunbar98303b92009-09-13 08:03:58 +00003129 // Aggregates <= 4 bytes are returned in r0; other aggregates
3130 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003131 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003132 if (Size <= 32) {
3133 // Return in the smallest viable integer type.
3134 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003135 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003136 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003137 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3138 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003139 }
3140
Daniel Dunbar98303b92009-09-13 08:03:58 +00003141 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003142}
3143
3144llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003145 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003146 llvm::Type *BP = CGF.Int8PtrTy;
3147 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003148
3149 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003150 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003151 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00003152 // Handle address alignment for type alignment > 32 bits
3153 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
3154 if (TyAlign > 4) {
3155 assert((TyAlign & (TyAlign - 1)) == 0 &&
3156 "Alignment is not power of 2!");
3157 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3158 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3159 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
3160 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
3161 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003162 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003163 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003164 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3165
3166 uint64_t Offset =
3167 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3168 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003169 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003170 "ap.next");
3171 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3172
3173 return AddrTyped;
3174}
3175
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003176//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003177// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003178//===----------------------------------------------------------------------===//
3179
3180namespace {
3181
Justin Holewinski2c585b92012-05-24 17:43:12 +00003182class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003183public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003184 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003185
3186 ABIArgInfo classifyReturnType(QualType RetTy) const;
3187 ABIArgInfo classifyArgumentType(QualType Ty) const;
3188
3189 virtual void computeInfo(CGFunctionInfo &FI) const;
3190 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3191 CodeGenFunction &CFG) const;
3192};
3193
Justin Holewinski2c585b92012-05-24 17:43:12 +00003194class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003195public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003196 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3197 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003198
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003199 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3200 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003201};
3202
Justin Holewinski2c585b92012-05-24 17:43:12 +00003203ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003204 if (RetTy->isVoidType())
3205 return ABIArgInfo::getIgnore();
3206 if (isAggregateTypeForABI(RetTy))
3207 return ABIArgInfo::getIndirect(0);
3208 return ABIArgInfo::getDirect();
3209}
3210
Justin Holewinski2c585b92012-05-24 17:43:12 +00003211ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003212 if (isAggregateTypeForABI(Ty))
3213 return ABIArgInfo::getIndirect(0);
3214
3215 return ABIArgInfo::getDirect();
3216}
3217
Justin Holewinski2c585b92012-05-24 17:43:12 +00003218void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003219 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3220 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3221 it != ie; ++it)
3222 it->info = classifyArgumentType(it->type);
3223
3224 // Always honor user-specified calling convention.
3225 if (FI.getCallingConvention() != llvm::CallingConv::C)
3226 return;
3227
3228 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003229 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3230 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003231 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003232 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003233 if (LangOpts.OpenCL || LangOpts.CUDA) {
3234 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003235 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003236 } else {
3237 // If we are in standard C/C++ mode, use the triple to decide on the default
3238 StringRef Env =
3239 getContext().getTargetInfo().getTriple().getEnvironmentName();
3240 if (Env == "device")
3241 DefaultCC = llvm::CallingConv::PTX_Device;
3242 else
3243 DefaultCC = llvm::CallingConv::PTX_Kernel;
3244 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003245 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003246
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003247}
3248
Justin Holewinski2c585b92012-05-24 17:43:12 +00003249llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3250 CodeGenFunction &CFG) const {
3251 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003252}
3253
Justin Holewinski2c585b92012-05-24 17:43:12 +00003254void NVPTXTargetCodeGenInfo::
3255SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3256 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003257 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3258 if (!FD) return;
3259
3260 llvm::Function *F = cast<llvm::Function>(GV);
3261
3262 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003263 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003264 // Use OpenCL function attributes to set proper calling conventions
3265 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003266 if (FD->hasAttr<OpenCLKernelAttr>()) {
3267 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003268 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003269 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003270 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003271 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003272 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003273
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003274 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003275 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003276 // CUDA __global__ functions get a kernel calling convention. Since
3277 // __global__ functions cannot be called from the device, we do not
3278 // need to set the noinline attribute.
3279 if (FD->getAttr<CUDAGlobalAttr>())
3280 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003281 }
3282}
3283
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003284}
3285
3286//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003287// MBlaze ABI Implementation
3288//===----------------------------------------------------------------------===//
3289
3290namespace {
3291
3292class MBlazeABIInfo : public ABIInfo {
3293public:
3294 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3295
3296 bool isPromotableIntegerType(QualType Ty) const;
3297
3298 ABIArgInfo classifyReturnType(QualType RetTy) const;
3299 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3300
3301 virtual void computeInfo(CGFunctionInfo &FI) const {
3302 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3303 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3304 it != ie; ++it)
3305 it->info = classifyArgumentType(it->type);
3306 }
3307
3308 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3309 CodeGenFunction &CGF) const;
3310};
3311
3312class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3313public:
3314 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3315 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3316 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3317 CodeGen::CodeGenModule &M) const;
3318};
3319
3320}
3321
3322bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3323 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3324 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3325 switch (BT->getKind()) {
3326 case BuiltinType::Bool:
3327 case BuiltinType::Char_S:
3328 case BuiltinType::Char_U:
3329 case BuiltinType::SChar:
3330 case BuiltinType::UChar:
3331 case BuiltinType::Short:
3332 case BuiltinType::UShort:
3333 return true;
3334 default:
3335 return false;
3336 }
3337 return false;
3338}
3339
3340llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3341 CodeGenFunction &CGF) const {
3342 // FIXME: Implement
3343 return 0;
3344}
3345
3346
3347ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3348 if (RetTy->isVoidType())
3349 return ABIArgInfo::getIgnore();
3350 if (isAggregateTypeForABI(RetTy))
3351 return ABIArgInfo::getIndirect(0);
3352
3353 return (isPromotableIntegerType(RetTy) ?
3354 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3355}
3356
3357ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3358 if (isAggregateTypeForABI(Ty))
3359 return ABIArgInfo::getIndirect(0);
3360
3361 return (isPromotableIntegerType(Ty) ?
3362 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3363}
3364
3365void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3366 llvm::GlobalValue *GV,
3367 CodeGen::CodeGenModule &M)
3368 const {
3369 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3370 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003371
Wesley Peck276fdf42010-12-19 19:57:51 +00003372 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3373 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3374 CC = llvm::CallingConv::MBLAZE_INTR;
3375 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3376 CC = llvm::CallingConv::MBLAZE_SVOL;
3377
3378 if (CC != llvm::CallingConv::C) {
3379 // Handle 'interrupt_handler' attribute:
3380 llvm::Function *F = cast<llvm::Function>(GV);
3381
3382 // Step 1: Set ISR calling convention.
3383 F->setCallingConv(CC);
3384
3385 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003386 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003387 }
3388
3389 // Step 3: Emit _interrupt_handler alias.
3390 if (CC == llvm::CallingConv::MBLAZE_INTR)
3391 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3392 "_interrupt_handler", GV, &M.getModule());
3393}
3394
3395
3396//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003397// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003398//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003399
3400namespace {
3401
3402class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3403public:
Chris Lattnerea044322010-07-29 02:01:43 +00003404 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3405 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003406 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3407 CodeGen::CodeGenModule &M) const;
3408};
3409
3410}
3411
3412void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3413 llvm::GlobalValue *GV,
3414 CodeGen::CodeGenModule &M) const {
3415 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3416 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3417 // Handle 'interrupt' attribute:
3418 llvm::Function *F = cast<llvm::Function>(GV);
3419
3420 // Step 1: Set ISR calling convention.
3421 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3422
3423 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003424 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003425
3426 // Step 3: Emit ISR vector alias.
3427 unsigned Num = attr->getNumber() + 0xffe0;
3428 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003429 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003430 GV, &M.getModule());
3431 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003432 }
3433}
3434
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003435//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003436// MIPS ABI Implementation. This works for both little-endian and
3437// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003438//===----------------------------------------------------------------------===//
3439
John McCallaeeb7012010-05-27 06:19:26 +00003440namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003441class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003442 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003443 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3444 void CoerceToIntArgs(uint64_t TySize,
3445 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003446 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003447 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003448 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003449public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003450 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003451 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3452 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003453
3454 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003455 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003456 virtual void computeInfo(CGFunctionInfo &FI) const;
3457 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3458 CodeGenFunction &CGF) const;
3459};
3460
John McCallaeeb7012010-05-27 06:19:26 +00003461class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003462 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003463public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003464 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3465 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3466 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003467
3468 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3469 return 29;
3470 }
3471
3472 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003473 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003474
3475 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003476 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003477 }
John McCallaeeb7012010-05-27 06:19:26 +00003478};
3479}
3480
Akira Hatanakac359f202012-07-03 19:24:06 +00003481void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3482 SmallVector<llvm::Type*, 8> &ArgList) const {
3483 llvm::IntegerType *IntTy =
3484 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003485
3486 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3487 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3488 ArgList.push_back(IntTy);
3489
3490 // If necessary, add one more integer type to ArgList.
3491 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3492
3493 if (R)
3494 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003495}
3496
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003497// In N32/64, an aligned double precision floating point field is passed in
3498// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003499llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003500 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3501
3502 if (IsO32) {
3503 CoerceToIntArgs(TySize, ArgList);
3504 return llvm::StructType::get(getVMContext(), ArgList);
3505 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003506
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003507 if (Ty->isComplexType())
3508 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003509
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003510 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003511
Akira Hatanakac359f202012-07-03 19:24:06 +00003512 // Unions/vectors are passed in integer registers.
3513 if (!RT || !RT->isStructureOrClassType()) {
3514 CoerceToIntArgs(TySize, ArgList);
3515 return llvm::StructType::get(getVMContext(), ArgList);
3516 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003517
3518 const RecordDecl *RD = RT->getDecl();
3519 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003520 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003521
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003522 uint64_t LastOffset = 0;
3523 unsigned idx = 0;
3524 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3525
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003526 // Iterate over fields in the struct/class and check if there are any aligned
3527 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003528 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3529 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003530 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003531 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3532
3533 if (!BT || BT->getKind() != BuiltinType::Double)
3534 continue;
3535
3536 uint64_t Offset = Layout.getFieldOffset(idx);
3537 if (Offset % 64) // Ignore doubles that are not aligned.
3538 continue;
3539
3540 // Add ((Offset - LastOffset) / 64) args of type i64.
3541 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3542 ArgList.push_back(I64);
3543
3544 // Add double type.
3545 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3546 LastOffset = Offset + 64;
3547 }
3548
Akira Hatanakac359f202012-07-03 19:24:06 +00003549 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3550 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003551
3552 return llvm::StructType::get(getVMContext(), ArgList);
3553}
3554
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003555llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003556 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003557
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003558 if ((Align - 1) & Offset)
3559 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3560
3561 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003562}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003563
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003564ABIArgInfo
3565MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003566 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003567 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003568 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003569
Akira Hatanakac359f202012-07-03 19:24:06 +00003570 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3571 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003572 Offset = llvm::RoundUpToAlignment(Offset, Align);
3573 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003574
Akira Hatanakac359f202012-07-03 19:24:06 +00003575 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003576 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003577 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003578 return ABIArgInfo::getIgnore();
3579
Akira Hatanaka511949b2011-08-01 18:09:58 +00003580 // Records with non trivial destructors/constructors should not be passed
3581 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003582 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003583 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003584 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003585 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003586
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003587 // If we have reached here, aggregates are passed directly by coercing to
3588 // another structure type. Padding is inserted if the offset of the
3589 // aggregate is unaligned.
3590 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3591 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003592 }
3593
3594 // Treat an enum type as its underlying type.
3595 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3596 Ty = EnumTy->getDecl()->getIntegerType();
3597
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003598 if (Ty->isPromotableIntegerType())
3599 return ABIArgInfo::getExtend();
3600
3601 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003602}
3603
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003604llvm::Type*
3605MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003606 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003607 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003608
Akira Hatanakada54ff32012-02-09 18:49:26 +00003609 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003610 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003611 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3612 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003613
Akira Hatanakada54ff32012-02-09 18:49:26 +00003614 // N32/64 returns struct/classes in floating point registers if the
3615 // following conditions are met:
3616 // 1. The size of the struct/class is no larger than 128-bit.
3617 // 2. The struct/class has one or two fields all of which are floating
3618 // point types.
3619 // 3. The offset of the first field is zero (this follows what gcc does).
3620 //
3621 // Any other composite results are returned in integer registers.
3622 //
3623 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3624 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3625 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003626 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003627
Akira Hatanakada54ff32012-02-09 18:49:26 +00003628 if (!BT || !BT->isFloatingPoint())
3629 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003630
David Blaikie262bc182012-04-30 02:36:29 +00003631 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003632 }
3633
3634 if (b == e)
3635 return llvm::StructType::get(getVMContext(), RTList,
3636 RD->hasAttr<PackedAttr>());
3637
3638 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003639 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003640 }
3641
Akira Hatanakac359f202012-07-03 19:24:06 +00003642 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003643 return llvm::StructType::get(getVMContext(), RTList);
3644}
3645
Akira Hatanaka619e8872011-06-02 00:09:17 +00003646ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003647 uint64_t Size = getContext().getTypeSize(RetTy);
3648
3649 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003650 return ABIArgInfo::getIgnore();
3651
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003652 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003653 if (Size <= 128) {
3654 if (RetTy->isAnyComplexType())
3655 return ABIArgInfo::getDirect();
3656
Akira Hatanakac359f202012-07-03 19:24:06 +00003657 // O32 returns integer vectors in registers.
3658 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3659 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3660
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003661 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003662 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3663 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003664
3665 return ABIArgInfo::getIndirect(0);
3666 }
3667
3668 // Treat an enum type as its underlying type.
3669 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3670 RetTy = EnumTy->getDecl()->getIntegerType();
3671
3672 return (RetTy->isPromotableIntegerType() ?
3673 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3674}
3675
3676void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003677 ABIArgInfo &RetInfo = FI.getReturnInfo();
3678 RetInfo = classifyReturnType(FI.getReturnType());
3679
3680 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003681 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003682
Akira Hatanaka619e8872011-06-02 00:09:17 +00003683 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3684 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003685 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003686}
3687
3688llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3689 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003690 llvm::Type *BP = CGF.Int8PtrTy;
3691 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003692
3693 CGBuilderTy &Builder = CGF.Builder;
3694 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3695 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003696 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003697 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3698 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003699 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3700 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003701
3702 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003703 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3704 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3705 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3706 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003707 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3708 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3709 }
3710 else
3711 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3712
3713 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003714 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003715 uint64_t Offset =
3716 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3717 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003718 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003719 "ap.next");
3720 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3721
3722 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003723}
3724
John McCallaeeb7012010-05-27 06:19:26 +00003725bool
3726MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3727 llvm::Value *Address) const {
3728 // This information comes from gcc's implementation, which seems to
3729 // as canonical as it gets.
3730
John McCallaeeb7012010-05-27 06:19:26 +00003731 // Everything on MIPS is 4 bytes. Double-precision FP registers
3732 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003733 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003734
3735 // 0-31 are the general purpose registers, $0 - $31.
3736 // 32-63 are the floating-point registers, $f0 - $f31.
3737 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3738 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003739 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003740
3741 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3742 // They are one bit wide and ignored here.
3743
3744 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3745 // (coprocessor 1 is the FP unit)
3746 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3747 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3748 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003749 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003750 return false;
3751}
3752
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003753//===----------------------------------------------------------------------===//
3754// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3755// Currently subclassed only to implement custom OpenCL C function attribute
3756// handling.
3757//===----------------------------------------------------------------------===//
3758
3759namespace {
3760
3761class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3762public:
3763 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3764 : DefaultTargetCodeGenInfo(CGT) {}
3765
3766 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3767 CodeGen::CodeGenModule &M) const;
3768};
3769
3770void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3771 llvm::GlobalValue *GV,
3772 CodeGen::CodeGenModule &M) const {
3773 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3774 if (!FD) return;
3775
3776 llvm::Function *F = cast<llvm::Function>(GV);
3777
David Blaikie4e4d0842012-03-11 07:00:24 +00003778 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003779 if (FD->hasAttr<OpenCLKernelAttr>()) {
3780 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003781 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003782
3783 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3784
3785 // Convert the reqd_work_group_size() attributes to metadata.
3786 llvm::LLVMContext &Context = F->getContext();
3787 llvm::NamedMDNode *OpenCLMetadata =
3788 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3789
3790 SmallVector<llvm::Value*, 5> Operands;
3791 Operands.push_back(F);
3792
Chris Lattner8b418682012-02-07 00:39:47 +00003793 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3794 llvm::APInt(32,
3795 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3796 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3797 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003798 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003799 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3800 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003801 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3802
3803 // Add a boolean constant operand for "required" (true) or "hint" (false)
3804 // for implementing the work_group_size_hint attr later. Currently
3805 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003806 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003807 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3808 }
3809 }
3810 }
3811}
3812
3813}
John McCallaeeb7012010-05-27 06:19:26 +00003814
Tony Linthicum96319392011-12-12 21:14:55 +00003815//===----------------------------------------------------------------------===//
3816// Hexagon ABI Implementation
3817//===----------------------------------------------------------------------===//
3818
3819namespace {
3820
3821class HexagonABIInfo : public ABIInfo {
3822
3823
3824public:
3825 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3826
3827private:
3828
3829 ABIArgInfo classifyReturnType(QualType RetTy) const;
3830 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3831
3832 virtual void computeInfo(CGFunctionInfo &FI) const;
3833
3834 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3835 CodeGenFunction &CGF) const;
3836};
3837
3838class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3839public:
3840 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3841 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3842
3843 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3844 return 29;
3845 }
3846};
3847
3848}
3849
3850void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3851 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3852 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3853 it != ie; ++it)
3854 it->info = classifyArgumentType(it->type);
3855}
3856
3857ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3858 if (!isAggregateTypeForABI(Ty)) {
3859 // Treat an enum type as its underlying type.
3860 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3861 Ty = EnumTy->getDecl()->getIntegerType();
3862
3863 return (Ty->isPromotableIntegerType() ?
3864 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3865 }
3866
3867 // Ignore empty records.
3868 if (isEmptyRecord(getContext(), Ty, true))
3869 return ABIArgInfo::getIgnore();
3870
3871 // Structures with either a non-trivial destructor or a non-trivial
3872 // copy constructor are always indirect.
3873 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3874 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3875
3876 uint64_t Size = getContext().getTypeSize(Ty);
3877 if (Size > 64)
3878 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3879 // Pass in the smallest viable integer type.
3880 else if (Size > 32)
3881 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3882 else if (Size > 16)
3883 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3884 else if (Size > 8)
3885 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3886 else
3887 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3888}
3889
3890ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3891 if (RetTy->isVoidType())
3892 return ABIArgInfo::getIgnore();
3893
3894 // Large vector types should be returned via memory.
3895 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3896 return ABIArgInfo::getIndirect(0);
3897
3898 if (!isAggregateTypeForABI(RetTy)) {
3899 // Treat an enum type as its underlying type.
3900 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3901 RetTy = EnumTy->getDecl()->getIntegerType();
3902
3903 return (RetTy->isPromotableIntegerType() ?
3904 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3905 }
3906
3907 // Structures with either a non-trivial destructor or a non-trivial
3908 // copy constructor are always indirect.
3909 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3910 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3911
3912 if (isEmptyRecord(getContext(), RetTy, true))
3913 return ABIArgInfo::getIgnore();
3914
3915 // Aggregates <= 8 bytes are returned in r0; other aggregates
3916 // are returned indirectly.
3917 uint64_t Size = getContext().getTypeSize(RetTy);
3918 if (Size <= 64) {
3919 // Return in the smallest viable integer type.
3920 if (Size <= 8)
3921 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3922 if (Size <= 16)
3923 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3924 if (Size <= 32)
3925 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3926 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3927 }
3928
3929 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3930}
3931
3932llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00003933 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00003934 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00003935 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00003936
3937 CGBuilderTy &Builder = CGF.Builder;
3938 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3939 "ap");
3940 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3941 llvm::Type *PTy =
3942 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3943 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3944
3945 uint64_t Offset =
3946 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3947 llvm::Value *NextAddr =
3948 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3949 "ap.next");
3950 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3951
3952 return AddrTyped;
3953}
3954
3955
Chris Lattnerea044322010-07-29 02:01:43 +00003956const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003957 if (TheTargetCodeGenInfo)
3958 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003959
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003960 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003961 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003962 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003963 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003964
Derek Schuff9ed63f82012-09-06 17:37:28 +00003965 case llvm::Triple::le32:
3966 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00003967 case llvm::Triple::mips:
3968 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003969 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003970
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003971 case llvm::Triple::mips64:
3972 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003973 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003974
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003975 case llvm::Triple::arm:
3976 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003977 {
3978 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003979
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003980 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003981 Kind = ARMABIInfo::APCS;
3982 else if (CodeGenOpts.FloatABI == "hard")
3983 Kind = ARMABIInfo::AAPCS_VFP;
3984
3985 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3986 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003987
John McCallec853ba2010-03-11 00:10:12 +00003988 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003989 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003990 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003991 if (Triple.isOSBinFormatELF())
3992 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
3993 else
3994 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003995
Peter Collingbourneedb66f32012-05-20 23:28:41 +00003996 case llvm::Triple::nvptx:
3997 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003998 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003999
Wesley Peck276fdf42010-12-19 19:57:51 +00004000 case llvm::Triple::mblaze:
4001 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4002
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004003 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004004 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004005
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004006 case llvm::Triple::tce:
4007 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4008
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004009 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004010 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004011
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004012 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004013 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004014 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4015 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004016
4017 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004018 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004019 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004020 case llvm::Triple::AuroraUX:
4021 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004022 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004023 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004024 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004025 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004026 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4027 false,
4028 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004029
4030 case llvm::Triple::Win32:
4031 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004032 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4033 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004034
4035 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004036 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004037 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4038 false,
4039 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004040 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004041 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004042
Eli Friedmanee1ad992011-12-02 00:11:43 +00004043 case llvm::Triple::x86_64: {
4044 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4045
Chris Lattnerf13721d2010-08-31 16:44:54 +00004046 switch (Triple.getOS()) {
4047 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004048 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004049 case llvm::Triple::Cygwin:
4050 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
4051 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004052 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4053 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004054 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004055 }
Tony Linthicum96319392011-12-12 21:14:55 +00004056 case llvm::Triple::hexagon:
4057 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004058 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004059}