blob: 77c4c9b07ed38ef9a2a64901b95752ecb7c9e108 [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
Eli Friedmanf37bd2f2011-12-01 04:53:19 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(
102 const CodeGen::CGFunctionInfo &) 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)
Daniel Dunbar626f1d82009-09-13 08:03:58 +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) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000232 const FieldDecl *FD = *i;
233 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) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000304 const FieldDecl *FD = *i;
305
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
Bill Wendling5cd41c42010-10-18 03:41:31 +0000392/// UseX86_MMXType - Return true if this is an MMX type that should use the special
393/// 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;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000421
422 static bool isRegisterSize(unsigned Size) {
423 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
424 }
425
426 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
427
Daniel Dunbar557893d2010-04-21 19:10:51 +0000428 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
429 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000430 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000431
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000432 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000433 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000434
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000435public:
Chris Lattner2b037972010-07-29 02:01:43 +0000436
Chris Lattner458b2aa2010-07-29 02:16:43 +0000437 ABIArgInfo classifyReturnType(QualType RetTy) const;
438 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000439
Chris Lattner22326a12010-07-29 02:31:05 +0000440 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000441 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000442 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
443 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000444 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000445 }
446
447 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
448 CodeGenFunction &CGF) const;
449
Eli Friedman33465822011-07-08 23:31:17 +0000450 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
451 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
452 IsMMXDisabled(m) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000453};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000455class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
456public:
Eli Friedman33465822011-07-08 23:31:17 +0000457 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
458 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000459
460 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
461 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000462
463 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
464 // Darwin uses different dwarf register numbers for EH.
465 if (CGM.isTargetDarwin()) return 5;
466
467 return 4;
468 }
469
470 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
471 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000472
Jay Foad7c57be32011-07-11 09:56:20 +0000473 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000474 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000475 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000476 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
477 }
478
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000479};
480
481}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000482
483/// shouldReturnTypeInRegister - Determine if the given type should be
484/// passed in a register (for the Darwin ABI).
485bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
486 ASTContext &Context) {
487 uint64_t Size = Context.getTypeSize(Ty);
488
489 // Type must be register sized.
490 if (!isRegisterSize(Size))
491 return false;
492
493 if (Ty->isVectorType()) {
494 // 64- and 128- bit vectors inside structures are not returned in
495 // registers.
496 if (Size == 64 || Size == 128)
497 return false;
498
499 return true;
500 }
501
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000502 // If this is a builtin, pointer, enum, complex type, member pointer, or
503 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000504 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000505 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000506 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000507 return true;
508
509 // Arrays are treated like records.
510 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
511 return shouldReturnTypeInRegister(AT->getElementType(), Context);
512
513 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000514 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000515 if (!RT) return false;
516
Anders Carlsson40446e82010-01-27 03:25:19 +0000517 // FIXME: Traverse bases here too.
518
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000519 // Structure types are passed in register if all fields would be
520 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000521 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
522 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000523 const FieldDecl *FD = *i;
524
525 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000526 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000527 continue;
528
529 // Check fields recursively.
530 if (!shouldReturnTypeInRegister(FD->getType(), Context))
531 return false;
532 }
533
534 return true;
535}
536
Chris Lattner458b2aa2010-07-29 02:16:43 +0000537ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
538 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000539 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000540
Chris Lattner458b2aa2010-07-29 02:16:43 +0000541 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000542 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000543 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000544 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000545
546 // 128-bit vectors are a special case; they are returned in
547 // registers and we need to make sure to pick a type the LLVM
548 // backend will like.
549 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000550 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000551 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000552
553 // Always return in register if it fits in a general purpose
554 // register, or if it is 64 bits and has a single element.
555 if ((Size == 8 || Size == 16 || Size == 32) ||
556 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000557 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000558 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000559
560 return ABIArgInfo::getIndirect(0);
561 }
562
563 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000564 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000565
John McCalla1dee5302010-08-22 10:59:02 +0000566 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000567 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000568 // Structures with either a non-trivial destructor or a non-trivial
569 // copy constructor are always indirect.
570 if (hasNonTrivialDestructorOrCopyConstructor(RT))
571 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000572
Anders Carlsson5789c492009-10-20 22:07:59 +0000573 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000574 if (RT->getDecl()->hasFlexibleArrayMember())
575 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000576 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000577
David Chisnallde3a0692009-08-17 23:08:21 +0000578 // If specified, structs and unions are always indirect.
579 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000580 return ABIArgInfo::getIndirect(0);
581
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000582 // Small structures which are register sized are generally returned
583 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000584 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
585 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000586
587 // As a special-case, if the struct is a "single-element" struct, and
588 // the field is of type "float" or "double", return it in a
589 // floating-point register. We apply a similar transformation for
590 // pointer types to improve the quality of the generated IR.
591 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
592 if (SeltTy->isRealFloatingType() || SeltTy->hasPointerRepresentation())
593 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
594
595 // FIXME: We should be able to narrow this integer in cases with dead
596 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000597 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000598 }
599
600 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000601 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000602
Chris Lattner458b2aa2010-07-29 02:16:43 +0000603 // Treat an enum type as its underlying type.
604 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
605 RetTy = EnumTy->getDecl()->getIntegerType();
606
607 return (RetTy->isPromotableIntegerType() ?
608 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000609}
610
Daniel Dunbared23de32010-09-16 20:42:00 +0000611static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
612 const RecordType *RT = Ty->getAs<RecordType>();
613 if (!RT)
614 return 0;
615 const RecordDecl *RD = RT->getDecl();
616
617 // If this is a C++ record, check the bases first.
618 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
619 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
620 e = CXXRD->bases_end(); i != e; ++i)
621 if (!isRecordWithSSEVectorType(Context, i->getType()))
622 return false;
623
624 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
625 i != e; ++i) {
626 QualType FT = i->getType();
627
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000628 if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
Daniel Dunbared23de32010-09-16 20:42:00 +0000629 return true;
630
631 if (isRecordWithSSEVectorType(Context, FT))
632 return true;
633 }
634
635 return false;
636}
637
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000638unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
639 unsigned Align) const {
640 // Otherwise, if the alignment is less than or equal to the minimum ABI
641 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000642 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000643 return 0; // Use default alignment.
644
645 // On non-Darwin, the stack type alignment is always 4.
646 if (!IsDarwinVectorABI) {
647 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000648 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000649 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000650
Daniel Dunbared23de32010-09-16 20:42:00 +0000651 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000652 if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
Daniel Dunbared23de32010-09-16 20:42:00 +0000653 return 16;
654
655 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000656}
657
Chris Lattner458b2aa2010-07-29 02:16:43 +0000658ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000659 if (!ByVal)
660 return ABIArgInfo::getIndirect(0, false);
661
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000662 // Compute the byval alignment.
663 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
664 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
665 if (StackAlign == 0)
Chris Lattnere76b95a2011-05-22 23:35:00 +0000666 return ABIArgInfo::getIndirect(4);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000667
668 // If the stack alignment is less than the type alignment, realign the
669 // argument.
670 if (StackAlign < TypeAlign)
671 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
672 /*Realign=*/true);
673
674 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000675}
676
Chris Lattner458b2aa2010-07-29 02:16:43 +0000677ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000678 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000679 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000680 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000681 if (const RecordType *RT = Ty->getAs<RecordType>()) {
682 // Structures with either a non-trivial destructor or a non-trivial
683 // copy constructor are always indirect.
684 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000685 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000686
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000687 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000688 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000689 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000690
Eli Friedman9f061a32011-11-18 00:28:11 +0000691 // Ignore empty structs/unions.
Eli Friedmanf22fa9e2011-11-18 04:01:36 +0000692 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000693 return ABIArgInfo::getIgnore();
694
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000695 // Expand small (<= 128-bit) record types when we know that the stack layout
696 // of those arguments will match the struct. This is important because the
697 // LLVM backend isn't smart enough to remove byval, which inhibits many
698 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000699 if (getContext().getTypeSize(Ty) <= 4*32 &&
700 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000701 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000702
Chris Lattner458b2aa2010-07-29 02:16:43 +0000703 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000704 }
705
Chris Lattnerd774ae92010-08-26 20:05:13 +0000706 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000707 // On Darwin, some vectors are passed in memory, we handle this by passing
708 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000709 if (IsDarwinVectorABI) {
710 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000711 if ((Size == 8 || Size == 16 || Size == 32) ||
712 (Size == 64 && VT->getNumElements() == 1))
713 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
714 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000715 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000716
Chris Lattnera5f58b02011-07-09 17:41:47 +0000717 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +0000718 if (UseX86_MMXType(IRType)) {
Eli Friedman33465822011-07-08 23:31:17 +0000719 if (IsMMXDisabled)
720 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
721 64));
Bill Wendling5cd41c42010-10-18 03:41:31 +0000722 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
723 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
724 return AAI;
725 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000726
Chris Lattnerd774ae92010-08-26 20:05:13 +0000727 return ABIArgInfo::getDirect();
728 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000729
730
Chris Lattner458b2aa2010-07-29 02:16:43 +0000731 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
732 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000733
Chris Lattner458b2aa2010-07-29 02:16:43 +0000734 return (Ty->isPromotableIntegerType() ?
735 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000736}
737
738llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
739 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +0000740 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
741 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000742
743 CGBuilderTy &Builder = CGF.Builder;
744 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
745 "ap");
746 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000747
748 // Compute if the address needs to be aligned
749 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
750 Align = getTypeStackAlignInBytes(Ty, Align);
751 Align = std::max(Align, 4U);
752 if (Align > 4) {
753 // addr = (addr + align - 1) & -align;
754 llvm::Value *Offset =
755 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
756 Addr = CGF.Builder.CreateGEP(Addr, Offset);
757 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
758 CGF.Int32Ty);
759 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
760 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
761 Addr->getType(),
762 "ap.cur.aligned");
763 }
764
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000765 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000766 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000767 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
768
769 uint64_t Offset =
Eli Friedman1d7dd3b2011-11-18 02:12:09 +0000770 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000771 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000772 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000773 "ap.next");
774 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
775
776 return AddrTyped;
777}
778
Charles Davis4ea31ab2010-02-13 15:54:06 +0000779void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
780 llvm::GlobalValue *GV,
781 CodeGen::CodeGenModule &CGM) const {
782 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
783 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
784 // Get the LLVM function.
785 llvm::Function *Fn = cast<llvm::Function>(GV);
786
787 // Now add the 'alignstack' attribute with a value of 16.
788 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
789 }
790 }
791}
792
John McCallbeec5a02010-03-06 00:35:14 +0000793bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
794 CodeGen::CodeGenFunction &CGF,
795 llvm::Value *Address) const {
796 CodeGen::CGBuilderTy &Builder = CGF.Builder;
797 llvm::LLVMContext &Context = CGF.getLLVMContext();
798
Chris Lattner2192fe52011-07-18 04:24:23 +0000799 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallbeec5a02010-03-06 00:35:14 +0000800 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000801
John McCallbeec5a02010-03-06 00:35:14 +0000802 // 0-7 are the eight integer registers; the order is different
803 // on Darwin (for EH), but the range is the same.
804 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000805 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000806
807 if (CGF.CGM.isTargetDarwin()) {
808 // 12-16 are st(0..4). Not sure why we stop at 4.
809 // These have size 16, which is sizeof(long double) on
810 // platforms with 8-byte alignment for that type.
811 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000812 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000813
John McCallbeec5a02010-03-06 00:35:14 +0000814 } else {
815 // 9 is %eflags, which doesn't get a size on Darwin for some
816 // reason.
817 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
818
819 // 11-16 are st(0..5). Not sure why we stop at 5.
820 // These have size 12, which is sizeof(long double) on
821 // platforms with 4-byte alignment for that type.
822 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000823 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
824 }
John McCallbeec5a02010-03-06 00:35:14 +0000825
826 return false;
827}
828
Chris Lattner0cf24192010-06-28 20:05:43 +0000829//===----------------------------------------------------------------------===//
830// X86-64 ABI Implementation
831//===----------------------------------------------------------------------===//
832
833
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000834namespace {
835/// X86_64ABIInfo - The X86_64 ABI information.
836class X86_64ABIInfo : public ABIInfo {
837 enum Class {
838 Integer = 0,
839 SSE,
840 SSEUp,
841 X87,
842 X87Up,
843 ComplexX87,
844 NoClass,
845 Memory
846 };
847
848 /// merge - Implement the X86_64 ABI merging algorithm.
849 ///
850 /// Merge an accumulating classification \arg Accum with a field
851 /// classification \arg Field.
852 ///
853 /// \param Accum - The accumulating classification. This should
854 /// always be either NoClass or the result of a previous merge
855 /// call. In addition, this should never be Memory (the caller
856 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000857 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000858
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000859 /// postMerge - Implement the X86_64 ABI post merging algorithm.
860 ///
861 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
862 /// final MEMORY or SSE classes when necessary.
863 ///
864 /// \param AggregateSize - The size of the current aggregate in
865 /// the classification process.
866 ///
867 /// \param Lo - The classification for the parts of the type
868 /// residing in the low word of the containing object.
869 ///
870 /// \param Hi - The classification for the parts of the type
871 /// residing in the higher words of the containing object.
872 ///
873 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
874
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000875 /// classify - Determine the x86_64 register classes in which the
876 /// given type T should be passed.
877 ///
878 /// \param Lo - The classification for the parts of the type
879 /// residing in the low word of the containing object.
880 ///
881 /// \param Hi - The classification for the parts of the type
882 /// residing in the high word of the containing object.
883 ///
884 /// \param OffsetBase - The bit offset of this type in the
885 /// containing object. Some parameters are classified different
886 /// depending on whether they straddle an eightbyte boundary.
887 ///
888 /// If a word is unused its result will be NoClass; if a type should
889 /// be passed in Memory then at least the classification of \arg Lo
890 /// will be Memory.
891 ///
892 /// The \arg Lo class will be NoClass iff the argument is ignored.
893 ///
894 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
895 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000896 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000897
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000898 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000899 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
900 unsigned IROffset, QualType SourceTy,
901 unsigned SourceOffset) const;
902 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
903 unsigned IROffset, QualType SourceTy,
904 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000905
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000906 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000907 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000908 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000909
910 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000911 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000912 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000913
Chris Lattner458b2aa2010-07-29 02:16:43 +0000914 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000915
Bill Wendling5cd41c42010-10-18 03:41:31 +0000916 ABIArgInfo classifyArgumentType(QualType Ty,
917 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000918 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000919
John McCalle0fda732011-04-21 01:20:55 +0000920 /// The 0.98 ABI revision clarified a lot of ambiguities,
921 /// unfortunately in ways that were not always consistent with
922 /// certain previous compilers. In particular, platforms which
923 /// required strict binary compatibility with older versions of GCC
924 /// may need to exempt themselves.
925 bool honorsRevision0_98() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000926 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +0000927 }
928
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929public:
Chris Lattner2b037972010-07-29 02:01:43 +0000930 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000931
Chris Lattner22326a12010-07-29 02:31:05 +0000932 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000933
934 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
935 CodeGenFunction &CGF) const;
936};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000937
Chris Lattner04dc9572010-08-31 16:44:54 +0000938/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000939class WinX86_64ABIInfo : public ABIInfo {
940
941 ABIArgInfo classify(QualType Ty) const;
942
Chris Lattner04dc9572010-08-31 16:44:54 +0000943public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000944 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
945
946 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000947
948 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
949 CodeGenFunction &CGF) const;
950};
951
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000952class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
953public:
Chris Lattner2b037972010-07-29 02:01:43 +0000954 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
955 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000956
957 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
958 return 7;
959 }
960
961 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
962 llvm::Value *Address) const {
963 CodeGen::CGBuilderTy &Builder = CGF.Builder;
964 llvm::LLVMContext &Context = CGF.getLLVMContext();
965
Chris Lattner2192fe52011-07-18 04:24:23 +0000966 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallbeec5a02010-03-06 00:35:14 +0000967 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000968
John McCall943fae92010-05-27 06:19:26 +0000969 // 0-15 are the 16 integer registers.
970 // 16 is %rip.
971 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000972
973 return false;
974 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000975
Jay Foad7c57be32011-07-11 09:56:20 +0000976 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000977 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000978 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000979 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
980 }
981
Eli Friedmanf37bd2f2011-12-01 04:53:19 +0000982 bool isNoProtoCallVariadic(const CodeGen::CGFunctionInfo &FI) const {
John McCallcbc038a2011-09-21 08:08:30 +0000983 // The default CC on x86-64 sets %al to the number of SSA
984 // registers used, and GCC sets this when calling an unprototyped
Eli Friedmanf37bd2f2011-12-01 04:53:19 +0000985 // function, so we override the default behavior. However, don't do
986 // that when AVX types are involved.
987 if (FI.getCallingConvention() == llvm::CallingConv::C) {
988 bool HasAVXType = false;
989 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
990 ie = FI.arg_end();
991 it != ie; ++it) {
992 if (it->info.isDirect()) {
993 llvm::Type *Ty = it->info.getCoerceToType();
994 if (llvm::VectorType *VTy = dyn_cast_or_null<llvm::VectorType>(Ty)) {
995 if (VTy->getBitWidth() > 128) {
996 HasAVXType = true;
997 break;
998 }
999 }
1000 }
1001 }
1002 if (!HasAVXType)
1003 return true;
1004 }
John McCallcbc038a2011-09-21 08:08:30 +00001005
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001006 return TargetCodeGenInfo::isNoProtoCallVariadic(FI);
John McCallcbc038a2011-09-21 08:08:30 +00001007 }
1008
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001009};
1010
Chris Lattner04dc9572010-08-31 16:44:54 +00001011class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1012public:
1013 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1014 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1015
1016 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1017 return 7;
1018 }
1019
1020 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1021 llvm::Value *Address) const {
1022 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1023 llvm::LLVMContext &Context = CGF.getLLVMContext();
1024
Chris Lattner2192fe52011-07-18 04:24:23 +00001025 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattner04dc9572010-08-31 16:44:54 +00001026 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001027
Chris Lattner04dc9572010-08-31 16:44:54 +00001028 // 0-15 are the 16 integer registers.
1029 // 16 is %rip.
1030 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1031
1032 return false;
1033 }
1034};
1035
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001036}
1037
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001038void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1039 Class &Hi) const {
1040 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1041 //
1042 // (a) If one of the classes is Memory, the whole argument is passed in
1043 // memory.
1044 //
1045 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1046 // memory.
1047 //
1048 // (c) If the size of the aggregate exceeds two eightbytes and the first
1049 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1050 // argument is passed in memory. NOTE: This is necessary to keep the
1051 // ABI working for processors that don't support the __m256 type.
1052 //
1053 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1054 //
1055 // Some of these are enforced by the merging logic. Others can arise
1056 // only with unions; for example:
1057 // union { _Complex double; unsigned; }
1058 //
1059 // Note that clauses (b) and (c) were added in 0.98.
1060 //
1061 if (Hi == Memory)
1062 Lo = Memory;
1063 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1064 Lo = Memory;
1065 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1066 Lo = Memory;
1067 if (Hi == SSEUp && Lo != SSE)
1068 Hi = SSE;
1069}
1070
Chris Lattnerd776fb12010-06-28 21:43:59 +00001071X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001072 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1073 // classified recursively so that always two fields are
1074 // considered. The resulting class is calculated according to
1075 // the classes of the fields in the eightbyte:
1076 //
1077 // (a) If both classes are equal, this is the resulting class.
1078 //
1079 // (b) If one of the classes is NO_CLASS, the resulting class is
1080 // the other class.
1081 //
1082 // (c) If one of the classes is MEMORY, the result is the MEMORY
1083 // class.
1084 //
1085 // (d) If one of the classes is INTEGER, the result is the
1086 // INTEGER.
1087 //
1088 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1089 // MEMORY is used as class.
1090 //
1091 // (f) Otherwise class SSE is used.
1092
1093 // Accum should never be memory (we should have returned) or
1094 // ComplexX87 (because this cannot be passed in a structure).
1095 assert((Accum != Memory && Accum != ComplexX87) &&
1096 "Invalid accumulated classification during merge.");
1097 if (Accum == Field || Field == NoClass)
1098 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001099 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001100 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001101 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001102 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001103 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001104 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001105 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1106 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001107 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001108 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001109}
1110
Chris Lattner5c740f12010-06-30 19:14:05 +00001111void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001112 Class &Lo, Class &Hi) const {
1113 // FIXME: This code can be simplified by introducing a simple value class for
1114 // Class pairs with appropriate constructor methods for the various
1115 // situations.
1116
1117 // FIXME: Some of the split computations are wrong; unaligned vectors
1118 // shouldn't be passed in registers for example, so there is no chance they
1119 // can straddle an eightbyte. Verify & simplify.
1120
1121 Lo = Hi = NoClass;
1122
1123 Class &Current = OffsetBase < 64 ? Lo : Hi;
1124 Current = Memory;
1125
John McCall9dd450b2009-09-21 23:43:11 +00001126 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001127 BuiltinType::Kind k = BT->getKind();
1128
1129 if (k == BuiltinType::Void) {
1130 Current = NoClass;
1131 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1132 Lo = Integer;
1133 Hi = Integer;
1134 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1135 Current = Integer;
1136 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1137 Current = SSE;
1138 } else if (k == BuiltinType::LongDouble) {
1139 Lo = X87;
1140 Hi = X87Up;
1141 }
1142 // FIXME: _Decimal32 and _Decimal64 are SSE.
1143 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001144 return;
1145 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001146
Chris Lattnerd776fb12010-06-28 21:43:59 +00001147 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001148 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001149 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001150 return;
1151 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001152
Chris Lattnerd776fb12010-06-28 21:43:59 +00001153 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001154 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001155 return;
1156 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001157
Chris Lattnerd776fb12010-06-28 21:43:59 +00001158 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001159 if (Ty->isMemberFunctionPointerType())
1160 Lo = Hi = Integer;
1161 else
1162 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001163 return;
1164 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001165
Chris Lattnerd776fb12010-06-28 21:43:59 +00001166 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001167 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001168 if (Size == 32) {
1169 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1170 // float> as integer.
1171 Current = Integer;
1172
1173 // If this type crosses an eightbyte boundary, it should be
1174 // split.
1175 uint64_t EB_Real = (OffsetBase) / 64;
1176 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1177 if (EB_Real != EB_Imag)
1178 Hi = Lo;
1179 } else if (Size == 64) {
1180 // gcc passes <1 x double> in memory. :(
1181 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1182 return;
1183
1184 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001185 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001186 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1187 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1188 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001189 Current = Integer;
1190 else
1191 Current = SSE;
1192
1193 // If this type crosses an eightbyte boundary, it should be
1194 // split.
1195 if (OffsetBase && OffsetBase != 64)
1196 Hi = Lo;
Bruno Cardoso Lopes37b7fd02011-07-12 02:47:38 +00001197 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001198 // Arguments of 256-bits are split into four eightbyte chunks. The
1199 // least significant one belongs to class SSE and all the others to class
1200 // SSEUP. The original Lo and Hi design considers that types can't be
1201 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1202 // This design isn't correct for 256-bits, but since there're no cases
1203 // where the upper parts would need to be inspected, avoid adding
1204 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001205 Lo = SSE;
1206 Hi = SSEUp;
1207 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001208 return;
1209 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001210
Chris Lattnerd776fb12010-06-28 21:43:59 +00001211 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001212 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001213
Chris Lattner2b037972010-07-29 02:01:43 +00001214 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001215 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001216 if (Size <= 64)
1217 Current = Integer;
1218 else if (Size <= 128)
1219 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001220 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001221 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001222 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001223 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001224 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001225 Current = ComplexX87;
1226
1227 // If this complex type crosses an eightbyte boundary then it
1228 // should be split.
1229 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001230 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001231 if (Hi == NoClass && EB_Real != EB_Imag)
1232 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001233
Chris Lattnerd776fb12010-06-28 21:43:59 +00001234 return;
1235 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001236
Chris Lattner2b037972010-07-29 02:01:43 +00001237 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001238 // Arrays are treated like structures.
1239
Chris Lattner2b037972010-07-29 02:01:43 +00001240 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001241
1242 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001243 // than four eightbytes, ..., it has class MEMORY.
1244 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001245 return;
1246
1247 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1248 // fields, it has class MEMORY.
1249 //
1250 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001251 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001252 return;
1253
1254 // Otherwise implement simplified merge. We could be smarter about
1255 // this, but it isn't worth it and would be harder to verify.
1256 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001257 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001258 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001259
1260 // The only case a 256-bit wide vector could be used is when the array
1261 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1262 // to work for sizes wider than 128, early check and fallback to memory.
1263 if (Size > 128 && EltSize != 256)
1264 return;
1265
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001266 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1267 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001268 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001269 Lo = merge(Lo, FieldLo);
1270 Hi = merge(Hi, FieldHi);
1271 if (Lo == Memory || Hi == Memory)
1272 break;
1273 }
1274
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001275 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001276 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001277 return;
1278 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001279
Chris Lattnerd776fb12010-06-28 21:43:59 +00001280 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001281 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001282
1283 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001284 // than four eightbytes, ..., it has class MEMORY.
1285 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001286 return;
1287
Anders Carlsson20759ad2009-09-16 15:53:40 +00001288 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1289 // copy constructor or a non-trivial destructor, it is passed by invisible
1290 // reference.
1291 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1292 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001293
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001294 const RecordDecl *RD = RT->getDecl();
1295
1296 // Assume variable sized types are passed in memory.
1297 if (RD->hasFlexibleArrayMember())
1298 return;
1299
Chris Lattner2b037972010-07-29 02:01:43 +00001300 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001301
1302 // Reset Lo class, this will be recomputed.
1303 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001304
1305 // If this is a C++ record, classify the bases first.
1306 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1307 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1308 e = CXXRD->bases_end(); i != e; ++i) {
1309 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1310 "Unexpected base class!");
1311 const CXXRecordDecl *Base =
1312 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1313
1314 // Classify this field.
1315 //
1316 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1317 // single eightbyte, each is classified separately. Each eightbyte gets
1318 // initialized to class NO_CLASS.
1319 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001320 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001321 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001322 Lo = merge(Lo, FieldLo);
1323 Hi = merge(Hi, FieldHi);
1324 if (Lo == Memory || Hi == Memory)
1325 break;
1326 }
1327 }
1328
1329 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001330 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001331 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001332 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001333 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1334 bool BitField = i->isBitField();
1335
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001336 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1337 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001338 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001339 // The only case a 256-bit wide vector could be used is when the struct
1340 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1341 // to work for sizes wider than 128, early check and fallback to memory.
1342 //
1343 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1344 Lo = Memory;
1345 return;
1346 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001347 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001348 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001349 Lo = Memory;
1350 return;
1351 }
1352
1353 // Classify this field.
1354 //
1355 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1356 // exceeds a single eightbyte, each is classified
1357 // separately. Each eightbyte gets initialized to class
1358 // NO_CLASS.
1359 Class FieldLo, FieldHi;
1360
1361 // Bit-fields require special handling, they do not force the
1362 // structure to be passed in memory even if unaligned, and
1363 // therefore they can straddle an eightbyte.
1364 if (BitField) {
1365 // Ignore padding bit-fields.
1366 if (i->isUnnamedBitfield())
1367 continue;
1368
1369 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001370 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001371
1372 uint64_t EB_Lo = Offset / 64;
1373 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1374 FieldLo = FieldHi = NoClass;
1375 if (EB_Lo) {
1376 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1377 FieldLo = NoClass;
1378 FieldHi = Integer;
1379 } else {
1380 FieldLo = Integer;
1381 FieldHi = EB_Hi ? Integer : NoClass;
1382 }
1383 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001384 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001385 Lo = merge(Lo, FieldLo);
1386 Hi = merge(Hi, FieldHi);
1387 if (Lo == Memory || Hi == Memory)
1388 break;
1389 }
1390
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001391 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001392 }
1393}
1394
Chris Lattner22a931e2010-06-29 06:01:59 +00001395ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001396 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1397 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001398 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001399 // Treat an enum type as its underlying type.
1400 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1401 Ty = EnumTy->getDecl()->getIntegerType();
1402
1403 return (Ty->isPromotableIntegerType() ?
1404 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1405 }
1406
1407 return ABIArgInfo::getIndirect(0);
1408}
1409
Chris Lattner22a931e2010-06-29 06:01:59 +00001410ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001411 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1412 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001413 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001414 // Treat an enum type as its underlying type.
1415 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1416 Ty = EnumTy->getDecl()->getIntegerType();
1417
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001418 return (Ty->isPromotableIntegerType() ?
1419 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001420 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001421
Daniel Dunbar53fac692010-04-21 19:49:55 +00001422 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1423 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001424
Chris Lattner44c2b902011-05-22 23:21:23 +00001425 // Compute the byval alignment. We specify the alignment of the byval in all
1426 // cases so that the mid-level optimizer knows the alignment of the byval.
1427 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1428 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001429}
1430
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001431/// GetByteVectorType - The ABI specifies that a value should be passed in an
1432/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001433/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001434llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001435 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001436
Chris Lattner9fa15c32010-07-29 05:02:29 +00001437 // Wrapper structs that just contain vectors are passed just like vectors,
1438 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001439 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001440 while (STy && STy->getNumElements() == 1) {
1441 IRType = STy->getElementType(0);
1442 STy = dyn_cast<llvm::StructType>(IRType);
1443 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001444
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001445 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001446 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1447 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001448 unsigned BitWidth = VT->getBitWidth();
Tanya Lattner71f1b2d2011-11-28 23:18:11 +00001449 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001450 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1451 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1452 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1453 EltTy->isIntegerTy(128)))
1454 return VT;
1455 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001456
Chris Lattner4200fe42010-07-29 04:56:46 +00001457 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1458}
1459
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001460/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1461/// is known to either be off the end of the specified type or being in
1462/// alignment padding. The user type specified is known to be at most 128 bits
1463/// in size, and have passed through X86_64ABIInfo::classify with a successful
1464/// classification that put one of the two halves in the INTEGER class.
1465///
1466/// It is conservatively correct to return false.
1467static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1468 unsigned EndBit, ASTContext &Context) {
1469 // If the bytes being queried are off the end of the type, there is no user
1470 // data hiding here. This handles analysis of builtins, vectors and other
1471 // types that don't contain interesting padding.
1472 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1473 if (TySize <= StartBit)
1474 return true;
1475
Chris Lattner98076a22010-07-29 07:43:55 +00001476 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1477 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1478 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1479
1480 // Check each element to see if the element overlaps with the queried range.
1481 for (unsigned i = 0; i != NumElts; ++i) {
1482 // If the element is after the span we care about, then we're done..
1483 unsigned EltOffset = i*EltSize;
1484 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001485
Chris Lattner98076a22010-07-29 07:43:55 +00001486 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1487 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1488 EndBit-EltOffset, Context))
1489 return false;
1490 }
1491 // If it overlaps no elements, then it is safe to process as padding.
1492 return true;
1493 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001494
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001495 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1496 const RecordDecl *RD = RT->getDecl();
1497 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001498
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001499 // If this is a C++ record, check the bases first.
1500 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1501 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1502 e = CXXRD->bases_end(); i != e; ++i) {
1503 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1504 "Unexpected base class!");
1505 const CXXRecordDecl *Base =
1506 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001507
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001508 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001509 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001510 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001511
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001512 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1513 if (!BitsContainNoUserData(i->getType(), BaseStart,
1514 EndBit-BaseOffset, Context))
1515 return false;
1516 }
1517 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001518
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001519 // Verify that no field has data that overlaps the region of interest. Yes
1520 // this could be sped up a lot by being smarter about queried fields,
1521 // however we're only looking at structs up to 16 bytes, so we don't care
1522 // much.
1523 unsigned idx = 0;
1524 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1525 i != e; ++i, ++idx) {
1526 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001527
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001528 // If we found a field after the region we care about, then we're done.
1529 if (FieldOffset >= EndBit) break;
1530
1531 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1532 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1533 Context))
1534 return false;
1535 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001536
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001537 // If nothing in this record overlapped the area of interest, then we're
1538 // clean.
1539 return true;
1540 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001541
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001542 return false;
1543}
1544
Chris Lattnere556a712010-07-29 18:39:32 +00001545/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1546/// float member at the specified offset. For example, {int,{float}} has a
1547/// float at offset 4. It is conservatively correct for this routine to return
1548/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00001549static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnere556a712010-07-29 18:39:32 +00001550 const llvm::TargetData &TD) {
1551 // Base case if we find a float.
1552 if (IROffset == 0 && IRType->isFloatTy())
1553 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001554
Chris Lattnere556a712010-07-29 18:39:32 +00001555 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001556 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00001557 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1558 unsigned Elt = SL->getElementContainingOffset(IROffset);
1559 IROffset -= SL->getElementOffset(Elt);
1560 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1561 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001562
Chris Lattnere556a712010-07-29 18:39:32 +00001563 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001564 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1565 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00001566 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1567 IROffset -= IROffset/EltSize*EltSize;
1568 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1569 }
1570
1571 return false;
1572}
1573
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001574
1575/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1576/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001577llvm::Type *X86_64ABIInfo::
1578GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001579 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001580 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001581 // pass as float if the last 4 bytes is just padding. This happens for
1582 // structs that contain 3 floats.
1583 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1584 SourceOffset*8+64, getContext()))
1585 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001586
Chris Lattnere556a712010-07-29 18:39:32 +00001587 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1588 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1589 // case.
1590 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001591 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1592 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001593
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001594 return llvm::Type::getDoubleTy(getVMContext());
1595}
1596
1597
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001598/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1599/// an 8-byte GPR. This means that we either have a scalar or we are talking
1600/// about the high or low part of an up-to-16-byte struct. This routine picks
1601/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001602/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1603/// etc).
1604///
1605/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1606/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1607/// the 8-byte value references. PrefType may be null.
1608///
1609/// SourceTy is the source level type for the entire argument. SourceOffset is
1610/// an offset into this that we're processing (which is always either 0 or 8).
1611///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001612llvm::Type *X86_64ABIInfo::
1613GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001614 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001615 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1616 // returning an 8-byte unit starting with it. See if we can safely use it.
1617 if (IROffset == 0) {
1618 // Pointers and int64's always fill the 8-byte unit.
1619 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1620 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001621
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001622 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1623 // goodness in the source type is just tail padding. This is allowed to
1624 // kick in for struct {double,int} on the int, but not on
1625 // struct{double,int,int} because we wouldn't return the second int. We
1626 // have to do this analysis on the source type because we can't depend on
1627 // unions being lowered a specific way etc.
1628 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1629 IRType->isIntegerTy(32)) {
1630 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001631
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001632 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1633 SourceOffset*8+64, getContext()))
1634 return IRType;
1635 }
1636 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001637
Chris Lattner2192fe52011-07-18 04:24:23 +00001638 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001639 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001640 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001641 if (IROffset < SL->getSizeInBytes()) {
1642 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1643 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001644
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001645 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1646 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001647 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001648 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001649
Chris Lattner2192fe52011-07-18 04:24:23 +00001650 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001651 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001652 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1653 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001654 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1655 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001656 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001657
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001658 // Okay, we don't have any better idea of what to pass, so we pass this in an
1659 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001660 unsigned TySizeInBytes =
1661 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001662
Chris Lattner3f763422010-07-29 17:34:39 +00001663 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001664
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001665 // It is always safe to classify this as an integer type up to i64 that
1666 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001667 return llvm::IntegerType::get(getVMContext(),
1668 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001669}
1670
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001671
1672/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1673/// be used as elements of a two register pair to pass or return, return a
1674/// first class aggregate to represent them. For example, if the low part of
1675/// a by-value argument should be passed as i32* and the high part as float,
1676/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001677static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001678GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001679 const llvm::TargetData &TD) {
1680 // In order to correctly satisfy the ABI, we need to the high part to start
1681 // at offset 8. If the high and low parts we inferred are both 4-byte types
1682 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1683 // the second element at offset 8. Check for this:
1684 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1685 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1686 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1687 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001688
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001689 // To handle this, we have to increase the size of the low part so that the
1690 // second element will start at an 8 byte offset. We can't increase the size
1691 // of the second element because it might make us access off the end of the
1692 // struct.
1693 if (HiStart != 8) {
1694 // There are only two sorts of types the ABI generation code can produce for
1695 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1696 // Promote these to a larger type.
1697 if (Lo->isFloatTy())
1698 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1699 else {
1700 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1701 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1702 }
1703 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001704
Chris Lattnera5f58b02011-07-09 17:41:47 +00001705 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001706
1707
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001708 // Verify that the second element is at an 8-byte offset.
1709 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1710 "Invalid x86-64 argument pair!");
1711 return Result;
1712}
1713
Chris Lattner31faff52010-07-28 23:06:14 +00001714ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001715classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001716 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1717 // classification algorithm.
1718 X86_64ABIInfo::Class Lo, Hi;
1719 classify(RetTy, 0, Lo, Hi);
1720
1721 // Check some invariants.
1722 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001723 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1724
Chris Lattnera5f58b02011-07-09 17:41:47 +00001725 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001726 switch (Lo) {
1727 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001728 if (Hi == NoClass)
1729 return ABIArgInfo::getIgnore();
1730 // If the low part is just padding, it takes no register, leave ResType
1731 // null.
1732 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1733 "Unknown missing lo part");
1734 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001735
1736 case SSEUp:
1737 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001738 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001739
1740 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1741 // hidden argument.
1742 case Memory:
1743 return getIndirectReturnResult(RetTy);
1744
1745 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1746 // available register of the sequence %rax, %rdx is used.
1747 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001748 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001749
Chris Lattner1f3a0632010-07-29 21:42:50 +00001750 // If we have a sign or zero extended integer, make sure to return Extend
1751 // so that the parameter gets the right LLVM IR attributes.
1752 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1753 // Treat an enum type as its underlying type.
1754 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1755 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001756
Chris Lattner1f3a0632010-07-29 21:42:50 +00001757 if (RetTy->isIntegralOrEnumerationType() &&
1758 RetTy->isPromotableIntegerType())
1759 return ABIArgInfo::getExtend();
1760 }
Chris Lattner31faff52010-07-28 23:06:14 +00001761 break;
1762
1763 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1764 // available SSE register of the sequence %xmm0, %xmm1 is used.
1765 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001766 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001767 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001768
1769 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1770 // returned on the X87 stack in %st0 as 80-bit x87 number.
1771 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001772 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001773 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001774
1775 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1776 // part of the value is returned in %st0 and the imaginary part in
1777 // %st1.
1778 case ComplexX87:
1779 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001780 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001781 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001782 NULL);
1783 break;
1784 }
1785
Chris Lattnera5f58b02011-07-09 17:41:47 +00001786 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001787 switch (Hi) {
1788 // Memory was handled previously and X87 should
1789 // never occur as a hi class.
1790 case Memory:
1791 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00001792 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001793
1794 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001795 case NoClass:
1796 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001797
Chris Lattner52b3c132010-09-01 00:20:33 +00001798 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001799 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001800 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1801 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001802 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001803 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001804 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001805 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1806 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001807 break;
1808
1809 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001810 // is passed in the next available eightbyte chunk if the last used
1811 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00001812 //
Chris Lattner57540c52011-04-15 05:22:18 +00001813 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001814 case SSEUp:
1815 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001816 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001817 break;
1818
1819 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1820 // returned together with the previous X87 value in %st0.
1821 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001822 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001823 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001824 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001825 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001826 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001827 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001828 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1829 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001830 }
Chris Lattner31faff52010-07-28 23:06:14 +00001831 break;
1832 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001833
Chris Lattner52b3c132010-09-01 00:20:33 +00001834 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001835 // known to pass in the high eightbyte of the result. We do this by forming a
1836 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001837 if (HighPart)
1838 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001839
Chris Lattner1f3a0632010-07-29 21:42:50 +00001840 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001841}
1842
Chris Lattner458b2aa2010-07-29 02:16:43 +00001843ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +00001844 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001845 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001846 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001847
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001848 // Check some invariants.
1849 // FIXME: Enforce these by construction.
1850 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001851 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1852
1853 neededInt = 0;
1854 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001855 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001856 switch (Lo) {
1857 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001858 if (Hi == NoClass)
1859 return ABIArgInfo::getIgnore();
1860 // If the low part is just padding, it takes no register, leave ResType
1861 // null.
1862 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1863 "Unknown missing lo part");
1864 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001865
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001866 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1867 // on the stack.
1868 case Memory:
1869
1870 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1871 // COMPLEX_X87, it is passed in memory.
1872 case X87:
1873 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001874 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1875 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001876 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001877
1878 case SSEUp:
1879 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001880 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001881
1882 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1883 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1884 // and %r9 is used.
1885 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001886 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001887
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001888 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001889 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001890
1891 // If we have a sign or zero extended integer, make sure to return Extend
1892 // so that the parameter gets the right LLVM IR attributes.
1893 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1894 // Treat an enum type as its underlying type.
1895 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1896 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001897
Chris Lattner1f3a0632010-07-29 21:42:50 +00001898 if (Ty->isIntegralOrEnumerationType() &&
1899 Ty->isPromotableIntegerType())
1900 return ABIArgInfo::getExtend();
1901 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001902
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001903 break;
1904
1905 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1906 // available SSE register is used, the registers are taken in the
1907 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001908 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001909 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001910 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001911 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001912 break;
1913 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001914 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001915
Chris Lattnera5f58b02011-07-09 17:41:47 +00001916 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001917 switch (Hi) {
1918 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00001919 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001920 // which is passed in memory.
1921 case Memory:
1922 case X87:
1923 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00001924 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001925
1926 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001927
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001928 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001929 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001930 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001931 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001932
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001933 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1934 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001935 break;
1936
1937 // X87Up generally doesn't occur here (long double is passed in
1938 // memory), except in situations involving unions.
1939 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001940 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001941 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001942
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001943 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1944 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001945
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001946 ++neededSSE;
1947 break;
1948
1949 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1950 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001951 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001952 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001953 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001954 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001955 break;
1956 }
1957
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001958 // If a high part was specified, merge it together with the low part. It is
1959 // known to pass in the high eightbyte of the result. We do this by forming a
1960 // first class struct aggregate with the high and low part: {low, high}
1961 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001962 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001963
Chris Lattner1f3a0632010-07-29 21:42:50 +00001964 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001965}
1966
Chris Lattner22326a12010-07-29 02:31:05 +00001967void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001968
Chris Lattner458b2aa2010-07-29 02:16:43 +00001969 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001970
1971 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001972 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001973
1974 // If the return value is indirect, then the hidden argument is consuming one
1975 // integer register.
1976 if (FI.getReturnInfo().isIndirect())
1977 --freeIntRegs;
1978
1979 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1980 // get assigned (in left-to-right order) for passing as follows...
1981 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1982 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00001983 unsigned neededInt, neededSSE;
1984 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001985
1986 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1987 // eightbyte of an argument, the whole argument is passed on the
1988 // stack. If registers have already been assigned for some
1989 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001990 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001991 freeIntRegs -= neededInt;
1992 freeSSERegs -= neededSSE;
1993 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001994 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001995 }
1996 }
1997}
1998
1999static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2000 QualType Ty,
2001 CodeGenFunction &CGF) {
2002 llvm::Value *overflow_arg_area_p =
2003 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2004 llvm::Value *overflow_arg_area =
2005 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2006
2007 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2008 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00002009 // It isn't stated explicitly in the standard, but in practice we use
2010 // alignment greater than 16 where necessary.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002011 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2012 if (Align > 8) {
Eli Friedmana1748562011-11-18 02:44:19 +00002013 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson41a75022009-08-13 21:57:51 +00002014 llvm::Value *Offset =
Eli Friedmana1748562011-11-18 02:44:19 +00002015 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002016 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2017 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002018 CGF.Int64Ty);
Eli Friedmana1748562011-11-18 02:44:19 +00002019 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002020 overflow_arg_area =
2021 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2022 overflow_arg_area->getType(),
2023 "overflow_arg_area.align");
2024 }
2025
2026 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00002027 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002028 llvm::Value *Res =
2029 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002030 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002031
2032 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2033 // l->overflow_arg_area + sizeof(type).
2034 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2035 // an 8 byte boundary.
2036
2037 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00002038 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002039 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002040 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2041 "overflow_arg_area.next");
2042 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2043
2044 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2045 return Res;
2046}
2047
2048llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2049 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00002050 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00002051
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002052 // Assume that va_list type is correct; should be pointer to LLVM type:
2053 // struct {
2054 // i32 gp_offset;
2055 // i32 fp_offset;
2056 // i8* overflow_arg_area;
2057 // i8* reg_save_area;
2058 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002059 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002060
Chris Lattner9723d6c2010-03-11 18:19:55 +00002061 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00002062 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002063
2064 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2065 // in the registers. If not go to step 7.
2066 if (!neededInt && !neededSSE)
2067 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2068
2069 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2070 // general purpose registers needed to pass type and num_fp to hold
2071 // the number of floating point registers needed.
2072
2073 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2074 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2075 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2076 //
2077 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2078 // register save space).
2079
2080 llvm::Value *InRegs = 0;
2081 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2082 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2083 if (neededInt) {
2084 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2085 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002086 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2087 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002088 }
2089
2090 if (neededSSE) {
2091 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2092 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2093 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002094 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2095 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002096 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2097 }
2098
2099 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2100 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2101 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2102 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2103
2104 // Emit code to load the value if it was passed in registers.
2105
2106 CGF.EmitBlock(InRegBlock);
2107
2108 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2109 // an offset of l->gp_offset and/or l->fp_offset. This may require
2110 // copying to a temporary location in case the parameter is passed
2111 // in different register classes or requires an alignment greater
2112 // than 8 for general purpose registers and 16 for XMM registers.
2113 //
2114 // FIXME: This really results in shameful code when we end up needing to
2115 // collect arguments from different places; often what should result in a
2116 // simple assembling of a structure from scattered addresses has many more
2117 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002118 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002119 llvm::Value *RegAddr =
2120 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2121 "reg_save_area");
2122 if (neededInt && neededSSE) {
2123 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002124 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002125 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002126 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2127 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002128 llvm::Type *TyLo = ST->getElementType(0);
2129 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002130 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002131 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002132 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2133 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002134 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2135 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002136 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2137 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002138 llvm::Value *V =
2139 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2140 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2141 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2142 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2143
Owen Anderson170229f2009-07-14 23:10:40 +00002144 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002145 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002146 } else if (neededInt) {
2147 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2148 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002149 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002150 } else if (neededSSE == 1) {
2151 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2152 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2153 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002154 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002155 assert(neededSSE == 2 && "Invalid number of needed registers!");
2156 // SSE registers are spaced 16 bytes apart in the register save
2157 // area, we need to collect the two eightbytes together.
2158 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002159 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foad7c57be32011-07-11 09:56:20 +00002160 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2192fe52011-07-18 04:24:23 +00002161 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002162 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2192fe52011-07-18 04:24:23 +00002163 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002164 DoubleTy, NULL);
2165 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2166 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2167 DblPtrTy));
2168 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2169 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2170 DblPtrTy));
2171 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2172 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2173 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002174 }
2175
2176 // AMD64-ABI 3.5.7p5: Step 5. Set:
2177 // l->gp_offset = l->gp_offset + num_gp * 8
2178 // l->fp_offset = l->fp_offset + num_fp * 16.
2179 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002180 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002181 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2182 gp_offset_p);
2183 }
2184 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002185 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002186 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2187 fp_offset_p);
2188 }
2189 CGF.EmitBranch(ContBlock);
2190
2191 // Emit code to load the value if it was passed in memory.
2192
2193 CGF.EmitBlock(InMemBlock);
2194 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2195
2196 // Return the appropriate result.
2197
2198 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002199 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002200 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002201 ResAddr->addIncoming(RegAddr, InRegBlock);
2202 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002203 return ResAddr;
2204}
2205
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002206ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2207
2208 if (Ty->isVoidType())
2209 return ABIArgInfo::getIgnore();
2210
2211 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2212 Ty = EnumTy->getDecl()->getIntegerType();
2213
2214 uint64_t Size = getContext().getTypeSize(Ty);
2215
2216 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002217 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2218 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002219 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2220
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002221 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2222 if (Size == 128 &&
Douglas Gregore8bbc122011-09-02 00:18:52 +00002223 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002224 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2225 Size));
2226
2227 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2228 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2229 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002230 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002231 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2232 Size));
2233
2234 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2235 }
2236
2237 if (Ty->isPromotableIntegerType())
2238 return ABIArgInfo::getExtend();
2239
2240 return ABIArgInfo::getDirect();
2241}
2242
2243void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2244
2245 QualType RetTy = FI.getReturnType();
2246 FI.getReturnInfo() = classify(RetTy);
2247
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002248 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2249 it != ie; ++it)
2250 it->info = classify(it->type);
2251}
2252
Chris Lattner04dc9572010-08-31 16:44:54 +00002253llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2254 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002255 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2256 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002257
Chris Lattner04dc9572010-08-31 16:44:54 +00002258 CGBuilderTy &Builder = CGF.Builder;
2259 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2260 "ap");
2261 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2262 llvm::Type *PTy =
2263 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2264 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2265
2266 uint64_t Offset =
2267 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2268 llvm::Value *NextAddr =
2269 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2270 "ap.next");
2271 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2272
2273 return AddrTyped;
2274}
Chris Lattner0cf24192010-06-28 20:05:43 +00002275
John McCallea8d8bb2010-03-11 00:10:12 +00002276// PowerPC-32
2277
2278namespace {
2279class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2280public:
Chris Lattner2b037972010-07-29 02:01:43 +00002281 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002282
John McCallea8d8bb2010-03-11 00:10:12 +00002283 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2284 // This is recovered from gcc output.
2285 return 1; // r1 is the dedicated stack pointer
2286 }
2287
2288 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002289 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002290};
2291
2292}
2293
2294bool
2295PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2296 llvm::Value *Address) const {
2297 // This is calculated from the LLVM and GCC tables and verified
2298 // against gcc output. AFAIK all ABIs use the same encoding.
2299
2300 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2301 llvm::LLVMContext &Context = CGF.getLLVMContext();
2302
Chris Lattner2192fe52011-07-18 04:24:23 +00002303 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallea8d8bb2010-03-11 00:10:12 +00002304 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2305 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2306 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2307
2308 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002309 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002310
2311 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002312 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002313
2314 // 64-76 are various 4-byte special-purpose registers:
2315 // 64: mq
2316 // 65: lr
2317 // 66: ctr
2318 // 67: ap
2319 // 68-75 cr0-7
2320 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002321 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002322
2323 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002324 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002325
2326 // 109: vrsave
2327 // 110: vscr
2328 // 111: spe_acc
2329 // 112: spefscr
2330 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002331 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002332
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002333 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002334}
2335
2336
Chris Lattner0cf24192010-06-28 20:05:43 +00002337//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002338// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002339//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002340
2341namespace {
2342
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002343class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002344public:
2345 enum ABIKind {
2346 APCS = 0,
2347 AAPCS = 1,
2348 AAPCS_VFP
2349 };
2350
2351private:
2352 ABIKind Kind;
2353
2354public:
Chris Lattner2b037972010-07-29 02:01:43 +00002355 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002356
John McCall3480ef22011-08-30 01:42:09 +00002357 bool isEABI() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +00002358 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall3480ef22011-08-30 01:42:09 +00002359 return (Env == "gnueabi" || Env == "eabi");
2360 }
2361
Daniel Dunbar020daa92009-09-12 01:00:39 +00002362private:
2363 ABIKind getABIKind() const { return Kind; }
2364
Chris Lattner458b2aa2010-07-29 02:16:43 +00002365 ABIArgInfo classifyReturnType(QualType RetTy) const;
2366 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002367
Chris Lattner22326a12010-07-29 02:31:05 +00002368 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002369
2370 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2371 CodeGenFunction &CGF) const;
2372};
2373
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002374class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2375public:
Chris Lattner2b037972010-07-29 02:01:43 +00002376 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2377 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002378
John McCall3480ef22011-08-30 01:42:09 +00002379 const ARMABIInfo &getABIInfo() const {
2380 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2381 }
2382
John McCallbeec5a02010-03-06 00:35:14 +00002383 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2384 return 13;
2385 }
Roman Divackyc1617352011-05-18 19:36:54 +00002386
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002387 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00002388 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2389 }
2390
Roman Divackyc1617352011-05-18 19:36:54 +00002391 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2392 llvm::Value *Address) const {
2393 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2394 llvm::LLVMContext &Context = CGF.getLLVMContext();
2395
Chris Lattner2192fe52011-07-18 04:24:23 +00002396 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divackyc1617352011-05-18 19:36:54 +00002397 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2398
2399 // 0-15 are the 16 integer registers.
2400 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2401
2402 return false;
2403 }
John McCall3480ef22011-08-30 01:42:09 +00002404
2405 unsigned getSizeOfUnwindException() const {
2406 if (getABIInfo().isEABI()) return 88;
2407 return TargetCodeGenInfo::getSizeOfUnwindException();
2408 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002409};
2410
Daniel Dunbard59655c2009-09-12 00:59:49 +00002411}
2412
Chris Lattner22326a12010-07-29 02:31:05 +00002413void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002414 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002415 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002416 it != ie; ++it)
2417 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002418
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002419 // Always honor user-specified calling convention.
2420 if (FI.getCallingConvention() != llvm::CallingConv::C)
2421 return;
2422
2423 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002424 llvm::CallingConv::ID DefaultCC;
John McCall3480ef22011-08-30 01:42:09 +00002425 if (isEABI())
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002426 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002427 else
2428 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002429
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002430 // If user did not ask for specific calling convention explicitly (e.g. via
2431 // pcs attribute), set effective calling convention if it's different than ABI
2432 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002433 switch (getABIKind()) {
2434 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002435 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2436 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002437 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002438 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002439 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2440 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002441 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002442 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002443 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2444 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002445 break;
2446 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002447}
2448
Bob Wilsone826a2a2011-08-03 05:58:22 +00002449/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2450/// aggregate. If HAMembers is non-null, the number of base elements
2451/// contained in the type is returned through it; this is used for the
2452/// recursive calls that check aggregate component types.
2453static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2454 ASTContext &Context,
2455 uint64_t *HAMembers = 0) {
2456 uint64_t Members;
2457 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2458 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2459 return false;
2460 Members *= AT->getSize().getZExtValue();
2461 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2462 const RecordDecl *RD = RT->getDecl();
2463 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2464 return false;
2465 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2466 if (!CXXRD->isAggregate())
2467 return false;
2468 }
2469 Members = 0;
2470 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2471 i != e; ++i) {
2472 const FieldDecl *FD = *i;
2473 uint64_t FldMembers;
2474 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2475 return false;
2476 Members += FldMembers;
2477 }
2478 } else {
2479 Members = 1;
2480 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2481 Members = 2;
2482 Ty = CT->getElementType();
2483 }
2484
2485 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2486 // double, or 64-bit or 128-bit vectors.
2487 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2488 if (BT->getKind() != BuiltinType::Float &&
2489 BT->getKind() != BuiltinType::Double)
2490 return false;
2491 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2492 unsigned VecSize = Context.getTypeSize(VT);
2493 if (VecSize != 64 && VecSize != 128)
2494 return false;
2495 } else {
2496 return false;
2497 }
2498
2499 // The base type must be the same for all members. Vector types of the
2500 // same total size are treated as being equivalent here.
2501 const Type *TyPtr = Ty.getTypePtr();
2502 if (!Base)
2503 Base = TyPtr;
2504 if (Base != TyPtr &&
2505 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2506 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2507 return false;
2508 }
2509
2510 // Homogeneous Aggregates can have at most 4 members of the base type.
2511 if (HAMembers)
2512 *HAMembers = Members;
2513 return (Members <= 4);
2514}
2515
Chris Lattner458b2aa2010-07-29 02:16:43 +00002516ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002517 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002518 // Treat an enum type as its underlying type.
2519 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2520 Ty = EnumTy->getDecl()->getIntegerType();
2521
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002522 return (Ty->isPromotableIntegerType() ?
2523 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002524 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002525
Daniel Dunbar09d33622009-09-14 21:54:03 +00002526 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002527 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002528 return ABIArgInfo::getIgnore();
2529
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002530 // Structures with either a non-trivial destructor or a non-trivial
2531 // copy constructor are always indirect.
2532 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2533 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2534
Bob Wilsone826a2a2011-08-03 05:58:22 +00002535 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2536 // Homogeneous Aggregates need to be expanded.
2537 const Type *Base = 0;
2538 if (isHomogeneousAggregate(Ty, Base, getContext()))
2539 return ABIArgInfo::getExpand();
2540 }
2541
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002542 // Otherwise, pass by coercing to a structure of the appropriate size.
2543 //
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002544 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2545 // backend doesn't support byval.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002546 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2192fe52011-07-18 04:24:23 +00002547 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002548 unsigned SizeRegs;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002549 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002550 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2551 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002552 } else {
2553 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2554 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002555 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002556
Chris Lattnera5f58b02011-07-09 17:41:47 +00002557 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002558 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002559 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002560}
2561
Chris Lattner458b2aa2010-07-29 02:16:43 +00002562static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002563 llvm::LLVMContext &VMContext) {
2564 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2565 // is called integer-like if its size is less than or equal to one word, and
2566 // the offset of each of its addressable sub-fields is zero.
2567
2568 uint64_t Size = Context.getTypeSize(Ty);
2569
2570 // Check that the type fits in a word.
2571 if (Size > 32)
2572 return false;
2573
2574 // FIXME: Handle vector types!
2575 if (Ty->isVectorType())
2576 return false;
2577
Daniel Dunbard53bac72009-09-14 02:20:34 +00002578 // Float types are never treated as "integer like".
2579 if (Ty->isRealFloatingType())
2580 return false;
2581
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002582 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002583 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002584 return true;
2585
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002586 // Small complex integer types are "integer like".
2587 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2588 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002589
2590 // Single element and zero sized arrays should be allowed, by the definition
2591 // above, but they are not.
2592
2593 // Otherwise, it must be a record type.
2594 const RecordType *RT = Ty->getAs<RecordType>();
2595 if (!RT) return false;
2596
2597 // Ignore records with flexible arrays.
2598 const RecordDecl *RD = RT->getDecl();
2599 if (RD->hasFlexibleArrayMember())
2600 return false;
2601
2602 // Check that all sub-fields are at offset 0, and are themselves "integer
2603 // like".
2604 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2605
2606 bool HadField = false;
2607 unsigned idx = 0;
2608 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2609 i != e; ++i, ++idx) {
2610 const FieldDecl *FD = *i;
2611
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002612 // Bit-fields are not addressable, we only need to verify they are "integer
2613 // like". We still have to disallow a subsequent non-bitfield, for example:
2614 // struct { int : 0; int x }
2615 // is non-integer like according to gcc.
2616 if (FD->isBitField()) {
2617 if (!RD->isUnion())
2618 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002619
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002620 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2621 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002622
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002623 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002624 }
2625
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002626 // Check if this field is at offset 0.
2627 if (Layout.getFieldOffset(idx) != 0)
2628 return false;
2629
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002630 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2631 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002632
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002633 // Only allow at most one field in a structure. This doesn't match the
2634 // wording above, but follows gcc in situations with a field following an
2635 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002636 if (!RD->isUnion()) {
2637 if (HadField)
2638 return false;
2639
2640 HadField = true;
2641 }
2642 }
2643
2644 return true;
2645}
2646
Chris Lattner458b2aa2010-07-29 02:16:43 +00002647ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002648 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002649 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002650
Daniel Dunbar19964db2010-09-23 01:54:32 +00002651 // Large vector types should be returned via memory.
2652 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2653 return ABIArgInfo::getIndirect(0);
2654
John McCalla1dee5302010-08-22 10:59:02 +00002655 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002656 // Treat an enum type as its underlying type.
2657 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2658 RetTy = EnumTy->getDecl()->getIntegerType();
2659
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002660 return (RetTy->isPromotableIntegerType() ?
2661 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002662 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002663
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002664 // Structures with either a non-trivial destructor or a non-trivial
2665 // copy constructor are always indirect.
2666 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2667 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2668
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002669 // Are we following APCS?
2670 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002671 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002672 return ABIArgInfo::getIgnore();
2673
Daniel Dunbareedf1512010-02-01 23:31:19 +00002674 // Complex types are all returned as packed integers.
2675 //
2676 // FIXME: Consider using 2 x vector types if the back end handles them
2677 // correctly.
2678 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002679 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002680 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002681
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002682 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002683 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002684 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002685 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002686 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002687 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002688 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002689 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2690 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002691 }
2692
2693 // Otherwise return in memory.
2694 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002695 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002696
2697 // Otherwise this is an AAPCS variant.
2698
Chris Lattner458b2aa2010-07-29 02:16:43 +00002699 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002700 return ABIArgInfo::getIgnore();
2701
Bob Wilson1d9269a2011-11-02 04:51:36 +00002702 // Check for homogeneous aggregates with AAPCS-VFP.
2703 if (getABIKind() == AAPCS_VFP) {
2704 const Type *Base = 0;
2705 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2706 // Homogeneous Aggregates are returned directly.
2707 return ABIArgInfo::getDirect();
2708 }
2709
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002710 // Aggregates <= 4 bytes are returned in r0; other aggregates
2711 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002712 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002713 if (Size <= 32) {
2714 // Return in the smallest viable integer type.
2715 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002716 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002717 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002718 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2719 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002720 }
2721
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002722 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002723}
2724
2725llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002726 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002727 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2728 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002729
2730 CGBuilderTy &Builder = CGF.Builder;
2731 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2732 "ap");
2733 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindola11d994b2011-08-02 22:33:37 +00002734 // Handle address alignment for type alignment > 32 bits
2735 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2736 if (TyAlign > 4) {
2737 assert((TyAlign & (TyAlign - 1)) == 0 &&
2738 "Alignment is not power of 2!");
2739 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2740 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2741 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2742 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2743 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002744 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002745 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002746 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2747
2748 uint64_t Offset =
2749 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2750 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002751 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002752 "ap.next");
2753 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2754
2755 return AddrTyped;
2756}
2757
Chris Lattner0cf24192010-06-28 20:05:43 +00002758//===----------------------------------------------------------------------===//
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002759// PTX ABI Implementation
2760//===----------------------------------------------------------------------===//
2761
2762namespace {
2763
2764class PTXABIInfo : public ABIInfo {
2765public:
2766 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2767
2768 ABIArgInfo classifyReturnType(QualType RetTy) const;
2769 ABIArgInfo classifyArgumentType(QualType Ty) const;
2770
2771 virtual void computeInfo(CGFunctionInfo &FI) const;
2772 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2773 CodeGenFunction &CFG) const;
2774};
2775
2776class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2777public:
2778 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2779 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00002780
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00002781 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2782 CodeGen::CodeGenModule &M) const;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002783};
2784
2785ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2786 if (RetTy->isVoidType())
2787 return ABIArgInfo::getIgnore();
2788 if (isAggregateTypeForABI(RetTy))
2789 return ABIArgInfo::getIndirect(0);
2790 return ABIArgInfo::getDirect();
2791}
2792
2793ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2794 if (isAggregateTypeForABI(Ty))
2795 return ABIArgInfo::getIndirect(0);
2796
2797 return ABIArgInfo::getDirect();
2798}
2799
2800void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2801 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2802 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2803 it != ie; ++it)
2804 it->info = classifyArgumentType(it->type);
2805
2806 // Always honor user-specified calling convention.
2807 if (FI.getCallingConvention() != llvm::CallingConv::C)
2808 return;
2809
2810 // Calling convention as default by an ABI.
2811 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002812 const LangOptions &LangOpts = getContext().getLangOptions();
2813 if (LangOpts.OpenCL || LangOpts.CUDA) {
2814 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002815 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski38031972011-10-05 17:58:44 +00002816 } else {
2817 // If we are in standard C/C++ mode, use the triple to decide on the default
2818 StringRef Env =
2819 getContext().getTargetInfo().getTriple().getEnvironmentName();
2820 if (Env == "device")
2821 DefaultCC = llvm::CallingConv::PTX_Device;
2822 else
2823 DefaultCC = llvm::CallingConv::PTX_Kernel;
2824 }
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002825 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski38031972011-10-05 17:58:44 +00002826
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002827}
2828
2829llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2830 CodeGenFunction &CFG) const {
2831 llvm_unreachable("PTX does not support varargs");
2832 return 0;
2833}
2834
Justin Holewinski38031972011-10-05 17:58:44 +00002835void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2836 llvm::GlobalValue *GV,
2837 CodeGen::CodeGenModule &M) const{
2838 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2839 if (!FD) return;
2840
2841 llvm::Function *F = cast<llvm::Function>(GV);
2842
2843 // Perform special handling in OpenCL mode
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002844 if (M.getLangOptions().OpenCL) {
Justin Holewinski38031972011-10-05 17:58:44 +00002845 // Use OpenCL function attributes to set proper calling conventions
2846 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00002847 if (FD->hasAttr<OpenCLKernelAttr>()) {
2848 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002849 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002850 // And kernel functions are not subject to inlining
2851 F->addFnAttr(llvm::Attribute::NoInline);
2852 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002853 }
Justin Holewinski38031972011-10-05 17:58:44 +00002854
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002855 // Perform special handling in CUDA mode.
2856 if (M.getLangOptions().CUDA) {
2857 // CUDA __global__ functions get a kernel calling convention. Since
2858 // __global__ functions cannot be called from the device, we do not
2859 // need to set the noinline attribute.
2860 if (FD->getAttr<CUDAGlobalAttr>())
2861 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002862 }
2863}
2864
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002865}
2866
2867//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00002868// MBlaze ABI Implementation
2869//===----------------------------------------------------------------------===//
2870
2871namespace {
2872
2873class MBlazeABIInfo : public ABIInfo {
2874public:
2875 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2876
2877 bool isPromotableIntegerType(QualType Ty) const;
2878
2879 ABIArgInfo classifyReturnType(QualType RetTy) const;
2880 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2881
2882 virtual void computeInfo(CGFunctionInfo &FI) const {
2883 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2884 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2885 it != ie; ++it)
2886 it->info = classifyArgumentType(it->type);
2887 }
2888
2889 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2890 CodeGenFunction &CGF) const;
2891};
2892
2893class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2894public:
2895 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2896 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2897 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2898 CodeGen::CodeGenModule &M) const;
2899};
2900
2901}
2902
2903bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2904 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2905 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2906 switch (BT->getKind()) {
2907 case BuiltinType::Bool:
2908 case BuiltinType::Char_S:
2909 case BuiltinType::Char_U:
2910 case BuiltinType::SChar:
2911 case BuiltinType::UChar:
2912 case BuiltinType::Short:
2913 case BuiltinType::UShort:
2914 return true;
2915 default:
2916 return false;
2917 }
2918 return false;
2919}
2920
2921llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2922 CodeGenFunction &CGF) const {
2923 // FIXME: Implement
2924 return 0;
2925}
2926
2927
2928ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2929 if (RetTy->isVoidType())
2930 return ABIArgInfo::getIgnore();
2931 if (isAggregateTypeForABI(RetTy))
2932 return ABIArgInfo::getIndirect(0);
2933
2934 return (isPromotableIntegerType(RetTy) ?
2935 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2936}
2937
2938ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2939 if (isAggregateTypeForABI(Ty))
2940 return ABIArgInfo::getIndirect(0);
2941
2942 return (isPromotableIntegerType(Ty) ?
2943 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2944}
2945
2946void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2947 llvm::GlobalValue *GV,
2948 CodeGen::CodeGenModule &M)
2949 const {
2950 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2951 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002952
Wesley Peck36a1f682010-12-19 19:57:51 +00002953 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2954 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2955 CC = llvm::CallingConv::MBLAZE_INTR;
2956 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2957 CC = llvm::CallingConv::MBLAZE_SVOL;
2958
2959 if (CC != llvm::CallingConv::C) {
2960 // Handle 'interrupt_handler' attribute:
2961 llvm::Function *F = cast<llvm::Function>(GV);
2962
2963 // Step 1: Set ISR calling convention.
2964 F->setCallingConv(CC);
2965
2966 // Step 2: Add attributes goodness.
2967 F->addFnAttr(llvm::Attribute::NoInline);
2968 }
2969
2970 // Step 3: Emit _interrupt_handler alias.
2971 if (CC == llvm::CallingConv::MBLAZE_INTR)
2972 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2973 "_interrupt_handler", GV, &M.getModule());
2974}
2975
2976
2977//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002978// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002979//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002980
2981namespace {
2982
2983class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2984public:
Chris Lattner2b037972010-07-29 02:01:43 +00002985 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2986 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002987 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2988 CodeGen::CodeGenModule &M) const;
2989};
2990
2991}
2992
2993void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2994 llvm::GlobalValue *GV,
2995 CodeGen::CodeGenModule &M) const {
2996 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2997 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2998 // Handle 'interrupt' attribute:
2999 llvm::Function *F = cast<llvm::Function>(GV);
3000
3001 // Step 1: Set ISR calling convention.
3002 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3003
3004 // Step 2: Add attributes goodness.
3005 F->addFnAttr(llvm::Attribute::NoInline);
3006
3007 // Step 3: Emit ISR vector alias.
3008 unsigned Num = attr->getNumber() + 0xffe0;
3009 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003010 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003011 GV, &M.getModule());
3012 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003013 }
3014}
3015
Chris Lattner0cf24192010-06-28 20:05:43 +00003016//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00003017// MIPS ABI Implementation. This works for both little-endian and
3018// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00003019//===----------------------------------------------------------------------===//
3020
John McCall943fae92010-05-27 06:19:26 +00003021namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00003022class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00003023 bool IsO32;
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00003024 unsigned MinABIStackAlignInBytes;
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003025 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003026public:
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00003027 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3028 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00003029
3030 ABIArgInfo classifyReturnType(QualType RetTy) const;
3031 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3032 virtual void computeInfo(CGFunctionInfo &FI) const;
3033 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3034 CodeGenFunction &CGF) const;
3035};
3036
John McCall943fae92010-05-27 06:19:26 +00003037class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003038 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00003039public:
Akira Hatanaka14378522011-11-02 23:14:57 +00003040 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3041 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3042 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00003043
3044 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3045 return 29;
3046 }
3047
3048 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003049 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00003050
3051 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003052 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00003053 }
John McCall943fae92010-05-27 06:19:26 +00003054};
3055}
3056
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003057// In N32/64, an aligned double precision floating point field is passed in
3058// a register.
3059llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3060 if (IsO32)
3061 return 0;
3062
3063 const RecordType *RT = Ty->getAsStructureType();
3064
3065 if (!RT)
3066 return 0;
3067
3068 const RecordDecl *RD = RT->getDecl();
3069 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3070 uint64_t StructSize = getContext().getTypeSize(Ty);
3071 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3072
3073 SmallVector<llvm::Type*, 8> ArgList;
3074 uint64_t LastOffset = 0;
3075 unsigned idx = 0;
3076 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3077
3078 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3079 i != e; ++i, ++idx) {
3080 const QualType Ty = (*i)->getType();
3081 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3082
3083 if (!BT || BT->getKind() != BuiltinType::Double)
3084 continue;
3085
3086 uint64_t Offset = Layout.getFieldOffset(idx);
3087 if (Offset % 64) // Ignore doubles that are not aligned.
3088 continue;
3089
3090 // Add ((Offset - LastOffset) / 64) args of type i64.
3091 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3092 ArgList.push_back(I64);
3093
3094 // Add double type.
3095 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3096 LastOffset = Offset + 64;
3097 }
3098
3099 // This structure doesn't have an aligned double field.
3100 if (!LastOffset)
3101 return 0;
3102
3103 // Add ((StructSize - LastOffset) / 64) args of type i64.
3104 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3105 ArgList.push_back(I64);
3106
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003107 // If the size of the remainder is not zero, add one more integer type to
3108 // ArgList.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003109 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003110 if (R)
3111 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003112
3113 return llvm::StructType::get(getVMContext(), ArgList);
3114}
3115
Akira Hatanakab579fe52011-06-02 00:09:17 +00003116ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3117 if (isAggregateTypeForABI(Ty)) {
3118 // Ignore empty aggregates.
3119 if (getContext().getTypeSize(Ty) == 0)
3120 return ABIArgInfo::getIgnore();
3121
Akira Hatanakadf425db2011-08-01 18:09:58 +00003122 // Records with non trivial destructors/constructors should not be passed
3123 // by value.
3124 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3125 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3126
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003127 llvm::Type *ResType;
3128 if ((ResType = HandleStructTy(Ty)))
3129 return ABIArgInfo::getDirect(ResType);
3130
Akira Hatanakab579fe52011-06-02 00:09:17 +00003131 return ABIArgInfo::getIndirect(0);
3132 }
3133
3134 // Treat an enum type as its underlying type.
3135 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3136 Ty = EnumTy->getDecl()->getIntegerType();
3137
3138 return (Ty->isPromotableIntegerType() ?
3139 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3140}
3141
3142ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3143 if (RetTy->isVoidType())
3144 return ABIArgInfo::getIgnore();
3145
3146 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanaka14378522011-11-02 23:14:57 +00003147 if ((IsO32 && RetTy->isAnyComplexType()) ||
3148 (!IsO32 && (getContext().getTypeSize(RetTy) <= 128)))
Akira Hatanakab579fe52011-06-02 00:09:17 +00003149 return ABIArgInfo::getDirect();
3150
3151 return ABIArgInfo::getIndirect(0);
3152 }
3153
3154 // Treat an enum type as its underlying type.
3155 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3156 RetTy = EnumTy->getDecl()->getIntegerType();
3157
3158 return (RetTy->isPromotableIntegerType() ?
3159 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3160}
3161
3162void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3163 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3164 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3165 it != ie; ++it)
3166 it->info = classifyArgumentType(it->type);
3167}
3168
3169llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3170 CodeGenFunction &CGF) const {
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003171 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3172 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3173
3174 CGBuilderTy &Builder = CGF.Builder;
3175 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3176 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3177 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3178 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3179 llvm::Value *AddrTyped;
3180
3181 if (TypeAlign > MinABIStackAlignInBytes) {
3182 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3183 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3184 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3185 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3186 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3187 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3188 }
3189 else
3190 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3191
3192 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanakaae31c7a2011-08-12 02:30:14 +00003193 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003194 uint64_t Offset =
3195 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3196 llvm::Value *NextAddr =
3197 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3198 "ap.next");
3199 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3200
3201 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003202}
3203
John McCall943fae92010-05-27 06:19:26 +00003204bool
3205MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3206 llvm::Value *Address) const {
3207 // This information comes from gcc's implementation, which seems to
3208 // as canonical as it gets.
3209
3210 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3211 llvm::LLVMContext &Context = CGF.getLLVMContext();
3212
3213 // Everything on MIPS is 4 bytes. Double-precision FP registers
3214 // are aliased to pairs of single-precision FP registers.
Chris Lattner2192fe52011-07-18 04:24:23 +00003215 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall943fae92010-05-27 06:19:26 +00003216 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3217
3218 // 0-31 are the general purpose registers, $0 - $31.
3219 // 32-63 are the floating-point registers, $f0 - $f31.
3220 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3221 // 66 is the (notional, I think) register for signal-handler return.
3222 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3223
3224 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3225 // They are one bit wide and ignored here.
3226
3227 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3228 // (coprocessor 1 is the FP unit)
3229 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3230 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3231 // 176-181 are the DSP accumulator registers.
3232 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3233
3234 return false;
3235}
3236
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003237//===----------------------------------------------------------------------===//
3238// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3239// Currently subclassed only to implement custom OpenCL C function attribute
3240// handling.
3241//===----------------------------------------------------------------------===//
3242
3243namespace {
3244
3245class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3246public:
3247 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3248 : DefaultTargetCodeGenInfo(CGT) {}
3249
3250 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3251 CodeGen::CodeGenModule &M) const;
3252};
3253
3254void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3255 llvm::GlobalValue *GV,
3256 CodeGen::CodeGenModule &M) const {
3257 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3258 if (!FD) return;
3259
3260 llvm::Function *F = cast<llvm::Function>(GV);
3261
3262 if (M.getLangOptions().OpenCL) {
3263 if (FD->hasAttr<OpenCLKernelAttr>()) {
3264 // OpenCL C Kernel functions are not subject to inlining
3265 F->addFnAttr(llvm::Attribute::NoInline);
3266
3267 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3268
3269 // Convert the reqd_work_group_size() attributes to metadata.
3270 llvm::LLVMContext &Context = F->getContext();
3271 llvm::NamedMDNode *OpenCLMetadata =
3272 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3273
3274 SmallVector<llvm::Value*, 5> Operands;
3275 Operands.push_back(F);
3276
3277 Operands.push_back(llvm::Constant::getIntegerValue(
3278 llvm::Type::getInt32Ty(Context),
3279 llvm::APInt(
3280 32,
3281 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3282 Operands.push_back(llvm::Constant::getIntegerValue(
3283 llvm::Type::getInt32Ty(Context),
3284 llvm::APInt(
3285 32,
3286 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3287 Operands.push_back(llvm::Constant::getIntegerValue(
3288 llvm::Type::getInt32Ty(Context),
3289 llvm::APInt(
3290 32,
3291 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3292
3293 // Add a boolean constant operand for "required" (true) or "hint" (false)
3294 // for implementing the work_group_size_hint attr later. Currently
3295 // always true as the hint is not yet implemented.
3296 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3297
3298 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3299 }
3300 }
3301 }
3302}
3303
3304}
John McCall943fae92010-05-27 06:19:26 +00003305
Chris Lattner2b037972010-07-29 02:01:43 +00003306const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003307 if (TheTargetCodeGenInfo)
3308 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003309
Douglas Gregore8bbc122011-09-02 00:18:52 +00003310 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00003311 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003312 default:
Chris Lattner2b037972010-07-29 02:01:43 +00003313 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003314
John McCall943fae92010-05-27 06:19:26 +00003315 case llvm::Triple::mips:
3316 case llvm::Triple::mipsel:
Akira Hatanaka14378522011-11-02 23:14:57 +00003317 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCall943fae92010-05-27 06:19:26 +00003318
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003319 case llvm::Triple::mips64:
3320 case llvm::Triple::mips64el:
Akira Hatanaka14378522011-11-02 23:14:57 +00003321 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003322
Daniel Dunbard59655c2009-09-12 00:59:49 +00003323 case llvm::Triple::arm:
3324 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003325 {
3326 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003327
Douglas Gregore8bbc122011-09-02 00:18:52 +00003328 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003329 Kind = ARMABIInfo::APCS;
3330 else if (CodeGenOpts.FloatABI == "hard")
3331 Kind = ARMABIInfo::AAPCS_VFP;
3332
3333 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3334 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00003335
John McCallea8d8bb2010-03-11 00:10:12 +00003336 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00003337 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00003338
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003339 case llvm::Triple::ptx32:
3340 case llvm::Triple::ptx64:
3341 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3342
Wesley Peck36a1f682010-12-19 19:57:51 +00003343 case llvm::Triple::mblaze:
3344 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3345
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003346 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003347 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003348
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003349 case llvm::Triple::tce:
3350 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3351
Eli Friedman33465822011-07-08 23:31:17 +00003352 case llvm::Triple::x86: {
Douglas Gregore8bbc122011-09-02 00:18:52 +00003353 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedman33465822011-07-08 23:31:17 +00003354
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003355 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003356 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003357 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003358
3359 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003360 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003361 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003362 case llvm::Triple::AuroraUX:
3363 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003364 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003365 case llvm::Triple::OpenBSD:
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003366 case llvm::Triple::NetBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003367 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003368 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003369
3370 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003371 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003372 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003373 }
Eli Friedman33465822011-07-08 23:31:17 +00003374 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003375
Daniel Dunbare3532f82009-08-24 08:52:16 +00003376 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00003377 switch (Triple.getOS()) {
3378 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003379 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003380 case llvm::Triple::Cygwin:
3381 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3382 default:
3383 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3384 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003385 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003386}