blob: 006cc4b65520e5bddad58830e04d899bc1acc63a [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
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000437public:
Chris Lattner2b037972010-07-29 02:01:43 +0000438
Aaron Ballman3c424412012-02-22 03:04:13 +0000439 ABIArgInfo classifyReturnType(QualType RetTy,
440 unsigned callingConvention) const;
Chris Lattner458b2aa2010-07-29 02:16:43 +0000441 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000442
Chris Lattner22326a12010-07-29 02:31:05 +0000443 virtual void computeInfo(CGFunctionInfo &FI) const {
Aaron Ballman3c424412012-02-22 03:04:13 +0000444 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
445 FI.getCallingConvention());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000446 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
447 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000448 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449 }
450
451 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
452 CodeGenFunction &CGF) const;
453
Eli Friedmana98d1f82012-01-25 22:46:34 +0000454 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
Eli Friedman33465822011-07-08 23:31:17 +0000455 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Eli Friedmana98d1f82012-01-25 22:46:34 +0000456 IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000457};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000458
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000459class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
460public:
Eli Friedmana98d1f82012-01-25 22:46:34 +0000461 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
462 bool d, bool p, bool m, bool w)
463 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000464
465 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
466 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000467
468 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
469 // Darwin uses different dwarf register numbers for EH.
470 if (CGM.isTargetDarwin()) return 5;
471
472 return 4;
473 }
474
475 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
476 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000477
Jay Foad7c57be32011-07-11 09:56:20 +0000478 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000479 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000480 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000481 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
482 }
483
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000484};
485
486}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000487
488/// shouldReturnTypeInRegister - Determine if the given type should be
489/// passed in a register (for the Darwin ABI).
490bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman3c424412012-02-22 03:04:13 +0000491 ASTContext &Context,
492 unsigned callingConvention) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000493 uint64_t Size = Context.getTypeSize(Ty);
494
495 // Type must be register sized.
496 if (!isRegisterSize(Size))
497 return false;
498
499 if (Ty->isVectorType()) {
500 // 64- and 128- bit vectors inside structures are not returned in
501 // registers.
502 if (Size == 64 || Size == 128)
503 return false;
504
505 return true;
506 }
507
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000508 // If this is a builtin, pointer, enum, complex type, member pointer, or
509 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000510 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000511 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000512 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000513 return true;
514
515 // Arrays are treated like records.
516 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman3c424412012-02-22 03:04:13 +0000517 return shouldReturnTypeInRegister(AT->getElementType(), Context,
518 callingConvention);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000519
520 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000521 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000522 if (!RT) return false;
523
Anders Carlsson40446e82010-01-27 03:25:19 +0000524 // FIXME: Traverse bases here too.
525
Aaron Ballman3c424412012-02-22 03:04:13 +0000526 // For thiscall conventions, structures will never be returned in
527 // a register. This is for compatibility with the MSVC ABI
528 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
529 RT->isStructureType()) {
530 return false;
531 }
532
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000533 // Structure types are passed in register if all fields would be
534 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000535 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
536 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000537 const FieldDecl *FD = *i;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000538
539 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000540 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000541 continue;
542
543 // Check fields recursively.
Aaron Ballman3c424412012-02-22 03:04:13 +0000544 if (!shouldReturnTypeInRegister(FD->getType(), Context,
545 callingConvention))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000546 return false;
547 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000548 return true;
549}
550
Aaron Ballman3c424412012-02-22 03:04:13 +0000551ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
552 unsigned callingConvention) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000553 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000554 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000555
Chris Lattner458b2aa2010-07-29 02:16:43 +0000556 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000557 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000558 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000559 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000560
561 // 128-bit vectors are a special case; they are returned in
562 // registers and we need to make sure to pick a type the LLVM
563 // backend will like.
564 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000565 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000566 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000567
568 // Always return in register if it fits in a general purpose
569 // register, or if it is 64 bits and has a single element.
570 if ((Size == 8 || Size == 16 || Size == 32) ||
571 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000572 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000573 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000574
575 return ABIArgInfo::getIndirect(0);
576 }
577
578 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000579 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000580
John McCalla1dee5302010-08-22 10:59:02 +0000581 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000582 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000583 // Structures with either a non-trivial destructor or a non-trivial
584 // copy constructor are always indirect.
585 if (hasNonTrivialDestructorOrCopyConstructor(RT))
586 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000587
Anders Carlsson5789c492009-10-20 22:07:59 +0000588 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000589 if (RT->getDecl()->hasFlexibleArrayMember())
590 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000591 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000592
David Chisnallde3a0692009-08-17 23:08:21 +0000593 // If specified, structs and unions are always indirect.
594 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000595 return ABIArgInfo::getIndirect(0);
596
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000597 // Small structures which are register sized are generally returned
598 // in a register.
Aaron Ballman3c424412012-02-22 03:04:13 +0000599 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
600 callingConvention)) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000601 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000602
603 // As a special-case, if the struct is a "single-element" struct, and
604 // the field is of type "float" or "double", return it in a
Eli Friedmana98d1f82012-01-25 22:46:34 +0000605 // floating-point register. (MSVC does not apply this special case.)
606 // We apply a similar transformation for pointer types to improve the
607 // quality of the generated IR.
Eli Friedmanee945342011-11-18 01:25:50 +0000608 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedmana98d1f82012-01-25 22:46:34 +0000609 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
610 || SeltTy->hasPointerRepresentation())
Eli Friedmanee945342011-11-18 01:25:50 +0000611 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
612
613 // FIXME: We should be able to narrow this integer in cases with dead
614 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000615 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000616 }
617
618 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000619 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000620
Chris Lattner458b2aa2010-07-29 02:16:43 +0000621 // Treat an enum type as its underlying type.
622 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
623 RetTy = EnumTy->getDecl()->getIntegerType();
624
625 return (RetTy->isPromotableIntegerType() ?
626 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000627}
628
Eli Friedman7919bea2012-06-05 19:40:46 +0000629static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
630 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
631}
632
Daniel Dunbared23de32010-09-16 20:42:00 +0000633static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
634 const RecordType *RT = Ty->getAs<RecordType>();
635 if (!RT)
636 return 0;
637 const RecordDecl *RD = RT->getDecl();
638
639 // If this is a C++ record, check the bases first.
640 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
641 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
642 e = CXXRD->bases_end(); i != e; ++i)
643 if (!isRecordWithSSEVectorType(Context, i->getType()))
644 return false;
645
646 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
647 i != e; ++i) {
648 QualType FT = i->getType();
649
Eli Friedman7919bea2012-06-05 19:40:46 +0000650 if (isSSEVectorType(Context, FT))
Daniel Dunbared23de32010-09-16 20:42:00 +0000651 return true;
652
653 if (isRecordWithSSEVectorType(Context, FT))
654 return true;
655 }
656
657 return false;
658}
659
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000660unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
661 unsigned Align) const {
662 // Otherwise, if the alignment is less than or equal to the minimum ABI
663 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000664 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000665 return 0; // Use default alignment.
666
667 // On non-Darwin, the stack type alignment is always 4.
668 if (!IsDarwinVectorABI) {
669 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000670 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000671 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000672
Daniel Dunbared23de32010-09-16 20:42:00 +0000673 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7919bea2012-06-05 19:40:46 +0000674 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
675 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbared23de32010-09-16 20:42:00 +0000676 return 16;
677
678 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000679}
680
Chris Lattner458b2aa2010-07-29 02:16:43 +0000681ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000682 if (!ByVal)
683 return ABIArgInfo::getIndirect(0, false);
684
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000685 // Compute the byval alignment.
686 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
687 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
688 if (StackAlign == 0)
Chris Lattnere76b95a2011-05-22 23:35:00 +0000689 return ABIArgInfo::getIndirect(4);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000690
691 // If the stack alignment is less than the type alignment, realign the
692 // argument.
693 if (StackAlign < TypeAlign)
694 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
695 /*Realign=*/true);
696
697 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000698}
699
Chris Lattner458b2aa2010-07-29 02:16:43 +0000700ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000701 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000702 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000703 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000704 if (const RecordType *RT = Ty->getAs<RecordType>()) {
705 // Structures with either a non-trivial destructor or a non-trivial
706 // copy constructor are always indirect.
707 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000708 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000709
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000710 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000711 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000712 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000713
Eli Friedman9f061a32011-11-18 00:28:11 +0000714 // Ignore empty structs/unions.
Eli Friedmanf22fa9e2011-11-18 04:01:36 +0000715 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000716 return ABIArgInfo::getIgnore();
717
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000718 // Expand small (<= 128-bit) record types when we know that the stack layout
719 // of those arguments will match the struct. This is important because the
720 // LLVM backend isn't smart enough to remove byval, which inhibits many
721 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000722 if (getContext().getTypeSize(Ty) <= 4*32 &&
723 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000724 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000725
Chris Lattner458b2aa2010-07-29 02:16:43 +0000726 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000727 }
728
Chris Lattnerd774ae92010-08-26 20:05:13 +0000729 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000730 // On Darwin, some vectors are passed in memory, we handle this by passing
731 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000732 if (IsDarwinVectorABI) {
733 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000734 if ((Size == 8 || Size == 16 || Size == 32) ||
735 (Size == 64 && VT->getNumElements() == 1))
736 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
737 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000738 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000739
Chris Lattnera5f58b02011-07-09 17:41:47 +0000740 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +0000741 if (UseX86_MMXType(IRType)) {
Eli Friedman33465822011-07-08 23:31:17 +0000742 if (IsMMXDisabled)
743 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
744 64));
Bill Wendling5cd41c42010-10-18 03:41:31 +0000745 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
746 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
747 return AAI;
748 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000749
Chris Lattnerd774ae92010-08-26 20:05:13 +0000750 return ABIArgInfo::getDirect();
751 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000752
753
Chris Lattner458b2aa2010-07-29 02:16:43 +0000754 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
755 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000756
Chris Lattner458b2aa2010-07-29 02:16:43 +0000757 return (Ty->isPromotableIntegerType() ?
758 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000759}
760
761llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
762 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +0000763 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000764
765 CGBuilderTy &Builder = CGF.Builder;
766 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
767 "ap");
768 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000769
770 // Compute if the address needs to be aligned
771 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
772 Align = getTypeStackAlignInBytes(Ty, Align);
773 Align = std::max(Align, 4U);
774 if (Align > 4) {
775 // addr = (addr + align - 1) & -align;
776 llvm::Value *Offset =
777 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
778 Addr = CGF.Builder.CreateGEP(Addr, Offset);
779 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
780 CGF.Int32Ty);
781 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
782 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
783 Addr->getType(),
784 "ap.cur.aligned");
785 }
786
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000787 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000788 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000789 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
790
791 uint64_t Offset =
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000792 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000793 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000794 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000795 "ap.next");
796 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
797
798 return AddrTyped;
799}
800
Charles Davis4ea31ab2010-02-13 15:54:06 +0000801void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
802 llvm::GlobalValue *GV,
803 CodeGen::CodeGenModule &CGM) const {
804 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
805 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
806 // Get the LLVM function.
807 llvm::Function *Fn = cast<llvm::Function>(GV);
808
809 // Now add the 'alignstack' attribute with a value of 16.
810 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
811 }
812 }
813}
814
John McCallbeec5a02010-03-06 00:35:14 +0000815bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
816 CodeGen::CodeGenFunction &CGF,
817 llvm::Value *Address) const {
818 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallbeec5a02010-03-06 00:35:14 +0000819
Chris Lattnerece04092012-02-07 00:39:47 +0000820 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000821
John McCallbeec5a02010-03-06 00:35:14 +0000822 // 0-7 are the eight integer registers; the order is different
823 // on Darwin (for EH), but the range is the same.
824 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000825 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000826
827 if (CGF.CGM.isTargetDarwin()) {
828 // 12-16 are st(0..4). Not sure why we stop at 4.
829 // These have size 16, which is sizeof(long double) on
830 // platforms with 8-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +0000831 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCall943fae92010-05-27 06:19:26 +0000832 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000833
John McCallbeec5a02010-03-06 00:35:14 +0000834 } else {
835 // 9 is %eflags, which doesn't get a size on Darwin for some
836 // reason.
837 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
838
839 // 11-16 are st(0..5). Not sure why we stop at 5.
840 // These have size 12, which is sizeof(long double) on
841 // platforms with 4-byte alignment for that type.
Chris Lattnerece04092012-02-07 00:39:47 +0000842 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCall943fae92010-05-27 06:19:26 +0000843 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
844 }
John McCallbeec5a02010-03-06 00:35:14 +0000845
846 return false;
847}
848
Chris Lattner0cf24192010-06-28 20:05:43 +0000849//===----------------------------------------------------------------------===//
850// X86-64 ABI Implementation
851//===----------------------------------------------------------------------===//
852
853
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000854namespace {
855/// X86_64ABIInfo - The X86_64 ABI information.
856class X86_64ABIInfo : public ABIInfo {
857 enum Class {
858 Integer = 0,
859 SSE,
860 SSEUp,
861 X87,
862 X87Up,
863 ComplexX87,
864 NoClass,
865 Memory
866 };
867
868 /// merge - Implement the X86_64 ABI merging algorithm.
869 ///
870 /// Merge an accumulating classification \arg Accum with a field
871 /// classification \arg Field.
872 ///
873 /// \param Accum - The accumulating classification. This should
874 /// always be either NoClass or the result of a previous merge
875 /// call. In addition, this should never be Memory (the caller
876 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000877 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000878
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000879 /// postMerge - Implement the X86_64 ABI post merging algorithm.
880 ///
881 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
882 /// final MEMORY or SSE classes when necessary.
883 ///
884 /// \param AggregateSize - The size of the current aggregate in
885 /// the classification process.
886 ///
887 /// \param Lo - The classification for the parts of the type
888 /// residing in the low word of the containing object.
889 ///
890 /// \param Hi - The classification for the parts of the type
891 /// residing in the higher words of the containing object.
892 ///
893 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
894
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000895 /// classify - Determine the x86_64 register classes in which the
896 /// given type T should be passed.
897 ///
898 /// \param Lo - The classification for the parts of the type
899 /// residing in the low word of the containing object.
900 ///
901 /// \param Hi - The classification for the parts of the type
902 /// residing in the high word of the containing object.
903 ///
904 /// \param OffsetBase - The bit offset of this type in the
905 /// containing object. Some parameters are classified different
906 /// depending on whether they straddle an eightbyte boundary.
907 ///
908 /// If a word is unused its result will be NoClass; if a type should
909 /// be passed in Memory then at least the classification of \arg Lo
910 /// will be Memory.
911 ///
912 /// The \arg Lo class will be NoClass iff the argument is ignored.
913 ///
914 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
915 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000916 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000917
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000918 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000919 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
920 unsigned IROffset, QualType SourceTy,
921 unsigned SourceOffset) const;
922 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
923 unsigned IROffset, QualType SourceTy,
924 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000925
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000926 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000927 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000928 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000929
930 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000931 /// such that the argument will be passed in memory.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +0000932 ///
933 /// \param freeIntRegs - The number of free integer registers remaining
934 /// available.
935 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000936
Chris Lattner458b2aa2010-07-29 02:16:43 +0000937 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000938
Bill Wendling5cd41c42010-10-18 03:41:31 +0000939 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +0000940 unsigned freeIntRegs,
Bill Wendling5cd41c42010-10-18 03:41:31 +0000941 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000942 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000943
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000944 bool IsIllegalVectorType(QualType Ty) const;
945
John McCalle0fda732011-04-21 01:20:55 +0000946 /// The 0.98 ABI revision clarified a lot of ambiguities,
947 /// unfortunately in ways that were not always consistent with
948 /// certain previous compilers. In particular, platforms which
949 /// required strict binary compatibility with older versions of GCC
950 /// may need to exempt themselves.
951 bool honorsRevision0_98() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000952 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +0000953 }
954
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000955 bool HasAVX;
956
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000957public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000958 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
959 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000960
John McCalla729c622012-02-17 03:33:10 +0000961 bool isPassedUsingAVXType(QualType type) const {
962 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +0000963 // The freeIntRegs argument doesn't matter here.
964 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCalla729c622012-02-17 03:33:10 +0000965 if (info.isDirect()) {
966 llvm::Type *ty = info.getCoerceToType();
967 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
968 return (vectorTy->getBitWidth() > 128);
969 }
970 return false;
971 }
972
Chris Lattner22326a12010-07-29 02:31:05 +0000973 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000974
975 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
976 CodeGenFunction &CGF) const;
977};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000978
Chris Lattner04dc9572010-08-31 16:44:54 +0000979/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000980class WinX86_64ABIInfo : public ABIInfo {
981
982 ABIArgInfo classify(QualType Ty) const;
983
Chris Lattner04dc9572010-08-31 16:44:54 +0000984public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000985 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
986
987 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000988
989 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
990 CodeGenFunction &CGF) const;
991};
992
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000993class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
994public:
Eli Friedmanbfd5add2011-12-02 00:11:43 +0000995 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
996 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000997
John McCalla729c622012-02-17 03:33:10 +0000998 const X86_64ABIInfo &getABIInfo() const {
999 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1000 }
1001
John McCallbeec5a02010-03-06 00:35:14 +00001002 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1003 return 7;
1004 }
1005
1006 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1007 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001008 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001009
John McCall943fae92010-05-27 06:19:26 +00001010 // 0-15 are the 16 integer registers.
1011 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001012 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +00001013 return false;
1014 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001015
Jay Foad7c57be32011-07-11 09:56:20 +00001016 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001017 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +00001018 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +00001019 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1020 }
1021
John McCalla729c622012-02-17 03:33:10 +00001022 bool isNoProtoCallVariadic(const CallArgList &args,
1023 const FunctionNoProtoType *fnType) const {
John McCallcbc038a2011-09-21 08:08:30 +00001024 // The default CC on x86-64 sets %al to the number of SSA
1025 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001026 // function, so we override the default behavior. However, don't do
Eli Friedmanb8e45b22011-12-06 03:08:26 +00001027 // that when AVX types are involved: the ABI explicitly states it is
1028 // undefined, and it doesn't work in practice because of how the ABI
1029 // defines varargs anyway.
John McCalla729c622012-02-17 03:33:10 +00001030 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001031 bool HasAVXType = false;
John McCalla729c622012-02-17 03:33:10 +00001032 for (CallArgList::const_iterator
1033 it = args.begin(), ie = args.end(); it != ie; ++it) {
1034 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1035 HasAVXType = true;
1036 break;
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001037 }
1038 }
John McCalla729c622012-02-17 03:33:10 +00001039
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001040 if (!HasAVXType)
1041 return true;
1042 }
John McCallcbc038a2011-09-21 08:08:30 +00001043
John McCalla729c622012-02-17 03:33:10 +00001044 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCallcbc038a2011-09-21 08:08:30 +00001045 }
1046
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001047};
1048
Chris Lattner04dc9572010-08-31 16:44:54 +00001049class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1050public:
1051 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1052 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1053
1054 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1055 return 7;
1056 }
1057
1058 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1059 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00001060 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001061
Chris Lattner04dc9572010-08-31 16:44:54 +00001062 // 0-15 are the 16 integer registers.
1063 // 16 is %rip.
Chris Lattnerece04092012-02-07 00:39:47 +00001064 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattner04dc9572010-08-31 16:44:54 +00001065 return false;
1066 }
1067};
1068
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001069}
1070
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001071void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1072 Class &Hi) const {
1073 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1074 //
1075 // (a) If one of the classes is Memory, the whole argument is passed in
1076 // memory.
1077 //
1078 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1079 // memory.
1080 //
1081 // (c) If the size of the aggregate exceeds two eightbytes and the first
1082 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1083 // argument is passed in memory. NOTE: This is necessary to keep the
1084 // ABI working for processors that don't support the __m256 type.
1085 //
1086 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1087 //
1088 // Some of these are enforced by the merging logic. Others can arise
1089 // only with unions; for example:
1090 // union { _Complex double; unsigned; }
1091 //
1092 // Note that clauses (b) and (c) were added in 0.98.
1093 //
1094 if (Hi == Memory)
1095 Lo = Memory;
1096 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1097 Lo = Memory;
1098 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1099 Lo = Memory;
1100 if (Hi == SSEUp && Lo != SSE)
1101 Hi = SSE;
1102}
1103
Chris Lattnerd776fb12010-06-28 21:43:59 +00001104X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001105 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1106 // classified recursively so that always two fields are
1107 // considered. The resulting class is calculated according to
1108 // the classes of the fields in the eightbyte:
1109 //
1110 // (a) If both classes are equal, this is the resulting class.
1111 //
1112 // (b) If one of the classes is NO_CLASS, the resulting class is
1113 // the other class.
1114 //
1115 // (c) If one of the classes is MEMORY, the result is the MEMORY
1116 // class.
1117 //
1118 // (d) If one of the classes is INTEGER, the result is the
1119 // INTEGER.
1120 //
1121 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1122 // MEMORY is used as class.
1123 //
1124 // (f) Otherwise class SSE is used.
1125
1126 // Accum should never be memory (we should have returned) or
1127 // ComplexX87 (because this cannot be passed in a structure).
1128 assert((Accum != Memory && Accum != ComplexX87) &&
1129 "Invalid accumulated classification during merge.");
1130 if (Accum == Field || Field == NoClass)
1131 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001132 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001133 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001134 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001135 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001136 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001137 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001138 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1139 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001140 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001141 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001142}
1143
Chris Lattner5c740f12010-06-30 19:14:05 +00001144void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001145 Class &Lo, Class &Hi) const {
1146 // FIXME: This code can be simplified by introducing a simple value class for
1147 // Class pairs with appropriate constructor methods for the various
1148 // situations.
1149
1150 // FIXME: Some of the split computations are wrong; unaligned vectors
1151 // shouldn't be passed in registers for example, so there is no chance they
1152 // can straddle an eightbyte. Verify & simplify.
1153
1154 Lo = Hi = NoClass;
1155
1156 Class &Current = OffsetBase < 64 ? Lo : Hi;
1157 Current = Memory;
1158
John McCall9dd450b2009-09-21 23:43:11 +00001159 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001160 BuiltinType::Kind k = BT->getKind();
1161
1162 if (k == BuiltinType::Void) {
1163 Current = NoClass;
1164 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1165 Lo = Integer;
1166 Hi = Integer;
1167 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1168 Current = Integer;
1169 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1170 Current = SSE;
1171 } else if (k == BuiltinType::LongDouble) {
1172 Lo = X87;
1173 Hi = X87Up;
1174 }
1175 // FIXME: _Decimal32 and _Decimal64 are SSE.
1176 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001177 return;
1178 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001179
Chris Lattnerd776fb12010-06-28 21:43:59 +00001180 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001181 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001182 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001183 return;
1184 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001185
Chris Lattnerd776fb12010-06-28 21:43:59 +00001186 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001187 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001188 return;
1189 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001190
Chris Lattnerd776fb12010-06-28 21:43:59 +00001191 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001192 if (Ty->isMemberFunctionPointerType())
1193 Lo = Hi = Integer;
1194 else
1195 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001196 return;
1197 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001198
Chris Lattnerd776fb12010-06-28 21:43:59 +00001199 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001200 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001201 if (Size == 32) {
1202 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1203 // float> as integer.
1204 Current = Integer;
1205
1206 // If this type crosses an eightbyte boundary, it should be
1207 // split.
1208 uint64_t EB_Real = (OffsetBase) / 64;
1209 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1210 if (EB_Real != EB_Imag)
1211 Hi = Lo;
1212 } else if (Size == 64) {
1213 // gcc passes <1 x double> in memory. :(
1214 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1215 return;
1216
1217 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001218 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001219 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1220 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1221 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001222 Current = Integer;
1223 else
1224 Current = SSE;
1225
1226 // If this type crosses an eightbyte boundary, it should be
1227 // split.
1228 if (OffsetBase && OffsetBase != 64)
1229 Hi = Lo;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001230 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001231 // Arguments of 256-bits are split into four eightbyte chunks. The
1232 // least significant one belongs to class SSE and all the others to class
1233 // SSEUP. The original Lo and Hi design considers that types can't be
1234 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1235 // This design isn't correct for 256-bits, but since there're no cases
1236 // where the upper parts would need to be inspected, avoid adding
1237 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001238 Lo = SSE;
1239 Hi = SSEUp;
1240 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001241 return;
1242 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001243
Chris Lattnerd776fb12010-06-28 21:43:59 +00001244 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001245 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001246
Chris Lattner2b037972010-07-29 02:01:43 +00001247 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001248 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001249 if (Size <= 64)
1250 Current = Integer;
1251 else if (Size <= 128)
1252 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001253 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001254 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001255 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001256 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001257 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001258 Current = ComplexX87;
1259
1260 // If this complex type crosses an eightbyte boundary then it
1261 // should be split.
1262 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001263 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001264 if (Hi == NoClass && EB_Real != EB_Imag)
1265 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001266
Chris Lattnerd776fb12010-06-28 21:43:59 +00001267 return;
1268 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001269
Chris Lattner2b037972010-07-29 02:01:43 +00001270 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001271 // Arrays are treated like structures.
1272
Chris Lattner2b037972010-07-29 02:01:43 +00001273 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001274
1275 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001276 // than four eightbytes, ..., it has class MEMORY.
1277 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001278 return;
1279
1280 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1281 // fields, it has class MEMORY.
1282 //
1283 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001284 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001285 return;
1286
1287 // Otherwise implement simplified merge. We could be smarter about
1288 // this, but it isn't worth it and would be harder to verify.
1289 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001290 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001291 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001292
1293 // The only case a 256-bit wide vector could be used is when the array
1294 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1295 // to work for sizes wider than 128, early check and fallback to memory.
1296 if (Size > 128 && EltSize != 256)
1297 return;
1298
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001299 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1300 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001301 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001302 Lo = merge(Lo, FieldLo);
1303 Hi = merge(Hi, FieldHi);
1304 if (Lo == Memory || Hi == Memory)
1305 break;
1306 }
1307
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001308 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001309 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001310 return;
1311 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001312
Chris Lattnerd776fb12010-06-28 21:43:59 +00001313 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001314 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001315
1316 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001317 // than four eightbytes, ..., it has class MEMORY.
1318 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001319 return;
1320
Anders Carlsson20759ad2009-09-16 15:53:40 +00001321 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1322 // copy constructor or a non-trivial destructor, it is passed by invisible
1323 // reference.
1324 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1325 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001326
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001327 const RecordDecl *RD = RT->getDecl();
1328
1329 // Assume variable sized types are passed in memory.
1330 if (RD->hasFlexibleArrayMember())
1331 return;
1332
Chris Lattner2b037972010-07-29 02:01:43 +00001333 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001334
1335 // Reset Lo class, this will be recomputed.
1336 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001337
1338 // If this is a C++ record, classify the bases first.
1339 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1340 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1341 e = CXXRD->bases_end(); i != e; ++i) {
1342 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1343 "Unexpected base class!");
1344 const CXXRecordDecl *Base =
1345 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1346
1347 // Classify this field.
1348 //
1349 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1350 // single eightbyte, each is classified separately. Each eightbyte gets
1351 // initialized to class NO_CLASS.
1352 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001353 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001354 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001355 Lo = merge(Lo, FieldLo);
1356 Hi = merge(Hi, FieldHi);
1357 if (Lo == Memory || Hi == Memory)
1358 break;
1359 }
1360 }
1361
1362 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001363 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001364 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001365 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001366 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1367 bool BitField = i->isBitField();
1368
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001369 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1370 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001371 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001372 // The only case a 256-bit wide vector could be used is when the struct
1373 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1374 // to work for sizes wider than 128, early check and fallback to memory.
1375 //
1376 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1377 Lo = Memory;
1378 return;
1379 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001380 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001381 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001382 Lo = Memory;
1383 return;
1384 }
1385
1386 // Classify this field.
1387 //
1388 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1389 // exceeds a single eightbyte, each is classified
1390 // separately. Each eightbyte gets initialized to class
1391 // NO_CLASS.
1392 Class FieldLo, FieldHi;
1393
1394 // Bit-fields require special handling, they do not force the
1395 // structure to be passed in memory even if unaligned, and
1396 // therefore they can straddle an eightbyte.
1397 if (BitField) {
1398 // Ignore padding bit-fields.
1399 if (i->isUnnamedBitfield())
1400 continue;
1401
1402 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001403 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001404
1405 uint64_t EB_Lo = Offset / 64;
1406 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1407 FieldLo = FieldHi = NoClass;
1408 if (EB_Lo) {
1409 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1410 FieldLo = NoClass;
1411 FieldHi = Integer;
1412 } else {
1413 FieldLo = Integer;
1414 FieldHi = EB_Hi ? Integer : NoClass;
1415 }
1416 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001417 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001418 Lo = merge(Lo, FieldLo);
1419 Hi = merge(Hi, FieldHi);
1420 if (Lo == Memory || Hi == Memory)
1421 break;
1422 }
1423
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001424 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001425 }
1426}
1427
Chris Lattner22a931e2010-06-29 06:01:59 +00001428ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001429 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1430 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001431 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001432 // Treat an enum type as its underlying type.
1433 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1434 Ty = EnumTy->getDecl()->getIntegerType();
1435
1436 return (Ty->isPromotableIntegerType() ?
1437 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1438 }
1439
1440 return ABIArgInfo::getIndirect(0);
1441}
1442
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001443bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1444 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1445 uint64_t Size = getContext().getTypeSize(VecTy);
1446 unsigned LargestVector = HasAVX ? 256 : 128;
1447 if (Size <= 64 || Size > LargestVector)
1448 return true;
1449 }
1450
1451 return false;
1452}
1453
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001454ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1455 unsigned freeIntRegs) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001456 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1457 // place naturally.
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001458 //
1459 // This assumption is optimistic, as there could be free registers available
1460 // when we need to pass this argument in memory, and LLVM could try to pass
1461 // the argument in the free register. This does not seem to happen currently,
1462 // but this code would be much safer if we could mark the argument with
1463 // 'onstack'. See PR12193.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001464 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001465 // Treat an enum type as its underlying type.
1466 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1467 Ty = EnumTy->getDecl()->getIntegerType();
1468
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001469 return (Ty->isPromotableIntegerType() ?
1470 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001471 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001472
Daniel Dunbar53fac692010-04-21 19:49:55 +00001473 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1474 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001475
Chris Lattner44c2b902011-05-22 23:21:23 +00001476 // Compute the byval alignment. We specify the alignment of the byval in all
1477 // cases so that the mid-level optimizer knows the alignment of the byval.
1478 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001479
1480 // Attempt to avoid passing indirect results using byval when possible. This
1481 // is important for good codegen.
1482 //
1483 // We do this by coercing the value into a scalar type which the backend can
1484 // handle naturally (i.e., without using byval).
1485 //
1486 // For simplicity, we currently only do this when we have exhausted all of the
1487 // free integer registers. Doing this when there are free integer registers
1488 // would require more care, as we would have to ensure that the coerced value
1489 // did not claim the unused register. That would require either reording the
1490 // arguments to the function (so that any subsequent inreg values came first),
1491 // or only doing this optimization when there were no following arguments that
1492 // might be inreg.
1493 //
1494 // We currently expect it to be rare (particularly in well written code) for
1495 // arguments to be passed on the stack when there are still free integer
1496 // registers available (this would typically imply large structs being passed
1497 // by value), so this seems like a fair tradeoff for now.
1498 //
1499 // We can revisit this if the backend grows support for 'onstack' parameter
1500 // attributes. See PR12193.
1501 if (freeIntRegs == 0) {
1502 uint64_t Size = getContext().getTypeSize(Ty);
1503
1504 // If this type fits in an eightbyte, coerce it into the matching integral
1505 // type, which will end up on the stack (with alignment 8).
1506 if (Align == 8 && Size <= 64)
1507 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1508 Size));
1509 }
1510
Chris Lattner44c2b902011-05-22 23:21:23 +00001511 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001512}
1513
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001514/// GetByteVectorType - The ABI specifies that a value should be passed in an
1515/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001516/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001517llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001518 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001519
Chris Lattner9fa15c32010-07-29 05:02:29 +00001520 // Wrapper structs that just contain vectors are passed just like vectors,
1521 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001522 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001523 while (STy && STy->getNumElements() == 1) {
1524 IRType = STy->getElementType(0);
1525 STy = dyn_cast<llvm::StructType>(IRType);
1526 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001527
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001528 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001529 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1530 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001531 unsigned BitWidth = VT->getBitWidth();
Tanya Lattner71f1b2d2011-11-28 23:18:11 +00001532 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001533 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1534 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1535 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1536 EltTy->isIntegerTy(128)))
1537 return VT;
1538 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001539
Chris Lattner4200fe42010-07-29 04:56:46 +00001540 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1541}
1542
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001543/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1544/// is known to either be off the end of the specified type or being in
1545/// alignment padding. The user type specified is known to be at most 128 bits
1546/// in size, and have passed through X86_64ABIInfo::classify with a successful
1547/// classification that put one of the two halves in the INTEGER class.
1548///
1549/// It is conservatively correct to return false.
1550static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1551 unsigned EndBit, ASTContext &Context) {
1552 // If the bytes being queried are off the end of the type, there is no user
1553 // data hiding here. This handles analysis of builtins, vectors and other
1554 // types that don't contain interesting padding.
1555 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1556 if (TySize <= StartBit)
1557 return true;
1558
Chris Lattner98076a22010-07-29 07:43:55 +00001559 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1560 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1561 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1562
1563 // Check each element to see if the element overlaps with the queried range.
1564 for (unsigned i = 0; i != NumElts; ++i) {
1565 // If the element is after the span we care about, then we're done..
1566 unsigned EltOffset = i*EltSize;
1567 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001568
Chris Lattner98076a22010-07-29 07:43:55 +00001569 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1570 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1571 EndBit-EltOffset, Context))
1572 return false;
1573 }
1574 // If it overlaps no elements, then it is safe to process as padding.
1575 return true;
1576 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001577
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001578 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1579 const RecordDecl *RD = RT->getDecl();
1580 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001581
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001582 // If this is a C++ record, check the bases first.
1583 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1584 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1585 e = CXXRD->bases_end(); i != e; ++i) {
1586 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1587 "Unexpected base class!");
1588 const CXXRecordDecl *Base =
1589 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001590
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001591 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001592 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001593 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001594
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001595 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1596 if (!BitsContainNoUserData(i->getType(), BaseStart,
1597 EndBit-BaseOffset, Context))
1598 return false;
1599 }
1600 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001601
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001602 // Verify that no field has data that overlaps the region of interest. Yes
1603 // this could be sped up a lot by being smarter about queried fields,
1604 // however we're only looking at structs up to 16 bytes, so we don't care
1605 // much.
1606 unsigned idx = 0;
1607 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1608 i != e; ++i, ++idx) {
1609 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001610
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001611 // If we found a field after the region we care about, then we're done.
1612 if (FieldOffset >= EndBit) break;
1613
1614 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1615 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1616 Context))
1617 return false;
1618 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001619
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001620 // If nothing in this record overlapped the area of interest, then we're
1621 // clean.
1622 return true;
1623 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001624
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001625 return false;
1626}
1627
Chris Lattnere556a712010-07-29 18:39:32 +00001628/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1629/// float member at the specified offset. For example, {int,{float}} has a
1630/// float at offset 4. It is conservatively correct for this routine to return
1631/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00001632static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnere556a712010-07-29 18:39:32 +00001633 const llvm::TargetData &TD) {
1634 // Base case if we find a float.
1635 if (IROffset == 0 && IRType->isFloatTy())
1636 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001637
Chris Lattnere556a712010-07-29 18:39:32 +00001638 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001639 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00001640 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1641 unsigned Elt = SL->getElementContainingOffset(IROffset);
1642 IROffset -= SL->getElementOffset(Elt);
1643 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1644 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001645
Chris Lattnere556a712010-07-29 18:39:32 +00001646 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001647 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1648 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00001649 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1650 IROffset -= IROffset/EltSize*EltSize;
1651 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1652 }
1653
1654 return false;
1655}
1656
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001657
1658/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1659/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001660llvm::Type *X86_64ABIInfo::
1661GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001662 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001663 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001664 // pass as float if the last 4 bytes is just padding. This happens for
1665 // structs that contain 3 floats.
1666 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1667 SourceOffset*8+64, getContext()))
1668 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001669
Chris Lattnere556a712010-07-29 18:39:32 +00001670 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1671 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1672 // case.
1673 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001674 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1675 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001676
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001677 return llvm::Type::getDoubleTy(getVMContext());
1678}
1679
1680
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001681/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1682/// an 8-byte GPR. This means that we either have a scalar or we are talking
1683/// about the high or low part of an up-to-16-byte struct. This routine picks
1684/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001685/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1686/// etc).
1687///
1688/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1689/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1690/// the 8-byte value references. PrefType may be null.
1691///
1692/// SourceTy is the source level type for the entire argument. SourceOffset is
1693/// an offset into this that we're processing (which is always either 0 or 8).
1694///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001695llvm::Type *X86_64ABIInfo::
1696GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001697 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001698 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1699 // returning an 8-byte unit starting with it. See if we can safely use it.
1700 if (IROffset == 0) {
1701 // Pointers and int64's always fill the 8-byte unit.
1702 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1703 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001704
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001705 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1706 // goodness in the source type is just tail padding. This is allowed to
1707 // kick in for struct {double,int} on the int, but not on
1708 // struct{double,int,int} because we wouldn't return the second int. We
1709 // have to do this analysis on the source type because we can't depend on
1710 // unions being lowered a specific way etc.
1711 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1712 IRType->isIntegerTy(32)) {
1713 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001714
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001715 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1716 SourceOffset*8+64, getContext()))
1717 return IRType;
1718 }
1719 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001720
Chris Lattner2192fe52011-07-18 04:24:23 +00001721 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001722 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001723 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001724 if (IROffset < SL->getSizeInBytes()) {
1725 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1726 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001727
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001728 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1729 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001730 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001731 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001732
Chris Lattner2192fe52011-07-18 04:24:23 +00001733 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001734 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001735 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1736 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001737 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1738 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001739 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001740
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001741 // Okay, we don't have any better idea of what to pass, so we pass this in an
1742 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001743 unsigned TySizeInBytes =
1744 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001745
Chris Lattner3f763422010-07-29 17:34:39 +00001746 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001747
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001748 // It is always safe to classify this as an integer type up to i64 that
1749 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001750 return llvm::IntegerType::get(getVMContext(),
1751 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001752}
1753
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001754
1755/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1756/// be used as elements of a two register pair to pass or return, return a
1757/// first class aggregate to represent them. For example, if the low part of
1758/// a by-value argument should be passed as i32* and the high part as float,
1759/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001760static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001761GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001762 const llvm::TargetData &TD) {
1763 // In order to correctly satisfy the ABI, we need to the high part to start
1764 // at offset 8. If the high and low parts we inferred are both 4-byte types
1765 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1766 // the second element at offset 8. Check for this:
1767 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1768 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1769 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1770 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001771
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001772 // To handle this, we have to increase the size of the low part so that the
1773 // second element will start at an 8 byte offset. We can't increase the size
1774 // of the second element because it might make us access off the end of the
1775 // struct.
1776 if (HiStart != 8) {
1777 // There are only two sorts of types the ABI generation code can produce for
1778 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1779 // Promote these to a larger type.
1780 if (Lo->isFloatTy())
1781 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1782 else {
1783 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1784 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1785 }
1786 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001787
Chris Lattnera5f58b02011-07-09 17:41:47 +00001788 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001789
1790
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001791 // Verify that the second element is at an 8-byte offset.
1792 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1793 "Invalid x86-64 argument pair!");
1794 return Result;
1795}
1796
Chris Lattner31faff52010-07-28 23:06:14 +00001797ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001798classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001799 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1800 // classification algorithm.
1801 X86_64ABIInfo::Class Lo, Hi;
1802 classify(RetTy, 0, Lo, Hi);
1803
1804 // Check some invariants.
1805 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001806 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1807
Chris Lattnera5f58b02011-07-09 17:41:47 +00001808 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001809 switch (Lo) {
1810 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001811 if (Hi == NoClass)
1812 return ABIArgInfo::getIgnore();
1813 // If the low part is just padding, it takes no register, leave ResType
1814 // null.
1815 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1816 "Unknown missing lo part");
1817 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001818
1819 case SSEUp:
1820 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001821 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001822
1823 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1824 // hidden argument.
1825 case Memory:
1826 return getIndirectReturnResult(RetTy);
1827
1828 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1829 // available register of the sequence %rax, %rdx is used.
1830 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001831 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001832
Chris Lattner1f3a0632010-07-29 21:42:50 +00001833 // If we have a sign or zero extended integer, make sure to return Extend
1834 // so that the parameter gets the right LLVM IR attributes.
1835 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1836 // Treat an enum type as its underlying type.
1837 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1838 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001839
Chris Lattner1f3a0632010-07-29 21:42:50 +00001840 if (RetTy->isIntegralOrEnumerationType() &&
1841 RetTy->isPromotableIntegerType())
1842 return ABIArgInfo::getExtend();
1843 }
Chris Lattner31faff52010-07-28 23:06:14 +00001844 break;
1845
1846 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1847 // available SSE register of the sequence %xmm0, %xmm1 is used.
1848 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001849 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001850 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001851
1852 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1853 // returned on the X87 stack in %st0 as 80-bit x87 number.
1854 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001855 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001856 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001857
1858 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1859 // part of the value is returned in %st0 and the imaginary part in
1860 // %st1.
1861 case ComplexX87:
1862 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001863 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001864 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001865 NULL);
1866 break;
1867 }
1868
Chris Lattnera5f58b02011-07-09 17:41:47 +00001869 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001870 switch (Hi) {
1871 // Memory was handled previously and X87 should
1872 // never occur as a hi class.
1873 case Memory:
1874 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00001875 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001876
1877 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001878 case NoClass:
1879 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001880
Chris Lattner52b3c132010-09-01 00:20:33 +00001881 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001882 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001883 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1884 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001885 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001886 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001887 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001888 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1889 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001890 break;
1891
1892 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001893 // is passed in the next available eightbyte chunk if the last used
1894 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00001895 //
Chris Lattner57540c52011-04-15 05:22:18 +00001896 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001897 case SSEUp:
1898 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001899 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001900 break;
1901
1902 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1903 // returned together with the previous X87 value in %st0.
1904 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001905 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001906 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001907 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001908 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001909 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001910 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001911 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1912 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001913 }
Chris Lattner31faff52010-07-28 23:06:14 +00001914 break;
1915 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001916
Chris Lattner52b3c132010-09-01 00:20:33 +00001917 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001918 // known to pass in the high eightbyte of the result. We do this by forming a
1919 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001920 if (HighPart)
1921 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001922
Chris Lattner1f3a0632010-07-29 21:42:50 +00001923 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001924}
1925
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001926ABIArgInfo X86_64ABIInfo::classifyArgumentType(
1927 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
1928 const
1929{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001930 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001931 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001932
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001933 // Check some invariants.
1934 // FIXME: Enforce these by construction.
1935 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001936 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1937
1938 neededInt = 0;
1939 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001940 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001941 switch (Lo) {
1942 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001943 if (Hi == NoClass)
1944 return ABIArgInfo::getIgnore();
1945 // If the low part is just padding, it takes no register, leave ResType
1946 // null.
1947 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1948 "Unknown missing lo part");
1949 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001950
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1952 // on the stack.
1953 case Memory:
1954
1955 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1956 // COMPLEX_X87, it is passed in memory.
1957 case X87:
1958 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001959 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1960 ++neededInt;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00001961 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001962
1963 case SSEUp:
1964 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001965 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001966
1967 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1968 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1969 // and %r9 is used.
1970 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001971 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001972
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001973 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001974 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001975
1976 // If we have a sign or zero extended integer, make sure to return Extend
1977 // so that the parameter gets the right LLVM IR attributes.
1978 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1979 // Treat an enum type as its underlying type.
1980 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1981 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001982
Chris Lattner1f3a0632010-07-29 21:42:50 +00001983 if (Ty->isIntegralOrEnumerationType() &&
1984 Ty->isPromotableIntegerType())
1985 return ABIArgInfo::getExtend();
1986 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001987
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001988 break;
1989
1990 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1991 // available SSE register is used, the registers are taken in the
1992 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001993 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001994 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001995 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001996 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001997 break;
1998 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001999 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002000
Chris Lattnera5f58b02011-07-09 17:41:47 +00002001 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002002 switch (Hi) {
2003 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00002004 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002005 // which is passed in memory.
2006 case Memory:
2007 case X87:
2008 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00002009 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002010
2011 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002012
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002013 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002014 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00002015 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00002016 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002017
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002018 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2019 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002020 break;
2021
2022 // X87Up generally doesn't occur here (long double is passed in
2023 // memory), except in situations involving unions.
2024 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002025 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00002026 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002027
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002028 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2029 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00002030
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002031 ++neededSSE;
2032 break;
2033
2034 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2035 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002036 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002037 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00002038 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00002039 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002040 break;
2041 }
2042
Chris Lattnerbe5eb172010-09-01 00:24:35 +00002043 // If a high part was specified, merge it together with the low part. It is
2044 // known to pass in the high eightbyte of the result. We do this by forming a
2045 // first class struct aggregate with the high and low part: {low, high}
2046 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00002047 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00002048
Chris Lattner1f3a0632010-07-29 21:42:50 +00002049 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002050}
2051
Chris Lattner22326a12010-07-29 02:31:05 +00002052void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002053
Chris Lattner458b2aa2010-07-29 02:16:43 +00002054 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002055
2056 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002057 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002058
2059 // If the return value is indirect, then the hidden argument is consuming one
2060 // integer register.
2061 if (FI.getReturnInfo().isIndirect())
2062 --freeIntRegs;
2063
2064 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2065 // get assigned (in left-to-right order) for passing as follows...
2066 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2067 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00002068 unsigned neededInt, neededSSE;
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002069 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2070 neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002071
2072 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2073 // eightbyte of an argument, the whole argument is passed on the
2074 // stack. If registers have already been assigned for some
2075 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002076 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002077 freeIntRegs -= neededInt;
2078 freeSSERegs -= neededSSE;
2079 } else {
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002080 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002081 }
2082 }
2083}
2084
2085static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2086 QualType Ty,
2087 CodeGenFunction &CGF) {
2088 llvm::Value *overflow_arg_area_p =
2089 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2090 llvm::Value *overflow_arg_area =
2091 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2092
2093 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2094 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00002095 // It isn't stated explicitly in the standard, but in practice we use
2096 // alignment greater than 16 where necessary.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002097 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2098 if (Align > 8) {
Eli Friedmana1748562011-11-18 02:44:19 +00002099 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson41a75022009-08-13 21:57:51 +00002100 llvm::Value *Offset =
Eli Friedmana1748562011-11-18 02:44:19 +00002101 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002102 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2103 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002104 CGF.Int64Ty);
Eli Friedmana1748562011-11-18 02:44:19 +00002105 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002106 overflow_arg_area =
2107 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2108 overflow_arg_area->getType(),
2109 "overflow_arg_area.align");
2110 }
2111
2112 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00002113 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002114 llvm::Value *Res =
2115 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002116 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002117
2118 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2119 // l->overflow_arg_area + sizeof(type).
2120 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2121 // an 8 byte boundary.
2122
2123 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00002124 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002125 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002126 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2127 "overflow_arg_area.next");
2128 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2129
2130 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2131 return Res;
2132}
2133
2134llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2135 CodeGenFunction &CGF) const {
2136 // Assume that va_list type is correct; should be pointer to LLVM type:
2137 // struct {
2138 // i32 gp_offset;
2139 // i32 fp_offset;
2140 // i8* overflow_arg_area;
2141 // i8* reg_save_area;
2142 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002143 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002144
Chris Lattner9723d6c2010-03-11 18:19:55 +00002145 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbarf07b5ec2012-03-10 01:03:58 +00002146 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002147
2148 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2149 // in the registers. If not go to step 7.
2150 if (!neededInt && !neededSSE)
2151 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2152
2153 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2154 // general purpose registers needed to pass type and num_fp to hold
2155 // the number of floating point registers needed.
2156
2157 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2158 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2159 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2160 //
2161 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2162 // register save space).
2163
2164 llvm::Value *InRegs = 0;
2165 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2166 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2167 if (neededInt) {
2168 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2169 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002170 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2171 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002172 }
2173
2174 if (neededSSE) {
2175 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2176 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2177 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002178 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2179 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002180 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2181 }
2182
2183 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2184 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2185 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2186 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2187
2188 // Emit code to load the value if it was passed in registers.
2189
2190 CGF.EmitBlock(InRegBlock);
2191
2192 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2193 // an offset of l->gp_offset and/or l->fp_offset. This may require
2194 // copying to a temporary location in case the parameter is passed
2195 // in different register classes or requires an alignment greater
2196 // than 8 for general purpose registers and 16 for XMM registers.
2197 //
2198 // FIXME: This really results in shameful code when we end up needing to
2199 // collect arguments from different places; often what should result in a
2200 // simple assembling of a structure from scattered addresses has many more
2201 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002202 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002203 llvm::Value *RegAddr =
2204 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2205 "reg_save_area");
2206 if (neededInt && neededSSE) {
2207 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002208 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002209 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002210 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2211 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002212 llvm::Type *TyLo = ST->getElementType(0);
2213 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002214 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002215 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002216 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2217 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002218 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2219 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002220 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2221 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002222 llvm::Value *V =
2223 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2224 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2225 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2226 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2227
Owen Anderson170229f2009-07-14 23:10:40 +00002228 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002229 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002230 } else if (neededInt) {
2231 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2232 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002233 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002234 } else if (neededSSE == 1) {
2235 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2236 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2237 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002238 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002239 assert(neededSSE == 2 && "Invalid number of needed registers!");
2240 // SSE registers are spaced 16 bytes apart in the register save
2241 // area, we need to collect the two eightbytes together.
2242 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002243 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerece04092012-02-07 00:39:47 +00002244 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2192fe52011-07-18 04:24:23 +00002245 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002246 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2192fe52011-07-18 04:24:23 +00002247 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002248 DoubleTy, NULL);
2249 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2250 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2251 DblPtrTy));
2252 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2253 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2254 DblPtrTy));
2255 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2256 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2257 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002258 }
2259
2260 // AMD64-ABI 3.5.7p5: Step 5. Set:
2261 // l->gp_offset = l->gp_offset + num_gp * 8
2262 // l->fp_offset = l->fp_offset + num_fp * 16.
2263 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002264 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002265 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2266 gp_offset_p);
2267 }
2268 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002269 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002270 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2271 fp_offset_p);
2272 }
2273 CGF.EmitBranch(ContBlock);
2274
2275 // Emit code to load the value if it was passed in memory.
2276
2277 CGF.EmitBlock(InMemBlock);
2278 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2279
2280 // Return the appropriate result.
2281
2282 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002283 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002284 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002285 ResAddr->addIncoming(RegAddr, InRegBlock);
2286 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002287 return ResAddr;
2288}
2289
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002290ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2291
2292 if (Ty->isVoidType())
2293 return ABIArgInfo::getIgnore();
2294
2295 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2296 Ty = EnumTy->getDecl()->getIntegerType();
2297
2298 uint64_t Size = getContext().getTypeSize(Ty);
2299
2300 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002301 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2302 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002303 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2304
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002305 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2306 if (Size == 128 &&
Eli Friedmana98d1f82012-01-25 22:46:34 +00002307 getContext().getTargetInfo().getTriple().getOS()
2308 == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002309 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2310 Size));
2311
2312 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2313 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2314 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002315 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002316 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2317 Size));
2318
2319 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2320 }
2321
2322 if (Ty->isPromotableIntegerType())
2323 return ABIArgInfo::getExtend();
2324
2325 return ABIArgInfo::getDirect();
2326}
2327
2328void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2329
2330 QualType RetTy = FI.getReturnType();
2331 FI.getReturnInfo() = classify(RetTy);
2332
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002333 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2334 it != ie; ++it)
2335 it->info = classify(it->type);
2336}
2337
Chris Lattner04dc9572010-08-31 16:44:54 +00002338llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2339 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002340 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattner0cf24192010-06-28 20:05:43 +00002341
Chris Lattner04dc9572010-08-31 16:44:54 +00002342 CGBuilderTy &Builder = CGF.Builder;
2343 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2344 "ap");
2345 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2346 llvm::Type *PTy =
2347 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2348 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2349
2350 uint64_t Offset =
2351 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2352 llvm::Value *NextAddr =
2353 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2354 "ap.next");
2355 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2356
2357 return AddrTyped;
2358}
Chris Lattner0cf24192010-06-28 20:05:43 +00002359
John McCallea8d8bb2010-03-11 00:10:12 +00002360// PowerPC-32
2361
2362namespace {
2363class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2364public:
Chris Lattner2b037972010-07-29 02:01:43 +00002365 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002366
John McCallea8d8bb2010-03-11 00:10:12 +00002367 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2368 // This is recovered from gcc output.
2369 return 1; // r1 is the dedicated stack pointer
2370 }
2371
2372 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002373 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002374};
2375
2376}
2377
2378bool
2379PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2380 llvm::Value *Address) const {
2381 // This is calculated from the LLVM and GCC tables and verified
2382 // against gcc output. AFAIK all ABIs use the same encoding.
2383
2384 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallea8d8bb2010-03-11 00:10:12 +00002385
Chris Lattnerece04092012-02-07 00:39:47 +00002386 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallea8d8bb2010-03-11 00:10:12 +00002387 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2388 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2389 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2390
2391 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002392 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002393
2394 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002395 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002396
2397 // 64-76 are various 4-byte special-purpose registers:
2398 // 64: mq
2399 // 65: lr
2400 // 66: ctr
2401 // 67: ap
2402 // 68-75 cr0-7
2403 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002404 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002405
2406 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002407 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002408
2409 // 109: vrsave
2410 // 110: vscr
2411 // 111: spe_acc
2412 // 112: spefscr
2413 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002414 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002415
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002416 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002417}
2418
Roman Divackyd966e722012-05-09 18:22:46 +00002419// PowerPC-64
2420
2421namespace {
2422class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2423public:
2424 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2425
2426 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2427 // This is recovered from gcc output.
2428 return 1; // r1 is the dedicated stack pointer
2429 }
2430
2431 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2432 llvm::Value *Address) const;
2433};
2434
2435}
2436
2437bool
2438PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2439 llvm::Value *Address) const {
2440 // This is calculated from the LLVM and GCC tables and verified
2441 // against gcc output. AFAIK all ABIs use the same encoding.
2442
2443 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2444
2445 llvm::IntegerType *i8 = CGF.Int8Ty;
2446 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2447 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2448 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2449
2450 // 0-31: r0-31, the 8-byte general-purpose registers
2451 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2452
2453 // 32-63: fp0-31, the 8-byte floating-point registers
2454 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2455
2456 // 64-76 are various 4-byte special-purpose registers:
2457 // 64: mq
2458 // 65: lr
2459 // 66: ctr
2460 // 67: ap
2461 // 68-75 cr0-7
2462 // 76: xer
2463 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2464
2465 // 77-108: v0-31, the 16-byte vector registers
2466 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2467
2468 // 109: vrsave
2469 // 110: vscr
2470 // 111: spe_acc
2471 // 112: spefscr
2472 // 113: sfp
2473 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2474
2475 return false;
2476}
John McCallea8d8bb2010-03-11 00:10:12 +00002477
Chris Lattner0cf24192010-06-28 20:05:43 +00002478//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002479// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002480//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002481
2482namespace {
2483
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002484class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002485public:
2486 enum ABIKind {
2487 APCS = 0,
2488 AAPCS = 1,
2489 AAPCS_VFP
2490 };
2491
2492private:
2493 ABIKind Kind;
2494
2495public:
Chris Lattner2b037972010-07-29 02:01:43 +00002496 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002497
John McCall3480ef22011-08-30 01:42:09 +00002498 bool isEABI() const {
Eli Friedmana98d1f82012-01-25 22:46:34 +00002499 StringRef Env =
2500 getContext().getTargetInfo().getTriple().getEnvironmentName();
Chandler Carruthc89aa9d2012-01-10 19:47:42 +00002501 return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
John McCall3480ef22011-08-30 01:42:09 +00002502 }
2503
Daniel Dunbar020daa92009-09-12 01:00:39 +00002504private:
2505 ABIKind getABIKind() const { return Kind; }
2506
Chris Lattner458b2aa2010-07-29 02:16:43 +00002507 ABIArgInfo classifyReturnType(QualType RetTy) const;
2508 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002509
Chris Lattner22326a12010-07-29 02:31:05 +00002510 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002511
2512 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2513 CodeGenFunction &CGF) const;
2514};
2515
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002516class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2517public:
Chris Lattner2b037972010-07-29 02:01:43 +00002518 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2519 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002520
John McCall3480ef22011-08-30 01:42:09 +00002521 const ARMABIInfo &getABIInfo() const {
2522 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2523 }
2524
John McCallbeec5a02010-03-06 00:35:14 +00002525 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2526 return 13;
2527 }
Roman Divackyc1617352011-05-18 19:36:54 +00002528
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002529 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00002530 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2531 }
2532
Roman Divackyc1617352011-05-18 19:36:54 +00002533 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2534 llvm::Value *Address) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002535 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divackyc1617352011-05-18 19:36:54 +00002536
2537 // 0-15 are the 16 integer registers.
Chris Lattnerece04092012-02-07 00:39:47 +00002538 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divackyc1617352011-05-18 19:36:54 +00002539 return false;
2540 }
John McCall3480ef22011-08-30 01:42:09 +00002541
2542 unsigned getSizeOfUnwindException() const {
2543 if (getABIInfo().isEABI()) return 88;
2544 return TargetCodeGenInfo::getSizeOfUnwindException();
2545 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002546};
2547
Daniel Dunbard59655c2009-09-12 00:59:49 +00002548}
2549
Chris Lattner22326a12010-07-29 02:31:05 +00002550void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002551 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002552 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002553 it != ie; ++it)
2554 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002555
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002556 // Always honor user-specified calling convention.
2557 if (FI.getCallingConvention() != llvm::CallingConv::C)
2558 return;
2559
2560 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002561 llvm::CallingConv::ID DefaultCC;
John McCall3480ef22011-08-30 01:42:09 +00002562 if (isEABI())
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002563 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002564 else
2565 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002566
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002567 // If user did not ask for specific calling convention explicitly (e.g. via
2568 // pcs attribute), set effective calling convention if it's different than ABI
2569 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002570 switch (getABIKind()) {
2571 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002572 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2573 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002574 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002575 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002576 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2577 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002578 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002579 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002580 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2581 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002582 break;
2583 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002584}
2585
Bob Wilsone826a2a2011-08-03 05:58:22 +00002586/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2587/// aggregate. If HAMembers is non-null, the number of base elements
2588/// contained in the type is returned through it; this is used for the
2589/// recursive calls that check aggregate component types.
2590static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2591 ASTContext &Context,
2592 uint64_t *HAMembers = 0) {
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002593 uint64_t Members = 0;
Bob Wilsone826a2a2011-08-03 05:58:22 +00002594 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2595 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2596 return false;
2597 Members *= AT->getSize().getZExtValue();
2598 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2599 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002600 if (RD->hasFlexibleArrayMember())
Bob Wilsone826a2a2011-08-03 05:58:22 +00002601 return false;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002602
Bob Wilsone826a2a2011-08-03 05:58:22 +00002603 Members = 0;
2604 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2605 i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00002606 const FieldDecl *FD = *i;
Bob Wilsone826a2a2011-08-03 05:58:22 +00002607 uint64_t FldMembers;
2608 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2609 return false;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002610
2611 Members = (RD->isUnion() ?
2612 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilsone826a2a2011-08-03 05:58:22 +00002613 }
2614 } else {
2615 Members = 1;
2616 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2617 Members = 2;
2618 Ty = CT->getElementType();
2619 }
2620
2621 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2622 // double, or 64-bit or 128-bit vectors.
2623 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2624 if (BT->getKind() != BuiltinType::Float &&
2625 BT->getKind() != BuiltinType::Double)
2626 return false;
2627 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2628 unsigned VecSize = Context.getTypeSize(VT);
2629 if (VecSize != 64 && VecSize != 128)
2630 return false;
2631 } else {
2632 return false;
2633 }
2634
2635 // The base type must be the same for all members. Vector types of the
2636 // same total size are treated as being equivalent here.
2637 const Type *TyPtr = Ty.getTypePtr();
2638 if (!Base)
2639 Base = TyPtr;
2640 if (Base != TyPtr &&
2641 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2642 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2643 return false;
2644 }
2645
2646 // Homogeneous Aggregates can have at most 4 members of the base type.
2647 if (HAMembers)
2648 *HAMembers = Members;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002649
2650 return (Members > 0 && Members <= 4);
Bob Wilsone826a2a2011-08-03 05:58:22 +00002651}
2652
Chris Lattner458b2aa2010-07-29 02:16:43 +00002653ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002654 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002655 // Treat an enum type as its underlying type.
2656 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2657 Ty = EnumTy->getDecl()->getIntegerType();
2658
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002659 return (Ty->isPromotableIntegerType() ?
2660 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002661 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002662
Daniel Dunbar09d33622009-09-14 21:54:03 +00002663 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002664 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002665 return ABIArgInfo::getIgnore();
2666
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002667 // Structures with either a non-trivial destructor or a non-trivial
2668 // copy constructor are always indirect.
2669 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2670 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2671
Bob Wilsone826a2a2011-08-03 05:58:22 +00002672 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2673 // Homogeneous Aggregates need to be expanded.
2674 const Type *Base = 0;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002675 if (isHomogeneousAggregate(Ty, Base, getContext())) {
2676 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilsone826a2a2011-08-03 05:58:22 +00002677 return ABIArgInfo::getExpand();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002678 }
Bob Wilsone826a2a2011-08-03 05:58:22 +00002679 }
2680
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002681 // Otherwise, pass by coercing to a structure of the appropriate size.
2682 //
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002683 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2192fe52011-07-18 04:24:23 +00002684 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002685 unsigned SizeRegs;
Manman Ren6fdb1582012-06-25 22:04:00 +00002686 if (getContext().getTypeSizeInChars(Ty) <= CharUnits::fromQuantity(64)) {
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002687 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2688 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren6fdb1582012-06-25 22:04:00 +00002689 } else if (getABIKind() == ARMABIInfo::APCS) {
2690 // Initial ARM ByVal support is APCS-only.
2691 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2692 } else {
2693 // FIXME: This is kind of nasty... but there isn't much choice
2694 // because most of the ARM calling conventions don't yet support
2695 // byval.
2696 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2697 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002698 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002699
Chris Lattnera5f58b02011-07-09 17:41:47 +00002700 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002701 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002702 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002703}
2704
Chris Lattner458b2aa2010-07-29 02:16:43 +00002705static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002706 llvm::LLVMContext &VMContext) {
2707 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2708 // is called integer-like if its size is less than or equal to one word, and
2709 // the offset of each of its addressable sub-fields is zero.
2710
2711 uint64_t Size = Context.getTypeSize(Ty);
2712
2713 // Check that the type fits in a word.
2714 if (Size > 32)
2715 return false;
2716
2717 // FIXME: Handle vector types!
2718 if (Ty->isVectorType())
2719 return false;
2720
Daniel Dunbard53bac72009-09-14 02:20:34 +00002721 // Float types are never treated as "integer like".
2722 if (Ty->isRealFloatingType())
2723 return false;
2724
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002725 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002726 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002727 return true;
2728
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002729 // Small complex integer types are "integer like".
2730 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2731 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002732
2733 // Single element and zero sized arrays should be allowed, by the definition
2734 // above, but they are not.
2735
2736 // Otherwise, it must be a record type.
2737 const RecordType *RT = Ty->getAs<RecordType>();
2738 if (!RT) return false;
2739
2740 // Ignore records with flexible arrays.
2741 const RecordDecl *RD = RT->getDecl();
2742 if (RD->hasFlexibleArrayMember())
2743 return false;
2744
2745 // Check that all sub-fields are at offset 0, and are themselves "integer
2746 // like".
2747 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2748
2749 bool HadField = false;
2750 unsigned idx = 0;
2751 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2752 i != e; ++i, ++idx) {
David Blaikie40ed2972012-06-06 20:45:41 +00002753 const FieldDecl *FD = *i;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002754
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002755 // Bit-fields are not addressable, we only need to verify they are "integer
2756 // like". We still have to disallow a subsequent non-bitfield, for example:
2757 // struct { int : 0; int x }
2758 // is non-integer like according to gcc.
2759 if (FD->isBitField()) {
2760 if (!RD->isUnion())
2761 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002762
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002763 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2764 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002765
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002766 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002767 }
2768
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002769 // Check if this field is at offset 0.
2770 if (Layout.getFieldOffset(idx) != 0)
2771 return false;
2772
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002773 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2774 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002775
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002776 // Only allow at most one field in a structure. This doesn't match the
2777 // wording above, but follows gcc in situations with a field following an
2778 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002779 if (!RD->isUnion()) {
2780 if (HadField)
2781 return false;
2782
2783 HadField = true;
2784 }
2785 }
2786
2787 return true;
2788}
2789
Chris Lattner458b2aa2010-07-29 02:16:43 +00002790ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002791 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002792 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002793
Daniel Dunbar19964db2010-09-23 01:54:32 +00002794 // Large vector types should be returned via memory.
2795 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2796 return ABIArgInfo::getIndirect(0);
2797
John McCalla1dee5302010-08-22 10:59:02 +00002798 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002799 // Treat an enum type as its underlying type.
2800 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2801 RetTy = EnumTy->getDecl()->getIntegerType();
2802
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002803 return (RetTy->isPromotableIntegerType() ?
2804 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002805 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002806
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002807 // Structures with either a non-trivial destructor or a non-trivial
2808 // copy constructor are always indirect.
2809 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2810 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2811
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002812 // Are we following APCS?
2813 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002814 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002815 return ABIArgInfo::getIgnore();
2816
Daniel Dunbareedf1512010-02-01 23:31:19 +00002817 // Complex types are all returned as packed integers.
2818 //
2819 // FIXME: Consider using 2 x vector types if the back end handles them
2820 // correctly.
2821 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002822 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002823 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002824
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002825 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002826 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002827 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002828 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002829 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002830 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002831 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002832 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2833 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002834 }
2835
2836 // Otherwise return in memory.
2837 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002838 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002839
2840 // Otherwise this is an AAPCS variant.
2841
Chris Lattner458b2aa2010-07-29 02:16:43 +00002842 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002843 return ABIArgInfo::getIgnore();
2844
Bob Wilson1d9269a2011-11-02 04:51:36 +00002845 // Check for homogeneous aggregates with AAPCS-VFP.
2846 if (getABIKind() == AAPCS_VFP) {
2847 const Type *Base = 0;
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002848 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
2849 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson1d9269a2011-11-02 04:51:36 +00002850 // Homogeneous Aggregates are returned directly.
2851 return ABIArgInfo::getDirect();
Anton Korobeynikov4215ca72012-04-13 11:22:00 +00002852 }
Bob Wilson1d9269a2011-11-02 04:51:36 +00002853 }
2854
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002855 // Aggregates <= 4 bytes are returned in r0; other aggregates
2856 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002857 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002858 if (Size <= 32) {
2859 // Return in the smallest viable integer type.
2860 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002861 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002862 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002863 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2864 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002865 }
2866
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002867 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002868}
2869
2870llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002871 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00002872 llvm::Type *BP = CGF.Int8PtrTy;
2873 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002874
2875 CGBuilderTy &Builder = CGF.Builder;
Chris Lattnerece04092012-02-07 00:39:47 +00002876 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002877 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindola11d994b2011-08-02 22:33:37 +00002878 // Handle address alignment for type alignment > 32 bits
2879 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2880 if (TyAlign > 4) {
2881 assert((TyAlign & (TyAlign - 1)) == 0 &&
2882 "Alignment is not power of 2!");
2883 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2884 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2885 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2886 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2887 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002888 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002889 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002890 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2891
2892 uint64_t Offset =
2893 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2894 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002895 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002896 "ap.next");
2897 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2898
2899 return AddrTyped;
2900}
2901
Chris Lattner0cf24192010-06-28 20:05:43 +00002902//===----------------------------------------------------------------------===//
Justin Holewinski83e96682012-05-24 17:43:12 +00002903// NVPTX ABI Implementation
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002904//===----------------------------------------------------------------------===//
2905
2906namespace {
2907
Justin Holewinski83e96682012-05-24 17:43:12 +00002908class NVPTXABIInfo : public ABIInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002909public:
Justin Holewinski83e96682012-05-24 17:43:12 +00002910 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002911
2912 ABIArgInfo classifyReturnType(QualType RetTy) const;
2913 ABIArgInfo classifyArgumentType(QualType Ty) const;
2914
2915 virtual void computeInfo(CGFunctionInfo &FI) const;
2916 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2917 CodeGenFunction &CFG) const;
2918};
2919
Justin Holewinski83e96682012-05-24 17:43:12 +00002920class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002921public:
Justin Holewinski83e96682012-05-24 17:43:12 +00002922 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
2923 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00002924
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00002925 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2926 CodeGen::CodeGenModule &M) const;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002927};
2928
Justin Holewinski83e96682012-05-24 17:43:12 +00002929ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002930 if (RetTy->isVoidType())
2931 return ABIArgInfo::getIgnore();
2932 if (isAggregateTypeForABI(RetTy))
2933 return ABIArgInfo::getIndirect(0);
2934 return ABIArgInfo::getDirect();
2935}
2936
Justin Holewinski83e96682012-05-24 17:43:12 +00002937ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002938 if (isAggregateTypeForABI(Ty))
2939 return ABIArgInfo::getIndirect(0);
2940
2941 return ABIArgInfo::getDirect();
2942}
2943
Justin Holewinski83e96682012-05-24 17:43:12 +00002944void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002945 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2946 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2947 it != ie; ++it)
2948 it->info = classifyArgumentType(it->type);
2949
2950 // Always honor user-specified calling convention.
2951 if (FI.getCallingConvention() != llvm::CallingConv::C)
2952 return;
2953
2954 // Calling convention as default by an ABI.
Justin Holewinski83e96682012-05-24 17:43:12 +00002955 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
2956 // but we should switch to NVVM metadata later on.
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002957 llvm::CallingConv::ID DefaultCC;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002958 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002959 if (LangOpts.OpenCL || LangOpts.CUDA) {
2960 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002961 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski38031972011-10-05 17:58:44 +00002962 } else {
2963 // If we are in standard C/C++ mode, use the triple to decide on the default
2964 StringRef Env =
2965 getContext().getTargetInfo().getTriple().getEnvironmentName();
2966 if (Env == "device")
2967 DefaultCC = llvm::CallingConv::PTX_Device;
2968 else
2969 DefaultCC = llvm::CallingConv::PTX_Kernel;
2970 }
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002971 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski38031972011-10-05 17:58:44 +00002972
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002973}
2974
Justin Holewinski83e96682012-05-24 17:43:12 +00002975llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2976 CodeGenFunction &CFG) const {
2977 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002978}
2979
Justin Holewinski83e96682012-05-24 17:43:12 +00002980void NVPTXTargetCodeGenInfo::
2981SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2982 CodeGen::CodeGenModule &M) const{
Justin Holewinski38031972011-10-05 17:58:44 +00002983 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2984 if (!FD) return;
2985
2986 llvm::Function *F = cast<llvm::Function>(GV);
2987
2988 // Perform special handling in OpenCL mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00002989 if (M.getLangOpts().OpenCL) {
Justin Holewinski38031972011-10-05 17:58:44 +00002990 // Use OpenCL function attributes to set proper calling conventions
2991 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00002992 if (FD->hasAttr<OpenCLKernelAttr>()) {
2993 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002994 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002995 // And kernel functions are not subject to inlining
2996 F->addFnAttr(llvm::Attribute::NoInline);
2997 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002998 }
Justin Holewinski38031972011-10-05 17:58:44 +00002999
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00003000 // Perform special handling in CUDA mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003001 if (M.getLangOpts().CUDA) {
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00003002 // CUDA __global__ functions get a kernel calling convention. Since
3003 // __global__ functions cannot be called from the device, we do not
3004 // need to set the noinline attribute.
3005 if (FD->getAttr<CUDAGlobalAttr>())
3006 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00003007 }
3008}
3009
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003010}
3011
3012//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00003013// MBlaze ABI Implementation
3014//===----------------------------------------------------------------------===//
3015
3016namespace {
3017
3018class MBlazeABIInfo : public ABIInfo {
3019public:
3020 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3021
3022 bool isPromotableIntegerType(QualType Ty) const;
3023
3024 ABIArgInfo classifyReturnType(QualType RetTy) const;
3025 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3026
3027 virtual void computeInfo(CGFunctionInfo &FI) const {
3028 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3029 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3030 it != ie; ++it)
3031 it->info = classifyArgumentType(it->type);
3032 }
3033
3034 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3035 CodeGenFunction &CGF) const;
3036};
3037
3038class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3039public:
3040 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3041 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3042 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3043 CodeGen::CodeGenModule &M) const;
3044};
3045
3046}
3047
3048bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3049 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3050 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3051 switch (BT->getKind()) {
3052 case BuiltinType::Bool:
3053 case BuiltinType::Char_S:
3054 case BuiltinType::Char_U:
3055 case BuiltinType::SChar:
3056 case BuiltinType::UChar:
3057 case BuiltinType::Short:
3058 case BuiltinType::UShort:
3059 return true;
3060 default:
3061 return false;
3062 }
3063 return false;
3064}
3065
3066llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3067 CodeGenFunction &CGF) const {
3068 // FIXME: Implement
3069 return 0;
3070}
3071
3072
3073ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3074 if (RetTy->isVoidType())
3075 return ABIArgInfo::getIgnore();
3076 if (isAggregateTypeForABI(RetTy))
3077 return ABIArgInfo::getIndirect(0);
3078
3079 return (isPromotableIntegerType(RetTy) ?
3080 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3081}
3082
3083ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3084 if (isAggregateTypeForABI(Ty))
3085 return ABIArgInfo::getIndirect(0);
3086
3087 return (isPromotableIntegerType(Ty) ?
3088 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3089}
3090
3091void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3092 llvm::GlobalValue *GV,
3093 CodeGen::CodeGenModule &M)
3094 const {
3095 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3096 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00003097
Wesley Peck36a1f682010-12-19 19:57:51 +00003098 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3099 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3100 CC = llvm::CallingConv::MBLAZE_INTR;
3101 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3102 CC = llvm::CallingConv::MBLAZE_SVOL;
3103
3104 if (CC != llvm::CallingConv::C) {
3105 // Handle 'interrupt_handler' attribute:
3106 llvm::Function *F = cast<llvm::Function>(GV);
3107
3108 // Step 1: Set ISR calling convention.
3109 F->setCallingConv(CC);
3110
3111 // Step 2: Add attributes goodness.
3112 F->addFnAttr(llvm::Attribute::NoInline);
3113 }
3114
3115 // Step 3: Emit _interrupt_handler alias.
3116 if (CC == llvm::CallingConv::MBLAZE_INTR)
3117 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3118 "_interrupt_handler", GV, &M.getModule());
3119}
3120
3121
3122//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003123// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00003124//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003125
3126namespace {
3127
3128class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3129public:
Chris Lattner2b037972010-07-29 02:01:43 +00003130 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3131 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003132 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3133 CodeGen::CodeGenModule &M) const;
3134};
3135
3136}
3137
3138void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3139 llvm::GlobalValue *GV,
3140 CodeGen::CodeGenModule &M) const {
3141 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3142 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3143 // Handle 'interrupt' attribute:
3144 llvm::Function *F = cast<llvm::Function>(GV);
3145
3146 // Step 1: Set ISR calling convention.
3147 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3148
3149 // Step 2: Add attributes goodness.
3150 F->addFnAttr(llvm::Attribute::NoInline);
3151
3152 // Step 3: Emit ISR vector alias.
3153 unsigned Num = attr->getNumber() + 0xffe0;
3154 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003155 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003156 GV, &M.getModule());
3157 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003158 }
3159}
3160
Chris Lattner0cf24192010-06-28 20:05:43 +00003161//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00003162// MIPS ABI Implementation. This works for both little-endian and
3163// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00003164//===----------------------------------------------------------------------===//
3165
John McCall943fae92010-05-27 06:19:26 +00003166namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00003167class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00003168 bool IsO32;
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003169 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3170 void CoerceToIntArgs(uint64_t TySize,
3171 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003172 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003173 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka1632af62012-01-09 19:31:25 +00003174 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003175public:
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00003176 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003177 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3178 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00003179
3180 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003181 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003182 virtual void computeInfo(CGFunctionInfo &FI) const;
3183 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3184 CodeGenFunction &CGF) const;
3185};
3186
John McCall943fae92010-05-27 06:19:26 +00003187class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003188 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00003189public:
Akira Hatanaka14378522011-11-02 23:14:57 +00003190 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3191 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3192 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00003193
3194 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3195 return 29;
3196 }
3197
3198 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003199 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00003200
3201 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003202 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00003203 }
John McCall943fae92010-05-27 06:19:26 +00003204};
3205}
3206
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003207void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3208 SmallVector<llvm::Type*, 8> &ArgList) const {
3209 llvm::IntegerType *IntTy =
3210 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003211
3212 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3213 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3214 ArgList.push_back(IntTy);
3215
3216 // If necessary, add one more integer type to ArgList.
3217 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3218
3219 if (R)
3220 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003221}
3222
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003223// In N32/64, an aligned double precision floating point field is passed in
3224// a register.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003225llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003226 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3227
3228 if (IsO32) {
3229 CoerceToIntArgs(TySize, ArgList);
3230 return llvm::StructType::get(getVMContext(), ArgList);
3231 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003232
Akira Hatanaka02e13e52012-01-12 00:52:17 +00003233 if (Ty->isComplexType())
3234 return CGT.ConvertType(Ty);
Akira Hatanaka79f04612012-01-10 23:12:19 +00003235
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00003236 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003237
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003238 // Unions/vectors are passed in integer registers.
3239 if (!RT || !RT->isStructureOrClassType()) {
3240 CoerceToIntArgs(TySize, ArgList);
3241 return llvm::StructType::get(getVMContext(), ArgList);
3242 }
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003243
3244 const RecordDecl *RD = RT->getDecl();
3245 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003246 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003247
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003248 uint64_t LastOffset = 0;
3249 unsigned idx = 0;
3250 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3251
Akira Hatanaka4984f5d2012-02-09 19:54:16 +00003252 // Iterate over fields in the struct/class and check if there are any aligned
3253 // double fields.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003254 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3255 i != e; ++i, ++idx) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00003256 const QualType Ty = i->getType();
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003257 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3258
3259 if (!BT || BT->getKind() != BuiltinType::Double)
3260 continue;
3261
3262 uint64_t Offset = Layout.getFieldOffset(idx);
3263 if (Offset % 64) // Ignore doubles that are not aligned.
3264 continue;
3265
3266 // Add ((Offset - LastOffset) / 64) args of type i64.
3267 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3268 ArgList.push_back(I64);
3269
3270 // Add double type.
3271 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3272 LastOffset = Offset + 64;
3273 }
3274
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003275 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3276 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003277
3278 return llvm::StructType::get(getVMContext(), ArgList);
3279}
3280
Akira Hatanaka1632af62012-01-09 19:31:25 +00003281llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003282 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanaka1632af62012-01-09 19:31:25 +00003283
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003284 if ((Align - 1) & Offset)
3285 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3286
3287 return 0;
Akira Hatanaka1632af62012-01-09 19:31:25 +00003288}
Akira Hatanaka21ee88c2012-01-10 22:44:52 +00003289
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003290ABIArgInfo
3291MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanaka1632af62012-01-09 19:31:25 +00003292 uint64_t OrigOffset = Offset;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003293 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanaka1632af62012-01-09 19:31:25 +00003294 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003295
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003296 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3297 (uint64_t)StackAlignInBytes);
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003298 Offset = llvm::RoundUpToAlignment(Offset, Align);
3299 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanaka1632af62012-01-09 19:31:25 +00003300
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003301 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanakab579fe52011-06-02 00:09:17 +00003302 // Ignore empty aggregates.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003303 if (TySize == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00003304 return ABIArgInfo::getIgnore();
3305
Akira Hatanakadf425db2011-08-01 18:09:58 +00003306 // Records with non trivial destructors/constructors should not be passed
3307 // by value.
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003308 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003309 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanakadf425db2011-08-01 18:09:58 +00003310 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003311 }
Akira Hatanakadf425db2011-08-01 18:09:58 +00003312
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003313 // If we have reached here, aggregates are passed directly by coercing to
3314 // another structure type. Padding is inserted if the offset of the
3315 // aggregate is unaligned.
3316 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3317 getPaddingType(Align, OrigOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00003318 }
3319
3320 // Treat an enum type as its underlying type.
3321 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3322 Ty = EnumTy->getDecl()->getIntegerType();
3323
Akira Hatanaka1632af62012-01-09 19:31:25 +00003324 if (Ty->isPromotableIntegerType())
3325 return ABIArgInfo::getExtend();
3326
3327 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanakab579fe52011-06-02 00:09:17 +00003328}
3329
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003330llvm::Type*
3331MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakab6f74432012-02-09 18:49:26 +00003332 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003333 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003334
Akira Hatanakab6f74432012-02-09 18:49:26 +00003335 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003336 const RecordDecl *RD = RT->getDecl();
Akira Hatanakab6f74432012-02-09 18:49:26 +00003337 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3338 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003339
Akira Hatanakab6f74432012-02-09 18:49:26 +00003340 // N32/64 returns struct/classes in floating point registers if the
3341 // following conditions are met:
3342 // 1. The size of the struct/class is no larger than 128-bit.
3343 // 2. The struct/class has one or two fields all of which are floating
3344 // point types.
3345 // 3. The offset of the first field is zero (this follows what gcc does).
3346 //
3347 // Any other composite results are returned in integer registers.
3348 //
3349 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3350 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3351 for (; b != e; ++b) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00003352 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003353
Akira Hatanakab6f74432012-02-09 18:49:26 +00003354 if (!BT || !BT->isFloatingPoint())
3355 break;
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003356
David Blaikie2d7c57e2012-04-30 02:36:29 +00003357 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakab6f74432012-02-09 18:49:26 +00003358 }
3359
3360 if (b == e)
3361 return llvm::StructType::get(getVMContext(), RTList,
3362 RD->hasAttr<PackedAttr>());
3363
3364 RTList.clear();
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003365 }
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003366 }
3367
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003368 CoerceToIntArgs(Size, RTList);
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003369 return llvm::StructType::get(getVMContext(), RTList);
3370}
3371
Akira Hatanakab579fe52011-06-02 00:09:17 +00003372ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanaka60f5fe62012-01-23 23:18:57 +00003373 uint64_t Size = getContext().getTypeSize(RetTy);
3374
3375 if (RetTy->isVoidType() || Size == 0)
Akira Hatanakab579fe52011-06-02 00:09:17 +00003376 return ABIArgInfo::getIgnore();
3377
Akira Hatanakac37eddf2012-05-11 21:01:17 +00003378 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003379 if (Size <= 128) {
3380 if (RetTy->isAnyComplexType())
3381 return ABIArgInfo::getDirect();
3382
Akira Hatanakae1e3ad32012-07-03 19:24:06 +00003383 // O32 returns integer vectors in registers.
3384 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3385 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3386
Akira Hatanakac07c4652012-02-08 01:31:22 +00003387 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakaf093f5b2012-01-04 03:34:42 +00003388 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3389 }
Akira Hatanakab579fe52011-06-02 00:09:17 +00003390
3391 return ABIArgInfo::getIndirect(0);
3392 }
3393
3394 // Treat an enum type as its underlying type.
3395 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3396 RetTy = EnumTy->getDecl()->getIntegerType();
3397
3398 return (RetTy->isPromotableIntegerType() ?
3399 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3400}
3401
3402void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanaka32604a92012-01-12 01:10:09 +00003403 ABIArgInfo &RetInfo = FI.getReturnInfo();
3404 RetInfo = classifyReturnType(FI.getReturnType());
3405
3406 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka8ab86cb2012-05-11 21:56:58 +00003407 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanaka32604a92012-01-12 01:10:09 +00003408
Akira Hatanakab579fe52011-06-02 00:09:17 +00003409 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3410 it != ie; ++it)
Akira Hatanakaf64e1ad2012-01-07 00:25:33 +00003411 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanakab579fe52011-06-02 00:09:17 +00003412}
3413
3414llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3415 CodeGenFunction &CGF) const {
Chris Lattnerece04092012-02-07 00:39:47 +00003416 llvm::Type *BP = CGF.Int8PtrTy;
3417 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003418
3419 CGBuilderTy &Builder = CGF.Builder;
3420 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3421 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka37715282012-01-23 23:59:52 +00003422 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003423 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3424 llvm::Value *AddrTyped;
Akira Hatanaka37715282012-01-23 23:59:52 +00003425 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3426 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003427
3428 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka37715282012-01-23 23:59:52 +00003429 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3430 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3431 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3432 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003433 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3434 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3435 }
3436 else
3437 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3438
3439 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka37715282012-01-23 23:59:52 +00003440 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003441 uint64_t Offset =
3442 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3443 llvm::Value *NextAddr =
Akira Hatanaka37715282012-01-23 23:59:52 +00003444 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003445 "ap.next");
3446 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3447
3448 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003449}
3450
John McCall943fae92010-05-27 06:19:26 +00003451bool
3452MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3453 llvm::Value *Address) const {
3454 // This information comes from gcc's implementation, which seems to
3455 // as canonical as it gets.
3456
John McCall943fae92010-05-27 06:19:26 +00003457 // Everything on MIPS is 4 bytes. Double-precision FP registers
3458 // are aliased to pairs of single-precision FP registers.
Chris Lattnerece04092012-02-07 00:39:47 +00003459 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCall943fae92010-05-27 06:19:26 +00003460
3461 // 0-31 are the general purpose registers, $0 - $31.
3462 // 32-63 are the floating-point registers, $f0 - $f31.
3463 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3464 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattnerece04092012-02-07 00:39:47 +00003465 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCall943fae92010-05-27 06:19:26 +00003466
3467 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3468 // They are one bit wide and ignored here.
3469
3470 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3471 // (coprocessor 1 is the FP unit)
3472 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3473 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3474 // 176-181 are the DSP accumulator registers.
Chris Lattnerece04092012-02-07 00:39:47 +00003475 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCall943fae92010-05-27 06:19:26 +00003476 return false;
3477}
3478
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003479//===----------------------------------------------------------------------===//
3480// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3481// Currently subclassed only to implement custom OpenCL C function attribute
3482// handling.
3483//===----------------------------------------------------------------------===//
3484
3485namespace {
3486
3487class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3488public:
3489 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3490 : DefaultTargetCodeGenInfo(CGT) {}
3491
3492 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3493 CodeGen::CodeGenModule &M) const;
3494};
3495
3496void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3497 llvm::GlobalValue *GV,
3498 CodeGen::CodeGenModule &M) const {
3499 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3500 if (!FD) return;
3501
3502 llvm::Function *F = cast<llvm::Function>(GV);
3503
David Blaikiebbafb8a2012-03-11 07:00:24 +00003504 if (M.getLangOpts().OpenCL) {
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003505 if (FD->hasAttr<OpenCLKernelAttr>()) {
3506 // OpenCL C Kernel functions are not subject to inlining
3507 F->addFnAttr(llvm::Attribute::NoInline);
3508
3509 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3510
3511 // Convert the reqd_work_group_size() attributes to metadata.
3512 llvm::LLVMContext &Context = F->getContext();
3513 llvm::NamedMDNode *OpenCLMetadata =
3514 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3515
3516 SmallVector<llvm::Value*, 5> Operands;
3517 Operands.push_back(F);
3518
Chris Lattnerece04092012-02-07 00:39:47 +00003519 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3520 llvm::APInt(32,
3521 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3522 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3523 llvm::APInt(32,
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003524 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattnerece04092012-02-07 00:39:47 +00003525 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3526 llvm::APInt(32,
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003527 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3528
3529 // Add a boolean constant operand for "required" (true) or "hint" (false)
3530 // for implementing the work_group_size_hint attr later. Currently
3531 // always true as the hint is not yet implemented.
Chris Lattnerece04092012-02-07 00:39:47 +00003532 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003533 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3534 }
3535 }
3536 }
3537}
3538
3539}
John McCall943fae92010-05-27 06:19:26 +00003540
Tony Linthicum76329bf2011-12-12 21:14:55 +00003541//===----------------------------------------------------------------------===//
3542// Hexagon ABI Implementation
3543//===----------------------------------------------------------------------===//
3544
3545namespace {
3546
3547class HexagonABIInfo : public ABIInfo {
3548
3549
3550public:
3551 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3552
3553private:
3554
3555 ABIArgInfo classifyReturnType(QualType RetTy) const;
3556 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3557
3558 virtual void computeInfo(CGFunctionInfo &FI) const;
3559
3560 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3561 CodeGenFunction &CGF) const;
3562};
3563
3564class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3565public:
3566 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3567 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3568
3569 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3570 return 29;
3571 }
3572};
3573
3574}
3575
3576void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3577 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3578 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3579 it != ie; ++it)
3580 it->info = classifyArgumentType(it->type);
3581}
3582
3583ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3584 if (!isAggregateTypeForABI(Ty)) {
3585 // Treat an enum type as its underlying type.
3586 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3587 Ty = EnumTy->getDecl()->getIntegerType();
3588
3589 return (Ty->isPromotableIntegerType() ?
3590 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3591 }
3592
3593 // Ignore empty records.
3594 if (isEmptyRecord(getContext(), Ty, true))
3595 return ABIArgInfo::getIgnore();
3596
3597 // Structures with either a non-trivial destructor or a non-trivial
3598 // copy constructor are always indirect.
3599 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3600 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3601
3602 uint64_t Size = getContext().getTypeSize(Ty);
3603 if (Size > 64)
3604 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3605 // Pass in the smallest viable integer type.
3606 else if (Size > 32)
3607 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3608 else if (Size > 16)
3609 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3610 else if (Size > 8)
3611 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3612 else
3613 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3614}
3615
3616ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3617 if (RetTy->isVoidType())
3618 return ABIArgInfo::getIgnore();
3619
3620 // Large vector types should be returned via memory.
3621 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3622 return ABIArgInfo::getIndirect(0);
3623
3624 if (!isAggregateTypeForABI(RetTy)) {
3625 // Treat an enum type as its underlying type.
3626 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3627 RetTy = EnumTy->getDecl()->getIntegerType();
3628
3629 return (RetTy->isPromotableIntegerType() ?
3630 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3631 }
3632
3633 // Structures with either a non-trivial destructor or a non-trivial
3634 // copy constructor are always indirect.
3635 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3636 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3637
3638 if (isEmptyRecord(getContext(), RetTy, true))
3639 return ABIArgInfo::getIgnore();
3640
3641 // Aggregates <= 8 bytes are returned in r0; other aggregates
3642 // are returned indirectly.
3643 uint64_t Size = getContext().getTypeSize(RetTy);
3644 if (Size <= 64) {
3645 // Return in the smallest viable integer type.
3646 if (Size <= 8)
3647 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3648 if (Size <= 16)
3649 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3650 if (Size <= 32)
3651 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3652 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3653 }
3654
3655 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3656}
3657
3658llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattnerece04092012-02-07 00:39:47 +00003659 CodeGenFunction &CGF) const {
Tony Linthicum76329bf2011-12-12 21:14:55 +00003660 // FIXME: Need to handle alignment
Chris Lattnerece04092012-02-07 00:39:47 +00003661 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum76329bf2011-12-12 21:14:55 +00003662
3663 CGBuilderTy &Builder = CGF.Builder;
3664 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3665 "ap");
3666 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3667 llvm::Type *PTy =
3668 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3669 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3670
3671 uint64_t Offset =
3672 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3673 llvm::Value *NextAddr =
3674 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3675 "ap.next");
3676 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3677
3678 return AddrTyped;
3679}
3680
3681
Chris Lattner2b037972010-07-29 02:01:43 +00003682const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003683 if (TheTargetCodeGenInfo)
3684 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003685
Douglas Gregore8bbc122011-09-02 00:18:52 +00003686 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00003687 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003688 default:
Chris Lattner2b037972010-07-29 02:01:43 +00003689 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003690
John McCall943fae92010-05-27 06:19:26 +00003691 case llvm::Triple::mips:
3692 case llvm::Triple::mipsel:
Akira Hatanaka14378522011-11-02 23:14:57 +00003693 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCall943fae92010-05-27 06:19:26 +00003694
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003695 case llvm::Triple::mips64:
3696 case llvm::Triple::mips64el:
Akira Hatanaka14378522011-11-02 23:14:57 +00003697 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003698
Daniel Dunbard59655c2009-09-12 00:59:49 +00003699 case llvm::Triple::arm:
3700 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003701 {
3702 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003703
Douglas Gregore8bbc122011-09-02 00:18:52 +00003704 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003705 Kind = ARMABIInfo::APCS;
3706 else if (CodeGenOpts.FloatABI == "hard")
3707 Kind = ARMABIInfo::AAPCS_VFP;
3708
3709 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3710 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00003711
John McCallea8d8bb2010-03-11 00:10:12 +00003712 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00003713 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divackyd966e722012-05-09 18:22:46 +00003714 case llvm::Triple::ppc64:
3715 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00003716
Peter Collingbournec947aae2012-05-20 23:28:41 +00003717 case llvm::Triple::nvptx:
3718 case llvm::Triple::nvptx64:
Justin Holewinski83e96682012-05-24 17:43:12 +00003719 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003720
Wesley Peck36a1f682010-12-19 19:57:51 +00003721 case llvm::Triple::mblaze:
3722 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3723
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003724 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003725 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003726
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003727 case llvm::Triple::tce:
3728 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3729
Eli Friedman33465822011-07-08 23:31:17 +00003730 case llvm::Triple::x86: {
Douglas Gregore8bbc122011-09-02 00:18:52 +00003731 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedman33465822011-07-08 23:31:17 +00003732
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003733 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003734 return *(TheTargetCodeGenInfo =
Eli Friedmana98d1f82012-01-25 22:46:34 +00003735 new X86_32TargetCodeGenInfo(
3736 Types, true, true, DisableMMX, false));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003737
3738 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003739 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003740 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003741 case llvm::Triple::AuroraUX:
3742 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003743 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003744 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003745 return *(TheTargetCodeGenInfo =
Eli Friedmana98d1f82012-01-25 22:46:34 +00003746 new X86_32TargetCodeGenInfo(
3747 Types, false, true, DisableMMX, false));
3748
3749 case llvm::Triple::Win32:
3750 return *(TheTargetCodeGenInfo =
3751 new X86_32TargetCodeGenInfo(
3752 Types, false, true, DisableMMX, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003753
3754 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003755 return *(TheTargetCodeGenInfo =
Eli Friedmana98d1f82012-01-25 22:46:34 +00003756 new X86_32TargetCodeGenInfo(
3757 Types, false, false, DisableMMX, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003758 }
Eli Friedman33465822011-07-08 23:31:17 +00003759 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003760
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003761 case llvm::Triple::x86_64: {
3762 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3763
Chris Lattner04dc9572010-08-31 16:44:54 +00003764 switch (Triple.getOS()) {
3765 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003766 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003767 case llvm::Triple::Cygwin:
3768 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3769 default:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003770 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3771 HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00003772 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003773 }
Tony Linthicum76329bf2011-12-12 21:14:55 +00003774 case llvm::Triple::hexagon:
3775 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003776 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003777}