blob: 06e1a3e42bc5d8511e56b4db1f454d17786f4145 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-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 Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-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 McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-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
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall3480ef22011-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 McCalla729c622012-02-17 03:33:10 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-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 Dunbar626f1d82009-09-13 08:03:58 +0000110static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000111
112/// isEmptyField - Return true iff a the field is "empty", that is it
113/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000114static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
115 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000116 if (FD->isUnnamedBitfield())
117 return true;
118
119 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000120
Eli Friedman0b3f2012011-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 Dunbar626f1d82009-09-13 08:03:58 +0000123 if (AllowArrays)
Eli Friedman0b3f2012011-11-18 03:47:20 +0000124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
125 if (AT->getSize() == 0)
126 return true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000127 FT = AT->getElementType();
Eli Friedman0b3f2012011-11-18 03:47:20 +0000128 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000129
Daniel Dunbarcd20ce12010-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 Dunbar626f1d82009-09-13 08:03:58 +0000141 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000142}
143
144/// isEmptyRecord - Return true iff a structure contains only empty
145/// fields. Note that a structure with a flexible array member is not
146/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000147static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000148 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Dunbarcd20ce12010-05-17 16:46:00 +0000154
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000155 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisd42411f2011-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 Dunbarcd20ce12010-05-17 16:46:00 +0000161
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
163 i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +0000164 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000165 return false;
166 return true;
167}
168
Anders Carlsson20759ad2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000175
Anders Carlsson20759ad2009-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 Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000208
Daniel Dunbar12ebb472010-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 Dunbar12ebb472010-05-11 21:15:36 +0000213 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000214 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000232 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000233 QualType FT = FD->getType();
234
235 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000236 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 McCalla1dee5302010-08-22 10:59:02 +0000251 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-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 Friedmanee945342011-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 Korobeynikov244360d2009-06-05 22:08:42 +0000265 return Found;
266}
267
268static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000269 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000270 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
271 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-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 Dunbar11c08c82009-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 Friedmane5c85622011-11-18 01:32:26 +0000300 uint64_t Size = 0;
301
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000302 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
303 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000304 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-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 Friedmane5c85622011-11-18 01:32:26 +0000314
315 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000316 }
317
Eli Friedmane5c85622011-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 Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +0000331public:
332 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000333
Chris Lattner458b2aa2010-07-29 02:16:43 +0000334 ABIArgInfo classifyReturnType(QualType RetTy) const;
335 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000336
Chris Lattner22326a12010-07-29 02:31:05 +0000337 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000341 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000342 }
343
344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345 CodeGenFunction &CGF) const;
346};
347
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000348class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
349public:
Chris Lattner2b037972010-07-29 02:01:43 +0000350 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-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 Lattner458b2aa2010-07-29 02:16:43 +0000359ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung180319f2011-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 Korobeynikov55bcea12010-01-10 12:58:08 +0000366 return ABIArgInfo::getIndirect(0);
Jan Wen Voung180319f2011-11-03 00:59:44 +0000367 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000368
Chris Lattner9723d6c2010-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 Gregora71cc152010-02-02 20:10:50 +0000372
Chris Lattner9723d6c2010-03-11 18:19:55 +0000373 return (Ty->isPromotableIntegerType() ?
374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000375}
376
Bob Wilsonbd4520b2011-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
Eli Friedmana98d1f82012-01-25 22:46:34 +0000392/// UseX86_MMXType - Return true if this is an MMX type that should use the
393/// special x86_mmx type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000394bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendling5cd41c42010-10-18 03:41:31 +0000395 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
396 // special x86_mmx type.
397 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
398 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
399 IRType->getScalarSizeInBits() != 64;
400}
401
Jay Foad7c57be32011-07-11 09:56:20 +0000402static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000403 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000404 llvm::Type* Ty) {
Bill Wendlingec9d2632011-03-07 22:47:14 +0000405 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000406 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
407 return Ty;
408}
409
Chris Lattner0cf24192010-06-28 20:05:43 +0000410//===----------------------------------------------------------------------===//
411// X86-32 ABI Implementation
412//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000413
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000414/// X86_32ABIInfo - The X86-32 ABI information.
415class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000416 static const unsigned MinABIStackAlignInBytes = 4;
417
David Chisnallde3a0692009-08-17 23:08:21 +0000418 bool IsDarwinVectorABI;
419 bool IsSmallStructInRegABI;
Eli Friedman33465822011-07-08 23:31:17 +0000420 bool IsMMXDisabled;
Eli Friedmana98d1f82012-01-25 22:46:34 +0000421 bool IsWin32FloatStructABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000422
423 static bool isRegisterSize(unsigned Size) {
424 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
425 }
426
Aaron Ballman3c424412012-02-22 03:04:13 +0000427 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
428 unsigned callingConvention);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000429
Daniel Dunbar557893d2010-04-21 19:10:51 +0000430 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
431 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000432 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000433
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000434 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000435 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000436
Rafael Espindola75419dc2012-07-23 23:30:29 +0000437 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman3c424412012-02-22 03:04:13 +0000438 unsigned callingConvention) const;
Chris Lattner458b2aa2010-07-29 02:16:43 +0000439 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000440
Rafael Espindola75419dc2012-07-23 23:30:29 +0000441public:
442
Rafael Espindolaa6472962012-07-24 00:01:07 +0000443 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000444 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
445 CodeGenFunction &CGF) const;
446
Eli Friedmana98d1f82012-01-25 22:46:34 +0000447 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
Eli Friedman33465822011-07-08 23:31:17 +0000448 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Eli Friedmana98d1f82012-01-25 22:46:34 +0000449 IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000450};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000451
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000452class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
453public:
Eli Friedmana98d1f82012-01-25 22:46:34 +0000454 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
455 bool d, bool p, bool m, bool w)
456 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000457
458 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
459 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000460
461 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
462 // Darwin uses different dwarf register numbers for EH.
463 if (CGM.isTargetDarwin()) return 5;
464
465 return 4;
466 }
467
468 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
469 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000470
Jay Foad7c57be32011-07-11 09:56:20 +0000471 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000472 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000473 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000474 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
475 }
476
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000477};
478
479}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000480
481/// shouldReturnTypeInRegister - Determine if the given type should be
482/// passed in a register (for the Darwin ABI).
483bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman3c424412012-02-22 03:04:13 +0000484 ASTContext &Context,
485 unsigned callingConvention) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000486 uint64_t Size = Context.getTypeSize(Ty);
487
488 // Type must be register sized.
489 if (!isRegisterSize(Size))
490 return false;
491
492 if (Ty->isVectorType()) {
493 // 64- and 128- bit vectors inside structures are not returned in
494 // registers.
495 if (Size == 64 || Size == 128)
496 return false;
497
498 return true;
499 }
500
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000501 // If this is a builtin, pointer, enum, complex type, member pointer, or
502 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000503 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000504 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000505 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000506 return true;
507
508 // Arrays are treated like records.
509 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman3c424412012-02-22 03:04:13 +0000510 return shouldReturnTypeInRegister(AT->getElementType(), Context,
511 callingConvention);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512
513 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000514 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000515 if (!RT) return false;
516
Anders Carlsson40446e82010-01-27 03:25:19 +0000517 // FIXME: Traverse bases here too.
518
Aaron Ballman3c424412012-02-22 03:04:13 +0000519 // For thiscall conventions, structures will never be returned in
520 // a register. This is for compatibility with the MSVC ABI
521 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
522 RT->isStructureType()) {
523 return false;
524 }
525
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000526 // Structure types are passed in register if all fields would be
527 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000528 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
529 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000530 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000531
532 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000533 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000534 continue;
535
536 // Check fields recursively.
Aaron Ballman3c424412012-02-22 03:04:13 +0000537 if (!shouldReturnTypeInRegister(FD->getType(), Context,
538 callingConvention))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000539 return false;
540 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000541 return true;
542}
543
Aaron Ballman3c424412012-02-22 03:04:13 +0000544ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
545 unsigned callingConvention) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000546 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000547 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000548
Chris Lattner458b2aa2010-07-29 02:16:43 +0000549 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000550 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000551 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000552 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000553
554 // 128-bit vectors are a special case; they are returned in
555 // registers and we need to make sure to pick a type the LLVM
556 // backend will like.
557 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000558 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000559 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000560
561 // Always return in register if it fits in a general purpose
562 // register, or if it is 64 bits and has a single element.
563 if ((Size == 8 || Size == 16 || Size == 32) ||
564 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000565 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000566 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000567
568 return ABIArgInfo::getIndirect(0);
569 }
570
571 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000572 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000573
John McCalla1dee5302010-08-22 10:59:02 +0000574 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000575 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000576 // Structures with either a non-trivial destructor or a non-trivial
577 // copy constructor are always indirect.
578 if (hasNonTrivialDestructorOrCopyConstructor(RT))
579 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000580
Anders Carlsson5789c492009-10-20 22:07:59 +0000581 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000582 if (RT->getDecl()->hasFlexibleArrayMember())
583 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000584 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000585
David Chisnallde3a0692009-08-17 23:08:21 +0000586 // If specified, structs and unions are always indirect.
587 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000588 return ABIArgInfo::getIndirect(0);
589
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000590 // Small structures which are register sized are generally returned
591 // in a register.
Aaron Ballman3c424412012-02-22 03:04:13 +0000592 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
593 callingConvention)) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000594 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000595
596 // As a special-case, if the struct is a "single-element" struct, and
597 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +0000598 // floating-point register. (MSVC does not apply this special case.)
599 // We apply a similar transformation for pointer types to improve the
600 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +0000601 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedmana98d1f82012-01-25 22:46:34 +0000602 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
603 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +0000604 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
605
606 // FIXME: We should be able to narrow this integer in cases with dead
607 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000608 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000609 }
610
611 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000612 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000613
Chris Lattner458b2aa2010-07-29 02:16:43 +0000614 // Treat an enum type as its underlying type.
615 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
616 RetTy = EnumTy->getDecl()->getIntegerType();
617
618 return (RetTy->isPromotableIntegerType() ?
619 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000620}
621
Eli Friedman7919bea2012-06-05 19:40:46 +0000622static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
623 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
624}
625
Daniel Dunbared23de32010-09-16 20:42:00 +0000626static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
627 const RecordType *RT = Ty->getAs<RecordType>();
628 if (!RT)
629 return 0;
630 const RecordDecl *RD = RT->getDecl();
631
632 // If this is a C++ record, check the bases first.
633 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
634 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
635 e = CXXRD->bases_end(); i != e; ++i)
636 if (!isRecordWithSSEVectorType(Context, i->getType()))
637 return false;
638
639 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
640 i != e; ++i) {
641 QualType FT = i->getType();
642
Eli Friedman7919bea2012-06-05 19:40:46 +0000643 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +0000644 return true;
645
646 if (isRecordWithSSEVectorType(Context, FT))
647 return true;
648 }
649
650 return false;
651}
652
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000653unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
654 unsigned Align) const {
655 // Otherwise, if the alignment is less than or equal to the minimum ABI
656 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000657 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000658 return 0; // Use default alignment.
659
660 // On non-Darwin, the stack type alignment is always 4.
661 if (!IsDarwinVectorABI) {
662 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000663 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000664 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000665
Daniel Dunbared23de32010-09-16 20:42:00 +0000666 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +0000667 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
668 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +0000669 return 16;
670
671 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000672}
673
Chris Lattner458b2aa2010-07-29 02:16:43 +0000674ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000675 if (!ByVal)
676 return ABIArgInfo::getIndirect(0, false);
677
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000678 // Compute the byval alignment.
679 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
680 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
681 if (StackAlign == 0)
Chris Lattnere76b95a2011-05-22 23:35:00 +0000682 return ABIArgInfo::getIndirect(4);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000683
684 // If the stack alignment is less than the type alignment, realign the
685 // argument.
686 if (StackAlign < TypeAlign)
687 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
688 /*Realign=*/true);
689
690 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000691}
692
Chris Lattner458b2aa2010-07-29 02:16:43 +0000693ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000694 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000695 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000696 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000697 if (const RecordType *RT = Ty->getAs<RecordType>()) {
698 // Structures with either a non-trivial destructor or a non-trivial
699 // copy constructor are always indirect.
700 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000701 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000702
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000703 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000704 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000705 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000706
Eli Friedman9f061a32011-11-18 00:28:11 +0000707 // Ignore empty structs/unions.
Eli Friedmanf22fa9e2011-11-18 04:01:36 +0000708 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000709 return ABIArgInfo::getIgnore();
710
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000711 // Expand small (<= 128-bit) record types when we know that the stack layout
712 // of those arguments will match the struct. This is important because the
713 // LLVM backend isn't smart enough to remove byval, which inhibits many
714 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000715 if (getContext().getTypeSize(Ty) <= 4*32 &&
716 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000717 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000718
Chris Lattner458b2aa2010-07-29 02:16:43 +0000719 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000720 }
721
Chris Lattnerd774ae92010-08-26 20:05:13 +0000722 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000723 // On Darwin, some vectors are passed in memory, we handle this by passing
724 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000725 if (IsDarwinVectorABI) {
726 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000727 if ((Size == 8 || Size == 16 || Size == 32) ||
728 (Size == 64 && VT->getNumElements() == 1))
729 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
730 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000731 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000732
Chris Lattnera5f58b02011-07-09 17:41:47 +0000733 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +0000734 if (UseX86_MMXType(IRType)) {
Eli Friedman33465822011-07-08 23:31:17 +0000735 if (IsMMXDisabled)
736 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
737 64));
Bill Wendling5cd41c42010-10-18 03:41:31 +0000738 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
739 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
740 return AAI;
741 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000742
Chris Lattnerd774ae92010-08-26 20:05:13 +0000743 return ABIArgInfo::getDirect();
744 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000745
746
Chris Lattner458b2aa2010-07-29 02:16:43 +0000747 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
748 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000749
Chris Lattner458b2aa2010-07-29 02:16:43 +0000750 return (Ty->isPromotableIntegerType() ?
751 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000752}
753
Rafael Espindolaa6472962012-07-24 00:01:07 +0000754void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
755 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
756 FI.getCallingConvention());
757 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
758 it != ie; ++it)
759 it->info = classifyArgumentType(it->type);
760}
761
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000762llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
763 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +0000764 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000765
766 CGBuilderTy &Builder = CGF.Builder;
767 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
768 "ap");
769 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000770
771 // Compute if the address needs to be aligned
772 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
773 Align = getTypeStackAlignInBytes(Ty, Align);
774 Align = std::max(Align, 4U);
775 if (Align > 4) {
776 // addr = (addr + align - 1) & -align;
777 llvm::Value *Offset =
778 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
779 Addr = CGF.Builder.CreateGEP(Addr, Offset);
780 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
781 CGF.Int32Ty);
782 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
783 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
784 Addr->getType(),
785 "ap.cur.aligned");
786 }
787
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000788 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000789 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000790 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
791
792 uint64_t Offset =
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000793 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000794 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000795 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000796 "ap.next");
797 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
798
799 return AddrTyped;
800}
801
Charles Davis4ea31ab2010-02-13 15:54:06 +0000802void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
803 llvm::GlobalValue *GV,
804 CodeGen::CodeGenModule &CGM) const {
805 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
806 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
807 // Get the LLVM function.
808 llvm::Function *Fn = cast<llvm::Function>(GV);
809
810 // Now add the 'alignstack' attribute with a value of 16.
811 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
812 }
813 }
814}
815
John McCallbeec5a02010-03-06 00:35:14 +0000816bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
817 CodeGen::CodeGenFunction &CGF,
818 llvm::Value *Address) const {
819 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +0000820
Chris Lattnerece04092012-02-07 00:39:47 +0000821 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000822
John McCallbeec5a02010-03-06 00:35:14 +0000823 // 0-7 are the eight integer registers; the order is different
824 // on Darwin (for EH), but the range is the same.
825 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000826 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000827
828 if (CGF.CGM.isTargetDarwin()) {
829 // 12-16 are st(0..4). Not sure why we stop at 4.
830 // These have size 16, which is sizeof(long double) on
831 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +0000832 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +0000833 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000834
John McCallbeec5a02010-03-06 00:35:14 +0000835 } else {
836 // 9 is %eflags, which doesn't get a size on Darwin for some
837 // reason.
838 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
839
840 // 11-16 are st(0..5). Not sure why we stop at 5.
841 // These have size 12, which is sizeof(long double) on
842 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +0000843 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +0000844 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
845 }
John McCallbeec5a02010-03-06 00:35:14 +0000846
847 return false;
848}
849
Chris Lattner0cf24192010-06-28 20:05:43 +0000850//===----------------------------------------------------------------------===//
851// X86-64 ABI Implementation
852//===----------------------------------------------------------------------===//
853
854
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000855namespace {
856/// X86_64ABIInfo - The X86_64 ABI information.
857class X86_64ABIInfo : public ABIInfo {
858 enum Class {
859 Integer = 0,
860 SSE,
861 SSEUp,
862 X87,
863 X87Up,
864 ComplexX87,
865 NoClass,
866 Memory
867 };
868
869 /// merge - Implement the X86_64 ABI merging algorithm.
870 ///
871 /// Merge an accumulating classification \arg Accum with a field
872 /// classification \arg Field.
873 ///
874 /// \param Accum - The accumulating classification. This should
875 /// always be either NoClass or the result of a previous merge
876 /// call. In addition, this should never be Memory (the caller
877 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000878 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000879
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000880 /// postMerge - Implement the X86_64 ABI post merging algorithm.
881 ///
882 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
883 /// final MEMORY or SSE classes when necessary.
884 ///
885 /// \param AggregateSize - The size of the current aggregate in
886 /// the classification process.
887 ///
888 /// \param Lo - The classification for the parts of the type
889 /// residing in the low word of the containing object.
890 ///
891 /// \param Hi - The classification for the parts of the type
892 /// residing in the higher words of the containing object.
893 ///
894 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
895
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000896 /// classify - Determine the x86_64 register classes in which the
897 /// given type T should be passed.
898 ///
899 /// \param Lo - The classification for the parts of the type
900 /// residing in the low word of the containing object.
901 ///
902 /// \param Hi - The classification for the parts of the type
903 /// residing in the high word of the containing object.
904 ///
905 /// \param OffsetBase - The bit offset of this type in the
906 /// containing object. Some parameters are classified different
907 /// depending on whether they straddle an eightbyte boundary.
908 ///
909 /// If a word is unused its result will be NoClass; if a type should
910 /// be passed in Memory then at least the classification of \arg Lo
911 /// will be Memory.
912 ///
913 /// The \arg Lo class will be NoClass iff the argument is ignored.
914 ///
915 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
916 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000917 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000918
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000919 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000920 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
921 unsigned IROffset, QualType SourceTy,
922 unsigned SourceOffset) const;
923 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
924 unsigned IROffset, QualType SourceTy,
925 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000926
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000927 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000928 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000929 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000930
931 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000932 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +0000933 ///
934 /// \param freeIntRegs - The number of free integer registers remaining
935 /// available.
936 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000937
Chris Lattner458b2aa2010-07-29 02:16:43 +0000938 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000939
Bill Wendling5cd41c42010-10-18 03:41:31 +0000940 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +0000941 unsigned freeIntRegs,
Bill Wendling5cd41c42010-10-18 03:41:31 +0000942 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000943 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000944
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000945 bool IsIllegalVectorType(QualType Ty) const;
946
John McCalle0fda732011-04-21 01:20:55 +0000947 /// The 0.98 ABI revision clarified a lot of ambiguities,
948 /// unfortunately in ways that were not always consistent with
949 /// certain previous compilers. In particular, platforms which
950 /// required strict binary compatibility with older versions of GCC
951 /// may need to exempt themselves.
952 bool honorsRevision0_98() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000953 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +0000954 }
955
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000956 bool HasAVX;
957
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000958public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000959 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
960 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000961
John McCalla729c622012-02-17 03:33:10 +0000962 bool isPassedUsingAVXType(QualType type) const {
963 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +0000964 // The freeIntRegs argument doesn't matter here.
965 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCalla729c622012-02-17 03:33:10 +0000966 if (info.isDirect()) {
967 llvm::Type *ty = info.getCoerceToType();
968 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
969 return (vectorTy->getBitWidth() > 128);
970 }
971 return false;
972 }
973
Chris Lattner22326a12010-07-29 02:31:05 +0000974 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000975
976 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
977 CodeGenFunction &CGF) const;
978};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000979
Chris Lattner04dc9572010-08-31 16:44:54 +0000980/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000981class WinX86_64ABIInfo : public ABIInfo {
982
983 ABIArgInfo classify(QualType Ty) const;
984
Chris Lattner04dc9572010-08-31 16:44:54 +0000985public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000986 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
987
988 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000989
990 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
991 CodeGenFunction &CGF) const;
992};
993
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000994class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
995public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000996 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
997 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000998
John McCalla729c622012-02-17 03:33:10 +0000999 const X86_64ABIInfo &getABIInfo() const {
1000 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1001 }
1002
John McCallbeec5a02010-03-06 00:35:14 +00001003 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1004 return 7;
1005 }
1006
1007 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1008 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001009 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001010
John McCall943fae92010-05-27 06:19:26 +00001011 // 0-15 are the 16 integer registers.
1012 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001013 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00001014 return false;
1015 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001016
Jay Foad7c57be32011-07-11 09:56:20 +00001017 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001018 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +00001019 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001020 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1021 }
1022
John McCalla729c622012-02-17 03:33:10 +00001023 bool isNoProtoCallVariadic(const CallArgList &args,
1024 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +00001025 // The default CC on x86-64 sets %al to the number of SSA
1026 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001027 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00001028 // that when AVX types are involved: the ABI explicitly states it is
1029 // undefined, and it doesn't work in practice because of how the ABI
1030 // defines varargs anyway.
John McCalla729c622012-02-17 03:33:10 +00001031 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001032 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00001033 for (CallArgList::const_iterator
1034 it = args.begin(), ie = args.end(); it != ie; ++it) {
1035 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1036 HasAVXType = true;
1037 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001038 }
1039 }
John McCalla729c622012-02-17 03:33:10 +00001040
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001041 if (!HasAVXType)
1042 return true;
1043 }
John McCallcbc038a2011-09-21 08:08:30 +00001044
John McCalla729c622012-02-17 03:33:10 +00001045 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00001046 }
1047
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001048};
1049
Chris Lattner04dc9572010-08-31 16:44:54 +00001050class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1051public:
1052 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1053 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1054
1055 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1056 return 7;
1057 }
1058
1059 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1060 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001061 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001062
Chris Lattner04dc9572010-08-31 16:44:54 +00001063 // 0-15 are the 16 integer registers.
1064 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001065 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00001066 return false;
1067 }
1068};
1069
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001070}
1071
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001072void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1073 Class &Hi) const {
1074 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1075 //
1076 // (a) If one of the classes is Memory, the whole argument is passed in
1077 // memory.
1078 //
1079 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1080 // memory.
1081 //
1082 // (c) If the size of the aggregate exceeds two eightbytes and the first
1083 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1084 // argument is passed in memory. NOTE: This is necessary to keep the
1085 // ABI working for processors that don't support the __m256 type.
1086 //
1087 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1088 //
1089 // Some of these are enforced by the merging logic. Others can arise
1090 // only with unions; for example:
1091 // union { _Complex double; unsigned; }
1092 //
1093 // Note that clauses (b) and (c) were added in 0.98.
1094 //
1095 if (Hi == Memory)
1096 Lo = Memory;
1097 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1098 Lo = Memory;
1099 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1100 Lo = Memory;
1101 if (Hi == SSEUp && Lo != SSE)
1102 Hi = SSE;
1103}
1104
Chris Lattnerd776fb12010-06-28 21:43:59 +00001105X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001106 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1107 // classified recursively so that always two fields are
1108 // considered. The resulting class is calculated according to
1109 // the classes of the fields in the eightbyte:
1110 //
1111 // (a) If both classes are equal, this is the resulting class.
1112 //
1113 // (b) If one of the classes is NO_CLASS, the resulting class is
1114 // the other class.
1115 //
1116 // (c) If one of the classes is MEMORY, the result is the MEMORY
1117 // class.
1118 //
1119 // (d) If one of the classes is INTEGER, the result is the
1120 // INTEGER.
1121 //
1122 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1123 // MEMORY is used as class.
1124 //
1125 // (f) Otherwise class SSE is used.
1126
1127 // Accum should never be memory (we should have returned) or
1128 // ComplexX87 (because this cannot be passed in a structure).
1129 assert((Accum != Memory && Accum != ComplexX87) &&
1130 "Invalid accumulated classification during merge.");
1131 if (Accum == Field || Field == NoClass)
1132 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001133 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001134 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001135 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001136 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001137 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001138 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001139 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1140 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001141 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001142 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001143}
1144
Chris Lattner5c740f12010-06-30 19:14:05 +00001145void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001146 Class &Lo, Class &Hi) const {
1147 // FIXME: This code can be simplified by introducing a simple value class for
1148 // Class pairs with appropriate constructor methods for the various
1149 // situations.
1150
1151 // FIXME: Some of the split computations are wrong; unaligned vectors
1152 // shouldn't be passed in registers for example, so there is no chance they
1153 // can straddle an eightbyte. Verify & simplify.
1154
1155 Lo = Hi = NoClass;
1156
1157 Class &Current = OffsetBase < 64 ? Lo : Hi;
1158 Current = Memory;
1159
John McCall9dd450b2009-09-21 23:43:11 +00001160 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001161 BuiltinType::Kind k = BT->getKind();
1162
1163 if (k == BuiltinType::Void) {
1164 Current = NoClass;
1165 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1166 Lo = Integer;
1167 Hi = Integer;
1168 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1169 Current = Integer;
1170 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1171 Current = SSE;
1172 } else if (k == BuiltinType::LongDouble) {
1173 Lo = X87;
1174 Hi = X87Up;
1175 }
1176 // FIXME: _Decimal32 and _Decimal64 are SSE.
1177 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001178 return;
1179 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001180
Chris Lattnerd776fb12010-06-28 21:43:59 +00001181 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001182 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001183 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001184 return;
1185 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001186
Chris Lattnerd776fb12010-06-28 21:43:59 +00001187 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001188 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001189 return;
1190 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001191
Chris Lattnerd776fb12010-06-28 21:43:59 +00001192 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001193 if (Ty->isMemberFunctionPointerType())
1194 Lo = Hi = Integer;
1195 else
1196 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001197 return;
1198 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001199
Chris Lattnerd776fb12010-06-28 21:43:59 +00001200 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001201 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001202 if (Size == 32) {
1203 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1204 // float> as integer.
1205 Current = Integer;
1206
1207 // If this type crosses an eightbyte boundary, it should be
1208 // split.
1209 uint64_t EB_Real = (OffsetBase) / 64;
1210 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1211 if (EB_Real != EB_Imag)
1212 Hi = Lo;
1213 } else if (Size == 64) {
1214 // gcc passes <1 x double> in memory. :(
1215 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1216 return;
1217
1218 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001219 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001220 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1221 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1222 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001223 Current = Integer;
1224 else
1225 Current = SSE;
1226
1227 // If this type crosses an eightbyte boundary, it should be
1228 // split.
1229 if (OffsetBase && OffsetBase != 64)
1230 Hi = Lo;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001231 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001232 // Arguments of 256-bits are split into four eightbyte chunks. The
1233 // least significant one belongs to class SSE and all the others to class
1234 // SSEUP. The original Lo and Hi design considers that types can't be
1235 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1236 // This design isn't correct for 256-bits, but since there're no cases
1237 // where the upper parts would need to be inspected, avoid adding
1238 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001239 Lo = SSE;
1240 Hi = SSEUp;
1241 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001242 return;
1243 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001244
Chris Lattnerd776fb12010-06-28 21:43:59 +00001245 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001246 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001247
Chris Lattner2b037972010-07-29 02:01:43 +00001248 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001249 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001250 if (Size <= 64)
1251 Current = Integer;
1252 else if (Size <= 128)
1253 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001254 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001255 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001256 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001257 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001258 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001259 Current = ComplexX87;
1260
1261 // If this complex type crosses an eightbyte boundary then it
1262 // should be split.
1263 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001264 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001265 if (Hi == NoClass && EB_Real != EB_Imag)
1266 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001267
Chris Lattnerd776fb12010-06-28 21:43:59 +00001268 return;
1269 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001270
Chris Lattner2b037972010-07-29 02:01:43 +00001271 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001272 // Arrays are treated like structures.
1273
Chris Lattner2b037972010-07-29 02:01:43 +00001274 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001275
1276 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001277 // than four eightbytes, ..., it has class MEMORY.
1278 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001279 return;
1280
1281 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1282 // fields, it has class MEMORY.
1283 //
1284 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001285 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001286 return;
1287
1288 // Otherwise implement simplified merge. We could be smarter about
1289 // this, but it isn't worth it and would be harder to verify.
1290 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001291 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001292 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001293
1294 // The only case a 256-bit wide vector could be used is when the array
1295 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1296 // to work for sizes wider than 128, early check and fallback to memory.
1297 if (Size > 128 && EltSize != 256)
1298 return;
1299
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001300 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1301 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001302 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001303 Lo = merge(Lo, FieldLo);
1304 Hi = merge(Hi, FieldHi);
1305 if (Lo == Memory || Hi == Memory)
1306 break;
1307 }
1308
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001309 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001310 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001311 return;
1312 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001313
Chris Lattnerd776fb12010-06-28 21:43:59 +00001314 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001315 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001316
1317 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001318 // than four eightbytes, ..., it has class MEMORY.
1319 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001320 return;
1321
Anders Carlsson20759ad2009-09-16 15:53:40 +00001322 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1323 // copy constructor or a non-trivial destructor, it is passed by invisible
1324 // reference.
1325 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1326 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001327
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001328 const RecordDecl *RD = RT->getDecl();
1329
1330 // Assume variable sized types are passed in memory.
1331 if (RD->hasFlexibleArrayMember())
1332 return;
1333
Chris Lattner2b037972010-07-29 02:01:43 +00001334 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001335
1336 // Reset Lo class, this will be recomputed.
1337 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001338
1339 // If this is a C++ record, classify the bases first.
1340 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1341 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1342 e = CXXRD->bases_end(); i != e; ++i) {
1343 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1344 "Unexpected base class!");
1345 const CXXRecordDecl *Base =
1346 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1347
1348 // Classify this field.
1349 //
1350 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1351 // single eightbyte, each is classified separately. Each eightbyte gets
1352 // initialized to class NO_CLASS.
1353 Class FieldLo, FieldHi;
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001354 uint64_t Offset =
1355 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner22a931e2010-06-29 06:01:59 +00001356 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001357 Lo = merge(Lo, FieldLo);
1358 Hi = merge(Hi, FieldHi);
1359 if (Lo == Memory || Hi == Memory)
1360 break;
1361 }
1362 }
1363
1364 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001365 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001366 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001367 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001368 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1369 bool BitField = i->isBitField();
1370
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001371 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1372 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001373 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001374 // The only case a 256-bit wide vector could be used is when the struct
1375 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1376 // to work for sizes wider than 128, early check and fallback to memory.
1377 //
1378 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1379 Lo = Memory;
1380 return;
1381 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001382 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001383 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001384 Lo = Memory;
1385 return;
1386 }
1387
1388 // Classify this field.
1389 //
1390 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1391 // exceeds a single eightbyte, each is classified
1392 // separately. Each eightbyte gets initialized to class
1393 // NO_CLASS.
1394 Class FieldLo, FieldHi;
1395
1396 // Bit-fields require special handling, they do not force the
1397 // structure to be passed in memory even if unaligned, and
1398 // therefore they can straddle an eightbyte.
1399 if (BitField) {
1400 // Ignore padding bit-fields.
1401 if (i->isUnnamedBitfield())
1402 continue;
1403
1404 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001405 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001406
1407 uint64_t EB_Lo = Offset / 64;
1408 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1409 FieldLo = FieldHi = NoClass;
1410 if (EB_Lo) {
1411 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1412 FieldLo = NoClass;
1413 FieldHi = Integer;
1414 } else {
1415 FieldLo = Integer;
1416 FieldHi = EB_Hi ? Integer : NoClass;
1417 }
1418 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001419 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001420 Lo = merge(Lo, FieldLo);
1421 Hi = merge(Hi, FieldHi);
1422 if (Lo == Memory || Hi == Memory)
1423 break;
1424 }
1425
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001426 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001427 }
1428}
1429
Chris Lattner22a931e2010-06-29 06:01:59 +00001430ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001431 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1432 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001433 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001434 // Treat an enum type as its underlying type.
1435 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1436 Ty = EnumTy->getDecl()->getIntegerType();
1437
1438 return (Ty->isPromotableIntegerType() ?
1439 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1440 }
1441
1442 return ABIArgInfo::getIndirect(0);
1443}
1444
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001445bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1446 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1447 uint64_t Size = getContext().getTypeSize(VecTy);
1448 unsigned LargestVector = HasAVX ? 256 : 128;
1449 if (Size <= 64 || Size > LargestVector)
1450 return true;
1451 }
1452
1453 return false;
1454}
1455
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001456ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1457 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001458 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1459 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001460 //
1461 // This assumption is optimistic, as there could be free registers available
1462 // when we need to pass this argument in memory, and LLVM could try to pass
1463 // the argument in the free register. This does not seem to happen currently,
1464 // but this code would be much safer if we could mark the argument with
1465 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001466 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001467 // Treat an enum type as its underlying type.
1468 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1469 Ty = EnumTy->getDecl()->getIntegerType();
1470
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001471 return (Ty->isPromotableIntegerType() ?
1472 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001473 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001474
Daniel Dunbar53fac692010-04-21 19:49:55 +00001475 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1476 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001477
Chris Lattner44c2b902011-05-22 23:21:23 +00001478 // Compute the byval alignment. We specify the alignment of the byval in all
1479 // cases so that the mid-level optimizer knows the alignment of the byval.
1480 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001481
1482 // Attempt to avoid passing indirect results using byval when possible. This
1483 // is important for good codegen.
1484 //
1485 // We do this by coercing the value into a scalar type which the backend can
1486 // handle naturally (i.e., without using byval).
1487 //
1488 // For simplicity, we currently only do this when we have exhausted all of the
1489 // free integer registers. Doing this when there are free integer registers
1490 // would require more care, as we would have to ensure that the coerced value
1491 // did not claim the unused register. That would require either reording the
1492 // arguments to the function (so that any subsequent inreg values came first),
1493 // or only doing this optimization when there were no following arguments that
1494 // might be inreg.
1495 //
1496 // We currently expect it to be rare (particularly in well written code) for
1497 // arguments to be passed on the stack when there are still free integer
1498 // registers available (this would typically imply large structs being passed
1499 // by value), so this seems like a fair tradeoff for now.
1500 //
1501 // We can revisit this if the backend grows support for 'onstack' parameter
1502 // attributes. See PR12193.
1503 if (freeIntRegs == 0) {
1504 uint64_t Size = getContext().getTypeSize(Ty);
1505
1506 // If this type fits in an eightbyte, coerce it into the matching integral
1507 // type, which will end up on the stack (with alignment 8).
1508 if (Align == 8 && Size <= 64)
1509 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1510 Size));
1511 }
1512
Chris Lattner44c2b902011-05-22 23:21:23 +00001513 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001514}
1515
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001516/// GetByteVectorType - The ABI specifies that a value should be passed in an
1517/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001518/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001519llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001520 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001521
Chris Lattner9fa15c32010-07-29 05:02:29 +00001522 // Wrapper structs that just contain vectors are passed just like vectors,
1523 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001524 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001525 while (STy && STy->getNumElements() == 1) {
1526 IRType = STy->getElementType(0);
1527 STy = dyn_cast<llvm::StructType>(IRType);
1528 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001529
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001530 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001531 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1532 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001533 unsigned BitWidth = VT->getBitWidth();
Tanya Lattner71f1b2d2011-11-28 23:18:11 +00001534 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001535 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1536 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1537 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1538 EltTy->isIntegerTy(128)))
1539 return VT;
1540 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001541
Chris Lattner4200fe42010-07-29 04:56:46 +00001542 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1543}
1544
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001545/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1546/// is known to either be off the end of the specified type or being in
1547/// alignment padding. The user type specified is known to be at most 128 bits
1548/// in size, and have passed through X86_64ABIInfo::classify with a successful
1549/// classification that put one of the two halves in the INTEGER class.
1550///
1551/// It is conservatively correct to return false.
1552static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1553 unsigned EndBit, ASTContext &Context) {
1554 // If the bytes being queried are off the end of the type, there is no user
1555 // data hiding here. This handles analysis of builtins, vectors and other
1556 // types that don't contain interesting padding.
1557 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1558 if (TySize <= StartBit)
1559 return true;
1560
Chris Lattner98076a22010-07-29 07:43:55 +00001561 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1562 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1563 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1564
1565 // Check each element to see if the element overlaps with the queried range.
1566 for (unsigned i = 0; i != NumElts; ++i) {
1567 // If the element is after the span we care about, then we're done..
1568 unsigned EltOffset = i*EltSize;
1569 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001570
Chris Lattner98076a22010-07-29 07:43:55 +00001571 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1572 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1573 EndBit-EltOffset, Context))
1574 return false;
1575 }
1576 // If it overlaps no elements, then it is safe to process as padding.
1577 return true;
1578 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001579
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001580 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1581 const RecordDecl *RD = RT->getDecl();
1582 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001583
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001584 // If this is a C++ record, check the bases first.
1585 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1586 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1587 e = CXXRD->bases_end(); i != e; ++i) {
1588 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1589 "Unexpected base class!");
1590 const CXXRecordDecl *Base =
1591 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001592
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001593 // If the base is after the span we care about, ignore it.
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001594 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001595 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001596
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001597 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1598 if (!BitsContainNoUserData(i->getType(), BaseStart,
1599 EndBit-BaseOffset, Context))
1600 return false;
1601 }
1602 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001603
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001604 // Verify that no field has data that overlaps the region of interest. Yes
1605 // this could be sped up a lot by being smarter about queried fields,
1606 // however we're only looking at structs up to 16 bytes, so we don't care
1607 // much.
1608 unsigned idx = 0;
1609 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1610 i != e; ++i, ++idx) {
1611 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001612
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001613 // If we found a field after the region we care about, then we're done.
1614 if (FieldOffset >= EndBit) break;
1615
1616 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1617 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1618 Context))
1619 return false;
1620 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001621
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001622 // If nothing in this record overlapped the area of interest, then we're
1623 // clean.
1624 return true;
1625 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001626
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001627 return false;
1628}
1629
Chris Lattnere556a712010-07-29 18:39:32 +00001630/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1631/// float member at the specified offset. For example, {int,{float}} has a
1632/// float at offset 4. It is conservatively correct for this routine to return
1633/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00001634static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnere556a712010-07-29 18:39:32 +00001635 const llvm::TargetData &TD) {
1636 // Base case if we find a float.
1637 if (IROffset == 0 && IRType->isFloatTy())
1638 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001639
Chris Lattnere556a712010-07-29 18:39:32 +00001640 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001641 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00001642 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1643 unsigned Elt = SL->getElementContainingOffset(IROffset);
1644 IROffset -= SL->getElementOffset(Elt);
1645 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1646 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001647
Chris Lattnere556a712010-07-29 18:39:32 +00001648 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001649 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1650 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00001651 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1652 IROffset -= IROffset/EltSize*EltSize;
1653 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1654 }
1655
1656 return false;
1657}
1658
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001659
1660/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1661/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001662llvm::Type *X86_64ABIInfo::
1663GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001664 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001665 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001666 // pass as float if the last 4 bytes is just padding. This happens for
1667 // structs that contain 3 floats.
1668 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1669 SourceOffset*8+64, getContext()))
1670 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001671
Chris Lattnere556a712010-07-29 18:39:32 +00001672 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1673 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1674 // case.
1675 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001676 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1677 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001678
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001679 return llvm::Type::getDoubleTy(getVMContext());
1680}
1681
1682
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001683/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1684/// an 8-byte GPR. This means that we either have a scalar or we are talking
1685/// about the high or low part of an up-to-16-byte struct. This routine picks
1686/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001687/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1688/// etc).
1689///
1690/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1691/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1692/// the 8-byte value references. PrefType may be null.
1693///
1694/// SourceTy is the source level type for the entire argument. SourceOffset is
1695/// an offset into this that we're processing (which is always either 0 or 8).
1696///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001697llvm::Type *X86_64ABIInfo::
1698GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001699 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001700 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1701 // returning an 8-byte unit starting with it. See if we can safely use it.
1702 if (IROffset == 0) {
1703 // Pointers and int64's always fill the 8-byte unit.
1704 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1705 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001706
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001707 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1708 // goodness in the source type is just tail padding. This is allowed to
1709 // kick in for struct {double,int} on the int, but not on
1710 // struct{double,int,int} because we wouldn't return the second int. We
1711 // have to do this analysis on the source type because we can't depend on
1712 // unions being lowered a specific way etc.
1713 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1714 IRType->isIntegerTy(32)) {
1715 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001716
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001717 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1718 SourceOffset*8+64, getContext()))
1719 return IRType;
1720 }
1721 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001722
Chris Lattner2192fe52011-07-18 04:24:23 +00001723 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001724 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001725 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001726 if (IROffset < SL->getSizeInBytes()) {
1727 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1728 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001729
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001730 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1731 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001732 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001733 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001734
Chris Lattner2192fe52011-07-18 04:24:23 +00001735 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001736 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001737 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1738 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001739 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1740 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001741 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001742
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001743 // Okay, we don't have any better idea of what to pass, so we pass this in an
1744 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001745 unsigned TySizeInBytes =
1746 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001747
Chris Lattner3f763422010-07-29 17:34:39 +00001748 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001749
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001750 // It is always safe to classify this as an integer type up to i64 that
1751 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001752 return llvm::IntegerType::get(getVMContext(),
1753 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001754}
1755
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001756
1757/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1758/// be used as elements of a two register pair to pass or return, return a
1759/// first class aggregate to represent them. For example, if the low part of
1760/// a by-value argument should be passed as i32* and the high part as float,
1761/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001762static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001763GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001764 const llvm::TargetData &TD) {
1765 // In order to correctly satisfy the ABI, we need to the high part to start
1766 // at offset 8. If the high and low parts we inferred are both 4-byte types
1767 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1768 // the second element at offset 8. Check for this:
1769 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1770 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1771 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1772 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001773
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001774 // To handle this, we have to increase the size of the low part so that the
1775 // second element will start at an 8 byte offset. We can't increase the size
1776 // of the second element because it might make us access off the end of the
1777 // struct.
1778 if (HiStart != 8) {
1779 // There are only two sorts of types the ABI generation code can produce for
1780 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1781 // Promote these to a larger type.
1782 if (Lo->isFloatTy())
1783 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1784 else {
1785 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1786 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1787 }
1788 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001789
Chris Lattnera5f58b02011-07-09 17:41:47 +00001790 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001791
1792
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001793 // Verify that the second element is at an 8-byte offset.
1794 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1795 "Invalid x86-64 argument pair!");
1796 return Result;
1797}
1798
Chris Lattner31faff52010-07-28 23:06:14 +00001799ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001800classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001801 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1802 // classification algorithm.
1803 X86_64ABIInfo::Class Lo, Hi;
1804 classify(RetTy, 0, Lo, Hi);
1805
1806 // Check some invariants.
1807 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001808 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1809
Chris Lattnera5f58b02011-07-09 17:41:47 +00001810 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001811 switch (Lo) {
1812 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001813 if (Hi == NoClass)
1814 return ABIArgInfo::getIgnore();
1815 // If the low part is just padding, it takes no register, leave ResType
1816 // null.
1817 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1818 "Unknown missing lo part");
1819 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001820
1821 case SSEUp:
1822 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001823 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001824
1825 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1826 // hidden argument.
1827 case Memory:
1828 return getIndirectReturnResult(RetTy);
1829
1830 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1831 // available register of the sequence %rax, %rdx is used.
1832 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001833 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001834
Chris Lattner1f3a0632010-07-29 21:42:50 +00001835 // If we have a sign or zero extended integer, make sure to return Extend
1836 // so that the parameter gets the right LLVM IR attributes.
1837 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1838 // Treat an enum type as its underlying type.
1839 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1840 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001841
Chris Lattner1f3a0632010-07-29 21:42:50 +00001842 if (RetTy->isIntegralOrEnumerationType() &&
1843 RetTy->isPromotableIntegerType())
1844 return ABIArgInfo::getExtend();
1845 }
Chris Lattner31faff52010-07-28 23:06:14 +00001846 break;
1847
1848 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1849 // available SSE register of the sequence %xmm0, %xmm1 is used.
1850 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001851 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001852 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001853
1854 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1855 // returned on the X87 stack in %st0 as 80-bit x87 number.
1856 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001857 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001858 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001859
1860 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1861 // part of the value is returned in %st0 and the imaginary part in
1862 // %st1.
1863 case ComplexX87:
1864 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001865 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001866 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001867 NULL);
1868 break;
1869 }
1870
Chris Lattnera5f58b02011-07-09 17:41:47 +00001871 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001872 switch (Hi) {
1873 // Memory was handled previously and X87 should
1874 // never occur as a hi class.
1875 case Memory:
1876 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00001877 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001878
1879 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001880 case NoClass:
1881 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001882
Chris Lattner52b3c132010-09-01 00:20:33 +00001883 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001884 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001885 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1886 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001887 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001888 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001889 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001890 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1891 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001892 break;
1893
1894 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001895 // is passed in the next available eightbyte chunk if the last used
1896 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00001897 //
Chris Lattner57540c52011-04-15 05:22:18 +00001898 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001899 case SSEUp:
1900 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001901 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001902 break;
1903
1904 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1905 // returned together with the previous X87 value in %st0.
1906 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001907 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001908 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001909 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001910 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001911 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001912 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001913 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1914 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001915 }
Chris Lattner31faff52010-07-28 23:06:14 +00001916 break;
1917 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001918
Chris Lattner52b3c132010-09-01 00:20:33 +00001919 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001920 // known to pass in the high eightbyte of the result. We do this by forming a
1921 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001922 if (HighPart)
1923 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001924
Chris Lattner1f3a0632010-07-29 21:42:50 +00001925 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001926}
1927
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001928ABIArgInfo X86_64ABIInfo::classifyArgumentType(
1929 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
1930 const
1931{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001932 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001933 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001934
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001935 // Check some invariants.
1936 // FIXME: Enforce these by construction.
1937 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001938 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1939
1940 neededInt = 0;
1941 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001942 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001943 switch (Lo) {
1944 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001945 if (Hi == NoClass)
1946 return ABIArgInfo::getIgnore();
1947 // If the low part is just padding, it takes no register, leave ResType
1948 // null.
1949 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1950 "Unknown missing lo part");
1951 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001952
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001953 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1954 // on the stack.
1955 case Memory:
1956
1957 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1958 // COMPLEX_X87, it is passed in memory.
1959 case X87:
1960 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001961 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1962 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001963 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001964
1965 case SSEUp:
1966 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001967 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001968
1969 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1970 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1971 // and %r9 is used.
1972 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001973 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001974
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001975 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001976 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001977
1978 // If we have a sign or zero extended integer, make sure to return Extend
1979 // so that the parameter gets the right LLVM IR attributes.
1980 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1981 // Treat an enum type as its underlying type.
1982 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1983 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001984
Chris Lattner1f3a0632010-07-29 21:42:50 +00001985 if (Ty->isIntegralOrEnumerationType() &&
1986 Ty->isPromotableIntegerType())
1987 return ABIArgInfo::getExtend();
1988 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001989
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001990 break;
1991
1992 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1993 // available SSE register is used, the registers are taken in the
1994 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001995 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001996 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001997 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001998 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001999 break;
2000 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00002001 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002002
Chris Lattnera5f58b02011-07-09 17:41:47 +00002003 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002004 switch (Hi) {
2005 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00002006 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002007 // which is passed in memory.
2008 case Memory:
2009 case X87:
2010 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00002011 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002012
2013 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002014
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002015 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002016 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002017 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002018 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002019
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002020 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2021 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002022 break;
2023
2024 // X87Up generally doesn't occur here (long double is passed in
2025 // memory), except in situations involving unions.
2026 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002027 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002028 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002029
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002030 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2031 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002032
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002033 ++neededSSE;
2034 break;
2035
2036 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2037 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002038 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002039 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00002040 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002041 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002042 break;
2043 }
2044
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002045 // If a high part was specified, merge it together with the low part. It is
2046 // known to pass in the high eightbyte of the result. We do this by forming a
2047 // first class struct aggregate with the high and low part: {low, high}
2048 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002049 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002050
Chris Lattner1f3a0632010-07-29 21:42:50 +00002051 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002052}
2053
Chris Lattner22326a12010-07-29 02:31:05 +00002054void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002055
Chris Lattner458b2aa2010-07-29 02:16:43 +00002056 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002057
2058 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002059 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002060
2061 // If the return value is indirect, then the hidden argument is consuming one
2062 // integer register.
2063 if (FI.getReturnInfo().isIndirect())
2064 --freeIntRegs;
2065
2066 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2067 // get assigned (in left-to-right order) for passing as follows...
2068 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2069 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00002070 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002071 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2072 neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002073
2074 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2075 // eightbyte of an argument, the whole argument is passed on the
2076 // stack. If registers have already been assigned for some
2077 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002078 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002079 freeIntRegs -= neededInt;
2080 freeSSERegs -= neededSSE;
2081 } else {
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002082 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002083 }
2084 }
2085}
2086
2087static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2088 QualType Ty,
2089 CodeGenFunction &CGF) {
2090 llvm::Value *overflow_arg_area_p =
2091 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2092 llvm::Value *overflow_arg_area =
2093 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2094
2095 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2096 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00002097 // It isn't stated explicitly in the standard, but in practice we use
2098 // alignment greater than 16 where necessary.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002099 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2100 if (Align > 8) {
Eli Friedmana1748562011-11-18 02:44:19 +00002101 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson41a75022009-08-13 21:57:51 +00002102 llvm::Value *Offset =
Eli Friedmana1748562011-11-18 02:44:19 +00002103 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002104 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2105 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002106 CGF.Int64Ty);
Eli Friedmana1748562011-11-18 02:44:19 +00002107 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002108 overflow_arg_area =
2109 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2110 overflow_arg_area->getType(),
2111 "overflow_arg_area.align");
2112 }
2113
2114 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00002115 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002116 llvm::Value *Res =
2117 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002118 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002119
2120 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2121 // l->overflow_arg_area + sizeof(type).
2122 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2123 // an 8 byte boundary.
2124
2125 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00002126 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002127 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002128 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2129 "overflow_arg_area.next");
2130 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2131
2132 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2133 return Res;
2134}
2135
2136llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2137 CodeGenFunction &CGF) const {
2138 // Assume that va_list type is correct; should be pointer to LLVM type:
2139 // struct {
2140 // i32 gp_offset;
2141 // i32 fp_offset;
2142 // i8* overflow_arg_area;
2143 // i8* reg_save_area;
2144 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002145 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002146
Chris Lattner9723d6c2010-03-11 18:19:55 +00002147 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002148 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002149
2150 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2151 // in the registers. If not go to step 7.
2152 if (!neededInt && !neededSSE)
2153 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2154
2155 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2156 // general purpose registers needed to pass type and num_fp to hold
2157 // the number of floating point registers needed.
2158
2159 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2160 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2161 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2162 //
2163 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2164 // register save space).
2165
2166 llvm::Value *InRegs = 0;
2167 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2168 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2169 if (neededInt) {
2170 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2171 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002172 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2173 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002174 }
2175
2176 if (neededSSE) {
2177 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2178 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2179 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002180 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2181 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002182 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2183 }
2184
2185 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2186 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2187 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2188 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2189
2190 // Emit code to load the value if it was passed in registers.
2191
2192 CGF.EmitBlock(InRegBlock);
2193
2194 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2195 // an offset of l->gp_offset and/or l->fp_offset. This may require
2196 // copying to a temporary location in case the parameter is passed
2197 // in different register classes or requires an alignment greater
2198 // than 8 for general purpose registers and 16 for XMM registers.
2199 //
2200 // FIXME: This really results in shameful code when we end up needing to
2201 // collect arguments from different places; often what should result in a
2202 // simple assembling of a structure from scattered addresses has many more
2203 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002204 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002205 llvm::Value *RegAddr =
2206 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2207 "reg_save_area");
2208 if (neededInt && neededSSE) {
2209 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002210 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002211 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002212 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2213 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002214 llvm::Type *TyLo = ST->getElementType(0);
2215 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002216 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002217 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002218 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2219 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002220 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2221 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002222 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2223 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002224 llvm::Value *V =
2225 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2226 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2227 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2228 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2229
Owen Anderson170229f2009-07-14 23:10:40 +00002230 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002231 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002232 } else if (neededInt) {
2233 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2234 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002235 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002236 } else if (neededSSE == 1) {
2237 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2238 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2239 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002240 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002241 assert(neededSSE == 2 && "Invalid number of needed registers!");
2242 // SSE registers are spaced 16 bytes apart in the register save
2243 // area, we need to collect the two eightbytes together.
2244 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002245 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerece04092012-02-07 00:39:47 +00002246 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2192fe52011-07-18 04:24:23 +00002247 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002248 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2192fe52011-07-18 04:24:23 +00002249 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002250 DoubleTy, NULL);
2251 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2252 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2253 DblPtrTy));
2254 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2255 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2256 DblPtrTy));
2257 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2258 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2259 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002260 }
2261
2262 // AMD64-ABI 3.5.7p5: Step 5. Set:
2263 // l->gp_offset = l->gp_offset + num_gp * 8
2264 // l->fp_offset = l->fp_offset + num_fp * 16.
2265 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002266 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002267 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2268 gp_offset_p);
2269 }
2270 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002271 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002272 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2273 fp_offset_p);
2274 }
2275 CGF.EmitBranch(ContBlock);
2276
2277 // Emit code to load the value if it was passed in memory.
2278
2279 CGF.EmitBlock(InMemBlock);
2280 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2281
2282 // Return the appropriate result.
2283
2284 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002285 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002286 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002287 ResAddr->addIncoming(RegAddr, InRegBlock);
2288 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002289 return ResAddr;
2290}
2291
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002292ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2293
2294 if (Ty->isVoidType())
2295 return ABIArgInfo::getIgnore();
2296
2297 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2298 Ty = EnumTy->getDecl()->getIntegerType();
2299
2300 uint64_t Size = getContext().getTypeSize(Ty);
2301
2302 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002303 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2304 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002305 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2306
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002307 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2308 if (Size == 128 &&
Eli Friedmana98d1f82012-01-25 22:46:34 +00002309 getContext().getTargetInfo().getTriple().getOS()
2310 == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002311 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2312 Size));
2313
2314 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2315 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2316 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002317 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002318 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2319 Size));
2320
2321 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2322 }
2323
2324 if (Ty->isPromotableIntegerType())
2325 return ABIArgInfo::getExtend();
2326
2327 return ABIArgInfo::getDirect();
2328}
2329
2330void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2331
2332 QualType RetTy = FI.getReturnType();
2333 FI.getReturnInfo() = classify(RetTy);
2334
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002335 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2336 it != ie; ++it)
2337 it->info = classify(it->type);
2338}
2339
Chris Lattner04dc9572010-08-31 16:44:54 +00002340llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2341 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002342 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattner0cf24192010-06-28 20:05:43 +00002343
Chris Lattner04dc9572010-08-31 16:44:54 +00002344 CGBuilderTy &Builder = CGF.Builder;
2345 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2346 "ap");
2347 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2348 llvm::Type *PTy =
2349 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2350 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2351
2352 uint64_t Offset =
2353 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2354 llvm::Value *NextAddr =
2355 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2356 "ap.next");
2357 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2358
2359 return AddrTyped;
2360}
Chris Lattner0cf24192010-06-28 20:05:43 +00002361
John McCallea8d8bb2010-03-11 00:10:12 +00002362// PowerPC-32
2363
2364namespace {
2365class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2366public:
Chris Lattner2b037972010-07-29 02:01:43 +00002367 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002368
John McCallea8d8bb2010-03-11 00:10:12 +00002369 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2370 // This is recovered from gcc output.
2371 return 1; // r1 is the dedicated stack pointer
2372 }
2373
2374 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002375 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002376};
2377
2378}
2379
2380bool
2381PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2382 llvm::Value *Address) const {
2383 // This is calculated from the LLVM and GCC tables and verified
2384 // against gcc output. AFAIK all ABIs use the same encoding.
2385
2386 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00002387
Chris Lattnerece04092012-02-07 00:39:47 +00002388 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00002389 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2390 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2391 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2392
2393 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002394 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002395
2396 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002397 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002398
2399 // 64-76 are various 4-byte special-purpose registers:
2400 // 64: mq
2401 // 65: lr
2402 // 66: ctr
2403 // 67: ap
2404 // 68-75 cr0-7
2405 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002406 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002407
2408 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002409 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002410
2411 // 109: vrsave
2412 // 110: vscr
2413 // 111: spe_acc
2414 // 112: spefscr
2415 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002416 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002417
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002418 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002419}
2420
Roman Divackyd966e722012-05-09 18:22:46 +00002421// PowerPC-64
2422
2423namespace {
2424class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2425public:
2426 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2427
2428 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2429 // This is recovered from gcc output.
2430 return 1; // r1 is the dedicated stack pointer
2431 }
2432
2433 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2434 llvm::Value *Address) const;
2435};
2436
2437}
2438
2439bool
2440PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2441 llvm::Value *Address) const {
2442 // This is calculated from the LLVM and GCC tables and verified
2443 // against gcc output. AFAIK all ABIs use the same encoding.
2444
2445 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2446
2447 llvm::IntegerType *i8 = CGF.Int8Ty;
2448 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2449 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2450 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2451
2452 // 0-31: r0-31, the 8-byte general-purpose registers
2453 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2454
2455 // 32-63: fp0-31, the 8-byte floating-point registers
2456 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2457
2458 // 64-76 are various 4-byte special-purpose registers:
2459 // 64: mq
2460 // 65: lr
2461 // 66: ctr
2462 // 67: ap
2463 // 68-75 cr0-7
2464 // 76: xer
2465 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2466
2467 // 77-108: v0-31, the 16-byte vector registers
2468 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2469
2470 // 109: vrsave
2471 // 110: vscr
2472 // 111: spe_acc
2473 // 112: spefscr
2474 // 113: sfp
2475 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2476
2477 return false;
2478}
John McCallea8d8bb2010-03-11 00:10:12 +00002479
Chris Lattner0cf24192010-06-28 20:05:43 +00002480//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002481// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002482//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002483
2484namespace {
2485
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002486class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002487public:
2488 enum ABIKind {
2489 APCS = 0,
2490 AAPCS = 1,
2491 AAPCS_VFP
2492 };
2493
2494private:
2495 ABIKind Kind;
2496
2497public:
Chris Lattner2b037972010-07-29 02:01:43 +00002498 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002499
John McCall3480ef22011-08-30 01:42:09 +00002500 bool isEABI() const {
Eli Friedmana98d1f82012-01-25 22:46:34 +00002501 StringRef Env =
2502 getContext().getTargetInfo().getTriple().getEnvironmentName();
Chandler Carruthc89aa9d2012-01-10 19:47:42 +00002503 return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
John McCall3480ef22011-08-30 01:42:09 +00002504 }
2505
Daniel Dunbar020daa92009-09-12 01:00:39 +00002506private:
2507 ABIKind getABIKind() const { return Kind; }
2508
Chris Lattner458b2aa2010-07-29 02:16:43 +00002509 ABIArgInfo classifyReturnType(QualType RetTy) const;
2510 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002511
Chris Lattner22326a12010-07-29 02:31:05 +00002512 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002513
2514 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2515 CodeGenFunction &CGF) const;
2516};
2517
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002518class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2519public:
Chris Lattner2b037972010-07-29 02:01:43 +00002520 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2521 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002522
John McCall3480ef22011-08-30 01:42:09 +00002523 const ARMABIInfo &getABIInfo() const {
2524 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2525 }
2526
John McCallbeec5a02010-03-06 00:35:14 +00002527 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2528 return 13;
2529 }
Roman Divackyc1617352011-05-18 19:36:54 +00002530
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002531 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00002532 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2533 }
2534
Roman Divackyc1617352011-05-18 19:36:54 +00002535 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2536 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002537 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00002538
2539 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00002540 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00002541 return false;
2542 }
John McCall3480ef22011-08-30 01:42:09 +00002543
2544 unsigned getSizeOfUnwindException() const {
2545 if (getABIInfo().isEABI()) return 88;
2546 return TargetCodeGenInfo::getSizeOfUnwindException();
2547 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002548};
2549
Daniel Dunbard59655c2009-09-12 00:59:49 +00002550}
2551
Chris Lattner22326a12010-07-29 02:31:05 +00002552void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002553 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002554 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002555 it != ie; ++it)
2556 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002557
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002558 // Always honor user-specified calling convention.
2559 if (FI.getCallingConvention() != llvm::CallingConv::C)
2560 return;
2561
2562 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002563 llvm::CallingConv::ID DefaultCC;
John McCall3480ef22011-08-30 01:42:09 +00002564 if (isEABI())
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002565 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002566 else
2567 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002568
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002569 // If user did not ask for specific calling convention explicitly (e.g. via
2570 // pcs attribute), set effective calling convention if it's different than ABI
2571 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002572 switch (getABIKind()) {
2573 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002574 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2575 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002576 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002577 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002578 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2579 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002580 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002581 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002582 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2583 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002584 break;
2585 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002586}
2587
Bob Wilsone826a2a2011-08-03 05:58:22 +00002588/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2589/// aggregate. If HAMembers is non-null, the number of base elements
2590/// contained in the type is returned through it; this is used for the
2591/// recursive calls that check aggregate component types.
2592static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2593 ASTContext &Context,
2594 uint64_t *HAMembers = 0) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002595 uint64_t Members = 0;
Bob Wilsone826a2a2011-08-03 05:58:22 +00002596 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2597 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2598 return false;
2599 Members *= AT->getSize().getZExtValue();
2600 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2601 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002602 if (RD->hasFlexibleArrayMember())
Bob Wilsone826a2a2011-08-03 05:58:22 +00002603 return false;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002604
Bob Wilsone826a2a2011-08-03 05:58:22 +00002605 Members = 0;
2606 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2607 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00002608 const FieldDecl *FD = *i;
Bob Wilsone826a2a2011-08-03 05:58:22 +00002609 uint64_t FldMembers;
2610 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2611 return false;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002612
2613 Members = (RD->isUnion() ?
2614 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilsone826a2a2011-08-03 05:58:22 +00002615 }
2616 } else {
2617 Members = 1;
2618 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2619 Members = 2;
2620 Ty = CT->getElementType();
2621 }
2622
2623 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2624 // double, or 64-bit or 128-bit vectors.
2625 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2626 if (BT->getKind() != BuiltinType::Float &&
Tim Northovereb752d42012-07-20 22:29:29 +00002627 BT->getKind() != BuiltinType::Double &&
2628 BT->getKind() != BuiltinType::LongDouble)
Bob Wilsone826a2a2011-08-03 05:58:22 +00002629 return false;
2630 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2631 unsigned VecSize = Context.getTypeSize(VT);
2632 if (VecSize != 64 && VecSize != 128)
2633 return false;
2634 } else {
2635 return false;
2636 }
2637
2638 // The base type must be the same for all members. Vector types of the
2639 // same total size are treated as being equivalent here.
2640 const Type *TyPtr = Ty.getTypePtr();
2641 if (!Base)
2642 Base = TyPtr;
2643 if (Base != TyPtr &&
2644 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2645 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2646 return false;
2647 }
2648
2649 // Homogeneous Aggregates can have at most 4 members of the base type.
2650 if (HAMembers)
2651 *HAMembers = Members;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002652
2653 return (Members > 0 && Members <= 4);
Bob Wilsone826a2a2011-08-03 05:58:22 +00002654}
2655
Chris Lattner458b2aa2010-07-29 02:16:43 +00002656ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002657 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002658 // Treat an enum type as its underlying type.
2659 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2660 Ty = EnumTy->getDecl()->getIntegerType();
2661
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002662 return (Ty->isPromotableIntegerType() ?
2663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002664 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002665
Daniel Dunbar09d33622009-09-14 21:54:03 +00002666 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002667 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002668 return ABIArgInfo::getIgnore();
2669
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002670 // Structures with either a non-trivial destructor or a non-trivial
2671 // copy constructor are always indirect.
2672 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2673 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2674
Bob Wilsone826a2a2011-08-03 05:58:22 +00002675 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2676 // Homogeneous Aggregates need to be expanded.
2677 const Type *Base = 0;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002678 if (isHomogeneousAggregate(Ty, Base, getContext())) {
2679 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilsone826a2a2011-08-03 05:58:22 +00002680 return ABIArgInfo::getExpand();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002681 }
Bob Wilsone826a2a2011-08-03 05:58:22 +00002682 }
2683
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002684 // Otherwise, pass by coercing to a structure of the appropriate size.
2685 //
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002686 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2192fe52011-07-18 04:24:23 +00002687 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002688 unsigned SizeRegs;
Manman Ren6fdb1582012-06-25 22:04:00 +00002689 if (getContext().getTypeSizeInChars(Ty) <= CharUnits::fromQuantity(64)) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002690 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2691 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00002692 } else if (getABIKind() == ARMABIInfo::APCS) {
2693 // Initial ARM ByVal support is APCS-only.
2694 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2695 } else {
2696 // FIXME: This is kind of nasty... but there isn't much choice
2697 // because most of the ARM calling conventions don't yet support
2698 // byval.
2699 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2700 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002701 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002702
Chris Lattnera5f58b02011-07-09 17:41:47 +00002703 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002704 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002705 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002706}
2707
Chris Lattner458b2aa2010-07-29 02:16:43 +00002708static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002709 llvm::LLVMContext &VMContext) {
2710 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2711 // is called integer-like if its size is less than or equal to one word, and
2712 // the offset of each of its addressable sub-fields is zero.
2713
2714 uint64_t Size = Context.getTypeSize(Ty);
2715
2716 // Check that the type fits in a word.
2717 if (Size > 32)
2718 return false;
2719
2720 // FIXME: Handle vector types!
2721 if (Ty->isVectorType())
2722 return false;
2723
Daniel Dunbard53bac72009-09-14 02:20:34 +00002724 // Float types are never treated as "integer like".
2725 if (Ty->isRealFloatingType())
2726 return false;
2727
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002728 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002729 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002730 return true;
2731
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002732 // Small complex integer types are "integer like".
2733 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2734 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002735
2736 // Single element and zero sized arrays should be allowed, by the definition
2737 // above, but they are not.
2738
2739 // Otherwise, it must be a record type.
2740 const RecordType *RT = Ty->getAs<RecordType>();
2741 if (!RT) return false;
2742
2743 // Ignore records with flexible arrays.
2744 const RecordDecl *RD = RT->getDecl();
2745 if (RD->hasFlexibleArrayMember())
2746 return false;
2747
2748 // Check that all sub-fields are at offset 0, and are themselves "integer
2749 // like".
2750 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2751
2752 bool HadField = false;
2753 unsigned idx = 0;
2754 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2755 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00002756 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002757
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002758 // Bit-fields are not addressable, we only need to verify they are "integer
2759 // like". We still have to disallow a subsequent non-bitfield, for example:
2760 // struct { int : 0; int x }
2761 // is non-integer like according to gcc.
2762 if (FD->isBitField()) {
2763 if (!RD->isUnion())
2764 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002765
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002766 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2767 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002768
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002769 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002770 }
2771
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002772 // Check if this field is at offset 0.
2773 if (Layout.getFieldOffset(idx) != 0)
2774 return false;
2775
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002776 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2777 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002778
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002779 // Only allow at most one field in a structure. This doesn't match the
2780 // wording above, but follows gcc in situations with a field following an
2781 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002782 if (!RD->isUnion()) {
2783 if (HadField)
2784 return false;
2785
2786 HadField = true;
2787 }
2788 }
2789
2790 return true;
2791}
2792
Chris Lattner458b2aa2010-07-29 02:16:43 +00002793ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002794 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002795 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002796
Daniel Dunbar19964db2010-09-23 01:54:32 +00002797 // Large vector types should be returned via memory.
2798 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2799 return ABIArgInfo::getIndirect(0);
2800
John McCalla1dee5302010-08-22 10:59:02 +00002801 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002802 // Treat an enum type as its underlying type.
2803 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2804 RetTy = EnumTy->getDecl()->getIntegerType();
2805
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002806 return (RetTy->isPromotableIntegerType() ?
2807 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002808 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002809
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002810 // Structures with either a non-trivial destructor or a non-trivial
2811 // copy constructor are always indirect.
2812 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2813 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2814
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002815 // Are we following APCS?
2816 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002817 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002818 return ABIArgInfo::getIgnore();
2819
Daniel Dunbareedf1512010-02-01 23:31:19 +00002820 // Complex types are all returned as packed integers.
2821 //
2822 // FIXME: Consider using 2 x vector types if the back end handles them
2823 // correctly.
2824 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002825 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002826 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002827
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002828 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002829 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002830 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002831 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002832 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002833 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002834 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002835 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2836 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002837 }
2838
2839 // Otherwise return in memory.
2840 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002841 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002842
2843 // Otherwise this is an AAPCS variant.
2844
Chris Lattner458b2aa2010-07-29 02:16:43 +00002845 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002846 return ABIArgInfo::getIgnore();
2847
Bob Wilson1d9269a2011-11-02 04:51:36 +00002848 // Check for homogeneous aggregates with AAPCS-VFP.
2849 if (getABIKind() == AAPCS_VFP) {
2850 const Type *Base = 0;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002851 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
2852 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00002853 // Homogeneous Aggregates are returned directly.
2854 return ABIArgInfo::getDirect();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002855 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00002856 }
2857
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002858 // Aggregates <= 4 bytes are returned in r0; other aggregates
2859 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002860 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002861 if (Size <= 32) {
2862 // Return in the smallest viable integer type.
2863 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002864 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002865 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002866 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2867 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002868 }
2869
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002870 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002871}
2872
2873llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002874 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002875 llvm::Type *BP = CGF.Int8PtrTy;
2876 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002877
2878 CGBuilderTy &Builder = CGF.Builder;
Chris Lattnerece04092012-02-07 00:39:47 +00002879 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002880 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindola11d994b2011-08-02 22:33:37 +00002881 // Handle address alignment for type alignment > 32 bits
2882 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2883 if (TyAlign > 4) {
2884 assert((TyAlign & (TyAlign - 1)) == 0 &&
2885 "Alignment is not power of 2!");
2886 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2887 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2888 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2889 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2890 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002891 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002892 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002893 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2894
2895 uint64_t Offset =
2896 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2897 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002898 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002899 "ap.next");
2900 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2901
2902 return AddrTyped;
2903}
2904
Chris Lattner0cf24192010-06-28 20:05:43 +00002905//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00002906// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002907//===----------------------------------------------------------------------===//
2908
2909namespace {
2910
Justin Holewinski83e96682012-05-24 17:43:12 +00002911class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002912public:
Justin Holewinski83e96682012-05-24 17:43:12 +00002913 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002914
2915 ABIArgInfo classifyReturnType(QualType RetTy) const;
2916 ABIArgInfo classifyArgumentType(QualType Ty) const;
2917
2918 virtual void computeInfo(CGFunctionInfo &FI) const;
2919 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2920 CodeGenFunction &CFG) const;
2921};
2922
Justin Holewinski83e96682012-05-24 17:43:12 +00002923class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002924public:
Justin Holewinski83e96682012-05-24 17:43:12 +00002925 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
2926 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00002927
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00002928 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2929 CodeGen::CodeGenModule &M) const;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002930};
2931
Justin Holewinski83e96682012-05-24 17:43:12 +00002932ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002933 if (RetTy->isVoidType())
2934 return ABIArgInfo::getIgnore();
2935 if (isAggregateTypeForABI(RetTy))
2936 return ABIArgInfo::getIndirect(0);
2937 return ABIArgInfo::getDirect();
2938}
2939
Justin Holewinski83e96682012-05-24 17:43:12 +00002940ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002941 if (isAggregateTypeForABI(Ty))
2942 return ABIArgInfo::getIndirect(0);
2943
2944 return ABIArgInfo::getDirect();
2945}
2946
Justin Holewinski83e96682012-05-24 17:43:12 +00002947void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002948 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2949 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2950 it != ie; ++it)
2951 it->info = classifyArgumentType(it->type);
2952
2953 // Always honor user-specified calling convention.
2954 if (FI.getCallingConvention() != llvm::CallingConv::C)
2955 return;
2956
2957 // Calling convention as default by an ABI.
Justin Holewinski83e96682012-05-24 17:43:12 +00002958 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
2959 // but we should switch to NVVM metadata later on.
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002960 llvm::CallingConv::ID DefaultCC;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002961 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002962 if (LangOpts.OpenCL || LangOpts.CUDA) {
2963 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002964 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski38031972011-10-05 17:58:44 +00002965 } else {
2966 // If we are in standard C/C++ mode, use the triple to decide on the default
2967 StringRef Env =
2968 getContext().getTargetInfo().getTriple().getEnvironmentName();
2969 if (Env == "device")
2970 DefaultCC = llvm::CallingConv::PTX_Device;
2971 else
2972 DefaultCC = llvm::CallingConv::PTX_Kernel;
2973 }
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002974 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski38031972011-10-05 17:58:44 +00002975
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002976}
2977
Justin Holewinski83e96682012-05-24 17:43:12 +00002978llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2979 CodeGenFunction &CFG) const {
2980 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002981}
2982
Justin Holewinski83e96682012-05-24 17:43:12 +00002983void NVPTXTargetCodeGenInfo::
2984SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2985 CodeGen::CodeGenModule &M) const{
Justin Holewinski38031972011-10-05 17:58:44 +00002986 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2987 if (!FD) return;
2988
2989 llvm::Function *F = cast<llvm::Function>(GV);
2990
2991 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00002992 if (M.getLangOpts().OpenCL) {
Justin Holewinski38031972011-10-05 17:58:44 +00002993 // Use OpenCL function attributes to set proper calling conventions
2994 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00002995 if (FD->hasAttr<OpenCLKernelAttr>()) {
2996 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002997 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002998 // And kernel functions are not subject to inlining
2999 F->addFnAttr(llvm::Attribute::NoInline);
3000 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00003001 }
Justin Holewinski38031972011-10-05 17:58:44 +00003002
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00003003 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003004 if (M.getLangOpts().CUDA) {
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00003005 // CUDA __global__ functions get a kernel calling convention. Since
3006 // __global__ functions cannot be called from the device, we do not
3007 // need to set the noinline attribute.
3008 if (FD->getAttr<CUDAGlobalAttr>())
3009 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00003010 }
3011}
3012
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003013}
3014
3015//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00003016// MBlaze ABI Implementation
3017//===----------------------------------------------------------------------===//
3018
3019namespace {
3020
3021class MBlazeABIInfo : public ABIInfo {
3022public:
3023 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3024
3025 bool isPromotableIntegerType(QualType Ty) const;
3026
3027 ABIArgInfo classifyReturnType(QualType RetTy) const;
3028 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3029
3030 virtual void computeInfo(CGFunctionInfo &FI) const {
3031 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3032 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3033 it != ie; ++it)
3034 it->info = classifyArgumentType(it->type);
3035 }
3036
3037 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3038 CodeGenFunction &CGF) const;
3039};
3040
3041class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3042public:
3043 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3044 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3045 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3046 CodeGen::CodeGenModule &M) const;
3047};
3048
3049}
3050
3051bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3052 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3053 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3054 switch (BT->getKind()) {
3055 case BuiltinType::Bool:
3056 case BuiltinType::Char_S:
3057 case BuiltinType::Char_U:
3058 case BuiltinType::SChar:
3059 case BuiltinType::UChar:
3060 case BuiltinType::Short:
3061 case BuiltinType::UShort:
3062 return true;
3063 default:
3064 return false;
3065 }
3066 return false;
3067}
3068
3069llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3070 CodeGenFunction &CGF) const {
3071 // FIXME: Implement
3072 return 0;
3073}
3074
3075
3076ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3077 if (RetTy->isVoidType())
3078 return ABIArgInfo::getIgnore();
3079 if (isAggregateTypeForABI(RetTy))
3080 return ABIArgInfo::getIndirect(0);
3081
3082 return (isPromotableIntegerType(RetTy) ?
3083 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3084}
3085
3086ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3087 if (isAggregateTypeForABI(Ty))
3088 return ABIArgInfo::getIndirect(0);
3089
3090 return (isPromotableIntegerType(Ty) ?
3091 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3092}
3093
3094void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3095 llvm::GlobalValue *GV,
3096 CodeGen::CodeGenModule &M)
3097 const {
3098 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3099 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00003100
Wesley Peck36a1f682010-12-19 19:57:51 +00003101 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3102 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3103 CC = llvm::CallingConv::MBLAZE_INTR;
3104 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3105 CC = llvm::CallingConv::MBLAZE_SVOL;
3106
3107 if (CC != llvm::CallingConv::C) {
3108 // Handle 'interrupt_handler' attribute:
3109 llvm::Function *F = cast<llvm::Function>(GV);
3110
3111 // Step 1: Set ISR calling convention.
3112 F->setCallingConv(CC);
3113
3114 // Step 2: Add attributes goodness.
3115 F->addFnAttr(llvm::Attribute::NoInline);
3116 }
3117
3118 // Step 3: Emit _interrupt_handler alias.
3119 if (CC == llvm::CallingConv::MBLAZE_INTR)
3120 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3121 "_interrupt_handler", GV, &M.getModule());
3122}
3123
3124
3125//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003126// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00003127//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003128
3129namespace {
3130
3131class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3132public:
Chris Lattner2b037972010-07-29 02:01:43 +00003133 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3134 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003135 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3136 CodeGen::CodeGenModule &M) const;
3137};
3138
3139}
3140
3141void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3142 llvm::GlobalValue *GV,
3143 CodeGen::CodeGenModule &M) const {
3144 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3145 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3146 // Handle 'interrupt' attribute:
3147 llvm::Function *F = cast<llvm::Function>(GV);
3148
3149 // Step 1: Set ISR calling convention.
3150 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3151
3152 // Step 2: Add attributes goodness.
3153 F->addFnAttr(llvm::Attribute::NoInline);
3154
3155 // Step 3: Emit ISR vector alias.
3156 unsigned Num = attr->getNumber() + 0xffe0;
3157 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003158 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003159 GV, &M.getModule());
3160 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003161 }
3162}
3163
Chris Lattner0cf24192010-06-28 20:05:43 +00003164//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00003165// MIPS ABI Implementation. This works for both little-endian and
3166// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00003167//===----------------------------------------------------------------------===//
3168
John McCall943fae92010-05-27 06:19:26 +00003169namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00003170class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00003171 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003172 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3173 void CoerceToIntArgs(uint64_t TySize,
3174 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003175 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003176 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00003177 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003178public:
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00003179 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003180 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3181 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00003182
3183 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003184 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003185 virtual void computeInfo(CGFunctionInfo &FI) const;
3186 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3187 CodeGenFunction &CGF) const;
3188};
3189
John McCall943fae92010-05-27 06:19:26 +00003190class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003191 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00003192public:
Akira Hatanaka14378522011-11-02 23:14:57 +00003193 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3194 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3195 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00003196
3197 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3198 return 29;
3199 }
3200
3201 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003202 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00003203
3204 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003205 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00003206 }
John McCall943fae92010-05-27 06:19:26 +00003207};
3208}
3209
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003210void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3211 SmallVector<llvm::Type*, 8> &ArgList) const {
3212 llvm::IntegerType *IntTy =
3213 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003214
3215 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3216 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3217 ArgList.push_back(IntTy);
3218
3219 // If necessary, add one more integer type to ArgList.
3220 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3221
3222 if (R)
3223 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003224}
3225
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003226// In N32/64, an aligned double precision floating point field is passed in
3227// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003228llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003229 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3230
3231 if (IsO32) {
3232 CoerceToIntArgs(TySize, ArgList);
3233 return llvm::StructType::get(getVMContext(), ArgList);
3234 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003235
Akira Hatanaka02e13e52012-01-12 00:52:17 +00003236 if (Ty->isComplexType())
3237 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00003238
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00003239 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003240
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003241 // Unions/vectors are passed in integer registers.
3242 if (!RT || !RT->isStructureOrClassType()) {
3243 CoerceToIntArgs(TySize, ArgList);
3244 return llvm::StructType::get(getVMContext(), ArgList);
3245 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003246
3247 const RecordDecl *RD = RT->getDecl();
3248 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003249 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003250
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003251 uint64_t LastOffset = 0;
3252 unsigned idx = 0;
3253 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3254
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00003255 // Iterate over fields in the struct/class and check if there are any aligned
3256 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003257 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3258 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00003259 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003260 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3261
3262 if (!BT || BT->getKind() != BuiltinType::Double)
3263 continue;
3264
3265 uint64_t Offset = Layout.getFieldOffset(idx);
3266 if (Offset % 64) // Ignore doubles that are not aligned.
3267 continue;
3268
3269 // Add ((Offset - LastOffset) / 64) args of type i64.
3270 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3271 ArgList.push_back(I64);
3272
3273 // Add double type.
3274 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3275 LastOffset = Offset + 64;
3276 }
3277
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003278 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3279 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003280
3281 return llvm::StructType::get(getVMContext(), ArgList);
3282}
3283
Akira Hatanaka1632af62012-01-09 19:31:25 +00003284llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003285 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanaka1632af62012-01-09 19:31:25 +00003286
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003287 if ((Align - 1) & Offset)
3288 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3289
3290 return 0;
Akira Hatanaka1632af62012-01-09 19:31:25 +00003291}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00003292
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003293ABIArgInfo
3294MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanaka1632af62012-01-09 19:31:25 +00003295 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003296 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00003297 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003298
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003299 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3300 (uint64_t)StackAlignInBytes);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003301 Offset = llvm::RoundUpToAlignment(Offset, Align);
3302 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00003303
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003304 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00003305 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003306 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00003307 return ABIArgInfo::getIgnore();
3308
Akira Hatanakadf425db2011-08-01 18:09:58 +00003309 // Records with non trivial destructors/constructors should not be passed
3310 // by value.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003311 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003312 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanakadf425db2011-08-01 18:09:58 +00003313 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003314 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00003315
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003316 // If we have reached here, aggregates are passed directly by coercing to
3317 // another structure type. Padding is inserted if the offset of the
3318 // aggregate is unaligned.
3319 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3320 getPaddingType(Align, OrigOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00003321 }
3322
3323 // Treat an enum type as its underlying type.
3324 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3325 Ty = EnumTy->getDecl()->getIntegerType();
3326
Akira Hatanaka1632af62012-01-09 19:31:25 +00003327 if (Ty->isPromotableIntegerType())
3328 return ABIArgInfo::getExtend();
3329
3330 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00003331}
3332
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003333llvm::Type*
3334MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00003335 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003336 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003337
Akira Hatanakab6f74432012-02-09 18:49:26 +00003338 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003339 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00003340 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3341 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003342
Akira Hatanakab6f74432012-02-09 18:49:26 +00003343 // N32/64 returns struct/classes in floating point registers if the
3344 // following conditions are met:
3345 // 1. The size of the struct/class is no larger than 128-bit.
3346 // 2. The struct/class has one or two fields all of which are floating
3347 // point types.
3348 // 3. The offset of the first field is zero (this follows what gcc does).
3349 //
3350 // Any other composite results are returned in integer registers.
3351 //
3352 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3353 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3354 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00003355 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003356
Akira Hatanakab6f74432012-02-09 18:49:26 +00003357 if (!BT || !BT->isFloatingPoint())
3358 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003359
David Blaikie2d7c57e2012-04-30 02:36:29 +00003360 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00003361 }
3362
3363 if (b == e)
3364 return llvm::StructType::get(getVMContext(), RTList,
3365 RD->hasAttr<PackedAttr>());
3366
3367 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003368 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003369 }
3370
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003371 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003372 return llvm::StructType::get(getVMContext(), RTList);
3373}
3374
Akira Hatanakab579fe52011-06-02 00:09:17 +00003375ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00003376 uint64_t Size = getContext().getTypeSize(RetTy);
3377
3378 if (RetTy->isVoidType() || Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00003379 return ABIArgInfo::getIgnore();
3380
Akira Hatanakac37eddf2012-05-11 21:01:17 +00003381 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003382 if (Size <= 128) {
3383 if (RetTy->isAnyComplexType())
3384 return ABIArgInfo::getDirect();
3385
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003386 // O32 returns integer vectors in registers.
3387 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3388 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3389
Akira Hatanakac07c4652012-02-08 01:31:22 +00003390 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003391 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3392 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00003393
3394 return ABIArgInfo::getIndirect(0);
3395 }
3396
3397 // Treat an enum type as its underlying type.
3398 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3399 RetTy = EnumTy->getDecl()->getIntegerType();
3400
3401 return (RetTy->isPromotableIntegerType() ?
3402 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3403}
3404
3405void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00003406 ABIArgInfo &RetInfo = FI.getReturnInfo();
3407 RetInfo = classifyReturnType(FI.getReturnType());
3408
3409 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003410 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00003411
Akira Hatanakab579fe52011-06-02 00:09:17 +00003412 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3413 it != ie; ++it)
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003414 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00003415}
3416
3417llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3418 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00003419 llvm::Type *BP = CGF.Int8PtrTy;
3420 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003421
3422 CGBuilderTy &Builder = CGF.Builder;
3423 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3424 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka37715282012-01-23 23:59:52 +00003425 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003426 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3427 llvm::Value *AddrTyped;
Akira Hatanaka37715282012-01-23 23:59:52 +00003428 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3429 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003430
3431 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka37715282012-01-23 23:59:52 +00003432 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3433 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3434 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3435 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003436 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3437 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3438 }
3439 else
3440 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3441
3442 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka37715282012-01-23 23:59:52 +00003443 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003444 uint64_t Offset =
3445 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3446 llvm::Value *NextAddr =
Akira Hatanaka37715282012-01-23 23:59:52 +00003447 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003448 "ap.next");
3449 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3450
3451 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003452}
3453
John McCall943fae92010-05-27 06:19:26 +00003454bool
3455MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3456 llvm::Value *Address) const {
3457 // This information comes from gcc's implementation, which seems to
3458 // as canonical as it gets.
3459
John McCall943fae92010-05-27 06:19:26 +00003460 // Everything on MIPS is 4 bytes. Double-precision FP registers
3461 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00003462 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00003463
3464 // 0-31 are the general purpose registers, $0 - $31.
3465 // 32-63 are the floating-point registers, $f0 - $f31.
3466 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3467 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00003468 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00003469
3470 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3471 // They are one bit wide and ignored here.
3472
3473 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3474 // (coprocessor 1 is the FP unit)
3475 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3476 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3477 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00003478 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00003479 return false;
3480}
3481
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003482//===----------------------------------------------------------------------===//
3483// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3484// Currently subclassed only to implement custom OpenCL C function attribute
3485// handling.
3486//===----------------------------------------------------------------------===//
3487
3488namespace {
3489
3490class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3491public:
3492 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3493 : DefaultTargetCodeGenInfo(CGT) {}
3494
3495 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3496 CodeGen::CodeGenModule &M) const;
3497};
3498
3499void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3500 llvm::GlobalValue *GV,
3501 CodeGen::CodeGenModule &M) const {
3502 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3503 if (!FD) return;
3504
3505 llvm::Function *F = cast<llvm::Function>(GV);
3506
David Blaikiebbafb8a2012-03-11 07:00:24 +00003507 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003508 if (FD->hasAttr<OpenCLKernelAttr>()) {
3509 // OpenCL C Kernel functions are not subject to inlining
3510 F->addFnAttr(llvm::Attribute::NoInline);
3511
3512 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3513
3514 // Convert the reqd_work_group_size() attributes to metadata.
3515 llvm::LLVMContext &Context = F->getContext();
3516 llvm::NamedMDNode *OpenCLMetadata =
3517 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3518
3519 SmallVector<llvm::Value*, 5> Operands;
3520 Operands.push_back(F);
3521
Chris Lattnerece04092012-02-07 00:39:47 +00003522 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3523 llvm::APInt(32,
3524 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3525 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3526 llvm::APInt(32,
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003527 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattnerece04092012-02-07 00:39:47 +00003528 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3529 llvm::APInt(32,
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003530 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3531
3532 // Add a boolean constant operand for "required" (true) or "hint" (false)
3533 // for implementing the work_group_size_hint attr later. Currently
3534 // always true as the hint is not yet implemented.
Chris Lattnerece04092012-02-07 00:39:47 +00003535 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003536 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3537 }
3538 }
3539 }
3540}
3541
3542}
John McCall943fae92010-05-27 06:19:26 +00003543
Tony Linthicum76329bf2011-12-12 21:14:55 +00003544//===----------------------------------------------------------------------===//
3545// Hexagon ABI Implementation
3546//===----------------------------------------------------------------------===//
3547
3548namespace {
3549
3550class HexagonABIInfo : public ABIInfo {
3551
3552
3553public:
3554 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3555
3556private:
3557
3558 ABIArgInfo classifyReturnType(QualType RetTy) const;
3559 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3560
3561 virtual void computeInfo(CGFunctionInfo &FI) const;
3562
3563 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3564 CodeGenFunction &CGF) const;
3565};
3566
3567class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3568public:
3569 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3570 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3571
3572 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3573 return 29;
3574 }
3575};
3576
3577}
3578
3579void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3580 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3581 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3582 it != ie; ++it)
3583 it->info = classifyArgumentType(it->type);
3584}
3585
3586ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3587 if (!isAggregateTypeForABI(Ty)) {
3588 // Treat an enum type as its underlying type.
3589 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3590 Ty = EnumTy->getDecl()->getIntegerType();
3591
3592 return (Ty->isPromotableIntegerType() ?
3593 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3594 }
3595
3596 // Ignore empty records.
3597 if (isEmptyRecord(getContext(), Ty, true))
3598 return ABIArgInfo::getIgnore();
3599
3600 // Structures with either a non-trivial destructor or a non-trivial
3601 // copy constructor are always indirect.
3602 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3603 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3604
3605 uint64_t Size = getContext().getTypeSize(Ty);
3606 if (Size > 64)
3607 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3608 // Pass in the smallest viable integer type.
3609 else if (Size > 32)
3610 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3611 else if (Size > 16)
3612 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3613 else if (Size > 8)
3614 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3615 else
3616 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3617}
3618
3619ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3620 if (RetTy->isVoidType())
3621 return ABIArgInfo::getIgnore();
3622
3623 // Large vector types should be returned via memory.
3624 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3625 return ABIArgInfo::getIndirect(0);
3626
3627 if (!isAggregateTypeForABI(RetTy)) {
3628 // Treat an enum type as its underlying type.
3629 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3630 RetTy = EnumTy->getDecl()->getIntegerType();
3631
3632 return (RetTy->isPromotableIntegerType() ?
3633 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3634 }
3635
3636 // Structures with either a non-trivial destructor or a non-trivial
3637 // copy constructor are always indirect.
3638 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3639 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3640
3641 if (isEmptyRecord(getContext(), RetTy, true))
3642 return ABIArgInfo::getIgnore();
3643
3644 // Aggregates <= 8 bytes are returned in r0; other aggregates
3645 // are returned indirectly.
3646 uint64_t Size = getContext().getTypeSize(RetTy);
3647 if (Size <= 64) {
3648 // Return in the smallest viable integer type.
3649 if (Size <= 8)
3650 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3651 if (Size <= 16)
3652 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3653 if (Size <= 32)
3654 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3655 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3656 }
3657
3658 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3659}
3660
3661llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattnerece04092012-02-07 00:39:47 +00003662 CodeGenFunction &CGF) const {
Tony Linthicum76329bf2011-12-12 21:14:55 +00003663 // FIXME: Need to handle alignment
Chris Lattnerece04092012-02-07 00:39:47 +00003664 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum76329bf2011-12-12 21:14:55 +00003665
3666 CGBuilderTy &Builder = CGF.Builder;
3667 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3668 "ap");
3669 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3670 llvm::Type *PTy =
3671 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3672 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3673
3674 uint64_t Offset =
3675 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3676 llvm::Value *NextAddr =
3677 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3678 "ap.next");
3679 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3680
3681 return AddrTyped;
3682}
3683
3684
Chris Lattner2b037972010-07-29 02:01:43 +00003685const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003686 if (TheTargetCodeGenInfo)
3687 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003688
Douglas Gregore8bbc122011-09-02 00:18:52 +00003689 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00003690 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003691 default:
Chris Lattner2b037972010-07-29 02:01:43 +00003692 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003693
John McCall943fae92010-05-27 06:19:26 +00003694 case llvm::Triple::mips:
3695 case llvm::Triple::mipsel:
Akira Hatanaka14378522011-11-02 23:14:57 +00003696 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCall943fae92010-05-27 06:19:26 +00003697
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003698 case llvm::Triple::mips64:
3699 case llvm::Triple::mips64el:
Akira Hatanaka14378522011-11-02 23:14:57 +00003700 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003701
Daniel Dunbard59655c2009-09-12 00:59:49 +00003702 case llvm::Triple::arm:
3703 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003704 {
3705 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003706
Douglas Gregore8bbc122011-09-02 00:18:52 +00003707 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003708 Kind = ARMABIInfo::APCS;
3709 else if (CodeGenOpts.FloatABI == "hard")
3710 Kind = ARMABIInfo::AAPCS_VFP;
3711
3712 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3713 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00003714
John McCallea8d8bb2010-03-11 00:10:12 +00003715 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00003716 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divackyd966e722012-05-09 18:22:46 +00003717 case llvm::Triple::ppc64:
3718 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00003719
Peter Collingbournec947aae2012-05-20 23:28:41 +00003720 case llvm::Triple::nvptx:
3721 case llvm::Triple::nvptx64:
Justin Holewinski83e96682012-05-24 17:43:12 +00003722 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003723
Wesley Peck36a1f682010-12-19 19:57:51 +00003724 case llvm::Triple::mblaze:
3725 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3726
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003727 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003728 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003729
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003730 case llvm::Triple::tce:
3731 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3732
Eli Friedman33465822011-07-08 23:31:17 +00003733 case llvm::Triple::x86: {
Douglas Gregore8bbc122011-09-02 00:18:52 +00003734 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedman33465822011-07-08 23:31:17 +00003735
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003736 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003737 return *(TheTargetCodeGenInfo =
Eli Friedmana98d1f82012-01-25 22:46:34 +00003738 new X86_32TargetCodeGenInfo(
3739 Types, true, true, DisableMMX, false));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003740
3741 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003742 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003743 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003744 case llvm::Triple::AuroraUX:
3745 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003746 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003747 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003748 return *(TheTargetCodeGenInfo =
Eli Friedmana98d1f82012-01-25 22:46:34 +00003749 new X86_32TargetCodeGenInfo(
3750 Types, false, true, DisableMMX, false));
3751
3752 case llvm::Triple::Win32:
3753 return *(TheTargetCodeGenInfo =
3754 new X86_32TargetCodeGenInfo(
3755 Types, false, true, DisableMMX, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003756
3757 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003758 return *(TheTargetCodeGenInfo =
Eli Friedmana98d1f82012-01-25 22:46:34 +00003759 new X86_32TargetCodeGenInfo(
3760 Types, false, false, DisableMMX, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003761 }
Eli Friedman33465822011-07-08 23:31:17 +00003762 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003763
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003764 case llvm::Triple::x86_64: {
3765 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3766
Chris Lattner04dc9572010-08-31 16:44:54 +00003767 switch (Triple.getOS()) {
3768 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003769 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003770 case llvm::Triple::Cygwin:
3771 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3772 default:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003773 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3774 HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00003775 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003776 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00003777 case llvm::Triple::hexagon:
3778 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003779 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003780}