blob: 7ad351050dd03228fda4d89acd10d6912c596216 [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
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000295 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
296 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000297 const FieldDecl *FD = *i;
298
299 if (!is32Or64BitBasicType(FD->getType(), Context))
300 return false;
301
302 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
303 // how to expand them yet, and the predicate for telling if a bitfield still
304 // counts as "basic" is more complicated than what we were doing previously.
305 if (FD->isBitField())
306 return false;
307 }
308
309 return true;
310}
311
312namespace {
313/// DefaultABIInfo - The default implementation for ABI specific
314/// details. This implementation provides information which results in
315/// self-consistent and sensible LLVM IR generation, but does not
316/// conform to any particular ABI.
317class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000318public:
319 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000320
Chris Lattner458b2aa2010-07-29 02:16:43 +0000321 ABIArgInfo classifyReturnType(QualType RetTy) const;
322 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000323
Chris Lattner22326a12010-07-29 02:31:05 +0000324 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000325 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000326 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
327 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000328 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000329 }
330
331 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
332 CodeGenFunction &CGF) const;
333};
334
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000335class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
336public:
Chris Lattner2b037972010-07-29 02:01:43 +0000337 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
338 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000339};
340
341llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
342 CodeGenFunction &CGF) const {
343 return 0;
344}
345
Chris Lattner458b2aa2010-07-29 02:16:43 +0000346ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung180319f2011-11-03 00:59:44 +0000347 if (isAggregateTypeForABI(Ty)) {
348 // Records with non trivial destructors/constructors should not be passed
349 // by value.
350 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
351 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
352
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000353 return ABIArgInfo::getIndirect(0);
Jan Wen Voung180319f2011-11-03 00:59:44 +0000354 }
Daniel Dunbar557893d2010-04-21 19:10:51 +0000355
Chris Lattner9723d6c2010-03-11 18:19:55 +0000356 // Treat an enum type as its underlying type.
357 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
358 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000359
Chris Lattner9723d6c2010-03-11 18:19:55 +0000360 return (Ty->isPromotableIntegerType() ?
361 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000362}
363
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000364ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
365 if (RetTy->isVoidType())
366 return ABIArgInfo::getIgnore();
367
368 if (isAggregateTypeForABI(RetTy))
369 return ABIArgInfo::getIndirect(0);
370
371 // Treat an enum type as its underlying type.
372 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
373 RetTy = EnumTy->getDecl()->getIntegerType();
374
375 return (RetTy->isPromotableIntegerType() ?
376 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
377}
378
Bill Wendling5cd41c42010-10-18 03:41:31 +0000379/// UseX86_MMXType - Return true if this is an MMX type that should use the special
380/// x86_mmx type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000381bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendling5cd41c42010-10-18 03:41:31 +0000382 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
383 // special x86_mmx type.
384 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
385 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
386 IRType->getScalarSizeInBits() != 64;
387}
388
Jay Foad7c57be32011-07-11 09:56:20 +0000389static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000390 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000391 llvm::Type* Ty) {
Bill Wendlingec9d2632011-03-07 22:47:14 +0000392 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000393 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
394 return Ty;
395}
396
Chris Lattner0cf24192010-06-28 20:05:43 +0000397//===----------------------------------------------------------------------===//
398// X86-32 ABI Implementation
399//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000400
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000401/// X86_32ABIInfo - The X86-32 ABI information.
402class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000403 static const unsigned MinABIStackAlignInBytes = 4;
404
David Chisnallde3a0692009-08-17 23:08:21 +0000405 bool IsDarwinVectorABI;
406 bool IsSmallStructInRegABI;
Eli Friedman33465822011-07-08 23:31:17 +0000407 bool IsMMXDisabled;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000408
409 static bool isRegisterSize(unsigned Size) {
410 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
411 }
412
413 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
414
Daniel Dunbar557893d2010-04-21 19:10:51 +0000415 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
416 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000417 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000418
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000419 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000420 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000421
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000422public:
Chris Lattner2b037972010-07-29 02:01:43 +0000423
Chris Lattner458b2aa2010-07-29 02:16:43 +0000424 ABIArgInfo classifyReturnType(QualType RetTy) const;
425 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000426
Chris Lattner22326a12010-07-29 02:31:05 +0000427 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000428 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000429 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
430 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000431 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000432 }
433
434 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
435 CodeGenFunction &CGF) const;
436
Eli Friedman33465822011-07-08 23:31:17 +0000437 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
438 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
439 IsMMXDisabled(m) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000440};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000441
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000442class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
443public:
Eli Friedman33465822011-07-08 23:31:17 +0000444 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
445 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000446
447 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
448 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000449
450 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
451 // Darwin uses different dwarf register numbers for EH.
452 if (CGM.isTargetDarwin()) return 5;
453
454 return 4;
455 }
456
457 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
458 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000459
Jay Foad7c57be32011-07-11 09:56:20 +0000460 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000461 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000462 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000463 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
464 }
465
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000466};
467
468}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000469
470/// shouldReturnTypeInRegister - Determine if the given type should be
471/// passed in a register (for the Darwin ABI).
472bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
473 ASTContext &Context) {
474 uint64_t Size = Context.getTypeSize(Ty);
475
476 // Type must be register sized.
477 if (!isRegisterSize(Size))
478 return false;
479
480 if (Ty->isVectorType()) {
481 // 64- and 128- bit vectors inside structures are not returned in
482 // registers.
483 if (Size == 64 || Size == 128)
484 return false;
485
486 return true;
487 }
488
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000489 // If this is a builtin, pointer, enum, complex type, member pointer, or
490 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000491 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000492 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000493 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000494 return true;
495
496 // Arrays are treated like records.
497 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
498 return shouldReturnTypeInRegister(AT->getElementType(), Context);
499
500 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000501 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000502 if (!RT) return false;
503
Anders Carlsson40446e82010-01-27 03:25:19 +0000504 // FIXME: Traverse bases here too.
505
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000506 // Structure types are passed in register if all fields would be
507 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000508 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
509 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000510 const FieldDecl *FD = *i;
511
512 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000513 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000514 continue;
515
516 // Check fields recursively.
517 if (!shouldReturnTypeInRegister(FD->getType(), Context))
518 return false;
519 }
520
521 return true;
522}
523
Chris Lattner458b2aa2010-07-29 02:16:43 +0000524ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
525 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000526 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000527
Chris Lattner458b2aa2010-07-29 02:16:43 +0000528 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000530 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000531 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000532
533 // 128-bit vectors are a special case; they are returned in
534 // registers and we need to make sure to pick a type the LLVM
535 // backend will like.
536 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000537 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000538 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000539
540 // Always return in register if it fits in a general purpose
541 // register, or if it is 64 bits and has a single element.
542 if ((Size == 8 || Size == 16 || Size == 32) ||
543 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000544 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000545 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000546
547 return ABIArgInfo::getIndirect(0);
548 }
549
550 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000551 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000552
John McCalla1dee5302010-08-22 10:59:02 +0000553 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000554 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000555 // Structures with either a non-trivial destructor or a non-trivial
556 // copy constructor are always indirect.
557 if (hasNonTrivialDestructorOrCopyConstructor(RT))
558 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000559
Anders Carlsson5789c492009-10-20 22:07:59 +0000560 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000561 if (RT->getDecl()->hasFlexibleArrayMember())
562 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000563 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000564
David Chisnallde3a0692009-08-17 23:08:21 +0000565 // If specified, structs and unions are always indirect.
566 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000567 return ABIArgInfo::getIndirect(0);
568
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000569 // Small structures which are register sized are generally returned
570 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000571 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
572 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanee945342011-11-18 01:25:50 +0000573
574 // As a special-case, if the struct is a "single-element" struct, and
575 // the field is of type "float" or "double", return it in a
576 // floating-point register. We apply a similar transformation for
577 // pointer types to improve the quality of the generated IR.
578 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
579 if (SeltTy->isRealFloatingType() || SeltTy->hasPointerRepresentation())
580 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
581
582 // FIXME: We should be able to narrow this integer in cases with dead
583 // padding.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000584 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000585 }
586
587 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000588 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000589
Chris Lattner458b2aa2010-07-29 02:16:43 +0000590 // Treat an enum type as its underlying type.
591 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
592 RetTy = EnumTy->getDecl()->getIntegerType();
593
594 return (RetTy->isPromotableIntegerType() ?
595 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000596}
597
Daniel Dunbared23de32010-09-16 20:42:00 +0000598static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
599 const RecordType *RT = Ty->getAs<RecordType>();
600 if (!RT)
601 return 0;
602 const RecordDecl *RD = RT->getDecl();
603
604 // If this is a C++ record, check the bases first.
605 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
606 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
607 e = CXXRD->bases_end(); i != e; ++i)
608 if (!isRecordWithSSEVectorType(Context, i->getType()))
609 return false;
610
611 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
612 i != e; ++i) {
613 QualType FT = i->getType();
614
615 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
616 return true;
617
618 if (isRecordWithSSEVectorType(Context, FT))
619 return true;
620 }
621
622 return false;
623}
624
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000625unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
626 unsigned Align) const {
627 // Otherwise, if the alignment is less than or equal to the minimum ABI
628 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000629 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000630 return 0; // Use default alignment.
631
632 // On non-Darwin, the stack type alignment is always 4.
633 if (!IsDarwinVectorABI) {
634 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000635 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000636 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000637
Daniel Dunbared23de32010-09-16 20:42:00 +0000638 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
639 if (isRecordWithSSEVectorType(getContext(), Ty))
640 return 16;
641
642 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000643}
644
Chris Lattner458b2aa2010-07-29 02:16:43 +0000645ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000646 if (!ByVal)
647 return ABIArgInfo::getIndirect(0, false);
648
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000649 // Compute the byval alignment.
650 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
651 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
652 if (StackAlign == 0)
Chris Lattnere76b95a2011-05-22 23:35:00 +0000653 return ABIArgInfo::getIndirect(4);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000654
655 // If the stack alignment is less than the type alignment, realign the
656 // argument.
657 if (StackAlign < TypeAlign)
658 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
659 /*Realign=*/true);
660
661 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000662}
663
Chris Lattner458b2aa2010-07-29 02:16:43 +0000664ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000665 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000666 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000667 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000668 if (const RecordType *RT = Ty->getAs<RecordType>()) {
669 // Structures with either a non-trivial destructor or a non-trivial
670 // copy constructor are always indirect.
671 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000672 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000673
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000674 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000675 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000676 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000677
Eli Friedman9f061a32011-11-18 00:28:11 +0000678 // Ignore empty structs/unions.
679 if (Ty->isRecordType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000680 return ABIArgInfo::getIgnore();
681
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000682 // Expand small (<= 128-bit) record types when we know that the stack layout
683 // of those arguments will match the struct. This is important because the
684 // LLVM backend isn't smart enough to remove byval, which inhibits many
685 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000686 if (getContext().getTypeSize(Ty) <= 4*32 &&
687 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000688 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000689
Chris Lattner458b2aa2010-07-29 02:16:43 +0000690 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000691 }
692
Chris Lattnerd774ae92010-08-26 20:05:13 +0000693 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000694 // On Darwin, some vectors are passed in memory, we handle this by passing
695 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000696 if (IsDarwinVectorABI) {
697 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000698 if ((Size == 8 || Size == 16 || Size == 32) ||
699 (Size == 64 && VT->getNumElements() == 1))
700 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
701 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000702 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000703
Chris Lattnera5f58b02011-07-09 17:41:47 +0000704 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +0000705 if (UseX86_MMXType(IRType)) {
Eli Friedman33465822011-07-08 23:31:17 +0000706 if (IsMMXDisabled)
707 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
708 64));
Bill Wendling5cd41c42010-10-18 03:41:31 +0000709 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
710 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
711 return AAI;
712 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000713
Chris Lattnerd774ae92010-08-26 20:05:13 +0000714 return ABIArgInfo::getDirect();
715 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000716
717
Chris Lattner458b2aa2010-07-29 02:16:43 +0000718 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
719 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000720
Chris Lattner458b2aa2010-07-29 02:16:43 +0000721 return (Ty->isPromotableIntegerType() ?
722 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000723}
724
725llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
726 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +0000727 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
728 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000729
730 CGBuilderTy &Builder = CGF.Builder;
731 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
732 "ap");
733 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
734 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000735 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000736 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
737
738 uint64_t Offset =
739 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
740 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000741 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000742 "ap.next");
743 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
744
745 return AddrTyped;
746}
747
Charles Davis4ea31ab2010-02-13 15:54:06 +0000748void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
749 llvm::GlobalValue *GV,
750 CodeGen::CodeGenModule &CGM) const {
751 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
752 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
753 // Get the LLVM function.
754 llvm::Function *Fn = cast<llvm::Function>(GV);
755
756 // Now add the 'alignstack' attribute with a value of 16.
757 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
758 }
759 }
760}
761
John McCallbeec5a02010-03-06 00:35:14 +0000762bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
763 CodeGen::CodeGenFunction &CGF,
764 llvm::Value *Address) const {
765 CodeGen::CGBuilderTy &Builder = CGF.Builder;
766 llvm::LLVMContext &Context = CGF.getLLVMContext();
767
Chris Lattner2192fe52011-07-18 04:24:23 +0000768 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallbeec5a02010-03-06 00:35:14 +0000769 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000770
John McCallbeec5a02010-03-06 00:35:14 +0000771 // 0-7 are the eight integer registers; the order is different
772 // on Darwin (for EH), but the range is the same.
773 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000774 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000775
776 if (CGF.CGM.isTargetDarwin()) {
777 // 12-16 are st(0..4). Not sure why we stop at 4.
778 // These have size 16, which is sizeof(long double) on
779 // platforms with 8-byte alignment for that type.
780 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000781 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000782
John McCallbeec5a02010-03-06 00:35:14 +0000783 } else {
784 // 9 is %eflags, which doesn't get a size on Darwin for some
785 // reason.
786 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
787
788 // 11-16 are st(0..5). Not sure why we stop at 5.
789 // These have size 12, which is sizeof(long double) on
790 // platforms with 4-byte alignment for that type.
791 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000792 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
793 }
John McCallbeec5a02010-03-06 00:35:14 +0000794
795 return false;
796}
797
Chris Lattner0cf24192010-06-28 20:05:43 +0000798//===----------------------------------------------------------------------===//
799// X86-64 ABI Implementation
800//===----------------------------------------------------------------------===//
801
802
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000803namespace {
804/// X86_64ABIInfo - The X86_64 ABI information.
805class X86_64ABIInfo : public ABIInfo {
806 enum Class {
807 Integer = 0,
808 SSE,
809 SSEUp,
810 X87,
811 X87Up,
812 ComplexX87,
813 NoClass,
814 Memory
815 };
816
817 /// merge - Implement the X86_64 ABI merging algorithm.
818 ///
819 /// Merge an accumulating classification \arg Accum with a field
820 /// classification \arg Field.
821 ///
822 /// \param Accum - The accumulating classification. This should
823 /// always be either NoClass or the result of a previous merge
824 /// call. In addition, this should never be Memory (the caller
825 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000826 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000827
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000828 /// postMerge - Implement the X86_64 ABI post merging algorithm.
829 ///
830 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
831 /// final MEMORY or SSE classes when necessary.
832 ///
833 /// \param AggregateSize - The size of the current aggregate in
834 /// the classification process.
835 ///
836 /// \param Lo - The classification for the parts of the type
837 /// residing in the low word of the containing object.
838 ///
839 /// \param Hi - The classification for the parts of the type
840 /// residing in the higher words of the containing object.
841 ///
842 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
843
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000844 /// classify - Determine the x86_64 register classes in which the
845 /// given type T should be passed.
846 ///
847 /// \param Lo - The classification for the parts of the type
848 /// residing in the low word of the containing object.
849 ///
850 /// \param Hi - The classification for the parts of the type
851 /// residing in the high word of the containing object.
852 ///
853 /// \param OffsetBase - The bit offset of this type in the
854 /// containing object. Some parameters are classified different
855 /// depending on whether they straddle an eightbyte boundary.
856 ///
857 /// If a word is unused its result will be NoClass; if a type should
858 /// be passed in Memory then at least the classification of \arg Lo
859 /// will be Memory.
860 ///
861 /// The \arg Lo class will be NoClass iff the argument is ignored.
862 ///
863 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
864 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000865 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000866
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000867 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000868 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
869 unsigned IROffset, QualType SourceTy,
870 unsigned SourceOffset) const;
871 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
872 unsigned IROffset, QualType SourceTy,
873 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000874
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000875 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000876 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000877 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000878
879 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000880 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000881 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000882
Chris Lattner458b2aa2010-07-29 02:16:43 +0000883 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000884
Bill Wendling5cd41c42010-10-18 03:41:31 +0000885 ABIArgInfo classifyArgumentType(QualType Ty,
886 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000887 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000888
John McCalle0fda732011-04-21 01:20:55 +0000889 /// The 0.98 ABI revision clarified a lot of ambiguities,
890 /// unfortunately in ways that were not always consistent with
891 /// certain previous compilers. In particular, platforms which
892 /// required strict binary compatibility with older versions of GCC
893 /// may need to exempt themselves.
894 bool honorsRevision0_98() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000895 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCalle0fda732011-04-21 01:20:55 +0000896 }
897
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000898public:
Chris Lattner2b037972010-07-29 02:01:43 +0000899 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000900
Chris Lattner22326a12010-07-29 02:31:05 +0000901 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000902
903 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
904 CodeGenFunction &CGF) const;
905};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000906
Chris Lattner04dc9572010-08-31 16:44:54 +0000907/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000908class WinX86_64ABIInfo : public ABIInfo {
909
910 ABIArgInfo classify(QualType Ty) const;
911
Chris Lattner04dc9572010-08-31 16:44:54 +0000912public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000913 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
914
915 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000916
917 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
918 CodeGenFunction &CGF) const;
919};
920
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000921class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
922public:
Chris Lattner2b037972010-07-29 02:01:43 +0000923 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
924 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000925
926 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
927 return 7;
928 }
929
930 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
931 llvm::Value *Address) const {
932 CodeGen::CGBuilderTy &Builder = CGF.Builder;
933 llvm::LLVMContext &Context = CGF.getLLVMContext();
934
Chris Lattner2192fe52011-07-18 04:24:23 +0000935 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallbeec5a02010-03-06 00:35:14 +0000936 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000937
John McCall943fae92010-05-27 06:19:26 +0000938 // 0-15 are the 16 integer registers.
939 // 16 is %rip.
940 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000941
942 return false;
943 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000944
Jay Foad7c57be32011-07-11 09:56:20 +0000945 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000946 StringRef Constraint,
Jay Foad7c57be32011-07-11 09:56:20 +0000947 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000948 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
949 }
950
John McCallcbc038a2011-09-21 08:08:30 +0000951 bool isNoProtoCallVariadic(CallingConv CC) const {
952 // The default CC on x86-64 sets %al to the number of SSA
953 // registers used, and GCC sets this when calling an unprototyped
954 // function, so we override the default behavior.
955 if (CC == CC_Default || CC == CC_C) return true;
956
957 return TargetCodeGenInfo::isNoProtoCallVariadic(CC);
958 }
959
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000960};
961
Chris Lattner04dc9572010-08-31 16:44:54 +0000962class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
963public:
964 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
965 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
966
967 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
968 return 7;
969 }
970
971 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
972 llvm::Value *Address) const {
973 CodeGen::CGBuilderTy &Builder = CGF.Builder;
974 llvm::LLVMContext &Context = CGF.getLLVMContext();
975
Chris Lattner2192fe52011-07-18 04:24:23 +0000976 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattner04dc9572010-08-31 16:44:54 +0000977 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000978
Chris Lattner04dc9572010-08-31 16:44:54 +0000979 // 0-15 are the 16 integer registers.
980 // 16 is %rip.
981 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
982
983 return false;
984 }
985};
986
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000987}
988
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +0000989void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
990 Class &Hi) const {
991 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
992 //
993 // (a) If one of the classes is Memory, the whole argument is passed in
994 // memory.
995 //
996 // (b) If X87UP is not preceded by X87, the whole argument is passed in
997 // memory.
998 //
999 // (c) If the size of the aggregate exceeds two eightbytes and the first
1000 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1001 // argument is passed in memory. NOTE: This is necessary to keep the
1002 // ABI working for processors that don't support the __m256 type.
1003 //
1004 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1005 //
1006 // Some of these are enforced by the merging logic. Others can arise
1007 // only with unions; for example:
1008 // union { _Complex double; unsigned; }
1009 //
1010 // Note that clauses (b) and (c) were added in 0.98.
1011 //
1012 if (Hi == Memory)
1013 Lo = Memory;
1014 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1015 Lo = Memory;
1016 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1017 Lo = Memory;
1018 if (Hi == SSEUp && Lo != SSE)
1019 Hi = SSE;
1020}
1021
Chris Lattnerd776fb12010-06-28 21:43:59 +00001022X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001023 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1024 // classified recursively so that always two fields are
1025 // considered. The resulting class is calculated according to
1026 // the classes of the fields in the eightbyte:
1027 //
1028 // (a) If both classes are equal, this is the resulting class.
1029 //
1030 // (b) If one of the classes is NO_CLASS, the resulting class is
1031 // the other class.
1032 //
1033 // (c) If one of the classes is MEMORY, the result is the MEMORY
1034 // class.
1035 //
1036 // (d) If one of the classes is INTEGER, the result is the
1037 // INTEGER.
1038 //
1039 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1040 // MEMORY is used as class.
1041 //
1042 // (f) Otherwise class SSE is used.
1043
1044 // Accum should never be memory (we should have returned) or
1045 // ComplexX87 (because this cannot be passed in a structure).
1046 assert((Accum != Memory && Accum != ComplexX87) &&
1047 "Invalid accumulated classification during merge.");
1048 if (Accum == Field || Field == NoClass)
1049 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001050 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001051 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001052 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001053 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001054 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001055 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001056 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1057 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001058 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001059 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001060}
1061
Chris Lattner5c740f12010-06-30 19:14:05 +00001062void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001063 Class &Lo, Class &Hi) const {
1064 // FIXME: This code can be simplified by introducing a simple value class for
1065 // Class pairs with appropriate constructor methods for the various
1066 // situations.
1067
1068 // FIXME: Some of the split computations are wrong; unaligned vectors
1069 // shouldn't be passed in registers for example, so there is no chance they
1070 // can straddle an eightbyte. Verify & simplify.
1071
1072 Lo = Hi = NoClass;
1073
1074 Class &Current = OffsetBase < 64 ? Lo : Hi;
1075 Current = Memory;
1076
John McCall9dd450b2009-09-21 23:43:11 +00001077 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001078 BuiltinType::Kind k = BT->getKind();
1079
1080 if (k == BuiltinType::Void) {
1081 Current = NoClass;
1082 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1083 Lo = Integer;
1084 Hi = Integer;
1085 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1086 Current = Integer;
1087 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1088 Current = SSE;
1089 } else if (k == BuiltinType::LongDouble) {
1090 Lo = X87;
1091 Hi = X87Up;
1092 }
1093 // FIXME: _Decimal32 and _Decimal64 are SSE.
1094 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001095 return;
1096 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001097
Chris Lattnerd776fb12010-06-28 21:43:59 +00001098 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001099 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001100 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001101 return;
1102 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001103
Chris Lattnerd776fb12010-06-28 21:43:59 +00001104 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001105 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001106 return;
1107 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001108
Chris Lattnerd776fb12010-06-28 21:43:59 +00001109 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001110 if (Ty->isMemberFunctionPointerType())
1111 Lo = Hi = Integer;
1112 else
1113 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 (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001118 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001119 if (Size == 32) {
1120 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1121 // float> as integer.
1122 Current = Integer;
1123
1124 // If this type crosses an eightbyte boundary, it should be
1125 // split.
1126 uint64_t EB_Real = (OffsetBase) / 64;
1127 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1128 if (EB_Real != EB_Imag)
1129 Hi = Lo;
1130 } else if (Size == 64) {
1131 // gcc passes <1 x double> in memory. :(
1132 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1133 return;
1134
1135 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001136 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001137 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1138 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1139 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001140 Current = Integer;
1141 else
1142 Current = SSE;
1143
1144 // If this type crosses an eightbyte boundary, it should be
1145 // split.
1146 if (OffsetBase && OffsetBase != 64)
1147 Hi = Lo;
Bruno Cardoso Lopes37b7fd02011-07-12 02:47:38 +00001148 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001149 // Arguments of 256-bits are split into four eightbyte chunks. The
1150 // least significant one belongs to class SSE and all the others to class
1151 // SSEUP. The original Lo and Hi design considers that types can't be
1152 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1153 // This design isn't correct for 256-bits, but since there're no cases
1154 // where the upper parts would need to be inspected, avoid adding
1155 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001156 Lo = SSE;
1157 Hi = SSEUp;
1158 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001159 return;
1160 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001161
Chris Lattnerd776fb12010-06-28 21:43:59 +00001162 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001163 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001164
Chris Lattner2b037972010-07-29 02:01:43 +00001165 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001166 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001167 if (Size <= 64)
1168 Current = Integer;
1169 else if (Size <= 128)
1170 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001171 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001172 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001173 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001174 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001175 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001176 Current = ComplexX87;
1177
1178 // If this complex type crosses an eightbyte boundary then it
1179 // should be split.
1180 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001181 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001182 if (Hi == NoClass && EB_Real != EB_Imag)
1183 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001184
Chris Lattnerd776fb12010-06-28 21:43:59 +00001185 return;
1186 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001187
Chris Lattner2b037972010-07-29 02:01:43 +00001188 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001189 // Arrays are treated like structures.
1190
Chris Lattner2b037972010-07-29 02:01:43 +00001191 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001192
1193 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001194 // than four eightbytes, ..., it has class MEMORY.
1195 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001196 return;
1197
1198 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1199 // fields, it has class MEMORY.
1200 //
1201 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001202 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001203 return;
1204
1205 // Otherwise implement simplified merge. We could be smarter about
1206 // this, but it isn't worth it and would be harder to verify.
1207 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001208 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001209 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001210
1211 // The only case a 256-bit wide vector could be used is when the array
1212 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1213 // to work for sizes wider than 128, early check and fallback to memory.
1214 if (Size > 128 && EltSize != 256)
1215 return;
1216
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001217 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1218 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001219 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001220 Lo = merge(Lo, FieldLo);
1221 Hi = merge(Hi, FieldHi);
1222 if (Lo == Memory || Hi == Memory)
1223 break;
1224 }
1225
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001226 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001227 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001228 return;
1229 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001230
Chris Lattnerd776fb12010-06-28 21:43:59 +00001231 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001232 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001233
1234 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001235 // than four eightbytes, ..., it has class MEMORY.
1236 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001237 return;
1238
Anders Carlsson20759ad2009-09-16 15:53:40 +00001239 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1240 // copy constructor or a non-trivial destructor, it is passed by invisible
1241 // reference.
1242 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1243 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001244
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001245 const RecordDecl *RD = RT->getDecl();
1246
1247 // Assume variable sized types are passed in memory.
1248 if (RD->hasFlexibleArrayMember())
1249 return;
1250
Chris Lattner2b037972010-07-29 02:01:43 +00001251 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001252
1253 // Reset Lo class, this will be recomputed.
1254 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001255
1256 // If this is a C++ record, classify the bases first.
1257 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1258 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1259 e = CXXRD->bases_end(); i != e; ++i) {
1260 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1261 "Unexpected base class!");
1262 const CXXRecordDecl *Base =
1263 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1264
1265 // Classify this field.
1266 //
1267 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1268 // single eightbyte, each is classified separately. Each eightbyte gets
1269 // initialized to class NO_CLASS.
1270 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001271 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001272 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001273 Lo = merge(Lo, FieldLo);
1274 Hi = merge(Hi, FieldHi);
1275 if (Lo == Memory || Hi == Memory)
1276 break;
1277 }
1278 }
1279
1280 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001281 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001282 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001283 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001284 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1285 bool BitField = i->isBitField();
1286
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001287 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1288 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001289 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001290 // The only case a 256-bit wide vector could be used is when the struct
1291 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1292 // to work for sizes wider than 128, early check and fallback to memory.
1293 //
1294 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1295 Lo = Memory;
1296 return;
1297 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001298 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001299 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001300 Lo = Memory;
1301 return;
1302 }
1303
1304 // Classify this field.
1305 //
1306 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1307 // exceeds a single eightbyte, each is classified
1308 // separately. Each eightbyte gets initialized to class
1309 // NO_CLASS.
1310 Class FieldLo, FieldHi;
1311
1312 // Bit-fields require special handling, they do not force the
1313 // structure to be passed in memory even if unaligned, and
1314 // therefore they can straddle an eightbyte.
1315 if (BitField) {
1316 // Ignore padding bit-fields.
1317 if (i->isUnnamedBitfield())
1318 continue;
1319
1320 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001321 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001322
1323 uint64_t EB_Lo = Offset / 64;
1324 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1325 FieldLo = FieldHi = NoClass;
1326 if (EB_Lo) {
1327 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1328 FieldLo = NoClass;
1329 FieldHi = Integer;
1330 } else {
1331 FieldLo = Integer;
1332 FieldHi = EB_Hi ? Integer : NoClass;
1333 }
1334 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001335 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001336 Lo = merge(Lo, FieldLo);
1337 Hi = merge(Hi, FieldHi);
1338 if (Lo == Memory || Hi == Memory)
1339 break;
1340 }
1341
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001342 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001343 }
1344}
1345
Chris Lattner22a931e2010-06-29 06:01:59 +00001346ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001347 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1348 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001349 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001350 // Treat an enum type as its underlying type.
1351 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1352 Ty = EnumTy->getDecl()->getIntegerType();
1353
1354 return (Ty->isPromotableIntegerType() ?
1355 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1356 }
1357
1358 return ABIArgInfo::getIndirect(0);
1359}
1360
Chris Lattner22a931e2010-06-29 06:01:59 +00001361ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001362 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1363 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001364 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001365 // Treat an enum type as its underlying type.
1366 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1367 Ty = EnumTy->getDecl()->getIntegerType();
1368
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001369 return (Ty->isPromotableIntegerType() ?
1370 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001371 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001372
Daniel Dunbar53fac692010-04-21 19:49:55 +00001373 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1374 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001375
Chris Lattner44c2b902011-05-22 23:21:23 +00001376 // Compute the byval alignment. We specify the alignment of the byval in all
1377 // cases so that the mid-level optimizer knows the alignment of the byval.
1378 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1379 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001380}
1381
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001382/// GetByteVectorType - The ABI specifies that a value should be passed in an
1383/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001384/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001385llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001386 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001387
Chris Lattner9fa15c32010-07-29 05:02:29 +00001388 // Wrapper structs that just contain vectors are passed just like vectors,
1389 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001390 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001391 while (STy && STy->getNumElements() == 1) {
1392 IRType = STy->getElementType(0);
1393 STy = dyn_cast<llvm::StructType>(IRType);
1394 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001395
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001396 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001397 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1398 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001399 unsigned BitWidth = VT->getBitWidth();
1400 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001401 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1402 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1403 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1404 EltTy->isIntegerTy(128)))
1405 return VT;
1406 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001407
Chris Lattner4200fe42010-07-29 04:56:46 +00001408 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1409}
1410
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001411/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1412/// is known to either be off the end of the specified type or being in
1413/// alignment padding. The user type specified is known to be at most 128 bits
1414/// in size, and have passed through X86_64ABIInfo::classify with a successful
1415/// classification that put one of the two halves in the INTEGER class.
1416///
1417/// It is conservatively correct to return false.
1418static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1419 unsigned EndBit, ASTContext &Context) {
1420 // If the bytes being queried are off the end of the type, there is no user
1421 // data hiding here. This handles analysis of builtins, vectors and other
1422 // types that don't contain interesting padding.
1423 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1424 if (TySize <= StartBit)
1425 return true;
1426
Chris Lattner98076a22010-07-29 07:43:55 +00001427 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1428 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1429 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1430
1431 // Check each element to see if the element overlaps with the queried range.
1432 for (unsigned i = 0; i != NumElts; ++i) {
1433 // If the element is after the span we care about, then we're done..
1434 unsigned EltOffset = i*EltSize;
1435 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001436
Chris Lattner98076a22010-07-29 07:43:55 +00001437 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1438 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1439 EndBit-EltOffset, Context))
1440 return false;
1441 }
1442 // If it overlaps no elements, then it is safe to process as padding.
1443 return true;
1444 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001445
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001446 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1447 const RecordDecl *RD = RT->getDecl();
1448 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001449
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001450 // If this is a C++ record, check the bases first.
1451 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1452 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1453 e = CXXRD->bases_end(); i != e; ++i) {
1454 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1455 "Unexpected base class!");
1456 const CXXRecordDecl *Base =
1457 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001458
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001459 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001460 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001461 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001462
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001463 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1464 if (!BitsContainNoUserData(i->getType(), BaseStart,
1465 EndBit-BaseOffset, Context))
1466 return false;
1467 }
1468 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001469
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001470 // Verify that no field has data that overlaps the region of interest. Yes
1471 // this could be sped up a lot by being smarter about queried fields,
1472 // however we're only looking at structs up to 16 bytes, so we don't care
1473 // much.
1474 unsigned idx = 0;
1475 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1476 i != e; ++i, ++idx) {
1477 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001478
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001479 // If we found a field after the region we care about, then we're done.
1480 if (FieldOffset >= EndBit) break;
1481
1482 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1483 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1484 Context))
1485 return false;
1486 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001487
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001488 // If nothing in this record overlapped the area of interest, then we're
1489 // clean.
1490 return true;
1491 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001492
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001493 return false;
1494}
1495
Chris Lattnere556a712010-07-29 18:39:32 +00001496/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1497/// float member at the specified offset. For example, {int,{float}} has a
1498/// float at offset 4. It is conservatively correct for this routine to return
1499/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00001500static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnere556a712010-07-29 18:39:32 +00001501 const llvm::TargetData &TD) {
1502 // Base case if we find a float.
1503 if (IROffset == 0 && IRType->isFloatTy())
1504 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001505
Chris Lattnere556a712010-07-29 18:39:32 +00001506 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001507 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00001508 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1509 unsigned Elt = SL->getElementContainingOffset(IROffset);
1510 IROffset -= SL->getElementOffset(Elt);
1511 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1512 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001513
Chris Lattnere556a712010-07-29 18:39:32 +00001514 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001515 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1516 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00001517 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1518 IROffset -= IROffset/EltSize*EltSize;
1519 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1520 }
1521
1522 return false;
1523}
1524
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001525
1526/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1527/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001528llvm::Type *X86_64ABIInfo::
1529GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001530 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001531 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001532 // pass as float if the last 4 bytes is just padding. This happens for
1533 // structs that contain 3 floats.
1534 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1535 SourceOffset*8+64, getContext()))
1536 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001537
Chris Lattnere556a712010-07-29 18:39:32 +00001538 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1539 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1540 // case.
1541 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001542 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1543 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001544
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001545 return llvm::Type::getDoubleTy(getVMContext());
1546}
1547
1548
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001549/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1550/// an 8-byte GPR. This means that we either have a scalar or we are talking
1551/// about the high or low part of an up-to-16-byte struct. This routine picks
1552/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001553/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1554/// etc).
1555///
1556/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1557/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1558/// the 8-byte value references. PrefType may be null.
1559///
1560/// SourceTy is the source level type for the entire argument. SourceOffset is
1561/// an offset into this that we're processing (which is always either 0 or 8).
1562///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001563llvm::Type *X86_64ABIInfo::
1564GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001565 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001566 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1567 // returning an 8-byte unit starting with it. See if we can safely use it.
1568 if (IROffset == 0) {
1569 // Pointers and int64's always fill the 8-byte unit.
1570 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1571 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001572
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001573 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1574 // goodness in the source type is just tail padding. This is allowed to
1575 // kick in for struct {double,int} on the int, but not on
1576 // struct{double,int,int} because we wouldn't return the second int. We
1577 // have to do this analysis on the source type because we can't depend on
1578 // unions being lowered a specific way etc.
1579 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1580 IRType->isIntegerTy(32)) {
1581 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001582
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001583 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1584 SourceOffset*8+64, getContext()))
1585 return IRType;
1586 }
1587 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001588
Chris Lattner2192fe52011-07-18 04:24:23 +00001589 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001590 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001591 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001592 if (IROffset < SL->getSizeInBytes()) {
1593 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1594 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001595
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001596 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1597 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001598 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001599 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001600
Chris Lattner2192fe52011-07-18 04:24:23 +00001601 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001602 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001603 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1604 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001605 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1606 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001607 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001608
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001609 // Okay, we don't have any better idea of what to pass, so we pass this in an
1610 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001611 unsigned TySizeInBytes =
1612 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001613
Chris Lattner3f763422010-07-29 17:34:39 +00001614 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001615
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001616 // It is always safe to classify this as an integer type up to i64 that
1617 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001618 return llvm::IntegerType::get(getVMContext(),
1619 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001620}
1621
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001622
1623/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1624/// be used as elements of a two register pair to pass or return, return a
1625/// first class aggregate to represent them. For example, if the low part of
1626/// a by-value argument should be passed as i32* and the high part as float,
1627/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001628static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001629GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001630 const llvm::TargetData &TD) {
1631 // In order to correctly satisfy the ABI, we need to the high part to start
1632 // at offset 8. If the high and low parts we inferred are both 4-byte types
1633 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1634 // the second element at offset 8. Check for this:
1635 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1636 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1637 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1638 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001639
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001640 // To handle this, we have to increase the size of the low part so that the
1641 // second element will start at an 8 byte offset. We can't increase the size
1642 // of the second element because it might make us access off the end of the
1643 // struct.
1644 if (HiStart != 8) {
1645 // There are only two sorts of types the ABI generation code can produce for
1646 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1647 // Promote these to a larger type.
1648 if (Lo->isFloatTy())
1649 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1650 else {
1651 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1652 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1653 }
1654 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001655
Chris Lattnera5f58b02011-07-09 17:41:47 +00001656 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001657
1658
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001659 // Verify that the second element is at an 8-byte offset.
1660 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1661 "Invalid x86-64 argument pair!");
1662 return Result;
1663}
1664
Chris Lattner31faff52010-07-28 23:06:14 +00001665ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001666classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001667 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1668 // classification algorithm.
1669 X86_64ABIInfo::Class Lo, Hi;
1670 classify(RetTy, 0, Lo, Hi);
1671
1672 // Check some invariants.
1673 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001674 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1675
Chris Lattnera5f58b02011-07-09 17:41:47 +00001676 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001677 switch (Lo) {
1678 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001679 if (Hi == NoClass)
1680 return ABIArgInfo::getIgnore();
1681 // If the low part is just padding, it takes no register, leave ResType
1682 // null.
1683 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1684 "Unknown missing lo part");
1685 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001686
1687 case SSEUp:
1688 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001689 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001690
1691 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1692 // hidden argument.
1693 case Memory:
1694 return getIndirectReturnResult(RetTy);
1695
1696 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1697 // available register of the sequence %rax, %rdx is used.
1698 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001699 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001700
Chris Lattner1f3a0632010-07-29 21:42:50 +00001701 // If we have a sign or zero extended integer, make sure to return Extend
1702 // so that the parameter gets the right LLVM IR attributes.
1703 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1704 // Treat an enum type as its underlying type.
1705 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1706 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001707
Chris Lattner1f3a0632010-07-29 21:42:50 +00001708 if (RetTy->isIntegralOrEnumerationType() &&
1709 RetTy->isPromotableIntegerType())
1710 return ABIArgInfo::getExtend();
1711 }
Chris Lattner31faff52010-07-28 23:06:14 +00001712 break;
1713
1714 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1715 // available SSE register of the sequence %xmm0, %xmm1 is used.
1716 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001717 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001718 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001719
1720 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1721 // returned on the X87 stack in %st0 as 80-bit x87 number.
1722 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001723 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001724 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001725
1726 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1727 // part of the value is returned in %st0 and the imaginary part in
1728 // %st1.
1729 case ComplexX87:
1730 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001731 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001732 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001733 NULL);
1734 break;
1735 }
1736
Chris Lattnera5f58b02011-07-09 17:41:47 +00001737 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001738 switch (Hi) {
1739 // Memory was handled previously and X87 should
1740 // never occur as a hi class.
1741 case Memory:
1742 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00001743 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001744
1745 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001746 case NoClass:
1747 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001748
Chris Lattner52b3c132010-09-01 00:20:33 +00001749 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001750 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001751 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1752 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001753 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001754 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001755 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001756 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1757 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001758 break;
1759
1760 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001761 // is passed in the next available eightbyte chunk if the last used
1762 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00001763 //
Chris Lattner57540c52011-04-15 05:22:18 +00001764 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001765 case SSEUp:
1766 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001767 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001768 break;
1769
1770 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1771 // returned together with the previous X87 value in %st0.
1772 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001773 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001774 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001775 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001776 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001777 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001778 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001779 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1780 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001781 }
Chris Lattner31faff52010-07-28 23:06:14 +00001782 break;
1783 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001784
Chris Lattner52b3c132010-09-01 00:20:33 +00001785 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001786 // known to pass in the high eightbyte of the result. We do this by forming a
1787 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001788 if (HighPart)
1789 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001790
Chris Lattner1f3a0632010-07-29 21:42:50 +00001791 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001792}
1793
Chris Lattner458b2aa2010-07-29 02:16:43 +00001794ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +00001795 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001796 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001797 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001798
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001799 // Check some invariants.
1800 // FIXME: Enforce these by construction.
1801 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001802 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1803
1804 neededInt = 0;
1805 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001806 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001807 switch (Lo) {
1808 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001809 if (Hi == NoClass)
1810 return ABIArgInfo::getIgnore();
1811 // If the low part is just padding, it takes no register, leave ResType
1812 // null.
1813 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1814 "Unknown missing lo part");
1815 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001816
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001817 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1818 // on the stack.
1819 case Memory:
1820
1821 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1822 // COMPLEX_X87, it is passed in memory.
1823 case X87:
1824 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001825 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1826 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001827 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001828
1829 case SSEUp:
1830 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001831 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001832
1833 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1834 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1835 // and %r9 is used.
1836 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001837 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001838
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001839 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001840 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001841
1842 // If we have a sign or zero extended integer, make sure to return Extend
1843 // so that the parameter gets the right LLVM IR attributes.
1844 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1845 // Treat an enum type as its underlying type.
1846 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1847 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001848
Chris Lattner1f3a0632010-07-29 21:42:50 +00001849 if (Ty->isIntegralOrEnumerationType() &&
1850 Ty->isPromotableIntegerType())
1851 return ABIArgInfo::getExtend();
1852 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001853
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001854 break;
1855
1856 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1857 // available SSE register is used, the registers are taken in the
1858 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001859 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001860 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001861 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001862 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001863 break;
1864 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001865 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001866
Chris Lattnera5f58b02011-07-09 17:41:47 +00001867 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001868 switch (Hi) {
1869 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00001870 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001871 // which is passed in memory.
1872 case Memory:
1873 case X87:
1874 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00001875 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001876
1877 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001878
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001879 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001880 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001881 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001882 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001883
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001884 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1885 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001886 break;
1887
1888 // X87Up generally doesn't occur here (long double is passed in
1889 // memory), except in situations involving unions.
1890 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001891 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001892 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001893
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001894 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1895 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001896
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001897 ++neededSSE;
1898 break;
1899
1900 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1901 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001902 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001903 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001904 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001905 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001906 break;
1907 }
1908
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001909 // If a high part was specified, merge it together with the low part. It is
1910 // known to pass in the high eightbyte of the result. We do this by forming a
1911 // first class struct aggregate with the high and low part: {low, high}
1912 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001913 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001914
Chris Lattner1f3a0632010-07-29 21:42:50 +00001915 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001916}
1917
Chris Lattner22326a12010-07-29 02:31:05 +00001918void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001919
Chris Lattner458b2aa2010-07-29 02:16:43 +00001920 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001921
1922 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001923 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001924
1925 // If the return value is indirect, then the hidden argument is consuming one
1926 // integer register.
1927 if (FI.getReturnInfo().isIndirect())
1928 --freeIntRegs;
1929
1930 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1931 // get assigned (in left-to-right order) for passing as follows...
1932 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1933 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00001934 unsigned neededInt, neededSSE;
1935 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001936
1937 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1938 // eightbyte of an argument, the whole argument is passed on the
1939 // stack. If registers have already been assigned for some
1940 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001941 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001942 freeIntRegs -= neededInt;
1943 freeSSERegs -= neededSSE;
1944 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001945 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001946 }
1947 }
1948}
1949
1950static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1951 QualType Ty,
1952 CodeGenFunction &CGF) {
1953 llvm::Value *overflow_arg_area_p =
1954 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1955 llvm::Value *overflow_arg_area =
1956 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1957
1958 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1959 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1960 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1961 if (Align > 8) {
1962 // Note that we follow the ABI & gcc here, even though the type
1963 // could in theory have an alignment greater than 16. This case
1964 // shouldn't ever matter in practice.
1965
1966 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001967 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001968 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001969 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1970 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001971 CGF.Int64Ty);
1972 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001973 overflow_arg_area =
1974 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1975 overflow_arg_area->getType(),
1976 "overflow_arg_area.align");
1977 }
1978
1979 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00001980 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001981 llvm::Value *Res =
1982 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001983 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001984
1985 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1986 // l->overflow_arg_area + sizeof(type).
1987 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1988 // an 8 byte boundary.
1989
1990 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001991 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001992 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001993 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1994 "overflow_arg_area.next");
1995 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1996
1997 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1998 return Res;
1999}
2000
2001llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2002 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00002003 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00002004
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002005 // Assume that va_list type is correct; should be pointer to LLVM type:
2006 // struct {
2007 // i32 gp_offset;
2008 // i32 fp_offset;
2009 // i8* overflow_arg_area;
2010 // i8* reg_save_area;
2011 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002012 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002013
Chris Lattner9723d6c2010-03-11 18:19:55 +00002014 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00002015 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002016
2017 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2018 // in the registers. If not go to step 7.
2019 if (!neededInt && !neededSSE)
2020 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2021
2022 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2023 // general purpose registers needed to pass type and num_fp to hold
2024 // the number of floating point registers needed.
2025
2026 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2027 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2028 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2029 //
2030 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2031 // register save space).
2032
2033 llvm::Value *InRegs = 0;
2034 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2035 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2036 if (neededInt) {
2037 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2038 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002039 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2040 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002041 }
2042
2043 if (neededSSE) {
2044 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2045 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2046 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002047 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2048 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002049 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2050 }
2051
2052 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2053 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2054 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2055 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2056
2057 // Emit code to load the value if it was passed in registers.
2058
2059 CGF.EmitBlock(InRegBlock);
2060
2061 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2062 // an offset of l->gp_offset and/or l->fp_offset. This may require
2063 // copying to a temporary location in case the parameter is passed
2064 // in different register classes or requires an alignment greater
2065 // than 8 for general purpose registers and 16 for XMM registers.
2066 //
2067 // FIXME: This really results in shameful code when we end up needing to
2068 // collect arguments from different places; often what should result in a
2069 // simple assembling of a structure from scattered addresses has many more
2070 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002071 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002072 llvm::Value *RegAddr =
2073 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2074 "reg_save_area");
2075 if (neededInt && neededSSE) {
2076 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002077 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002078 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002079 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2080 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002081 llvm::Type *TyLo = ST->getElementType(0);
2082 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002083 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002084 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002085 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2086 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002087 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2088 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002089 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2090 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002091 llvm::Value *V =
2092 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2093 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2094 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2095 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2096
Owen Anderson170229f2009-07-14 23:10:40 +00002097 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002098 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002099 } else if (neededInt) {
2100 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2101 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002102 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002103 } else if (neededSSE == 1) {
2104 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2105 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2106 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002107 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002108 assert(neededSSE == 2 && "Invalid number of needed registers!");
2109 // SSE registers are spaced 16 bytes apart in the register save
2110 // area, we need to collect the two eightbytes together.
2111 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002112 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foad7c57be32011-07-11 09:56:20 +00002113 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2192fe52011-07-18 04:24:23 +00002114 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002115 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2192fe52011-07-18 04:24:23 +00002116 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002117 DoubleTy, NULL);
2118 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2119 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2120 DblPtrTy));
2121 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2122 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2123 DblPtrTy));
2124 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2125 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2126 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002127 }
2128
2129 // AMD64-ABI 3.5.7p5: Step 5. Set:
2130 // l->gp_offset = l->gp_offset + num_gp * 8
2131 // l->fp_offset = l->fp_offset + num_fp * 16.
2132 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002133 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002134 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2135 gp_offset_p);
2136 }
2137 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002138 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002139 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2140 fp_offset_p);
2141 }
2142 CGF.EmitBranch(ContBlock);
2143
2144 // Emit code to load the value if it was passed in memory.
2145
2146 CGF.EmitBlock(InMemBlock);
2147 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2148
2149 // Return the appropriate result.
2150
2151 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002152 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002153 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002154 ResAddr->addIncoming(RegAddr, InRegBlock);
2155 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002156 return ResAddr;
2157}
2158
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002159ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2160
2161 if (Ty->isVoidType())
2162 return ABIArgInfo::getIgnore();
2163
2164 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2165 Ty = EnumTy->getDecl()->getIntegerType();
2166
2167 uint64_t Size = getContext().getTypeSize(Ty);
2168
2169 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002170 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2171 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002172 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2173
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002174 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2175 if (Size == 128 &&
Douglas Gregore8bbc122011-09-02 00:18:52 +00002176 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002177 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2178 Size));
2179
2180 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2181 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2182 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002183 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002184 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2185 Size));
2186
2187 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2188 }
2189
2190 if (Ty->isPromotableIntegerType())
2191 return ABIArgInfo::getExtend();
2192
2193 return ABIArgInfo::getDirect();
2194}
2195
2196void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2197
2198 QualType RetTy = FI.getReturnType();
2199 FI.getReturnInfo() = classify(RetTy);
2200
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002201 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2202 it != ie; ++it)
2203 it->info = classify(it->type);
2204}
2205
Chris Lattner04dc9572010-08-31 16:44:54 +00002206llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2207 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002208 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2209 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002210
Chris Lattner04dc9572010-08-31 16:44:54 +00002211 CGBuilderTy &Builder = CGF.Builder;
2212 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2213 "ap");
2214 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2215 llvm::Type *PTy =
2216 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2217 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2218
2219 uint64_t Offset =
2220 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2221 llvm::Value *NextAddr =
2222 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2223 "ap.next");
2224 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2225
2226 return AddrTyped;
2227}
Chris Lattner0cf24192010-06-28 20:05:43 +00002228
John McCallea8d8bb2010-03-11 00:10:12 +00002229// PowerPC-32
2230
2231namespace {
2232class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2233public:
Chris Lattner2b037972010-07-29 02:01:43 +00002234 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002235
John McCallea8d8bb2010-03-11 00:10:12 +00002236 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2237 // This is recovered from gcc output.
2238 return 1; // r1 is the dedicated stack pointer
2239 }
2240
2241 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002242 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002243};
2244
2245}
2246
2247bool
2248PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2249 llvm::Value *Address) const {
2250 // This is calculated from the LLVM and GCC tables and verified
2251 // against gcc output. AFAIK all ABIs use the same encoding.
2252
2253 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2254 llvm::LLVMContext &Context = CGF.getLLVMContext();
2255
Chris Lattner2192fe52011-07-18 04:24:23 +00002256 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallea8d8bb2010-03-11 00:10:12 +00002257 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2258 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2259 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2260
2261 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002262 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002263
2264 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002265 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002266
2267 // 64-76 are various 4-byte special-purpose registers:
2268 // 64: mq
2269 // 65: lr
2270 // 66: ctr
2271 // 67: ap
2272 // 68-75 cr0-7
2273 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002274 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002275
2276 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002277 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002278
2279 // 109: vrsave
2280 // 110: vscr
2281 // 111: spe_acc
2282 // 112: spefscr
2283 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002284 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002285
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002286 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002287}
2288
2289
Chris Lattner0cf24192010-06-28 20:05:43 +00002290//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002291// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002292//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002293
2294namespace {
2295
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002296class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002297public:
2298 enum ABIKind {
2299 APCS = 0,
2300 AAPCS = 1,
2301 AAPCS_VFP
2302 };
2303
2304private:
2305 ABIKind Kind;
2306
2307public:
Chris Lattner2b037972010-07-29 02:01:43 +00002308 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002309
John McCall3480ef22011-08-30 01:42:09 +00002310 bool isEABI() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +00002311 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall3480ef22011-08-30 01:42:09 +00002312 return (Env == "gnueabi" || Env == "eabi");
2313 }
2314
Daniel Dunbar020daa92009-09-12 01:00:39 +00002315private:
2316 ABIKind getABIKind() const { return Kind; }
2317
Chris Lattner458b2aa2010-07-29 02:16:43 +00002318 ABIArgInfo classifyReturnType(QualType RetTy) const;
2319 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002320
Chris Lattner22326a12010-07-29 02:31:05 +00002321 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002322
2323 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2324 CodeGenFunction &CGF) const;
2325};
2326
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002327class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2328public:
Chris Lattner2b037972010-07-29 02:01:43 +00002329 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2330 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002331
John McCall3480ef22011-08-30 01:42:09 +00002332 const ARMABIInfo &getABIInfo() const {
2333 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2334 }
2335
John McCallbeec5a02010-03-06 00:35:14 +00002336 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2337 return 13;
2338 }
Roman Divackyc1617352011-05-18 19:36:54 +00002339
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002340 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00002341 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2342 }
2343
Roman Divackyc1617352011-05-18 19:36:54 +00002344 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2345 llvm::Value *Address) const {
2346 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2347 llvm::LLVMContext &Context = CGF.getLLVMContext();
2348
Chris Lattner2192fe52011-07-18 04:24:23 +00002349 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divackyc1617352011-05-18 19:36:54 +00002350 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2351
2352 // 0-15 are the 16 integer registers.
2353 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2354
2355 return false;
2356 }
John McCall3480ef22011-08-30 01:42:09 +00002357
2358 unsigned getSizeOfUnwindException() const {
2359 if (getABIInfo().isEABI()) return 88;
2360 return TargetCodeGenInfo::getSizeOfUnwindException();
2361 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002362};
2363
Daniel Dunbard59655c2009-09-12 00:59:49 +00002364}
2365
Chris Lattner22326a12010-07-29 02:31:05 +00002366void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002367 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002368 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002369 it != ie; ++it)
2370 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002371
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002372 // Always honor user-specified calling convention.
2373 if (FI.getCallingConvention() != llvm::CallingConv::C)
2374 return;
2375
2376 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002377 llvm::CallingConv::ID DefaultCC;
John McCall3480ef22011-08-30 01:42:09 +00002378 if (isEABI())
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002379 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002380 else
2381 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002382
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002383 // If user did not ask for specific calling convention explicitly (e.g. via
2384 // pcs attribute), set effective calling convention if it's different than ABI
2385 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002386 switch (getABIKind()) {
2387 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002388 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2389 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002390 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002391 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002392 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2393 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002394 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002395 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002396 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2397 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002398 break;
2399 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002400}
2401
Bob Wilsone826a2a2011-08-03 05:58:22 +00002402/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2403/// aggregate. If HAMembers is non-null, the number of base elements
2404/// contained in the type is returned through it; this is used for the
2405/// recursive calls that check aggregate component types.
2406static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2407 ASTContext &Context,
2408 uint64_t *HAMembers = 0) {
2409 uint64_t Members;
2410 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2411 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2412 return false;
2413 Members *= AT->getSize().getZExtValue();
2414 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2415 const RecordDecl *RD = RT->getDecl();
2416 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2417 return false;
2418 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2419 if (!CXXRD->isAggregate())
2420 return false;
2421 }
2422 Members = 0;
2423 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2424 i != e; ++i) {
2425 const FieldDecl *FD = *i;
2426 uint64_t FldMembers;
2427 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2428 return false;
2429 Members += FldMembers;
2430 }
2431 } else {
2432 Members = 1;
2433 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2434 Members = 2;
2435 Ty = CT->getElementType();
2436 }
2437
2438 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2439 // double, or 64-bit or 128-bit vectors.
2440 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2441 if (BT->getKind() != BuiltinType::Float &&
2442 BT->getKind() != BuiltinType::Double)
2443 return false;
2444 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2445 unsigned VecSize = Context.getTypeSize(VT);
2446 if (VecSize != 64 && VecSize != 128)
2447 return false;
2448 } else {
2449 return false;
2450 }
2451
2452 // The base type must be the same for all members. Vector types of the
2453 // same total size are treated as being equivalent here.
2454 const Type *TyPtr = Ty.getTypePtr();
2455 if (!Base)
2456 Base = TyPtr;
2457 if (Base != TyPtr &&
2458 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2459 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2460 return false;
2461 }
2462
2463 // Homogeneous Aggregates can have at most 4 members of the base type.
2464 if (HAMembers)
2465 *HAMembers = Members;
2466 return (Members <= 4);
2467}
2468
Chris Lattner458b2aa2010-07-29 02:16:43 +00002469ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002470 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002471 // Treat an enum type as its underlying type.
2472 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2473 Ty = EnumTy->getDecl()->getIntegerType();
2474
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002475 return (Ty->isPromotableIntegerType() ?
2476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002477 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002478
Daniel Dunbar09d33622009-09-14 21:54:03 +00002479 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002480 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002481 return ABIArgInfo::getIgnore();
2482
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002483 // Structures with either a non-trivial destructor or a non-trivial
2484 // copy constructor are always indirect.
2485 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2486 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2487
Bob Wilsone826a2a2011-08-03 05:58:22 +00002488 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2489 // Homogeneous Aggregates need to be expanded.
2490 const Type *Base = 0;
2491 if (isHomogeneousAggregate(Ty, Base, getContext()))
2492 return ABIArgInfo::getExpand();
2493 }
2494
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002495 // Otherwise, pass by coercing to a structure of the appropriate size.
2496 //
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002497 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2498 // backend doesn't support byval.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002499 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2192fe52011-07-18 04:24:23 +00002500 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002501 unsigned SizeRegs;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002502 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002503 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2504 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002505 } else {
2506 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2507 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002508 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002509
Chris Lattnera5f58b02011-07-09 17:41:47 +00002510 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002511 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002512 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002513}
2514
Chris Lattner458b2aa2010-07-29 02:16:43 +00002515static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002516 llvm::LLVMContext &VMContext) {
2517 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2518 // is called integer-like if its size is less than or equal to one word, and
2519 // the offset of each of its addressable sub-fields is zero.
2520
2521 uint64_t Size = Context.getTypeSize(Ty);
2522
2523 // Check that the type fits in a word.
2524 if (Size > 32)
2525 return false;
2526
2527 // FIXME: Handle vector types!
2528 if (Ty->isVectorType())
2529 return false;
2530
Daniel Dunbard53bac72009-09-14 02:20:34 +00002531 // Float types are never treated as "integer like".
2532 if (Ty->isRealFloatingType())
2533 return false;
2534
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002535 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002536 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002537 return true;
2538
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002539 // Small complex integer types are "integer like".
2540 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2541 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002542
2543 // Single element and zero sized arrays should be allowed, by the definition
2544 // above, but they are not.
2545
2546 // Otherwise, it must be a record type.
2547 const RecordType *RT = Ty->getAs<RecordType>();
2548 if (!RT) return false;
2549
2550 // Ignore records with flexible arrays.
2551 const RecordDecl *RD = RT->getDecl();
2552 if (RD->hasFlexibleArrayMember())
2553 return false;
2554
2555 // Check that all sub-fields are at offset 0, and are themselves "integer
2556 // like".
2557 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2558
2559 bool HadField = false;
2560 unsigned idx = 0;
2561 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2562 i != e; ++i, ++idx) {
2563 const FieldDecl *FD = *i;
2564
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002565 // Bit-fields are not addressable, we only need to verify they are "integer
2566 // like". We still have to disallow a subsequent non-bitfield, for example:
2567 // struct { int : 0; int x }
2568 // is non-integer like according to gcc.
2569 if (FD->isBitField()) {
2570 if (!RD->isUnion())
2571 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002572
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002573 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2574 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002575
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002576 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002577 }
2578
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002579 // Check if this field is at offset 0.
2580 if (Layout.getFieldOffset(idx) != 0)
2581 return false;
2582
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002583 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2584 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002585
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002586 // Only allow at most one field in a structure. This doesn't match the
2587 // wording above, but follows gcc in situations with a field following an
2588 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002589 if (!RD->isUnion()) {
2590 if (HadField)
2591 return false;
2592
2593 HadField = true;
2594 }
2595 }
2596
2597 return true;
2598}
2599
Chris Lattner458b2aa2010-07-29 02:16:43 +00002600ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002601 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002602 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002603
Daniel Dunbar19964db2010-09-23 01:54:32 +00002604 // Large vector types should be returned via memory.
2605 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2606 return ABIArgInfo::getIndirect(0);
2607
John McCalla1dee5302010-08-22 10:59:02 +00002608 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002609 // Treat an enum type as its underlying type.
2610 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2611 RetTy = EnumTy->getDecl()->getIntegerType();
2612
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002613 return (RetTy->isPromotableIntegerType() ?
2614 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002615 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002616
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002617 // Structures with either a non-trivial destructor or a non-trivial
2618 // copy constructor are always indirect.
2619 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2620 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2621
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002622 // Are we following APCS?
2623 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002624 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002625 return ABIArgInfo::getIgnore();
2626
Daniel Dunbareedf1512010-02-01 23:31:19 +00002627 // Complex types are all returned as packed integers.
2628 //
2629 // FIXME: Consider using 2 x vector types if the back end handles them
2630 // correctly.
2631 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002632 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002633 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002634
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002635 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002636 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002637 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002638 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002639 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002640 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002641 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002642 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2643 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002644 }
2645
2646 // Otherwise return in memory.
2647 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002648 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002649
2650 // Otherwise this is an AAPCS variant.
2651
Chris Lattner458b2aa2010-07-29 02:16:43 +00002652 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002653 return ABIArgInfo::getIgnore();
2654
Bob Wilson1d9269a2011-11-02 04:51:36 +00002655 // Check for homogeneous aggregates with AAPCS-VFP.
2656 if (getABIKind() == AAPCS_VFP) {
2657 const Type *Base = 0;
2658 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2659 // Homogeneous Aggregates are returned directly.
2660 return ABIArgInfo::getDirect();
2661 }
2662
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002663 // Aggregates <= 4 bytes are returned in r0; other aggregates
2664 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002665 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002666 if (Size <= 32) {
2667 // Return in the smallest viable integer type.
2668 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002669 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002670 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002671 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2672 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002673 }
2674
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002675 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002676}
2677
2678llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002679 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002680 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2681 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002682
2683 CGBuilderTy &Builder = CGF.Builder;
2684 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2685 "ap");
2686 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindola11d994b2011-08-02 22:33:37 +00002687 // Handle address alignment for type alignment > 32 bits
2688 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2689 if (TyAlign > 4) {
2690 assert((TyAlign & (TyAlign - 1)) == 0 &&
2691 "Alignment is not power of 2!");
2692 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2693 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2694 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2695 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2696 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002697 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002698 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002699 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2700
2701 uint64_t Offset =
2702 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2703 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002704 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002705 "ap.next");
2706 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2707
2708 return AddrTyped;
2709}
2710
Chris Lattner0cf24192010-06-28 20:05:43 +00002711//===----------------------------------------------------------------------===//
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002712// PTX ABI Implementation
2713//===----------------------------------------------------------------------===//
2714
2715namespace {
2716
2717class PTXABIInfo : public ABIInfo {
2718public:
2719 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2720
2721 ABIArgInfo classifyReturnType(QualType RetTy) const;
2722 ABIArgInfo classifyArgumentType(QualType Ty) const;
2723
2724 virtual void computeInfo(CGFunctionInfo &FI) const;
2725 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2726 CodeGenFunction &CFG) const;
2727};
2728
2729class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2730public:
2731 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2732 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00002733
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00002734 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2735 CodeGen::CodeGenModule &M) const;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002736};
2737
2738ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2739 if (RetTy->isVoidType())
2740 return ABIArgInfo::getIgnore();
2741 if (isAggregateTypeForABI(RetTy))
2742 return ABIArgInfo::getIndirect(0);
2743 return ABIArgInfo::getDirect();
2744}
2745
2746ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2747 if (isAggregateTypeForABI(Ty))
2748 return ABIArgInfo::getIndirect(0);
2749
2750 return ABIArgInfo::getDirect();
2751}
2752
2753void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2754 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2755 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2756 it != ie; ++it)
2757 it->info = classifyArgumentType(it->type);
2758
2759 // Always honor user-specified calling convention.
2760 if (FI.getCallingConvention() != llvm::CallingConv::C)
2761 return;
2762
2763 // Calling convention as default by an ABI.
2764 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002765 const LangOptions &LangOpts = getContext().getLangOptions();
2766 if (LangOpts.OpenCL || LangOpts.CUDA) {
2767 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002768 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski38031972011-10-05 17:58:44 +00002769 } else {
2770 // If we are in standard C/C++ mode, use the triple to decide on the default
2771 StringRef Env =
2772 getContext().getTargetInfo().getTriple().getEnvironmentName();
2773 if (Env == "device")
2774 DefaultCC = llvm::CallingConv::PTX_Device;
2775 else
2776 DefaultCC = llvm::CallingConv::PTX_Kernel;
2777 }
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002778 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski38031972011-10-05 17:58:44 +00002779
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002780}
2781
2782llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2783 CodeGenFunction &CFG) const {
2784 llvm_unreachable("PTX does not support varargs");
2785 return 0;
2786}
2787
Justin Holewinski38031972011-10-05 17:58:44 +00002788void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2789 llvm::GlobalValue *GV,
2790 CodeGen::CodeGenModule &M) const{
2791 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2792 if (!FD) return;
2793
2794 llvm::Function *F = cast<llvm::Function>(GV);
2795
2796 // Perform special handling in OpenCL mode
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002797 if (M.getLangOptions().OpenCL) {
Justin Holewinski38031972011-10-05 17:58:44 +00002798 // Use OpenCL function attributes to set proper calling conventions
2799 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00002800 if (FD->hasAttr<OpenCLKernelAttr>()) {
2801 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002802 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002803 // And kernel functions are not subject to inlining
2804 F->addFnAttr(llvm::Attribute::NoInline);
2805 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002806 }
Justin Holewinski38031972011-10-05 17:58:44 +00002807
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002808 // Perform special handling in CUDA mode.
2809 if (M.getLangOptions().CUDA) {
2810 // CUDA __global__ functions get a kernel calling convention. Since
2811 // __global__ functions cannot be called from the device, we do not
2812 // need to set the noinline attribute.
2813 if (FD->getAttr<CUDAGlobalAttr>())
2814 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002815 }
2816}
2817
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002818}
2819
2820//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00002821// MBlaze ABI Implementation
2822//===----------------------------------------------------------------------===//
2823
2824namespace {
2825
2826class MBlazeABIInfo : public ABIInfo {
2827public:
2828 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2829
2830 bool isPromotableIntegerType(QualType Ty) const;
2831
2832 ABIArgInfo classifyReturnType(QualType RetTy) const;
2833 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2834
2835 virtual void computeInfo(CGFunctionInfo &FI) const {
2836 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2837 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2838 it != ie; ++it)
2839 it->info = classifyArgumentType(it->type);
2840 }
2841
2842 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2843 CodeGenFunction &CGF) const;
2844};
2845
2846class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2847public:
2848 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2849 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2850 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2851 CodeGen::CodeGenModule &M) const;
2852};
2853
2854}
2855
2856bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2857 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2858 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2859 switch (BT->getKind()) {
2860 case BuiltinType::Bool:
2861 case BuiltinType::Char_S:
2862 case BuiltinType::Char_U:
2863 case BuiltinType::SChar:
2864 case BuiltinType::UChar:
2865 case BuiltinType::Short:
2866 case BuiltinType::UShort:
2867 return true;
2868 default:
2869 return false;
2870 }
2871 return false;
2872}
2873
2874llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2875 CodeGenFunction &CGF) const {
2876 // FIXME: Implement
2877 return 0;
2878}
2879
2880
2881ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2882 if (RetTy->isVoidType())
2883 return ABIArgInfo::getIgnore();
2884 if (isAggregateTypeForABI(RetTy))
2885 return ABIArgInfo::getIndirect(0);
2886
2887 return (isPromotableIntegerType(RetTy) ?
2888 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2889}
2890
2891ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2892 if (isAggregateTypeForABI(Ty))
2893 return ABIArgInfo::getIndirect(0);
2894
2895 return (isPromotableIntegerType(Ty) ?
2896 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2897}
2898
2899void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2900 llvm::GlobalValue *GV,
2901 CodeGen::CodeGenModule &M)
2902 const {
2903 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2904 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002905
Wesley Peck36a1f682010-12-19 19:57:51 +00002906 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2907 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2908 CC = llvm::CallingConv::MBLAZE_INTR;
2909 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2910 CC = llvm::CallingConv::MBLAZE_SVOL;
2911
2912 if (CC != llvm::CallingConv::C) {
2913 // Handle 'interrupt_handler' attribute:
2914 llvm::Function *F = cast<llvm::Function>(GV);
2915
2916 // Step 1: Set ISR calling convention.
2917 F->setCallingConv(CC);
2918
2919 // Step 2: Add attributes goodness.
2920 F->addFnAttr(llvm::Attribute::NoInline);
2921 }
2922
2923 // Step 3: Emit _interrupt_handler alias.
2924 if (CC == llvm::CallingConv::MBLAZE_INTR)
2925 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2926 "_interrupt_handler", GV, &M.getModule());
2927}
2928
2929
2930//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002931// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002932//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002933
2934namespace {
2935
2936class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2937public:
Chris Lattner2b037972010-07-29 02:01:43 +00002938 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2939 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002940 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2941 CodeGen::CodeGenModule &M) const;
2942};
2943
2944}
2945
2946void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2947 llvm::GlobalValue *GV,
2948 CodeGen::CodeGenModule &M) const {
2949 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2950 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2951 // Handle 'interrupt' attribute:
2952 llvm::Function *F = cast<llvm::Function>(GV);
2953
2954 // Step 1: Set ISR calling convention.
2955 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2956
2957 // Step 2: Add attributes goodness.
2958 F->addFnAttr(llvm::Attribute::NoInline);
2959
2960 // Step 3: Emit ISR vector alias.
2961 unsigned Num = attr->getNumber() + 0xffe0;
2962 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002963 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002964 GV, &M.getModule());
2965 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002966 }
2967}
2968
Chris Lattner0cf24192010-06-28 20:05:43 +00002969//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002970// MIPS ABI Implementation. This works for both little-endian and
2971// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002972//===----------------------------------------------------------------------===//
2973
John McCall943fae92010-05-27 06:19:26 +00002974namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00002975class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00002976 bool IsO32;
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00002977 unsigned MinABIStackAlignInBytes;
Akira Hatanaka101f70d2011-11-02 23:54:49 +00002978 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00002979public:
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00002980 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
2981 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00002982
2983 ABIArgInfo classifyReturnType(QualType RetTy) const;
2984 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2985 virtual void computeInfo(CGFunctionInfo &FI) const;
2986 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2987 CodeGenFunction &CGF) const;
2988};
2989
John McCall943fae92010-05-27 06:19:26 +00002990class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00002991 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00002992public:
Akira Hatanaka14378522011-11-02 23:14:57 +00002993 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
2994 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
2995 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00002996
2997 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2998 return 29;
2999 }
3000
3001 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003002 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00003003
3004 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003005 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00003006 }
John McCall943fae92010-05-27 06:19:26 +00003007};
3008}
3009
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003010// In N32/64, an aligned double precision floating point field is passed in
3011// a register.
3012llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3013 if (IsO32)
3014 return 0;
3015
3016 const RecordType *RT = Ty->getAsStructureType();
3017
3018 if (!RT)
3019 return 0;
3020
3021 const RecordDecl *RD = RT->getDecl();
3022 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3023 uint64_t StructSize = getContext().getTypeSize(Ty);
3024 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3025
3026 SmallVector<llvm::Type*, 8> ArgList;
3027 uint64_t LastOffset = 0;
3028 unsigned idx = 0;
3029 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3030
3031 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3032 i != e; ++i, ++idx) {
3033 const QualType Ty = (*i)->getType();
3034 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3035
3036 if (!BT || BT->getKind() != BuiltinType::Double)
3037 continue;
3038
3039 uint64_t Offset = Layout.getFieldOffset(idx);
3040 if (Offset % 64) // Ignore doubles that are not aligned.
3041 continue;
3042
3043 // Add ((Offset - LastOffset) / 64) args of type i64.
3044 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3045 ArgList.push_back(I64);
3046
3047 // Add double type.
3048 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3049 LastOffset = Offset + 64;
3050 }
3051
3052 // This structure doesn't have an aligned double field.
3053 if (!LastOffset)
3054 return 0;
3055
3056 // Add ((StructSize - LastOffset) / 64) args of type i64.
3057 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3058 ArgList.push_back(I64);
3059
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003060 // If the size of the remainder is not zero, add one more integer type to
3061 // ArgList.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003062 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003063 if (R)
3064 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003065
3066 return llvm::StructType::get(getVMContext(), ArgList);
3067}
3068
Akira Hatanakab579fe52011-06-02 00:09:17 +00003069ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3070 if (isAggregateTypeForABI(Ty)) {
3071 // Ignore empty aggregates.
3072 if (getContext().getTypeSize(Ty) == 0)
3073 return ABIArgInfo::getIgnore();
3074
Akira Hatanakadf425db2011-08-01 18:09:58 +00003075 // Records with non trivial destructors/constructors should not be passed
3076 // by value.
3077 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3078 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3079
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003080 llvm::Type *ResType;
3081 if ((ResType = HandleStructTy(Ty)))
3082 return ABIArgInfo::getDirect(ResType);
3083
Akira Hatanakab579fe52011-06-02 00:09:17 +00003084 return ABIArgInfo::getIndirect(0);
3085 }
3086
3087 // Treat an enum type as its underlying type.
3088 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3089 Ty = EnumTy->getDecl()->getIntegerType();
3090
3091 return (Ty->isPromotableIntegerType() ?
3092 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3093}
3094
3095ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3096 if (RetTy->isVoidType())
3097 return ABIArgInfo::getIgnore();
3098
3099 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanaka14378522011-11-02 23:14:57 +00003100 if ((IsO32 && RetTy->isAnyComplexType()) ||
3101 (!IsO32 && (getContext().getTypeSize(RetTy) <= 128)))
Akira Hatanakab579fe52011-06-02 00:09:17 +00003102 return ABIArgInfo::getDirect();
3103
3104 return ABIArgInfo::getIndirect(0);
3105 }
3106
3107 // Treat an enum type as its underlying type.
3108 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3109 RetTy = EnumTy->getDecl()->getIntegerType();
3110
3111 return (RetTy->isPromotableIntegerType() ?
3112 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3113}
3114
3115void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3116 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3117 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3118 it != ie; ++it)
3119 it->info = classifyArgumentType(it->type);
3120}
3121
3122llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3123 CodeGenFunction &CGF) const {
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003124 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3125 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3126
3127 CGBuilderTy &Builder = CGF.Builder;
3128 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3129 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3130 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3131 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3132 llvm::Value *AddrTyped;
3133
3134 if (TypeAlign > MinABIStackAlignInBytes) {
3135 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3136 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3137 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3138 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3139 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3140 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3141 }
3142 else
3143 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3144
3145 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanakaae31c7a2011-08-12 02:30:14 +00003146 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003147 uint64_t Offset =
3148 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3149 llvm::Value *NextAddr =
3150 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3151 "ap.next");
3152 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3153
3154 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003155}
3156
John McCall943fae92010-05-27 06:19:26 +00003157bool
3158MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3159 llvm::Value *Address) const {
3160 // This information comes from gcc's implementation, which seems to
3161 // as canonical as it gets.
3162
3163 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3164 llvm::LLVMContext &Context = CGF.getLLVMContext();
3165
3166 // Everything on MIPS is 4 bytes. Double-precision FP registers
3167 // are aliased to pairs of single-precision FP registers.
Chris Lattner2192fe52011-07-18 04:24:23 +00003168 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall943fae92010-05-27 06:19:26 +00003169 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3170
3171 // 0-31 are the general purpose registers, $0 - $31.
3172 // 32-63 are the floating-point registers, $f0 - $f31.
3173 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3174 // 66 is the (notional, I think) register for signal-handler return.
3175 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3176
3177 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3178 // They are one bit wide and ignored here.
3179
3180 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3181 // (coprocessor 1 is the FP unit)
3182 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3183 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3184 // 176-181 are the DSP accumulator registers.
3185 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3186
3187 return false;
3188}
3189
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003190//===----------------------------------------------------------------------===//
3191// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3192// Currently subclassed only to implement custom OpenCL C function attribute
3193// handling.
3194//===----------------------------------------------------------------------===//
3195
3196namespace {
3197
3198class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3199public:
3200 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3201 : DefaultTargetCodeGenInfo(CGT) {}
3202
3203 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3204 CodeGen::CodeGenModule &M) const;
3205};
3206
3207void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3208 llvm::GlobalValue *GV,
3209 CodeGen::CodeGenModule &M) const {
3210 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3211 if (!FD) return;
3212
3213 llvm::Function *F = cast<llvm::Function>(GV);
3214
3215 if (M.getLangOptions().OpenCL) {
3216 if (FD->hasAttr<OpenCLKernelAttr>()) {
3217 // OpenCL C Kernel functions are not subject to inlining
3218 F->addFnAttr(llvm::Attribute::NoInline);
3219
3220 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3221
3222 // Convert the reqd_work_group_size() attributes to metadata.
3223 llvm::LLVMContext &Context = F->getContext();
3224 llvm::NamedMDNode *OpenCLMetadata =
3225 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3226
3227 SmallVector<llvm::Value*, 5> Operands;
3228 Operands.push_back(F);
3229
3230 Operands.push_back(llvm::Constant::getIntegerValue(
3231 llvm::Type::getInt32Ty(Context),
3232 llvm::APInt(
3233 32,
3234 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3235 Operands.push_back(llvm::Constant::getIntegerValue(
3236 llvm::Type::getInt32Ty(Context),
3237 llvm::APInt(
3238 32,
3239 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3240 Operands.push_back(llvm::Constant::getIntegerValue(
3241 llvm::Type::getInt32Ty(Context),
3242 llvm::APInt(
3243 32,
3244 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3245
3246 // Add a boolean constant operand for "required" (true) or "hint" (false)
3247 // for implementing the work_group_size_hint attr later. Currently
3248 // always true as the hint is not yet implemented.
3249 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3250
3251 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3252 }
3253 }
3254 }
3255}
3256
3257}
John McCall943fae92010-05-27 06:19:26 +00003258
Chris Lattner2b037972010-07-29 02:01:43 +00003259const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003260 if (TheTargetCodeGenInfo)
3261 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003262
Douglas Gregore8bbc122011-09-02 00:18:52 +00003263 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00003264 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003265 default:
Chris Lattner2b037972010-07-29 02:01:43 +00003266 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003267
John McCall943fae92010-05-27 06:19:26 +00003268 case llvm::Triple::mips:
3269 case llvm::Triple::mipsel:
Akira Hatanaka14378522011-11-02 23:14:57 +00003270 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCall943fae92010-05-27 06:19:26 +00003271
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003272 case llvm::Triple::mips64:
3273 case llvm::Triple::mips64el:
Akira Hatanaka14378522011-11-02 23:14:57 +00003274 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003275
Daniel Dunbard59655c2009-09-12 00:59:49 +00003276 case llvm::Triple::arm:
3277 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003278 {
3279 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003280
Douglas Gregore8bbc122011-09-02 00:18:52 +00003281 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003282 Kind = ARMABIInfo::APCS;
3283 else if (CodeGenOpts.FloatABI == "hard")
3284 Kind = ARMABIInfo::AAPCS_VFP;
3285
3286 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3287 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00003288
John McCallea8d8bb2010-03-11 00:10:12 +00003289 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00003290 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00003291
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003292 case llvm::Triple::ptx32:
3293 case llvm::Triple::ptx64:
3294 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3295
Wesley Peck36a1f682010-12-19 19:57:51 +00003296 case llvm::Triple::mblaze:
3297 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3298
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003299 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003300 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003301
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003302 case llvm::Triple::tce:
3303 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3304
Eli Friedman33465822011-07-08 23:31:17 +00003305 case llvm::Triple::x86: {
Douglas Gregore8bbc122011-09-02 00:18:52 +00003306 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedman33465822011-07-08 23:31:17 +00003307
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003308 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003309 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003310 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003311
3312 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003313 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003314 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003315 case llvm::Triple::AuroraUX:
3316 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003317 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003318 case llvm::Triple::OpenBSD:
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003319 case llvm::Triple::NetBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003320 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003321 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003322
3323 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003324 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003325 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003326 }
Eli Friedman33465822011-07-08 23:31:17 +00003327 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003328
Daniel Dunbare3532f82009-08-24 08:52:16 +00003329 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00003330 switch (Triple.getOS()) {
3331 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003332 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003333 case llvm::Triple::Cygwin:
3334 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3335 default:
3336 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3337 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003338 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003339}