blob: 5be38978f26a5cbbe78b8d3c0f1715175d07b4e7 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar7230fa52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2192fe52011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenberger4921fe22011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall3480ef22011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
98 return 32;
99}
100
John McCallcbc038a2011-09-21 08:08:30 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(CallingConv CC) const {
102 // The following conventions are known to require this to be false:
103 // x86_stdcall
104 // MIPS
105 // For everything else, we just prefer false unless we opt out.
106 return false;
107}
108
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000109static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000110
111/// isEmptyField - Return true iff a the field is "empty", that is it
112/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000113static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
114 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000115 if (FD->isUnnamedBitfield())
116 return true;
117
118 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000119
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000120 // Constant arrays of empty records count as empty, strip them off.
121 if (AllowArrays)
122 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
123 FT = AT->getElementType();
124
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000125 const RecordType *RT = FT->getAs<RecordType>();
126 if (!RT)
127 return false;
128
129 // C++ record fields are never empty, at least in the Itanium ABI.
130 //
131 // FIXME: We should use a predicate for whether this behavior is true in the
132 // current ABI.
133 if (isa<CXXRecordDecl>(RT->getDecl()))
134 return false;
135
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000136 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000137}
138
139/// isEmptyRecord - Return true iff a structure contains only empty
140/// fields. Note that a structure with a flexible array member is not
141/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000142static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000143 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000144 if (!RT)
145 return 0;
146 const RecordDecl *RD = RT->getDecl();
147 if (RD->hasFlexibleArrayMember())
148 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000149
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000150 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000151 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000152 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
153 e = CXXRD->bases_end(); i != e; ++i)
154 if (!isEmptyRecord(Context, i->getType(), true))
155 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000156
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000157 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
158 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000159 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000160 return false;
161 return true;
162}
163
Anders Carlsson20759ad2009-09-16 15:53:40 +0000164/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
165/// a non-trivial destructor or a non-trivial copy constructor.
166static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
167 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
168 if (!RD)
169 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000170
Anders Carlsson20759ad2009-09-16 15:53:40 +0000171 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
172}
173
174/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
175/// a record type with either a non-trivial destructor or a non-trivial copy
176/// constructor.
177static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
178 const RecordType *RT = T->getAs<RecordType>();
179 if (!RT)
180 return false;
181
182 return hasNonTrivialDestructorOrCopyConstructor(RT);
183}
184
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000185/// isSingleElementStruct - Determine if a structure is a "single
186/// element struct", i.e. it has exactly one non-empty field or
187/// exactly one field which is itself a single element
188/// struct. Structures with flexible array members are never
189/// considered single element structs.
190///
191/// \return The field declaration for the single non-empty field, if
192/// it exists.
193static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
194 const RecordType *RT = T->getAsStructureType();
195 if (!RT)
196 return 0;
197
198 const RecordDecl *RD = RT->getDecl();
199 if (RD->hasFlexibleArrayMember())
200 return 0;
201
202 const Type *Found = 0;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000203
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000204 // If this is a C++ record, check the bases first.
205 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
206 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
207 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000208 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000209 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000210 continue;
211
212 // If we already found an element then this isn't a single-element struct.
213 if (Found)
214 return 0;
215
216 // If this is non-empty and not a single element struct, the composite
217 // cannot be a single element struct.
218 Found = isSingleElementStruct(i->getType(), Context);
219 if (!Found)
220 return 0;
221 }
222 }
223
224 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000225 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
226 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000227 const FieldDecl *FD = *i;
228 QualType FT = FD->getType();
229
230 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000231 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000232 continue;
233
234 // If we already found an element then this isn't a single-element
235 // struct.
236 if (Found)
237 return 0;
238
239 // Treat single element arrays as the element.
240 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
241 if (AT->getSize().getZExtValue() != 1)
242 break;
243 FT = AT->getElementType();
244 }
245
John McCalla1dee5302010-08-22 10:59:02 +0000246 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000247 Found = FT.getTypePtr();
248 } else {
249 Found = isSingleElementStruct(FT, Context);
250 if (!Found)
251 return 0;
252 }
253 }
254
Eli Friedmanee945342011-11-18 01:25:50 +0000255 // We don't consider a struct a single-element struct if it has
256 // padding beyond the element type.
257 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
258 return 0;
259
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000260 return Found;
261}
262
263static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000264 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000265 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
266 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000267 return false;
268
269 uint64_t Size = Context.getTypeSize(Ty);
270 return Size == 32 || Size == 64;
271}
272
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000273/// canExpandIndirectArgument - Test whether an argument type which is to be
274/// passed indirectly (on the stack) would have the equivalent layout if it was
275/// expanded into separate arguments. If so, we prefer to do the latter to avoid
276/// inhibiting optimizations.
277///
278// FIXME: This predicate is missing many cases, currently it just follows
279// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
280// should probably make this smarter, or better yet make the LLVM backend
281// capable of handling it.
282static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
283 // We can only expand structure types.
284 const RecordType *RT = Ty->getAs<RecordType>();
285 if (!RT)
286 return false;
287
288 // We can only expand (C) structures.
289 //
290 // FIXME: This needs to be generalized to handle classes as well.
291 const RecordDecl *RD = RT->getDecl();
292 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
293 return false;
294
Eli Friedmane5c85622011-11-18 01:32:26 +0000295 uint64_t Size = 0;
296
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000297 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
298 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299 const FieldDecl *FD = *i;
300
301 if (!is32Or64BitBasicType(FD->getType(), Context))
302 return false;
303
304 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
305 // how to expand them yet, and the predicate for telling if a bitfield still
306 // counts as "basic" is more complicated than what we were doing previously.
307 if (FD->isBitField())
308 return false;
Eli Friedmane5c85622011-11-18 01:32:26 +0000309
310 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000311 }
312
Eli Friedmane5c85622011-11-18 01:32:26 +0000313 // Make sure there are not any holes in the struct.
314 if (Size != Context.getTypeSize(Ty))
315 return false;
316
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000317 return true;
318}
319
320namespace {
321/// DefaultABIInfo - The default implementation for ABI specific
322/// details. This implementation provides information which results in
323/// self-consistent and sensible LLVM IR generation, but does not
324/// conform to any particular ABI.
325class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000326public:
327 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000328
Chris Lattner458b2aa2010-07-29 02:16:43 +0000329 ABIArgInfo classifyReturnType(QualType RetTy) const;
330 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000331
Chris Lattner22326a12010-07-29 02:31:05 +0000332 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000333 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000334 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
335 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000336 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000337 }
338
339 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
340 CodeGenFunction &CGF) const;
341};
342
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000343class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
344public:
Chris Lattner2b037972010-07-29 02:01:43 +0000345 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
346 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000347};
348
349llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
350 CodeGenFunction &CGF) const {
351 return 0;
352}
353
Chris Lattner458b2aa2010-07-29 02:16:43 +0000354ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung180319f2011-11-03 00:59:44 +0000355 if (isAggregateTypeForABI(Ty)) {
356 // Records with non trivial destructors/constructors should not be passed
357 // by value.
358 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
359 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
360
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000361 return ABIArgInfo::getIndirect(0);
Jan Wen Voung180319f2011-11-03 00:59:44 +0000362 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000363
Chris Lattner9723d6c2010-03-11 18:19:55 +0000364 // Treat an enum type as its underlying type.
365 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
366 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000367
Chris Lattner9723d6c2010-03-11 18:19:55 +0000368 return (Ty->isPromotableIntegerType() ?
369 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000370}
371
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000372ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
373 if (RetTy->isVoidType())
374 return ABIArgInfo::getIgnore();
375
376 if (isAggregateTypeForABI(RetTy))
377 return ABIArgInfo::getIndirect(0);
378
379 // Treat an enum type as its underlying type.
380 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
381 RetTy = EnumTy->getDecl()->getIntegerType();
382
383 return (RetTy->isPromotableIntegerType() ?
384 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
385}
386
Bill Wendling5cd41c42010-10-18 03:41:31 +0000387/// UseX86_MMXType - Return true if this is an MMX type that should use the special
388/// x86_mmx type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000389bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendling5cd41c42010-10-18 03:41:31 +0000390 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
391 // special x86_mmx type.
392 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
393 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
394 IRType->getScalarSizeInBits() != 64;
395}
396
Jay Foad7c57be32011-07-11 09:56:20 +0000397static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000398 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000399 llvm::Type* Ty) {
Bill Wendlingec9d2632011-03-07 22:47:14 +0000400 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000401 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
402 return Ty;
403}
404
Chris Lattner0cf24192010-06-28 20:05:43 +0000405//===----------------------------------------------------------------------===//
406// X86-32 ABI Implementation
407//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000408
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000409/// X86_32ABIInfo - The X86-32 ABI information.
410class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000411 static const unsigned MinABIStackAlignInBytes = 4;
412
David Chisnallde3a0692009-08-17 23:08:21 +0000413 bool IsDarwinVectorABI;
414 bool IsSmallStructInRegABI;
Eli Friedman33465822011-07-08 23:31:17 +0000415 bool IsMMXDisabled;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000416
417 static bool isRegisterSize(unsigned Size) {
418 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
419 }
420
421 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
422
Daniel Dunbar557893d2010-04-21 19:10:51 +0000423 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
424 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000425 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000426
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000427 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000428 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000429
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000430public:
Chris Lattner2b037972010-07-29 02:01:43 +0000431
Chris Lattner458b2aa2010-07-29 02:16:43 +0000432 ABIArgInfo classifyReturnType(QualType RetTy) const;
433 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000434
Chris Lattner22326a12010-07-29 02:31:05 +0000435 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000436 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000437 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
438 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000439 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000440 }
441
442 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
443 CodeGenFunction &CGF) const;
444
Eli Friedman33465822011-07-08 23:31:17 +0000445 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
446 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
447 IsMMXDisabled(m) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000448};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000450class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
451public:
Eli Friedman33465822011-07-08 23:31:17 +0000452 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
453 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000454
455 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
456 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000457
458 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
459 // Darwin uses different dwarf register numbers for EH.
460 if (CGM.isTargetDarwin()) return 5;
461
462 return 4;
463 }
464
465 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
466 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000467
Jay Foad7c57be32011-07-11 09:56:20 +0000468 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000469 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000470 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000471 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
472 }
473
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000474};
475
476}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000477
478/// shouldReturnTypeInRegister - Determine if the given type should be
479/// passed in a register (for the Darwin ABI).
480bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
481 ASTContext &Context) {
482 uint64_t Size = Context.getTypeSize(Ty);
483
484 // Type must be register sized.
485 if (!isRegisterSize(Size))
486 return false;
487
488 if (Ty->isVectorType()) {
489 // 64- and 128- bit vectors inside structures are not returned in
490 // registers.
491 if (Size == 64 || Size == 128)
492 return false;
493
494 return true;
495 }
496
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000497 // If this is a builtin, pointer, enum, complex type, member pointer, or
498 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000499 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000500 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000501 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000502 return true;
503
504 // Arrays are treated like records.
505 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
506 return shouldReturnTypeInRegister(AT->getElementType(), Context);
507
508 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000509 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000510 if (!RT) return false;
511
Anders Carlsson40446e82010-01-27 03:25:19 +0000512 // FIXME: Traverse bases here too.
513
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000514 // Structure types are passed in register if all fields would be
515 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000516 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
517 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000518 const FieldDecl *FD = *i;
519
520 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000521 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000522 continue;
523
524 // Check fields recursively.
525 if (!shouldReturnTypeInRegister(FD->getType(), Context))
526 return false;
527 }
528
529 return true;
530}
531
Chris Lattner458b2aa2010-07-29 02:16:43 +0000532ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
533 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000534 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000535
Chris Lattner458b2aa2010-07-29 02:16:43 +0000536 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000538 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000539 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000540
541 // 128-bit vectors are a special case; they are returned in
542 // registers and we need to make sure to pick a type the LLVM
543 // backend will like.
544 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000545 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000546 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000547
548 // Always return in register if it fits in a general purpose
549 // register, or if it is 64 bits and has a single element.
550 if ((Size == 8 || Size == 16 || Size == 32) ||
551 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000552 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000553 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000554
555 return ABIArgInfo::getIndirect(0);
556 }
557
558 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000559 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000560
John McCalla1dee5302010-08-22 10:59:02 +0000561 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000562 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000563 // Structures with either a non-trivial destructor or a non-trivial
564 // copy constructor are always indirect.
565 if (hasNonTrivialDestructorOrCopyConstructor(RT))
566 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000567
Anders Carlsson5789c492009-10-20 22:07:59 +0000568 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000569 if (RT->getDecl()->hasFlexibleArrayMember())
570 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000571 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000572
David Chisnallde3a0692009-08-17 23:08:21 +0000573 // If specified, structs and unions are always indirect.
574 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000575 return ABIArgInfo::getIndirect(0);
576
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000577 // Small structures which are register sized are generally returned
578 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000579 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
580 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000581
582 // As a special-case, if the struct is a "single-element" struct, and
583 // the field is of type "float" or "double", return it in a
584 // floating-point register. We apply a similar transformation for
585 // pointer types to improve the quality of the generated IR.
586 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
587 if (SeltTy->isRealFloatingType() || SeltTy->hasPointerRepresentation())
588 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
589
590 // FIXME: We should be able to narrow this integer in cases with dead
591 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000592 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000593 }
594
595 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000596 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000597
Chris Lattner458b2aa2010-07-29 02:16:43 +0000598 // Treat an enum type as its underlying type.
599 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
600 RetTy = EnumTy->getDecl()->getIntegerType();
601
602 return (RetTy->isPromotableIntegerType() ?
603 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000604}
605
Daniel Dunbared23de32010-09-16 20:42:00 +0000606static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
607 const RecordType *RT = Ty->getAs<RecordType>();
608 if (!RT)
609 return 0;
610 const RecordDecl *RD = RT->getDecl();
611
612 // If this is a C++ record, check the bases first.
613 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
614 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
615 e = CXXRD->bases_end(); i != e; ++i)
616 if (!isRecordWithSSEVectorType(Context, i->getType()))
617 return false;
618
619 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
620 i != e; ++i) {
621 QualType FT = i->getType();
622
623 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
624 return true;
625
626 if (isRecordWithSSEVectorType(Context, FT))
627 return true;
628 }
629
630 return false;
631}
632
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000633unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
634 unsigned Align) const {
635 // Otherwise, if the alignment is less than or equal to the minimum ABI
636 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000637 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000638 return 0; // Use default alignment.
639
640 // On non-Darwin, the stack type alignment is always 4.
641 if (!IsDarwinVectorABI) {
642 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000643 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000644 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000645
Daniel Dunbared23de32010-09-16 20:42:00 +0000646 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
647 if (isRecordWithSSEVectorType(getContext(), Ty))
648 return 16;
649
650 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000651}
652
Chris Lattner458b2aa2010-07-29 02:16:43 +0000653ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000654 if (!ByVal)
655 return ABIArgInfo::getIndirect(0, false);
656
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000657 // Compute the byval alignment.
658 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
659 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
660 if (StackAlign == 0)
Chris Lattnere76b95a2011-05-22 23:35:00 +0000661 return ABIArgInfo::getIndirect(4);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000662
663 // If the stack alignment is less than the type alignment, realign the
664 // argument.
665 if (StackAlign < TypeAlign)
666 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
667 /*Realign=*/true);
668
669 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000670}
671
Chris Lattner458b2aa2010-07-29 02:16:43 +0000672ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000673 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000674 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000675 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000676 if (const RecordType *RT = Ty->getAs<RecordType>()) {
677 // Structures with either a non-trivial destructor or a non-trivial
678 // copy constructor are always indirect.
679 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000680 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000681
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000682 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000683 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000684 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000685
Eli Friedman9f061a32011-11-18 00:28:11 +0000686 // Ignore empty structs/unions.
687 if (Ty->isRecordType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000688 return ABIArgInfo::getIgnore();
689
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000690 // Expand small (<= 128-bit) record types when we know that the stack layout
691 // of those arguments will match the struct. This is important because the
692 // LLVM backend isn't smart enough to remove byval, which inhibits many
693 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000694 if (getContext().getTypeSize(Ty) <= 4*32 &&
695 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000696 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000697
Chris Lattner458b2aa2010-07-29 02:16:43 +0000698 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000699 }
700
Chris Lattnerd774ae92010-08-26 20:05:13 +0000701 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000702 // On Darwin, some vectors are passed in memory, we handle this by passing
703 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000704 if (IsDarwinVectorABI) {
705 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000706 if ((Size == 8 || Size == 16 || Size == 32) ||
707 (Size == 64 && VT->getNumElements() == 1))
708 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
709 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000710 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000711
Chris Lattnera5f58b02011-07-09 17:41:47 +0000712 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +0000713 if (UseX86_MMXType(IRType)) {
Eli Friedman33465822011-07-08 23:31:17 +0000714 if (IsMMXDisabled)
715 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
716 64));
Bill Wendling5cd41c42010-10-18 03:41:31 +0000717 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
718 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
719 return AAI;
720 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000721
Chris Lattnerd774ae92010-08-26 20:05:13 +0000722 return ABIArgInfo::getDirect();
723 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000724
725
Chris Lattner458b2aa2010-07-29 02:16:43 +0000726 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
727 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000728
Chris Lattner458b2aa2010-07-29 02:16:43 +0000729 return (Ty->isPromotableIntegerType() ?
730 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000731}
732
733llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
734 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +0000735 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
736 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000737
738 CGBuilderTy &Builder = CGF.Builder;
739 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
740 "ap");
741 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
742 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000743 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000744 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
745
746 uint64_t Offset =
747 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
748 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000749 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750 "ap.next");
751 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
752
753 return AddrTyped;
754}
755
Charles Davis4ea31ab2010-02-13 15:54:06 +0000756void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
757 llvm::GlobalValue *GV,
758 CodeGen::CodeGenModule &CGM) const {
759 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
760 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
761 // Get the LLVM function.
762 llvm::Function *Fn = cast<llvm::Function>(GV);
763
764 // Now add the 'alignstack' attribute with a value of 16.
765 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
766 }
767 }
768}
769
John McCallbeec5a02010-03-06 00:35:14 +0000770bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
771 CodeGen::CodeGenFunction &CGF,
772 llvm::Value *Address) const {
773 CodeGen::CGBuilderTy &Builder = CGF.Builder;
774 llvm::LLVMContext &Context = CGF.getLLVMContext();
775
Chris Lattner2192fe52011-07-18 04:24:23 +0000776 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallbeec5a02010-03-06 00:35:14 +0000777 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000778
John McCallbeec5a02010-03-06 00:35:14 +0000779 // 0-7 are the eight integer registers; the order is different
780 // on Darwin (for EH), but the range is the same.
781 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000782 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000783
784 if (CGF.CGM.isTargetDarwin()) {
785 // 12-16 are st(0..4). Not sure why we stop at 4.
786 // These have size 16, which is sizeof(long double) on
787 // platforms with 8-byte alignment for that type.
788 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000789 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000790
John McCallbeec5a02010-03-06 00:35:14 +0000791 } else {
792 // 9 is %eflags, which doesn't get a size on Darwin for some
793 // reason.
794 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
795
796 // 11-16 are st(0..5). Not sure why we stop at 5.
797 // These have size 12, which is sizeof(long double) on
798 // platforms with 4-byte alignment for that type.
799 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000800 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
801 }
John McCallbeec5a02010-03-06 00:35:14 +0000802
803 return false;
804}
805
Chris Lattner0cf24192010-06-28 20:05:43 +0000806//===----------------------------------------------------------------------===//
807// X86-64 ABI Implementation
808//===----------------------------------------------------------------------===//
809
810
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000811namespace {
812/// X86_64ABIInfo - The X86_64 ABI information.
813class X86_64ABIInfo : public ABIInfo {
814 enum Class {
815 Integer = 0,
816 SSE,
817 SSEUp,
818 X87,
819 X87Up,
820 ComplexX87,
821 NoClass,
822 Memory
823 };
824
825 /// merge - Implement the X86_64 ABI merging algorithm.
826 ///
827 /// Merge an accumulating classification \arg Accum with a field
828 /// classification \arg Field.
829 ///
830 /// \param Accum - The accumulating classification. This should
831 /// always be either NoClass or the result of a previous merge
832 /// call. In addition, this should never be Memory (the caller
833 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000834 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000835
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000836 /// postMerge - Implement the X86_64 ABI post merging algorithm.
837 ///
838 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
839 /// final MEMORY or SSE classes when necessary.
840 ///
841 /// \param AggregateSize - The size of the current aggregate in
842 /// the classification process.
843 ///
844 /// \param Lo - The classification for the parts of the type
845 /// residing in the low word of the containing object.
846 ///
847 /// \param Hi - The classification for the parts of the type
848 /// residing in the higher words of the containing object.
849 ///
850 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
851
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000852 /// classify - Determine the x86_64 register classes in which the
853 /// given type T should be passed.
854 ///
855 /// \param Lo - The classification for the parts of the type
856 /// residing in the low word of the containing object.
857 ///
858 /// \param Hi - The classification for the parts of the type
859 /// residing in the high word of the containing object.
860 ///
861 /// \param OffsetBase - The bit offset of this type in the
862 /// containing object. Some parameters are classified different
863 /// depending on whether they straddle an eightbyte boundary.
864 ///
865 /// If a word is unused its result will be NoClass; if a type should
866 /// be passed in Memory then at least the classification of \arg Lo
867 /// will be Memory.
868 ///
869 /// The \arg Lo class will be NoClass iff the argument is ignored.
870 ///
871 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
872 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000873 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000874
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000875 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000876 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
877 unsigned IROffset, QualType SourceTy,
878 unsigned SourceOffset) const;
879 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
880 unsigned IROffset, QualType SourceTy,
881 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000882
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000883 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000884 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000885 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000886
887 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000888 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000889 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000890
Chris Lattner458b2aa2010-07-29 02:16:43 +0000891 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000892
Bill Wendling5cd41c42010-10-18 03:41:31 +0000893 ABIArgInfo classifyArgumentType(QualType Ty,
894 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000895 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000896
John McCalle0fda732011-04-21 01:20:55 +0000897 /// The 0.98 ABI revision clarified a lot of ambiguities,
898 /// unfortunately in ways that were not always consistent with
899 /// certain previous compilers. In particular, platforms which
900 /// required strict binary compatibility with older versions of GCC
901 /// may need to exempt themselves.
902 bool honorsRevision0_98() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000903 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +0000904 }
905
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000906public:
Chris Lattner2b037972010-07-29 02:01:43 +0000907 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000908
Chris Lattner22326a12010-07-29 02:31:05 +0000909 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000910
911 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
912 CodeGenFunction &CGF) const;
913};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000914
Chris Lattner04dc9572010-08-31 16:44:54 +0000915/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000916class WinX86_64ABIInfo : public ABIInfo {
917
918 ABIArgInfo classify(QualType Ty) const;
919
Chris Lattner04dc9572010-08-31 16:44:54 +0000920public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000921 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
922
923 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000924
925 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
926 CodeGenFunction &CGF) const;
927};
928
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000929class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
930public:
Chris Lattner2b037972010-07-29 02:01:43 +0000931 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
932 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000933
934 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
935 return 7;
936 }
937
938 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
939 llvm::Value *Address) const {
940 CodeGen::CGBuilderTy &Builder = CGF.Builder;
941 llvm::LLVMContext &Context = CGF.getLLVMContext();
942
Chris Lattner2192fe52011-07-18 04:24:23 +0000943 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallbeec5a02010-03-06 00:35:14 +0000944 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000945
John McCall943fae92010-05-27 06:19:26 +0000946 // 0-15 are the 16 integer registers.
947 // 16 is %rip.
948 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000949
950 return false;
951 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000952
Jay Foad7c57be32011-07-11 09:56:20 +0000953 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000954 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000955 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000956 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
957 }
958
John McCallcbc038a2011-09-21 08:08:30 +0000959 bool isNoProtoCallVariadic(CallingConv CC) const {
960 // The default CC on x86-64 sets %al to the number of SSA
961 // registers used, and GCC sets this when calling an unprototyped
962 // function, so we override the default behavior.
963 if (CC == CC_Default || CC == CC_C) return true;
964
965 return TargetCodeGenInfo::isNoProtoCallVariadic(CC);
966 }
967
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000968};
969
Chris Lattner04dc9572010-08-31 16:44:54 +0000970class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
971public:
972 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
973 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
974
975 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
976 return 7;
977 }
978
979 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
980 llvm::Value *Address) const {
981 CodeGen::CGBuilderTy &Builder = CGF.Builder;
982 llvm::LLVMContext &Context = CGF.getLLVMContext();
983
Chris Lattner2192fe52011-07-18 04:24:23 +0000984 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattner04dc9572010-08-31 16:44:54 +0000985 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000986
Chris Lattner04dc9572010-08-31 16:44:54 +0000987 // 0-15 are the 16 integer registers.
988 // 16 is %rip.
989 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
990
991 return false;
992 }
993};
994
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000995}
996
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000997void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
998 Class &Hi) const {
999 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1000 //
1001 // (a) If one of the classes is Memory, the whole argument is passed in
1002 // memory.
1003 //
1004 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1005 // memory.
1006 //
1007 // (c) If the size of the aggregate exceeds two eightbytes and the first
1008 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1009 // argument is passed in memory. NOTE: This is necessary to keep the
1010 // ABI working for processors that don't support the __m256 type.
1011 //
1012 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1013 //
1014 // Some of these are enforced by the merging logic. Others can arise
1015 // only with unions; for example:
1016 // union { _Complex double; unsigned; }
1017 //
1018 // Note that clauses (b) and (c) were added in 0.98.
1019 //
1020 if (Hi == Memory)
1021 Lo = Memory;
1022 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1023 Lo = Memory;
1024 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1025 Lo = Memory;
1026 if (Hi == SSEUp && Lo != SSE)
1027 Hi = SSE;
1028}
1029
Chris Lattnerd776fb12010-06-28 21:43:59 +00001030X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001031 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1032 // classified recursively so that always two fields are
1033 // considered. The resulting class is calculated according to
1034 // the classes of the fields in the eightbyte:
1035 //
1036 // (a) If both classes are equal, this is the resulting class.
1037 //
1038 // (b) If one of the classes is NO_CLASS, the resulting class is
1039 // the other class.
1040 //
1041 // (c) If one of the classes is MEMORY, the result is the MEMORY
1042 // class.
1043 //
1044 // (d) If one of the classes is INTEGER, the result is the
1045 // INTEGER.
1046 //
1047 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1048 // MEMORY is used as class.
1049 //
1050 // (f) Otherwise class SSE is used.
1051
1052 // Accum should never be memory (we should have returned) or
1053 // ComplexX87 (because this cannot be passed in a structure).
1054 assert((Accum != Memory && Accum != ComplexX87) &&
1055 "Invalid accumulated classification during merge.");
1056 if (Accum == Field || Field == NoClass)
1057 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001058 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001059 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001060 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001061 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001062 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001063 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001064 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1065 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001066 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001067 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001068}
1069
Chris Lattner5c740f12010-06-30 19:14:05 +00001070void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001071 Class &Lo, Class &Hi) const {
1072 // FIXME: This code can be simplified by introducing a simple value class for
1073 // Class pairs with appropriate constructor methods for the various
1074 // situations.
1075
1076 // FIXME: Some of the split computations are wrong; unaligned vectors
1077 // shouldn't be passed in registers for example, so there is no chance they
1078 // can straddle an eightbyte. Verify & simplify.
1079
1080 Lo = Hi = NoClass;
1081
1082 Class &Current = OffsetBase < 64 ? Lo : Hi;
1083 Current = Memory;
1084
John McCall9dd450b2009-09-21 23:43:11 +00001085 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001086 BuiltinType::Kind k = BT->getKind();
1087
1088 if (k == BuiltinType::Void) {
1089 Current = NoClass;
1090 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1091 Lo = Integer;
1092 Hi = Integer;
1093 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1094 Current = Integer;
1095 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1096 Current = SSE;
1097 } else if (k == BuiltinType::LongDouble) {
1098 Lo = X87;
1099 Hi = X87Up;
1100 }
1101 // FIXME: _Decimal32 and _Decimal64 are SSE.
1102 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001103 return;
1104 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001105
Chris Lattnerd776fb12010-06-28 21:43:59 +00001106 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001107 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001108 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001109 return;
1110 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001111
Chris Lattnerd776fb12010-06-28 21:43:59 +00001112 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001113 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001114 return;
1115 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001116
Chris Lattnerd776fb12010-06-28 21:43:59 +00001117 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001118 if (Ty->isMemberFunctionPointerType())
1119 Lo = Hi = Integer;
1120 else
1121 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001122 return;
1123 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001124
Chris Lattnerd776fb12010-06-28 21:43:59 +00001125 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001126 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001127 if (Size == 32) {
1128 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1129 // float> as integer.
1130 Current = Integer;
1131
1132 // If this type crosses an eightbyte boundary, it should be
1133 // split.
1134 uint64_t EB_Real = (OffsetBase) / 64;
1135 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1136 if (EB_Real != EB_Imag)
1137 Hi = Lo;
1138 } else if (Size == 64) {
1139 // gcc passes <1 x double> in memory. :(
1140 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1141 return;
1142
1143 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001144 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001145 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1146 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1147 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001148 Current = Integer;
1149 else
1150 Current = SSE;
1151
1152 // If this type crosses an eightbyte boundary, it should be
1153 // split.
1154 if (OffsetBase && OffsetBase != 64)
1155 Hi = Lo;
Bruno Cardoso Lopes37b7fd02011-07-12 02:47:38 +00001156 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001157 // Arguments of 256-bits are split into four eightbyte chunks. The
1158 // least significant one belongs to class SSE and all the others to class
1159 // SSEUP. The original Lo and Hi design considers that types can't be
1160 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1161 // This design isn't correct for 256-bits, but since there're no cases
1162 // where the upper parts would need to be inspected, avoid adding
1163 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001164 Lo = SSE;
1165 Hi = SSEUp;
1166 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001167 return;
1168 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001169
Chris Lattnerd776fb12010-06-28 21:43:59 +00001170 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001171 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001172
Chris Lattner2b037972010-07-29 02:01:43 +00001173 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001174 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001175 if (Size <= 64)
1176 Current = Integer;
1177 else if (Size <= 128)
1178 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001179 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001180 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001181 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001182 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001183 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001184 Current = ComplexX87;
1185
1186 // If this complex type crosses an eightbyte boundary then it
1187 // should be split.
1188 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001189 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001190 if (Hi == NoClass && EB_Real != EB_Imag)
1191 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001192
Chris Lattnerd776fb12010-06-28 21:43:59 +00001193 return;
1194 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001195
Chris Lattner2b037972010-07-29 02:01:43 +00001196 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001197 // Arrays are treated like structures.
1198
Chris Lattner2b037972010-07-29 02:01:43 +00001199 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001200
1201 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001202 // than four eightbytes, ..., it has class MEMORY.
1203 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001204 return;
1205
1206 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1207 // fields, it has class MEMORY.
1208 //
1209 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001210 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001211 return;
1212
1213 // Otherwise implement simplified merge. We could be smarter about
1214 // this, but it isn't worth it and would be harder to verify.
1215 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001216 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001217 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001218
1219 // The only case a 256-bit wide vector could be used is when the array
1220 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1221 // to work for sizes wider than 128, early check and fallback to memory.
1222 if (Size > 128 && EltSize != 256)
1223 return;
1224
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001225 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1226 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001227 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001228 Lo = merge(Lo, FieldLo);
1229 Hi = merge(Hi, FieldHi);
1230 if (Lo == Memory || Hi == Memory)
1231 break;
1232 }
1233
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001234 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001235 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001236 return;
1237 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001238
Chris Lattnerd776fb12010-06-28 21:43:59 +00001239 if (const RecordType *RT = Ty->getAs<RecordType>()) {
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
Anders Carlsson20759ad2009-09-16 15:53:40 +00001247 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1248 // copy constructor or a non-trivial destructor, it is passed by invisible
1249 // reference.
1250 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1251 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001252
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001253 const RecordDecl *RD = RT->getDecl();
1254
1255 // Assume variable sized types are passed in memory.
1256 if (RD->hasFlexibleArrayMember())
1257 return;
1258
Chris Lattner2b037972010-07-29 02:01:43 +00001259 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001260
1261 // Reset Lo class, this will be recomputed.
1262 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001263
1264 // If this is a C++ record, classify the bases first.
1265 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1266 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1267 e = CXXRD->bases_end(); i != e; ++i) {
1268 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1269 "Unexpected base class!");
1270 const CXXRecordDecl *Base =
1271 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1272
1273 // Classify this field.
1274 //
1275 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1276 // single eightbyte, each is classified separately. Each eightbyte gets
1277 // initialized to class NO_CLASS.
1278 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001279 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001280 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001281 Lo = merge(Lo, FieldLo);
1282 Hi = merge(Hi, FieldHi);
1283 if (Lo == Memory || Hi == Memory)
1284 break;
1285 }
1286 }
1287
1288 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001289 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001290 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001291 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001292 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1293 bool BitField = i->isBitField();
1294
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001295 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1296 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001297 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001298 // The only case a 256-bit wide vector could be used is when the struct
1299 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1300 // to work for sizes wider than 128, early check and fallback to memory.
1301 //
1302 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1303 Lo = Memory;
1304 return;
1305 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001306 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001307 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001308 Lo = Memory;
1309 return;
1310 }
1311
1312 // Classify this field.
1313 //
1314 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1315 // exceeds a single eightbyte, each is classified
1316 // separately. Each eightbyte gets initialized to class
1317 // NO_CLASS.
1318 Class FieldLo, FieldHi;
1319
1320 // Bit-fields require special handling, they do not force the
1321 // structure to be passed in memory even if unaligned, and
1322 // therefore they can straddle an eightbyte.
1323 if (BitField) {
1324 // Ignore padding bit-fields.
1325 if (i->isUnnamedBitfield())
1326 continue;
1327
1328 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001329 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001330
1331 uint64_t EB_Lo = Offset / 64;
1332 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1333 FieldLo = FieldHi = NoClass;
1334 if (EB_Lo) {
1335 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1336 FieldLo = NoClass;
1337 FieldHi = Integer;
1338 } else {
1339 FieldLo = Integer;
1340 FieldHi = EB_Hi ? Integer : NoClass;
1341 }
1342 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001343 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001344 Lo = merge(Lo, FieldLo);
1345 Hi = merge(Hi, FieldHi);
1346 if (Lo == Memory || Hi == Memory)
1347 break;
1348 }
1349
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001350 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001351 }
1352}
1353
Chris Lattner22a931e2010-06-29 06:01:59 +00001354ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001355 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1356 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001357 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001358 // Treat an enum type as its underlying type.
1359 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1360 Ty = EnumTy->getDecl()->getIntegerType();
1361
1362 return (Ty->isPromotableIntegerType() ?
1363 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1364 }
1365
1366 return ABIArgInfo::getIndirect(0);
1367}
1368
Chris Lattner22a931e2010-06-29 06:01:59 +00001369ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001370 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1371 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001372 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001373 // Treat an enum type as its underlying type.
1374 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1375 Ty = EnumTy->getDecl()->getIntegerType();
1376
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001377 return (Ty->isPromotableIntegerType() ?
1378 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001379 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001380
Daniel Dunbar53fac692010-04-21 19:49:55 +00001381 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1382 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001383
Chris Lattner44c2b902011-05-22 23:21:23 +00001384 // Compute the byval alignment. We specify the alignment of the byval in all
1385 // cases so that the mid-level optimizer knows the alignment of the byval.
1386 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1387 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001388}
1389
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001390/// GetByteVectorType - The ABI specifies that a value should be passed in an
1391/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001392/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001393llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001394 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001395
Chris Lattner9fa15c32010-07-29 05:02:29 +00001396 // Wrapper structs that just contain vectors are passed just like vectors,
1397 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001398 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001399 while (STy && STy->getNumElements() == 1) {
1400 IRType = STy->getElementType(0);
1401 STy = dyn_cast<llvm::StructType>(IRType);
1402 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001403
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001404 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001405 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1406 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001407 unsigned BitWidth = VT->getBitWidth();
1408 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001409 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1410 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1411 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1412 EltTy->isIntegerTy(128)))
1413 return VT;
1414 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001415
Chris Lattner4200fe42010-07-29 04:56:46 +00001416 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1417}
1418
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001419/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1420/// is known to either be off the end of the specified type or being in
1421/// alignment padding. The user type specified is known to be at most 128 bits
1422/// in size, and have passed through X86_64ABIInfo::classify with a successful
1423/// classification that put one of the two halves in the INTEGER class.
1424///
1425/// It is conservatively correct to return false.
1426static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1427 unsigned EndBit, ASTContext &Context) {
1428 // If the bytes being queried are off the end of the type, there is no user
1429 // data hiding here. This handles analysis of builtins, vectors and other
1430 // types that don't contain interesting padding.
1431 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1432 if (TySize <= StartBit)
1433 return true;
1434
Chris Lattner98076a22010-07-29 07:43:55 +00001435 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1436 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1437 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1438
1439 // Check each element to see if the element overlaps with the queried range.
1440 for (unsigned i = 0; i != NumElts; ++i) {
1441 // If the element is after the span we care about, then we're done..
1442 unsigned EltOffset = i*EltSize;
1443 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001444
Chris Lattner98076a22010-07-29 07:43:55 +00001445 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1446 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1447 EndBit-EltOffset, Context))
1448 return false;
1449 }
1450 // If it overlaps no elements, then it is safe to process as padding.
1451 return true;
1452 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001453
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001454 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1455 const RecordDecl *RD = RT->getDecl();
1456 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001457
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001458 // If this is a C++ record, check the bases first.
1459 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1460 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1461 e = CXXRD->bases_end(); i != e; ++i) {
1462 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1463 "Unexpected base class!");
1464 const CXXRecordDecl *Base =
1465 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001466
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001467 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001468 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001469 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001470
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001471 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1472 if (!BitsContainNoUserData(i->getType(), BaseStart,
1473 EndBit-BaseOffset, Context))
1474 return false;
1475 }
1476 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001477
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001478 // Verify that no field has data that overlaps the region of interest. Yes
1479 // this could be sped up a lot by being smarter about queried fields,
1480 // however we're only looking at structs up to 16 bytes, so we don't care
1481 // much.
1482 unsigned idx = 0;
1483 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1484 i != e; ++i, ++idx) {
1485 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001486
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001487 // If we found a field after the region we care about, then we're done.
1488 if (FieldOffset >= EndBit) break;
1489
1490 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1491 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1492 Context))
1493 return false;
1494 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001495
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001496 // If nothing in this record overlapped the area of interest, then we're
1497 // clean.
1498 return true;
1499 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001500
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001501 return false;
1502}
1503
Chris Lattnere556a712010-07-29 18:39:32 +00001504/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1505/// float member at the specified offset. For example, {int,{float}} has a
1506/// float at offset 4. It is conservatively correct for this routine to return
1507/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00001508static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnere556a712010-07-29 18:39:32 +00001509 const llvm::TargetData &TD) {
1510 // Base case if we find a float.
1511 if (IROffset == 0 && IRType->isFloatTy())
1512 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001513
Chris Lattnere556a712010-07-29 18:39:32 +00001514 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001515 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00001516 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1517 unsigned Elt = SL->getElementContainingOffset(IROffset);
1518 IROffset -= SL->getElementOffset(Elt);
1519 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1520 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001521
Chris Lattnere556a712010-07-29 18:39:32 +00001522 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001523 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1524 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00001525 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1526 IROffset -= IROffset/EltSize*EltSize;
1527 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1528 }
1529
1530 return false;
1531}
1532
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001533
1534/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1535/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001536llvm::Type *X86_64ABIInfo::
1537GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001538 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001539 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001540 // pass as float if the last 4 bytes is just padding. This happens for
1541 // structs that contain 3 floats.
1542 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1543 SourceOffset*8+64, getContext()))
1544 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001545
Chris Lattnere556a712010-07-29 18:39:32 +00001546 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1547 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1548 // case.
1549 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001550 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1551 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001552
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001553 return llvm::Type::getDoubleTy(getVMContext());
1554}
1555
1556
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001557/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1558/// an 8-byte GPR. This means that we either have a scalar or we are talking
1559/// about the high or low part of an up-to-16-byte struct. This routine picks
1560/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001561/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1562/// etc).
1563///
1564/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1565/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1566/// the 8-byte value references. PrefType may be null.
1567///
1568/// SourceTy is the source level type for the entire argument. SourceOffset is
1569/// an offset into this that we're processing (which is always either 0 or 8).
1570///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001571llvm::Type *X86_64ABIInfo::
1572GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001573 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001574 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1575 // returning an 8-byte unit starting with it. See if we can safely use it.
1576 if (IROffset == 0) {
1577 // Pointers and int64's always fill the 8-byte unit.
1578 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1579 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001580
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001581 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1582 // goodness in the source type is just tail padding. This is allowed to
1583 // kick in for struct {double,int} on the int, but not on
1584 // struct{double,int,int} because we wouldn't return the second int. We
1585 // have to do this analysis on the source type because we can't depend on
1586 // unions being lowered a specific way etc.
1587 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1588 IRType->isIntegerTy(32)) {
1589 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001590
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001591 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1592 SourceOffset*8+64, getContext()))
1593 return IRType;
1594 }
1595 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001596
Chris Lattner2192fe52011-07-18 04:24:23 +00001597 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001598 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001599 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001600 if (IROffset < SL->getSizeInBytes()) {
1601 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1602 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001603
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001604 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1605 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001606 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001607 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001608
Chris Lattner2192fe52011-07-18 04:24:23 +00001609 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001610 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001611 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1612 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001613 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1614 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001615 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001616
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001617 // Okay, we don't have any better idea of what to pass, so we pass this in an
1618 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001619 unsigned TySizeInBytes =
1620 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001621
Chris Lattner3f763422010-07-29 17:34:39 +00001622 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001623
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001624 // It is always safe to classify this as an integer type up to i64 that
1625 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001626 return llvm::IntegerType::get(getVMContext(),
1627 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001628}
1629
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001630
1631/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1632/// be used as elements of a two register pair to pass or return, return a
1633/// first class aggregate to represent them. For example, if the low part of
1634/// a by-value argument should be passed as i32* and the high part as float,
1635/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001636static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001637GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001638 const llvm::TargetData &TD) {
1639 // In order to correctly satisfy the ABI, we need to the high part to start
1640 // at offset 8. If the high and low parts we inferred are both 4-byte types
1641 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1642 // the second element at offset 8. Check for this:
1643 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1644 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1645 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1646 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001647
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001648 // To handle this, we have to increase the size of the low part so that the
1649 // second element will start at an 8 byte offset. We can't increase the size
1650 // of the second element because it might make us access off the end of the
1651 // struct.
1652 if (HiStart != 8) {
1653 // There are only two sorts of types the ABI generation code can produce for
1654 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1655 // Promote these to a larger type.
1656 if (Lo->isFloatTy())
1657 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1658 else {
1659 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1660 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1661 }
1662 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001663
Chris Lattnera5f58b02011-07-09 17:41:47 +00001664 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001665
1666
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001667 // Verify that the second element is at an 8-byte offset.
1668 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1669 "Invalid x86-64 argument pair!");
1670 return Result;
1671}
1672
Chris Lattner31faff52010-07-28 23:06:14 +00001673ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001674classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001675 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1676 // classification algorithm.
1677 X86_64ABIInfo::Class Lo, Hi;
1678 classify(RetTy, 0, Lo, Hi);
1679
1680 // Check some invariants.
1681 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001682 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1683
Chris Lattnera5f58b02011-07-09 17:41:47 +00001684 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001685 switch (Lo) {
1686 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001687 if (Hi == NoClass)
1688 return ABIArgInfo::getIgnore();
1689 // If the low part is just padding, it takes no register, leave ResType
1690 // null.
1691 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1692 "Unknown missing lo part");
1693 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001694
1695 case SSEUp:
1696 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001697 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001698
1699 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1700 // hidden argument.
1701 case Memory:
1702 return getIndirectReturnResult(RetTy);
1703
1704 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1705 // available register of the sequence %rax, %rdx is used.
1706 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001707 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001708
Chris Lattner1f3a0632010-07-29 21:42:50 +00001709 // If we have a sign or zero extended integer, make sure to return Extend
1710 // so that the parameter gets the right LLVM IR attributes.
1711 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1712 // Treat an enum type as its underlying type.
1713 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1714 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001715
Chris Lattner1f3a0632010-07-29 21:42:50 +00001716 if (RetTy->isIntegralOrEnumerationType() &&
1717 RetTy->isPromotableIntegerType())
1718 return ABIArgInfo::getExtend();
1719 }
Chris Lattner31faff52010-07-28 23:06:14 +00001720 break;
1721
1722 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1723 // available SSE register of the sequence %xmm0, %xmm1 is used.
1724 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001725 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001726 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001727
1728 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1729 // returned on the X87 stack in %st0 as 80-bit x87 number.
1730 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001731 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001732 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001733
1734 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1735 // part of the value is returned in %st0 and the imaginary part in
1736 // %st1.
1737 case ComplexX87:
1738 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001739 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001740 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001741 NULL);
1742 break;
1743 }
1744
Chris Lattnera5f58b02011-07-09 17:41:47 +00001745 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001746 switch (Hi) {
1747 // Memory was handled previously and X87 should
1748 // never occur as a hi class.
1749 case Memory:
1750 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00001751 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001752
1753 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001754 case NoClass:
1755 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001756
Chris Lattner52b3c132010-09-01 00:20:33 +00001757 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001758 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001759 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1760 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001761 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001762 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001763 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001764 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1765 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001766 break;
1767
1768 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001769 // is passed in the next available eightbyte chunk if the last used
1770 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00001771 //
Chris Lattner57540c52011-04-15 05:22:18 +00001772 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001773 case SSEUp:
1774 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001775 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001776 break;
1777
1778 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1779 // returned together with the previous X87 value in %st0.
1780 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001781 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001782 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001783 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001784 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001785 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001786 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001787 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1788 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001789 }
Chris Lattner31faff52010-07-28 23:06:14 +00001790 break;
1791 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001792
Chris Lattner52b3c132010-09-01 00:20:33 +00001793 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001794 // known to pass in the high eightbyte of the result. We do this by forming a
1795 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001796 if (HighPart)
1797 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001798
Chris Lattner1f3a0632010-07-29 21:42:50 +00001799 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001800}
1801
Chris Lattner458b2aa2010-07-29 02:16:43 +00001802ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +00001803 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001804 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001805 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001806
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001807 // Check some invariants.
1808 // FIXME: Enforce these by construction.
1809 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001810 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1811
1812 neededInt = 0;
1813 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001814 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001815 switch (Lo) {
1816 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001817 if (Hi == NoClass)
1818 return ABIArgInfo::getIgnore();
1819 // If the low part is just padding, it takes no register, leave ResType
1820 // null.
1821 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1822 "Unknown missing lo part");
1823 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001824
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001825 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1826 // on the stack.
1827 case Memory:
1828
1829 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1830 // COMPLEX_X87, it is passed in memory.
1831 case X87:
1832 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001833 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1834 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001835 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001836
1837 case SSEUp:
1838 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001839 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001840
1841 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1842 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1843 // and %r9 is used.
1844 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001845 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001846
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001847 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001848 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001849
1850 // If we have a sign or zero extended integer, make sure to return Extend
1851 // so that the parameter gets the right LLVM IR attributes.
1852 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1853 // Treat an enum type as its underlying type.
1854 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1855 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001856
Chris Lattner1f3a0632010-07-29 21:42:50 +00001857 if (Ty->isIntegralOrEnumerationType() &&
1858 Ty->isPromotableIntegerType())
1859 return ABIArgInfo::getExtend();
1860 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001861
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001862 break;
1863
1864 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1865 // available SSE register is used, the registers are taken in the
1866 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001867 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001868 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001869 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001870 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001871 break;
1872 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001873 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001874
Chris Lattnera5f58b02011-07-09 17:41:47 +00001875 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001876 switch (Hi) {
1877 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00001878 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001879 // which is passed in memory.
1880 case Memory:
1881 case X87:
1882 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00001883 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001884
1885 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001886
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001887 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001888 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001889 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001890 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001891
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001892 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1893 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001894 break;
1895
1896 // X87Up generally doesn't occur here (long double is passed in
1897 // memory), except in situations involving unions.
1898 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001899 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001900 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001901
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001902 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1903 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001904
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001905 ++neededSSE;
1906 break;
1907
1908 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1909 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001910 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001911 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001912 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001913 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001914 break;
1915 }
1916
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001917 // If a high part was specified, merge it together with the low part. It is
1918 // known to pass in the high eightbyte of the result. We do this by forming a
1919 // first class struct aggregate with the high and low part: {low, high}
1920 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001921 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001922
Chris Lattner1f3a0632010-07-29 21:42:50 +00001923 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001924}
1925
Chris Lattner22326a12010-07-29 02:31:05 +00001926void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001927
Chris Lattner458b2aa2010-07-29 02:16:43 +00001928 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001929
1930 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001931 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001932
1933 // If the return value is indirect, then the hidden argument is consuming one
1934 // integer register.
1935 if (FI.getReturnInfo().isIndirect())
1936 --freeIntRegs;
1937
1938 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1939 // get assigned (in left-to-right order) for passing as follows...
1940 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1941 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00001942 unsigned neededInt, neededSSE;
1943 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001944
1945 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1946 // eightbyte of an argument, the whole argument is passed on the
1947 // stack. If registers have already been assigned for some
1948 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001949 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001950 freeIntRegs -= neededInt;
1951 freeSSERegs -= neededSSE;
1952 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001953 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001954 }
1955 }
1956}
1957
1958static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1959 QualType Ty,
1960 CodeGenFunction &CGF) {
1961 llvm::Value *overflow_arg_area_p =
1962 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1963 llvm::Value *overflow_arg_area =
1964 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1965
1966 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1967 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1968 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1969 if (Align > 8) {
1970 // Note that we follow the ABI & gcc here, even though the type
1971 // could in theory have an alignment greater than 16. This case
1972 // shouldn't ever matter in practice.
1973
1974 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001975 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001976 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001977 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1978 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001979 CGF.Int64Ty);
1980 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001981 overflow_arg_area =
1982 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1983 overflow_arg_area->getType(),
1984 "overflow_arg_area.align");
1985 }
1986
1987 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00001988 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001989 llvm::Value *Res =
1990 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001991 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001992
1993 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1994 // l->overflow_arg_area + sizeof(type).
1995 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1996 // an 8 byte boundary.
1997
1998 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001999 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002000 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002001 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2002 "overflow_arg_area.next");
2003 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2004
2005 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2006 return Res;
2007}
2008
2009llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2010 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00002011 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00002012
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002013 // Assume that va_list type is correct; should be pointer to LLVM type:
2014 // struct {
2015 // i32 gp_offset;
2016 // i32 fp_offset;
2017 // i8* overflow_arg_area;
2018 // i8* reg_save_area;
2019 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002020 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002021
Chris Lattner9723d6c2010-03-11 18:19:55 +00002022 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00002023 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002024
2025 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2026 // in the registers. If not go to step 7.
2027 if (!neededInt && !neededSSE)
2028 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2029
2030 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2031 // general purpose registers needed to pass type and num_fp to hold
2032 // the number of floating point registers needed.
2033
2034 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2035 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2036 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2037 //
2038 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2039 // register save space).
2040
2041 llvm::Value *InRegs = 0;
2042 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2043 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2044 if (neededInt) {
2045 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2046 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002047 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2048 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002049 }
2050
2051 if (neededSSE) {
2052 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2053 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2054 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002055 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2056 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002057 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2058 }
2059
2060 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2061 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2062 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2063 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2064
2065 // Emit code to load the value if it was passed in registers.
2066
2067 CGF.EmitBlock(InRegBlock);
2068
2069 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2070 // an offset of l->gp_offset and/or l->fp_offset. This may require
2071 // copying to a temporary location in case the parameter is passed
2072 // in different register classes or requires an alignment greater
2073 // than 8 for general purpose registers and 16 for XMM registers.
2074 //
2075 // FIXME: This really results in shameful code when we end up needing to
2076 // collect arguments from different places; often what should result in a
2077 // simple assembling of a structure from scattered addresses has many more
2078 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002079 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002080 llvm::Value *RegAddr =
2081 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2082 "reg_save_area");
2083 if (neededInt && neededSSE) {
2084 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002085 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002086 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002087 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2088 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002089 llvm::Type *TyLo = ST->getElementType(0);
2090 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002091 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002092 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002093 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2094 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002095 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2096 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002097 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2098 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002099 llvm::Value *V =
2100 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2101 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2102 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2103 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2104
Owen Anderson170229f2009-07-14 23:10:40 +00002105 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002106 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002107 } else if (neededInt) {
2108 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2109 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002110 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002111 } else if (neededSSE == 1) {
2112 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2113 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2114 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002115 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002116 assert(neededSSE == 2 && "Invalid number of needed registers!");
2117 // SSE registers are spaced 16 bytes apart in the register save
2118 // area, we need to collect the two eightbytes together.
2119 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002120 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foad7c57be32011-07-11 09:56:20 +00002121 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2192fe52011-07-18 04:24:23 +00002122 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002123 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2192fe52011-07-18 04:24:23 +00002124 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002125 DoubleTy, NULL);
2126 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2127 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2128 DblPtrTy));
2129 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2130 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2131 DblPtrTy));
2132 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2133 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2134 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002135 }
2136
2137 // AMD64-ABI 3.5.7p5: Step 5. Set:
2138 // l->gp_offset = l->gp_offset + num_gp * 8
2139 // l->fp_offset = l->fp_offset + num_fp * 16.
2140 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002141 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002142 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2143 gp_offset_p);
2144 }
2145 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002146 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002147 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2148 fp_offset_p);
2149 }
2150 CGF.EmitBranch(ContBlock);
2151
2152 // Emit code to load the value if it was passed in memory.
2153
2154 CGF.EmitBlock(InMemBlock);
2155 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2156
2157 // Return the appropriate result.
2158
2159 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002160 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002161 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002162 ResAddr->addIncoming(RegAddr, InRegBlock);
2163 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002164 return ResAddr;
2165}
2166
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002167ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2168
2169 if (Ty->isVoidType())
2170 return ABIArgInfo::getIgnore();
2171
2172 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2173 Ty = EnumTy->getDecl()->getIntegerType();
2174
2175 uint64_t Size = getContext().getTypeSize(Ty);
2176
2177 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002178 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2179 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002180 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2181
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002182 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2183 if (Size == 128 &&
Douglas Gregore8bbc122011-09-02 00:18:52 +00002184 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002185 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2186 Size));
2187
2188 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2189 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2190 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002191 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002192 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2193 Size));
2194
2195 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2196 }
2197
2198 if (Ty->isPromotableIntegerType())
2199 return ABIArgInfo::getExtend();
2200
2201 return ABIArgInfo::getDirect();
2202}
2203
2204void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2205
2206 QualType RetTy = FI.getReturnType();
2207 FI.getReturnInfo() = classify(RetTy);
2208
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002209 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2210 it != ie; ++it)
2211 it->info = classify(it->type);
2212}
2213
Chris Lattner04dc9572010-08-31 16:44:54 +00002214llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2215 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002216 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2217 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002218
Chris Lattner04dc9572010-08-31 16:44:54 +00002219 CGBuilderTy &Builder = CGF.Builder;
2220 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2221 "ap");
2222 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2223 llvm::Type *PTy =
2224 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2225 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2226
2227 uint64_t Offset =
2228 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2229 llvm::Value *NextAddr =
2230 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2231 "ap.next");
2232 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2233
2234 return AddrTyped;
2235}
Chris Lattner0cf24192010-06-28 20:05:43 +00002236
John McCallea8d8bb2010-03-11 00:10:12 +00002237// PowerPC-32
2238
2239namespace {
2240class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2241public:
Chris Lattner2b037972010-07-29 02:01:43 +00002242 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002243
John McCallea8d8bb2010-03-11 00:10:12 +00002244 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2245 // This is recovered from gcc output.
2246 return 1; // r1 is the dedicated stack pointer
2247 }
2248
2249 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002250 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002251};
2252
2253}
2254
2255bool
2256PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2257 llvm::Value *Address) const {
2258 // This is calculated from the LLVM and GCC tables and verified
2259 // against gcc output. AFAIK all ABIs use the same encoding.
2260
2261 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2262 llvm::LLVMContext &Context = CGF.getLLVMContext();
2263
Chris Lattner2192fe52011-07-18 04:24:23 +00002264 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallea8d8bb2010-03-11 00:10:12 +00002265 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2266 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2267 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2268
2269 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002270 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002271
2272 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002273 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002274
2275 // 64-76 are various 4-byte special-purpose registers:
2276 // 64: mq
2277 // 65: lr
2278 // 66: ctr
2279 // 67: ap
2280 // 68-75 cr0-7
2281 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002282 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002283
2284 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002285 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002286
2287 // 109: vrsave
2288 // 110: vscr
2289 // 111: spe_acc
2290 // 112: spefscr
2291 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002292 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002293
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002294 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002295}
2296
2297
Chris Lattner0cf24192010-06-28 20:05:43 +00002298//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002299// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002300//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002301
2302namespace {
2303
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002304class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002305public:
2306 enum ABIKind {
2307 APCS = 0,
2308 AAPCS = 1,
2309 AAPCS_VFP
2310 };
2311
2312private:
2313 ABIKind Kind;
2314
2315public:
Chris Lattner2b037972010-07-29 02:01:43 +00002316 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002317
John McCall3480ef22011-08-30 01:42:09 +00002318 bool isEABI() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +00002319 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall3480ef22011-08-30 01:42:09 +00002320 return (Env == "gnueabi" || Env == "eabi");
2321 }
2322
Daniel Dunbar020daa92009-09-12 01:00:39 +00002323private:
2324 ABIKind getABIKind() const { return Kind; }
2325
Chris Lattner458b2aa2010-07-29 02:16:43 +00002326 ABIArgInfo classifyReturnType(QualType RetTy) const;
2327 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002328
Chris Lattner22326a12010-07-29 02:31:05 +00002329 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002330
2331 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2332 CodeGenFunction &CGF) const;
2333};
2334
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002335class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2336public:
Chris Lattner2b037972010-07-29 02:01:43 +00002337 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2338 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002339
John McCall3480ef22011-08-30 01:42:09 +00002340 const ARMABIInfo &getABIInfo() const {
2341 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2342 }
2343
John McCallbeec5a02010-03-06 00:35:14 +00002344 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2345 return 13;
2346 }
Roman Divackyc1617352011-05-18 19:36:54 +00002347
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002348 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00002349 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2350 }
2351
Roman Divackyc1617352011-05-18 19:36:54 +00002352 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2353 llvm::Value *Address) const {
2354 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2355 llvm::LLVMContext &Context = CGF.getLLVMContext();
2356
Chris Lattner2192fe52011-07-18 04:24:23 +00002357 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divackyc1617352011-05-18 19:36:54 +00002358 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2359
2360 // 0-15 are the 16 integer registers.
2361 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2362
2363 return false;
2364 }
John McCall3480ef22011-08-30 01:42:09 +00002365
2366 unsigned getSizeOfUnwindException() const {
2367 if (getABIInfo().isEABI()) return 88;
2368 return TargetCodeGenInfo::getSizeOfUnwindException();
2369 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002370};
2371
Daniel Dunbard59655c2009-09-12 00:59:49 +00002372}
2373
Chris Lattner22326a12010-07-29 02:31:05 +00002374void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002375 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002376 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002377 it != ie; ++it)
2378 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002379
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002380 // Always honor user-specified calling convention.
2381 if (FI.getCallingConvention() != llvm::CallingConv::C)
2382 return;
2383
2384 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002385 llvm::CallingConv::ID DefaultCC;
John McCall3480ef22011-08-30 01:42:09 +00002386 if (isEABI())
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002387 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002388 else
2389 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002390
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002391 // If user did not ask for specific calling convention explicitly (e.g. via
2392 // pcs attribute), set effective calling convention if it's different than ABI
2393 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002394 switch (getABIKind()) {
2395 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002396 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2397 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002398 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002399 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002400 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2401 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002402 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002403 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002404 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2405 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002406 break;
2407 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002408}
2409
Bob Wilsone826a2a2011-08-03 05:58:22 +00002410/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2411/// aggregate. If HAMembers is non-null, the number of base elements
2412/// contained in the type is returned through it; this is used for the
2413/// recursive calls that check aggregate component types.
2414static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2415 ASTContext &Context,
2416 uint64_t *HAMembers = 0) {
2417 uint64_t Members;
2418 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2419 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2420 return false;
2421 Members *= AT->getSize().getZExtValue();
2422 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2423 const RecordDecl *RD = RT->getDecl();
2424 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2425 return false;
2426 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2427 if (!CXXRD->isAggregate())
2428 return false;
2429 }
2430 Members = 0;
2431 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2432 i != e; ++i) {
2433 const FieldDecl *FD = *i;
2434 uint64_t FldMembers;
2435 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2436 return false;
2437 Members += FldMembers;
2438 }
2439 } else {
2440 Members = 1;
2441 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2442 Members = 2;
2443 Ty = CT->getElementType();
2444 }
2445
2446 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2447 // double, or 64-bit or 128-bit vectors.
2448 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2449 if (BT->getKind() != BuiltinType::Float &&
2450 BT->getKind() != BuiltinType::Double)
2451 return false;
2452 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2453 unsigned VecSize = Context.getTypeSize(VT);
2454 if (VecSize != 64 && VecSize != 128)
2455 return false;
2456 } else {
2457 return false;
2458 }
2459
2460 // The base type must be the same for all members. Vector types of the
2461 // same total size are treated as being equivalent here.
2462 const Type *TyPtr = Ty.getTypePtr();
2463 if (!Base)
2464 Base = TyPtr;
2465 if (Base != TyPtr &&
2466 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2467 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2468 return false;
2469 }
2470
2471 // Homogeneous Aggregates can have at most 4 members of the base type.
2472 if (HAMembers)
2473 *HAMembers = Members;
2474 return (Members <= 4);
2475}
2476
Chris Lattner458b2aa2010-07-29 02:16:43 +00002477ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002478 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002479 // Treat an enum type as its underlying type.
2480 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2481 Ty = EnumTy->getDecl()->getIntegerType();
2482
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002483 return (Ty->isPromotableIntegerType() ?
2484 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002485 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002486
Daniel Dunbar09d33622009-09-14 21:54:03 +00002487 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002488 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002489 return ABIArgInfo::getIgnore();
2490
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002491 // Structures with either a non-trivial destructor or a non-trivial
2492 // copy constructor are always indirect.
2493 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2494 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2495
Bob Wilsone826a2a2011-08-03 05:58:22 +00002496 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2497 // Homogeneous Aggregates need to be expanded.
2498 const Type *Base = 0;
2499 if (isHomogeneousAggregate(Ty, Base, getContext()))
2500 return ABIArgInfo::getExpand();
2501 }
2502
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002503 // Otherwise, pass by coercing to a structure of the appropriate size.
2504 //
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002505 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2506 // backend doesn't support byval.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002507 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2192fe52011-07-18 04:24:23 +00002508 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002509 unsigned SizeRegs;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002510 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002511 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2512 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002513 } else {
2514 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2515 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002516 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002517
Chris Lattnera5f58b02011-07-09 17:41:47 +00002518 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002519 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002520 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002521}
2522
Chris Lattner458b2aa2010-07-29 02:16:43 +00002523static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002524 llvm::LLVMContext &VMContext) {
2525 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2526 // is called integer-like if its size is less than or equal to one word, and
2527 // the offset of each of its addressable sub-fields is zero.
2528
2529 uint64_t Size = Context.getTypeSize(Ty);
2530
2531 // Check that the type fits in a word.
2532 if (Size > 32)
2533 return false;
2534
2535 // FIXME: Handle vector types!
2536 if (Ty->isVectorType())
2537 return false;
2538
Daniel Dunbard53bac72009-09-14 02:20:34 +00002539 // Float types are never treated as "integer like".
2540 if (Ty->isRealFloatingType())
2541 return false;
2542
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002543 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002544 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002545 return true;
2546
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002547 // Small complex integer types are "integer like".
2548 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2549 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002550
2551 // Single element and zero sized arrays should be allowed, by the definition
2552 // above, but they are not.
2553
2554 // Otherwise, it must be a record type.
2555 const RecordType *RT = Ty->getAs<RecordType>();
2556 if (!RT) return false;
2557
2558 // Ignore records with flexible arrays.
2559 const RecordDecl *RD = RT->getDecl();
2560 if (RD->hasFlexibleArrayMember())
2561 return false;
2562
2563 // Check that all sub-fields are at offset 0, and are themselves "integer
2564 // like".
2565 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2566
2567 bool HadField = false;
2568 unsigned idx = 0;
2569 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2570 i != e; ++i, ++idx) {
2571 const FieldDecl *FD = *i;
2572
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002573 // Bit-fields are not addressable, we only need to verify they are "integer
2574 // like". We still have to disallow a subsequent non-bitfield, for example:
2575 // struct { int : 0; int x }
2576 // is non-integer like according to gcc.
2577 if (FD->isBitField()) {
2578 if (!RD->isUnion())
2579 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002580
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002581 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2582 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002583
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002584 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002585 }
2586
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002587 // Check if this field is at offset 0.
2588 if (Layout.getFieldOffset(idx) != 0)
2589 return false;
2590
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002591 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2592 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002593
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002594 // Only allow at most one field in a structure. This doesn't match the
2595 // wording above, but follows gcc in situations with a field following an
2596 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002597 if (!RD->isUnion()) {
2598 if (HadField)
2599 return false;
2600
2601 HadField = true;
2602 }
2603 }
2604
2605 return true;
2606}
2607
Chris Lattner458b2aa2010-07-29 02:16:43 +00002608ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002609 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002610 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002611
Daniel Dunbar19964db2010-09-23 01:54:32 +00002612 // Large vector types should be returned via memory.
2613 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2614 return ABIArgInfo::getIndirect(0);
2615
John McCalla1dee5302010-08-22 10:59:02 +00002616 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002617 // Treat an enum type as its underlying type.
2618 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2619 RetTy = EnumTy->getDecl()->getIntegerType();
2620
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002621 return (RetTy->isPromotableIntegerType() ?
2622 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002623 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002624
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002625 // Structures with either a non-trivial destructor or a non-trivial
2626 // copy constructor are always indirect.
2627 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2628 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2629
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002630 // Are we following APCS?
2631 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002632 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002633 return ABIArgInfo::getIgnore();
2634
Daniel Dunbareedf1512010-02-01 23:31:19 +00002635 // Complex types are all returned as packed integers.
2636 //
2637 // FIXME: Consider using 2 x vector types if the back end handles them
2638 // correctly.
2639 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002640 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002641 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002642
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002643 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002644 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002645 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002646 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002647 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002648 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002649 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002650 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2651 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002652 }
2653
2654 // Otherwise return in memory.
2655 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002656 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002657
2658 // Otherwise this is an AAPCS variant.
2659
Chris Lattner458b2aa2010-07-29 02:16:43 +00002660 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002661 return ABIArgInfo::getIgnore();
2662
Bob Wilson1d9269a2011-11-02 04:51:36 +00002663 // Check for homogeneous aggregates with AAPCS-VFP.
2664 if (getABIKind() == AAPCS_VFP) {
2665 const Type *Base = 0;
2666 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2667 // Homogeneous Aggregates are returned directly.
2668 return ABIArgInfo::getDirect();
2669 }
2670
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002671 // Aggregates <= 4 bytes are returned in r0; other aggregates
2672 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002673 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002674 if (Size <= 32) {
2675 // Return in the smallest viable integer type.
2676 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002677 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002678 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002679 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2680 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002681 }
2682
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002683 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002684}
2685
2686llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002687 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002688 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2689 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002690
2691 CGBuilderTy &Builder = CGF.Builder;
2692 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2693 "ap");
2694 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindola11d994b2011-08-02 22:33:37 +00002695 // Handle address alignment for type alignment > 32 bits
2696 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2697 if (TyAlign > 4) {
2698 assert((TyAlign & (TyAlign - 1)) == 0 &&
2699 "Alignment is not power of 2!");
2700 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2701 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2702 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2703 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2704 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002705 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002706 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002707 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2708
2709 uint64_t Offset =
2710 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2711 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002712 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002713 "ap.next");
2714 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2715
2716 return AddrTyped;
2717}
2718
Chris Lattner0cf24192010-06-28 20:05:43 +00002719//===----------------------------------------------------------------------===//
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002720// PTX ABI Implementation
2721//===----------------------------------------------------------------------===//
2722
2723namespace {
2724
2725class PTXABIInfo : public ABIInfo {
2726public:
2727 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2728
2729 ABIArgInfo classifyReturnType(QualType RetTy) const;
2730 ABIArgInfo classifyArgumentType(QualType Ty) const;
2731
2732 virtual void computeInfo(CGFunctionInfo &FI) const;
2733 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2734 CodeGenFunction &CFG) const;
2735};
2736
2737class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2738public:
2739 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2740 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00002741
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00002742 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2743 CodeGen::CodeGenModule &M) const;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002744};
2745
2746ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2747 if (RetTy->isVoidType())
2748 return ABIArgInfo::getIgnore();
2749 if (isAggregateTypeForABI(RetTy))
2750 return ABIArgInfo::getIndirect(0);
2751 return ABIArgInfo::getDirect();
2752}
2753
2754ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2755 if (isAggregateTypeForABI(Ty))
2756 return ABIArgInfo::getIndirect(0);
2757
2758 return ABIArgInfo::getDirect();
2759}
2760
2761void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2762 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2763 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2764 it != ie; ++it)
2765 it->info = classifyArgumentType(it->type);
2766
2767 // Always honor user-specified calling convention.
2768 if (FI.getCallingConvention() != llvm::CallingConv::C)
2769 return;
2770
2771 // Calling convention as default by an ABI.
2772 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002773 const LangOptions &LangOpts = getContext().getLangOptions();
2774 if (LangOpts.OpenCL || LangOpts.CUDA) {
2775 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002776 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski38031972011-10-05 17:58:44 +00002777 } else {
2778 // If we are in standard C/C++ mode, use the triple to decide on the default
2779 StringRef Env =
2780 getContext().getTargetInfo().getTriple().getEnvironmentName();
2781 if (Env == "device")
2782 DefaultCC = llvm::CallingConv::PTX_Device;
2783 else
2784 DefaultCC = llvm::CallingConv::PTX_Kernel;
2785 }
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002786 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski38031972011-10-05 17:58:44 +00002787
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002788}
2789
2790llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2791 CodeGenFunction &CFG) const {
2792 llvm_unreachable("PTX does not support varargs");
2793 return 0;
2794}
2795
Justin Holewinski38031972011-10-05 17:58:44 +00002796void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2797 llvm::GlobalValue *GV,
2798 CodeGen::CodeGenModule &M) const{
2799 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2800 if (!FD) return;
2801
2802 llvm::Function *F = cast<llvm::Function>(GV);
2803
2804 // Perform special handling in OpenCL mode
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002805 if (M.getLangOptions().OpenCL) {
Justin Holewinski38031972011-10-05 17:58:44 +00002806 // Use OpenCL function attributes to set proper calling conventions
2807 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00002808 if (FD->hasAttr<OpenCLKernelAttr>()) {
2809 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002810 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002811 // And kernel functions are not subject to inlining
2812 F->addFnAttr(llvm::Attribute::NoInline);
2813 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002814 }
Justin Holewinski38031972011-10-05 17:58:44 +00002815
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002816 // Perform special handling in CUDA mode.
2817 if (M.getLangOptions().CUDA) {
2818 // CUDA __global__ functions get a kernel calling convention. Since
2819 // __global__ functions cannot be called from the device, we do not
2820 // need to set the noinline attribute.
2821 if (FD->getAttr<CUDAGlobalAttr>())
2822 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002823 }
2824}
2825
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002826}
2827
2828//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00002829// MBlaze ABI Implementation
2830//===----------------------------------------------------------------------===//
2831
2832namespace {
2833
2834class MBlazeABIInfo : public ABIInfo {
2835public:
2836 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2837
2838 bool isPromotableIntegerType(QualType Ty) const;
2839
2840 ABIArgInfo classifyReturnType(QualType RetTy) const;
2841 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2842
2843 virtual void computeInfo(CGFunctionInfo &FI) const {
2844 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2845 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2846 it != ie; ++it)
2847 it->info = classifyArgumentType(it->type);
2848 }
2849
2850 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2851 CodeGenFunction &CGF) const;
2852};
2853
2854class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2855public:
2856 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2857 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2858 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2859 CodeGen::CodeGenModule &M) const;
2860};
2861
2862}
2863
2864bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2865 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2866 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2867 switch (BT->getKind()) {
2868 case BuiltinType::Bool:
2869 case BuiltinType::Char_S:
2870 case BuiltinType::Char_U:
2871 case BuiltinType::SChar:
2872 case BuiltinType::UChar:
2873 case BuiltinType::Short:
2874 case BuiltinType::UShort:
2875 return true;
2876 default:
2877 return false;
2878 }
2879 return false;
2880}
2881
2882llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2883 CodeGenFunction &CGF) const {
2884 // FIXME: Implement
2885 return 0;
2886}
2887
2888
2889ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2890 if (RetTy->isVoidType())
2891 return ABIArgInfo::getIgnore();
2892 if (isAggregateTypeForABI(RetTy))
2893 return ABIArgInfo::getIndirect(0);
2894
2895 return (isPromotableIntegerType(RetTy) ?
2896 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2897}
2898
2899ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2900 if (isAggregateTypeForABI(Ty))
2901 return ABIArgInfo::getIndirect(0);
2902
2903 return (isPromotableIntegerType(Ty) ?
2904 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2905}
2906
2907void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2908 llvm::GlobalValue *GV,
2909 CodeGen::CodeGenModule &M)
2910 const {
2911 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2912 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002913
Wesley Peck36a1f682010-12-19 19:57:51 +00002914 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2915 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2916 CC = llvm::CallingConv::MBLAZE_INTR;
2917 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2918 CC = llvm::CallingConv::MBLAZE_SVOL;
2919
2920 if (CC != llvm::CallingConv::C) {
2921 // Handle 'interrupt_handler' attribute:
2922 llvm::Function *F = cast<llvm::Function>(GV);
2923
2924 // Step 1: Set ISR calling convention.
2925 F->setCallingConv(CC);
2926
2927 // Step 2: Add attributes goodness.
2928 F->addFnAttr(llvm::Attribute::NoInline);
2929 }
2930
2931 // Step 3: Emit _interrupt_handler alias.
2932 if (CC == llvm::CallingConv::MBLAZE_INTR)
2933 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2934 "_interrupt_handler", GV, &M.getModule());
2935}
2936
2937
2938//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002939// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002940//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002941
2942namespace {
2943
2944class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2945public:
Chris Lattner2b037972010-07-29 02:01:43 +00002946 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2947 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002948 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2949 CodeGen::CodeGenModule &M) const;
2950};
2951
2952}
2953
2954void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2955 llvm::GlobalValue *GV,
2956 CodeGen::CodeGenModule &M) const {
2957 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2958 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2959 // Handle 'interrupt' attribute:
2960 llvm::Function *F = cast<llvm::Function>(GV);
2961
2962 // Step 1: Set ISR calling convention.
2963 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2964
2965 // Step 2: Add attributes goodness.
2966 F->addFnAttr(llvm::Attribute::NoInline);
2967
2968 // Step 3: Emit ISR vector alias.
2969 unsigned Num = attr->getNumber() + 0xffe0;
2970 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002971 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002972 GV, &M.getModule());
2973 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002974 }
2975}
2976
Chris Lattner0cf24192010-06-28 20:05:43 +00002977//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002978// MIPS ABI Implementation. This works for both little-endian and
2979// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002980//===----------------------------------------------------------------------===//
2981
John McCall943fae92010-05-27 06:19:26 +00002982namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00002983class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00002984 bool IsO32;
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00002985 unsigned MinABIStackAlignInBytes;
Akira Hatanaka101f70d2011-11-02 23:54:49 +00002986 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00002987public:
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00002988 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
2989 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00002990
2991 ABIArgInfo classifyReturnType(QualType RetTy) const;
2992 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2993 virtual void computeInfo(CGFunctionInfo &FI) const;
2994 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2995 CodeGenFunction &CGF) const;
2996};
2997
John McCall943fae92010-05-27 06:19:26 +00002998class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00002999 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00003000public:
Akira Hatanaka14378522011-11-02 23:14:57 +00003001 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3002 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3003 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00003004
3005 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3006 return 29;
3007 }
3008
3009 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003010 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00003011
3012 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003013 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00003014 }
John McCall943fae92010-05-27 06:19:26 +00003015};
3016}
3017
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003018// In N32/64, an aligned double precision floating point field is passed in
3019// a register.
3020llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3021 if (IsO32)
3022 return 0;
3023
3024 const RecordType *RT = Ty->getAsStructureType();
3025
3026 if (!RT)
3027 return 0;
3028
3029 const RecordDecl *RD = RT->getDecl();
3030 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3031 uint64_t StructSize = getContext().getTypeSize(Ty);
3032 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3033
3034 SmallVector<llvm::Type*, 8> ArgList;
3035 uint64_t LastOffset = 0;
3036 unsigned idx = 0;
3037 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3038
3039 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3040 i != e; ++i, ++idx) {
3041 const QualType Ty = (*i)->getType();
3042 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3043
3044 if (!BT || BT->getKind() != BuiltinType::Double)
3045 continue;
3046
3047 uint64_t Offset = Layout.getFieldOffset(idx);
3048 if (Offset % 64) // Ignore doubles that are not aligned.
3049 continue;
3050
3051 // Add ((Offset - LastOffset) / 64) args of type i64.
3052 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3053 ArgList.push_back(I64);
3054
3055 // Add double type.
3056 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3057 LastOffset = Offset + 64;
3058 }
3059
3060 // This structure doesn't have an aligned double field.
3061 if (!LastOffset)
3062 return 0;
3063
3064 // Add ((StructSize - LastOffset) / 64) args of type i64.
3065 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3066 ArgList.push_back(I64);
3067
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003068 // If the size of the remainder is not zero, add one more integer type to
3069 // ArgList.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003070 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003071 if (R)
3072 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003073
3074 return llvm::StructType::get(getVMContext(), ArgList);
3075}
3076
Akira Hatanakab579fe52011-06-02 00:09:17 +00003077ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3078 if (isAggregateTypeForABI(Ty)) {
3079 // Ignore empty aggregates.
3080 if (getContext().getTypeSize(Ty) == 0)
3081 return ABIArgInfo::getIgnore();
3082
Akira Hatanakadf425db2011-08-01 18:09:58 +00003083 // Records with non trivial destructors/constructors should not be passed
3084 // by value.
3085 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3086 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3087
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003088 llvm::Type *ResType;
3089 if ((ResType = HandleStructTy(Ty)))
3090 return ABIArgInfo::getDirect(ResType);
3091
Akira Hatanakab579fe52011-06-02 00:09:17 +00003092 return ABIArgInfo::getIndirect(0);
3093 }
3094
3095 // Treat an enum type as its underlying type.
3096 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3097 Ty = EnumTy->getDecl()->getIntegerType();
3098
3099 return (Ty->isPromotableIntegerType() ?
3100 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3101}
3102
3103ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3104 if (RetTy->isVoidType())
3105 return ABIArgInfo::getIgnore();
3106
3107 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanaka14378522011-11-02 23:14:57 +00003108 if ((IsO32 && RetTy->isAnyComplexType()) ||
3109 (!IsO32 && (getContext().getTypeSize(RetTy) <= 128)))
Akira Hatanakab579fe52011-06-02 00:09:17 +00003110 return ABIArgInfo::getDirect();
3111
3112 return ABIArgInfo::getIndirect(0);
3113 }
3114
3115 // Treat an enum type as its underlying type.
3116 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3117 RetTy = EnumTy->getDecl()->getIntegerType();
3118
3119 return (RetTy->isPromotableIntegerType() ?
3120 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3121}
3122
3123void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3124 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3125 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3126 it != ie; ++it)
3127 it->info = classifyArgumentType(it->type);
3128}
3129
3130llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3131 CodeGenFunction &CGF) const {
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003132 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3133 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3134
3135 CGBuilderTy &Builder = CGF.Builder;
3136 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3137 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3138 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3139 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3140 llvm::Value *AddrTyped;
3141
3142 if (TypeAlign > MinABIStackAlignInBytes) {
3143 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3144 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3145 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3146 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3147 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3148 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3149 }
3150 else
3151 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3152
3153 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanakaae31c7a2011-08-12 02:30:14 +00003154 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003155 uint64_t Offset =
3156 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3157 llvm::Value *NextAddr =
3158 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3159 "ap.next");
3160 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3161
3162 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003163}
3164
John McCall943fae92010-05-27 06:19:26 +00003165bool
3166MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3167 llvm::Value *Address) const {
3168 // This information comes from gcc's implementation, which seems to
3169 // as canonical as it gets.
3170
3171 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3172 llvm::LLVMContext &Context = CGF.getLLVMContext();
3173
3174 // Everything on MIPS is 4 bytes. Double-precision FP registers
3175 // are aliased to pairs of single-precision FP registers.
Chris Lattner2192fe52011-07-18 04:24:23 +00003176 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall943fae92010-05-27 06:19:26 +00003177 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3178
3179 // 0-31 are the general purpose registers, $0 - $31.
3180 // 32-63 are the floating-point registers, $f0 - $f31.
3181 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3182 // 66 is the (notional, I think) register for signal-handler return.
3183 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3184
3185 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3186 // They are one bit wide and ignored here.
3187
3188 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3189 // (coprocessor 1 is the FP unit)
3190 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3191 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3192 // 176-181 are the DSP accumulator registers.
3193 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3194
3195 return false;
3196}
3197
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003198//===----------------------------------------------------------------------===//
3199// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3200// Currently subclassed only to implement custom OpenCL C function attribute
3201// handling.
3202//===----------------------------------------------------------------------===//
3203
3204namespace {
3205
3206class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3207public:
3208 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3209 : DefaultTargetCodeGenInfo(CGT) {}
3210
3211 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3212 CodeGen::CodeGenModule &M) const;
3213};
3214
3215void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3216 llvm::GlobalValue *GV,
3217 CodeGen::CodeGenModule &M) const {
3218 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3219 if (!FD) return;
3220
3221 llvm::Function *F = cast<llvm::Function>(GV);
3222
3223 if (M.getLangOptions().OpenCL) {
3224 if (FD->hasAttr<OpenCLKernelAttr>()) {
3225 // OpenCL C Kernel functions are not subject to inlining
3226 F->addFnAttr(llvm::Attribute::NoInline);
3227
3228 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3229
3230 // Convert the reqd_work_group_size() attributes to metadata.
3231 llvm::LLVMContext &Context = F->getContext();
3232 llvm::NamedMDNode *OpenCLMetadata =
3233 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3234
3235 SmallVector<llvm::Value*, 5> Operands;
3236 Operands.push_back(F);
3237
3238 Operands.push_back(llvm::Constant::getIntegerValue(
3239 llvm::Type::getInt32Ty(Context),
3240 llvm::APInt(
3241 32,
3242 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3243 Operands.push_back(llvm::Constant::getIntegerValue(
3244 llvm::Type::getInt32Ty(Context),
3245 llvm::APInt(
3246 32,
3247 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3248 Operands.push_back(llvm::Constant::getIntegerValue(
3249 llvm::Type::getInt32Ty(Context),
3250 llvm::APInt(
3251 32,
3252 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3253
3254 // Add a boolean constant operand for "required" (true) or "hint" (false)
3255 // for implementing the work_group_size_hint attr later. Currently
3256 // always true as the hint is not yet implemented.
3257 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3258
3259 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3260 }
3261 }
3262 }
3263}
3264
3265}
John McCall943fae92010-05-27 06:19:26 +00003266
Chris Lattner2b037972010-07-29 02:01:43 +00003267const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003268 if (TheTargetCodeGenInfo)
3269 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003270
Douglas Gregore8bbc122011-09-02 00:18:52 +00003271 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00003272 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003273 default:
Chris Lattner2b037972010-07-29 02:01:43 +00003274 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003275
John McCall943fae92010-05-27 06:19:26 +00003276 case llvm::Triple::mips:
3277 case llvm::Triple::mipsel:
Akira Hatanaka14378522011-11-02 23:14:57 +00003278 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCall943fae92010-05-27 06:19:26 +00003279
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003280 case llvm::Triple::mips64:
3281 case llvm::Triple::mips64el:
Akira Hatanaka14378522011-11-02 23:14:57 +00003282 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003283
Daniel Dunbard59655c2009-09-12 00:59:49 +00003284 case llvm::Triple::arm:
3285 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003286 {
3287 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003288
Douglas Gregore8bbc122011-09-02 00:18:52 +00003289 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003290 Kind = ARMABIInfo::APCS;
3291 else if (CodeGenOpts.FloatABI == "hard")
3292 Kind = ARMABIInfo::AAPCS_VFP;
3293
3294 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3295 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00003296
John McCallea8d8bb2010-03-11 00:10:12 +00003297 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00003298 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00003299
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003300 case llvm::Triple::ptx32:
3301 case llvm::Triple::ptx64:
3302 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3303
Wesley Peck36a1f682010-12-19 19:57:51 +00003304 case llvm::Triple::mblaze:
3305 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3306
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003307 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003308 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003309
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003310 case llvm::Triple::tce:
3311 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3312
Eli Friedman33465822011-07-08 23:31:17 +00003313 case llvm::Triple::x86: {
Douglas Gregore8bbc122011-09-02 00:18:52 +00003314 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedman33465822011-07-08 23:31:17 +00003315
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003316 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003317 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003318 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003319
3320 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003321 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003322 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003323 case llvm::Triple::AuroraUX:
3324 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003325 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003326 case llvm::Triple::OpenBSD:
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003327 case llvm::Triple::NetBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003328 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003329 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003330
3331 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003332 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003333 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003334 }
Eli Friedman33465822011-07-08 23:31:17 +00003335 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003336
Daniel Dunbare3532f82009-08-24 08:52:16 +00003337 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00003338 switch (Triple.getOS()) {
3339 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003340 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003341 case llvm::Triple::Cygwin:
3342 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3343 default:
3344 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3345 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003346 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003347}