blob: ce729ce8dad159a261b7ea51fcb1a275b7e610ec [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);
Chris Lattnerea044322010-07-29 02:01:43 +00001340 uint64_t Size =
1341 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001342
1343 uint64_t EB_Lo = Offset / 64;
1344 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1345 FieldLo = FieldHi = NoClass;
1346 if (EB_Lo) {
1347 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1348 FieldLo = NoClass;
1349 FieldHi = Integer;
1350 } else {
1351 FieldLo = Integer;
1352 FieldHi = EB_Hi ? Integer : NoClass;
1353 }
1354 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001355 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 Lo = merge(Lo, FieldLo);
1357 Hi = merge(Hi, FieldHi);
1358 if (Lo == Memory || Hi == Memory)
1359 break;
1360 }
1361
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001362 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001363 }
1364}
1365
Chris Lattner9c254f02010-06-29 06:01:59 +00001366ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001367 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1368 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001369 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001370 // Treat an enum type as its underlying type.
1371 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1372 Ty = EnumTy->getDecl()->getIntegerType();
1373
1374 return (Ty->isPromotableIntegerType() ?
1375 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1376 }
1377
1378 return ABIArgInfo::getIndirect(0);
1379}
1380
Chris Lattner9c254f02010-06-29 06:01:59 +00001381ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001382 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1383 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001384 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001385 // Treat an enum type as its underlying type.
1386 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1387 Ty = EnumTy->getDecl()->getIntegerType();
1388
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001389 return (Ty->isPromotableIntegerType() ?
1390 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001391 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001393 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1394 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001395
Chris Lattner855d2272011-05-22 23:21:23 +00001396 // Compute the byval alignment. We specify the alignment of the byval in all
1397 // cases so that the mid-level optimizer knows the alignment of the byval.
1398 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1399 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001400}
1401
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001402/// GetByteVectorType - The ABI specifies that a value should be passed in an
1403/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001404/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001405llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001406 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001407
Chris Lattner15842bd2010-07-29 05:02:29 +00001408 // Wrapper structs that just contain vectors are passed just like vectors,
1409 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001410 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001411 while (STy && STy->getNumElements() == 1) {
1412 IRType = STy->getElementType(0);
1413 STy = dyn_cast<llvm::StructType>(IRType);
1414 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001415
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001416 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001417 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1418 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001419 unsigned BitWidth = VT->getBitWidth();
1420 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001421 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1422 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1423 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1424 EltTy->isIntegerTy(128)))
1425 return VT;
1426 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001427
Chris Lattner0f408f52010-07-29 04:56:46 +00001428 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1429}
1430
Chris Lattnere2962be2010-07-29 07:30:00 +00001431/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1432/// is known to either be off the end of the specified type or being in
1433/// alignment padding. The user type specified is known to be at most 128 bits
1434/// in size, and have passed through X86_64ABIInfo::classify with a successful
1435/// classification that put one of the two halves in the INTEGER class.
1436///
1437/// It is conservatively correct to return false.
1438static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1439 unsigned EndBit, ASTContext &Context) {
1440 // If the bytes being queried are off the end of the type, there is no user
1441 // data hiding here. This handles analysis of builtins, vectors and other
1442 // types that don't contain interesting padding.
1443 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1444 if (TySize <= StartBit)
1445 return true;
1446
Chris Lattner021c3a32010-07-29 07:43:55 +00001447 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1448 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1449 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1450
1451 // Check each element to see if the element overlaps with the queried range.
1452 for (unsigned i = 0; i != NumElts; ++i) {
1453 // If the element is after the span we care about, then we're done..
1454 unsigned EltOffset = i*EltSize;
1455 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001456
Chris Lattner021c3a32010-07-29 07:43:55 +00001457 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1458 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1459 EndBit-EltOffset, Context))
1460 return false;
1461 }
1462 // If it overlaps no elements, then it is safe to process as padding.
1463 return true;
1464 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001465
Chris Lattnere2962be2010-07-29 07:30:00 +00001466 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1467 const RecordDecl *RD = RT->getDecl();
1468 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001469
Chris Lattnere2962be2010-07-29 07:30:00 +00001470 // If this is a C++ record, check the bases first.
1471 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1472 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1473 e = CXXRD->bases_end(); i != e; ++i) {
1474 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1475 "Unexpected base class!");
1476 const CXXRecordDecl *Base =
1477 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001478
Chris Lattnere2962be2010-07-29 07:30:00 +00001479 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001480 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001481 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001482
Chris Lattnere2962be2010-07-29 07:30:00 +00001483 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1484 if (!BitsContainNoUserData(i->getType(), BaseStart,
1485 EndBit-BaseOffset, Context))
1486 return false;
1487 }
1488 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001489
Chris Lattnere2962be2010-07-29 07:30:00 +00001490 // Verify that no field has data that overlaps the region of interest. Yes
1491 // this could be sped up a lot by being smarter about queried fields,
1492 // however we're only looking at structs up to 16 bytes, so we don't care
1493 // much.
1494 unsigned idx = 0;
1495 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1496 i != e; ++i, ++idx) {
1497 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001498
Chris Lattnere2962be2010-07-29 07:30:00 +00001499 // If we found a field after the region we care about, then we're done.
1500 if (FieldOffset >= EndBit) break;
1501
1502 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1503 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1504 Context))
1505 return false;
1506 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001507
Chris Lattnere2962be2010-07-29 07:30:00 +00001508 // If nothing in this record overlapped the area of interest, then we're
1509 // clean.
1510 return true;
1511 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001512
Chris Lattnere2962be2010-07-29 07:30:00 +00001513 return false;
1514}
1515
Chris Lattner0b362002010-07-29 18:39:32 +00001516/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1517/// float member at the specified offset. For example, {int,{float}} has a
1518/// float at offset 4. It is conservatively correct for this routine to return
1519/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001520static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001521 const llvm::TargetData &TD) {
1522 // Base case if we find a float.
1523 if (IROffset == 0 && IRType->isFloatTy())
1524 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001525
Chris Lattner0b362002010-07-29 18:39:32 +00001526 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001527 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001528 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1529 unsigned Elt = SL->getElementContainingOffset(IROffset);
1530 IROffset -= SL->getElementOffset(Elt);
1531 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1532 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001533
Chris Lattner0b362002010-07-29 18:39:32 +00001534 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001535 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1536 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001537 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1538 IROffset -= IROffset/EltSize*EltSize;
1539 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1540 }
1541
1542 return false;
1543}
1544
Chris Lattnerf47c9442010-07-29 18:13:09 +00001545
1546/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1547/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001548llvm::Type *X86_64ABIInfo::
1549GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001550 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001551 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001552 // pass as float if the last 4 bytes is just padding. This happens for
1553 // structs that contain 3 floats.
1554 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1555 SourceOffset*8+64, getContext()))
1556 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001557
Chris Lattner0b362002010-07-29 18:39:32 +00001558 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1559 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1560 // case.
1561 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001562 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1563 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001564
Chris Lattnerf47c9442010-07-29 18:13:09 +00001565 return llvm::Type::getDoubleTy(getVMContext());
1566}
1567
1568
Chris Lattner0d2656d2010-07-29 17:40:35 +00001569/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1570/// an 8-byte GPR. This means that we either have a scalar or we are talking
1571/// about the high or low part of an up-to-16-byte struct. This routine picks
1572/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001573/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1574/// etc).
1575///
1576/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1577/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1578/// the 8-byte value references. PrefType may be null.
1579///
1580/// SourceTy is the source level type for the entire argument. SourceOffset is
1581/// an offset into this that we're processing (which is always either 0 or 8).
1582///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001583llvm::Type *X86_64ABIInfo::
1584GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001585 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001586 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1587 // returning an 8-byte unit starting with it. See if we can safely use it.
1588 if (IROffset == 0) {
1589 // Pointers and int64's always fill the 8-byte unit.
1590 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1591 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001592
Chris Lattnere2962be2010-07-29 07:30:00 +00001593 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1594 // goodness in the source type is just tail padding. This is allowed to
1595 // kick in for struct {double,int} on the int, but not on
1596 // struct{double,int,int} because we wouldn't return the second int. We
1597 // have to do this analysis on the source type because we can't depend on
1598 // unions being lowered a specific way etc.
1599 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1600 IRType->isIntegerTy(32)) {
1601 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001602
Chris Lattnere2962be2010-07-29 07:30:00 +00001603 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1604 SourceOffset*8+64, getContext()))
1605 return IRType;
1606 }
1607 }
Chris Lattner49382de2010-07-28 22:44:07 +00001608
Chris Lattner2acc6e32011-07-18 04:24:23 +00001609 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001610 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001611 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001612 if (IROffset < SL->getSizeInBytes()) {
1613 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1614 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001615
Chris Lattner0d2656d2010-07-29 17:40:35 +00001616 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1617 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001618 }
Chris Lattner49382de2010-07-28 22:44:07 +00001619 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001620
Chris Lattner2acc6e32011-07-18 04:24:23 +00001621 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001622 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001623 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1624 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001625 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1626 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001627 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001628
Chris Lattner49382de2010-07-28 22:44:07 +00001629 // Okay, we don't have any better idea of what to pass, so we pass this in an
1630 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001631 unsigned TySizeInBytes =
1632 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001633
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001634 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001635
Chris Lattner49382de2010-07-28 22:44:07 +00001636 // It is always safe to classify this as an integer type up to i64 that
1637 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001638 return llvm::IntegerType::get(getVMContext(),
1639 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001640}
1641
Chris Lattner66e7b682010-09-01 00:50:20 +00001642
1643/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1644/// be used as elements of a two register pair to pass or return, return a
1645/// first class aggregate to represent them. For example, if the low part of
1646/// a by-value argument should be passed as i32* and the high part as float,
1647/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001648static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001649GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001650 const llvm::TargetData &TD) {
1651 // In order to correctly satisfy the ABI, we need to the high part to start
1652 // at offset 8. If the high and low parts we inferred are both 4-byte types
1653 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1654 // the second element at offset 8. Check for this:
1655 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1656 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1657 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1658 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001659
Chris Lattner66e7b682010-09-01 00:50:20 +00001660 // To handle this, we have to increase the size of the low part so that the
1661 // second element will start at an 8 byte offset. We can't increase the size
1662 // of the second element because it might make us access off the end of the
1663 // struct.
1664 if (HiStart != 8) {
1665 // There are only two sorts of types the ABI generation code can produce for
1666 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1667 // Promote these to a larger type.
1668 if (Lo->isFloatTy())
1669 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1670 else {
1671 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1672 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1673 }
1674 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001675
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001676 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001677
1678
Chris Lattner66e7b682010-09-01 00:50:20 +00001679 // Verify that the second element is at an 8-byte offset.
1680 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1681 "Invalid x86-64 argument pair!");
1682 return Result;
1683}
1684
Chris Lattner519f68c2010-07-28 23:06:14 +00001685ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001686classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001687 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1688 // classification algorithm.
1689 X86_64ABIInfo::Class Lo, Hi;
1690 classify(RetTy, 0, Lo, Hi);
1691
1692 // Check some invariants.
1693 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001694 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1695
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001696 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001697 switch (Lo) {
1698 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001699 if (Hi == NoClass)
1700 return ABIArgInfo::getIgnore();
1701 // If the low part is just padding, it takes no register, leave ResType
1702 // null.
1703 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1704 "Unknown missing lo part");
1705 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001706
1707 case SSEUp:
1708 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001709 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001710
1711 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1712 // hidden argument.
1713 case Memory:
1714 return getIndirectReturnResult(RetTy);
1715
1716 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1717 // available register of the sequence %rax, %rdx is used.
1718 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001719 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001720
Chris Lattnereb518b42010-07-29 21:42:50 +00001721 // If we have a sign or zero extended integer, make sure to return Extend
1722 // so that the parameter gets the right LLVM IR attributes.
1723 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1724 // Treat an enum type as its underlying type.
1725 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1726 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001727
Chris Lattnereb518b42010-07-29 21:42:50 +00001728 if (RetTy->isIntegralOrEnumerationType() &&
1729 RetTy->isPromotableIntegerType())
1730 return ABIArgInfo::getExtend();
1731 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001732 break;
1733
1734 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1735 // available SSE register of the sequence %xmm0, %xmm1 is used.
1736 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001737 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001738 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001739
1740 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1741 // returned on the X87 stack in %st0 as 80-bit x87 number.
1742 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001743 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001744 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001745
1746 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1747 // part of the value is returned in %st0 and the imaginary part in
1748 // %st1.
1749 case ComplexX87:
1750 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001751 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001752 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001753 NULL);
1754 break;
1755 }
1756
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001757 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001758 switch (Hi) {
1759 // Memory was handled previously and X87 should
1760 // never occur as a hi class.
1761 case Memory:
1762 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001763 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001764
1765 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001766 case NoClass:
1767 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001768
Chris Lattner3db4dde2010-09-01 00:20:33 +00001769 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001770 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001771 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1772 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001773 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001774 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001775 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001776 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1777 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001778 break;
1779
1780 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001781 // is passed in the next available eightbyte chunk if the last used
1782 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001783 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001784 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001785 case SSEUp:
1786 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001787 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001788 break;
1789
1790 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1791 // returned together with the previous X87 value in %st0.
1792 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001793 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001794 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001795 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001796 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001797 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001798 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001799 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1800 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001801 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001802 break;
1803 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001804
Chris Lattner3db4dde2010-09-01 00:20:33 +00001805 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001806 // known to pass in the high eightbyte of the result. We do this by forming a
1807 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001808 if (HighPart)
1809 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001810
Chris Lattnereb518b42010-07-29 21:42:50 +00001811 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001812}
1813
Chris Lattnera3c109b2010-07-29 02:16:43 +00001814ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001815 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001816 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001817 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001818
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001819 // Check some invariants.
1820 // FIXME: Enforce these by construction.
1821 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001822 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1823
1824 neededInt = 0;
1825 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001826 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001827 switch (Lo) {
1828 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001829 if (Hi == NoClass)
1830 return ABIArgInfo::getIgnore();
1831 // If the low part is just padding, it takes no register, leave ResType
1832 // null.
1833 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1834 "Unknown missing lo part");
1835 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001836
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001837 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1838 // on the stack.
1839 case Memory:
1840
1841 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1842 // COMPLEX_X87, it is passed in memory.
1843 case X87:
1844 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001845 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1846 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001847 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001848
1849 case SSEUp:
1850 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001851 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001852
1853 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1854 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1855 // and %r9 is used.
1856 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001857 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001858
Chris Lattner49382de2010-07-28 22:44:07 +00001859 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001860 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001861
1862 // If we have a sign or zero extended integer, make sure to return Extend
1863 // so that the parameter gets the right LLVM IR attributes.
1864 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1865 // Treat an enum type as its underlying type.
1866 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1867 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001868
Chris Lattnereb518b42010-07-29 21:42:50 +00001869 if (Ty->isIntegralOrEnumerationType() &&
1870 Ty->isPromotableIntegerType())
1871 return ABIArgInfo::getExtend();
1872 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001873
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001874 break;
1875
1876 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1877 // available SSE register is used, the registers are taken in the
1878 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001879 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001880 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001881 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001882 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001883 break;
1884 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001885 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001886
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001887 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001888 switch (Hi) {
1889 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001890 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001891 // which is passed in memory.
1892 case Memory:
1893 case X87:
1894 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001895 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001896
1897 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001898
Chris Lattner645406a2010-09-01 00:24:35 +00001899 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001900 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001901 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001902 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001903
Chris Lattner645406a2010-09-01 00:24:35 +00001904 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1905 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001906 break;
1907
1908 // X87Up generally doesn't occur here (long double is passed in
1909 // memory), except in situations involving unions.
1910 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001911 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001912 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001913
Chris Lattner645406a2010-09-01 00:24:35 +00001914 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1915 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001916
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001917 ++neededSSE;
1918 break;
1919
1920 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1921 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001922 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001923 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001924 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001925 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001926 break;
1927 }
1928
Chris Lattner645406a2010-09-01 00:24:35 +00001929 // If a high part was specified, merge it together with the low part. It is
1930 // known to pass in the high eightbyte of the result. We do this by forming a
1931 // first class struct aggregate with the high and low part: {low, high}
1932 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001933 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001934
Chris Lattnereb518b42010-07-29 21:42:50 +00001935 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001936}
1937
Chris Lattneree5dcd02010-07-29 02:31:05 +00001938void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001939
Chris Lattnera3c109b2010-07-29 02:16:43 +00001940 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001941
1942 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001943 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001944
1945 // If the return value is indirect, then the hidden argument is consuming one
1946 // integer register.
1947 if (FI.getReturnInfo().isIndirect())
1948 --freeIntRegs;
1949
1950 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1951 // get assigned (in left-to-right order) for passing as follows...
1952 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1953 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001954 unsigned neededInt, neededSSE;
1955 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001956
1957 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1958 // eightbyte of an argument, the whole argument is passed on the
1959 // stack. If registers have already been assigned for some
1960 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001961 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001962 freeIntRegs -= neededInt;
1963 freeSSERegs -= neededSSE;
1964 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001965 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001966 }
1967 }
1968}
1969
1970static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1971 QualType Ty,
1972 CodeGenFunction &CGF) {
1973 llvm::Value *overflow_arg_area_p =
1974 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1975 llvm::Value *overflow_arg_area =
1976 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1977
1978 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1979 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1980 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1981 if (Align > 8) {
1982 // Note that we follow the ABI & gcc here, even though the type
1983 // could in theory have an alignment greater than 16. This case
1984 // shouldn't ever matter in practice.
1985
1986 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001987 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001988 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001989 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1990 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001991 CGF.Int64Ty);
1992 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001993 overflow_arg_area =
1994 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1995 overflow_arg_area->getType(),
1996 "overflow_arg_area.align");
1997 }
1998
1999 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002000 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002001 llvm::Value *Res =
2002 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002003 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002004
2005 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2006 // l->overflow_arg_area + sizeof(type).
2007 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2008 // an 8 byte boundary.
2009
2010 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002011 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002012 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002013 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2014 "overflow_arg_area.next");
2015 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2016
2017 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2018 return Res;
2019}
2020
2021llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2022 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002023 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002025 // Assume that va_list type is correct; should be pointer to LLVM type:
2026 // struct {
2027 // i32 gp_offset;
2028 // i32 fp_offset;
2029 // i8* overflow_arg_area;
2030 // i8* reg_save_area;
2031 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002032 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002033
Chris Lattnera14db752010-03-11 18:19:55 +00002034 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002035 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002036
2037 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2038 // in the registers. If not go to step 7.
2039 if (!neededInt && !neededSSE)
2040 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2041
2042 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2043 // general purpose registers needed to pass type and num_fp to hold
2044 // the number of floating point registers needed.
2045
2046 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2047 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2048 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2049 //
2050 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2051 // register save space).
2052
2053 llvm::Value *InRegs = 0;
2054 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2055 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2056 if (neededInt) {
2057 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2058 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002059 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2060 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002061 }
2062
2063 if (neededSSE) {
2064 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2065 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2066 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002067 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2068 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002069 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2070 }
2071
2072 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2073 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2074 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2075 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2076
2077 // Emit code to load the value if it was passed in registers.
2078
2079 CGF.EmitBlock(InRegBlock);
2080
2081 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2082 // an offset of l->gp_offset and/or l->fp_offset. This may require
2083 // copying to a temporary location in case the parameter is passed
2084 // in different register classes or requires an alignment greater
2085 // than 8 for general purpose registers and 16 for XMM registers.
2086 //
2087 // FIXME: This really results in shameful code when we end up needing to
2088 // collect arguments from different places; often what should result in a
2089 // simple assembling of a structure from scattered addresses has many more
2090 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002091 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002092 llvm::Value *RegAddr =
2093 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2094 "reg_save_area");
2095 if (neededInt && neededSSE) {
2096 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002097 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002098 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002099 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2100 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002101 llvm::Type *TyLo = ST->getElementType(0);
2102 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002103 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002104 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002105 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2106 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002107 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2108 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002109 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2110 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002111 llvm::Value *V =
2112 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2113 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2114 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2115 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2116
Owen Andersona1cf15f2009-07-14 23:10:40 +00002117 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002118 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002119 } else if (neededInt) {
2120 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2121 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002122 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002123 } else if (neededSSE == 1) {
2124 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2125 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2126 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002127 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002128 assert(neededSSE == 2 && "Invalid number of needed registers!");
2129 // SSE registers are spaced 16 bytes apart in the register save
2130 // area, we need to collect the two eightbytes together.
2131 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002132 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002133 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002134 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002135 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002136 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002137 DoubleTy, NULL);
2138 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2139 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2140 DblPtrTy));
2141 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2142 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2143 DblPtrTy));
2144 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2145 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2146 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002147 }
2148
2149 // AMD64-ABI 3.5.7p5: Step 5. Set:
2150 // l->gp_offset = l->gp_offset + num_gp * 8
2151 // l->fp_offset = l->fp_offset + num_fp * 16.
2152 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002153 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002154 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2155 gp_offset_p);
2156 }
2157 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002158 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002159 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2160 fp_offset_p);
2161 }
2162 CGF.EmitBranch(ContBlock);
2163
2164 // Emit code to load the value if it was passed in memory.
2165
2166 CGF.EmitBlock(InMemBlock);
2167 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2168
2169 // Return the appropriate result.
2170
2171 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002172 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002173 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002174 ResAddr->addIncoming(RegAddr, InRegBlock);
2175 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002176 return ResAddr;
2177}
2178
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002179ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2180
2181 if (Ty->isVoidType())
2182 return ABIArgInfo::getIgnore();
2183
2184 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2185 Ty = EnumTy->getDecl()->getIntegerType();
2186
2187 uint64_t Size = getContext().getTypeSize(Ty);
2188
2189 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002190 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2191 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002192 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2193
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002194 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2195 if (Size == 128 &&
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002196 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002197 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2198 Size));
2199
2200 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2201 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2202 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002203 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002204 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2205 Size));
2206
2207 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2208 }
2209
2210 if (Ty->isPromotableIntegerType())
2211 return ABIArgInfo::getExtend();
2212
2213 return ABIArgInfo::getDirect();
2214}
2215
2216void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2217
2218 QualType RetTy = FI.getReturnType();
2219 FI.getReturnInfo() = classify(RetTy);
2220
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002221 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2222 it != ie; ++it)
2223 it->info = classify(it->type);
2224}
2225
Chris Lattnerf13721d2010-08-31 16:44:54 +00002226llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2227 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002228 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2229 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002230
Chris Lattnerf13721d2010-08-31 16:44:54 +00002231 CGBuilderTy &Builder = CGF.Builder;
2232 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2233 "ap");
2234 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2235 llvm::Type *PTy =
2236 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2237 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2238
2239 uint64_t Offset =
2240 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2241 llvm::Value *NextAddr =
2242 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2243 "ap.next");
2244 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2245
2246 return AddrTyped;
2247}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002248
John McCallec853ba2010-03-11 00:10:12 +00002249// PowerPC-32
2250
2251namespace {
2252class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2253public:
Chris Lattnerea044322010-07-29 02:01:43 +00002254 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002255
John McCallec853ba2010-03-11 00:10:12 +00002256 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2257 // This is recovered from gcc output.
2258 return 1; // r1 is the dedicated stack pointer
2259 }
2260
2261 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002262 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002263};
2264
2265}
2266
2267bool
2268PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2269 llvm::Value *Address) const {
2270 // This is calculated from the LLVM and GCC tables and verified
2271 // against gcc output. AFAIK all ABIs use the same encoding.
2272
2273 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2274 llvm::LLVMContext &Context = CGF.getLLVMContext();
2275
Chris Lattner2acc6e32011-07-18 04:24:23 +00002276 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002277 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2278 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2279 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2280
2281 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002282 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002283
2284 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002285 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002286
2287 // 64-76 are various 4-byte special-purpose registers:
2288 // 64: mq
2289 // 65: lr
2290 // 66: ctr
2291 // 67: ap
2292 // 68-75 cr0-7
2293 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002294 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002295
2296 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002297 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002298
2299 // 109: vrsave
2300 // 110: vscr
2301 // 111: spe_acc
2302 // 112: spefscr
2303 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002304 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002305
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002306 return false;
John McCallec853ba2010-03-11 00:10:12 +00002307}
2308
2309
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002310//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002311// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002312//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002313
2314namespace {
2315
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002316class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002317public:
2318 enum ABIKind {
2319 APCS = 0,
2320 AAPCS = 1,
2321 AAPCS_VFP
2322 };
2323
2324private:
2325 ABIKind Kind;
2326
2327public:
Chris Lattnerea044322010-07-29 02:01:43 +00002328 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002329
John McCall49e34be2011-08-30 01:42:09 +00002330 bool isEABI() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002331 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall49e34be2011-08-30 01:42:09 +00002332 return (Env == "gnueabi" || Env == "eabi");
2333 }
2334
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002335private:
2336 ABIKind getABIKind() const { return Kind; }
2337
Chris Lattnera3c109b2010-07-29 02:16:43 +00002338 ABIArgInfo classifyReturnType(QualType RetTy) const;
2339 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002340
Chris Lattneree5dcd02010-07-29 02:31:05 +00002341 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002342
2343 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2344 CodeGenFunction &CGF) const;
2345};
2346
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002347class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2348public:
Chris Lattnerea044322010-07-29 02:01:43 +00002349 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2350 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002351
John McCall49e34be2011-08-30 01:42:09 +00002352 const ARMABIInfo &getABIInfo() const {
2353 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2354 }
2355
John McCall6374c332010-03-06 00:35:14 +00002356 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2357 return 13;
2358 }
Roman Divacky09345d12011-05-18 19:36:54 +00002359
Chris Lattner5f9e2722011-07-23 10:55:15 +00002360 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002361 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2362 }
2363
Roman Divacky09345d12011-05-18 19:36:54 +00002364 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2365 llvm::Value *Address) const {
2366 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2367 llvm::LLVMContext &Context = CGF.getLLVMContext();
2368
Chris Lattner2acc6e32011-07-18 04:24:23 +00002369 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002370 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2371
2372 // 0-15 are the 16 integer registers.
2373 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2374
2375 return false;
2376 }
John McCall49e34be2011-08-30 01:42:09 +00002377
2378 unsigned getSizeOfUnwindException() const {
2379 if (getABIInfo().isEABI()) return 88;
2380 return TargetCodeGenInfo::getSizeOfUnwindException();
2381 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002382};
2383
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002384}
2385
Chris Lattneree5dcd02010-07-29 02:31:05 +00002386void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002387 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002388 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002389 it != ie; ++it)
2390 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002391
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002392 // Always honor user-specified calling convention.
2393 if (FI.getCallingConvention() != llvm::CallingConv::C)
2394 return;
2395
2396 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002397 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002398 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002399 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002400 else
2401 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002402
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002403 // If user did not ask for specific calling convention explicitly (e.g. via
2404 // pcs attribute), set effective calling convention if it's different than ABI
2405 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002406 switch (getABIKind()) {
2407 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002408 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2409 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002410 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002411 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002412 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2413 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002414 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002415 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002416 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2417 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002418 break;
2419 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002420}
2421
Bob Wilson194f06a2011-08-03 05:58:22 +00002422/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2423/// aggregate. If HAMembers is non-null, the number of base elements
2424/// contained in the type is returned through it; this is used for the
2425/// recursive calls that check aggregate component types.
2426static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2427 ASTContext &Context,
2428 uint64_t *HAMembers = 0) {
2429 uint64_t Members;
2430 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2431 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2432 return false;
2433 Members *= AT->getSize().getZExtValue();
2434 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2435 const RecordDecl *RD = RT->getDecl();
2436 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2437 return false;
2438 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2439 if (!CXXRD->isAggregate())
2440 return false;
2441 }
2442 Members = 0;
2443 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2444 i != e; ++i) {
2445 const FieldDecl *FD = *i;
2446 uint64_t FldMembers;
2447 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2448 return false;
2449 Members += FldMembers;
2450 }
2451 } else {
2452 Members = 1;
2453 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2454 Members = 2;
2455 Ty = CT->getElementType();
2456 }
2457
2458 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2459 // double, or 64-bit or 128-bit vectors.
2460 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2461 if (BT->getKind() != BuiltinType::Float &&
2462 BT->getKind() != BuiltinType::Double)
2463 return false;
2464 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2465 unsigned VecSize = Context.getTypeSize(VT);
2466 if (VecSize != 64 && VecSize != 128)
2467 return false;
2468 } else {
2469 return false;
2470 }
2471
2472 // The base type must be the same for all members. Vector types of the
2473 // same total size are treated as being equivalent here.
2474 const Type *TyPtr = Ty.getTypePtr();
2475 if (!Base)
2476 Base = TyPtr;
2477 if (Base != TyPtr &&
2478 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2479 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2480 return false;
2481 }
2482
2483 // Homogeneous Aggregates can have at most 4 members of the base type.
2484 if (HAMembers)
2485 *HAMembers = Members;
2486 return (Members <= 4);
2487}
2488
Chris Lattnera3c109b2010-07-29 02:16:43 +00002489ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002490 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002491 // Treat an enum type as its underlying type.
2492 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2493 Ty = EnumTy->getDecl()->getIntegerType();
2494
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002495 return (Ty->isPromotableIntegerType() ?
2496 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002497 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002498
Daniel Dunbar42025572009-09-14 21:54:03 +00002499 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002500 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002501 return ABIArgInfo::getIgnore();
2502
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002503 // Structures with either a non-trivial destructor or a non-trivial
2504 // copy constructor are always indirect.
2505 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2506 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2507
Bob Wilson194f06a2011-08-03 05:58:22 +00002508 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2509 // Homogeneous Aggregates need to be expanded.
2510 const Type *Base = 0;
2511 if (isHomogeneousAggregate(Ty, Base, getContext()))
2512 return ABIArgInfo::getExpand();
2513 }
2514
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002515 // Otherwise, pass by coercing to a structure of the appropriate size.
2516 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002517 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2518 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002519 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002520 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002521 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002522 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002523 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2524 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002525 } else {
2526 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2527 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002528 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002529
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002530 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002531 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002532 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002533}
2534
Chris Lattnera3c109b2010-07-29 02:16:43 +00002535static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002536 llvm::LLVMContext &VMContext) {
2537 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2538 // is called integer-like if its size is less than or equal to one word, and
2539 // the offset of each of its addressable sub-fields is zero.
2540
2541 uint64_t Size = Context.getTypeSize(Ty);
2542
2543 // Check that the type fits in a word.
2544 if (Size > 32)
2545 return false;
2546
2547 // FIXME: Handle vector types!
2548 if (Ty->isVectorType())
2549 return false;
2550
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002551 // Float types are never treated as "integer like".
2552 if (Ty->isRealFloatingType())
2553 return false;
2554
Daniel Dunbar98303b92009-09-13 08:03:58 +00002555 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002556 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002557 return true;
2558
Daniel Dunbar45815812010-02-01 23:31:26 +00002559 // Small complex integer types are "integer like".
2560 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2561 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002562
2563 // Single element and zero sized arrays should be allowed, by the definition
2564 // above, but they are not.
2565
2566 // Otherwise, it must be a record type.
2567 const RecordType *RT = Ty->getAs<RecordType>();
2568 if (!RT) return false;
2569
2570 // Ignore records with flexible arrays.
2571 const RecordDecl *RD = RT->getDecl();
2572 if (RD->hasFlexibleArrayMember())
2573 return false;
2574
2575 // Check that all sub-fields are at offset 0, and are themselves "integer
2576 // like".
2577 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2578
2579 bool HadField = false;
2580 unsigned idx = 0;
2581 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2582 i != e; ++i, ++idx) {
2583 const FieldDecl *FD = *i;
2584
Daniel Dunbar679855a2010-01-29 03:22:29 +00002585 // Bit-fields are not addressable, we only need to verify they are "integer
2586 // like". We still have to disallow a subsequent non-bitfield, for example:
2587 // struct { int : 0; int x }
2588 // is non-integer like according to gcc.
2589 if (FD->isBitField()) {
2590 if (!RD->isUnion())
2591 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002592
Daniel Dunbar679855a2010-01-29 03:22:29 +00002593 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2594 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002595
Daniel Dunbar679855a2010-01-29 03:22:29 +00002596 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002597 }
2598
Daniel Dunbar679855a2010-01-29 03:22:29 +00002599 // Check if this field is at offset 0.
2600 if (Layout.getFieldOffset(idx) != 0)
2601 return false;
2602
Daniel Dunbar98303b92009-09-13 08:03:58 +00002603 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2604 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002605
Daniel Dunbar679855a2010-01-29 03:22:29 +00002606 // Only allow at most one field in a structure. This doesn't match the
2607 // wording above, but follows gcc in situations with a field following an
2608 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002609 if (!RD->isUnion()) {
2610 if (HadField)
2611 return false;
2612
2613 HadField = true;
2614 }
2615 }
2616
2617 return true;
2618}
2619
Chris Lattnera3c109b2010-07-29 02:16:43 +00002620ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002621 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002622 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002623
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002624 // Large vector types should be returned via memory.
2625 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2626 return ABIArgInfo::getIndirect(0);
2627
John McCalld608cdb2010-08-22 10:59:02 +00002628 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002629 // Treat an enum type as its underlying type.
2630 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2631 RetTy = EnumTy->getDecl()->getIntegerType();
2632
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002633 return (RetTy->isPromotableIntegerType() ?
2634 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002635 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002636
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002637 // Structures with either a non-trivial destructor or a non-trivial
2638 // copy constructor are always indirect.
2639 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2640 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2641
Daniel Dunbar98303b92009-09-13 08:03:58 +00002642 // Are we following APCS?
2643 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002644 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002645 return ABIArgInfo::getIgnore();
2646
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002647 // Complex types are all returned as packed integers.
2648 //
2649 // FIXME: Consider using 2 x vector types if the back end handles them
2650 // correctly.
2651 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002652 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002653 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002654
Daniel Dunbar98303b92009-09-13 08:03:58 +00002655 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002656 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002657 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002658 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002659 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002660 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002661 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002662 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2663 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002664 }
2665
2666 // Otherwise return in memory.
2667 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002668 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002669
2670 // Otherwise this is an AAPCS variant.
2671
Chris Lattnera3c109b2010-07-29 02:16:43 +00002672 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002673 return ABIArgInfo::getIgnore();
2674
Daniel Dunbar98303b92009-09-13 08:03:58 +00002675 // Aggregates <= 4 bytes are returned in r0; other aggregates
2676 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002677 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002678 if (Size <= 32) {
2679 // Return in the smallest viable integer type.
2680 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002681 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002682 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002683 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2684 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002685 }
2686
Daniel Dunbar98303b92009-09-13 08:03:58 +00002687 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002688}
2689
2690llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002691 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002692 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2693 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002694
2695 CGBuilderTy &Builder = CGF.Builder;
2696 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2697 "ap");
2698 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002699 // Handle address alignment for type alignment > 32 bits
2700 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2701 if (TyAlign > 4) {
2702 assert((TyAlign & (TyAlign - 1)) == 0 &&
2703 "Alignment is not power of 2!");
2704 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2705 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2706 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2707 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2708 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002709 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002710 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002711 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2712
2713 uint64_t Offset =
2714 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2715 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002716 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002717 "ap.next");
2718 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2719
2720 return AddrTyped;
2721}
2722
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002723//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002724// PTX ABI Implementation
2725//===----------------------------------------------------------------------===//
2726
2727namespace {
2728
2729class PTXABIInfo : public ABIInfo {
2730public:
2731 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2732
2733 ABIArgInfo classifyReturnType(QualType RetTy) const;
2734 ABIArgInfo classifyArgumentType(QualType Ty) const;
2735
2736 virtual void computeInfo(CGFunctionInfo &FI) const;
2737 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2738 CodeGenFunction &CFG) const;
2739};
2740
2741class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2742public:
2743 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2744 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002745
2746 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2747 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002748};
2749
2750ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2751 if (RetTy->isVoidType())
2752 return ABIArgInfo::getIgnore();
2753 if (isAggregateTypeForABI(RetTy))
2754 return ABIArgInfo::getIndirect(0);
2755 return ABIArgInfo::getDirect();
2756}
2757
2758ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2759 if (isAggregateTypeForABI(Ty))
2760 return ABIArgInfo::getIndirect(0);
2761
2762 return ABIArgInfo::getDirect();
2763}
2764
2765void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2766 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2767 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2768 it != ie; ++it)
2769 it->info = classifyArgumentType(it->type);
2770
2771 // Always honor user-specified calling convention.
2772 if (FI.getCallingConvention() != llvm::CallingConv::C)
2773 return;
2774
2775 // Calling convention as default by an ABI.
2776 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002777 const LangOptions &LangOpts = getContext().getLangOptions();
2778 if (LangOpts.OpenCL || LangOpts.CUDA) {
2779 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002780 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002781 } else {
2782 // If we are in standard C/C++ mode, use the triple to decide on the default
2783 StringRef Env =
2784 getContext().getTargetInfo().getTriple().getEnvironmentName();
2785 if (Env == "device")
2786 DefaultCC = llvm::CallingConv::PTX_Device;
2787 else
2788 DefaultCC = llvm::CallingConv::PTX_Kernel;
2789 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002790 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002791
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002792}
2793
2794llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2795 CodeGenFunction &CFG) const {
2796 llvm_unreachable("PTX does not support varargs");
2797 return 0;
2798}
2799
Justin Holewinski818eafb2011-10-05 17:58:44 +00002800void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2801 llvm::GlobalValue *GV,
2802 CodeGen::CodeGenModule &M) const{
2803 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2804 if (!FD) return;
2805
2806 llvm::Function *F = cast<llvm::Function>(GV);
2807
2808 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002809 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002810 // Use OpenCL function attributes to set proper calling conventions
2811 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002812 if (FD->hasAttr<OpenCLKernelAttr>()) {
2813 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002814 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002815 // And kernel functions are not subject to inlining
2816 F->addFnAttr(llvm::Attribute::NoInline);
2817 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002818 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002819
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002820 // Perform special handling in CUDA mode.
2821 if (M.getLangOptions().CUDA) {
2822 // CUDA __global__ functions get a kernel calling convention. Since
2823 // __global__ functions cannot be called from the device, we do not
2824 // need to set the noinline attribute.
2825 if (FD->getAttr<CUDAGlobalAttr>())
2826 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002827 }
2828}
2829
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002830}
2831
2832//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002833// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002834//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002835
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002836namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002837
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002838class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002839public:
2840 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2841
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002842 bool isPromotableIntegerType(QualType Ty) const;
2843
Chris Lattnera3c109b2010-07-29 02:16:43 +00002844 ABIArgInfo classifyReturnType(QualType RetTy) const;
2845 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002846
Chris Lattneree5dcd02010-07-29 02:31:05 +00002847 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002848 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002849 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2850 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002851 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002852 }
2853
2854 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2855 CodeGenFunction &CGF) const;
2856};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002857
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002858class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2859public:
Chris Lattnerea044322010-07-29 02:01:43 +00002860 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2861 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002862};
2863
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002864}
2865
2866bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2867 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002868 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002869 switch (BT->getKind()) {
2870 case BuiltinType::Bool:
2871 case BuiltinType::Char_S:
2872 case BuiltinType::Char_U:
2873 case BuiltinType::SChar:
2874 case BuiltinType::UChar:
2875 case BuiltinType::Short:
2876 case BuiltinType::UShort:
2877 case BuiltinType::Int:
2878 case BuiltinType::UInt:
2879 return true;
2880 default:
2881 return false;
2882 }
2883 return false;
2884}
2885
2886llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2887 CodeGenFunction &CGF) const {
2888 // FIXME: Implement
2889 return 0;
2890}
2891
2892
Chris Lattnera3c109b2010-07-29 02:16:43 +00002893ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2894 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002895 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002896 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002897 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002898
2899 return (isPromotableIntegerType(RetTy) ?
2900 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002901}
2902
Chris Lattnera3c109b2010-07-29 02:16:43 +00002903ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002904 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002905 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002906
2907 return (isPromotableIntegerType(Ty) ?
2908 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002909}
2910
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002911//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002912// MBlaze ABI Implementation
2913//===----------------------------------------------------------------------===//
2914
2915namespace {
2916
2917class MBlazeABIInfo : public ABIInfo {
2918public:
2919 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2920
2921 bool isPromotableIntegerType(QualType Ty) const;
2922
2923 ABIArgInfo classifyReturnType(QualType RetTy) const;
2924 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2925
2926 virtual void computeInfo(CGFunctionInfo &FI) const {
2927 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2928 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2929 it != ie; ++it)
2930 it->info = classifyArgumentType(it->type);
2931 }
2932
2933 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2934 CodeGenFunction &CGF) const;
2935};
2936
2937class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2938public:
2939 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2940 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2941 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2942 CodeGen::CodeGenModule &M) const;
2943};
2944
2945}
2946
2947bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2948 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2949 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2950 switch (BT->getKind()) {
2951 case BuiltinType::Bool:
2952 case BuiltinType::Char_S:
2953 case BuiltinType::Char_U:
2954 case BuiltinType::SChar:
2955 case BuiltinType::UChar:
2956 case BuiltinType::Short:
2957 case BuiltinType::UShort:
2958 return true;
2959 default:
2960 return false;
2961 }
2962 return false;
2963}
2964
2965llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2966 CodeGenFunction &CGF) const {
2967 // FIXME: Implement
2968 return 0;
2969}
2970
2971
2972ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2973 if (RetTy->isVoidType())
2974 return ABIArgInfo::getIgnore();
2975 if (isAggregateTypeForABI(RetTy))
2976 return ABIArgInfo::getIndirect(0);
2977
2978 return (isPromotableIntegerType(RetTy) ?
2979 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2980}
2981
2982ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2983 if (isAggregateTypeForABI(Ty))
2984 return ABIArgInfo::getIndirect(0);
2985
2986 return (isPromotableIntegerType(Ty) ?
2987 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2988}
2989
2990void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2991 llvm::GlobalValue *GV,
2992 CodeGen::CodeGenModule &M)
2993 const {
2994 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2995 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002996
Wesley Peck276fdf42010-12-19 19:57:51 +00002997 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2998 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2999 CC = llvm::CallingConv::MBLAZE_INTR;
3000 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3001 CC = llvm::CallingConv::MBLAZE_SVOL;
3002
3003 if (CC != llvm::CallingConv::C) {
3004 // Handle 'interrupt_handler' attribute:
3005 llvm::Function *F = cast<llvm::Function>(GV);
3006
3007 // Step 1: Set ISR calling convention.
3008 F->setCallingConv(CC);
3009
3010 // Step 2: Add attributes goodness.
3011 F->addFnAttr(llvm::Attribute::NoInline);
3012 }
3013
3014 // Step 3: Emit _interrupt_handler alias.
3015 if (CC == llvm::CallingConv::MBLAZE_INTR)
3016 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3017 "_interrupt_handler", GV, &M.getModule());
3018}
3019
3020
3021//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003022// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003023//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003024
3025namespace {
3026
3027class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3028public:
Chris Lattnerea044322010-07-29 02:01:43 +00003029 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3030 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003031 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3032 CodeGen::CodeGenModule &M) const;
3033};
3034
3035}
3036
3037void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3038 llvm::GlobalValue *GV,
3039 CodeGen::CodeGenModule &M) const {
3040 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3041 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3042 // Handle 'interrupt' attribute:
3043 llvm::Function *F = cast<llvm::Function>(GV);
3044
3045 // Step 1: Set ISR calling convention.
3046 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3047
3048 // Step 2: Add attributes goodness.
3049 F->addFnAttr(llvm::Attribute::NoInline);
3050
3051 // Step 3: Emit ISR vector alias.
3052 unsigned Num = attr->getNumber() + 0xffe0;
3053 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003054 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003055 GV, &M.getModule());
3056 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003057 }
3058}
3059
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003060//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003061// MIPS ABI Implementation. This works for both little-endian and
3062// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003063//===----------------------------------------------------------------------===//
3064
John McCallaeeb7012010-05-27 06:19:26 +00003065namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003066class MipsABIInfo : public ABIInfo {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003067 static const unsigned MinABIStackAlignInBytes = 4;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003068public:
3069 MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3070
3071 ABIArgInfo classifyReturnType(QualType RetTy) const;
3072 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3073 virtual void computeInfo(CGFunctionInfo &FI) const;
3074 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3075 CodeGenFunction &CGF) const;
3076};
3077
Akira Hatanaka3827e422011-08-12 01:43:14 +00003078const unsigned MipsABIInfo::MinABIStackAlignInBytes;
3079
John McCallaeeb7012010-05-27 06:19:26 +00003080class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003081 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003082public:
Akira Hatanakae624fa02011-09-20 18:23:28 +00003083 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, unsigned SZ)
3084 : TargetCodeGenInfo(new MipsABIInfo(CGT)), SizeOfUnwindException(SZ) {}
John McCallaeeb7012010-05-27 06:19:26 +00003085
3086 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3087 return 29;
3088 }
3089
3090 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003091 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003092
3093 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003094 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003095 }
John McCallaeeb7012010-05-27 06:19:26 +00003096};
3097}
3098
Akira Hatanaka619e8872011-06-02 00:09:17 +00003099ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3100 if (isAggregateTypeForABI(Ty)) {
3101 // Ignore empty aggregates.
3102 if (getContext().getTypeSize(Ty) == 0)
3103 return ABIArgInfo::getIgnore();
3104
Akira Hatanaka511949b2011-08-01 18:09:58 +00003105 // Records with non trivial destructors/constructors should not be passed
3106 // by value.
3107 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3108 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3109
Akira Hatanaka619e8872011-06-02 00:09:17 +00003110 return ABIArgInfo::getIndirect(0);
3111 }
3112
3113 // Treat an enum type as its underlying type.
3114 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3115 Ty = EnumTy->getDecl()->getIntegerType();
3116
3117 return (Ty->isPromotableIntegerType() ?
3118 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3119}
3120
3121ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3122 if (RetTy->isVoidType())
3123 return ABIArgInfo::getIgnore();
3124
3125 if (isAggregateTypeForABI(RetTy)) {
3126 if (RetTy->isAnyComplexType())
3127 return ABIArgInfo::getDirect();
3128
3129 return ABIArgInfo::getIndirect(0);
3130 }
3131
3132 // Treat an enum type as its underlying type.
3133 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3134 RetTy = EnumTy->getDecl()->getIntegerType();
3135
3136 return (RetTy->isPromotableIntegerType() ?
3137 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3138}
3139
3140void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3141 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3142 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3143 it != ie; ++it)
3144 it->info = classifyArgumentType(it->type);
3145}
3146
3147llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3148 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003149 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3150 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3151
3152 CGBuilderTy &Builder = CGF.Builder;
3153 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3154 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3155 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3156 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3157 llvm::Value *AddrTyped;
3158
3159 if (TypeAlign > MinABIStackAlignInBytes) {
3160 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3161 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3162 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3163 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3164 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3165 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3166 }
3167 else
3168 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3169
3170 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka7b0a0382011-08-12 02:30:14 +00003171 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003172 uint64_t Offset =
3173 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3174 llvm::Value *NextAddr =
3175 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3176 "ap.next");
3177 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3178
3179 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003180}
3181
John McCallaeeb7012010-05-27 06:19:26 +00003182bool
3183MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3184 llvm::Value *Address) const {
3185 // This information comes from gcc's implementation, which seems to
3186 // as canonical as it gets.
3187
3188 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3189 llvm::LLVMContext &Context = CGF.getLLVMContext();
3190
3191 // Everything on MIPS is 4 bytes. Double-precision FP registers
3192 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003193 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003194 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3195
3196 // 0-31 are the general purpose registers, $0 - $31.
3197 // 32-63 are the floating-point registers, $f0 - $f31.
3198 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3199 // 66 is the (notional, I think) register for signal-handler return.
3200 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3201
3202 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3203 // They are one bit wide and ignored here.
3204
3205 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3206 // (coprocessor 1 is the FP unit)
3207 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3208 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3209 // 176-181 are the DSP accumulator registers.
3210 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3211
3212 return false;
3213}
3214
3215
Chris Lattnerea044322010-07-29 02:01:43 +00003216const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003217 if (TheTargetCodeGenInfo)
3218 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003219
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003220 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003221 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003222 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003223 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003224
John McCallaeeb7012010-05-27 06:19:26 +00003225 case llvm::Triple::mips:
3226 case llvm::Triple::mipsel:
Akira Hatanakae624fa02011-09-20 18:23:28 +00003227 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, 24));
John McCallaeeb7012010-05-27 06:19:26 +00003228
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003229 case llvm::Triple::mips64:
3230 case llvm::Triple::mips64el:
3231 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, 32));
3232
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003233 case llvm::Triple::arm:
3234 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003235 {
3236 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003237
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003238 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003239 Kind = ARMABIInfo::APCS;
3240 else if (CodeGenOpts.FloatABI == "hard")
3241 Kind = ARMABIInfo::AAPCS_VFP;
3242
3243 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3244 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003245
John McCallec853ba2010-03-11 00:10:12 +00003246 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003247 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003248
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003249 case llvm::Triple::ptx32:
3250 case llvm::Triple::ptx64:
3251 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3252
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003253 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00003254 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003255
Wesley Peck276fdf42010-12-19 19:57:51 +00003256 case llvm::Triple::mblaze:
3257 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3258
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003259 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003260 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003261
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003262 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003263 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003264
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003265 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003266 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003267 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003268
3269 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003270 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003271 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003272 case llvm::Triple::AuroraUX:
3273 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003274 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003275 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003276 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003277 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003278 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003279
3280 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003281 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003282 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003283 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003284 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003285
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003286 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003287 switch (Triple.getOS()) {
3288 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003289 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003290 case llvm::Triple::Cygwin:
3291 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3292 default:
3293 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3294 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003295 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003296}