blob: 0c070a19d8b41f1977739858e59bd803fe285bab [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 {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 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=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 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()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " 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
Daniel Dunbar98303b92009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000091
92/// isEmptyField - Return true iff a the field is "empty", that is it
93/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000100
Daniel Dunbar98303b92009-09-13 08:03:58 +0000101 // Constant arrays of empty records count as empty, strip them off.
102 if (AllowArrays)
103 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
104 FT = AT->getElementType();
105
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000106 const RecordType *RT = FT->getAs<RecordType>();
107 if (!RT)
108 return false;
109
110 // C++ record fields are never empty, at least in the Itanium ABI.
111 //
112 // FIXME: We should use a predicate for whether this behavior is true in the
113 // current ABI.
114 if (isa<CXXRecordDecl>(RT->getDecl()))
115 return false;
116
Daniel Dunbar98303b92009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118}
119
120/// isEmptyRecord - Return true iff a structure contains only empty
121/// fields. Note that a structure with a flexible array member is not
122/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000125 if (!RT)
126 return 0;
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000131 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000133 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
134 e = CXXRD->bases_end(); i != e; ++i)
135 if (!isEmptyRecord(Context, i->getType(), true))
136 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000137
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000145/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
146/// a non-trivial destructor or a non-trivial copy constructor.
147static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
148 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
149 if (!RD)
150 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000151
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000152 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
153}
154
155/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
156/// a record type with either a non-trivial destructor or a non-trivial copy
157/// constructor.
158static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
159 const RecordType *RT = T->getAs<RecordType>();
160 if (!RT)
161 return false;
162
163 return hasNonTrivialDestructorOrCopyConstructor(RT);
164}
165
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166/// isSingleElementStruct - Determine if a structure is a "single
167/// element struct", i.e. it has exactly one non-empty field or
168/// exactly one field which is itself a single element
169/// struct. Structures with flexible array members are never
170/// considered single element structs.
171///
172/// \return The field declaration for the single non-empty field, if
173/// it exists.
174static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
175 const RecordType *RT = T->getAsStructureType();
176 if (!RT)
177 return 0;
178
179 const RecordDecl *RD = RT->getDecl();
180 if (RD->hasFlexibleArrayMember())
181 return 0;
182
183 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000184
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000185 // If this is a C++ record, check the bases first.
186 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
187 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
188 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000191 continue;
192
193 // If we already found an element then this isn't a single-element struct.
194 if (Found)
195 return 0;
196
197 // If this is non-empty and not a single element struct, the composite
198 // cannot be a single element struct.
199 Found = isSingleElementStruct(i->getType(), Context);
200 if (!Found)
201 return 0;
202 }
203 }
204
205 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000213 continue;
214
215 // If we already found an element then this isn't a single-element
216 // struct.
217 if (Found)
218 return 0;
219
220 // Treat single element arrays as the element.
221 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
222 if (AT->getSize().getZExtValue() != 1)
223 break;
224 FT = AT->getElementType();
225 }
226
John McCalld608cdb2010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 Found = FT.getTypePtr();
229 } else {
230 Found = isSingleElementStruct(FT, Context);
231 if (!Found)
232 return 0;
233 }
234 }
235
236 return Found;
237}
238
239static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000243 return false;
244
245 uint64_t Size = Context.getTypeSize(Ty);
246 return Size == 32 || Size == 64;
247}
248
Daniel Dunbar53012f42009-11-09 01:33:53 +0000249/// canExpandIndirectArgument - Test whether an argument type which is to be
250/// passed indirectly (on the stack) would have the equivalent layout if it was
251/// expanded into separate arguments. If so, we prefer to do the latter to avoid
252/// inhibiting optimizations.
253///
254// FIXME: This predicate is missing many cases, currently it just follows
255// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
256// should probably make this smarter, or better yet make the LLVM backend
257// capable of handling it.
258static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
259 // We can only expand structure types.
260 const RecordType *RT = Ty->getAs<RecordType>();
261 if (!RT)
262 return false;
263
264 // We can only expand (C) structures.
265 //
266 // FIXME: This needs to be generalized to handle classes as well.
267 const RecordDecl *RD = RT->getDecl();
268 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
269 return false;
270
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000273 const FieldDecl *FD = *i;
274
275 if (!is32Or64BitBasicType(FD->getType(), Context))
276 return false;
277
278 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
279 // how to expand them yet, and the predicate for telling if a bitfield still
280 // counts as "basic" is more complicated than what we were doing previously.
281 if (FD->isBitField())
282 return false;
283 }
284
285 return true;
286}
287
288namespace {
289/// DefaultABIInfo - The default implementation for ABI specific
290/// details. This implementation provides information which results in
291/// self-consistent and sensible LLVM IR generation, but does not
292/// conform to any particular ABI.
293class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000296
Chris Lattnera3c109b2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000299
Chris Lattneree5dcd02010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattnerea044322010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000315};
316
317llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const {
319 return 0;
320}
321
Chris Lattnera3c109b2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000325
Chris Lattnera14db752010-03-11 18:19:55 +0000326 // Treat an enum type as its underlying type.
327 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
328 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000329
Chris Lattnera14db752010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000332}
333
Bob Wilson0024f942011-01-10 23:54:17 +0000334ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
335 if (RetTy->isVoidType())
336 return ABIArgInfo::getIgnore();
337
338 if (isAggregateTypeForABI(RetTy))
339 return ABIArgInfo::getIndirect(0);
340
341 // Treat an enum type as its underlying type.
342 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
343 RetTy = EnumTy->getDecl()->getIntegerType();
344
345 return (RetTy->isPromotableIntegerType() ?
346 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
347}
348
Bill Wendlingbb465d72010-10-18 03:41:31 +0000349/// UseX86_MMXType - Return true if this is an MMX type that should use the special
350/// x86_mmx type.
351bool UseX86_MMXType(const llvm::Type *IRType) {
352 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
353 // special x86_mmx type.
354 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
355 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
356 IRType->getScalarSizeInBits() != 64;
357}
358
Jay Foadef6de3d2011-07-11 09:56:20 +0000359static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
360 llvm::StringRef Constraint,
361 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000362 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000363 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
364 return Ty;
365}
366
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000367//===----------------------------------------------------------------------===//
368// X86-32 ABI Implementation
369//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000370
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000371/// X86_32ABIInfo - The X86-32 ABI information.
372class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000373 static const unsigned MinABIStackAlignInBytes = 4;
374
David Chisnall1e4249c2009-08-17 23:08:21 +0000375 bool IsDarwinVectorABI;
376 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000377 bool IsMMXDisabled;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000378
379 static bool isRegisterSize(unsigned Size) {
380 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
381 }
382
383 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
384
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000385 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
386 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000387 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000388
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000389 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000390 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000391
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000392public:
Chris Lattnerea044322010-07-29 02:01:43 +0000393
Chris Lattnera3c109b2010-07-29 02:16:43 +0000394 ABIArgInfo classifyReturnType(QualType RetTy) const;
395 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000396
Chris Lattneree5dcd02010-07-29 02:31:05 +0000397 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000398 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000399 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
400 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000401 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000402 }
403
404 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
405 CodeGenFunction &CGF) const;
406
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000407 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
408 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
409 IsMMXDisabled(m) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000410};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000411
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000412class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
413public:
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000414 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
415 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000416
417 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
418 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000419
420 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
421 // Darwin uses different dwarf register numbers for EH.
422 if (CGM.isTargetDarwin()) return 5;
423
424 return 4;
425 }
426
427 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
428 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000429
Jay Foadef6de3d2011-07-11 09:56:20 +0000430 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
431 llvm::StringRef Constraint,
432 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000433 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
434 }
435
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000436};
437
438}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000439
440/// shouldReturnTypeInRegister - Determine if the given type should be
441/// passed in a register (for the Darwin ABI).
442bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
443 ASTContext &Context) {
444 uint64_t Size = Context.getTypeSize(Ty);
445
446 // Type must be register sized.
447 if (!isRegisterSize(Size))
448 return false;
449
450 if (Ty->isVectorType()) {
451 // 64- and 128- bit vectors inside structures are not returned in
452 // registers.
453 if (Size == 64 || Size == 128)
454 return false;
455
456 return true;
457 }
458
Daniel Dunbar77115232010-05-15 00:00:30 +0000459 // If this is a builtin, pointer, enum, complex type, member pointer, or
460 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000461 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000462 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000463 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000464 return true;
465
466 // Arrays are treated like records.
467 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
468 return shouldReturnTypeInRegister(AT->getElementType(), Context);
469
470 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000471 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000472 if (!RT) return false;
473
Anders Carlssona8874232010-01-27 03:25:19 +0000474 // FIXME: Traverse bases here too.
475
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000476 // Structure types are passed in register if all fields would be
477 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000478 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
479 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000480 const FieldDecl *FD = *i;
481
482 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000483 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000484 continue;
485
486 // Check fields recursively.
487 if (!shouldReturnTypeInRegister(FD->getType(), Context))
488 return false;
489 }
490
491 return true;
492}
493
Chris Lattnera3c109b2010-07-29 02:16:43 +0000494ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
495 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000496 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000497
Chris Lattnera3c109b2010-07-29 02:16:43 +0000498 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000499 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000500 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000501 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000502
503 // 128-bit vectors are a special case; they are returned in
504 // registers and we need to make sure to pick a type the LLVM
505 // backend will like.
506 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000507 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000508 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000509
510 // Always return in register if it fits in a general purpose
511 // register, or if it is 64 bits and has a single element.
512 if ((Size == 8 || Size == 16 || Size == 32) ||
513 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000514 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000515 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000516
517 return ABIArgInfo::getIndirect(0);
518 }
519
520 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000521 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000522
John McCalld608cdb2010-08-22 10:59:02 +0000523 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000524 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000525 // Structures with either a non-trivial destructor or a non-trivial
526 // copy constructor are always indirect.
527 if (hasNonTrivialDestructorOrCopyConstructor(RT))
528 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000529
Anders Carlsson40092972009-10-20 22:07:59 +0000530 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000531 if (RT->getDecl()->hasFlexibleArrayMember())
532 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000533 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000534
David Chisnall1e4249c2009-08-17 23:08:21 +0000535 // If specified, structs and unions are always indirect.
536 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 return ABIArgInfo::getIndirect(0);
538
539 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000540 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000541 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000542 if (BT->isIntegerType()) {
543 // We need to use the size of the structure, padding
544 // bit-fields can adjust that to be larger than the single
545 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000546 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000547 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000548 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
549 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000550
Chris Lattnera3c109b2010-07-29 02:16:43 +0000551 if (BT->getKind() == BuiltinType::Float) {
552 assert(getContext().getTypeSize(RetTy) ==
553 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000554 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000555 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000556 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000557
Chris Lattnera3c109b2010-07-29 02:16:43 +0000558 if (BT->getKind() == BuiltinType::Double) {
559 assert(getContext().getTypeSize(RetTy) ==
560 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000561 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000562 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563 }
564 } else if (SeltTy->isPointerType()) {
565 // FIXME: It would be really nice if this could come out as the proper
566 // pointer type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000567 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000568 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000569 } else if (SeltTy->isVectorType()) {
570 // 64- and 128-bit vectors are never returned in a
571 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000572 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000573 if (Size == 64 || Size == 128)
574 return ABIArgInfo::getIndirect(0);
575
Chris Lattnera3c109b2010-07-29 02:16:43 +0000576 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000577 }
578 }
579
580 // Small structures which are register sized are generally returned
581 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000582 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
583 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000584 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000585 }
586
587 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000588 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000589
Chris Lattnera3c109b2010-07-29 02:16:43 +0000590 // Treat an enum type as its underlying type.
591 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
592 RetTy = EnumTy->getDecl()->getIntegerType();
593
594 return (RetTy->isPromotableIntegerType() ?
595 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000596}
597
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000598static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
599 const RecordType *RT = Ty->getAs<RecordType>();
600 if (!RT)
601 return 0;
602 const RecordDecl *RD = RT->getDecl();
603
604 // If this is a C++ record, check the bases first.
605 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
606 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
607 e = CXXRD->bases_end(); i != e; ++i)
608 if (!isRecordWithSSEVectorType(Context, i->getType()))
609 return false;
610
611 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
612 i != e; ++i) {
613 QualType FT = i->getType();
614
615 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
616 return true;
617
618 if (isRecordWithSSEVectorType(Context, FT))
619 return true;
620 }
621
622 return false;
623}
624
Daniel Dunbare59d8582010-09-16 20:42:06 +0000625unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
626 unsigned Align) const {
627 // Otherwise, if the alignment is less than or equal to the minimum ABI
628 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000629 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000630 return 0; // Use default alignment.
631
632 // On non-Darwin, the stack type alignment is always 4.
633 if (!IsDarwinVectorABI) {
634 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000635 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000636 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000637
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000638 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
639 if (isRecordWithSSEVectorType(getContext(), Ty))
640 return 16;
641
642 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000643}
644
Chris Lattnera3c109b2010-07-29 02:16:43 +0000645ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000646 if (!ByVal)
647 return ABIArgInfo::getIndirect(0, false);
648
Daniel Dunbare59d8582010-09-16 20:42:06 +0000649 // Compute the byval alignment.
650 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
651 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
652 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000653 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000654
655 // If the stack alignment is less than the type alignment, realign the
656 // argument.
657 if (StackAlign < TypeAlign)
658 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
659 /*Realign=*/true);
660
661 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000662}
663
Chris Lattnera3c109b2010-07-29 02:16:43 +0000664ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000665 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000666 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000667 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000668 if (const RecordType *RT = Ty->getAs<RecordType>()) {
669 // Structures with either a non-trivial destructor or a non-trivial
670 // copy constructor are always indirect.
671 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000672 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000673
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000674 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000675 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000676 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677
678 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000679 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000680 return ABIArgInfo::getIgnore();
681
Daniel Dunbar53012f42009-11-09 01:33:53 +0000682 // Expand small (<= 128-bit) record types when we know that the stack layout
683 // of those arguments will match the struct. This is important because the
684 // LLVM backend isn't smart enough to remove byval, which inhibits many
685 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000686 if (getContext().getTypeSize(Ty) <= 4*32 &&
687 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000688 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000689
Chris Lattnera3c109b2010-07-29 02:16:43 +0000690 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000691 }
692
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000693 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000694 // On Darwin, some vectors are passed in memory, we handle this by passing
695 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000696 if (IsDarwinVectorABI) {
697 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000698 if ((Size == 8 || Size == 16 || Size == 32) ||
699 (Size == 64 && VT->getNumElements() == 1))
700 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
701 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000702 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000703
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000704 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000705 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000706 if (IsMMXDisabled)
707 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
708 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000709 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
710 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
711 return AAI;
712 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000713
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000714 return ABIArgInfo::getDirect();
715 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000716
717
Chris Lattnera3c109b2010-07-29 02:16:43 +0000718 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
719 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000720
Chris Lattnera3c109b2010-07-29 02:16:43 +0000721 return (Ty->isPromotableIntegerType() ?
722 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000723}
724
725llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
726 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000727 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000728 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000729
730 CGBuilderTy &Builder = CGF.Builder;
731 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
732 "ap");
733 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
734 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000735 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000736 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
737
738 uint64_t Offset =
739 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
740 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000741 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000742 "ap.next");
743 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
744
745 return AddrTyped;
746}
747
Charles Davis74f72932010-02-13 15:54:06 +0000748void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
749 llvm::GlobalValue *GV,
750 CodeGen::CodeGenModule &CGM) const {
751 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
752 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
753 // Get the LLVM function.
754 llvm::Function *Fn = cast<llvm::Function>(GV);
755
756 // Now add the 'alignstack' attribute with a value of 16.
757 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
758 }
759 }
760}
761
John McCall6374c332010-03-06 00:35:14 +0000762bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
763 CodeGen::CodeGenFunction &CGF,
764 llvm::Value *Address) const {
765 CodeGen::CGBuilderTy &Builder = CGF.Builder;
766 llvm::LLVMContext &Context = CGF.getLLVMContext();
767
768 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
769 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000770
John McCall6374c332010-03-06 00:35:14 +0000771 // 0-7 are the eight integer registers; the order is different
772 // on Darwin (for EH), but the range is the same.
773 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000774 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000775
776 if (CGF.CGM.isTargetDarwin()) {
777 // 12-16 are st(0..4). Not sure why we stop at 4.
778 // These have size 16, which is sizeof(long double) on
779 // platforms with 8-byte alignment for that type.
780 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000781 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000782
John McCall6374c332010-03-06 00:35:14 +0000783 } else {
784 // 9 is %eflags, which doesn't get a size on Darwin for some
785 // reason.
786 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
787
788 // 11-16 are st(0..5). Not sure why we stop at 5.
789 // These have size 12, which is sizeof(long double) on
790 // platforms with 4-byte alignment for that type.
791 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000792 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
793 }
John McCall6374c332010-03-06 00:35:14 +0000794
795 return false;
796}
797
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000798//===----------------------------------------------------------------------===//
799// X86-64 ABI Implementation
800//===----------------------------------------------------------------------===//
801
802
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000803namespace {
804/// X86_64ABIInfo - The X86_64 ABI information.
805class X86_64ABIInfo : public ABIInfo {
806 enum Class {
807 Integer = 0,
808 SSE,
809 SSEUp,
810 X87,
811 X87Up,
812 ComplexX87,
813 NoClass,
814 Memory
815 };
816
817 /// merge - Implement the X86_64 ABI merging algorithm.
818 ///
819 /// Merge an accumulating classification \arg Accum with a field
820 /// classification \arg Field.
821 ///
822 /// \param Accum - The accumulating classification. This should
823 /// always be either NoClass or the result of a previous merge
824 /// call. In addition, this should never be Memory (the caller
825 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000826 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000827
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000828 /// postMerge - Implement the X86_64 ABI post merging algorithm.
829 ///
830 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
831 /// final MEMORY or SSE classes when necessary.
832 ///
833 /// \param AggregateSize - The size of the current aggregate in
834 /// the classification process.
835 ///
836 /// \param Lo - The classification for the parts of the type
837 /// residing in the low word of the containing object.
838 ///
839 /// \param Hi - The classification for the parts of the type
840 /// residing in the higher words of the containing object.
841 ///
842 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
843
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000844 /// classify - Determine the x86_64 register classes in which the
845 /// given type T should be passed.
846 ///
847 /// \param Lo - The classification for the parts of the type
848 /// residing in the low word of the containing object.
849 ///
850 /// \param Hi - The classification for the parts of the type
851 /// residing in the high word of the containing object.
852 ///
853 /// \param OffsetBase - The bit offset of this type in the
854 /// containing object. Some parameters are classified different
855 /// depending on whether they straddle an eightbyte boundary.
856 ///
857 /// If a word is unused its result will be NoClass; if a type should
858 /// be passed in Memory then at least the classification of \arg Lo
859 /// will be Memory.
860 ///
861 /// The \arg Lo class will be NoClass iff the argument is ignored.
862 ///
863 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
864 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000865 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000866
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000867 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000868 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
869 unsigned IROffset, QualType SourceTy,
870 unsigned SourceOffset) const;
871 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
872 unsigned IROffset, QualType SourceTy,
873 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000874
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000875 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000876 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000877 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000878
879 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000880 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000881 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000882
Chris Lattnera3c109b2010-07-29 02:16:43 +0000883 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000884
Bill Wendlingbb465d72010-10-18 03:41:31 +0000885 ABIArgInfo classifyArgumentType(QualType Ty,
886 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000887 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000888
John McCall67a57732011-04-21 01:20:55 +0000889 /// The 0.98 ABI revision clarified a lot of ambiguities,
890 /// unfortunately in ways that were not always consistent with
891 /// certain previous compilers. In particular, platforms which
892 /// required strict binary compatibility with older versions of GCC
893 /// may need to exempt themselves.
894 bool honorsRevision0_98() const {
895 return !getContext().Target.getTriple().isOSDarwin();
896 }
897
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000898public:
Chris Lattnerea044322010-07-29 02:01:43 +0000899 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000900
Chris Lattneree5dcd02010-07-29 02:31:05 +0000901 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000902
903 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
904 CodeGenFunction &CGF) const;
905};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000906
Chris Lattnerf13721d2010-08-31 16:44:54 +0000907/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000908class WinX86_64ABIInfo : public ABIInfo {
909
910 ABIArgInfo classify(QualType Ty) const;
911
Chris Lattnerf13721d2010-08-31 16:44:54 +0000912public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000913 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
914
915 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000916
917 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
918 CodeGenFunction &CGF) const;
919};
920
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000921class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
922public:
Chris Lattnerea044322010-07-29 02:01:43 +0000923 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
924 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000925
926 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
927 return 7;
928 }
929
930 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
931 llvm::Value *Address) const {
932 CodeGen::CGBuilderTy &Builder = CGF.Builder;
933 llvm::LLVMContext &Context = CGF.getLLVMContext();
934
935 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
936 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000937
John McCallaeeb7012010-05-27 06:19:26 +0000938 // 0-15 are the 16 integer registers.
939 // 16 is %rip.
940 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000941
942 return false;
943 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000944
Jay Foadef6de3d2011-07-11 09:56:20 +0000945 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
946 llvm::StringRef Constraint,
947 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000948 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
949 }
950
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000951};
952
Chris Lattnerf13721d2010-08-31 16:44:54 +0000953class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
954public:
955 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
956 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
957
958 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
959 return 7;
960 }
961
962 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
963 llvm::Value *Address) const {
964 CodeGen::CGBuilderTy &Builder = CGF.Builder;
965 llvm::LLVMContext &Context = CGF.getLLVMContext();
966
967 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
968 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000969
Chris Lattnerf13721d2010-08-31 16:44:54 +0000970 // 0-15 are the 16 integer registers.
971 // 16 is %rip.
972 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
973
974 return false;
975 }
976};
977
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000978}
979
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000980void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
981 Class &Hi) const {
982 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
983 //
984 // (a) If one of the classes is Memory, the whole argument is passed in
985 // memory.
986 //
987 // (b) If X87UP is not preceded by X87, the whole argument is passed in
988 // memory.
989 //
990 // (c) If the size of the aggregate exceeds two eightbytes and the first
991 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
992 // argument is passed in memory. NOTE: This is necessary to keep the
993 // ABI working for processors that don't support the __m256 type.
994 //
995 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
996 //
997 // Some of these are enforced by the merging logic. Others can arise
998 // only with unions; for example:
999 // union { _Complex double; unsigned; }
1000 //
1001 // Note that clauses (b) and (c) were added in 0.98.
1002 //
1003 if (Hi == Memory)
1004 Lo = Memory;
1005 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1006 Lo = Memory;
1007 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1008 Lo = Memory;
1009 if (Hi == SSEUp && Lo != SSE)
1010 Hi = SSE;
1011}
1012
Chris Lattner1090a9b2010-06-28 21:43:59 +00001013X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001014 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1015 // classified recursively so that always two fields are
1016 // considered. The resulting class is calculated according to
1017 // the classes of the fields in the eightbyte:
1018 //
1019 // (a) If both classes are equal, this is the resulting class.
1020 //
1021 // (b) If one of the classes is NO_CLASS, the resulting class is
1022 // the other class.
1023 //
1024 // (c) If one of the classes is MEMORY, the result is the MEMORY
1025 // class.
1026 //
1027 // (d) If one of the classes is INTEGER, the result is the
1028 // INTEGER.
1029 //
1030 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1031 // MEMORY is used as class.
1032 //
1033 // (f) Otherwise class SSE is used.
1034
1035 // Accum should never be memory (we should have returned) or
1036 // ComplexX87 (because this cannot be passed in a structure).
1037 assert((Accum != Memory && Accum != ComplexX87) &&
1038 "Invalid accumulated classification during merge.");
1039 if (Accum == Field || Field == NoClass)
1040 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001041 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001043 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001044 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001045 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001046 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001047 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1048 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001049 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001050 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001051}
1052
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001053void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001054 Class &Lo, Class &Hi) const {
1055 // FIXME: This code can be simplified by introducing a simple value class for
1056 // Class pairs with appropriate constructor methods for the various
1057 // situations.
1058
1059 // FIXME: Some of the split computations are wrong; unaligned vectors
1060 // shouldn't be passed in registers for example, so there is no chance they
1061 // can straddle an eightbyte. Verify & simplify.
1062
1063 Lo = Hi = NoClass;
1064
1065 Class &Current = OffsetBase < 64 ? Lo : Hi;
1066 Current = Memory;
1067
John McCall183700f2009-09-21 23:43:11 +00001068 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001069 BuiltinType::Kind k = BT->getKind();
1070
1071 if (k == BuiltinType::Void) {
1072 Current = NoClass;
1073 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1074 Lo = Integer;
1075 Hi = Integer;
1076 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1077 Current = Integer;
1078 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1079 Current = SSE;
1080 } else if (k == BuiltinType::LongDouble) {
1081 Lo = X87;
1082 Hi = X87Up;
1083 }
1084 // FIXME: _Decimal32 and _Decimal64 are SSE.
1085 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001086 return;
1087 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001088
Chris Lattner1090a9b2010-06-28 21:43:59 +00001089 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001090 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001091 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001092 return;
1093 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001094
Chris Lattner1090a9b2010-06-28 21:43:59 +00001095 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001096 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001097 return;
1098 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001099
Chris Lattner1090a9b2010-06-28 21:43:59 +00001100 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001101 if (Ty->isMemberFunctionPointerType())
1102 Lo = Hi = Integer;
1103 else
1104 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001105 return;
1106 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001107
Chris Lattner1090a9b2010-06-28 21:43:59 +00001108 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001109 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001110 if (Size == 32) {
1111 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1112 // float> as integer.
1113 Current = Integer;
1114
1115 // If this type crosses an eightbyte boundary, it should be
1116 // split.
1117 uint64_t EB_Real = (OffsetBase) / 64;
1118 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1119 if (EB_Real != EB_Imag)
1120 Hi = Lo;
1121 } else if (Size == 64) {
1122 // gcc passes <1 x double> in memory. :(
1123 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1124 return;
1125
1126 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001127 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001128 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1129 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1130 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001131 Current = Integer;
1132 else
1133 Current = SSE;
1134
1135 // If this type crosses an eightbyte boundary, it should be
1136 // split.
1137 if (OffsetBase && OffsetBase != 64)
1138 Hi = Lo;
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001139 } else if (Size == 128 | Size == 256) {
1140 // Arguments of 256-bits are split into four eightbyte chunks. The
1141 // least significant one belongs to class SSE and all the others to class
1142 // SSEUP. The original Lo and Hi design considers that types can't be
1143 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1144 // This design isn't correct for 256-bits, but since there're no cases
1145 // where the upper parts would need to be inspected, avoid adding
1146 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001147 Lo = SSE;
1148 Hi = SSEUp;
1149 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001150 return;
1151 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001152
Chris Lattner1090a9b2010-06-28 21:43:59 +00001153 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001154 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001155
Chris Lattnerea044322010-07-29 02:01:43 +00001156 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001157 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158 if (Size <= 64)
1159 Current = Integer;
1160 else if (Size <= 128)
1161 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001162 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001163 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001164 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001165 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001166 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001167 Current = ComplexX87;
1168
1169 // If this complex type crosses an eightbyte boundary then it
1170 // should be split.
1171 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001172 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001173 if (Hi == NoClass && EB_Real != EB_Imag)
1174 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001175
Chris Lattner1090a9b2010-06-28 21:43:59 +00001176 return;
1177 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001178
Chris Lattnerea044322010-07-29 02:01:43 +00001179 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001180 // Arrays are treated like structures.
1181
Chris Lattnerea044322010-07-29 02:01:43 +00001182 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001183
1184 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001185 // than four eightbytes, ..., it has class MEMORY.
1186 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001187 return;
1188
1189 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1190 // fields, it has class MEMORY.
1191 //
1192 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001193 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001194 return;
1195
1196 // Otherwise implement simplified merge. We could be smarter about
1197 // this, but it isn't worth it and would be harder to verify.
1198 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001199 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001200 uint64_t ArraySize = AT->getSize().getZExtValue();
1201 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1202 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001203 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001204 Lo = merge(Lo, FieldLo);
1205 Hi = merge(Hi, FieldHi);
1206 if (Lo == Memory || Hi == Memory)
1207 break;
1208 }
1209
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001210 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001211 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001212 return;
1213 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001214
Chris Lattner1090a9b2010-06-28 21:43:59 +00001215 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001216 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001217
1218 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001219 // than four eightbytes, ..., it has class MEMORY.
1220 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001221 return;
1222
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001223 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1224 // copy constructor or a non-trivial destructor, it is passed by invisible
1225 // reference.
1226 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1227 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001228
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001229 const RecordDecl *RD = RT->getDecl();
1230
1231 // Assume variable sized types are passed in memory.
1232 if (RD->hasFlexibleArrayMember())
1233 return;
1234
Chris Lattnerea044322010-07-29 02:01:43 +00001235 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236
1237 // Reset Lo class, this will be recomputed.
1238 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001239
1240 // If this is a C++ record, classify the bases first.
1241 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1242 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1243 e = CXXRD->bases_end(); i != e; ++i) {
1244 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1245 "Unexpected base class!");
1246 const CXXRecordDecl *Base =
1247 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1248
1249 // Classify this field.
1250 //
1251 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1252 // single eightbyte, each is classified separately. Each eightbyte gets
1253 // initialized to class NO_CLASS.
1254 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001255 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001256 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001257 Lo = merge(Lo, FieldLo);
1258 Hi = merge(Hi, FieldHi);
1259 if (Lo == Memory || Hi == Memory)
1260 break;
1261 }
1262 }
1263
1264 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001265 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001266 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1267 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001268 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1269 bool BitField = i->isBitField();
1270
1271 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1272 // fields, it has class MEMORY.
1273 //
1274 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001275 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001276 Lo = Memory;
1277 return;
1278 }
1279
1280 // Classify this field.
1281 //
1282 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1283 // exceeds a single eightbyte, each is classified
1284 // separately. Each eightbyte gets initialized to class
1285 // NO_CLASS.
1286 Class FieldLo, FieldHi;
1287
1288 // Bit-fields require special handling, they do not force the
1289 // structure to be passed in memory even if unaligned, and
1290 // therefore they can straddle an eightbyte.
1291 if (BitField) {
1292 // Ignore padding bit-fields.
1293 if (i->isUnnamedBitfield())
1294 continue;
1295
1296 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001297 uint64_t Size =
1298 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001299
1300 uint64_t EB_Lo = Offset / 64;
1301 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1302 FieldLo = FieldHi = NoClass;
1303 if (EB_Lo) {
1304 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1305 FieldLo = NoClass;
1306 FieldHi = Integer;
1307 } else {
1308 FieldLo = Integer;
1309 FieldHi = EB_Hi ? Integer : NoClass;
1310 }
1311 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001312 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001313 Lo = merge(Lo, FieldLo);
1314 Hi = merge(Hi, FieldHi);
1315 if (Lo == Memory || Hi == Memory)
1316 break;
1317 }
1318
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001319 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001320 }
1321}
1322
Chris Lattner9c254f02010-06-29 06:01:59 +00001323ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001324 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1325 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001326 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001327 // Treat an enum type as its underlying type.
1328 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1329 Ty = EnumTy->getDecl()->getIntegerType();
1330
1331 return (Ty->isPromotableIntegerType() ?
1332 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1333 }
1334
1335 return ABIArgInfo::getIndirect(0);
1336}
1337
Chris Lattner9c254f02010-06-29 06:01:59 +00001338ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001339 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1340 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001341 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001342 // Treat an enum type as its underlying type.
1343 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1344 Ty = EnumTy->getDecl()->getIntegerType();
1345
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001346 return (Ty->isPromotableIntegerType() ?
1347 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001348 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001349
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001350 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1351 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001352
Chris Lattner855d2272011-05-22 23:21:23 +00001353 // Compute the byval alignment. We specify the alignment of the byval in all
1354 // cases so that the mid-level optimizer knows the alignment of the byval.
1355 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1356 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001357}
1358
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001359/// GetByteVectorType - The ABI specifies that a value should be passed in an
1360/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001361/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001362llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001363 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001364
Chris Lattner15842bd2010-07-29 05:02:29 +00001365 // Wrapper structs that just contain vectors are passed just like vectors,
1366 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001367 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001368 while (STy && STy->getNumElements() == 1) {
1369 IRType = STy->getElementType(0);
1370 STy = dyn_cast<llvm::StructType>(IRType);
1371 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001372
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001373 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001374 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1375 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001376 unsigned BitWidth = VT->getBitWidth();
1377 if ((BitWidth == 128 || BitWidth == 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001378 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1379 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1380 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1381 EltTy->isIntegerTy(128)))
1382 return VT;
1383 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001384
Chris Lattner0f408f52010-07-29 04:56:46 +00001385 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1386}
1387
Chris Lattnere2962be2010-07-29 07:30:00 +00001388/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1389/// is known to either be off the end of the specified type or being in
1390/// alignment padding. The user type specified is known to be at most 128 bits
1391/// in size, and have passed through X86_64ABIInfo::classify with a successful
1392/// classification that put one of the two halves in the INTEGER class.
1393///
1394/// It is conservatively correct to return false.
1395static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1396 unsigned EndBit, ASTContext &Context) {
1397 // If the bytes being queried are off the end of the type, there is no user
1398 // data hiding here. This handles analysis of builtins, vectors and other
1399 // types that don't contain interesting padding.
1400 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1401 if (TySize <= StartBit)
1402 return true;
1403
Chris Lattner021c3a32010-07-29 07:43:55 +00001404 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1405 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1406 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1407
1408 // Check each element to see if the element overlaps with the queried range.
1409 for (unsigned i = 0; i != NumElts; ++i) {
1410 // If the element is after the span we care about, then we're done..
1411 unsigned EltOffset = i*EltSize;
1412 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001413
Chris Lattner021c3a32010-07-29 07:43:55 +00001414 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1415 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1416 EndBit-EltOffset, Context))
1417 return false;
1418 }
1419 // If it overlaps no elements, then it is safe to process as padding.
1420 return true;
1421 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001422
Chris Lattnere2962be2010-07-29 07:30:00 +00001423 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1424 const RecordDecl *RD = RT->getDecl();
1425 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001426
Chris Lattnere2962be2010-07-29 07:30:00 +00001427 // If this is a C++ record, check the bases first.
1428 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1429 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1430 e = CXXRD->bases_end(); i != e; ++i) {
1431 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1432 "Unexpected base class!");
1433 const CXXRecordDecl *Base =
1434 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001435
Chris Lattnere2962be2010-07-29 07:30:00 +00001436 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001437 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001438 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001439
Chris Lattnere2962be2010-07-29 07:30:00 +00001440 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1441 if (!BitsContainNoUserData(i->getType(), BaseStart,
1442 EndBit-BaseOffset, Context))
1443 return false;
1444 }
1445 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001446
Chris Lattnere2962be2010-07-29 07:30:00 +00001447 // Verify that no field has data that overlaps the region of interest. Yes
1448 // this could be sped up a lot by being smarter about queried fields,
1449 // however we're only looking at structs up to 16 bytes, so we don't care
1450 // much.
1451 unsigned idx = 0;
1452 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1453 i != e; ++i, ++idx) {
1454 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001455
Chris Lattnere2962be2010-07-29 07:30:00 +00001456 // If we found a field after the region we care about, then we're done.
1457 if (FieldOffset >= EndBit) break;
1458
1459 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1460 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1461 Context))
1462 return false;
1463 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001464
Chris Lattnere2962be2010-07-29 07:30:00 +00001465 // If nothing in this record overlapped the area of interest, then we're
1466 // clean.
1467 return true;
1468 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001469
Chris Lattnere2962be2010-07-29 07:30:00 +00001470 return false;
1471}
1472
Chris Lattner0b362002010-07-29 18:39:32 +00001473/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1474/// float member at the specified offset. For example, {int,{float}} has a
1475/// float at offset 4. It is conservatively correct for this routine to return
1476/// false.
1477static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1478 const llvm::TargetData &TD) {
1479 // Base case if we find a float.
1480 if (IROffset == 0 && IRType->isFloatTy())
1481 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001482
Chris Lattner0b362002010-07-29 18:39:32 +00001483 // If this is a struct, recurse into the field at the specified offset.
1484 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1485 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1486 unsigned Elt = SL->getElementContainingOffset(IROffset);
1487 IROffset -= SL->getElementOffset(Elt);
1488 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1489 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001490
Chris Lattner0b362002010-07-29 18:39:32 +00001491 // If this is an array, recurse into the field at the specified offset.
1492 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1493 const llvm::Type *EltTy = ATy->getElementType();
1494 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1495 IROffset -= IROffset/EltSize*EltSize;
1496 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1497 }
1498
1499 return false;
1500}
1501
Chris Lattnerf47c9442010-07-29 18:13:09 +00001502
1503/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1504/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001505llvm::Type *X86_64ABIInfo::
1506GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001507 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001508 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001509 // pass as float if the last 4 bytes is just padding. This happens for
1510 // structs that contain 3 floats.
1511 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1512 SourceOffset*8+64, getContext()))
1513 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001514
Chris Lattner0b362002010-07-29 18:39:32 +00001515 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1516 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1517 // case.
1518 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001519 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1520 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001521
Chris Lattnerf47c9442010-07-29 18:13:09 +00001522 return llvm::Type::getDoubleTy(getVMContext());
1523}
1524
1525
Chris Lattner0d2656d2010-07-29 17:40:35 +00001526/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1527/// an 8-byte GPR. This means that we either have a scalar or we are talking
1528/// about the high or low part of an up-to-16-byte struct. This routine picks
1529/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001530/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1531/// etc).
1532///
1533/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1534/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1535/// the 8-byte value references. PrefType may be null.
1536///
1537/// SourceTy is the source level type for the entire argument. SourceOffset is
1538/// an offset into this that we're processing (which is always either 0 or 8).
1539///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001540llvm::Type *X86_64ABIInfo::
1541GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001542 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001543 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1544 // returning an 8-byte unit starting with it. See if we can safely use it.
1545 if (IROffset == 0) {
1546 // Pointers and int64's always fill the 8-byte unit.
1547 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1548 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001549
Chris Lattnere2962be2010-07-29 07:30:00 +00001550 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1551 // goodness in the source type is just tail padding. This is allowed to
1552 // kick in for struct {double,int} on the int, but not on
1553 // struct{double,int,int} because we wouldn't return the second int. We
1554 // have to do this analysis on the source type because we can't depend on
1555 // unions being lowered a specific way etc.
1556 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1557 IRType->isIntegerTy(32)) {
1558 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001559
Chris Lattnere2962be2010-07-29 07:30:00 +00001560 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1561 SourceOffset*8+64, getContext()))
1562 return IRType;
1563 }
1564 }
Chris Lattner49382de2010-07-28 22:44:07 +00001565
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001566 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001567 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001568 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001569 if (IROffset < SL->getSizeInBytes()) {
1570 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1571 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001572
Chris Lattner0d2656d2010-07-29 17:40:35 +00001573 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1574 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001575 }
Chris Lattner49382de2010-07-28 22:44:07 +00001576 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001577
Chris Lattner021c3a32010-07-29 07:43:55 +00001578 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001579 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001580 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1581 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001582 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1583 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001584 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001585
Chris Lattner49382de2010-07-28 22:44:07 +00001586 // Okay, we don't have any better idea of what to pass, so we pass this in an
1587 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001588 unsigned TySizeInBytes =
1589 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001590
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001591 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001592
Chris Lattner49382de2010-07-28 22:44:07 +00001593 // It is always safe to classify this as an integer type up to i64 that
1594 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001595 return llvm::IntegerType::get(getVMContext(),
1596 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001597}
1598
Chris Lattner66e7b682010-09-01 00:50:20 +00001599
1600/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1601/// be used as elements of a two register pair to pass or return, return a
1602/// first class aggregate to represent them. For example, if the low part of
1603/// a by-value argument should be passed as i32* and the high part as float,
1604/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001605static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001606GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001607 const llvm::TargetData &TD) {
1608 // In order to correctly satisfy the ABI, we need to the high part to start
1609 // at offset 8. If the high and low parts we inferred are both 4-byte types
1610 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1611 // the second element at offset 8. Check for this:
1612 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1613 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1614 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1615 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001616
Chris Lattner66e7b682010-09-01 00:50:20 +00001617 // To handle this, we have to increase the size of the low part so that the
1618 // second element will start at an 8 byte offset. We can't increase the size
1619 // of the second element because it might make us access off the end of the
1620 // struct.
1621 if (HiStart != 8) {
1622 // There are only two sorts of types the ABI generation code can produce for
1623 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1624 // Promote these to a larger type.
1625 if (Lo->isFloatTy())
1626 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1627 else {
1628 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1629 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1630 }
1631 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001632
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001633 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001634
1635
Chris Lattner66e7b682010-09-01 00:50:20 +00001636 // Verify that the second element is at an 8-byte offset.
1637 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1638 "Invalid x86-64 argument pair!");
1639 return Result;
1640}
1641
Chris Lattner519f68c2010-07-28 23:06:14 +00001642ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001643classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001644 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1645 // classification algorithm.
1646 X86_64ABIInfo::Class Lo, Hi;
1647 classify(RetTy, 0, Lo, Hi);
1648
1649 // Check some invariants.
1650 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001651 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1652
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001653 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001654 switch (Lo) {
1655 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001656 if (Hi == NoClass)
1657 return ABIArgInfo::getIgnore();
1658 // If the low part is just padding, it takes no register, leave ResType
1659 // null.
1660 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1661 "Unknown missing lo part");
1662 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001663
1664 case SSEUp:
1665 case X87Up:
1666 assert(0 && "Invalid classification for lo word.");
1667
1668 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1669 // hidden argument.
1670 case Memory:
1671 return getIndirectReturnResult(RetTy);
1672
1673 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1674 // available register of the sequence %rax, %rdx is used.
1675 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001676 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001677
Chris Lattnereb518b42010-07-29 21:42:50 +00001678 // If we have a sign or zero extended integer, make sure to return Extend
1679 // so that the parameter gets the right LLVM IR attributes.
1680 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1681 // Treat an enum type as its underlying type.
1682 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1683 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001684
Chris Lattnereb518b42010-07-29 21:42:50 +00001685 if (RetTy->isIntegralOrEnumerationType() &&
1686 RetTy->isPromotableIntegerType())
1687 return ABIArgInfo::getExtend();
1688 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001689 break;
1690
1691 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1692 // available SSE register of the sequence %xmm0, %xmm1 is used.
1693 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001694 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001695 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001696
1697 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1698 // returned on the X87 stack in %st0 as 80-bit x87 number.
1699 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001700 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001701 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001702
1703 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1704 // part of the value is returned in %st0 and the imaginary part in
1705 // %st1.
1706 case ComplexX87:
1707 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001708 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001709 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001710 NULL);
1711 break;
1712 }
1713
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001714 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001715 switch (Hi) {
1716 // Memory was handled previously and X87 should
1717 // never occur as a hi class.
1718 case Memory:
1719 case X87:
1720 assert(0 && "Invalid classification for hi word.");
1721
1722 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001723 case NoClass:
1724 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001725
Chris Lattner3db4dde2010-09-01 00:20:33 +00001726 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001727 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001728 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1729 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001730 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001731 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001732 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001733 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1734 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001735 break;
1736
1737 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001738 // is passed in the next available eightbyte chunk if the last used
1739 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001740 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001741 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001742 case SSEUp:
1743 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001744 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001745 break;
1746
1747 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1748 // returned together with the previous X87 value in %st0.
1749 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001750 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001751 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001752 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001753 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001754 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001755 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001756 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1757 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001758 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001759 break;
1760 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001761
Chris Lattner3db4dde2010-09-01 00:20:33 +00001762 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001763 // known to pass in the high eightbyte of the result. We do this by forming a
1764 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001765 if (HighPart)
1766 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001767
Chris Lattnereb518b42010-07-29 21:42:50 +00001768 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001769}
1770
Chris Lattnera3c109b2010-07-29 02:16:43 +00001771ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001772 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001773 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001774 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001775
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001776 // Check some invariants.
1777 // FIXME: Enforce these by construction.
1778 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001779 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1780
1781 neededInt = 0;
1782 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001783 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001784 switch (Lo) {
1785 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001786 if (Hi == NoClass)
1787 return ABIArgInfo::getIgnore();
1788 // If the low part is just padding, it takes no register, leave ResType
1789 // null.
1790 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1791 "Unknown missing lo part");
1792 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001794 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1795 // on the stack.
1796 case Memory:
1797
1798 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1799 // COMPLEX_X87, it is passed in memory.
1800 case X87:
1801 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001802 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1803 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001804 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001805
1806 case SSEUp:
1807 case X87Up:
1808 assert(0 && "Invalid classification for lo word.");
1809
1810 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1811 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1812 // and %r9 is used.
1813 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001814 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001815
Chris Lattner49382de2010-07-28 22:44:07 +00001816 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001817 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001818
1819 // If we have a sign or zero extended integer, make sure to return Extend
1820 // so that the parameter gets the right LLVM IR attributes.
1821 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1822 // Treat an enum type as its underlying type.
1823 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1824 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001825
Chris Lattnereb518b42010-07-29 21:42:50 +00001826 if (Ty->isIntegralOrEnumerationType() &&
1827 Ty->isPromotableIntegerType())
1828 return ABIArgInfo::getExtend();
1829 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001830
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001831 break;
1832
1833 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1834 // available SSE register is used, the registers are taken in the
1835 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001836 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001837 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001838 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001839 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001840 break;
1841 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001842 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001843
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001844 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001845 switch (Hi) {
1846 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001847 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001848 // which is passed in memory.
1849 case Memory:
1850 case X87:
1851 case ComplexX87:
1852 assert(0 && "Invalid classification for hi word.");
1853 break;
1854
1855 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001856
Chris Lattner645406a2010-09-01 00:24:35 +00001857 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001858 ++neededInt;
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 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001861
Chris Lattner645406a2010-09-01 00:24:35 +00001862 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1863 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001864 break;
1865
1866 // X87Up generally doesn't occur here (long double is passed in
1867 // memory), except in situations involving unions.
1868 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001869 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001870 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001871
Chris Lattner645406a2010-09-01 00:24:35 +00001872 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1873 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001874
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001875 ++neededSSE;
1876 break;
1877
1878 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1879 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001880 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001881 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001882 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001883 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001884 break;
1885 }
1886
Chris Lattner645406a2010-09-01 00:24:35 +00001887 // If a high part was specified, merge it together with the low part. It is
1888 // known to pass in the high eightbyte of the result. We do this by forming a
1889 // first class struct aggregate with the high and low part: {low, high}
1890 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001891 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001892
Chris Lattnereb518b42010-07-29 21:42:50 +00001893 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001894}
1895
Chris Lattneree5dcd02010-07-29 02:31:05 +00001896void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001897
Chris Lattnera3c109b2010-07-29 02:16:43 +00001898 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001899
1900 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001901 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001902
1903 // If the return value is indirect, then the hidden argument is consuming one
1904 // integer register.
1905 if (FI.getReturnInfo().isIndirect())
1906 --freeIntRegs;
1907
1908 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1909 // get assigned (in left-to-right order) for passing as follows...
1910 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1911 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001912 unsigned neededInt, neededSSE;
1913 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001914
1915 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1916 // eightbyte of an argument, the whole argument is passed on the
1917 // stack. If registers have already been assigned for some
1918 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001919 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001920 freeIntRegs -= neededInt;
1921 freeSSERegs -= neededSSE;
1922 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001923 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001924 }
1925 }
1926}
1927
1928static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1929 QualType Ty,
1930 CodeGenFunction &CGF) {
1931 llvm::Value *overflow_arg_area_p =
1932 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1933 llvm::Value *overflow_arg_area =
1934 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1935
1936 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1937 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1938 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1939 if (Align > 8) {
1940 // Note that we follow the ABI & gcc here, even though the type
1941 // could in theory have an alignment greater than 16. This case
1942 // shouldn't ever matter in practice.
1943
1944 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001945 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001946 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001947 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1948 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001949 CGF.Int64Ty);
1950 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001951 overflow_arg_area =
1952 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1953 overflow_arg_area->getType(),
1954 "overflow_arg_area.align");
1955 }
1956
1957 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1958 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1959 llvm::Value *Res =
1960 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001961 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001962
1963 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1964 // l->overflow_arg_area + sizeof(type).
1965 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1966 // an 8 byte boundary.
1967
1968 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001969 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001970 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001971 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1972 "overflow_arg_area.next");
1973 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1974
1975 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1976 return Res;
1977}
1978
1979llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1980 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001981 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001983 // Assume that va_list type is correct; should be pointer to LLVM type:
1984 // struct {
1985 // i32 gp_offset;
1986 // i32 fp_offset;
1987 // i8* overflow_arg_area;
1988 // i8* reg_save_area;
1989 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00001990 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001991
Chris Lattnera14db752010-03-11 18:19:55 +00001992 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001993 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001994
1995 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1996 // in the registers. If not go to step 7.
1997 if (!neededInt && !neededSSE)
1998 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1999
2000 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2001 // general purpose registers needed to pass type and num_fp to hold
2002 // the number of floating point registers needed.
2003
2004 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2005 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2006 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2007 //
2008 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2009 // register save space).
2010
2011 llvm::Value *InRegs = 0;
2012 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2013 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2014 if (neededInt) {
2015 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2016 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002017 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2018 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002019 }
2020
2021 if (neededSSE) {
2022 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2023 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2024 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002025 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2026 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002027 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2028 }
2029
2030 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2031 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2032 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2033 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2034
2035 // Emit code to load the value if it was passed in registers.
2036
2037 CGF.EmitBlock(InRegBlock);
2038
2039 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2040 // an offset of l->gp_offset and/or l->fp_offset. This may require
2041 // copying to a temporary location in case the parameter is passed
2042 // in different register classes or requires an alignment greater
2043 // than 8 for general purpose registers and 16 for XMM registers.
2044 //
2045 // FIXME: This really results in shameful code when we end up needing to
2046 // collect arguments from different places; often what should result in a
2047 // simple assembling of a structure from scattered addresses has many more
2048 // loads than necessary. Can we clean this up?
2049 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2050 llvm::Value *RegAddr =
2051 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2052 "reg_save_area");
2053 if (neededInt && neededSSE) {
2054 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002055 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002056 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2057 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2058 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2059 const llvm::Type *TyLo = ST->getElementType(0);
2060 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002061 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002062 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00002063 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2064 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002065 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2066 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002067 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2068 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002069 llvm::Value *V =
2070 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2071 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2072 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2073 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2074
Owen Andersona1cf15f2009-07-14 23:10:40 +00002075 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002076 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002077 } else if (neededInt) {
2078 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2079 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002080 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002081 } else if (neededSSE == 1) {
2082 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2083 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2084 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002085 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002086 assert(neededSSE == 2 && "Invalid number of needed registers!");
2087 // SSE registers are spaced 16 bytes apart in the register save
2088 // area, we need to collect the two eightbytes together.
2089 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002090 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002091 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002092 const llvm::Type *DblPtrTy =
2093 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner7650d952011-06-18 22:49:11 +00002094 const llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002095 DoubleTy, NULL);
2096 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2097 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2098 DblPtrTy));
2099 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2100 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2101 DblPtrTy));
2102 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2103 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2104 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002105 }
2106
2107 // AMD64-ABI 3.5.7p5: Step 5. Set:
2108 // l->gp_offset = l->gp_offset + num_gp * 8
2109 // l->fp_offset = l->fp_offset + num_fp * 16.
2110 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002111 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2113 gp_offset_p);
2114 }
2115 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002116 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002117 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2118 fp_offset_p);
2119 }
2120 CGF.EmitBranch(ContBlock);
2121
2122 // Emit code to load the value if it was passed in memory.
2123
2124 CGF.EmitBlock(InMemBlock);
2125 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2126
2127 // Return the appropriate result.
2128
2129 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002130 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002131 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002132 ResAddr->addIncoming(RegAddr, InRegBlock);
2133 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002134 return ResAddr;
2135}
2136
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002137ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2138
2139 if (Ty->isVoidType())
2140 return ABIArgInfo::getIgnore();
2141
2142 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2143 Ty = EnumTy->getDecl()->getIntegerType();
2144
2145 uint64_t Size = getContext().getTypeSize(Ty);
2146
2147 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002148 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2149 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002150 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2151
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002152 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2153 if (Size == 128 &&
2154 getContext().Target.getTriple().getOS() == llvm::Triple::MinGW32)
2155 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2156 Size));
2157
2158 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2159 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2160 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002161 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002162 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2163 Size));
2164
2165 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2166 }
2167
2168 if (Ty->isPromotableIntegerType())
2169 return ABIArgInfo::getExtend();
2170
2171 return ABIArgInfo::getDirect();
2172}
2173
2174void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2175
2176 QualType RetTy = FI.getReturnType();
2177 FI.getReturnInfo() = classify(RetTy);
2178
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002179 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2180 it != ie; ++it)
2181 it->info = classify(it->type);
2182}
2183
Chris Lattnerf13721d2010-08-31 16:44:54 +00002184llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2185 CodeGenFunction &CGF) const {
2186 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2187 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002188
Chris Lattnerf13721d2010-08-31 16:44:54 +00002189 CGBuilderTy &Builder = CGF.Builder;
2190 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2191 "ap");
2192 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2193 llvm::Type *PTy =
2194 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2195 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2196
2197 uint64_t Offset =
2198 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2199 llvm::Value *NextAddr =
2200 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2201 "ap.next");
2202 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2203
2204 return AddrTyped;
2205}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002206
John McCallec853ba2010-03-11 00:10:12 +00002207// PowerPC-32
2208
2209namespace {
2210class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2211public:
Chris Lattnerea044322010-07-29 02:01:43 +00002212 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002213
John McCallec853ba2010-03-11 00:10:12 +00002214 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2215 // This is recovered from gcc output.
2216 return 1; // r1 is the dedicated stack pointer
2217 }
2218
2219 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002220 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002221};
2222
2223}
2224
2225bool
2226PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2227 llvm::Value *Address) const {
2228 // This is calculated from the LLVM and GCC tables and verified
2229 // against gcc output. AFAIK all ABIs use the same encoding.
2230
2231 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2232 llvm::LLVMContext &Context = CGF.getLLVMContext();
2233
2234 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2235 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2236 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2237 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2238
2239 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002240 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002241
2242 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002243 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002244
2245 // 64-76 are various 4-byte special-purpose registers:
2246 // 64: mq
2247 // 65: lr
2248 // 66: ctr
2249 // 67: ap
2250 // 68-75 cr0-7
2251 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002252 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002253
2254 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002255 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002256
2257 // 109: vrsave
2258 // 110: vscr
2259 // 111: spe_acc
2260 // 112: spefscr
2261 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002262 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002263
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002264 return false;
John McCallec853ba2010-03-11 00:10:12 +00002265}
2266
2267
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002268//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002269// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002270//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002271
2272namespace {
2273
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002274class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002275public:
2276 enum ABIKind {
2277 APCS = 0,
2278 AAPCS = 1,
2279 AAPCS_VFP
2280 };
2281
2282private:
2283 ABIKind Kind;
2284
2285public:
Chris Lattnerea044322010-07-29 02:01:43 +00002286 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002287
2288private:
2289 ABIKind getABIKind() const { return Kind; }
2290
Chris Lattnera3c109b2010-07-29 02:16:43 +00002291 ABIArgInfo classifyReturnType(QualType RetTy) const;
2292 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002293
Chris Lattneree5dcd02010-07-29 02:31:05 +00002294 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002295
2296 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2297 CodeGenFunction &CGF) const;
2298};
2299
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002300class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2301public:
Chris Lattnerea044322010-07-29 02:01:43 +00002302 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2303 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002304
2305 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2306 return 13;
2307 }
Roman Divacky09345d12011-05-18 19:36:54 +00002308
John McCallf85e1932011-06-15 23:02:42 +00002309 llvm::StringRef getARCRetainAutoreleasedReturnValueMarker() const {
2310 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2311 }
2312
Roman Divacky09345d12011-05-18 19:36:54 +00002313 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2314 llvm::Value *Address) const {
2315 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2316 llvm::LLVMContext &Context = CGF.getLLVMContext();
2317
2318 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2319 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2320
2321 // 0-15 are the 16 integer registers.
2322 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2323
2324 return false;
2325 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002326};
2327
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002328}
2329
Chris Lattneree5dcd02010-07-29 02:31:05 +00002330void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002331 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002332 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002333 it != ie; ++it)
2334 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002335
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002336 // Always honor user-specified calling convention.
2337 if (FI.getCallingConvention() != llvm::CallingConv::C)
2338 return;
2339
2340 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002341 llvm::CallingConv::ID DefaultCC;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002342 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2343 if (Env == "gnueabi" || Env == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002344 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002345 else
2346 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002347
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002348 // If user did not ask for specific calling convention explicitly (e.g. via
2349 // pcs attribute), set effective calling convention if it's different than ABI
2350 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002351 switch (getABIKind()) {
2352 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002353 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2354 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002355 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002356 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002357 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2358 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002359 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002360 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002361 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2362 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002363 break;
2364 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002365}
2366
Chris Lattnera3c109b2010-07-29 02:16:43 +00002367ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002368 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002369 // Treat an enum type as its underlying type.
2370 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2371 Ty = EnumTy->getDecl()->getIntegerType();
2372
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002373 return (Ty->isPromotableIntegerType() ?
2374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002375 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002376
Daniel Dunbar42025572009-09-14 21:54:03 +00002377 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002378 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002379 return ABIArgInfo::getIgnore();
2380
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002381 // Structures with either a non-trivial destructor or a non-trivial
2382 // copy constructor are always indirect.
2383 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2384 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2385
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002386 // Otherwise, pass by coercing to a structure of the appropriate size.
2387 //
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002388 // FIXME: This doesn't handle alignment > 64 bits.
2389 const llvm::Type* ElemTy;
2390 unsigned SizeRegs;
Stuart Hastingsa1dadc92011-04-28 21:35:59 +00002391 if (getContext().getTypeSizeInChars(Ty) <= CharUnits::fromQuantity(64)) {
Eric Christopherad27eea2011-04-26 01:02:04 +00002392 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2393 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002394 } else if (getABIKind() == ARMABIInfo::APCS) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002395 // Initial ARM ByVal support is APCS-only.
2396 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2397 } else {
2398 // FIXME: This is kind of nasty... but there isn't much choice
2399 // because most of the ARM calling conventions don't yet support
2400 // byval.
2401 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2402 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002403 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002404
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002405 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002406 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002407 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002408}
2409
Chris Lattnera3c109b2010-07-29 02:16:43 +00002410static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002411 llvm::LLVMContext &VMContext) {
2412 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2413 // is called integer-like if its size is less than or equal to one word, and
2414 // the offset of each of its addressable sub-fields is zero.
2415
2416 uint64_t Size = Context.getTypeSize(Ty);
2417
2418 // Check that the type fits in a word.
2419 if (Size > 32)
2420 return false;
2421
2422 // FIXME: Handle vector types!
2423 if (Ty->isVectorType())
2424 return false;
2425
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002426 // Float types are never treated as "integer like".
2427 if (Ty->isRealFloatingType())
2428 return false;
2429
Daniel Dunbar98303b92009-09-13 08:03:58 +00002430 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002431 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002432 return true;
2433
Daniel Dunbar45815812010-02-01 23:31:26 +00002434 // Small complex integer types are "integer like".
2435 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2436 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002437
2438 // Single element and zero sized arrays should be allowed, by the definition
2439 // above, but they are not.
2440
2441 // Otherwise, it must be a record type.
2442 const RecordType *RT = Ty->getAs<RecordType>();
2443 if (!RT) return false;
2444
2445 // Ignore records with flexible arrays.
2446 const RecordDecl *RD = RT->getDecl();
2447 if (RD->hasFlexibleArrayMember())
2448 return false;
2449
2450 // Check that all sub-fields are at offset 0, and are themselves "integer
2451 // like".
2452 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2453
2454 bool HadField = false;
2455 unsigned idx = 0;
2456 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2457 i != e; ++i, ++idx) {
2458 const FieldDecl *FD = *i;
2459
Daniel Dunbar679855a2010-01-29 03:22:29 +00002460 // Bit-fields are not addressable, we only need to verify they are "integer
2461 // like". We still have to disallow a subsequent non-bitfield, for example:
2462 // struct { int : 0; int x }
2463 // is non-integer like according to gcc.
2464 if (FD->isBitField()) {
2465 if (!RD->isUnion())
2466 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002467
Daniel Dunbar679855a2010-01-29 03:22:29 +00002468 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2469 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002470
Daniel Dunbar679855a2010-01-29 03:22:29 +00002471 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002472 }
2473
Daniel Dunbar679855a2010-01-29 03:22:29 +00002474 // Check if this field is at offset 0.
2475 if (Layout.getFieldOffset(idx) != 0)
2476 return false;
2477
Daniel Dunbar98303b92009-09-13 08:03:58 +00002478 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2479 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002480
Daniel Dunbar679855a2010-01-29 03:22:29 +00002481 // Only allow at most one field in a structure. This doesn't match the
2482 // wording above, but follows gcc in situations with a field following an
2483 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002484 if (!RD->isUnion()) {
2485 if (HadField)
2486 return false;
2487
2488 HadField = true;
2489 }
2490 }
2491
2492 return true;
2493}
2494
Chris Lattnera3c109b2010-07-29 02:16:43 +00002495ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002496 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002497 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002498
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002499 // Large vector types should be returned via memory.
2500 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2501 return ABIArgInfo::getIndirect(0);
2502
John McCalld608cdb2010-08-22 10:59:02 +00002503 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002504 // Treat an enum type as its underlying type.
2505 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2506 RetTy = EnumTy->getDecl()->getIntegerType();
2507
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002508 return (RetTy->isPromotableIntegerType() ?
2509 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002510 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002511
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002512 // Structures with either a non-trivial destructor or a non-trivial
2513 // copy constructor are always indirect.
2514 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2515 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2516
Daniel Dunbar98303b92009-09-13 08:03:58 +00002517 // Are we following APCS?
2518 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002519 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002520 return ABIArgInfo::getIgnore();
2521
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002522 // Complex types are all returned as packed integers.
2523 //
2524 // FIXME: Consider using 2 x vector types if the back end handles them
2525 // correctly.
2526 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002527 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002528 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002529
Daniel Dunbar98303b92009-09-13 08:03:58 +00002530 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002531 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002532 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002533 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002534 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002535 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002536 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002537 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2538 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002539 }
2540
2541 // Otherwise return in memory.
2542 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002543 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002544
2545 // Otherwise this is an AAPCS variant.
2546
Chris Lattnera3c109b2010-07-29 02:16:43 +00002547 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002548 return ABIArgInfo::getIgnore();
2549
Daniel Dunbar98303b92009-09-13 08:03:58 +00002550 // Aggregates <= 4 bytes are returned in r0; other aggregates
2551 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002552 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002553 if (Size <= 32) {
2554 // Return in the smallest viable integer type.
2555 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002556 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002557 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002558 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2559 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002560 }
2561
Daniel Dunbar98303b92009-09-13 08:03:58 +00002562 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002563}
2564
2565llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002566 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002567 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002568 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002569 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002570
2571 CGBuilderTy &Builder = CGF.Builder;
2572 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2573 "ap");
2574 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2575 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002576 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002577 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2578
2579 uint64_t Offset =
2580 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2581 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002582 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002583 "ap.next");
2584 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2585
2586 return AddrTyped;
2587}
2588
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002589//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002590// PTX ABI Implementation
2591//===----------------------------------------------------------------------===//
2592
2593namespace {
2594
2595class PTXABIInfo : public ABIInfo {
2596public:
2597 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2598
2599 ABIArgInfo classifyReturnType(QualType RetTy) const;
2600 ABIArgInfo classifyArgumentType(QualType Ty) const;
2601
2602 virtual void computeInfo(CGFunctionInfo &FI) const;
2603 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2604 CodeGenFunction &CFG) const;
2605};
2606
2607class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2608public:
2609 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2610 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
2611};
2612
2613ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2614 if (RetTy->isVoidType())
2615 return ABIArgInfo::getIgnore();
2616 if (isAggregateTypeForABI(RetTy))
2617 return ABIArgInfo::getIndirect(0);
2618 return ABIArgInfo::getDirect();
2619}
2620
2621ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2622 if (isAggregateTypeForABI(Ty))
2623 return ABIArgInfo::getIndirect(0);
2624
2625 return ABIArgInfo::getDirect();
2626}
2627
2628void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2629 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2630 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2631 it != ie; ++it)
2632 it->info = classifyArgumentType(it->type);
2633
2634 // Always honor user-specified calling convention.
2635 if (FI.getCallingConvention() != llvm::CallingConv::C)
2636 return;
2637
2638 // Calling convention as default by an ABI.
2639 llvm::CallingConv::ID DefaultCC;
2640 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2641 if (Env == "device")
2642 DefaultCC = llvm::CallingConv::PTX_Device;
2643 else
2644 DefaultCC = llvm::CallingConv::PTX_Kernel;
2645
2646 FI.setEffectiveCallingConvention(DefaultCC);
2647}
2648
2649llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2650 CodeGenFunction &CFG) const {
2651 llvm_unreachable("PTX does not support varargs");
2652 return 0;
2653}
2654
2655}
2656
2657//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002658// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002659//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002660
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002661namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002662
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002663class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002664public:
2665 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2666
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002667 bool isPromotableIntegerType(QualType Ty) const;
2668
Chris Lattnera3c109b2010-07-29 02:16:43 +00002669 ABIArgInfo classifyReturnType(QualType RetTy) const;
2670 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002671
Chris Lattneree5dcd02010-07-29 02:31:05 +00002672 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002673 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002674 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2675 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002676 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002677 }
2678
2679 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2680 CodeGenFunction &CGF) const;
2681};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002682
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002683class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2684public:
Chris Lattnerea044322010-07-29 02:01:43 +00002685 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2686 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002687};
2688
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002689}
2690
2691bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2692 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002693 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002694 switch (BT->getKind()) {
2695 case BuiltinType::Bool:
2696 case BuiltinType::Char_S:
2697 case BuiltinType::Char_U:
2698 case BuiltinType::SChar:
2699 case BuiltinType::UChar:
2700 case BuiltinType::Short:
2701 case BuiltinType::UShort:
2702 case BuiltinType::Int:
2703 case BuiltinType::UInt:
2704 return true;
2705 default:
2706 return false;
2707 }
2708 return false;
2709}
2710
2711llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2712 CodeGenFunction &CGF) const {
2713 // FIXME: Implement
2714 return 0;
2715}
2716
2717
Chris Lattnera3c109b2010-07-29 02:16:43 +00002718ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2719 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002720 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002721 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002722 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002723
2724 return (isPromotableIntegerType(RetTy) ?
2725 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002726}
2727
Chris Lattnera3c109b2010-07-29 02:16:43 +00002728ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002729 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002730 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002731
2732 return (isPromotableIntegerType(Ty) ?
2733 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002734}
2735
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002736//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002737// MBlaze ABI Implementation
2738//===----------------------------------------------------------------------===//
2739
2740namespace {
2741
2742class MBlazeABIInfo : public ABIInfo {
2743public:
2744 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2745
2746 bool isPromotableIntegerType(QualType Ty) const;
2747
2748 ABIArgInfo classifyReturnType(QualType RetTy) const;
2749 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2750
2751 virtual void computeInfo(CGFunctionInfo &FI) const {
2752 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2753 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2754 it != ie; ++it)
2755 it->info = classifyArgumentType(it->type);
2756 }
2757
2758 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2759 CodeGenFunction &CGF) const;
2760};
2761
2762class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2763public:
2764 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2765 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2766 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2767 CodeGen::CodeGenModule &M) const;
2768};
2769
2770}
2771
2772bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2773 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2774 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2775 switch (BT->getKind()) {
2776 case BuiltinType::Bool:
2777 case BuiltinType::Char_S:
2778 case BuiltinType::Char_U:
2779 case BuiltinType::SChar:
2780 case BuiltinType::UChar:
2781 case BuiltinType::Short:
2782 case BuiltinType::UShort:
2783 return true;
2784 default:
2785 return false;
2786 }
2787 return false;
2788}
2789
2790llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2791 CodeGenFunction &CGF) const {
2792 // FIXME: Implement
2793 return 0;
2794}
2795
2796
2797ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2798 if (RetTy->isVoidType())
2799 return ABIArgInfo::getIgnore();
2800 if (isAggregateTypeForABI(RetTy))
2801 return ABIArgInfo::getIndirect(0);
2802
2803 return (isPromotableIntegerType(RetTy) ?
2804 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2805}
2806
2807ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2808 if (isAggregateTypeForABI(Ty))
2809 return ABIArgInfo::getIndirect(0);
2810
2811 return (isPromotableIntegerType(Ty) ?
2812 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2813}
2814
2815void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2816 llvm::GlobalValue *GV,
2817 CodeGen::CodeGenModule &M)
2818 const {
2819 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2820 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002821
Wesley Peck276fdf42010-12-19 19:57:51 +00002822 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2823 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2824 CC = llvm::CallingConv::MBLAZE_INTR;
2825 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2826 CC = llvm::CallingConv::MBLAZE_SVOL;
2827
2828 if (CC != llvm::CallingConv::C) {
2829 // Handle 'interrupt_handler' attribute:
2830 llvm::Function *F = cast<llvm::Function>(GV);
2831
2832 // Step 1: Set ISR calling convention.
2833 F->setCallingConv(CC);
2834
2835 // Step 2: Add attributes goodness.
2836 F->addFnAttr(llvm::Attribute::NoInline);
2837 }
2838
2839 // Step 3: Emit _interrupt_handler alias.
2840 if (CC == llvm::CallingConv::MBLAZE_INTR)
2841 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2842 "_interrupt_handler", GV, &M.getModule());
2843}
2844
2845
2846//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002847// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002848//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002849
2850namespace {
2851
2852class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2853public:
Chris Lattnerea044322010-07-29 02:01:43 +00002854 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2855 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002856 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2857 CodeGen::CodeGenModule &M) const;
2858};
2859
2860}
2861
2862void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2863 llvm::GlobalValue *GV,
2864 CodeGen::CodeGenModule &M) const {
2865 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2866 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2867 // Handle 'interrupt' attribute:
2868 llvm::Function *F = cast<llvm::Function>(GV);
2869
2870 // Step 1: Set ISR calling convention.
2871 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2872
2873 // Step 2: Add attributes goodness.
2874 F->addFnAttr(llvm::Attribute::NoInline);
2875
2876 // Step 3: Emit ISR vector alias.
2877 unsigned Num = attr->getNumber() + 0xffe0;
2878 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Benjamin Kramer77d66052010-11-12 15:42:18 +00002879 "vector_" + llvm::Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002880 GV, &M.getModule());
2881 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002882 }
2883}
2884
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002885//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002886// MIPS ABI Implementation. This works for both little-endian and
2887// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002888//===----------------------------------------------------------------------===//
2889
John McCallaeeb7012010-05-27 06:19:26 +00002890namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00002891class MipsABIInfo : public ABIInfo {
2892public:
2893 MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2894
2895 ABIArgInfo classifyReturnType(QualType RetTy) const;
2896 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2897 virtual void computeInfo(CGFunctionInfo &FI) const;
2898 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2899 CodeGenFunction &CGF) const;
2900};
2901
John McCallaeeb7012010-05-27 06:19:26 +00002902class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2903public:
Chris Lattnerea044322010-07-29 02:01:43 +00002904 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
Akira Hatanaka619e8872011-06-02 00:09:17 +00002905 : TargetCodeGenInfo(new MipsABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002906
2907 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2908 return 29;
2909 }
2910
2911 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002912 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002913};
2914}
2915
Akira Hatanaka619e8872011-06-02 00:09:17 +00002916ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
2917 if (isAggregateTypeForABI(Ty)) {
2918 // Ignore empty aggregates.
2919 if (getContext().getTypeSize(Ty) == 0)
2920 return ABIArgInfo::getIgnore();
2921
2922 return ABIArgInfo::getIndirect(0);
2923 }
2924
2925 // Treat an enum type as its underlying type.
2926 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2927 Ty = EnumTy->getDecl()->getIntegerType();
2928
2929 return (Ty->isPromotableIntegerType() ?
2930 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2931}
2932
2933ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
2934 if (RetTy->isVoidType())
2935 return ABIArgInfo::getIgnore();
2936
2937 if (isAggregateTypeForABI(RetTy)) {
2938 if (RetTy->isAnyComplexType())
2939 return ABIArgInfo::getDirect();
2940
2941 return ABIArgInfo::getIndirect(0);
2942 }
2943
2944 // Treat an enum type as its underlying type.
2945 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2946 RetTy = EnumTy->getDecl()->getIntegerType();
2947
2948 return (RetTy->isPromotableIntegerType() ?
2949 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2950}
2951
2952void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
2953 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2954 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2955 it != ie; ++it)
2956 it->info = classifyArgumentType(it->type);
2957}
2958
2959llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2960 CodeGenFunction &CGF) const {
2961 return 0;
2962}
2963
John McCallaeeb7012010-05-27 06:19:26 +00002964bool
2965MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2966 llvm::Value *Address) const {
2967 // This information comes from gcc's implementation, which seems to
2968 // as canonical as it gets.
2969
2970 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2971 llvm::LLVMContext &Context = CGF.getLLVMContext();
2972
2973 // Everything on MIPS is 4 bytes. Double-precision FP registers
2974 // are aliased to pairs of single-precision FP registers.
2975 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2976 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2977
2978 // 0-31 are the general purpose registers, $0 - $31.
2979 // 32-63 are the floating-point registers, $f0 - $f31.
2980 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2981 // 66 is the (notional, I think) register for signal-handler return.
2982 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2983
2984 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2985 // They are one bit wide and ignored here.
2986
2987 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2988 // (coprocessor 1 is the FP unit)
2989 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2990 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2991 // 176-181 are the DSP accumulator registers.
2992 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2993
2994 return false;
2995}
2996
2997
Chris Lattnerea044322010-07-29 02:01:43 +00002998const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002999 if (TheTargetCodeGenInfo)
3000 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003001
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003002 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
3003 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003004
Chris Lattner9c254f02010-06-29 06:01:59 +00003005 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003006 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003007 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003008 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003009
John McCallaeeb7012010-05-27 06:19:26 +00003010 case llvm::Triple::mips:
3011 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00003012 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00003013
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003014 case llvm::Triple::arm:
3015 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003016 {
3017 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003018
Sandeep Patel34c1af82011-04-05 00:23:47 +00003019 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
3020 Kind = ARMABIInfo::APCS;
3021 else if (CodeGenOpts.FloatABI == "hard")
3022 Kind = ARMABIInfo::AAPCS_VFP;
3023
3024 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3025 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003026
John McCallec853ba2010-03-11 00:10:12 +00003027 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003028 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003029
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003030 case llvm::Triple::ptx32:
3031 case llvm::Triple::ptx64:
3032 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3033
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003034 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00003035 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003036
Wesley Peck276fdf42010-12-19 19:57:51 +00003037 case llvm::Triple::mblaze:
3038 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3039
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003040 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003041 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003042
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003043 case llvm::Triple::x86: {
3044 bool DisableMMX = strcmp(getContext().Target.getABI(), "no-mmx") == 0;
3045
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003046 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003047 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003048 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003049
3050 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003051 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003052 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003053 case llvm::Triple::AuroraUX:
3054 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003055 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003056 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003057 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003058 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003059 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003060
3061 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003062 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003063 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003064 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003065 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003066
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003067 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003068 switch (Triple.getOS()) {
3069 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003070 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003071 case llvm::Triple::Cygwin:
3072 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3073 default:
3074 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3075 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003076 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003077}