blob: 563ca5cadd0d8897eea3ba9e7f1da7dc6cf78e39 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-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 Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-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 McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-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 Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-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
Daniel Dunbar98303b92009-09-13 08:03:58 +0000101static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000102
103/// isEmptyField - Return true iff a the field is "empty", that is it
104/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000105static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
106 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000107 if (FD->isUnnamedBitfield())
108 return true;
109
110 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111
Daniel Dunbar98303b92009-09-13 08:03:58 +0000112 // Constant arrays of empty records count as empty, strip them off.
113 if (AllowArrays)
114 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
115 FT = AT->getElementType();
116
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000117 const RecordType *RT = FT->getAs<RecordType>();
118 if (!RT)
119 return false;
120
121 // C++ record fields are never empty, at least in the Itanium ABI.
122 //
123 // FIXME: We should use a predicate for whether this behavior is true in the
124 // current ABI.
125 if (isa<CXXRecordDecl>(RT->getDecl()))
126 return false;
127
Daniel Dunbar98303b92009-09-13 08:03:58 +0000128 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000129}
130
131/// isEmptyRecord - Return true iff a structure contains only empty
132/// fields. Note that a structure with a flexible array member is not
133/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000134static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000135 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000136 if (!RT)
137 return 0;
138 const RecordDecl *RD = RT->getDecl();
139 if (RD->hasFlexibleArrayMember())
140 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000141
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000142 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000143 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000144 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
145 e = CXXRD->bases_end(); i != e; ++i)
146 if (!isEmptyRecord(Context, i->getType(), true))
147 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000148
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000149 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
150 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000151 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000152 return false;
153 return true;
154}
155
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000156/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
157/// a non-trivial destructor or a non-trivial copy constructor.
158static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
159 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
160 if (!RD)
161 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000162
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000163 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
164}
165
166/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
167/// a record type with either a non-trivial destructor or a non-trivial copy
168/// constructor.
169static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
170 const RecordType *RT = T->getAs<RecordType>();
171 if (!RT)
172 return false;
173
174 return hasNonTrivialDestructorOrCopyConstructor(RT);
175}
176
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000177/// isSingleElementStruct - Determine if a structure is a "single
178/// element struct", i.e. it has exactly one non-empty field or
179/// exactly one field which is itself a single element
180/// struct. Structures with flexible array members are never
181/// considered single element structs.
182///
183/// \return The field declaration for the single non-empty field, if
184/// it exists.
185static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
186 const RecordType *RT = T->getAsStructureType();
187 if (!RT)
188 return 0;
189
190 const RecordDecl *RD = RT->getDecl();
191 if (RD->hasFlexibleArrayMember())
192 return 0;
193
194 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000195
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000196 // If this is a C++ record, check the bases first.
197 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
198 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
199 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000200 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000201 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000202 continue;
203
204 // If we already found an element then this isn't a single-element struct.
205 if (Found)
206 return 0;
207
208 // If this is non-empty and not a single element struct, the composite
209 // cannot be a single element struct.
210 Found = isSingleElementStruct(i->getType(), Context);
211 if (!Found)
212 return 0;
213 }
214 }
215
216 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000217 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
218 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000219 const FieldDecl *FD = *i;
220 QualType FT = FD->getType();
221
222 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000223 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000224 continue;
225
226 // If we already found an element then this isn't a single-element
227 // struct.
228 if (Found)
229 return 0;
230
231 // Treat single element arrays as the element.
232 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
233 if (AT->getSize().getZExtValue() != 1)
234 break;
235 FT = AT->getElementType();
236 }
237
John McCalld608cdb2010-08-22 10:59:02 +0000238 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000239 Found = FT.getTypePtr();
240 } else {
241 Found = isSingleElementStruct(FT, Context);
242 if (!Found)
243 return 0;
244 }
245 }
246
247 return Found;
248}
249
250static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000251 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000252 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
253 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000254 return false;
255
256 uint64_t Size = Context.getTypeSize(Ty);
257 return Size == 32 || Size == 64;
258}
259
Daniel Dunbar53012f42009-11-09 01:33:53 +0000260/// canExpandIndirectArgument - Test whether an argument type which is to be
261/// passed indirectly (on the stack) would have the equivalent layout if it was
262/// expanded into separate arguments. If so, we prefer to do the latter to avoid
263/// inhibiting optimizations.
264///
265// FIXME: This predicate is missing many cases, currently it just follows
266// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
267// should probably make this smarter, or better yet make the LLVM backend
268// capable of handling it.
269static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
270 // We can only expand structure types.
271 const RecordType *RT = Ty->getAs<RecordType>();
272 if (!RT)
273 return false;
274
275 // We can only expand (C) structures.
276 //
277 // FIXME: This needs to be generalized to handle classes as well.
278 const RecordDecl *RD = RT->getDecl();
279 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
280 return false;
281
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000282 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
283 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000284 const FieldDecl *FD = *i;
285
286 if (!is32Or64BitBasicType(FD->getType(), Context))
287 return false;
288
289 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
290 // how to expand them yet, and the predicate for telling if a bitfield still
291 // counts as "basic" is more complicated than what we were doing previously.
292 if (FD->isBitField())
293 return false;
294 }
295
296 return true;
297}
298
299namespace {
300/// DefaultABIInfo - The default implementation for ABI specific
301/// details. This implementation provides information which results in
302/// self-consistent and sensible LLVM IR generation, but does not
303/// conform to any particular ABI.
304class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000305public:
306 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000307
Chris Lattnera3c109b2010-07-29 02:16:43 +0000308 ABIArgInfo classifyReturnType(QualType RetTy) const;
309 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000310
Chris Lattneree5dcd02010-07-29 02:31:05 +0000311 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000312 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000313 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
314 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000315 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000316 }
317
318 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
319 CodeGenFunction &CGF) const;
320};
321
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000322class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
323public:
Chris Lattnerea044322010-07-29 02:01:43 +0000324 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
325 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000326};
327
328llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
329 CodeGenFunction &CGF) const {
330 return 0;
331}
332
Chris Lattnera3c109b2010-07-29 02:16:43 +0000333ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000334 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000335 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000336
Chris Lattnera14db752010-03-11 18:19:55 +0000337 // Treat an enum type as its underlying type.
338 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
339 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000340
Chris Lattnera14db752010-03-11 18:19:55 +0000341 return (Ty->isPromotableIntegerType() ?
342 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000343}
344
Bob Wilson0024f942011-01-10 23:54:17 +0000345ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
346 if (RetTy->isVoidType())
347 return ABIArgInfo::getIgnore();
348
349 if (isAggregateTypeForABI(RetTy))
350 return ABIArgInfo::getIndirect(0);
351
352 // Treat an enum type as its underlying type.
353 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
354 RetTy = EnumTy->getDecl()->getIntegerType();
355
356 return (RetTy->isPromotableIntegerType() ?
357 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
358}
359
Bill Wendlingbb465d72010-10-18 03:41:31 +0000360/// UseX86_MMXType - Return true if this is an MMX type that should use the special
361/// x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000362bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000363 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
364 // special x86_mmx type.
365 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
366 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
367 IRType->getScalarSizeInBits() != 64;
368}
369
Jay Foadef6de3d2011-07-11 09:56:20 +0000370static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000371 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000372 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000373 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000374 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
375 return Ty;
376}
377
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000378//===----------------------------------------------------------------------===//
379// X86-32 ABI Implementation
380//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000381
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000382/// X86_32ABIInfo - The X86-32 ABI information.
383class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000384 static const unsigned MinABIStackAlignInBytes = 4;
385
David Chisnall1e4249c2009-08-17 23:08:21 +0000386 bool IsDarwinVectorABI;
387 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000388 bool IsMMXDisabled;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000389
390 static bool isRegisterSize(unsigned Size) {
391 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
392 }
393
394 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
395
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000396 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
397 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000398 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000399
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000400 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000401 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000402
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000403public:
Chris Lattnerea044322010-07-29 02:01:43 +0000404
Chris Lattnera3c109b2010-07-29 02:16:43 +0000405 ABIArgInfo classifyReturnType(QualType RetTy) const;
406 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000407
Chris Lattneree5dcd02010-07-29 02:31:05 +0000408 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000409 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000410 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
411 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000412 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000413 }
414
415 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
416 CodeGenFunction &CGF) const;
417
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000418 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
419 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
420 IsMMXDisabled(m) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000421};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000422
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000423class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
424public:
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000425 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
426 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000427
428 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
429 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000430
431 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
432 // Darwin uses different dwarf register numbers for EH.
433 if (CGM.isTargetDarwin()) return 5;
434
435 return 4;
436 }
437
438 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
439 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000440
Jay Foadef6de3d2011-07-11 09:56:20 +0000441 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000442 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000443 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000444 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
445 }
446
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000447};
448
449}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000450
451/// shouldReturnTypeInRegister - Determine if the given type should be
452/// passed in a register (for the Darwin ABI).
453bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
454 ASTContext &Context) {
455 uint64_t Size = Context.getTypeSize(Ty);
456
457 // Type must be register sized.
458 if (!isRegisterSize(Size))
459 return false;
460
461 if (Ty->isVectorType()) {
462 // 64- and 128- bit vectors inside structures are not returned in
463 // registers.
464 if (Size == 64 || Size == 128)
465 return false;
466
467 return true;
468 }
469
Daniel Dunbar77115232010-05-15 00:00:30 +0000470 // If this is a builtin, pointer, enum, complex type, member pointer, or
471 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000472 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000473 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000474 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000475 return true;
476
477 // Arrays are treated like records.
478 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
479 return shouldReturnTypeInRegister(AT->getElementType(), Context);
480
481 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000482 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000483 if (!RT) return false;
484
Anders Carlssona8874232010-01-27 03:25:19 +0000485 // FIXME: Traverse bases here too.
486
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000487 // Structure types are passed in register if all fields would be
488 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000489 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
490 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000491 const FieldDecl *FD = *i;
492
493 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000494 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000495 continue;
496
497 // Check fields recursively.
498 if (!shouldReturnTypeInRegister(FD->getType(), Context))
499 return false;
500 }
501
502 return true;
503}
504
Chris Lattnera3c109b2010-07-29 02:16:43 +0000505ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
506 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000508
Chris Lattnera3c109b2010-07-29 02:16:43 +0000509 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000510 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000511 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000512 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000513
514 // 128-bit vectors are a special case; they are returned in
515 // registers and we need to make sure to pick a type the LLVM
516 // backend will like.
517 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000518 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000519 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000520
521 // Always return in register if it fits in a general purpose
522 // register, or if it is 64 bits and has a single element.
523 if ((Size == 8 || Size == 16 || Size == 32) ||
524 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000525 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000526 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000527
528 return ABIArgInfo::getIndirect(0);
529 }
530
531 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000532 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000533
John McCalld608cdb2010-08-22 10:59:02 +0000534 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000535 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000536 // Structures with either a non-trivial destructor or a non-trivial
537 // copy constructor are always indirect.
538 if (hasNonTrivialDestructorOrCopyConstructor(RT))
539 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000540
Anders Carlsson40092972009-10-20 22:07:59 +0000541 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000542 if (RT->getDecl()->hasFlexibleArrayMember())
543 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000544 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000545
David Chisnall1e4249c2009-08-17 23:08:21 +0000546 // If specified, structs and unions are always indirect.
547 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000548 return ABIArgInfo::getIndirect(0);
549
550 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000551 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000552 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000553 if (BT->isIntegerType()) {
554 // We need to use the size of the structure, padding
555 // bit-fields can adjust that to be larger than the single
556 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000557 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000558 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000559 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
560 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000561
Chris Lattnera3c109b2010-07-29 02:16:43 +0000562 if (BT->getKind() == BuiltinType::Float) {
563 assert(getContext().getTypeSize(RetTy) ==
564 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000565 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000566 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000567 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000568
Chris Lattnera3c109b2010-07-29 02:16:43 +0000569 if (BT->getKind() == BuiltinType::Double) {
570 assert(getContext().getTypeSize(RetTy) ==
571 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000572 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000573 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000574 }
575 } else if (SeltTy->isPointerType()) {
576 // FIXME: It would be really nice if this could come out as the proper
577 // pointer type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000578 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000579 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000580 } else if (SeltTy->isVectorType()) {
581 // 64- and 128-bit vectors are never returned in a
582 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000583 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000584 if (Size == 64 || Size == 128)
585 return ABIArgInfo::getIndirect(0);
586
Chris Lattnera3c109b2010-07-29 02:16:43 +0000587 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000588 }
589 }
590
591 // Small structures which are register sized are generally returned
592 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000593 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
594 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000595 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000596 }
597
598 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000599 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000600
Chris Lattnera3c109b2010-07-29 02:16:43 +0000601 // Treat an enum type as its underlying type.
602 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
603 RetTy = EnumTy->getDecl()->getIntegerType();
604
605 return (RetTy->isPromotableIntegerType() ?
606 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607}
608
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000609static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
610 const RecordType *RT = Ty->getAs<RecordType>();
611 if (!RT)
612 return 0;
613 const RecordDecl *RD = RT->getDecl();
614
615 // If this is a C++ record, check the bases first.
616 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
617 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
618 e = CXXRD->bases_end(); i != e; ++i)
619 if (!isRecordWithSSEVectorType(Context, i->getType()))
620 return false;
621
622 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
623 i != e; ++i) {
624 QualType FT = i->getType();
625
626 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
627 return true;
628
629 if (isRecordWithSSEVectorType(Context, FT))
630 return true;
631 }
632
633 return false;
634}
635
Daniel Dunbare59d8582010-09-16 20:42:06 +0000636unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
637 unsigned Align) const {
638 // Otherwise, if the alignment is less than or equal to the minimum ABI
639 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000640 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000641 return 0; // Use default alignment.
642
643 // On non-Darwin, the stack type alignment is always 4.
644 if (!IsDarwinVectorABI) {
645 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000646 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000647 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000648
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000649 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
650 if (isRecordWithSSEVectorType(getContext(), Ty))
651 return 16;
652
653 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000654}
655
Chris Lattnera3c109b2010-07-29 02:16:43 +0000656ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000657 if (!ByVal)
658 return ABIArgInfo::getIndirect(0, false);
659
Daniel Dunbare59d8582010-09-16 20:42:06 +0000660 // Compute the byval alignment.
661 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
662 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
663 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000664 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000665
666 // If the stack alignment is less than the type alignment, realign the
667 // argument.
668 if (StackAlign < TypeAlign)
669 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
670 /*Realign=*/true);
671
672 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000673}
674
Chris Lattnera3c109b2010-07-29 02:16:43 +0000675ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000676 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000677 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000678 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000679 if (const RecordType *RT = Ty->getAs<RecordType>()) {
680 // Structures with either a non-trivial destructor or a non-trivial
681 // copy constructor are always indirect.
682 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000683 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000684
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000686 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000687 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000688
689 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000690 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000691 return ABIArgInfo::getIgnore();
692
Daniel Dunbar53012f42009-11-09 01:33:53 +0000693 // Expand small (<= 128-bit) record types when we know that the stack layout
694 // of those arguments will match the struct. This is important because the
695 // LLVM backend isn't smart enough to remove byval, which inhibits many
696 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000697 if (getContext().getTypeSize(Ty) <= 4*32 &&
698 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000699 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000700
Chris Lattnera3c109b2010-07-29 02:16:43 +0000701 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000702 }
703
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000704 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000705 // On Darwin, some vectors are passed in memory, we handle this by passing
706 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000707 if (IsDarwinVectorABI) {
708 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000709 if ((Size == 8 || Size == 16 || Size == 32) ||
710 (Size == 64 && VT->getNumElements() == 1))
711 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
712 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000713 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000714
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000715 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000716 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000717 if (IsMMXDisabled)
718 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
719 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000720 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
721 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
722 return AAI;
723 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000724
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000725 return ABIArgInfo::getDirect();
726 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000727
728
Chris Lattnera3c109b2010-07-29 02:16:43 +0000729 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
730 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000731
Chris Lattnera3c109b2010-07-29 02:16:43 +0000732 return (Ty->isPromotableIntegerType() ?
733 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000734}
735
736llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
737 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000738 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
739 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000740
741 CGBuilderTy &Builder = CGF.Builder;
742 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
743 "ap");
744 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
745 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000746 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000747 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
748
749 uint64_t Offset =
750 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
751 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000752 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000753 "ap.next");
754 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
755
756 return AddrTyped;
757}
758
Charles Davis74f72932010-02-13 15:54:06 +0000759void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
760 llvm::GlobalValue *GV,
761 CodeGen::CodeGenModule &CGM) const {
762 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
763 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
764 // Get the LLVM function.
765 llvm::Function *Fn = cast<llvm::Function>(GV);
766
767 // Now add the 'alignstack' attribute with a value of 16.
768 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
769 }
770 }
771}
772
John McCall6374c332010-03-06 00:35:14 +0000773bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
774 CodeGen::CodeGenFunction &CGF,
775 llvm::Value *Address) const {
776 CodeGen::CGBuilderTy &Builder = CGF.Builder;
777 llvm::LLVMContext &Context = CGF.getLLVMContext();
778
Chris Lattner2acc6e32011-07-18 04:24:23 +0000779 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000780 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000781
John McCall6374c332010-03-06 00:35:14 +0000782 // 0-7 are the eight integer registers; the order is different
783 // on Darwin (for EH), but the range is the same.
784 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000785 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000786
787 if (CGF.CGM.isTargetDarwin()) {
788 // 12-16 are st(0..4). Not sure why we stop at 4.
789 // These have size 16, which is sizeof(long double) on
790 // platforms with 8-byte alignment for that type.
791 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000792 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000793
John McCall6374c332010-03-06 00:35:14 +0000794 } else {
795 // 9 is %eflags, which doesn't get a size on Darwin for some
796 // reason.
797 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
798
799 // 11-16 are st(0..5). Not sure why we stop at 5.
800 // These have size 12, which is sizeof(long double) on
801 // platforms with 4-byte alignment for that type.
802 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000803 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
804 }
John McCall6374c332010-03-06 00:35:14 +0000805
806 return false;
807}
808
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000809//===----------------------------------------------------------------------===//
810// X86-64 ABI Implementation
811//===----------------------------------------------------------------------===//
812
813
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000814namespace {
815/// X86_64ABIInfo - The X86_64 ABI information.
816class X86_64ABIInfo : public ABIInfo {
817 enum Class {
818 Integer = 0,
819 SSE,
820 SSEUp,
821 X87,
822 X87Up,
823 ComplexX87,
824 NoClass,
825 Memory
826 };
827
828 /// merge - Implement the X86_64 ABI merging algorithm.
829 ///
830 /// Merge an accumulating classification \arg Accum with a field
831 /// classification \arg Field.
832 ///
833 /// \param Accum - The accumulating classification. This should
834 /// always be either NoClass or the result of a previous merge
835 /// call. In addition, this should never be Memory (the caller
836 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000837 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000838
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000839 /// postMerge - Implement the X86_64 ABI post merging algorithm.
840 ///
841 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
842 /// final MEMORY or SSE classes when necessary.
843 ///
844 /// \param AggregateSize - The size of the current aggregate in
845 /// the classification process.
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 higher words of the containing object.
852 ///
853 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
854
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000855 /// classify - Determine the x86_64 register classes in which the
856 /// given type T should be passed.
857 ///
858 /// \param Lo - The classification for the parts of the type
859 /// residing in the low word of the containing object.
860 ///
861 /// \param Hi - The classification for the parts of the type
862 /// residing in the high word of the containing object.
863 ///
864 /// \param OffsetBase - The bit offset of this type in the
865 /// containing object. Some parameters are classified different
866 /// depending on whether they straddle an eightbyte boundary.
867 ///
868 /// If a word is unused its result will be NoClass; if a type should
869 /// be passed in Memory then at least the classification of \arg Lo
870 /// will be Memory.
871 ///
872 /// The \arg Lo class will be NoClass iff the argument is ignored.
873 ///
874 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
875 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000876 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000877
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000878 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000879 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
880 unsigned IROffset, QualType SourceTy,
881 unsigned SourceOffset) const;
882 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
883 unsigned IROffset, QualType SourceTy,
884 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000885
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000886 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000887 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000888 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000889
890 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000891 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000892 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000893
Chris Lattnera3c109b2010-07-29 02:16:43 +0000894 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000895
Bill Wendlingbb465d72010-10-18 03:41:31 +0000896 ABIArgInfo classifyArgumentType(QualType Ty,
897 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000898 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000899
John McCall67a57732011-04-21 01:20:55 +0000900 /// The 0.98 ABI revision clarified a lot of ambiguities,
901 /// unfortunately in ways that were not always consistent with
902 /// certain previous compilers. In particular, platforms which
903 /// required strict binary compatibility with older versions of GCC
904 /// may need to exempt themselves.
905 bool honorsRevision0_98() const {
906 return !getContext().Target.getTriple().isOSDarwin();
907 }
908
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000909public:
Chris Lattnerea044322010-07-29 02:01:43 +0000910 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000911
Chris Lattneree5dcd02010-07-29 02:31:05 +0000912 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000913
914 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
915 CodeGenFunction &CGF) const;
916};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000917
Chris Lattnerf13721d2010-08-31 16:44:54 +0000918/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000919class WinX86_64ABIInfo : public ABIInfo {
920
921 ABIArgInfo classify(QualType Ty) const;
922
Chris Lattnerf13721d2010-08-31 16:44:54 +0000923public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000924 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
925
926 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000927
928 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
929 CodeGenFunction &CGF) const;
930};
931
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000932class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
933public:
Chris Lattnerea044322010-07-29 02:01:43 +0000934 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
935 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000936
937 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
938 return 7;
939 }
940
941 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
942 llvm::Value *Address) const {
943 CodeGen::CGBuilderTy &Builder = CGF.Builder;
944 llvm::LLVMContext &Context = CGF.getLLVMContext();
945
Chris Lattner2acc6e32011-07-18 04:24:23 +0000946 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000947 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000948
John McCallaeeb7012010-05-27 06:19:26 +0000949 // 0-15 are the 16 integer registers.
950 // 16 is %rip.
951 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000952
953 return false;
954 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000955
Jay Foadef6de3d2011-07-11 09:56:20 +0000956 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000957 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000958 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000959 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
960 }
961
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000962};
963
Chris Lattnerf13721d2010-08-31 16:44:54 +0000964class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
965public:
966 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
967 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
968
969 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
970 return 7;
971 }
972
973 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
974 llvm::Value *Address) const {
975 CodeGen::CGBuilderTy &Builder = CGF.Builder;
976 llvm::LLVMContext &Context = CGF.getLLVMContext();
977
Chris Lattner2acc6e32011-07-18 04:24:23 +0000978 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattnerf13721d2010-08-31 16:44:54 +0000979 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000980
Chris Lattnerf13721d2010-08-31 16:44:54 +0000981 // 0-15 are the 16 integer registers.
982 // 16 is %rip.
983 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
984
985 return false;
986 }
987};
988
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000989}
990
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000991void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
992 Class &Hi) const {
993 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
994 //
995 // (a) If one of the classes is Memory, the whole argument is passed in
996 // memory.
997 //
998 // (b) If X87UP is not preceded by X87, the whole argument is passed in
999 // memory.
1000 //
1001 // (c) If the size of the aggregate exceeds two eightbytes and the first
1002 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1003 // argument is passed in memory. NOTE: This is necessary to keep the
1004 // ABI working for processors that don't support the __m256 type.
1005 //
1006 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1007 //
1008 // Some of these are enforced by the merging logic. Others can arise
1009 // only with unions; for example:
1010 // union { _Complex double; unsigned; }
1011 //
1012 // Note that clauses (b) and (c) were added in 0.98.
1013 //
1014 if (Hi == Memory)
1015 Lo = Memory;
1016 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1017 Lo = Memory;
1018 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1019 Lo = Memory;
1020 if (Hi == SSEUp && Lo != SSE)
1021 Hi = SSE;
1022}
1023
Chris Lattner1090a9b2010-06-28 21:43:59 +00001024X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001025 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1026 // classified recursively so that always two fields are
1027 // considered. The resulting class is calculated according to
1028 // the classes of the fields in the eightbyte:
1029 //
1030 // (a) If both classes are equal, this is the resulting class.
1031 //
1032 // (b) If one of the classes is NO_CLASS, the resulting class is
1033 // the other class.
1034 //
1035 // (c) If one of the classes is MEMORY, the result is the MEMORY
1036 // class.
1037 //
1038 // (d) If one of the classes is INTEGER, the result is the
1039 // INTEGER.
1040 //
1041 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1042 // MEMORY is used as class.
1043 //
1044 // (f) Otherwise class SSE is used.
1045
1046 // Accum should never be memory (we should have returned) or
1047 // ComplexX87 (because this cannot be passed in a structure).
1048 assert((Accum != Memory && Accum != ComplexX87) &&
1049 "Invalid accumulated classification during merge.");
1050 if (Accum == Field || Field == NoClass)
1051 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001052 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001053 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001054 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001055 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001056 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001057 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001058 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1059 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001060 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001061 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001062}
1063
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001064void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001065 Class &Lo, Class &Hi) const {
1066 // FIXME: This code can be simplified by introducing a simple value class for
1067 // Class pairs with appropriate constructor methods for the various
1068 // situations.
1069
1070 // FIXME: Some of the split computations are wrong; unaligned vectors
1071 // shouldn't be passed in registers for example, so there is no chance they
1072 // can straddle an eightbyte. Verify & simplify.
1073
1074 Lo = Hi = NoClass;
1075
1076 Class &Current = OffsetBase < 64 ? Lo : Hi;
1077 Current = Memory;
1078
John McCall183700f2009-09-21 23:43:11 +00001079 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001080 BuiltinType::Kind k = BT->getKind();
1081
1082 if (k == BuiltinType::Void) {
1083 Current = NoClass;
1084 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1085 Lo = Integer;
1086 Hi = Integer;
1087 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1088 Current = Integer;
1089 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1090 Current = SSE;
1091 } else if (k == BuiltinType::LongDouble) {
1092 Lo = X87;
1093 Hi = X87Up;
1094 }
1095 // FIXME: _Decimal32 and _Decimal64 are SSE.
1096 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001097 return;
1098 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001099
Chris Lattner1090a9b2010-06-28 21:43:59 +00001100 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001101 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001102 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001103 return;
1104 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001105
Chris Lattner1090a9b2010-06-28 21:43:59 +00001106 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001107 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001108 return;
1109 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001110
Chris Lattner1090a9b2010-06-28 21:43:59 +00001111 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001112 if (Ty->isMemberFunctionPointerType())
1113 Lo = Hi = Integer;
1114 else
1115 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001116 return;
1117 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001118
Chris Lattner1090a9b2010-06-28 21:43:59 +00001119 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001120 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001121 if (Size == 32) {
1122 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1123 // float> as integer.
1124 Current = Integer;
1125
1126 // If this type crosses an eightbyte boundary, it should be
1127 // split.
1128 uint64_t EB_Real = (OffsetBase) / 64;
1129 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1130 if (EB_Real != EB_Imag)
1131 Hi = Lo;
1132 } else if (Size == 64) {
1133 // gcc passes <1 x double> in memory. :(
1134 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1135 return;
1136
1137 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001138 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001139 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1140 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1141 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001142 Current = Integer;
1143 else
1144 Current = SSE;
1145
1146 // If this type crosses an eightbyte boundary, it should be
1147 // split.
1148 if (OffsetBase && OffsetBase != 64)
1149 Hi = Lo;
Bruno Cardoso Lopes75d28b52011-07-12 02:47:38 +00001150 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001151 // Arguments of 256-bits are split into four eightbyte chunks. The
1152 // least significant one belongs to class SSE and all the others to class
1153 // SSEUP. The original Lo and Hi design considers that types can't be
1154 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1155 // This design isn't correct for 256-bits, but since there're no cases
1156 // where the upper parts would need to be inspected, avoid adding
1157 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158 Lo = SSE;
1159 Hi = SSEUp;
1160 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001161 return;
1162 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001163
Chris Lattner1090a9b2010-06-28 21:43:59 +00001164 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001165 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001166
Chris Lattnerea044322010-07-29 02:01:43 +00001167 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001168 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001169 if (Size <= 64)
1170 Current = Integer;
1171 else if (Size <= 128)
1172 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001173 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001174 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001175 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001176 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001177 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001178 Current = ComplexX87;
1179
1180 // If this complex type crosses an eightbyte boundary then it
1181 // should be split.
1182 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001183 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001184 if (Hi == NoClass && EB_Real != EB_Imag)
1185 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001186
Chris Lattner1090a9b2010-06-28 21:43:59 +00001187 return;
1188 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001189
Chris Lattnerea044322010-07-29 02:01:43 +00001190 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001191 // Arrays are treated like structures.
1192
Chris Lattnerea044322010-07-29 02:01:43 +00001193 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194
1195 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001196 // than four eightbytes, ..., it has class MEMORY.
1197 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001198 return;
1199
1200 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1201 // fields, it has class MEMORY.
1202 //
1203 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001204 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001205 return;
1206
1207 // Otherwise implement simplified merge. We could be smarter about
1208 // this, but it isn't worth it and would be harder to verify.
1209 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001210 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001211 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001212
1213 // The only case a 256-bit wide vector could be used is when the array
1214 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1215 // to work for sizes wider than 128, early check and fallback to memory.
1216 if (Size > 128 && EltSize != 256)
1217 return;
1218
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001219 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1220 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001221 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001222 Lo = merge(Lo, FieldLo);
1223 Hi = merge(Hi, FieldHi);
1224 if (Lo == Memory || Hi == Memory)
1225 break;
1226 }
1227
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001228 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001229 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001230 return;
1231 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001232
Chris Lattner1090a9b2010-06-28 21:43:59 +00001233 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001234 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001235
1236 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001237 // than four eightbytes, ..., it has class MEMORY.
1238 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001239 return;
1240
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001241 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1242 // copy constructor or a non-trivial destructor, it is passed by invisible
1243 // reference.
1244 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1245 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001246
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001247 const RecordDecl *RD = RT->getDecl();
1248
1249 // Assume variable sized types are passed in memory.
1250 if (RD->hasFlexibleArrayMember())
1251 return;
1252
Chris Lattnerea044322010-07-29 02:01:43 +00001253 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001254
1255 // Reset Lo class, this will be recomputed.
1256 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001257
1258 // If this is a C++ record, classify the bases first.
1259 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1260 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1261 e = CXXRD->bases_end(); i != e; ++i) {
1262 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1263 "Unexpected base class!");
1264 const CXXRecordDecl *Base =
1265 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1266
1267 // Classify this field.
1268 //
1269 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1270 // single eightbyte, each is classified separately. Each eightbyte gets
1271 // initialized to class NO_CLASS.
1272 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001273 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001274 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001275 Lo = merge(Lo, FieldLo);
1276 Hi = merge(Hi, FieldHi);
1277 if (Lo == Memory || Hi == Memory)
1278 break;
1279 }
1280 }
1281
1282 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001283 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001284 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001285 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001286 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1287 bool BitField = i->isBitField();
1288
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001289 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1290 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001291 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001292 // The only case a 256-bit wide vector could be used is when the struct
1293 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1294 // to work for sizes wider than 128, early check and fallback to memory.
1295 //
1296 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1297 Lo = Memory;
1298 return;
1299 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001300 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001301 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001302 Lo = Memory;
1303 return;
1304 }
1305
1306 // Classify this field.
1307 //
1308 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1309 // exceeds a single eightbyte, each is classified
1310 // separately. Each eightbyte gets initialized to class
1311 // NO_CLASS.
1312 Class FieldLo, FieldHi;
1313
1314 // Bit-fields require special handling, they do not force the
1315 // structure to be passed in memory even if unaligned, and
1316 // therefore they can straddle an eightbyte.
1317 if (BitField) {
1318 // Ignore padding bit-fields.
1319 if (i->isUnnamedBitfield())
1320 continue;
1321
1322 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001323 uint64_t Size =
1324 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001325
1326 uint64_t EB_Lo = Offset / 64;
1327 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1328 FieldLo = FieldHi = NoClass;
1329 if (EB_Lo) {
1330 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1331 FieldLo = NoClass;
1332 FieldHi = Integer;
1333 } else {
1334 FieldLo = Integer;
1335 FieldHi = EB_Hi ? Integer : NoClass;
1336 }
1337 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001338 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001339 Lo = merge(Lo, FieldLo);
1340 Hi = merge(Hi, FieldHi);
1341 if (Lo == Memory || Hi == Memory)
1342 break;
1343 }
1344
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001345 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001346 }
1347}
1348
Chris Lattner9c254f02010-06-29 06:01:59 +00001349ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001350 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1351 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001352 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001353 // Treat an enum type as its underlying type.
1354 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1355 Ty = EnumTy->getDecl()->getIntegerType();
1356
1357 return (Ty->isPromotableIntegerType() ?
1358 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1359 }
1360
1361 return ABIArgInfo::getIndirect(0);
1362}
1363
Chris Lattner9c254f02010-06-29 06:01:59 +00001364ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001365 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1366 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001367 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001368 // Treat an enum type as its underlying type.
1369 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1370 Ty = EnumTy->getDecl()->getIntegerType();
1371
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001372 return (Ty->isPromotableIntegerType() ?
1373 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001374 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001375
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001376 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1377 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001378
Chris Lattner855d2272011-05-22 23:21:23 +00001379 // Compute the byval alignment. We specify the alignment of the byval in all
1380 // cases so that the mid-level optimizer knows the alignment of the byval.
1381 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1382 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001383}
1384
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001385/// GetByteVectorType - The ABI specifies that a value should be passed in an
1386/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001387/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001388llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001389 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001390
Chris Lattner15842bd2010-07-29 05:02:29 +00001391 // Wrapper structs that just contain vectors are passed just like vectors,
1392 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001393 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001394 while (STy && STy->getNumElements() == 1) {
1395 IRType = STy->getElementType(0);
1396 STy = dyn_cast<llvm::StructType>(IRType);
1397 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001398
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001399 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001400 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1401 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001402 unsigned BitWidth = VT->getBitWidth();
1403 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001404 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1405 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1406 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1407 EltTy->isIntegerTy(128)))
1408 return VT;
1409 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001410
Chris Lattner0f408f52010-07-29 04:56:46 +00001411 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1412}
1413
Chris Lattnere2962be2010-07-29 07:30:00 +00001414/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1415/// is known to either be off the end of the specified type or being in
1416/// alignment padding. The user type specified is known to be at most 128 bits
1417/// in size, and have passed through X86_64ABIInfo::classify with a successful
1418/// classification that put one of the two halves in the INTEGER class.
1419///
1420/// It is conservatively correct to return false.
1421static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1422 unsigned EndBit, ASTContext &Context) {
1423 // If the bytes being queried are off the end of the type, there is no user
1424 // data hiding here. This handles analysis of builtins, vectors and other
1425 // types that don't contain interesting padding.
1426 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1427 if (TySize <= StartBit)
1428 return true;
1429
Chris Lattner021c3a32010-07-29 07:43:55 +00001430 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1431 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1432 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1433
1434 // Check each element to see if the element overlaps with the queried range.
1435 for (unsigned i = 0; i != NumElts; ++i) {
1436 // If the element is after the span we care about, then we're done..
1437 unsigned EltOffset = i*EltSize;
1438 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001439
Chris Lattner021c3a32010-07-29 07:43:55 +00001440 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1441 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1442 EndBit-EltOffset, Context))
1443 return false;
1444 }
1445 // If it overlaps no elements, then it is safe to process as padding.
1446 return true;
1447 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001448
Chris Lattnere2962be2010-07-29 07:30:00 +00001449 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1450 const RecordDecl *RD = RT->getDecl();
1451 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001452
Chris Lattnere2962be2010-07-29 07:30:00 +00001453 // If this is a C++ record, check the bases first.
1454 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1455 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1456 e = CXXRD->bases_end(); i != e; ++i) {
1457 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1458 "Unexpected base class!");
1459 const CXXRecordDecl *Base =
1460 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001461
Chris Lattnere2962be2010-07-29 07:30:00 +00001462 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001463 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001464 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001465
Chris Lattnere2962be2010-07-29 07:30:00 +00001466 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1467 if (!BitsContainNoUserData(i->getType(), BaseStart,
1468 EndBit-BaseOffset, Context))
1469 return false;
1470 }
1471 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001472
Chris Lattnere2962be2010-07-29 07:30:00 +00001473 // Verify that no field has data that overlaps the region of interest. Yes
1474 // this could be sped up a lot by being smarter about queried fields,
1475 // however we're only looking at structs up to 16 bytes, so we don't care
1476 // much.
1477 unsigned idx = 0;
1478 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1479 i != e; ++i, ++idx) {
1480 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001481
Chris Lattnere2962be2010-07-29 07:30:00 +00001482 // If we found a field after the region we care about, then we're done.
1483 if (FieldOffset >= EndBit) break;
1484
1485 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1486 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1487 Context))
1488 return false;
1489 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001490
Chris Lattnere2962be2010-07-29 07:30:00 +00001491 // If nothing in this record overlapped the area of interest, then we're
1492 // clean.
1493 return true;
1494 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001495
Chris Lattnere2962be2010-07-29 07:30:00 +00001496 return false;
1497}
1498
Chris Lattner0b362002010-07-29 18:39:32 +00001499/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1500/// float member at the specified offset. For example, {int,{float}} has a
1501/// float at offset 4. It is conservatively correct for this routine to return
1502/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001503static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001504 const llvm::TargetData &TD) {
1505 // Base case if we find a float.
1506 if (IROffset == 0 && IRType->isFloatTy())
1507 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001508
Chris Lattner0b362002010-07-29 18:39:32 +00001509 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001510 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001511 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1512 unsigned Elt = SL->getElementContainingOffset(IROffset);
1513 IROffset -= SL->getElementOffset(Elt);
1514 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1515 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001516
Chris Lattner0b362002010-07-29 18:39:32 +00001517 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001518 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1519 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001520 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1521 IROffset -= IROffset/EltSize*EltSize;
1522 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1523 }
1524
1525 return false;
1526}
1527
Chris Lattnerf47c9442010-07-29 18:13:09 +00001528
1529/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1530/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001531llvm::Type *X86_64ABIInfo::
1532GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001533 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001534 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001535 // pass as float if the last 4 bytes is just padding. This happens for
1536 // structs that contain 3 floats.
1537 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1538 SourceOffset*8+64, getContext()))
1539 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001540
Chris Lattner0b362002010-07-29 18:39:32 +00001541 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1542 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1543 // case.
1544 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001545 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1546 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001547
Chris Lattnerf47c9442010-07-29 18:13:09 +00001548 return llvm::Type::getDoubleTy(getVMContext());
1549}
1550
1551
Chris Lattner0d2656d2010-07-29 17:40:35 +00001552/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1553/// an 8-byte GPR. This means that we either have a scalar or we are talking
1554/// about the high or low part of an up-to-16-byte struct. This routine picks
1555/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001556/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1557/// etc).
1558///
1559/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1560/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1561/// the 8-byte value references. PrefType may be null.
1562///
1563/// SourceTy is the source level type for the entire argument. SourceOffset is
1564/// an offset into this that we're processing (which is always either 0 or 8).
1565///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001566llvm::Type *X86_64ABIInfo::
1567GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001568 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001569 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1570 // returning an 8-byte unit starting with it. See if we can safely use it.
1571 if (IROffset == 0) {
1572 // Pointers and int64's always fill the 8-byte unit.
1573 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1574 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001575
Chris Lattnere2962be2010-07-29 07:30:00 +00001576 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1577 // goodness in the source type is just tail padding. This is allowed to
1578 // kick in for struct {double,int} on the int, but not on
1579 // struct{double,int,int} because we wouldn't return the second int. We
1580 // have to do this analysis on the source type because we can't depend on
1581 // unions being lowered a specific way etc.
1582 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1583 IRType->isIntegerTy(32)) {
1584 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001585
Chris Lattnere2962be2010-07-29 07:30:00 +00001586 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1587 SourceOffset*8+64, getContext()))
1588 return IRType;
1589 }
1590 }
Chris Lattner49382de2010-07-28 22:44:07 +00001591
Chris Lattner2acc6e32011-07-18 04:24:23 +00001592 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001593 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001594 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001595 if (IROffset < SL->getSizeInBytes()) {
1596 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1597 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001598
Chris Lattner0d2656d2010-07-29 17:40:35 +00001599 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1600 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001601 }
Chris Lattner49382de2010-07-28 22:44:07 +00001602 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001603
Chris Lattner2acc6e32011-07-18 04:24:23 +00001604 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001605 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001606 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1607 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001608 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1609 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001610 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001611
Chris Lattner49382de2010-07-28 22:44:07 +00001612 // Okay, we don't have any better idea of what to pass, so we pass this in an
1613 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001614 unsigned TySizeInBytes =
1615 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001616
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001617 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001618
Chris Lattner49382de2010-07-28 22:44:07 +00001619 // It is always safe to classify this as an integer type up to i64 that
1620 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001621 return llvm::IntegerType::get(getVMContext(),
1622 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001623}
1624
Chris Lattner66e7b682010-09-01 00:50:20 +00001625
1626/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1627/// be used as elements of a two register pair to pass or return, return a
1628/// first class aggregate to represent them. For example, if the low part of
1629/// a by-value argument should be passed as i32* and the high part as float,
1630/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001631static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001632GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001633 const llvm::TargetData &TD) {
1634 // In order to correctly satisfy the ABI, we need to the high part to start
1635 // at offset 8. If the high and low parts we inferred are both 4-byte types
1636 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1637 // the second element at offset 8. Check for this:
1638 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1639 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1640 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1641 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001642
Chris Lattner66e7b682010-09-01 00:50:20 +00001643 // To handle this, we have to increase the size of the low part so that the
1644 // second element will start at an 8 byte offset. We can't increase the size
1645 // of the second element because it might make us access off the end of the
1646 // struct.
1647 if (HiStart != 8) {
1648 // There are only two sorts of types the ABI generation code can produce for
1649 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1650 // Promote these to a larger type.
1651 if (Lo->isFloatTy())
1652 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1653 else {
1654 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1655 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1656 }
1657 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001658
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001659 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001660
1661
Chris Lattner66e7b682010-09-01 00:50:20 +00001662 // Verify that the second element is at an 8-byte offset.
1663 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1664 "Invalid x86-64 argument pair!");
1665 return Result;
1666}
1667
Chris Lattner519f68c2010-07-28 23:06:14 +00001668ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001669classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001670 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1671 // classification algorithm.
1672 X86_64ABIInfo::Class Lo, Hi;
1673 classify(RetTy, 0, Lo, Hi);
1674
1675 // Check some invariants.
1676 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001677 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1678
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001679 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001680 switch (Lo) {
1681 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001682 if (Hi == NoClass)
1683 return ABIArgInfo::getIgnore();
1684 // If the low part is just padding, it takes no register, leave ResType
1685 // null.
1686 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1687 "Unknown missing lo part");
1688 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001689
1690 case SSEUp:
1691 case X87Up:
1692 assert(0 && "Invalid classification for lo word.");
1693
1694 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1695 // hidden argument.
1696 case Memory:
1697 return getIndirectReturnResult(RetTy);
1698
1699 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1700 // available register of the sequence %rax, %rdx is used.
1701 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001702 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001703
Chris Lattnereb518b42010-07-29 21:42:50 +00001704 // If we have a sign or zero extended integer, make sure to return Extend
1705 // so that the parameter gets the right LLVM IR attributes.
1706 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1707 // Treat an enum type as its underlying type.
1708 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1709 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001710
Chris Lattnereb518b42010-07-29 21:42:50 +00001711 if (RetTy->isIntegralOrEnumerationType() &&
1712 RetTy->isPromotableIntegerType())
1713 return ABIArgInfo::getExtend();
1714 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001715 break;
1716
1717 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1718 // available SSE register of the sequence %xmm0, %xmm1 is used.
1719 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001720 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001721 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001722
1723 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1724 // returned on the X87 stack in %st0 as 80-bit x87 number.
1725 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001726 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001727 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001728
1729 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1730 // part of the value is returned in %st0 and the imaginary part in
1731 // %st1.
1732 case ComplexX87:
1733 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001734 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001735 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001736 NULL);
1737 break;
1738 }
1739
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001740 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001741 switch (Hi) {
1742 // Memory was handled previously and X87 should
1743 // never occur as a hi class.
1744 case Memory:
1745 case X87:
1746 assert(0 && "Invalid classification for hi word.");
1747
1748 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001749 case NoClass:
1750 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001751
Chris Lattner3db4dde2010-09-01 00:20:33 +00001752 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001753 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001754 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1755 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001756 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001757 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001758 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001759 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1760 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001761 break;
1762
1763 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001764 // is passed in the next available eightbyte chunk if the last used
1765 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001766 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001767 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001768 case SSEUp:
1769 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001770 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001771 break;
1772
1773 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1774 // returned together with the previous X87 value in %st0.
1775 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001776 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001777 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001778 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001779 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001780 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001781 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001782 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1783 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001784 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001785 break;
1786 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001787
Chris Lattner3db4dde2010-09-01 00:20:33 +00001788 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001789 // known to pass in the high eightbyte of the result. We do this by forming a
1790 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001791 if (HighPart)
1792 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001793
Chris Lattnereb518b42010-07-29 21:42:50 +00001794 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001795}
1796
Chris Lattnera3c109b2010-07-29 02:16:43 +00001797ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001798 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001799 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001800 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001801
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001802 // Check some invariants.
1803 // FIXME: Enforce these by construction.
1804 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001805 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1806
1807 neededInt = 0;
1808 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001809 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001810 switch (Lo) {
1811 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001812 if (Hi == NoClass)
1813 return ABIArgInfo::getIgnore();
1814 // If the low part is just padding, it takes no register, leave ResType
1815 // null.
1816 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1817 "Unknown missing lo part");
1818 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001819
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001820 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1821 // on the stack.
1822 case Memory:
1823
1824 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1825 // COMPLEX_X87, it is passed in memory.
1826 case X87:
1827 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001828 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1829 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001830 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001831
1832 case SSEUp:
1833 case X87Up:
1834 assert(0 && "Invalid classification for lo word.");
1835
1836 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1837 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1838 // and %r9 is used.
1839 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001840 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001841
Chris Lattner49382de2010-07-28 22:44:07 +00001842 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001843 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001844
1845 // If we have a sign or zero extended integer, make sure to return Extend
1846 // so that the parameter gets the right LLVM IR attributes.
1847 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1848 // Treat an enum type as its underlying type.
1849 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1850 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001851
Chris Lattnereb518b42010-07-29 21:42:50 +00001852 if (Ty->isIntegralOrEnumerationType() &&
1853 Ty->isPromotableIntegerType())
1854 return ABIArgInfo::getExtend();
1855 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001856
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001857 break;
1858
1859 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1860 // available SSE register is used, the registers are taken in the
1861 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001862 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001863 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001864 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001865 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001866 break;
1867 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001868 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001869
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001870 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001871 switch (Hi) {
1872 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001873 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001874 // which is passed in memory.
1875 case Memory:
1876 case X87:
1877 case ComplexX87:
1878 assert(0 && "Invalid classification for hi word.");
1879 break;
1880
1881 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001882
Chris Lattner645406a2010-09-01 00:24:35 +00001883 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001884 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001885 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001886 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001887
Chris Lattner645406a2010-09-01 00:24:35 +00001888 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1889 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001890 break;
1891
1892 // X87Up generally doesn't occur here (long double is passed in
1893 // memory), except in situations involving unions.
1894 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001895 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001896 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001897
Chris Lattner645406a2010-09-01 00:24:35 +00001898 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1899 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001900
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001901 ++neededSSE;
1902 break;
1903
1904 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1905 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001906 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001907 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001908 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001909 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001910 break;
1911 }
1912
Chris Lattner645406a2010-09-01 00:24:35 +00001913 // If a high part was specified, merge it together with the low part. It is
1914 // known to pass in the high eightbyte of the result. We do this by forming a
1915 // first class struct aggregate with the high and low part: {low, high}
1916 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001917 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001918
Chris Lattnereb518b42010-07-29 21:42:50 +00001919 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001920}
1921
Chris Lattneree5dcd02010-07-29 02:31:05 +00001922void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001923
Chris Lattnera3c109b2010-07-29 02:16:43 +00001924 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001925
1926 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001927 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001928
1929 // If the return value is indirect, then the hidden argument is consuming one
1930 // integer register.
1931 if (FI.getReturnInfo().isIndirect())
1932 --freeIntRegs;
1933
1934 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1935 // get assigned (in left-to-right order) for passing as follows...
1936 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1937 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001938 unsigned neededInt, neededSSE;
1939 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001940
1941 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1942 // eightbyte of an argument, the whole argument is passed on the
1943 // stack. If registers have already been assigned for some
1944 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001945 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001946 freeIntRegs -= neededInt;
1947 freeSSERegs -= neededSSE;
1948 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001949 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001950 }
1951 }
1952}
1953
1954static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1955 QualType Ty,
1956 CodeGenFunction &CGF) {
1957 llvm::Value *overflow_arg_area_p =
1958 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1959 llvm::Value *overflow_arg_area =
1960 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1961
1962 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1963 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1964 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1965 if (Align > 8) {
1966 // Note that we follow the ABI & gcc here, even though the type
1967 // could in theory have an alignment greater than 16. This case
1968 // shouldn't ever matter in practice.
1969
1970 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001971 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001972 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001973 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1974 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001975 CGF.Int64Ty);
1976 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001977 overflow_arg_area =
1978 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1979 overflow_arg_area->getType(),
1980 "overflow_arg_area.align");
1981 }
1982
1983 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001984 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001985 llvm::Value *Res =
1986 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001987 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001988
1989 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1990 // l->overflow_arg_area + sizeof(type).
1991 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1992 // an 8 byte boundary.
1993
1994 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001995 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001996 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001997 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1998 "overflow_arg_area.next");
1999 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2000
2001 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2002 return Res;
2003}
2004
2005llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2006 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002007 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002009 // Assume that va_list type is correct; should be pointer to LLVM type:
2010 // struct {
2011 // i32 gp_offset;
2012 // i32 fp_offset;
2013 // i8* overflow_arg_area;
2014 // i8* reg_save_area;
2015 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002016 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002017
Chris Lattnera14db752010-03-11 18:19:55 +00002018 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002019 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002020
2021 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2022 // in the registers. If not go to step 7.
2023 if (!neededInt && !neededSSE)
2024 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2025
2026 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2027 // general purpose registers needed to pass type and num_fp to hold
2028 // the number of floating point registers needed.
2029
2030 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2031 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2032 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2033 //
2034 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2035 // register save space).
2036
2037 llvm::Value *InRegs = 0;
2038 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2039 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2040 if (neededInt) {
2041 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2042 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002043 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2044 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002045 }
2046
2047 if (neededSSE) {
2048 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2049 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2050 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002051 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2052 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002053 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2054 }
2055
2056 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2057 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2058 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2059 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2060
2061 // Emit code to load the value if it was passed in registers.
2062
2063 CGF.EmitBlock(InRegBlock);
2064
2065 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2066 // an offset of l->gp_offset and/or l->fp_offset. This may require
2067 // copying to a temporary location in case the parameter is passed
2068 // in different register classes or requires an alignment greater
2069 // than 8 for general purpose registers and 16 for XMM registers.
2070 //
2071 // FIXME: This really results in shameful code when we end up needing to
2072 // collect arguments from different places; often what should result in a
2073 // simple assembling of a structure from scattered addresses has many more
2074 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002075 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002076 llvm::Value *RegAddr =
2077 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2078 "reg_save_area");
2079 if (neededInt && neededSSE) {
2080 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002081 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002082 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002083 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2084 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002085 llvm::Type *TyLo = ST->getElementType(0);
2086 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002087 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002088 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002089 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2090 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002091 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2092 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002093 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2094 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002095 llvm::Value *V =
2096 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2097 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2098 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2099 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2100
Owen Andersona1cf15f2009-07-14 23:10:40 +00002101 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002102 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002103 } else if (neededInt) {
2104 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2105 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002106 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002107 } else if (neededSSE == 1) {
2108 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2109 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2110 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002111 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002112 assert(neededSSE == 2 && "Invalid number of needed registers!");
2113 // SSE registers are spaced 16 bytes apart in the register save
2114 // area, we need to collect the two eightbytes together.
2115 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002116 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002117 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002118 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002119 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002120 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002121 DoubleTy, NULL);
2122 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2123 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2124 DblPtrTy));
2125 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2126 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2127 DblPtrTy));
2128 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2129 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2130 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002131 }
2132
2133 // AMD64-ABI 3.5.7p5: Step 5. Set:
2134 // l->gp_offset = l->gp_offset + num_gp * 8
2135 // l->fp_offset = l->fp_offset + num_fp * 16.
2136 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002137 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002138 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2139 gp_offset_p);
2140 }
2141 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002142 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002143 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2144 fp_offset_p);
2145 }
2146 CGF.EmitBranch(ContBlock);
2147
2148 // Emit code to load the value if it was passed in memory.
2149
2150 CGF.EmitBlock(InMemBlock);
2151 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2152
2153 // Return the appropriate result.
2154
2155 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002156 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002157 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002158 ResAddr->addIncoming(RegAddr, InRegBlock);
2159 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002160 return ResAddr;
2161}
2162
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002163ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2164
2165 if (Ty->isVoidType())
2166 return ABIArgInfo::getIgnore();
2167
2168 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2169 Ty = EnumTy->getDecl()->getIntegerType();
2170
2171 uint64_t Size = getContext().getTypeSize(Ty);
2172
2173 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002174 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2175 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002176 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2177
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002178 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2179 if (Size == 128 &&
2180 getContext().Target.getTriple().getOS() == llvm::Triple::MinGW32)
2181 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2182 Size));
2183
2184 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2185 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2186 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002187 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002188 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2189 Size));
2190
2191 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2192 }
2193
2194 if (Ty->isPromotableIntegerType())
2195 return ABIArgInfo::getExtend();
2196
2197 return ABIArgInfo::getDirect();
2198}
2199
2200void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2201
2202 QualType RetTy = FI.getReturnType();
2203 FI.getReturnInfo() = classify(RetTy);
2204
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002205 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2206 it != ie; ++it)
2207 it->info = classify(it->type);
2208}
2209
Chris Lattnerf13721d2010-08-31 16:44:54 +00002210llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2211 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002212 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2213 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002214
Chris Lattnerf13721d2010-08-31 16:44:54 +00002215 CGBuilderTy &Builder = CGF.Builder;
2216 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2217 "ap");
2218 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2219 llvm::Type *PTy =
2220 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2221 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2222
2223 uint64_t Offset =
2224 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2225 llvm::Value *NextAddr =
2226 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2227 "ap.next");
2228 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2229
2230 return AddrTyped;
2231}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002232
John McCallec853ba2010-03-11 00:10:12 +00002233// PowerPC-32
2234
2235namespace {
2236class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2237public:
Chris Lattnerea044322010-07-29 02:01:43 +00002238 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002239
John McCallec853ba2010-03-11 00:10:12 +00002240 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2241 // This is recovered from gcc output.
2242 return 1; // r1 is the dedicated stack pointer
2243 }
2244
2245 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002246 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002247};
2248
2249}
2250
2251bool
2252PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2253 llvm::Value *Address) const {
2254 // This is calculated from the LLVM and GCC tables and verified
2255 // against gcc output. AFAIK all ABIs use the same encoding.
2256
2257 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2258 llvm::LLVMContext &Context = CGF.getLLVMContext();
2259
Chris Lattner2acc6e32011-07-18 04:24:23 +00002260 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002261 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2262 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2263 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2264
2265 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002266 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002267
2268 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002269 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002270
2271 // 64-76 are various 4-byte special-purpose registers:
2272 // 64: mq
2273 // 65: lr
2274 // 66: ctr
2275 // 67: ap
2276 // 68-75 cr0-7
2277 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002278 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002279
2280 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002281 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002282
2283 // 109: vrsave
2284 // 110: vscr
2285 // 111: spe_acc
2286 // 112: spefscr
2287 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002288 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002289
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002290 return false;
John McCallec853ba2010-03-11 00:10:12 +00002291}
2292
2293
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002294//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002295// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002296//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002297
2298namespace {
2299
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002300class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002301public:
2302 enum ABIKind {
2303 APCS = 0,
2304 AAPCS = 1,
2305 AAPCS_VFP
2306 };
2307
2308private:
2309 ABIKind Kind;
2310
2311public:
Chris Lattnerea044322010-07-29 02:01:43 +00002312 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002313
John McCall49e34be2011-08-30 01:42:09 +00002314 bool isEABI() const {
2315 StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2316 return (Env == "gnueabi" || Env == "eabi");
2317 }
2318
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002319private:
2320 ABIKind getABIKind() const { return Kind; }
2321
Chris Lattnera3c109b2010-07-29 02:16:43 +00002322 ABIArgInfo classifyReturnType(QualType RetTy) const;
2323 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002324
Chris Lattneree5dcd02010-07-29 02:31:05 +00002325 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002326
2327 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2328 CodeGenFunction &CGF) const;
2329};
2330
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002331class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2332public:
Chris Lattnerea044322010-07-29 02:01:43 +00002333 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2334 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002335
John McCall49e34be2011-08-30 01:42:09 +00002336 const ARMABIInfo &getABIInfo() const {
2337 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2338 }
2339
John McCall6374c332010-03-06 00:35:14 +00002340 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2341 return 13;
2342 }
Roman Divacky09345d12011-05-18 19:36:54 +00002343
Chris Lattner5f9e2722011-07-23 10:55:15 +00002344 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002345 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2346 }
2347
Roman Divacky09345d12011-05-18 19:36:54 +00002348 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2349 llvm::Value *Address) const {
2350 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2351 llvm::LLVMContext &Context = CGF.getLLVMContext();
2352
Chris Lattner2acc6e32011-07-18 04:24:23 +00002353 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002354 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2355
2356 // 0-15 are the 16 integer registers.
2357 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2358
2359 return false;
2360 }
John McCall49e34be2011-08-30 01:42:09 +00002361
2362 unsigned getSizeOfUnwindException() const {
2363 if (getABIInfo().isEABI()) return 88;
2364 return TargetCodeGenInfo::getSizeOfUnwindException();
2365 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002366};
2367
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002368}
2369
Chris Lattneree5dcd02010-07-29 02:31:05 +00002370void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002371 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002372 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002373 it != ie; ++it)
2374 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002375
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002376 // Always honor user-specified calling convention.
2377 if (FI.getCallingConvention() != llvm::CallingConv::C)
2378 return;
2379
2380 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002381 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002382 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002383 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002384 else
2385 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002386
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002387 // If user did not ask for specific calling convention explicitly (e.g. via
2388 // pcs attribute), set effective calling convention if it's different than ABI
2389 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002390 switch (getABIKind()) {
2391 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002392 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2393 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002394 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002395 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002396 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2397 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002398 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002399 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002400 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2401 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002402 break;
2403 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002404}
2405
Bob Wilson194f06a2011-08-03 05:58:22 +00002406/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2407/// aggregate. If HAMembers is non-null, the number of base elements
2408/// contained in the type is returned through it; this is used for the
2409/// recursive calls that check aggregate component types.
2410static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2411 ASTContext &Context,
2412 uint64_t *HAMembers = 0) {
2413 uint64_t Members;
2414 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2415 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2416 return false;
2417 Members *= AT->getSize().getZExtValue();
2418 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2419 const RecordDecl *RD = RT->getDecl();
2420 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2421 return false;
2422 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2423 if (!CXXRD->isAggregate())
2424 return false;
2425 }
2426 Members = 0;
2427 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2428 i != e; ++i) {
2429 const FieldDecl *FD = *i;
2430 uint64_t FldMembers;
2431 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2432 return false;
2433 Members += FldMembers;
2434 }
2435 } else {
2436 Members = 1;
2437 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2438 Members = 2;
2439 Ty = CT->getElementType();
2440 }
2441
2442 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2443 // double, or 64-bit or 128-bit vectors.
2444 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2445 if (BT->getKind() != BuiltinType::Float &&
2446 BT->getKind() != BuiltinType::Double)
2447 return false;
2448 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2449 unsigned VecSize = Context.getTypeSize(VT);
2450 if (VecSize != 64 && VecSize != 128)
2451 return false;
2452 } else {
2453 return false;
2454 }
2455
2456 // The base type must be the same for all members. Vector types of the
2457 // same total size are treated as being equivalent here.
2458 const Type *TyPtr = Ty.getTypePtr();
2459 if (!Base)
2460 Base = TyPtr;
2461 if (Base != TyPtr &&
2462 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2463 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2464 return false;
2465 }
2466
2467 // Homogeneous Aggregates can have at most 4 members of the base type.
2468 if (HAMembers)
2469 *HAMembers = Members;
2470 return (Members <= 4);
2471}
2472
Chris Lattnera3c109b2010-07-29 02:16:43 +00002473ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002474 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002475 // Treat an enum type as its underlying type.
2476 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2477 Ty = EnumTy->getDecl()->getIntegerType();
2478
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002479 return (Ty->isPromotableIntegerType() ?
2480 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002481 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002482
Daniel Dunbar42025572009-09-14 21:54:03 +00002483 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002484 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002485 return ABIArgInfo::getIgnore();
2486
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002487 // Structures with either a non-trivial destructor or a non-trivial
2488 // copy constructor are always indirect.
2489 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2490 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2491
Bob Wilson194f06a2011-08-03 05:58:22 +00002492 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2493 // Homogeneous Aggregates need to be expanded.
2494 const Type *Base = 0;
2495 if (isHomogeneousAggregate(Ty, Base, getContext()))
2496 return ABIArgInfo::getExpand();
2497 }
2498
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002499 // Otherwise, pass by coercing to a structure of the appropriate size.
2500 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002501 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2502 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002503 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002504 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002505 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002506 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002507 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2508 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002509 } else {
2510 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2511 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002512 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002513
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002514 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002515 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002516 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002517}
2518
Chris Lattnera3c109b2010-07-29 02:16:43 +00002519static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002520 llvm::LLVMContext &VMContext) {
2521 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2522 // is called integer-like if its size is less than or equal to one word, and
2523 // the offset of each of its addressable sub-fields is zero.
2524
2525 uint64_t Size = Context.getTypeSize(Ty);
2526
2527 // Check that the type fits in a word.
2528 if (Size > 32)
2529 return false;
2530
2531 // FIXME: Handle vector types!
2532 if (Ty->isVectorType())
2533 return false;
2534
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002535 // Float types are never treated as "integer like".
2536 if (Ty->isRealFloatingType())
2537 return false;
2538
Daniel Dunbar98303b92009-09-13 08:03:58 +00002539 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002540 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002541 return true;
2542
Daniel Dunbar45815812010-02-01 23:31:26 +00002543 // Small complex integer types are "integer like".
2544 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2545 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002546
2547 // Single element and zero sized arrays should be allowed, by the definition
2548 // above, but they are not.
2549
2550 // Otherwise, it must be a record type.
2551 const RecordType *RT = Ty->getAs<RecordType>();
2552 if (!RT) return false;
2553
2554 // Ignore records with flexible arrays.
2555 const RecordDecl *RD = RT->getDecl();
2556 if (RD->hasFlexibleArrayMember())
2557 return false;
2558
2559 // Check that all sub-fields are at offset 0, and are themselves "integer
2560 // like".
2561 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2562
2563 bool HadField = false;
2564 unsigned idx = 0;
2565 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2566 i != e; ++i, ++idx) {
2567 const FieldDecl *FD = *i;
2568
Daniel Dunbar679855a2010-01-29 03:22:29 +00002569 // Bit-fields are not addressable, we only need to verify they are "integer
2570 // like". We still have to disallow a subsequent non-bitfield, for example:
2571 // struct { int : 0; int x }
2572 // is non-integer like according to gcc.
2573 if (FD->isBitField()) {
2574 if (!RD->isUnion())
2575 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002576
Daniel Dunbar679855a2010-01-29 03:22:29 +00002577 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2578 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002579
Daniel Dunbar679855a2010-01-29 03:22:29 +00002580 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002581 }
2582
Daniel Dunbar679855a2010-01-29 03:22:29 +00002583 // Check if this field is at offset 0.
2584 if (Layout.getFieldOffset(idx) != 0)
2585 return false;
2586
Daniel Dunbar98303b92009-09-13 08:03:58 +00002587 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2588 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002589
Daniel Dunbar679855a2010-01-29 03:22:29 +00002590 // Only allow at most one field in a structure. This doesn't match the
2591 // wording above, but follows gcc in situations with a field following an
2592 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002593 if (!RD->isUnion()) {
2594 if (HadField)
2595 return false;
2596
2597 HadField = true;
2598 }
2599 }
2600
2601 return true;
2602}
2603
Chris Lattnera3c109b2010-07-29 02:16:43 +00002604ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002605 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002606 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002607
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002608 // Large vector types should be returned via memory.
2609 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2610 return ABIArgInfo::getIndirect(0);
2611
John McCalld608cdb2010-08-22 10:59:02 +00002612 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002613 // Treat an enum type as its underlying type.
2614 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2615 RetTy = EnumTy->getDecl()->getIntegerType();
2616
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002617 return (RetTy->isPromotableIntegerType() ?
2618 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002619 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002620
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002621 // Structures with either a non-trivial destructor or a non-trivial
2622 // copy constructor are always indirect.
2623 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2624 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2625
Daniel Dunbar98303b92009-09-13 08:03:58 +00002626 // Are we following APCS?
2627 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002628 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002629 return ABIArgInfo::getIgnore();
2630
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002631 // Complex types are all returned as packed integers.
2632 //
2633 // FIXME: Consider using 2 x vector types if the back end handles them
2634 // correctly.
2635 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002636 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002637 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002638
Daniel Dunbar98303b92009-09-13 08:03:58 +00002639 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002640 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002641 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002642 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002643 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002644 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002645 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002646 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2647 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002648 }
2649
2650 // Otherwise return in memory.
2651 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002652 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002653
2654 // Otherwise this is an AAPCS variant.
2655
Chris Lattnera3c109b2010-07-29 02:16:43 +00002656 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002657 return ABIArgInfo::getIgnore();
2658
Daniel Dunbar98303b92009-09-13 08:03:58 +00002659 // Aggregates <= 4 bytes are returned in r0; other aggregates
2660 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002661 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002662 if (Size <= 32) {
2663 // Return in the smallest viable integer type.
2664 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002665 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002666 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002667 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2668 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002669 }
2670
Daniel Dunbar98303b92009-09-13 08:03:58 +00002671 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002672}
2673
2674llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002675 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002676 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2677 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002678
2679 CGBuilderTy &Builder = CGF.Builder;
2680 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2681 "ap");
2682 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002683 // Handle address alignment for type alignment > 32 bits
2684 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2685 if (TyAlign > 4) {
2686 assert((TyAlign & (TyAlign - 1)) == 0 &&
2687 "Alignment is not power of 2!");
2688 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2689 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2690 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2691 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2692 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002693 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002694 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002695 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2696
2697 uint64_t Offset =
2698 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2699 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002700 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002701 "ap.next");
2702 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2703
2704 return AddrTyped;
2705}
2706
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002707//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002708// PTX ABI Implementation
2709//===----------------------------------------------------------------------===//
2710
2711namespace {
2712
2713class PTXABIInfo : public ABIInfo {
2714public:
2715 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2716
2717 ABIArgInfo classifyReturnType(QualType RetTy) const;
2718 ABIArgInfo classifyArgumentType(QualType Ty) const;
2719
2720 virtual void computeInfo(CGFunctionInfo &FI) const;
2721 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2722 CodeGenFunction &CFG) const;
2723};
2724
2725class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2726public:
2727 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2728 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
2729};
2730
2731ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2732 if (RetTy->isVoidType())
2733 return ABIArgInfo::getIgnore();
2734 if (isAggregateTypeForABI(RetTy))
2735 return ABIArgInfo::getIndirect(0);
2736 return ABIArgInfo::getDirect();
2737}
2738
2739ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2740 if (isAggregateTypeForABI(Ty))
2741 return ABIArgInfo::getIndirect(0);
2742
2743 return ABIArgInfo::getDirect();
2744}
2745
2746void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2747 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2748 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2749 it != ie; ++it)
2750 it->info = classifyArgumentType(it->type);
2751
2752 // Always honor user-specified calling convention.
2753 if (FI.getCallingConvention() != llvm::CallingConv::C)
2754 return;
2755
2756 // Calling convention as default by an ABI.
2757 llvm::CallingConv::ID DefaultCC;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002758 StringRef Env = getContext().Target.getTriple().getEnvironmentName();
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002759 if (Env == "device")
2760 DefaultCC = llvm::CallingConv::PTX_Device;
2761 else
2762 DefaultCC = llvm::CallingConv::PTX_Kernel;
2763
2764 FI.setEffectiveCallingConvention(DefaultCC);
2765}
2766
2767llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2768 CodeGenFunction &CFG) const {
2769 llvm_unreachable("PTX does not support varargs");
2770 return 0;
2771}
2772
2773}
2774
2775//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002776// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002777//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002778
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002779namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002780
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002781class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002782public:
2783 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2784
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002785 bool isPromotableIntegerType(QualType Ty) const;
2786
Chris Lattnera3c109b2010-07-29 02:16:43 +00002787 ABIArgInfo classifyReturnType(QualType RetTy) const;
2788 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002789
Chris Lattneree5dcd02010-07-29 02:31:05 +00002790 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002791 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002792 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2793 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002794 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002795 }
2796
2797 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2798 CodeGenFunction &CGF) const;
2799};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002800
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002801class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2802public:
Chris Lattnerea044322010-07-29 02:01:43 +00002803 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2804 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002805};
2806
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002807}
2808
2809bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2810 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002811 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002812 switch (BT->getKind()) {
2813 case BuiltinType::Bool:
2814 case BuiltinType::Char_S:
2815 case BuiltinType::Char_U:
2816 case BuiltinType::SChar:
2817 case BuiltinType::UChar:
2818 case BuiltinType::Short:
2819 case BuiltinType::UShort:
2820 case BuiltinType::Int:
2821 case BuiltinType::UInt:
2822 return true;
2823 default:
2824 return false;
2825 }
2826 return false;
2827}
2828
2829llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2830 CodeGenFunction &CGF) const {
2831 // FIXME: Implement
2832 return 0;
2833}
2834
2835
Chris Lattnera3c109b2010-07-29 02:16:43 +00002836ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2837 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002838 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002839 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002840 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002841
2842 return (isPromotableIntegerType(RetTy) ?
2843 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002844}
2845
Chris Lattnera3c109b2010-07-29 02:16:43 +00002846ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002847 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002848 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002849
2850 return (isPromotableIntegerType(Ty) ?
2851 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002852}
2853
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002854//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002855// MBlaze ABI Implementation
2856//===----------------------------------------------------------------------===//
2857
2858namespace {
2859
2860class MBlazeABIInfo : public ABIInfo {
2861public:
2862 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2863
2864 bool isPromotableIntegerType(QualType Ty) const;
2865
2866 ABIArgInfo classifyReturnType(QualType RetTy) const;
2867 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2868
2869 virtual void computeInfo(CGFunctionInfo &FI) const {
2870 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2871 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2872 it != ie; ++it)
2873 it->info = classifyArgumentType(it->type);
2874 }
2875
2876 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2877 CodeGenFunction &CGF) const;
2878};
2879
2880class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2881public:
2882 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2883 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2884 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2885 CodeGen::CodeGenModule &M) const;
2886};
2887
2888}
2889
2890bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2891 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2892 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2893 switch (BT->getKind()) {
2894 case BuiltinType::Bool:
2895 case BuiltinType::Char_S:
2896 case BuiltinType::Char_U:
2897 case BuiltinType::SChar:
2898 case BuiltinType::UChar:
2899 case BuiltinType::Short:
2900 case BuiltinType::UShort:
2901 return true;
2902 default:
2903 return false;
2904 }
2905 return false;
2906}
2907
2908llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2909 CodeGenFunction &CGF) const {
2910 // FIXME: Implement
2911 return 0;
2912}
2913
2914
2915ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2916 if (RetTy->isVoidType())
2917 return ABIArgInfo::getIgnore();
2918 if (isAggregateTypeForABI(RetTy))
2919 return ABIArgInfo::getIndirect(0);
2920
2921 return (isPromotableIntegerType(RetTy) ?
2922 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2923}
2924
2925ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2926 if (isAggregateTypeForABI(Ty))
2927 return ABIArgInfo::getIndirect(0);
2928
2929 return (isPromotableIntegerType(Ty) ?
2930 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2931}
2932
2933void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2934 llvm::GlobalValue *GV,
2935 CodeGen::CodeGenModule &M)
2936 const {
2937 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2938 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002939
Wesley Peck276fdf42010-12-19 19:57:51 +00002940 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2941 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2942 CC = llvm::CallingConv::MBLAZE_INTR;
2943 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2944 CC = llvm::CallingConv::MBLAZE_SVOL;
2945
2946 if (CC != llvm::CallingConv::C) {
2947 // Handle 'interrupt_handler' attribute:
2948 llvm::Function *F = cast<llvm::Function>(GV);
2949
2950 // Step 1: Set ISR calling convention.
2951 F->setCallingConv(CC);
2952
2953 // Step 2: Add attributes goodness.
2954 F->addFnAttr(llvm::Attribute::NoInline);
2955 }
2956
2957 // Step 3: Emit _interrupt_handler alias.
2958 if (CC == llvm::CallingConv::MBLAZE_INTR)
2959 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2960 "_interrupt_handler", GV, &M.getModule());
2961}
2962
2963
2964//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002965// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002966//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002967
2968namespace {
2969
2970class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2971public:
Chris Lattnerea044322010-07-29 02:01:43 +00002972 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2973 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002974 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2975 CodeGen::CodeGenModule &M) const;
2976};
2977
2978}
2979
2980void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2981 llvm::GlobalValue *GV,
2982 CodeGen::CodeGenModule &M) const {
2983 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2984 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2985 // Handle 'interrupt' attribute:
2986 llvm::Function *F = cast<llvm::Function>(GV);
2987
2988 // Step 1: Set ISR calling convention.
2989 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2990
2991 // Step 2: Add attributes goodness.
2992 F->addFnAttr(llvm::Attribute::NoInline);
2993
2994 // Step 3: Emit ISR vector alias.
2995 unsigned Num = attr->getNumber() + 0xffe0;
2996 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002997 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002998 GV, &M.getModule());
2999 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003000 }
3001}
3002
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003003//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003004// MIPS ABI Implementation. This works for both little-endian and
3005// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003006//===----------------------------------------------------------------------===//
3007
John McCallaeeb7012010-05-27 06:19:26 +00003008namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003009class MipsABIInfo : public ABIInfo {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003010 static const unsigned MinABIStackAlignInBytes = 4;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003011public:
3012 MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3013
3014 ABIArgInfo classifyReturnType(QualType RetTy) const;
3015 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3016 virtual void computeInfo(CGFunctionInfo &FI) const;
3017 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3018 CodeGenFunction &CGF) const;
3019};
3020
Akira Hatanaka3827e422011-08-12 01:43:14 +00003021const unsigned MipsABIInfo::MinABIStackAlignInBytes;
3022
John McCallaeeb7012010-05-27 06:19:26 +00003023class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
3024public:
Chris Lattnerea044322010-07-29 02:01:43 +00003025 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003026 : TargetCodeGenInfo(new MipsABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00003027
3028 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3029 return 29;
3030 }
3031
3032 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003033 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003034
3035 unsigned getSizeOfUnwindException() const {
3036 return 24;
3037 }
John McCallaeeb7012010-05-27 06:19:26 +00003038};
3039}
3040
Akira Hatanaka619e8872011-06-02 00:09:17 +00003041ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3042 if (isAggregateTypeForABI(Ty)) {
3043 // Ignore empty aggregates.
3044 if (getContext().getTypeSize(Ty) == 0)
3045 return ABIArgInfo::getIgnore();
3046
Akira Hatanaka511949b2011-08-01 18:09:58 +00003047 // Records with non trivial destructors/constructors should not be passed
3048 // by value.
3049 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3050 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3051
Akira Hatanaka619e8872011-06-02 00:09:17 +00003052 return ABIArgInfo::getIndirect(0);
3053 }
3054
3055 // Treat an enum type as its underlying type.
3056 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3057 Ty = EnumTy->getDecl()->getIntegerType();
3058
3059 return (Ty->isPromotableIntegerType() ?
3060 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3061}
3062
3063ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3064 if (RetTy->isVoidType())
3065 return ABIArgInfo::getIgnore();
3066
3067 if (isAggregateTypeForABI(RetTy)) {
3068 if (RetTy->isAnyComplexType())
3069 return ABIArgInfo::getDirect();
3070
3071 return ABIArgInfo::getIndirect(0);
3072 }
3073
3074 // Treat an enum type as its underlying type.
3075 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3076 RetTy = EnumTy->getDecl()->getIntegerType();
3077
3078 return (RetTy->isPromotableIntegerType() ?
3079 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3080}
3081
3082void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3083 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3084 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3085 it != ie; ++it)
3086 it->info = classifyArgumentType(it->type);
3087}
3088
3089llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3090 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003091 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3092 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3093
3094 CGBuilderTy &Builder = CGF.Builder;
3095 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3096 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3097 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3098 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3099 llvm::Value *AddrTyped;
3100
3101 if (TypeAlign > MinABIStackAlignInBytes) {
3102 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3103 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3104 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3105 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3106 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3107 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3108 }
3109 else
3110 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3111
3112 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka7b0a0382011-08-12 02:30:14 +00003113 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003114 uint64_t Offset =
3115 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3116 llvm::Value *NextAddr =
3117 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3118 "ap.next");
3119 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3120
3121 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003122}
3123
John McCallaeeb7012010-05-27 06:19:26 +00003124bool
3125MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3126 llvm::Value *Address) const {
3127 // This information comes from gcc's implementation, which seems to
3128 // as canonical as it gets.
3129
3130 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3131 llvm::LLVMContext &Context = CGF.getLLVMContext();
3132
3133 // Everything on MIPS is 4 bytes. Double-precision FP registers
3134 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003135 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003136 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3137
3138 // 0-31 are the general purpose registers, $0 - $31.
3139 // 32-63 are the floating-point registers, $f0 - $f31.
3140 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3141 // 66 is the (notional, I think) register for signal-handler return.
3142 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3143
3144 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3145 // They are one bit wide and ignored here.
3146
3147 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3148 // (coprocessor 1 is the FP unit)
3149 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3150 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3151 // 176-181 are the DSP accumulator registers.
3152 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3153
3154 return false;
3155}
3156
3157
Chris Lattnerea044322010-07-29 02:01:43 +00003158const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003159 if (TheTargetCodeGenInfo)
3160 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003161
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003162 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
3163 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003164
Chris Lattner9c254f02010-06-29 06:01:59 +00003165 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003166 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003167 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003168 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003169
John McCallaeeb7012010-05-27 06:19:26 +00003170 case llvm::Triple::mips:
3171 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00003172 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00003173
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003174 case llvm::Triple::arm:
3175 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003176 {
3177 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003178
Sandeep Patel34c1af82011-04-05 00:23:47 +00003179 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
3180 Kind = ARMABIInfo::APCS;
3181 else if (CodeGenOpts.FloatABI == "hard")
3182 Kind = ARMABIInfo::AAPCS_VFP;
3183
3184 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3185 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003186
John McCallec853ba2010-03-11 00:10:12 +00003187 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003188 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003189
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003190 case llvm::Triple::ptx32:
3191 case llvm::Triple::ptx64:
3192 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3193
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003194 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00003195 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003196
Wesley Peck276fdf42010-12-19 19:57:51 +00003197 case llvm::Triple::mblaze:
3198 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3199
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003200 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003201 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003202
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003203 case llvm::Triple::x86: {
3204 bool DisableMMX = strcmp(getContext().Target.getABI(), "no-mmx") == 0;
3205
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003206 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003207 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003208 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003209
3210 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003211 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003212 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003213 case llvm::Triple::AuroraUX:
3214 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003215 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003216 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003217 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003218 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003219 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003220
3221 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003222 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003223 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003224 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003225 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003226
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003227 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003228 switch (Triple.getOS()) {
3229 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003230 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003231 case llvm::Triple::Cygwin:
3232 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3233 default:
3234 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3235 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003236 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003237}