blob: 97f9b718e0f0c4f3411a89d3f1f6ffc7a29606de [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
John McCall01f151e2011-09-21 08:08:30 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(CallingConv CC) const {
102 // The following conventions are known to require this to be false:
103 // x86_stdcall
104 // MIPS
105 // For everything else, we just prefer false unless we opt out.
106 return false;
107}
108
Daniel Dunbar98303b92009-09-13 08:03:58 +0000109static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000110
111/// isEmptyField - Return true iff a the field is "empty", that is it
112/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000113static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
114 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000115 if (FD->isUnnamedBitfield())
116 return true;
117
118 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000119
Daniel Dunbar98303b92009-09-13 08:03:58 +0000120 // Constant arrays of empty records count as empty, strip them off.
121 if (AllowArrays)
122 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
123 FT = AT->getElementType();
124
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000125 const RecordType *RT = FT->getAs<RecordType>();
126 if (!RT)
127 return false;
128
129 // C++ record fields are never empty, at least in the Itanium ABI.
130 //
131 // FIXME: We should use a predicate for whether this behavior is true in the
132 // current ABI.
133 if (isa<CXXRecordDecl>(RT->getDecl()))
134 return false;
135
Daniel Dunbar98303b92009-09-13 08:03:58 +0000136 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000137}
138
139/// isEmptyRecord - Return true iff a structure contains only empty
140/// fields. Note that a structure with a flexible array member is not
141/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000142static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000143 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000144 if (!RT)
145 return 0;
146 const RecordDecl *RD = RT->getDecl();
147 if (RD->hasFlexibleArrayMember())
148 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000149
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000150 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000151 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000152 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
153 e = CXXRD->bases_end(); i != e; ++i)
154 if (!isEmptyRecord(Context, i->getType(), true))
155 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000157 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
158 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000159 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000160 return false;
161 return true;
162}
163
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000164/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
165/// a non-trivial destructor or a non-trivial copy constructor.
166static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
167 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
168 if (!RD)
169 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000170
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000171 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
172}
173
174/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
175/// a record type with either a non-trivial destructor or a non-trivial copy
176/// constructor.
177static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
178 const RecordType *RT = T->getAs<RecordType>();
179 if (!RT)
180 return false;
181
182 return hasNonTrivialDestructorOrCopyConstructor(RT);
183}
184
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000185/// isSingleElementStruct - Determine if a structure is a "single
186/// element struct", i.e. it has exactly one non-empty field or
187/// exactly one field which is itself a single element
188/// struct. Structures with flexible array members are never
189/// considered single element structs.
190///
191/// \return The field declaration for the single non-empty field, if
192/// it exists.
193static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
194 const RecordType *RT = T->getAsStructureType();
195 if (!RT)
196 return 0;
197
198 const RecordDecl *RD = RT->getDecl();
199 if (RD->hasFlexibleArrayMember())
200 return 0;
201
202 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000203
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000204 // If this is a C++ record, check the bases first.
205 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
206 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
207 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000208 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000209 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000210 continue;
211
212 // If we already found an element then this isn't a single-element struct.
213 if (Found)
214 return 0;
215
216 // If this is non-empty and not a single element struct, the composite
217 // cannot be a single element struct.
218 Found = isSingleElementStruct(i->getType(), Context);
219 if (!Found)
220 return 0;
221 }
222 }
223
224 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000225 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
226 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000227 const FieldDecl *FD = *i;
228 QualType FT = FD->getType();
229
230 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000231 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000232 continue;
233
234 // If we already found an element then this isn't a single-element
235 // struct.
236 if (Found)
237 return 0;
238
239 // Treat single element arrays as the element.
240 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
241 if (AT->getSize().getZExtValue() != 1)
242 break;
243 FT = AT->getElementType();
244 }
245
John McCalld608cdb2010-08-22 10:59:02 +0000246 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000247 Found = FT.getTypePtr();
248 } else {
249 Found = isSingleElementStruct(FT, Context);
250 if (!Found)
251 return 0;
252 }
253 }
254
255 return Found;
256}
257
258static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000259 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000260 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
261 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000262 return false;
263
264 uint64_t Size = Context.getTypeSize(Ty);
265 return Size == 32 || Size == 64;
266}
267
Daniel Dunbar53012f42009-11-09 01:33:53 +0000268/// canExpandIndirectArgument - Test whether an argument type which is to be
269/// passed indirectly (on the stack) would have the equivalent layout if it was
270/// expanded into separate arguments. If so, we prefer to do the latter to avoid
271/// inhibiting optimizations.
272///
273// FIXME: This predicate is missing many cases, currently it just follows
274// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
275// should probably make this smarter, or better yet make the LLVM backend
276// capable of handling it.
277static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
278 // We can only expand structure types.
279 const RecordType *RT = Ty->getAs<RecordType>();
280 if (!RT)
281 return false;
282
283 // We can only expand (C) structures.
284 //
285 // FIXME: This needs to be generalized to handle classes as well.
286 const RecordDecl *RD = RT->getDecl();
287 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
288 return false;
289
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000290 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
291 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000292 const FieldDecl *FD = *i;
293
294 if (!is32Or64BitBasicType(FD->getType(), Context))
295 return false;
296
297 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
298 // how to expand them yet, and the predicate for telling if a bitfield still
299 // counts as "basic" is more complicated than what we were doing previously.
300 if (FD->isBitField())
301 return false;
302 }
303
304 return true;
305}
306
307namespace {
308/// DefaultABIInfo - The default implementation for ABI specific
309/// details. This implementation provides information which results in
310/// self-consistent and sensible LLVM IR generation, but does not
311/// conform to any particular ABI.
312class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000313public:
314 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000315
Chris Lattnera3c109b2010-07-29 02:16:43 +0000316 ABIArgInfo classifyReturnType(QualType RetTy) const;
317 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000318
Chris Lattneree5dcd02010-07-29 02:31:05 +0000319 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000320 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000321 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
322 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000323 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000324 }
325
326 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
327 CodeGenFunction &CGF) const;
328};
329
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000330class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
331public:
Chris Lattnerea044322010-07-29 02:01:43 +0000332 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
333 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000334};
335
336llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
337 CodeGenFunction &CGF) const {
338 return 0;
339}
340
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000342 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000343 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000344
Chris Lattnera14db752010-03-11 18:19:55 +0000345 // Treat an enum type as its underlying type.
346 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
347 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000348
Chris Lattnera14db752010-03-11 18:19:55 +0000349 return (Ty->isPromotableIntegerType() ?
350 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000351}
352
Bob Wilson0024f942011-01-10 23:54:17 +0000353ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
354 if (RetTy->isVoidType())
355 return ABIArgInfo::getIgnore();
356
357 if (isAggregateTypeForABI(RetTy))
358 return ABIArgInfo::getIndirect(0);
359
360 // Treat an enum type as its underlying type.
361 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
362 RetTy = EnumTy->getDecl()->getIntegerType();
363
364 return (RetTy->isPromotableIntegerType() ?
365 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
366}
367
Bill Wendlingbb465d72010-10-18 03:41:31 +0000368/// UseX86_MMXType - Return true if this is an MMX type that should use the special
369/// x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000370bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000371 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
372 // special x86_mmx type.
373 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
374 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
375 IRType->getScalarSizeInBits() != 64;
376}
377
Jay Foadef6de3d2011-07-11 09:56:20 +0000378static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000379 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000380 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000381 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000382 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
383 return Ty;
384}
385
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000386//===----------------------------------------------------------------------===//
387// X86-32 ABI Implementation
388//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000389
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000390/// X86_32ABIInfo - The X86-32 ABI information.
391class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000392 static const unsigned MinABIStackAlignInBytes = 4;
393
David Chisnall1e4249c2009-08-17 23:08:21 +0000394 bool IsDarwinVectorABI;
395 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000396 bool IsMMXDisabled;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000397
398 static bool isRegisterSize(unsigned Size) {
399 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
400 }
401
402 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
403
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000404 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
405 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000406 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000407
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000408 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000409 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000410
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411public:
Chris Lattnerea044322010-07-29 02:01:43 +0000412
Chris Lattnera3c109b2010-07-29 02:16:43 +0000413 ABIArgInfo classifyReturnType(QualType RetTy) const;
414 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000415
Chris Lattneree5dcd02010-07-29 02:31:05 +0000416 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000417 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000418 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
419 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000420 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000421 }
422
423 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
424 CodeGenFunction &CGF) const;
425
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000426 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
427 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
428 IsMMXDisabled(m) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000429};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000430
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000431class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
432public:
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000433 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
434 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000435
436 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
437 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000438
439 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
440 // Darwin uses different dwarf register numbers for EH.
441 if (CGM.isTargetDarwin()) return 5;
442
443 return 4;
444 }
445
446 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
447 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000448
Jay Foadef6de3d2011-07-11 09:56:20 +0000449 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000450 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000451 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000452 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
453 }
454
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000455};
456
457}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000458
459/// shouldReturnTypeInRegister - Determine if the given type should be
460/// passed in a register (for the Darwin ABI).
461bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
462 ASTContext &Context) {
463 uint64_t Size = Context.getTypeSize(Ty);
464
465 // Type must be register sized.
466 if (!isRegisterSize(Size))
467 return false;
468
469 if (Ty->isVectorType()) {
470 // 64- and 128- bit vectors inside structures are not returned in
471 // registers.
472 if (Size == 64 || Size == 128)
473 return false;
474
475 return true;
476 }
477
Daniel Dunbar77115232010-05-15 00:00:30 +0000478 // If this is a builtin, pointer, enum, complex type, member pointer, or
479 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000480 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000481 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000482 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000483 return true;
484
485 // Arrays are treated like records.
486 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
487 return shouldReturnTypeInRegister(AT->getElementType(), Context);
488
489 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000490 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000491 if (!RT) return false;
492
Anders Carlssona8874232010-01-27 03:25:19 +0000493 // FIXME: Traverse bases here too.
494
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000495 // Structure types are passed in register if all fields would be
496 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000497 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
498 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000499 const FieldDecl *FD = *i;
500
501 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000502 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000503 continue;
504
505 // Check fields recursively.
506 if (!shouldReturnTypeInRegister(FD->getType(), Context))
507 return false;
508 }
509
510 return true;
511}
512
Chris Lattnera3c109b2010-07-29 02:16:43 +0000513ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
514 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000516
Chris Lattnera3c109b2010-07-29 02:16:43 +0000517 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000518 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000519 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000520 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000521
522 // 128-bit vectors are a special case; they are returned in
523 // registers and we need to make sure to pick a type the LLVM
524 // backend will like.
525 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000526 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000527 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000528
529 // Always return in register if it fits in a general purpose
530 // register, or if it is 64 bits and has a single element.
531 if ((Size == 8 || Size == 16 || Size == 32) ||
532 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000533 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000534 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535
536 return ABIArgInfo::getIndirect(0);
537 }
538
539 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000540 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000541
John McCalld608cdb2010-08-22 10:59:02 +0000542 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000543 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000544 // Structures with either a non-trivial destructor or a non-trivial
545 // copy constructor are always indirect.
546 if (hasNonTrivialDestructorOrCopyConstructor(RT))
547 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000548
Anders Carlsson40092972009-10-20 22:07:59 +0000549 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000550 if (RT->getDecl()->hasFlexibleArrayMember())
551 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000552 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000553
David Chisnall1e4249c2009-08-17 23:08:21 +0000554 // If specified, structs and unions are always indirect.
555 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000556 return ABIArgInfo::getIndirect(0);
557
558 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000559 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000560 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000561 if (BT->isIntegerType()) {
562 // We need to use the size of the structure, padding
563 // bit-fields can adjust that to be larger than the single
564 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000565 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000566 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000567 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
568 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000569
Chris Lattnera3c109b2010-07-29 02:16:43 +0000570 if (BT->getKind() == BuiltinType::Float) {
571 assert(getContext().getTypeSize(RetTy) ==
572 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000573 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000574 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000575 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000576
Chris Lattnera3c109b2010-07-29 02:16:43 +0000577 if (BT->getKind() == BuiltinType::Double) {
578 assert(getContext().getTypeSize(RetTy) ==
579 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000580 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000581 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000582 }
583 } else if (SeltTy->isPointerType()) {
584 // FIXME: It would be really nice if this could come out as the proper
585 // pointer type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000586 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000587 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000588 } else if (SeltTy->isVectorType()) {
589 // 64- and 128-bit vectors are never returned in a
590 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000591 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000592 if (Size == 64 || Size == 128)
593 return ABIArgInfo::getIndirect(0);
594
Chris Lattnera3c109b2010-07-29 02:16:43 +0000595 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000596 }
597 }
598
599 // Small structures which are register sized are generally returned
600 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000601 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
602 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000603 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000604 }
605
606 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000608
Chris Lattnera3c109b2010-07-29 02:16:43 +0000609 // Treat an enum type as its underlying type.
610 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
611 RetTy = EnumTy->getDecl()->getIntegerType();
612
613 return (RetTy->isPromotableIntegerType() ?
614 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000615}
616
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000617static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
618 const RecordType *RT = Ty->getAs<RecordType>();
619 if (!RT)
620 return 0;
621 const RecordDecl *RD = RT->getDecl();
622
623 // If this is a C++ record, check the bases first.
624 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
625 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
626 e = CXXRD->bases_end(); i != e; ++i)
627 if (!isRecordWithSSEVectorType(Context, i->getType()))
628 return false;
629
630 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
631 i != e; ++i) {
632 QualType FT = i->getType();
633
634 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
635 return true;
636
637 if (isRecordWithSSEVectorType(Context, FT))
638 return true;
639 }
640
641 return false;
642}
643
Daniel Dunbare59d8582010-09-16 20:42:06 +0000644unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
645 unsigned Align) const {
646 // Otherwise, if the alignment is less than or equal to the minimum ABI
647 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000648 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000649 return 0; // Use default alignment.
650
651 // On non-Darwin, the stack type alignment is always 4.
652 if (!IsDarwinVectorABI) {
653 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000654 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000655 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000656
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000657 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
658 if (isRecordWithSSEVectorType(getContext(), Ty))
659 return 16;
660
661 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000662}
663
Chris Lattnera3c109b2010-07-29 02:16:43 +0000664ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000665 if (!ByVal)
666 return ABIArgInfo::getIndirect(0, false);
667
Daniel Dunbare59d8582010-09-16 20:42:06 +0000668 // Compute the byval alignment.
669 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
670 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
671 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000672 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000673
674 // If the stack alignment is less than the type alignment, realign the
675 // argument.
676 if (StackAlign < TypeAlign)
677 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
678 /*Realign=*/true);
679
680 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000681}
682
Chris Lattnera3c109b2010-07-29 02:16:43 +0000683ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000684 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000685 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000686 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000687 if (const RecordType *RT = Ty->getAs<RecordType>()) {
688 // Structures with either a non-trivial destructor or a non-trivial
689 // copy constructor are always indirect.
690 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000691 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000692
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000693 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000694 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000695 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000696
697 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000698 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 return ABIArgInfo::getIgnore();
700
Daniel Dunbar53012f42009-11-09 01:33:53 +0000701 // Expand small (<= 128-bit) record types when we know that the stack layout
702 // of those arguments will match the struct. This is important because the
703 // LLVM backend isn't smart enough to remove byval, which inhibits many
704 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000705 if (getContext().getTypeSize(Ty) <= 4*32 &&
706 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000707 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000708
Chris Lattnera3c109b2010-07-29 02:16:43 +0000709 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000710 }
711
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000712 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000713 // On Darwin, some vectors are passed in memory, we handle this by passing
714 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000715 if (IsDarwinVectorABI) {
716 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000717 if ((Size == 8 || Size == 16 || Size == 32) ||
718 (Size == 64 && VT->getNumElements() == 1))
719 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
720 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000721 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000722
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000723 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000724 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000725 if (IsMMXDisabled)
726 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
727 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000728 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
729 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
730 return AAI;
731 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000732
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000733 return ABIArgInfo::getDirect();
734 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000735
736
Chris Lattnera3c109b2010-07-29 02:16:43 +0000737 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
738 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000739
Chris Lattnera3c109b2010-07-29 02:16:43 +0000740 return (Ty->isPromotableIntegerType() ?
741 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000742}
743
744llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
745 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000746 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
747 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000748
749 CGBuilderTy &Builder = CGF.Builder;
750 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
751 "ap");
752 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
753 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000754 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000755 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
756
757 uint64_t Offset =
758 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
759 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000760 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000761 "ap.next");
762 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
763
764 return AddrTyped;
765}
766
Charles Davis74f72932010-02-13 15:54:06 +0000767void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
768 llvm::GlobalValue *GV,
769 CodeGen::CodeGenModule &CGM) const {
770 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
771 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
772 // Get the LLVM function.
773 llvm::Function *Fn = cast<llvm::Function>(GV);
774
775 // Now add the 'alignstack' attribute with a value of 16.
776 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
777 }
778 }
779}
780
John McCall6374c332010-03-06 00:35:14 +0000781bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
782 CodeGen::CodeGenFunction &CGF,
783 llvm::Value *Address) const {
784 CodeGen::CGBuilderTy &Builder = CGF.Builder;
785 llvm::LLVMContext &Context = CGF.getLLVMContext();
786
Chris Lattner2acc6e32011-07-18 04:24:23 +0000787 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000788 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000789
John McCall6374c332010-03-06 00:35:14 +0000790 // 0-7 are the eight integer registers; the order is different
791 // on Darwin (for EH), but the range is the same.
792 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000793 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000794
795 if (CGF.CGM.isTargetDarwin()) {
796 // 12-16 are st(0..4). Not sure why we stop at 4.
797 // These have size 16, which is sizeof(long double) on
798 // platforms with 8-byte alignment for that type.
799 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000800 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000801
John McCall6374c332010-03-06 00:35:14 +0000802 } else {
803 // 9 is %eflags, which doesn't get a size on Darwin for some
804 // reason.
805 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
806
807 // 11-16 are st(0..5). Not sure why we stop at 5.
808 // These have size 12, which is sizeof(long double) on
809 // platforms with 4-byte alignment for that type.
810 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000811 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
812 }
John McCall6374c332010-03-06 00:35:14 +0000813
814 return false;
815}
816
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000817//===----------------------------------------------------------------------===//
818// X86-64 ABI Implementation
819//===----------------------------------------------------------------------===//
820
821
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000822namespace {
823/// X86_64ABIInfo - The X86_64 ABI information.
824class X86_64ABIInfo : public ABIInfo {
825 enum Class {
826 Integer = 0,
827 SSE,
828 SSEUp,
829 X87,
830 X87Up,
831 ComplexX87,
832 NoClass,
833 Memory
834 };
835
836 /// merge - Implement the X86_64 ABI merging algorithm.
837 ///
838 /// Merge an accumulating classification \arg Accum with a field
839 /// classification \arg Field.
840 ///
841 /// \param Accum - The accumulating classification. This should
842 /// always be either NoClass or the result of a previous merge
843 /// call. In addition, this should never be Memory (the caller
844 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000845 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000846
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000847 /// postMerge - Implement the X86_64 ABI post merging algorithm.
848 ///
849 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
850 /// final MEMORY or SSE classes when necessary.
851 ///
852 /// \param AggregateSize - The size of the current aggregate in
853 /// the classification process.
854 ///
855 /// \param Lo - The classification for the parts of the type
856 /// residing in the low word of the containing object.
857 ///
858 /// \param Hi - The classification for the parts of the type
859 /// residing in the higher words of the containing object.
860 ///
861 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
862
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000863 /// classify - Determine the x86_64 register classes in which the
864 /// given type T should be passed.
865 ///
866 /// \param Lo - The classification for the parts of the type
867 /// residing in the low word of the containing object.
868 ///
869 /// \param Hi - The classification for the parts of the type
870 /// residing in the high word of the containing object.
871 ///
872 /// \param OffsetBase - The bit offset of this type in the
873 /// containing object. Some parameters are classified different
874 /// depending on whether they straddle an eightbyte boundary.
875 ///
876 /// If a word is unused its result will be NoClass; if a type should
877 /// be passed in Memory then at least the classification of \arg Lo
878 /// will be Memory.
879 ///
880 /// The \arg Lo class will be NoClass iff the argument is ignored.
881 ///
882 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
883 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000884 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000885
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000886 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000887 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
888 unsigned IROffset, QualType SourceTy,
889 unsigned SourceOffset) const;
890 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
891 unsigned IROffset, QualType SourceTy,
892 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000893
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000894 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000895 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000896 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000897
898 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000899 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000900 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000901
Chris Lattnera3c109b2010-07-29 02:16:43 +0000902 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000903
Bill Wendlingbb465d72010-10-18 03:41:31 +0000904 ABIArgInfo classifyArgumentType(QualType Ty,
905 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000906 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000907
John McCall67a57732011-04-21 01:20:55 +0000908 /// The 0.98 ABI revision clarified a lot of ambiguities,
909 /// unfortunately in ways that were not always consistent with
910 /// certain previous compilers. In particular, platforms which
911 /// required strict binary compatibility with older versions of GCC
912 /// may need to exempt themselves.
913 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000914 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000915 }
916
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000917public:
Chris Lattnerea044322010-07-29 02:01:43 +0000918 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000919
Chris Lattneree5dcd02010-07-29 02:31:05 +0000920 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000921
922 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
923 CodeGenFunction &CGF) const;
924};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000925
Chris Lattnerf13721d2010-08-31 16:44:54 +0000926/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000927class WinX86_64ABIInfo : public ABIInfo {
928
929 ABIArgInfo classify(QualType Ty) const;
930
Chris Lattnerf13721d2010-08-31 16:44:54 +0000931public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000932 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
933
934 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000935
936 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
937 CodeGenFunction &CGF) const;
938};
939
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000940class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
941public:
Chris Lattnerea044322010-07-29 02:01:43 +0000942 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
943 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000944
945 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
946 return 7;
947 }
948
949 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
950 llvm::Value *Address) const {
951 CodeGen::CGBuilderTy &Builder = CGF.Builder;
952 llvm::LLVMContext &Context = CGF.getLLVMContext();
953
Chris Lattner2acc6e32011-07-18 04:24:23 +0000954 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000955 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000956
John McCallaeeb7012010-05-27 06:19:26 +0000957 // 0-15 are the 16 integer registers.
958 // 16 is %rip.
959 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000960
961 return false;
962 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000963
Jay Foadef6de3d2011-07-11 09:56:20 +0000964 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000965 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000966 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000967 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
968 }
969
John McCall01f151e2011-09-21 08:08:30 +0000970 bool isNoProtoCallVariadic(CallingConv CC) const {
971 // The default CC on x86-64 sets %al to the number of SSA
972 // registers used, and GCC sets this when calling an unprototyped
973 // function, so we override the default behavior.
974 if (CC == CC_Default || CC == CC_C) return true;
975
976 return TargetCodeGenInfo::isNoProtoCallVariadic(CC);
977 }
978
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000979};
980
Chris Lattnerf13721d2010-08-31 16:44:54 +0000981class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
982public:
983 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
984 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
985
986 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
987 return 7;
988 }
989
990 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
991 llvm::Value *Address) const {
992 CodeGen::CGBuilderTy &Builder = CGF.Builder;
993 llvm::LLVMContext &Context = CGF.getLLVMContext();
994
Chris Lattner2acc6e32011-07-18 04:24:23 +0000995 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattnerf13721d2010-08-31 16:44:54 +0000996 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000997
Chris Lattnerf13721d2010-08-31 16:44:54 +0000998 // 0-15 are the 16 integer registers.
999 // 16 is %rip.
1000 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1001
1002 return false;
1003 }
1004};
1005
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001006}
1007
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001008void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1009 Class &Hi) const {
1010 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1011 //
1012 // (a) If one of the classes is Memory, the whole argument is passed in
1013 // memory.
1014 //
1015 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1016 // memory.
1017 //
1018 // (c) If the size of the aggregate exceeds two eightbytes and the first
1019 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1020 // argument is passed in memory. NOTE: This is necessary to keep the
1021 // ABI working for processors that don't support the __m256 type.
1022 //
1023 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1024 //
1025 // Some of these are enforced by the merging logic. Others can arise
1026 // only with unions; for example:
1027 // union { _Complex double; unsigned; }
1028 //
1029 // Note that clauses (b) and (c) were added in 0.98.
1030 //
1031 if (Hi == Memory)
1032 Lo = Memory;
1033 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1034 Lo = Memory;
1035 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1036 Lo = Memory;
1037 if (Hi == SSEUp && Lo != SSE)
1038 Hi = SSE;
1039}
1040
Chris Lattner1090a9b2010-06-28 21:43:59 +00001041X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1043 // classified recursively so that always two fields are
1044 // considered. The resulting class is calculated according to
1045 // the classes of the fields in the eightbyte:
1046 //
1047 // (a) If both classes are equal, this is the resulting class.
1048 //
1049 // (b) If one of the classes is NO_CLASS, the resulting class is
1050 // the other class.
1051 //
1052 // (c) If one of the classes is MEMORY, the result is the MEMORY
1053 // class.
1054 //
1055 // (d) If one of the classes is INTEGER, the result is the
1056 // INTEGER.
1057 //
1058 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1059 // MEMORY is used as class.
1060 //
1061 // (f) Otherwise class SSE is used.
1062
1063 // Accum should never be memory (we should have returned) or
1064 // ComplexX87 (because this cannot be passed in a structure).
1065 assert((Accum != Memory && Accum != ComplexX87) &&
1066 "Invalid accumulated classification during merge.");
1067 if (Accum == Field || Field == NoClass)
1068 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001069 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001070 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001071 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001072 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001073 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001074 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001075 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1076 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001077 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001078 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001079}
1080
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001081void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001082 Class &Lo, Class &Hi) const {
1083 // FIXME: This code can be simplified by introducing a simple value class for
1084 // Class pairs with appropriate constructor methods for the various
1085 // situations.
1086
1087 // FIXME: Some of the split computations are wrong; unaligned vectors
1088 // shouldn't be passed in registers for example, so there is no chance they
1089 // can straddle an eightbyte. Verify & simplify.
1090
1091 Lo = Hi = NoClass;
1092
1093 Class &Current = OffsetBase < 64 ? Lo : Hi;
1094 Current = Memory;
1095
John McCall183700f2009-09-21 23:43:11 +00001096 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001097 BuiltinType::Kind k = BT->getKind();
1098
1099 if (k == BuiltinType::Void) {
1100 Current = NoClass;
1101 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1102 Lo = Integer;
1103 Hi = Integer;
1104 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1105 Current = Integer;
1106 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1107 Current = SSE;
1108 } else if (k == BuiltinType::LongDouble) {
1109 Lo = X87;
1110 Hi = X87Up;
1111 }
1112 // FIXME: _Decimal32 and _Decimal64 are SSE.
1113 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001114 return;
1115 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001116
Chris Lattner1090a9b2010-06-28 21:43:59 +00001117 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001119 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001120 return;
1121 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001122
Chris Lattner1090a9b2010-06-28 21:43:59 +00001123 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001125 return;
1126 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001127
Chris Lattner1090a9b2010-06-28 21:43:59 +00001128 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001129 if (Ty->isMemberFunctionPointerType())
1130 Lo = Hi = Integer;
1131 else
1132 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001133 return;
1134 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001135
Chris Lattner1090a9b2010-06-28 21:43:59 +00001136 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001137 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 if (Size == 32) {
1139 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1140 // float> as integer.
1141 Current = Integer;
1142
1143 // If this type crosses an eightbyte boundary, it should be
1144 // split.
1145 uint64_t EB_Real = (OffsetBase) / 64;
1146 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1147 if (EB_Real != EB_Imag)
1148 Hi = Lo;
1149 } else if (Size == 64) {
1150 // gcc passes <1 x double> in memory. :(
1151 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1152 return;
1153
1154 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001155 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001156 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1157 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1158 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159 Current = Integer;
1160 else
1161 Current = SSE;
1162
1163 // If this type crosses an eightbyte boundary, it should be
1164 // split.
1165 if (OffsetBase && OffsetBase != 64)
1166 Hi = Lo;
Bruno Cardoso Lopes75d28b52011-07-12 02:47:38 +00001167 } else if (Size == 128 || Size == 256) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001168 // Arguments of 256-bits are split into four eightbyte chunks. The
1169 // least significant one belongs to class SSE and all the others to class
1170 // SSEUP. The original Lo and Hi design considers that types can't be
1171 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1172 // This design isn't correct for 256-bits, but since there're no cases
1173 // where the upper parts would need to be inspected, avoid adding
1174 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175 Lo = SSE;
1176 Hi = SSEUp;
1177 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001178 return;
1179 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001180
Chris Lattner1090a9b2010-06-28 21:43:59 +00001181 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001182 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001183
Chris Lattnerea044322010-07-29 02:01:43 +00001184 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001185 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001186 if (Size <= 64)
1187 Current = Integer;
1188 else if (Size <= 128)
1189 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001190 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001191 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001192 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001193 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001194 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001195 Current = ComplexX87;
1196
1197 // If this complex type crosses an eightbyte boundary then it
1198 // should be split.
1199 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001200 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001201 if (Hi == NoClass && EB_Real != EB_Imag)
1202 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001203
Chris Lattner1090a9b2010-06-28 21:43:59 +00001204 return;
1205 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001206
Chris Lattnerea044322010-07-29 02:01:43 +00001207 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001208 // Arrays are treated like structures.
1209
Chris Lattnerea044322010-07-29 02:01:43 +00001210 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001211
1212 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001213 // than four eightbytes, ..., it has class MEMORY.
1214 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001215 return;
1216
1217 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1218 // fields, it has class MEMORY.
1219 //
1220 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001221 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001222 return;
1223
1224 // Otherwise implement simplified merge. We could be smarter about
1225 // this, but it isn't worth it and would be harder to verify.
1226 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001227 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001228 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001229
1230 // The only case a 256-bit wide vector could be used is when the array
1231 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1232 // to work for sizes wider than 128, early check and fallback to memory.
1233 if (Size > 128 && EltSize != 256)
1234 return;
1235
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1237 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001238 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001239 Lo = merge(Lo, FieldLo);
1240 Hi = merge(Hi, FieldHi);
1241 if (Lo == Memory || Hi == Memory)
1242 break;
1243 }
1244
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001245 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001246 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001247 return;
1248 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001249
Chris Lattner1090a9b2010-06-28 21:43:59 +00001250 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001251 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001252
1253 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001254 // than four eightbytes, ..., it has class MEMORY.
1255 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001256 return;
1257
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001258 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1259 // copy constructor or a non-trivial destructor, it is passed by invisible
1260 // reference.
1261 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1262 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001263
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001264 const RecordDecl *RD = RT->getDecl();
1265
1266 // Assume variable sized types are passed in memory.
1267 if (RD->hasFlexibleArrayMember())
1268 return;
1269
Chris Lattnerea044322010-07-29 02:01:43 +00001270 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001271
1272 // Reset Lo class, this will be recomputed.
1273 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001274
1275 // If this is a C++ record, classify the bases first.
1276 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1277 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1278 e = CXXRD->bases_end(); i != e; ++i) {
1279 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1280 "Unexpected base class!");
1281 const CXXRecordDecl *Base =
1282 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1283
1284 // Classify this field.
1285 //
1286 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1287 // single eightbyte, each is classified separately. Each eightbyte gets
1288 // initialized to class NO_CLASS.
1289 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001290 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001291 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001292 Lo = merge(Lo, FieldLo);
1293 Hi = merge(Hi, FieldHi);
1294 if (Lo == Memory || Hi == Memory)
1295 break;
1296 }
1297 }
1298
1299 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001300 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001301 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001302 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001303 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1304 bool BitField = i->isBitField();
1305
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001306 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1307 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001308 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001309 // The only case a 256-bit wide vector could be used is when the struct
1310 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1311 // to work for sizes wider than 128, early check and fallback to memory.
1312 //
1313 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1314 Lo = Memory;
1315 return;
1316 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001317 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001318 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001319 Lo = Memory;
1320 return;
1321 }
1322
1323 // Classify this field.
1324 //
1325 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1326 // exceeds a single eightbyte, each is classified
1327 // separately. Each eightbyte gets initialized to class
1328 // NO_CLASS.
1329 Class FieldLo, FieldHi;
1330
1331 // Bit-fields require special handling, they do not force the
1332 // structure to be passed in memory even if unaligned, and
1333 // therefore they can straddle an eightbyte.
1334 if (BitField) {
1335 // Ignore padding bit-fields.
1336 if (i->isUnnamedBitfield())
1337 continue;
1338
1339 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001340 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001341
1342 uint64_t EB_Lo = Offset / 64;
1343 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1344 FieldLo = FieldHi = NoClass;
1345 if (EB_Lo) {
1346 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1347 FieldLo = NoClass;
1348 FieldHi = Integer;
1349 } else {
1350 FieldLo = Integer;
1351 FieldHi = EB_Hi ? Integer : NoClass;
1352 }
1353 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001354 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001355 Lo = merge(Lo, FieldLo);
1356 Hi = merge(Hi, FieldHi);
1357 if (Lo == Memory || Hi == Memory)
1358 break;
1359 }
1360
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001361 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001362 }
1363}
1364
Chris Lattner9c254f02010-06-29 06:01:59 +00001365ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001366 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1367 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001368 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001369 // Treat an enum type as its underlying type.
1370 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1371 Ty = EnumTy->getDecl()->getIntegerType();
1372
1373 return (Ty->isPromotableIntegerType() ?
1374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1375 }
1376
1377 return ABIArgInfo::getIndirect(0);
1378}
1379
Chris Lattner9c254f02010-06-29 06:01:59 +00001380ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001381 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1382 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001383 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001384 // Treat an enum type as its underlying type.
1385 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1386 Ty = EnumTy->getDecl()->getIntegerType();
1387
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001388 return (Ty->isPromotableIntegerType() ?
1389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001390 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001391
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001392 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1393 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001394
Chris Lattner855d2272011-05-22 23:21:23 +00001395 // Compute the byval alignment. We specify the alignment of the byval in all
1396 // cases so that the mid-level optimizer knows the alignment of the byval.
1397 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1398 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001399}
1400
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001401/// GetByteVectorType - The ABI specifies that a value should be passed in an
1402/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001403/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001404llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001405 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001406
Chris Lattner15842bd2010-07-29 05:02:29 +00001407 // Wrapper structs that just contain vectors are passed just like vectors,
1408 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001409 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001410 while (STy && STy->getNumElements() == 1) {
1411 IRType = STy->getElementType(0);
1412 STy = dyn_cast<llvm::StructType>(IRType);
1413 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001414
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001415 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001416 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1417 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001418 unsigned BitWidth = VT->getBitWidth();
1419 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001420 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1421 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1422 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1423 EltTy->isIntegerTy(128)))
1424 return VT;
1425 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001426
Chris Lattner0f408f52010-07-29 04:56:46 +00001427 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1428}
1429
Chris Lattnere2962be2010-07-29 07:30:00 +00001430/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1431/// is known to either be off the end of the specified type or being in
1432/// alignment padding. The user type specified is known to be at most 128 bits
1433/// in size, and have passed through X86_64ABIInfo::classify with a successful
1434/// classification that put one of the two halves in the INTEGER class.
1435///
1436/// It is conservatively correct to return false.
1437static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1438 unsigned EndBit, ASTContext &Context) {
1439 // If the bytes being queried are off the end of the type, there is no user
1440 // data hiding here. This handles analysis of builtins, vectors and other
1441 // types that don't contain interesting padding.
1442 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1443 if (TySize <= StartBit)
1444 return true;
1445
Chris Lattner021c3a32010-07-29 07:43:55 +00001446 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1447 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1448 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1449
1450 // Check each element to see if the element overlaps with the queried range.
1451 for (unsigned i = 0; i != NumElts; ++i) {
1452 // If the element is after the span we care about, then we're done..
1453 unsigned EltOffset = i*EltSize;
1454 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001455
Chris Lattner021c3a32010-07-29 07:43:55 +00001456 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1457 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1458 EndBit-EltOffset, Context))
1459 return false;
1460 }
1461 // If it overlaps no elements, then it is safe to process as padding.
1462 return true;
1463 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001464
Chris Lattnere2962be2010-07-29 07:30:00 +00001465 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1466 const RecordDecl *RD = RT->getDecl();
1467 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001468
Chris Lattnere2962be2010-07-29 07:30:00 +00001469 // If this is a C++ record, check the bases first.
1470 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1471 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1472 e = CXXRD->bases_end(); i != e; ++i) {
1473 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1474 "Unexpected base class!");
1475 const CXXRecordDecl *Base =
1476 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001477
Chris Lattnere2962be2010-07-29 07:30:00 +00001478 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001479 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001480 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001481
Chris Lattnere2962be2010-07-29 07:30:00 +00001482 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1483 if (!BitsContainNoUserData(i->getType(), BaseStart,
1484 EndBit-BaseOffset, Context))
1485 return false;
1486 }
1487 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001488
Chris Lattnere2962be2010-07-29 07:30:00 +00001489 // Verify that no field has data that overlaps the region of interest. Yes
1490 // this could be sped up a lot by being smarter about queried fields,
1491 // however we're only looking at structs up to 16 bytes, so we don't care
1492 // much.
1493 unsigned idx = 0;
1494 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1495 i != e; ++i, ++idx) {
1496 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001497
Chris Lattnere2962be2010-07-29 07:30:00 +00001498 // If we found a field after the region we care about, then we're done.
1499 if (FieldOffset >= EndBit) break;
1500
1501 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1502 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1503 Context))
1504 return false;
1505 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001506
Chris Lattnere2962be2010-07-29 07:30:00 +00001507 // If nothing in this record overlapped the area of interest, then we're
1508 // clean.
1509 return true;
1510 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001511
Chris Lattnere2962be2010-07-29 07:30:00 +00001512 return false;
1513}
1514
Chris Lattner0b362002010-07-29 18:39:32 +00001515/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1516/// float member at the specified offset. For example, {int,{float}} has a
1517/// float at offset 4. It is conservatively correct for this routine to return
1518/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001519static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001520 const llvm::TargetData &TD) {
1521 // Base case if we find a float.
1522 if (IROffset == 0 && IRType->isFloatTy())
1523 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001524
Chris Lattner0b362002010-07-29 18:39:32 +00001525 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001526 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001527 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1528 unsigned Elt = SL->getElementContainingOffset(IROffset);
1529 IROffset -= SL->getElementOffset(Elt);
1530 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1531 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001532
Chris Lattner0b362002010-07-29 18:39:32 +00001533 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001534 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1535 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001536 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1537 IROffset -= IROffset/EltSize*EltSize;
1538 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1539 }
1540
1541 return false;
1542}
1543
Chris Lattnerf47c9442010-07-29 18:13:09 +00001544
1545/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1546/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001547llvm::Type *X86_64ABIInfo::
1548GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001549 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001550 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001551 // pass as float if the last 4 bytes is just padding. This happens for
1552 // structs that contain 3 floats.
1553 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1554 SourceOffset*8+64, getContext()))
1555 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001556
Chris Lattner0b362002010-07-29 18:39:32 +00001557 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1558 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1559 // case.
1560 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001561 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1562 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001563
Chris Lattnerf47c9442010-07-29 18:13:09 +00001564 return llvm::Type::getDoubleTy(getVMContext());
1565}
1566
1567
Chris Lattner0d2656d2010-07-29 17:40:35 +00001568/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1569/// an 8-byte GPR. This means that we either have a scalar or we are talking
1570/// about the high or low part of an up-to-16-byte struct. This routine picks
1571/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001572/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1573/// etc).
1574///
1575/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1576/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1577/// the 8-byte value references. PrefType may be null.
1578///
1579/// SourceTy is the source level type for the entire argument. SourceOffset is
1580/// an offset into this that we're processing (which is always either 0 or 8).
1581///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001582llvm::Type *X86_64ABIInfo::
1583GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001584 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001585 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1586 // returning an 8-byte unit starting with it. See if we can safely use it.
1587 if (IROffset == 0) {
1588 // Pointers and int64's always fill the 8-byte unit.
1589 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1590 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001591
Chris Lattnere2962be2010-07-29 07:30:00 +00001592 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1593 // goodness in the source type is just tail padding. This is allowed to
1594 // kick in for struct {double,int} on the int, but not on
1595 // struct{double,int,int} because we wouldn't return the second int. We
1596 // have to do this analysis on the source type because we can't depend on
1597 // unions being lowered a specific way etc.
1598 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1599 IRType->isIntegerTy(32)) {
1600 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001601
Chris Lattnere2962be2010-07-29 07:30:00 +00001602 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1603 SourceOffset*8+64, getContext()))
1604 return IRType;
1605 }
1606 }
Chris Lattner49382de2010-07-28 22:44:07 +00001607
Chris Lattner2acc6e32011-07-18 04:24:23 +00001608 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001609 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001610 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001611 if (IROffset < SL->getSizeInBytes()) {
1612 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1613 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001614
Chris Lattner0d2656d2010-07-29 17:40:35 +00001615 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1616 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001617 }
Chris Lattner49382de2010-07-28 22:44:07 +00001618 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001619
Chris Lattner2acc6e32011-07-18 04:24:23 +00001620 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001621 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001622 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1623 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001624 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1625 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001626 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001627
Chris Lattner49382de2010-07-28 22:44:07 +00001628 // Okay, we don't have any better idea of what to pass, so we pass this in an
1629 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001630 unsigned TySizeInBytes =
1631 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001632
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001633 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001634
Chris Lattner49382de2010-07-28 22:44:07 +00001635 // It is always safe to classify this as an integer type up to i64 that
1636 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001637 return llvm::IntegerType::get(getVMContext(),
1638 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001639}
1640
Chris Lattner66e7b682010-09-01 00:50:20 +00001641
1642/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1643/// be used as elements of a two register pair to pass or return, return a
1644/// first class aggregate to represent them. For example, if the low part of
1645/// a by-value argument should be passed as i32* and the high part as float,
1646/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001647static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001648GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001649 const llvm::TargetData &TD) {
1650 // In order to correctly satisfy the ABI, we need to the high part to start
1651 // at offset 8. If the high and low parts we inferred are both 4-byte types
1652 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1653 // the second element at offset 8. Check for this:
1654 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1655 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1656 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1657 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001658
Chris Lattner66e7b682010-09-01 00:50:20 +00001659 // To handle this, we have to increase the size of the low part so that the
1660 // second element will start at an 8 byte offset. We can't increase the size
1661 // of the second element because it might make us access off the end of the
1662 // struct.
1663 if (HiStart != 8) {
1664 // There are only two sorts of types the ABI generation code can produce for
1665 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1666 // Promote these to a larger type.
1667 if (Lo->isFloatTy())
1668 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1669 else {
1670 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1671 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1672 }
1673 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001674
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001675 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001676
1677
Chris Lattner66e7b682010-09-01 00:50:20 +00001678 // Verify that the second element is at an 8-byte offset.
1679 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1680 "Invalid x86-64 argument pair!");
1681 return Result;
1682}
1683
Chris Lattner519f68c2010-07-28 23:06:14 +00001684ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001685classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001686 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1687 // classification algorithm.
1688 X86_64ABIInfo::Class Lo, Hi;
1689 classify(RetTy, 0, Lo, Hi);
1690
1691 // Check some invariants.
1692 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001693 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1694
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001695 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001696 switch (Lo) {
1697 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001698 if (Hi == NoClass)
1699 return ABIArgInfo::getIgnore();
1700 // If the low part is just padding, it takes no register, leave ResType
1701 // null.
1702 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1703 "Unknown missing lo part");
1704 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001705
1706 case SSEUp:
1707 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001708 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001709
1710 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1711 // hidden argument.
1712 case Memory:
1713 return getIndirectReturnResult(RetTy);
1714
1715 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1716 // available register of the sequence %rax, %rdx is used.
1717 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001718 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001719
Chris Lattnereb518b42010-07-29 21:42:50 +00001720 // If we have a sign or zero extended integer, make sure to return Extend
1721 // so that the parameter gets the right LLVM IR attributes.
1722 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1723 // Treat an enum type as its underlying type.
1724 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1725 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001726
Chris Lattnereb518b42010-07-29 21:42:50 +00001727 if (RetTy->isIntegralOrEnumerationType() &&
1728 RetTy->isPromotableIntegerType())
1729 return ABIArgInfo::getExtend();
1730 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001731 break;
1732
1733 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1734 // available SSE register of the sequence %xmm0, %xmm1 is used.
1735 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001736 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001737 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001738
1739 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1740 // returned on the X87 stack in %st0 as 80-bit x87 number.
1741 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001742 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001743 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001744
1745 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1746 // part of the value is returned in %st0 and the imaginary part in
1747 // %st1.
1748 case ComplexX87:
1749 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001750 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001751 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001752 NULL);
1753 break;
1754 }
1755
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001756 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001757 switch (Hi) {
1758 // Memory was handled previously and X87 should
1759 // never occur as a hi class.
1760 case Memory:
1761 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001762 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001763
1764 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001765 case NoClass:
1766 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001767
Chris Lattner3db4dde2010-09-01 00:20:33 +00001768 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001769 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001770 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1771 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001772 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001773 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001774 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001775 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1776 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001777 break;
1778
1779 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001780 // is passed in the next available eightbyte chunk if the last used
1781 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001782 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001783 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001784 case SSEUp:
1785 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001786 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001787 break;
1788
1789 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1790 // returned together with the previous X87 value in %st0.
1791 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001792 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001793 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001794 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001795 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001796 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001797 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001798 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1799 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001800 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001801 break;
1802 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001803
Chris Lattner3db4dde2010-09-01 00:20:33 +00001804 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001805 // known to pass in the high eightbyte of the result. We do this by forming a
1806 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001807 if (HighPart)
1808 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001809
Chris Lattnereb518b42010-07-29 21:42:50 +00001810 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001811}
1812
Chris Lattnera3c109b2010-07-29 02:16:43 +00001813ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001814 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001815 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001816 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001817
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001818 // Check some invariants.
1819 // FIXME: Enforce these by construction.
1820 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001821 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1822
1823 neededInt = 0;
1824 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001825 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001826 switch (Lo) {
1827 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001828 if (Hi == NoClass)
1829 return ABIArgInfo::getIgnore();
1830 // If the low part is just padding, it takes no register, leave ResType
1831 // null.
1832 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1833 "Unknown missing lo part");
1834 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001835
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001836 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1837 // on the stack.
1838 case Memory:
1839
1840 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1841 // COMPLEX_X87, it is passed in memory.
1842 case X87:
1843 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001844 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1845 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001846 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001847
1848 case SSEUp:
1849 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001850 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001851
1852 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1853 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1854 // and %r9 is used.
1855 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001856 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001857
Chris Lattner49382de2010-07-28 22:44:07 +00001858 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001859 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001860
1861 // If we have a sign or zero extended integer, make sure to return Extend
1862 // so that the parameter gets the right LLVM IR attributes.
1863 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1864 // Treat an enum type as its underlying type.
1865 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1866 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001867
Chris Lattnereb518b42010-07-29 21:42:50 +00001868 if (Ty->isIntegralOrEnumerationType() &&
1869 Ty->isPromotableIntegerType())
1870 return ABIArgInfo::getExtend();
1871 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001872
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001873 break;
1874
1875 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1876 // available SSE register is used, the registers are taken in the
1877 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001878 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001879 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001880 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001881 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001882 break;
1883 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001884 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001885
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001886 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001887 switch (Hi) {
1888 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001889 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001890 // which is passed in memory.
1891 case Memory:
1892 case X87:
1893 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001894 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001895
1896 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001897
Chris Lattner645406a2010-09-01 00:24:35 +00001898 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001899 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001900 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001901 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001902
Chris Lattner645406a2010-09-01 00:24:35 +00001903 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1904 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001905 break;
1906
1907 // X87Up generally doesn't occur here (long double is passed in
1908 // memory), except in situations involving unions.
1909 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001910 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001911 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001912
Chris Lattner645406a2010-09-01 00:24:35 +00001913 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1914 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001915
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001916 ++neededSSE;
1917 break;
1918
1919 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1920 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001921 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001922 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001923 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001924 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001925 break;
1926 }
1927
Chris Lattner645406a2010-09-01 00:24:35 +00001928 // If a high part was specified, merge it together with the low part. It is
1929 // known to pass in the high eightbyte of the result. We do this by forming a
1930 // first class struct aggregate with the high and low part: {low, high}
1931 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001932 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001933
Chris Lattnereb518b42010-07-29 21:42:50 +00001934 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001935}
1936
Chris Lattneree5dcd02010-07-29 02:31:05 +00001937void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001938
Chris Lattnera3c109b2010-07-29 02:16:43 +00001939 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001940
1941 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001942 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001943
1944 // If the return value is indirect, then the hidden argument is consuming one
1945 // integer register.
1946 if (FI.getReturnInfo().isIndirect())
1947 --freeIntRegs;
1948
1949 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1950 // get assigned (in left-to-right order) for passing as follows...
1951 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1952 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001953 unsigned neededInt, neededSSE;
1954 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001955
1956 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1957 // eightbyte of an argument, the whole argument is passed on the
1958 // stack. If registers have already been assigned for some
1959 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001960 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001961 freeIntRegs -= neededInt;
1962 freeSSERegs -= neededSSE;
1963 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001964 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001965 }
1966 }
1967}
1968
1969static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1970 QualType Ty,
1971 CodeGenFunction &CGF) {
1972 llvm::Value *overflow_arg_area_p =
1973 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1974 llvm::Value *overflow_arg_area =
1975 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1976
1977 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1978 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1979 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1980 if (Align > 8) {
1981 // Note that we follow the ABI & gcc here, even though the type
1982 // could in theory have an alignment greater than 16. This case
1983 // shouldn't ever matter in practice.
1984
1985 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001986 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001987 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001988 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1989 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001990 CGF.Int64Ty);
1991 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001992 overflow_arg_area =
1993 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1994 overflow_arg_area->getType(),
1995 "overflow_arg_area.align");
1996 }
1997
1998 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001999 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002000 llvm::Value *Res =
2001 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002002 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002003
2004 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2005 // l->overflow_arg_area + sizeof(type).
2006 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2007 // an 8 byte boundary.
2008
2009 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002010 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002011 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002012 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2013 "overflow_arg_area.next");
2014 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2015
2016 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2017 return Res;
2018}
2019
2020llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2021 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002022 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002024 // Assume that va_list type is correct; should be pointer to LLVM type:
2025 // struct {
2026 // i32 gp_offset;
2027 // i32 fp_offset;
2028 // i8* overflow_arg_area;
2029 // i8* reg_save_area;
2030 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002031 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002032
Chris Lattnera14db752010-03-11 18:19:55 +00002033 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002034 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002035
2036 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2037 // in the registers. If not go to step 7.
2038 if (!neededInt && !neededSSE)
2039 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2040
2041 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2042 // general purpose registers needed to pass type and num_fp to hold
2043 // the number of floating point registers needed.
2044
2045 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2046 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2047 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2048 //
2049 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2050 // register save space).
2051
2052 llvm::Value *InRegs = 0;
2053 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2054 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2055 if (neededInt) {
2056 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2057 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002058 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2059 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002060 }
2061
2062 if (neededSSE) {
2063 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2064 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2065 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002066 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2067 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002068 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2069 }
2070
2071 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2072 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2073 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2074 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2075
2076 // Emit code to load the value if it was passed in registers.
2077
2078 CGF.EmitBlock(InRegBlock);
2079
2080 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2081 // an offset of l->gp_offset and/or l->fp_offset. This may require
2082 // copying to a temporary location in case the parameter is passed
2083 // in different register classes or requires an alignment greater
2084 // than 8 for general purpose registers and 16 for XMM registers.
2085 //
2086 // FIXME: This really results in shameful code when we end up needing to
2087 // collect arguments from different places; often what should result in a
2088 // simple assembling of a structure from scattered addresses has many more
2089 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002090 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002091 llvm::Value *RegAddr =
2092 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2093 "reg_save_area");
2094 if (neededInt && neededSSE) {
2095 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002096 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002097 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002098 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2099 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002100 llvm::Type *TyLo = ST->getElementType(0);
2101 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002102 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002103 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002104 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2105 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002106 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2107 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002108 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2109 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002110 llvm::Value *V =
2111 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2112 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2113 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2114 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2115
Owen Andersona1cf15f2009-07-14 23:10:40 +00002116 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002117 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002118 } else if (neededInt) {
2119 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2120 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002121 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002122 } else if (neededSSE == 1) {
2123 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2124 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2125 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002126 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002127 assert(neededSSE == 2 && "Invalid number of needed registers!");
2128 // SSE registers are spaced 16 bytes apart in the register save
2129 // area, we need to collect the two eightbytes together.
2130 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002131 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002132 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002133 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002134 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002135 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002136 DoubleTy, NULL);
2137 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2138 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2139 DblPtrTy));
2140 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2141 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2142 DblPtrTy));
2143 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2144 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2145 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002146 }
2147
2148 // AMD64-ABI 3.5.7p5: Step 5. Set:
2149 // l->gp_offset = l->gp_offset + num_gp * 8
2150 // l->fp_offset = l->fp_offset + num_fp * 16.
2151 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002152 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002153 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2154 gp_offset_p);
2155 }
2156 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002157 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002158 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2159 fp_offset_p);
2160 }
2161 CGF.EmitBranch(ContBlock);
2162
2163 // Emit code to load the value if it was passed in memory.
2164
2165 CGF.EmitBlock(InMemBlock);
2166 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2167
2168 // Return the appropriate result.
2169
2170 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002171 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002172 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002173 ResAddr->addIncoming(RegAddr, InRegBlock);
2174 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002175 return ResAddr;
2176}
2177
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002178ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2179
2180 if (Ty->isVoidType())
2181 return ABIArgInfo::getIgnore();
2182
2183 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2184 Ty = EnumTy->getDecl()->getIntegerType();
2185
2186 uint64_t Size = getContext().getTypeSize(Ty);
2187
2188 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002189 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2190 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002191 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2192
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002193 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2194 if (Size == 128 &&
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002195 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002196 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2197 Size));
2198
2199 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2200 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2201 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002202 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002203 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2204 Size));
2205
2206 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2207 }
2208
2209 if (Ty->isPromotableIntegerType())
2210 return ABIArgInfo::getExtend();
2211
2212 return ABIArgInfo::getDirect();
2213}
2214
2215void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2216
2217 QualType RetTy = FI.getReturnType();
2218 FI.getReturnInfo() = classify(RetTy);
2219
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002220 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2221 it != ie; ++it)
2222 it->info = classify(it->type);
2223}
2224
Chris Lattnerf13721d2010-08-31 16:44:54 +00002225llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2226 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002227 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2228 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002229
Chris Lattnerf13721d2010-08-31 16:44:54 +00002230 CGBuilderTy &Builder = CGF.Builder;
2231 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2232 "ap");
2233 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2234 llvm::Type *PTy =
2235 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2236 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2237
2238 uint64_t Offset =
2239 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2240 llvm::Value *NextAddr =
2241 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2242 "ap.next");
2243 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2244
2245 return AddrTyped;
2246}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002247
John McCallec853ba2010-03-11 00:10:12 +00002248// PowerPC-32
2249
2250namespace {
2251class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2252public:
Chris Lattnerea044322010-07-29 02:01:43 +00002253 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002254
John McCallec853ba2010-03-11 00:10:12 +00002255 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2256 // This is recovered from gcc output.
2257 return 1; // r1 is the dedicated stack pointer
2258 }
2259
2260 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002261 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002262};
2263
2264}
2265
2266bool
2267PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2268 llvm::Value *Address) const {
2269 // This is calculated from the LLVM and GCC tables and verified
2270 // against gcc output. AFAIK all ABIs use the same encoding.
2271
2272 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2273 llvm::LLVMContext &Context = CGF.getLLVMContext();
2274
Chris Lattner2acc6e32011-07-18 04:24:23 +00002275 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002276 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2277 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2278 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2279
2280 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002281 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002282
2283 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002284 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002285
2286 // 64-76 are various 4-byte special-purpose registers:
2287 // 64: mq
2288 // 65: lr
2289 // 66: ctr
2290 // 67: ap
2291 // 68-75 cr0-7
2292 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002293 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002294
2295 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002296 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002297
2298 // 109: vrsave
2299 // 110: vscr
2300 // 111: spe_acc
2301 // 112: spefscr
2302 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002303 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002304
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002305 return false;
John McCallec853ba2010-03-11 00:10:12 +00002306}
2307
2308
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002309//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002310// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002311//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002312
2313namespace {
2314
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002315class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002316public:
2317 enum ABIKind {
2318 APCS = 0,
2319 AAPCS = 1,
2320 AAPCS_VFP
2321 };
2322
2323private:
2324 ABIKind Kind;
2325
2326public:
Chris Lattnerea044322010-07-29 02:01:43 +00002327 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002328
John McCall49e34be2011-08-30 01:42:09 +00002329 bool isEABI() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002330 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall49e34be2011-08-30 01:42:09 +00002331 return (Env == "gnueabi" || Env == "eabi");
2332 }
2333
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002334private:
2335 ABIKind getABIKind() const { return Kind; }
2336
Chris Lattnera3c109b2010-07-29 02:16:43 +00002337 ABIArgInfo classifyReturnType(QualType RetTy) const;
2338 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002339
Chris Lattneree5dcd02010-07-29 02:31:05 +00002340 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002341
2342 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2343 CodeGenFunction &CGF) const;
2344};
2345
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002346class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2347public:
Chris Lattnerea044322010-07-29 02:01:43 +00002348 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2349 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002350
John McCall49e34be2011-08-30 01:42:09 +00002351 const ARMABIInfo &getABIInfo() const {
2352 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2353 }
2354
John McCall6374c332010-03-06 00:35:14 +00002355 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2356 return 13;
2357 }
Roman Divacky09345d12011-05-18 19:36:54 +00002358
Chris Lattner5f9e2722011-07-23 10:55:15 +00002359 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002360 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2361 }
2362
Roman Divacky09345d12011-05-18 19:36:54 +00002363 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2364 llvm::Value *Address) const {
2365 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2366 llvm::LLVMContext &Context = CGF.getLLVMContext();
2367
Chris Lattner2acc6e32011-07-18 04:24:23 +00002368 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002369 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2370
2371 // 0-15 are the 16 integer registers.
2372 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2373
2374 return false;
2375 }
John McCall49e34be2011-08-30 01:42:09 +00002376
2377 unsigned getSizeOfUnwindException() const {
2378 if (getABIInfo().isEABI()) return 88;
2379 return TargetCodeGenInfo::getSizeOfUnwindException();
2380 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002381};
2382
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002383}
2384
Chris Lattneree5dcd02010-07-29 02:31:05 +00002385void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002386 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002387 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002388 it != ie; ++it)
2389 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002390
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002391 // Always honor user-specified calling convention.
2392 if (FI.getCallingConvention() != llvm::CallingConv::C)
2393 return;
2394
2395 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002396 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002397 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002398 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002399 else
2400 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002401
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002402 // If user did not ask for specific calling convention explicitly (e.g. via
2403 // pcs attribute), set effective calling convention if it's different than ABI
2404 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002405 switch (getABIKind()) {
2406 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002407 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2408 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002409 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002410 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002411 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2412 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002413 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002414 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002415 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2416 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002417 break;
2418 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002419}
2420
Bob Wilson194f06a2011-08-03 05:58:22 +00002421/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2422/// aggregate. If HAMembers is non-null, the number of base elements
2423/// contained in the type is returned through it; this is used for the
2424/// recursive calls that check aggregate component types.
2425static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2426 ASTContext &Context,
2427 uint64_t *HAMembers = 0) {
2428 uint64_t Members;
2429 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2430 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2431 return false;
2432 Members *= AT->getSize().getZExtValue();
2433 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2434 const RecordDecl *RD = RT->getDecl();
2435 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2436 return false;
2437 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2438 if (!CXXRD->isAggregate())
2439 return false;
2440 }
2441 Members = 0;
2442 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2443 i != e; ++i) {
2444 const FieldDecl *FD = *i;
2445 uint64_t FldMembers;
2446 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2447 return false;
2448 Members += FldMembers;
2449 }
2450 } else {
2451 Members = 1;
2452 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2453 Members = 2;
2454 Ty = CT->getElementType();
2455 }
2456
2457 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2458 // double, or 64-bit or 128-bit vectors.
2459 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2460 if (BT->getKind() != BuiltinType::Float &&
2461 BT->getKind() != BuiltinType::Double)
2462 return false;
2463 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2464 unsigned VecSize = Context.getTypeSize(VT);
2465 if (VecSize != 64 && VecSize != 128)
2466 return false;
2467 } else {
2468 return false;
2469 }
2470
2471 // The base type must be the same for all members. Vector types of the
2472 // same total size are treated as being equivalent here.
2473 const Type *TyPtr = Ty.getTypePtr();
2474 if (!Base)
2475 Base = TyPtr;
2476 if (Base != TyPtr &&
2477 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2478 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2479 return false;
2480 }
2481
2482 // Homogeneous Aggregates can have at most 4 members of the base type.
2483 if (HAMembers)
2484 *HAMembers = Members;
2485 return (Members <= 4);
2486}
2487
Chris Lattnera3c109b2010-07-29 02:16:43 +00002488ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002489 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002490 // Treat an enum type as its underlying type.
2491 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2492 Ty = EnumTy->getDecl()->getIntegerType();
2493
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002494 return (Ty->isPromotableIntegerType() ?
2495 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002496 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002497
Daniel Dunbar42025572009-09-14 21:54:03 +00002498 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002499 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002500 return ABIArgInfo::getIgnore();
2501
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002502 // Structures with either a non-trivial destructor or a non-trivial
2503 // copy constructor are always indirect.
2504 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2505 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2506
Bob Wilson194f06a2011-08-03 05:58:22 +00002507 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2508 // Homogeneous Aggregates need to be expanded.
2509 const Type *Base = 0;
2510 if (isHomogeneousAggregate(Ty, Base, getContext()))
2511 return ABIArgInfo::getExpand();
2512 }
2513
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002514 // Otherwise, pass by coercing to a structure of the appropriate size.
2515 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002516 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2517 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002518 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002519 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002520 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002521 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002522 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2523 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002524 } else {
2525 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2526 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002527 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002528
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002529 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002530 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002531 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002532}
2533
Chris Lattnera3c109b2010-07-29 02:16:43 +00002534static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002535 llvm::LLVMContext &VMContext) {
2536 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2537 // is called integer-like if its size is less than or equal to one word, and
2538 // the offset of each of its addressable sub-fields is zero.
2539
2540 uint64_t Size = Context.getTypeSize(Ty);
2541
2542 // Check that the type fits in a word.
2543 if (Size > 32)
2544 return false;
2545
2546 // FIXME: Handle vector types!
2547 if (Ty->isVectorType())
2548 return false;
2549
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002550 // Float types are never treated as "integer like".
2551 if (Ty->isRealFloatingType())
2552 return false;
2553
Daniel Dunbar98303b92009-09-13 08:03:58 +00002554 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002555 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002556 return true;
2557
Daniel Dunbar45815812010-02-01 23:31:26 +00002558 // Small complex integer types are "integer like".
2559 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2560 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002561
2562 // Single element and zero sized arrays should be allowed, by the definition
2563 // above, but they are not.
2564
2565 // Otherwise, it must be a record type.
2566 const RecordType *RT = Ty->getAs<RecordType>();
2567 if (!RT) return false;
2568
2569 // Ignore records with flexible arrays.
2570 const RecordDecl *RD = RT->getDecl();
2571 if (RD->hasFlexibleArrayMember())
2572 return false;
2573
2574 // Check that all sub-fields are at offset 0, and are themselves "integer
2575 // like".
2576 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2577
2578 bool HadField = false;
2579 unsigned idx = 0;
2580 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2581 i != e; ++i, ++idx) {
2582 const FieldDecl *FD = *i;
2583
Daniel Dunbar679855a2010-01-29 03:22:29 +00002584 // Bit-fields are not addressable, we only need to verify they are "integer
2585 // like". We still have to disallow a subsequent non-bitfield, for example:
2586 // struct { int : 0; int x }
2587 // is non-integer like according to gcc.
2588 if (FD->isBitField()) {
2589 if (!RD->isUnion())
2590 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002591
Daniel Dunbar679855a2010-01-29 03:22:29 +00002592 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2593 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002594
Daniel Dunbar679855a2010-01-29 03:22:29 +00002595 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002596 }
2597
Daniel Dunbar679855a2010-01-29 03:22:29 +00002598 // Check if this field is at offset 0.
2599 if (Layout.getFieldOffset(idx) != 0)
2600 return false;
2601
Daniel Dunbar98303b92009-09-13 08:03:58 +00002602 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2603 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002604
Daniel Dunbar679855a2010-01-29 03:22:29 +00002605 // Only allow at most one field in a structure. This doesn't match the
2606 // wording above, but follows gcc in situations with a field following an
2607 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002608 if (!RD->isUnion()) {
2609 if (HadField)
2610 return false;
2611
2612 HadField = true;
2613 }
2614 }
2615
2616 return true;
2617}
2618
Chris Lattnera3c109b2010-07-29 02:16:43 +00002619ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002620 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002621 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002622
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002623 // Large vector types should be returned via memory.
2624 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2625 return ABIArgInfo::getIndirect(0);
2626
John McCalld608cdb2010-08-22 10:59:02 +00002627 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002628 // Treat an enum type as its underlying type.
2629 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2630 RetTy = EnumTy->getDecl()->getIntegerType();
2631
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002632 return (RetTy->isPromotableIntegerType() ?
2633 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002634 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002635
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002636 // Structures with either a non-trivial destructor or a non-trivial
2637 // copy constructor are always indirect.
2638 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2639 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2640
Daniel Dunbar98303b92009-09-13 08:03:58 +00002641 // Are we following APCS?
2642 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002643 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002644 return ABIArgInfo::getIgnore();
2645
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002646 // Complex types are all returned as packed integers.
2647 //
2648 // FIXME: Consider using 2 x vector types if the back end handles them
2649 // correctly.
2650 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002651 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002652 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002653
Daniel Dunbar98303b92009-09-13 08:03:58 +00002654 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002655 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002656 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002657 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002658 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002659 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002660 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002661 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2662 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002663 }
2664
2665 // Otherwise return in memory.
2666 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002667 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002668
2669 // Otherwise this is an AAPCS variant.
2670
Chris Lattnera3c109b2010-07-29 02:16:43 +00002671 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002672 return ABIArgInfo::getIgnore();
2673
Daniel Dunbar98303b92009-09-13 08:03:58 +00002674 // Aggregates <= 4 bytes are returned in r0; other aggregates
2675 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002676 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002677 if (Size <= 32) {
2678 // Return in the smallest viable integer type.
2679 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002680 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002681 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002682 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2683 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002684 }
2685
Daniel Dunbar98303b92009-09-13 08:03:58 +00002686 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002687}
2688
2689llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002690 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002691 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2692 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002693
2694 CGBuilderTy &Builder = CGF.Builder;
2695 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2696 "ap");
2697 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002698 // Handle address alignment for type alignment > 32 bits
2699 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2700 if (TyAlign > 4) {
2701 assert((TyAlign & (TyAlign - 1)) == 0 &&
2702 "Alignment is not power of 2!");
2703 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2704 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2705 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2706 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2707 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002708 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002709 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002710 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2711
2712 uint64_t Offset =
2713 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2714 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002715 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002716 "ap.next");
2717 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2718
2719 return AddrTyped;
2720}
2721
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002722//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002723// PTX ABI Implementation
2724//===----------------------------------------------------------------------===//
2725
2726namespace {
2727
2728class PTXABIInfo : public ABIInfo {
2729public:
2730 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2731
2732 ABIArgInfo classifyReturnType(QualType RetTy) const;
2733 ABIArgInfo classifyArgumentType(QualType Ty) const;
2734
2735 virtual void computeInfo(CGFunctionInfo &FI) const;
2736 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2737 CodeGenFunction &CFG) const;
2738};
2739
2740class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2741public:
2742 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2743 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002744
2745 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2746 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002747};
2748
2749ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2750 if (RetTy->isVoidType())
2751 return ABIArgInfo::getIgnore();
2752 if (isAggregateTypeForABI(RetTy))
2753 return ABIArgInfo::getIndirect(0);
2754 return ABIArgInfo::getDirect();
2755}
2756
2757ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2758 if (isAggregateTypeForABI(Ty))
2759 return ABIArgInfo::getIndirect(0);
2760
2761 return ABIArgInfo::getDirect();
2762}
2763
2764void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2765 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2766 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2767 it != ie; ++it)
2768 it->info = classifyArgumentType(it->type);
2769
2770 // Always honor user-specified calling convention.
2771 if (FI.getCallingConvention() != llvm::CallingConv::C)
2772 return;
2773
2774 // Calling convention as default by an ABI.
2775 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002776 const LangOptions &LangOpts = getContext().getLangOptions();
2777 if (LangOpts.OpenCL || LangOpts.CUDA) {
2778 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002779 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002780 } else {
2781 // If we are in standard C/C++ mode, use the triple to decide on the default
2782 StringRef Env =
2783 getContext().getTargetInfo().getTriple().getEnvironmentName();
2784 if (Env == "device")
2785 DefaultCC = llvm::CallingConv::PTX_Device;
2786 else
2787 DefaultCC = llvm::CallingConv::PTX_Kernel;
2788 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002789 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002790
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002791}
2792
2793llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2794 CodeGenFunction &CFG) const {
2795 llvm_unreachable("PTX does not support varargs");
2796 return 0;
2797}
2798
Justin Holewinski818eafb2011-10-05 17:58:44 +00002799void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2800 llvm::GlobalValue *GV,
2801 CodeGen::CodeGenModule &M) const{
2802 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2803 if (!FD) return;
2804
2805 llvm::Function *F = cast<llvm::Function>(GV);
2806
2807 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002808 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002809 // Use OpenCL function attributes to set proper calling conventions
2810 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002811 if (FD->hasAttr<OpenCLKernelAttr>()) {
2812 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002813 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002814 // And kernel functions are not subject to inlining
2815 F->addFnAttr(llvm::Attribute::NoInline);
2816 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002817 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002818
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002819 // Perform special handling in CUDA mode.
2820 if (M.getLangOptions().CUDA) {
2821 // CUDA __global__ functions get a kernel calling convention. Since
2822 // __global__ functions cannot be called from the device, we do not
2823 // need to set the noinline attribute.
2824 if (FD->getAttr<CUDAGlobalAttr>())
2825 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002826 }
2827}
2828
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002829}
2830
2831//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002832// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002833//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002834
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002835namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002836
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002837class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002838public:
2839 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2840
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002841 bool isPromotableIntegerType(QualType Ty) const;
2842
Chris Lattnera3c109b2010-07-29 02:16:43 +00002843 ABIArgInfo classifyReturnType(QualType RetTy) const;
2844 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002845
Chris Lattneree5dcd02010-07-29 02:31:05 +00002846 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002847 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002848 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2849 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002850 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002851 }
2852
2853 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2854 CodeGenFunction &CGF) const;
2855};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002856
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002857class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2858public:
Chris Lattnerea044322010-07-29 02:01:43 +00002859 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2860 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002861};
2862
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002863}
2864
2865bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2866 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002867 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002868 switch (BT->getKind()) {
2869 case BuiltinType::Bool:
2870 case BuiltinType::Char_S:
2871 case BuiltinType::Char_U:
2872 case BuiltinType::SChar:
2873 case BuiltinType::UChar:
2874 case BuiltinType::Short:
2875 case BuiltinType::UShort:
2876 case BuiltinType::Int:
2877 case BuiltinType::UInt:
2878 return true;
2879 default:
2880 return false;
2881 }
2882 return false;
2883}
2884
2885llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2886 CodeGenFunction &CGF) const {
2887 // FIXME: Implement
2888 return 0;
2889}
2890
2891
Chris Lattnera3c109b2010-07-29 02:16:43 +00002892ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2893 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002894 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002895 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002896 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002897
2898 return (isPromotableIntegerType(RetTy) ?
2899 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002900}
2901
Chris Lattnera3c109b2010-07-29 02:16:43 +00002902ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002903 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002904 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002905
2906 return (isPromotableIntegerType(Ty) ?
2907 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002908}
2909
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002910//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002911// MBlaze ABI Implementation
2912//===----------------------------------------------------------------------===//
2913
2914namespace {
2915
2916class MBlazeABIInfo : public ABIInfo {
2917public:
2918 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2919
2920 bool isPromotableIntegerType(QualType Ty) const;
2921
2922 ABIArgInfo classifyReturnType(QualType RetTy) const;
2923 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2924
2925 virtual void computeInfo(CGFunctionInfo &FI) const {
2926 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2927 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2928 it != ie; ++it)
2929 it->info = classifyArgumentType(it->type);
2930 }
2931
2932 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2933 CodeGenFunction &CGF) const;
2934};
2935
2936class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2937public:
2938 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2939 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2940 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2941 CodeGen::CodeGenModule &M) const;
2942};
2943
2944}
2945
2946bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2947 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2948 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2949 switch (BT->getKind()) {
2950 case BuiltinType::Bool:
2951 case BuiltinType::Char_S:
2952 case BuiltinType::Char_U:
2953 case BuiltinType::SChar:
2954 case BuiltinType::UChar:
2955 case BuiltinType::Short:
2956 case BuiltinType::UShort:
2957 return true;
2958 default:
2959 return false;
2960 }
2961 return false;
2962}
2963
2964llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2965 CodeGenFunction &CGF) const {
2966 // FIXME: Implement
2967 return 0;
2968}
2969
2970
2971ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2972 if (RetTy->isVoidType())
2973 return ABIArgInfo::getIgnore();
2974 if (isAggregateTypeForABI(RetTy))
2975 return ABIArgInfo::getIndirect(0);
2976
2977 return (isPromotableIntegerType(RetTy) ?
2978 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2979}
2980
2981ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2982 if (isAggregateTypeForABI(Ty))
2983 return ABIArgInfo::getIndirect(0);
2984
2985 return (isPromotableIntegerType(Ty) ?
2986 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2987}
2988
2989void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2990 llvm::GlobalValue *GV,
2991 CodeGen::CodeGenModule &M)
2992 const {
2993 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2994 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002995
Wesley Peck276fdf42010-12-19 19:57:51 +00002996 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2997 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2998 CC = llvm::CallingConv::MBLAZE_INTR;
2999 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3000 CC = llvm::CallingConv::MBLAZE_SVOL;
3001
3002 if (CC != llvm::CallingConv::C) {
3003 // Handle 'interrupt_handler' attribute:
3004 llvm::Function *F = cast<llvm::Function>(GV);
3005
3006 // Step 1: Set ISR calling convention.
3007 F->setCallingConv(CC);
3008
3009 // Step 2: Add attributes goodness.
3010 F->addFnAttr(llvm::Attribute::NoInline);
3011 }
3012
3013 // Step 3: Emit _interrupt_handler alias.
3014 if (CC == llvm::CallingConv::MBLAZE_INTR)
3015 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3016 "_interrupt_handler", GV, &M.getModule());
3017}
3018
3019
3020//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003021// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003022//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003023
3024namespace {
3025
3026class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3027public:
Chris Lattnerea044322010-07-29 02:01:43 +00003028 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3029 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003030 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3031 CodeGen::CodeGenModule &M) const;
3032};
3033
3034}
3035
3036void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3037 llvm::GlobalValue *GV,
3038 CodeGen::CodeGenModule &M) const {
3039 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3040 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3041 // Handle 'interrupt' attribute:
3042 llvm::Function *F = cast<llvm::Function>(GV);
3043
3044 // Step 1: Set ISR calling convention.
3045 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3046
3047 // Step 2: Add attributes goodness.
3048 F->addFnAttr(llvm::Attribute::NoInline);
3049
3050 // Step 3: Emit ISR vector alias.
3051 unsigned Num = attr->getNumber() + 0xffe0;
3052 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003053 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003054 GV, &M.getModule());
3055 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003056 }
3057}
3058
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003059//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003060// MIPS ABI Implementation. This works for both little-endian and
3061// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003062//===----------------------------------------------------------------------===//
3063
John McCallaeeb7012010-05-27 06:19:26 +00003064namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003065class MipsABIInfo : public ABIInfo {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003066 static const unsigned MinABIStackAlignInBytes = 4;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003067public:
3068 MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3069
3070 ABIArgInfo classifyReturnType(QualType RetTy) const;
3071 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3072 virtual void computeInfo(CGFunctionInfo &FI) const;
3073 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3074 CodeGenFunction &CGF) const;
3075};
3076
Akira Hatanaka3827e422011-08-12 01:43:14 +00003077const unsigned MipsABIInfo::MinABIStackAlignInBytes;
3078
John McCallaeeb7012010-05-27 06:19:26 +00003079class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003080 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003081public:
Akira Hatanakae624fa02011-09-20 18:23:28 +00003082 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, unsigned SZ)
3083 : TargetCodeGenInfo(new MipsABIInfo(CGT)), SizeOfUnwindException(SZ) {}
John McCallaeeb7012010-05-27 06:19:26 +00003084
3085 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3086 return 29;
3087 }
3088
3089 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003090 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003091
3092 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003093 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003094 }
John McCallaeeb7012010-05-27 06:19:26 +00003095};
3096}
3097
Akira Hatanaka619e8872011-06-02 00:09:17 +00003098ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3099 if (isAggregateTypeForABI(Ty)) {
3100 // Ignore empty aggregates.
3101 if (getContext().getTypeSize(Ty) == 0)
3102 return ABIArgInfo::getIgnore();
3103
Akira Hatanaka511949b2011-08-01 18:09:58 +00003104 // Records with non trivial destructors/constructors should not be passed
3105 // by value.
3106 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3107 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3108
Akira Hatanaka619e8872011-06-02 00:09:17 +00003109 return ABIArgInfo::getIndirect(0);
3110 }
3111
3112 // Treat an enum type as its underlying type.
3113 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3114 Ty = EnumTy->getDecl()->getIntegerType();
3115
3116 return (Ty->isPromotableIntegerType() ?
3117 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3118}
3119
3120ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3121 if (RetTy->isVoidType())
3122 return ABIArgInfo::getIgnore();
3123
3124 if (isAggregateTypeForABI(RetTy)) {
3125 if (RetTy->isAnyComplexType())
3126 return ABIArgInfo::getDirect();
3127
3128 return ABIArgInfo::getIndirect(0);
3129 }
3130
3131 // Treat an enum type as its underlying type.
3132 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3133 RetTy = EnumTy->getDecl()->getIntegerType();
3134
3135 return (RetTy->isPromotableIntegerType() ?
3136 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3137}
3138
3139void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3140 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3141 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3142 it != ie; ++it)
3143 it->info = classifyArgumentType(it->type);
3144}
3145
3146llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3147 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003148 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3149 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3150
3151 CGBuilderTy &Builder = CGF.Builder;
3152 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3153 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3154 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3155 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3156 llvm::Value *AddrTyped;
3157
3158 if (TypeAlign > MinABIStackAlignInBytes) {
3159 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3160 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3161 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3162 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3163 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3164 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3165 }
3166 else
3167 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3168
3169 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka7b0a0382011-08-12 02:30:14 +00003170 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003171 uint64_t Offset =
3172 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3173 llvm::Value *NextAddr =
3174 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3175 "ap.next");
3176 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3177
3178 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003179}
3180
John McCallaeeb7012010-05-27 06:19:26 +00003181bool
3182MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3183 llvm::Value *Address) const {
3184 // This information comes from gcc's implementation, which seems to
3185 // as canonical as it gets.
3186
3187 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3188 llvm::LLVMContext &Context = CGF.getLLVMContext();
3189
3190 // Everything on MIPS is 4 bytes. Double-precision FP registers
3191 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003192 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003193 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3194
3195 // 0-31 are the general purpose registers, $0 - $31.
3196 // 32-63 are the floating-point registers, $f0 - $f31.
3197 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3198 // 66 is the (notional, I think) register for signal-handler return.
3199 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3200
3201 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3202 // They are one bit wide and ignored here.
3203
3204 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3205 // (coprocessor 1 is the FP unit)
3206 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3207 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3208 // 176-181 are the DSP accumulator registers.
3209 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3210
3211 return false;
3212}
3213
3214
Chris Lattnerea044322010-07-29 02:01:43 +00003215const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003216 if (TheTargetCodeGenInfo)
3217 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003218
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003219 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003220 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003221 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003222 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003223
John McCallaeeb7012010-05-27 06:19:26 +00003224 case llvm::Triple::mips:
3225 case llvm::Triple::mipsel:
Akira Hatanakae624fa02011-09-20 18:23:28 +00003226 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, 24));
John McCallaeeb7012010-05-27 06:19:26 +00003227
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003228 case llvm::Triple::mips64:
3229 case llvm::Triple::mips64el:
3230 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, 32));
3231
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003232 case llvm::Triple::arm:
3233 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003234 {
3235 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003236
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003237 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003238 Kind = ARMABIInfo::APCS;
3239 else if (CodeGenOpts.FloatABI == "hard")
3240 Kind = ARMABIInfo::AAPCS_VFP;
3241
3242 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3243 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003244
John McCallec853ba2010-03-11 00:10:12 +00003245 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003246 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003247
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003248 case llvm::Triple::ptx32:
3249 case llvm::Triple::ptx64:
3250 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3251
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003252 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00003253 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003254
Wesley Peck276fdf42010-12-19 19:57:51 +00003255 case llvm::Triple::mblaze:
3256 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3257
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003258 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003259 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003260
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003261 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003262 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003263
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003264 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003265 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003266 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003267
3268 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003269 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003270 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003271 case llvm::Triple::AuroraUX:
3272 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003273 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003274 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003275 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003276 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003277 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003278
3279 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003280 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003281 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003282 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003283 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003284
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003285 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003286 switch (Triple.getOS()) {
3287 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003288 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003289 case llvm::Triple::Cygwin:
3290 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3291 default:
3292 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3293 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003294 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003295}