blob: f7cdea5efc662e10818872771ff18640d140ce2c [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar626f1d82009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000100
Daniel Dunbar626f1d82009-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 Dunbarcd20ce12010-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 Dunbar626f1d82009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Dunbarcd20ce12010-05-17 16:46:00 +0000130
Argyrios Kyrtzidisd42411f2011-05-17 02:17:52 +0000131 // If this is a C++ record, check the bases first.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisd42411f2011-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 Dunbarcd20ce12010-05-17 16:46:00 +0000137
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson20759ad2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000151
Anders Carlsson20759ad2009-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 Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000184
Daniel Dunbar12ebb472010-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 Dunbar12ebb472010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 McCalla1dee5302010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-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 Dunbar6b45b672010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-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 Dunbar11c08c82009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000296
Chris Lattner458b2aa2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299
Chris Lattner22326a12010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattner2b037972010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-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 Lattner458b2aa2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000325
Chris Lattner9723d6c2010-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 Gregora71cc152010-02-02 20:10:50 +0000329
Chris Lattner9723d6c2010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000332}
333
Bob Wilsonbd4520b2011-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 Wendling5cd41c42010-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 Foad7c57be32011-07-11 09:56:20 +0000359static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
360 llvm::StringRef Constraint,
361 llvm::Type* Ty) {
Bill Wendlingec9d2632011-03-07 22:47:14 +0000362 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000363 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
364 return Ty;
365}
366
Chris Lattner0cf24192010-06-28 20:05:43 +0000367//===----------------------------------------------------------------------===//
368// X86-32 ABI Implementation
369//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000370
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000371/// X86_32ABIInfo - The X86-32 ABI information.
372class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000373 static const unsigned MinABIStackAlignInBytes = 4;
374
David Chisnallde3a0692009-08-17 23:08:21 +0000375 bool IsDarwinVectorABI;
376 bool IsSmallStructInRegABI;
Eli Friedman33465822011-07-08 23:31:17 +0000377 bool IsMMXDisabled;
Anton Korobeynikov244360d2009-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 Dunbar557893d2010-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 Lattner458b2aa2010-07-29 02:16:43 +0000387 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000388
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000389 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000390 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000391
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000392public:
Chris Lattner2b037972010-07-29 02:01:43 +0000393
Chris Lattner458b2aa2010-07-29 02:16:43 +0000394 ABIArgInfo classifyReturnType(QualType RetTy) const;
395 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000396
Chris Lattner22326a12010-07-29 02:31:05 +0000397 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000398 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000399 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
400 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000401 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000402 }
403
404 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
405 CodeGenFunction &CGF) const;
406
Eli Friedman33465822011-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 Korobeynikov244360d2009-06-05 22:08:42 +0000410};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000411
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000412class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
413public:
Eli Friedman33465822011-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 Davis4ea31ab2010-02-13 15:54:06 +0000416
417 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
418 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-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 Collingbourne8f5cf742011-02-19 23:03:58 +0000429
Jay Foad7c57be32011-07-11 09:56:20 +0000430 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
431 llvm::StringRef Constraint,
432 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000433 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
434 }
435
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000436};
437
438}
Anton Korobeynikov244360d2009-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 Dunbar4bd95c62010-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 Dunbar6b45b672010-05-14 03:40:53 +0000461 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000462 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000463 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000471 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000472 if (!RT) return false;
473
Anders Carlsson40446e82010-01-27 03:25:19 +0000474 // FIXME: Traverse bases here too.
475
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000476 // Structure types are passed in register if all fields would be
477 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000480 const FieldDecl *FD = *i;
481
482 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000483 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000494ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
495 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000496 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000497
Chris Lattner458b2aa2010-07-29 02:16:43 +0000498 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000499 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000500 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000501 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-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 Lattnerfe34c1d2010-07-29 06:26:06 +0000507 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000508 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-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 Lattnerfe34c1d2010-07-29 06:26:06 +0000514 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000515 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000516
517 return ABIArgInfo::getIndirect(0);
518 }
519
520 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000521 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000522
John McCalla1dee5302010-08-22 10:59:02 +0000523 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000524 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-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. Spencerb2f376b2010-08-25 18:17:27 +0000529
Anders Carlsson5789c492009-10-20 22:07:59 +0000530 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000531 if (RT->getDecl()->hasFlexibleArrayMember())
532 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000533 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000534
David Chisnallde3a0692009-08-17 23:08:21 +0000535 // If specified, structs and unions are always indirect.
536 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 return ABIArgInfo::getIndirect(0);
538
539 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000540 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000541 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000546 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000547 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000548 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
549 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000550
Chris Lattner458b2aa2010-07-29 02:16:43 +0000551 if (BT->getKind() == BuiltinType::Float) {
552 assert(getContext().getTypeSize(RetTy) ==
553 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000554 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000555 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000556 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000557
Chris Lattner458b2aa2010-07-29 02:16:43 +0000558 if (BT->getKind() == BuiltinType::Double) {
559 assert(getContext().getTypeSize(RetTy) ==
560 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000561 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000562 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-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 Lattnera5f58b02011-07-09 17:41:47 +0000567 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000568 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000572 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000573 if (Size == 64 || Size == 128)
574 return ABIArgInfo::getIndirect(0);
575
Chris Lattner458b2aa2010-07-29 02:16:43 +0000576 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000577 }
578 }
579
580 // Small structures which are register sized are generally returned
581 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000582 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
583 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000584 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000585 }
586
587 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000588 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000589
Chris Lattner458b2aa2010-07-29 02:16:43 +0000590 // Treat an enum type as its underlying type.
591 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
592 RetTy = EnumTy->getDecl()->getIntegerType();
593
594 return (RetTy->isPromotableIntegerType() ?
595 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000596}
597
Daniel Dunbared23de32010-09-16 20:42:00 +0000598static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
599 const RecordType *RT = Ty->getAs<RecordType>();
600 if (!RT)
601 return 0;
602 const RecordDecl *RD = RT->getDecl();
603
604 // If this is a C++ record, check the bases first.
605 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
606 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
607 e = CXXRD->bases_end(); i != e; ++i)
608 if (!isRecordWithSSEVectorType(Context, i->getType()))
609 return false;
610
611 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
612 i != e; ++i) {
613 QualType FT = i->getType();
614
615 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
616 return true;
617
618 if (isRecordWithSSEVectorType(Context, FT))
619 return true;
620 }
621
622 return false;
623}
624
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000625unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
626 unsigned Align) const {
627 // Otherwise, if the alignment is less than or equal to the minimum ABI
628 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000629 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000630 return 0; // Use default alignment.
631
632 // On non-Darwin, the stack type alignment is always 4.
633 if (!IsDarwinVectorABI) {
634 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000635 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000636 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000637
Daniel Dunbared23de32010-09-16 20:42:00 +0000638 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
639 if (isRecordWithSSEVectorType(getContext(), Ty))
640 return 16;
641
642 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000643}
644
Chris Lattner458b2aa2010-07-29 02:16:43 +0000645ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000646 if (!ByVal)
647 return ABIArgInfo::getIndirect(0, false);
648
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000649 // Compute the byval alignment.
650 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
651 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
652 if (StackAlign == 0)
Chris Lattnere76b95a2011-05-22 23:35:00 +0000653 return ABIArgInfo::getIndirect(4);
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000654
655 // If the stack alignment is less than the type alignment, realign the
656 // argument.
657 if (StackAlign < TypeAlign)
658 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
659 /*Realign=*/true);
660
661 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000662}
663
Chris Lattner458b2aa2010-07-29 02:16:43 +0000664ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000665 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000666 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000667 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000668 if (const RecordType *RT = Ty->getAs<RecordType>()) {
669 // Structures with either a non-trivial destructor or a non-trivial
670 // copy constructor are always indirect.
671 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000672 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000673
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000674 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000675 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000676 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000677
678 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000679 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000680 return ABIArgInfo::getIgnore();
681
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000682 // Expand small (<= 128-bit) record types when we know that the stack layout
683 // of those arguments will match the struct. This is important because the
684 // LLVM backend isn't smart enough to remove byval, which inhibits many
685 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000686 if (getContext().getTypeSize(Ty) <= 4*32 &&
687 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000688 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000689
Chris Lattner458b2aa2010-07-29 02:16:43 +0000690 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000691 }
692
Chris Lattnerd774ae92010-08-26 20:05:13 +0000693 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000694 // On Darwin, some vectors are passed in memory, we handle this by passing
695 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000696 if (IsDarwinVectorABI) {
697 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000698 if ((Size == 8 || Size == 16 || Size == 32) ||
699 (Size == 64 && VT->getNumElements() == 1))
700 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
701 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000702 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000703
Chris Lattnera5f58b02011-07-09 17:41:47 +0000704 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +0000705 if (UseX86_MMXType(IRType)) {
Eli Friedman33465822011-07-08 23:31:17 +0000706 if (IsMMXDisabled)
707 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
708 64));
Bill Wendling5cd41c42010-10-18 03:41:31 +0000709 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
710 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
711 return AAI;
712 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000713
Chris Lattnerd774ae92010-08-26 20:05:13 +0000714 return ABIArgInfo::getDirect();
715 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000716
717
Chris Lattner458b2aa2010-07-29 02:16:43 +0000718 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
719 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000720
Chris Lattner458b2aa2010-07-29 02:16:43 +0000721 return (Ty->isPromotableIntegerType() ?
722 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000723}
724
725llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
726 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000727 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000728 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000729
730 CGBuilderTy &Builder = CGF.Builder;
731 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
732 "ap");
733 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
734 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000735 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000736 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
737
738 uint64_t Offset =
739 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
740 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000741 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000742 "ap.next");
743 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
744
745 return AddrTyped;
746}
747
Charles Davis4ea31ab2010-02-13 15:54:06 +0000748void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
749 llvm::GlobalValue *GV,
750 CodeGen::CodeGenModule &CGM) const {
751 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
752 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
753 // Get the LLVM function.
754 llvm::Function *Fn = cast<llvm::Function>(GV);
755
756 // Now add the 'alignstack' attribute with a value of 16.
757 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
758 }
759 }
760}
761
John McCallbeec5a02010-03-06 00:35:14 +0000762bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
763 CodeGen::CodeGenFunction &CGF,
764 llvm::Value *Address) const {
765 CodeGen::CGBuilderTy &Builder = CGF.Builder;
766 llvm::LLVMContext &Context = CGF.getLLVMContext();
767
768 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
769 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000770
John McCallbeec5a02010-03-06 00:35:14 +0000771 // 0-7 are the eight integer registers; the order is different
772 // on Darwin (for EH), but the range is the same.
773 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000774 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000775
776 if (CGF.CGM.isTargetDarwin()) {
777 // 12-16 are st(0..4). Not sure why we stop at 4.
778 // These have size 16, which is sizeof(long double) on
779 // platforms with 8-byte alignment for that type.
780 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000781 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000782
John McCallbeec5a02010-03-06 00:35:14 +0000783 } else {
784 // 9 is %eflags, which doesn't get a size on Darwin for some
785 // reason.
786 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
787
788 // 11-16 are st(0..5). Not sure why we stop at 5.
789 // These have size 12, which is sizeof(long double) on
790 // platforms with 4-byte alignment for that type.
791 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000792 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
793 }
John McCallbeec5a02010-03-06 00:35:14 +0000794
795 return false;
796}
797
Chris Lattner0cf24192010-06-28 20:05:43 +0000798//===----------------------------------------------------------------------===//
799// X86-64 ABI Implementation
800//===----------------------------------------------------------------------===//
801
802
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000803namespace {
804/// X86_64ABIInfo - The X86_64 ABI information.
805class X86_64ABIInfo : public ABIInfo {
806 enum Class {
807 Integer = 0,
808 SSE,
809 SSEUp,
810 X87,
811 X87Up,
812 ComplexX87,
813 NoClass,
814 Memory
815 };
816
817 /// merge - Implement the X86_64 ABI merging algorithm.
818 ///
819 /// Merge an accumulating classification \arg Accum with a field
820 /// classification \arg Field.
821 ///
822 /// \param Accum - The accumulating classification. This should
823 /// always be either NoClass or the result of a previous merge
824 /// call. In addition, this should never be Memory (the caller
825 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000826 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000827
828 /// classify - Determine the x86_64 register classes in which the
829 /// given type T should be passed.
830 ///
831 /// \param Lo - The classification for the parts of the type
832 /// residing in the low word of the containing object.
833 ///
834 /// \param Hi - The classification for the parts of the type
835 /// residing in the high word of the containing object.
836 ///
837 /// \param OffsetBase - The bit offset of this type in the
838 /// containing object. Some parameters are classified different
839 /// depending on whether they straddle an eightbyte boundary.
840 ///
841 /// If a word is unused its result will be NoClass; if a type should
842 /// be passed in Memory then at least the classification of \arg Lo
843 /// will be Memory.
844 ///
845 /// The \arg Lo class will be NoClass iff the argument is ignored.
846 ///
847 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
848 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000849 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000850
Chris Lattnera5f58b02011-07-09 17:41:47 +0000851 llvm::Type *Get16ByteVectorType(QualType Ty) const;
852 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
853 unsigned IROffset, QualType SourceTy,
854 unsigned SourceOffset) const;
855 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
856 unsigned IROffset, QualType SourceTy,
857 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000858
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000859 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000860 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000861 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000862
863 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000864 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000865 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000866
Chris Lattner458b2aa2010-07-29 02:16:43 +0000867 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000868
Bill Wendling5cd41c42010-10-18 03:41:31 +0000869 ABIArgInfo classifyArgumentType(QualType Ty,
870 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000871 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000872
John McCalle0fda732011-04-21 01:20:55 +0000873 /// The 0.98 ABI revision clarified a lot of ambiguities,
874 /// unfortunately in ways that were not always consistent with
875 /// certain previous compilers. In particular, platforms which
876 /// required strict binary compatibility with older versions of GCC
877 /// may need to exempt themselves.
878 bool honorsRevision0_98() const {
879 return !getContext().Target.getTriple().isOSDarwin();
880 }
881
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000882public:
Chris Lattner2b037972010-07-29 02:01:43 +0000883 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000884
Chris Lattner22326a12010-07-29 02:31:05 +0000885 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000886
887 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
888 CodeGenFunction &CGF) const;
889};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000890
Chris Lattner04dc9572010-08-31 16:44:54 +0000891/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000892class WinX86_64ABIInfo : public ABIInfo {
893
894 ABIArgInfo classify(QualType Ty) const;
895
Chris Lattner04dc9572010-08-31 16:44:54 +0000896public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000897 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
898
899 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000900
901 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
902 CodeGenFunction &CGF) const;
903};
904
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000905class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
906public:
Chris Lattner2b037972010-07-29 02:01:43 +0000907 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
908 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000909
910 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
911 return 7;
912 }
913
914 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
915 llvm::Value *Address) const {
916 CodeGen::CGBuilderTy &Builder = CGF.Builder;
917 llvm::LLVMContext &Context = CGF.getLLVMContext();
918
919 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
920 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000921
John McCall943fae92010-05-27 06:19:26 +0000922 // 0-15 are the 16 integer registers.
923 // 16 is %rip.
924 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000925
926 return false;
927 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000928
Jay Foad7c57be32011-07-11 09:56:20 +0000929 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
930 llvm::StringRef Constraint,
931 llvm::Type* Ty) const {
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000932 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
933 }
934
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000935};
936
Chris Lattner04dc9572010-08-31 16:44:54 +0000937class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
938public:
939 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
940 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
941
942 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
943 return 7;
944 }
945
946 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
947 llvm::Value *Address) const {
948 CodeGen::CGBuilderTy &Builder = CGF.Builder;
949 llvm::LLVMContext &Context = CGF.getLLVMContext();
950
951 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
952 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000953
Chris Lattner04dc9572010-08-31 16:44:54 +0000954 // 0-15 are the 16 integer registers.
955 // 16 is %rip.
956 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
957
958 return false;
959 }
960};
961
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000962}
963
Chris Lattnerd776fb12010-06-28 21:43:59 +0000964X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
966 // classified recursively so that always two fields are
967 // considered. The resulting class is calculated according to
968 // the classes of the fields in the eightbyte:
969 //
970 // (a) If both classes are equal, this is the resulting class.
971 //
972 // (b) If one of the classes is NO_CLASS, the resulting class is
973 // the other class.
974 //
975 // (c) If one of the classes is MEMORY, the result is the MEMORY
976 // class.
977 //
978 // (d) If one of the classes is INTEGER, the result is the
979 // INTEGER.
980 //
981 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
982 // MEMORY is used as class.
983 //
984 // (f) Otherwise class SSE is used.
985
986 // Accum should never be memory (we should have returned) or
987 // ComplexX87 (because this cannot be passed in a structure).
988 assert((Accum != Memory && Accum != ComplexX87) &&
989 "Invalid accumulated classification during merge.");
990 if (Accum == Field || Field == NoClass)
991 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000992 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000993 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000994 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000995 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000996 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000997 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000998 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
999 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001000 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001001 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001002}
1003
Chris Lattner5c740f12010-06-30 19:14:05 +00001004void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001005 Class &Lo, Class &Hi) const {
1006 // FIXME: This code can be simplified by introducing a simple value class for
1007 // Class pairs with appropriate constructor methods for the various
1008 // situations.
1009
1010 // FIXME: Some of the split computations are wrong; unaligned vectors
1011 // shouldn't be passed in registers for example, so there is no chance they
1012 // can straddle an eightbyte. Verify & simplify.
1013
1014 Lo = Hi = NoClass;
1015
1016 Class &Current = OffsetBase < 64 ? Lo : Hi;
1017 Current = Memory;
1018
John McCall9dd450b2009-09-21 23:43:11 +00001019 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001020 BuiltinType::Kind k = BT->getKind();
1021
1022 if (k == BuiltinType::Void) {
1023 Current = NoClass;
1024 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1025 Lo = Integer;
1026 Hi = Integer;
1027 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1028 Current = Integer;
1029 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1030 Current = SSE;
1031 } else if (k == BuiltinType::LongDouble) {
1032 Lo = X87;
1033 Hi = X87Up;
1034 }
1035 // FIXME: _Decimal32 and _Decimal64 are SSE.
1036 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001037 return;
1038 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001039
Chris Lattnerd776fb12010-06-28 21:43:59 +00001040 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001041 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001042 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001043 return;
1044 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001045
Chris Lattnerd776fb12010-06-28 21:43:59 +00001046 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001047 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001048 return;
1049 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001050
Chris Lattnerd776fb12010-06-28 21:43:59 +00001051 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001052 if (Ty->isMemberFunctionPointerType())
1053 Lo = Hi = Integer;
1054 else
1055 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001056 return;
1057 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001058
Chris Lattnerd776fb12010-06-28 21:43:59 +00001059 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001060 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001061 if (Size == 32) {
1062 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1063 // float> as integer.
1064 Current = Integer;
1065
1066 // If this type crosses an eightbyte boundary, it should be
1067 // split.
1068 uint64_t EB_Real = (OffsetBase) / 64;
1069 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1070 if (EB_Real != EB_Imag)
1071 Hi = Lo;
1072 } else if (Size == 64) {
1073 // gcc passes <1 x double> in memory. :(
1074 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1075 return;
1076
1077 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001078 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001079 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1080 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1081 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001082 Current = Integer;
1083 else
1084 Current = SSE;
1085
1086 // If this type crosses an eightbyte boundary, it should be
1087 // split.
1088 if (OffsetBase && OffsetBase != 64)
1089 Hi = Lo;
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001090 } else if (Size == 128) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001091 Lo = SSE;
1092 Hi = SSEUp;
1093 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001094 return;
1095 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001096
Chris Lattnerd776fb12010-06-28 21:43:59 +00001097 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001098 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001099
Chris Lattner2b037972010-07-29 02:01:43 +00001100 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001101 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001102 if (Size <= 64)
1103 Current = Integer;
1104 else if (Size <= 128)
1105 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001106 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001107 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001108 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001109 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001110 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001111 Current = ComplexX87;
1112
1113 // If this complex type crosses an eightbyte boundary then it
1114 // should be split.
1115 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001116 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001117 if (Hi == NoClass && EB_Real != EB_Imag)
1118 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001119
Chris Lattnerd776fb12010-06-28 21:43:59 +00001120 return;
1121 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001122
Chris Lattner2b037972010-07-29 02:01:43 +00001123 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001124 // Arrays are treated like structures.
1125
Chris Lattner2b037972010-07-29 02:01:43 +00001126 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001127
1128 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001129 // than two eightbytes, ..., it has class MEMORY.
1130 if (Size > 128)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001131 return;
1132
1133 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1134 // fields, it has class MEMORY.
1135 //
1136 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001137 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001138 return;
1139
1140 // Otherwise implement simplified merge. We could be smarter about
1141 // this, but it isn't worth it and would be harder to verify.
1142 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001143 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001144 uint64_t ArraySize = AT->getSize().getZExtValue();
1145 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1146 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001147 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001148 Lo = merge(Lo, FieldLo);
1149 Hi = merge(Hi, FieldHi);
1150 if (Lo == Memory || Hi == Memory)
1151 break;
1152 }
1153
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001154 // Do post merger cleanup (see below). Only case we worry about is Memory.
1155 if (Hi == Memory)
1156 Lo = Memory;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001157 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001158 return;
1159 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001160
Chris Lattnerd776fb12010-06-28 21:43:59 +00001161 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001162 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001163
1164 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001165 // than two eightbytes, ..., it has class MEMORY.
1166 if (Size > 128)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001167 return;
1168
Anders Carlsson20759ad2009-09-16 15:53:40 +00001169 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1170 // copy constructor or a non-trivial destructor, it is passed by invisible
1171 // reference.
1172 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1173 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001174
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001175 const RecordDecl *RD = RT->getDecl();
1176
1177 // Assume variable sized types are passed in memory.
1178 if (RD->hasFlexibleArrayMember())
1179 return;
1180
Chris Lattner2b037972010-07-29 02:01:43 +00001181 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001182
1183 // Reset Lo class, this will be recomputed.
1184 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001185
1186 // If this is a C++ record, classify the bases first.
1187 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1188 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1189 e = CXXRD->bases_end(); i != e; ++i) {
1190 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1191 "Unexpected base class!");
1192 const CXXRecordDecl *Base =
1193 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1194
1195 // Classify this field.
1196 //
1197 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1198 // single eightbyte, each is classified separately. Each eightbyte gets
1199 // initialized to class NO_CLASS.
1200 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001201 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001202 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001203 Lo = merge(Lo, FieldLo);
1204 Hi = merge(Hi, FieldHi);
1205 if (Lo == Memory || Hi == Memory)
1206 break;
1207 }
1208 }
1209
1210 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001211 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001212 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1213 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001214 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1215 bool BitField = i->isBitField();
1216
1217 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1218 // fields, it has class MEMORY.
1219 //
1220 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001221 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001222 Lo = Memory;
1223 return;
1224 }
1225
1226 // Classify this field.
1227 //
1228 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1229 // exceeds a single eightbyte, each is classified
1230 // separately. Each eightbyte gets initialized to class
1231 // NO_CLASS.
1232 Class FieldLo, FieldHi;
1233
1234 // Bit-fields require special handling, they do not force the
1235 // structure to be passed in memory even if unaligned, and
1236 // therefore they can straddle an eightbyte.
1237 if (BitField) {
1238 // Ignore padding bit-fields.
1239 if (i->isUnnamedBitfield())
1240 continue;
1241
1242 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001243 uint64_t Size =
1244 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001245
1246 uint64_t EB_Lo = Offset / 64;
1247 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1248 FieldLo = FieldHi = NoClass;
1249 if (EB_Lo) {
1250 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1251 FieldLo = NoClass;
1252 FieldHi = Integer;
1253 } else {
1254 FieldLo = Integer;
1255 FieldHi = EB_Hi ? Integer : NoClass;
1256 }
1257 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001258 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001259 Lo = merge(Lo, FieldLo);
1260 Hi = merge(Hi, FieldHi);
1261 if (Lo == Memory || Hi == Memory)
1262 break;
1263 }
1264
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001265 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1266 //
1267 // (a) If one of the classes is MEMORY, the whole argument is
1268 // passed in memory.
1269 //
1270 // (b) If X87UP is not preceded by X87, the whole argument is
1271 // passed in memory.
1272 //
1273 // (c) If the size of the aggregate exceeds two eightbytes and the first
1274 // eight-byte isn't SSE or any other eightbyte isn't SSEUP, the whole
1275 // argument is passed in memory.
1276 //
1277 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1278 //
1279 // Some of these are enforced by the merging logic. Others can arise
1280 // only with unions; for example:
1281 // union { _Complex double; unsigned; }
1282 //
1283 // Note that clauses (b) and (c) were added in 0.98.
1284 if (Hi == Memory)
1285 Lo = Memory;
1286 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1287 Lo = Memory;
1288 if (Hi == SSEUp && Lo != SSE)
1289 Hi = SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001290 }
1291}
1292
Chris Lattner22a931e2010-06-29 06:01:59 +00001293ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001294 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1295 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001296 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001297 // Treat an enum type as its underlying type.
1298 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1299 Ty = EnumTy->getDecl()->getIntegerType();
1300
1301 return (Ty->isPromotableIntegerType() ?
1302 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1303 }
1304
1305 return ABIArgInfo::getIndirect(0);
1306}
1307
Chris Lattner22a931e2010-06-29 06:01:59 +00001308ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001309 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1310 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001311 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001312 // Treat an enum type as its underlying type.
1313 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1314 Ty = EnumTy->getDecl()->getIntegerType();
1315
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001316 return (Ty->isPromotableIntegerType() ?
1317 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001318 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001319
Daniel Dunbar53fac692010-04-21 19:49:55 +00001320 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1321 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001322
Chris Lattner44c2b902011-05-22 23:21:23 +00001323 // Compute the byval alignment. We specify the alignment of the byval in all
1324 // cases so that the mid-level optimizer knows the alignment of the byval.
1325 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1326 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001327}
1328
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001329/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1330/// full vector XMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001331/// vector register.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001332llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
1333 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001334
Chris Lattner9fa15c32010-07-29 05:02:29 +00001335 // Wrapper structs that just contain vectors are passed just like vectors,
1336 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001337 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001338 while (STy && STy->getNumElements() == 1) {
1339 IRType = STy->getElementType(0);
1340 STy = dyn_cast<llvm::StructType>(IRType);
1341 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001342
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001343 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001344 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1345 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001346 if (VT->getBitWidth() == 128 &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001347 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1348 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1349 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1350 EltTy->isIntegerTy(128)))
1351 return VT;
1352 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001353
Chris Lattner4200fe42010-07-29 04:56:46 +00001354 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1355}
1356
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001357/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1358/// is known to either be off the end of the specified type or being in
1359/// alignment padding. The user type specified is known to be at most 128 bits
1360/// in size, and have passed through X86_64ABIInfo::classify with a successful
1361/// classification that put one of the two halves in the INTEGER class.
1362///
1363/// It is conservatively correct to return false.
1364static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1365 unsigned EndBit, ASTContext &Context) {
1366 // If the bytes being queried are off the end of the type, there is no user
1367 // data hiding here. This handles analysis of builtins, vectors and other
1368 // types that don't contain interesting padding.
1369 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1370 if (TySize <= StartBit)
1371 return true;
1372
Chris Lattner98076a22010-07-29 07:43:55 +00001373 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1374 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1375 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1376
1377 // Check each element to see if the element overlaps with the queried range.
1378 for (unsigned i = 0; i != NumElts; ++i) {
1379 // If the element is after the span we care about, then we're done..
1380 unsigned EltOffset = i*EltSize;
1381 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001382
Chris Lattner98076a22010-07-29 07:43:55 +00001383 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1384 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1385 EndBit-EltOffset, Context))
1386 return false;
1387 }
1388 // If it overlaps no elements, then it is safe to process as padding.
1389 return true;
1390 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001391
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001392 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1393 const RecordDecl *RD = RT->getDecl();
1394 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001395
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001396 // If this is a C++ record, check the bases first.
1397 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1398 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1399 e = CXXRD->bases_end(); i != e; ++i) {
1400 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1401 "Unexpected base class!");
1402 const CXXRecordDecl *Base =
1403 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001404
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001405 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001406 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001407 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001408
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001409 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1410 if (!BitsContainNoUserData(i->getType(), BaseStart,
1411 EndBit-BaseOffset, Context))
1412 return false;
1413 }
1414 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001415
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001416 // Verify that no field has data that overlaps the region of interest. Yes
1417 // this could be sped up a lot by being smarter about queried fields,
1418 // however we're only looking at structs up to 16 bytes, so we don't care
1419 // much.
1420 unsigned idx = 0;
1421 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1422 i != e; ++i, ++idx) {
1423 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001424
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001425 // If we found a field after the region we care about, then we're done.
1426 if (FieldOffset >= EndBit) break;
1427
1428 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1429 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1430 Context))
1431 return false;
1432 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001433
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001434 // If nothing in this record overlapped the area of interest, then we're
1435 // clean.
1436 return true;
1437 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001438
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001439 return false;
1440}
1441
Chris Lattnere556a712010-07-29 18:39:32 +00001442/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1443/// float member at the specified offset. For example, {int,{float}} has a
1444/// float at offset 4. It is conservatively correct for this routine to return
1445/// false.
1446static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1447 const llvm::TargetData &TD) {
1448 // Base case if we find a float.
1449 if (IROffset == 0 && IRType->isFloatTy())
1450 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001451
Chris Lattnere556a712010-07-29 18:39:32 +00001452 // If this is a struct, recurse into the field at the specified offset.
1453 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1454 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1455 unsigned Elt = SL->getElementContainingOffset(IROffset);
1456 IROffset -= SL->getElementOffset(Elt);
1457 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1458 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001459
Chris Lattnere556a712010-07-29 18:39:32 +00001460 // If this is an array, recurse into the field at the specified offset.
1461 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1462 const llvm::Type *EltTy = ATy->getElementType();
1463 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1464 IROffset -= IROffset/EltSize*EltSize;
1465 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1466 }
1467
1468 return false;
1469}
1470
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001471
1472/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1473/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001474llvm::Type *X86_64ABIInfo::
1475GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001476 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001477 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001478 // pass as float if the last 4 bytes is just padding. This happens for
1479 // structs that contain 3 floats.
1480 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1481 SourceOffset*8+64, getContext()))
1482 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001483
Chris Lattnere556a712010-07-29 18:39:32 +00001484 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1485 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1486 // case.
1487 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001488 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1489 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001490
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001491 return llvm::Type::getDoubleTy(getVMContext());
1492}
1493
1494
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001495/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1496/// an 8-byte GPR. This means that we either have a scalar or we are talking
1497/// about the high or low part of an up-to-16-byte struct. This routine picks
1498/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001499/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1500/// etc).
1501///
1502/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1503/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1504/// the 8-byte value references. PrefType may be null.
1505///
1506/// SourceTy is the source level type for the entire argument. SourceOffset is
1507/// an offset into this that we're processing (which is always either 0 or 8).
1508///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001509llvm::Type *X86_64ABIInfo::
1510GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001511 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001512 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1513 // returning an 8-byte unit starting with it. See if we can safely use it.
1514 if (IROffset == 0) {
1515 // Pointers and int64's always fill the 8-byte unit.
1516 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1517 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001518
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001519 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1520 // goodness in the source type is just tail padding. This is allowed to
1521 // kick in for struct {double,int} on the int, but not on
1522 // struct{double,int,int} because we wouldn't return the second int. We
1523 // have to do this analysis on the source type because we can't depend on
1524 // unions being lowered a specific way etc.
1525 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1526 IRType->isIntegerTy(32)) {
1527 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001528
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001529 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1530 SourceOffset*8+64, getContext()))
1531 return IRType;
1532 }
1533 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001534
Chris Lattnerce1bd752010-07-29 04:51:12 +00001535 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001536 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001537 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001538 if (IROffset < SL->getSizeInBytes()) {
1539 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1540 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001541
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001542 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1543 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001544 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001545 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001546
Chris Lattner98076a22010-07-29 07:43:55 +00001547 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001548 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001549 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1550 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001551 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1552 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001553 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001554
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001555 // Okay, we don't have any better idea of what to pass, so we pass this in an
1556 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001557 unsigned TySizeInBytes =
1558 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001559
Chris Lattner3f763422010-07-29 17:34:39 +00001560 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001561
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001562 // It is always safe to classify this as an integer type up to i64 that
1563 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001564 return llvm::IntegerType::get(getVMContext(),
1565 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001566}
1567
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001568
1569/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1570/// be used as elements of a two register pair to pass or return, return a
1571/// first class aggregate to represent them. For example, if the low part of
1572/// a by-value argument should be passed as i32* and the high part as float,
1573/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001574static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001575GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001576 const llvm::TargetData &TD) {
1577 // In order to correctly satisfy the ABI, we need to the high part to start
1578 // at offset 8. If the high and low parts we inferred are both 4-byte types
1579 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1580 // the second element at offset 8. Check for this:
1581 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1582 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1583 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1584 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001585
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001586 // To handle this, we have to increase the size of the low part so that the
1587 // second element will start at an 8 byte offset. We can't increase the size
1588 // of the second element because it might make us access off the end of the
1589 // struct.
1590 if (HiStart != 8) {
1591 // There are only two sorts of types the ABI generation code can produce for
1592 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1593 // Promote these to a larger type.
1594 if (Lo->isFloatTy())
1595 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1596 else {
1597 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1598 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1599 }
1600 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001601
Chris Lattnera5f58b02011-07-09 17:41:47 +00001602 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001603
1604
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001605 // Verify that the second element is at an 8-byte offset.
1606 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1607 "Invalid x86-64 argument pair!");
1608 return Result;
1609}
1610
Chris Lattner31faff52010-07-28 23:06:14 +00001611ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001612classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001613 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1614 // classification algorithm.
1615 X86_64ABIInfo::Class Lo, Hi;
1616 classify(RetTy, 0, Lo, Hi);
1617
1618 // Check some invariants.
1619 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001620 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1621
Chris Lattnera5f58b02011-07-09 17:41:47 +00001622 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001623 switch (Lo) {
1624 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001625 if (Hi == NoClass)
1626 return ABIArgInfo::getIgnore();
1627 // If the low part is just padding, it takes no register, leave ResType
1628 // null.
1629 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1630 "Unknown missing lo part");
1631 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001632
1633 case SSEUp:
1634 case X87Up:
1635 assert(0 && "Invalid classification for lo word.");
1636
1637 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1638 // hidden argument.
1639 case Memory:
1640 return getIndirectReturnResult(RetTy);
1641
1642 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1643 // available register of the sequence %rax, %rdx is used.
1644 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001645 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001646
Chris Lattner1f3a0632010-07-29 21:42:50 +00001647 // If we have a sign or zero extended integer, make sure to return Extend
1648 // so that the parameter gets the right LLVM IR attributes.
1649 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1650 // Treat an enum type as its underlying type.
1651 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1652 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001653
Chris Lattner1f3a0632010-07-29 21:42:50 +00001654 if (RetTy->isIntegralOrEnumerationType() &&
1655 RetTy->isPromotableIntegerType())
1656 return ABIArgInfo::getExtend();
1657 }
Chris Lattner31faff52010-07-28 23:06:14 +00001658 break;
1659
1660 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1661 // available SSE register of the sequence %xmm0, %xmm1 is used.
1662 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001663 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001664 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001665
1666 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1667 // returned on the X87 stack in %st0 as 80-bit x87 number.
1668 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001669 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001670 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001671
1672 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1673 // part of the value is returned in %st0 and the imaginary part in
1674 // %st1.
1675 case ComplexX87:
1676 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001677 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001678 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001679 NULL);
1680 break;
1681 }
1682
Chris Lattnera5f58b02011-07-09 17:41:47 +00001683 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001684 switch (Hi) {
1685 // Memory was handled previously and X87 should
1686 // never occur as a hi class.
1687 case Memory:
1688 case X87:
1689 assert(0 && "Invalid classification for hi word.");
1690
1691 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001692 case NoClass:
1693 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001694
Chris Lattner52b3c132010-09-01 00:20:33 +00001695 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001696 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001697 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1698 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001699 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001700 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001701 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001702 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1703 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001704 break;
1705
1706 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001707 // is passed in the upper half of the last used SSE register.
Chris Lattner31faff52010-07-28 23:06:14 +00001708 //
Chris Lattner57540c52011-04-15 05:22:18 +00001709 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001710 case SSEUp:
1711 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001712 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001713 break;
1714
1715 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1716 // returned together with the previous X87 value in %st0.
1717 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001718 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001719 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001720 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001721 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001722 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001723 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001724 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1725 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001726 }
Chris Lattner31faff52010-07-28 23:06:14 +00001727 break;
1728 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001729
Chris Lattner52b3c132010-09-01 00:20:33 +00001730 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001731 // known to pass in the high eightbyte of the result. We do this by forming a
1732 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001733 if (HighPart)
1734 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001735
Chris Lattner1f3a0632010-07-29 21:42:50 +00001736 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001737}
1738
Chris Lattner458b2aa2010-07-29 02:16:43 +00001739ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +00001740 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001741 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001742 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001743
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001744 // Check some invariants.
1745 // FIXME: Enforce these by construction.
1746 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001747 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1748
1749 neededInt = 0;
1750 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001751 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001752 switch (Lo) {
1753 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001754 if (Hi == NoClass)
1755 return ABIArgInfo::getIgnore();
1756 // If the low part is just padding, it takes no register, leave ResType
1757 // null.
1758 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1759 "Unknown missing lo part");
1760 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001761
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001762 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1763 // on the stack.
1764 case Memory:
1765
1766 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1767 // COMPLEX_X87, it is passed in memory.
1768 case X87:
1769 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001770 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1771 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001772 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001773
1774 case SSEUp:
1775 case X87Up:
1776 assert(0 && "Invalid classification for lo word.");
1777
1778 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1779 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1780 // and %r9 is used.
1781 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001782 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001783
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001784 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001785 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001786
1787 // If we have a sign or zero extended integer, make sure to return Extend
1788 // so that the parameter gets the right LLVM IR attributes.
1789 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1790 // Treat an enum type as its underlying type.
1791 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1792 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001793
Chris Lattner1f3a0632010-07-29 21:42:50 +00001794 if (Ty->isIntegralOrEnumerationType() &&
1795 Ty->isPromotableIntegerType())
1796 return ABIArgInfo::getExtend();
1797 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001798
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001799 break;
1800
1801 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1802 // available SSE register is used, the registers are taken in the
1803 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001804 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001805 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001806 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001807 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001808 break;
1809 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001810 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001811
Chris Lattnera5f58b02011-07-09 17:41:47 +00001812 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001813 switch (Hi) {
1814 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00001815 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001816 // which is passed in memory.
1817 case Memory:
1818 case X87:
1819 case ComplexX87:
1820 assert(0 && "Invalid classification for hi word.");
1821 break;
1822
1823 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001824
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001825 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001826 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001827 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001828 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001829
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001830 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1831 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001832 break;
1833
1834 // X87Up generally doesn't occur here (long double is passed in
1835 // memory), except in situations involving unions.
1836 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001837 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001838 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001839
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001840 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1841 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001842
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001843 ++neededSSE;
1844 break;
1845
1846 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1847 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001848 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001849 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001850 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001851 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001852 break;
1853 }
1854
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001855 // If a high part was specified, merge it together with the low part. It is
1856 // known to pass in the high eightbyte of the result. We do this by forming a
1857 // first class struct aggregate with the high and low part: {low, high}
1858 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001859 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001860
Chris Lattner1f3a0632010-07-29 21:42:50 +00001861 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001862}
1863
Chris Lattner22326a12010-07-29 02:31:05 +00001864void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001865
Chris Lattner458b2aa2010-07-29 02:16:43 +00001866 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001867
1868 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001869 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001870
1871 // If the return value is indirect, then the hidden argument is consuming one
1872 // integer register.
1873 if (FI.getReturnInfo().isIndirect())
1874 --freeIntRegs;
1875
1876 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1877 // get assigned (in left-to-right order) for passing as follows...
1878 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1879 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00001880 unsigned neededInt, neededSSE;
1881 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001882
1883 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1884 // eightbyte of an argument, the whole argument is passed on the
1885 // stack. If registers have already been assigned for some
1886 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001887 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001888 freeIntRegs -= neededInt;
1889 freeSSERegs -= neededSSE;
1890 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001891 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001892 }
1893 }
1894}
1895
1896static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1897 QualType Ty,
1898 CodeGenFunction &CGF) {
1899 llvm::Value *overflow_arg_area_p =
1900 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1901 llvm::Value *overflow_arg_area =
1902 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1903
1904 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1905 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1906 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1907 if (Align > 8) {
1908 // Note that we follow the ABI & gcc here, even though the type
1909 // could in theory have an alignment greater than 16. This case
1910 // shouldn't ever matter in practice.
1911
1912 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001913 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001914 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001915 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1916 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001917 CGF.Int64Ty);
1918 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001919 overflow_arg_area =
1920 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1921 overflow_arg_area->getType(),
1922 "overflow_arg_area.align");
1923 }
1924
1925 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1926 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1927 llvm::Value *Res =
1928 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001929 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001930
1931 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1932 // l->overflow_arg_area + sizeof(type).
1933 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1934 // an 8 byte boundary.
1935
1936 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001937 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001938 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001939 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1940 "overflow_arg_area.next");
1941 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1942
1943 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1944 return Res;
1945}
1946
1947llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1948 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001949 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001950
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 // Assume that va_list type is correct; should be pointer to LLVM type:
1952 // struct {
1953 // i32 gp_offset;
1954 // i32 fp_offset;
1955 // i8* overflow_arg_area;
1956 // i8* reg_save_area;
1957 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00001958 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001959
Chris Lattner9723d6c2010-03-11 18:19:55 +00001960 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001961 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001962
1963 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1964 // in the registers. If not go to step 7.
1965 if (!neededInt && !neededSSE)
1966 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1967
1968 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1969 // general purpose registers needed to pass type and num_fp to hold
1970 // the number of floating point registers needed.
1971
1972 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1973 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1974 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1975 //
1976 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1977 // register save space).
1978
1979 llvm::Value *InRegs = 0;
1980 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1981 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1982 if (neededInt) {
1983 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1984 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001985 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1986 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001987 }
1988
1989 if (neededSSE) {
1990 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1991 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1992 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001993 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1994 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001995 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1996 }
1997
1998 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1999 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2000 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2001 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2002
2003 // Emit code to load the value if it was passed in registers.
2004
2005 CGF.EmitBlock(InRegBlock);
2006
2007 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2008 // an offset of l->gp_offset and/or l->fp_offset. This may require
2009 // copying to a temporary location in case the parameter is passed
2010 // in different register classes or requires an alignment greater
2011 // than 8 for general purpose registers and 16 for XMM registers.
2012 //
2013 // FIXME: This really results in shameful code when we end up needing to
2014 // collect arguments from different places; often what should result in a
2015 // simple assembling of a structure from scattered addresses has many more
2016 // loads than necessary. Can we clean this up?
2017 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2018 llvm::Value *RegAddr =
2019 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2020 "reg_save_area");
2021 if (neededInt && neededSSE) {
2022 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002023 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002024 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2025 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2026 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2027 const llvm::Type *TyLo = ST->getElementType(0);
2028 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002029 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002030 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00002031 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2032 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002033 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2034 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002035 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2036 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002037 llvm::Value *V =
2038 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2039 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2040 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2041 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2042
Owen Anderson170229f2009-07-14 23:10:40 +00002043 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002044 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002045 } else if (neededInt) {
2046 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2047 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002048 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002049 } else if (neededSSE == 1) {
2050 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2051 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2052 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002053 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002054 assert(neededSSE == 2 && "Invalid number of needed registers!");
2055 // SSE registers are spaced 16 bytes apart in the register save
2056 // area, we need to collect the two eightbytes together.
2057 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002058 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foad7c57be32011-07-11 09:56:20 +00002059 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner0cf24192010-06-28 20:05:43 +00002060 const llvm::Type *DblPtrTy =
2061 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner845511f2011-06-18 22:49:11 +00002062 const llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002063 DoubleTy, NULL);
2064 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2065 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2066 DblPtrTy));
2067 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2068 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2069 DblPtrTy));
2070 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2071 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2072 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002073 }
2074
2075 // AMD64-ABI 3.5.7p5: Step 5. Set:
2076 // l->gp_offset = l->gp_offset + num_gp * 8
2077 // l->fp_offset = l->fp_offset + num_fp * 16.
2078 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002079 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002080 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2081 gp_offset_p);
2082 }
2083 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002084 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002085 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2086 fp_offset_p);
2087 }
2088 CGF.EmitBranch(ContBlock);
2089
2090 // Emit code to load the value if it was passed in memory.
2091
2092 CGF.EmitBlock(InMemBlock);
2093 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2094
2095 // Return the appropriate result.
2096
2097 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002098 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002099 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002100 ResAddr->addIncoming(RegAddr, InRegBlock);
2101 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002102 return ResAddr;
2103}
2104
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002105ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2106
2107 if (Ty->isVoidType())
2108 return ABIArgInfo::getIgnore();
2109
2110 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2111 Ty = EnumTy->getDecl()->getIntegerType();
2112
2113 uint64_t Size = getContext().getTypeSize(Ty);
2114
2115 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002116 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2117 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002118 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2119
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002120 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2121 if (Size == 128 &&
2122 getContext().Target.getTriple().getOS() == llvm::Triple::MinGW32)
2123 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2124 Size));
2125
2126 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2127 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2128 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002129 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002130 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2131 Size));
2132
2133 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2134 }
2135
2136 if (Ty->isPromotableIntegerType())
2137 return ABIArgInfo::getExtend();
2138
2139 return ABIArgInfo::getDirect();
2140}
2141
2142void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2143
2144 QualType RetTy = FI.getReturnType();
2145 FI.getReturnInfo() = classify(RetTy);
2146
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002147 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2148 it != ie; ++it)
2149 it->info = classify(it->type);
2150}
2151
Chris Lattner04dc9572010-08-31 16:44:54 +00002152llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2153 CodeGenFunction &CGF) const {
2154 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2155 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002156
Chris Lattner04dc9572010-08-31 16:44:54 +00002157 CGBuilderTy &Builder = CGF.Builder;
2158 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2159 "ap");
2160 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2161 llvm::Type *PTy =
2162 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2163 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2164
2165 uint64_t Offset =
2166 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2167 llvm::Value *NextAddr =
2168 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2169 "ap.next");
2170 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2171
2172 return AddrTyped;
2173}
Chris Lattner0cf24192010-06-28 20:05:43 +00002174
John McCallea8d8bb2010-03-11 00:10:12 +00002175// PowerPC-32
2176
2177namespace {
2178class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2179public:
Chris Lattner2b037972010-07-29 02:01:43 +00002180 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002181
John McCallea8d8bb2010-03-11 00:10:12 +00002182 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2183 // This is recovered from gcc output.
2184 return 1; // r1 is the dedicated stack pointer
2185 }
2186
2187 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002188 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002189};
2190
2191}
2192
2193bool
2194PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2195 llvm::Value *Address) const {
2196 // This is calculated from the LLVM and GCC tables and verified
2197 // against gcc output. AFAIK all ABIs use the same encoding.
2198
2199 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2200 llvm::LLVMContext &Context = CGF.getLLVMContext();
2201
2202 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2203 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2204 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2205 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2206
2207 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002208 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002209
2210 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002211 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002212
2213 // 64-76 are various 4-byte special-purpose registers:
2214 // 64: mq
2215 // 65: lr
2216 // 66: ctr
2217 // 67: ap
2218 // 68-75 cr0-7
2219 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002220 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002221
2222 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002223 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002224
2225 // 109: vrsave
2226 // 110: vscr
2227 // 111: spe_acc
2228 // 112: spefscr
2229 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002230 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002231
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002232 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002233}
2234
2235
Chris Lattner0cf24192010-06-28 20:05:43 +00002236//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002237// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002238//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002239
2240namespace {
2241
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002242class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002243public:
2244 enum ABIKind {
2245 APCS = 0,
2246 AAPCS = 1,
2247 AAPCS_VFP
2248 };
2249
2250private:
2251 ABIKind Kind;
2252
2253public:
Chris Lattner2b037972010-07-29 02:01:43 +00002254 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002255
2256private:
2257 ABIKind getABIKind() const { return Kind; }
2258
Chris Lattner458b2aa2010-07-29 02:16:43 +00002259 ABIArgInfo classifyReturnType(QualType RetTy) const;
2260 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002261
Chris Lattner22326a12010-07-29 02:31:05 +00002262 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002263
2264 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2265 CodeGenFunction &CGF) const;
2266};
2267
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002268class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2269public:
Chris Lattner2b037972010-07-29 02:01:43 +00002270 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2271 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002272
2273 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2274 return 13;
2275 }
Roman Divackyc1617352011-05-18 19:36:54 +00002276
John McCall31168b02011-06-15 23:02:42 +00002277 llvm::StringRef getARCRetainAutoreleasedReturnValueMarker() const {
2278 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2279 }
2280
Roman Divackyc1617352011-05-18 19:36:54 +00002281 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2282 llvm::Value *Address) const {
2283 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2284 llvm::LLVMContext &Context = CGF.getLLVMContext();
2285
2286 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2287 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2288
2289 // 0-15 are the 16 integer registers.
2290 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2291
2292 return false;
2293 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002294};
2295
Daniel Dunbard59655c2009-09-12 00:59:49 +00002296}
2297
Chris Lattner22326a12010-07-29 02:31:05 +00002298void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002299 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002300 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002301 it != ie; ++it)
2302 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002303
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002304 // Always honor user-specified calling convention.
2305 if (FI.getCallingConvention() != llvm::CallingConv::C)
2306 return;
2307
2308 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002309 llvm::CallingConv::ID DefaultCC;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002310 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2311 if (Env == "gnueabi" || Env == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002312 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002313 else
2314 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002315
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002316 // If user did not ask for specific calling convention explicitly (e.g. via
2317 // pcs attribute), set effective calling convention if it's different than ABI
2318 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002319 switch (getABIKind()) {
2320 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002321 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2322 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002323 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002324 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002325 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2326 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002327 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002328 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002329 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2330 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002331 break;
2332 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002333}
2334
Chris Lattner458b2aa2010-07-29 02:16:43 +00002335ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002336 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002337 // Treat an enum type as its underlying type.
2338 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2339 Ty = EnumTy->getDecl()->getIntegerType();
2340
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002341 return (Ty->isPromotableIntegerType() ?
2342 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002343 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002344
Daniel Dunbar09d33622009-09-14 21:54:03 +00002345 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002346 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002347 return ABIArgInfo::getIgnore();
2348
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002349 // Structures with either a non-trivial destructor or a non-trivial
2350 // copy constructor are always indirect.
2351 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2352 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2353
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002354 // Otherwise, pass by coercing to a structure of the appropriate size.
2355 //
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002356 // FIXME: This doesn't handle alignment > 64 bits.
2357 const llvm::Type* ElemTy;
2358 unsigned SizeRegs;
Stuart Hastings9f02fd92011-04-28 21:35:59 +00002359 if (getContext().getTypeSizeInChars(Ty) <= CharUnits::fromQuantity(64)) {
Eric Christopher6f095d62011-04-26 01:02:04 +00002360 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2361 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings4b214952011-04-28 18:16:06 +00002362 } else if (getABIKind() == ARMABIInfo::APCS) {
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002363 // Initial ARM ByVal support is APCS-only.
2364 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2365 } else {
2366 // FIXME: This is kind of nasty... but there isn't much choice
2367 // because most of the ARM calling conventions don't yet support
2368 // byval.
2369 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2370 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002371 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002372
Chris Lattnera5f58b02011-07-09 17:41:47 +00002373 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002374 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002375 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002376}
2377
Chris Lattner458b2aa2010-07-29 02:16:43 +00002378static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002379 llvm::LLVMContext &VMContext) {
2380 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2381 // is called integer-like if its size is less than or equal to one word, and
2382 // the offset of each of its addressable sub-fields is zero.
2383
2384 uint64_t Size = Context.getTypeSize(Ty);
2385
2386 // Check that the type fits in a word.
2387 if (Size > 32)
2388 return false;
2389
2390 // FIXME: Handle vector types!
2391 if (Ty->isVectorType())
2392 return false;
2393
Daniel Dunbard53bac72009-09-14 02:20:34 +00002394 // Float types are never treated as "integer like".
2395 if (Ty->isRealFloatingType())
2396 return false;
2397
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002398 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002399 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002400 return true;
2401
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002402 // Small complex integer types are "integer like".
2403 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2404 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002405
2406 // Single element and zero sized arrays should be allowed, by the definition
2407 // above, but they are not.
2408
2409 // Otherwise, it must be a record type.
2410 const RecordType *RT = Ty->getAs<RecordType>();
2411 if (!RT) return false;
2412
2413 // Ignore records with flexible arrays.
2414 const RecordDecl *RD = RT->getDecl();
2415 if (RD->hasFlexibleArrayMember())
2416 return false;
2417
2418 // Check that all sub-fields are at offset 0, and are themselves "integer
2419 // like".
2420 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2421
2422 bool HadField = false;
2423 unsigned idx = 0;
2424 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2425 i != e; ++i, ++idx) {
2426 const FieldDecl *FD = *i;
2427
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002428 // Bit-fields are not addressable, we only need to verify they are "integer
2429 // like". We still have to disallow a subsequent non-bitfield, for example:
2430 // struct { int : 0; int x }
2431 // is non-integer like according to gcc.
2432 if (FD->isBitField()) {
2433 if (!RD->isUnion())
2434 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002435
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002436 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2437 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002438
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002439 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002440 }
2441
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002442 // Check if this field is at offset 0.
2443 if (Layout.getFieldOffset(idx) != 0)
2444 return false;
2445
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002446 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2447 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002448
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002449 // Only allow at most one field in a structure. This doesn't match the
2450 // wording above, but follows gcc in situations with a field following an
2451 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002452 if (!RD->isUnion()) {
2453 if (HadField)
2454 return false;
2455
2456 HadField = true;
2457 }
2458 }
2459
2460 return true;
2461}
2462
Chris Lattner458b2aa2010-07-29 02:16:43 +00002463ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002464 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002465 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002466
Daniel Dunbar19964db2010-09-23 01:54:32 +00002467 // Large vector types should be returned via memory.
2468 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2469 return ABIArgInfo::getIndirect(0);
2470
John McCalla1dee5302010-08-22 10:59:02 +00002471 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002472 // Treat an enum type as its underlying type.
2473 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2474 RetTy = EnumTy->getDecl()->getIntegerType();
2475
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002476 return (RetTy->isPromotableIntegerType() ?
2477 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002478 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002479
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002480 // Structures with either a non-trivial destructor or a non-trivial
2481 // copy constructor are always indirect.
2482 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2483 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2484
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002485 // Are we following APCS?
2486 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002487 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002488 return ABIArgInfo::getIgnore();
2489
Daniel Dunbareedf1512010-02-01 23:31:19 +00002490 // Complex types are all returned as packed integers.
2491 //
2492 // FIXME: Consider using 2 x vector types if the back end handles them
2493 // correctly.
2494 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002495 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002496 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002497
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002498 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002499 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002500 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002501 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002502 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002503 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002504 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002505 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2506 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002507 }
2508
2509 // Otherwise return in memory.
2510 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002511 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002512
2513 // Otherwise this is an AAPCS variant.
2514
Chris Lattner458b2aa2010-07-29 02:16:43 +00002515 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002516 return ABIArgInfo::getIgnore();
2517
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002518 // Aggregates <= 4 bytes are returned in r0; other aggregates
2519 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002520 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002521 if (Size <= 32) {
2522 // Return in the smallest viable integer type.
2523 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002524 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002525 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002526 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2527 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002528 }
2529
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002530 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002531}
2532
2533llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002534 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002535 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002536 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002537 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002538
2539 CGBuilderTy &Builder = CGF.Builder;
2540 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2541 "ap");
2542 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2543 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002544 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002545 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2546
2547 uint64_t Offset =
2548 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2549 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002550 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002551 "ap.next");
2552 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2553
2554 return AddrTyped;
2555}
2556
Chris Lattner0cf24192010-06-28 20:05:43 +00002557//===----------------------------------------------------------------------===//
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002558// PTX ABI Implementation
2559//===----------------------------------------------------------------------===//
2560
2561namespace {
2562
2563class PTXABIInfo : public ABIInfo {
2564public:
2565 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2566
2567 ABIArgInfo classifyReturnType(QualType RetTy) const;
2568 ABIArgInfo classifyArgumentType(QualType Ty) const;
2569
2570 virtual void computeInfo(CGFunctionInfo &FI) const;
2571 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2572 CodeGenFunction &CFG) const;
2573};
2574
2575class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2576public:
2577 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2578 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
2579};
2580
2581ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2582 if (RetTy->isVoidType())
2583 return ABIArgInfo::getIgnore();
2584 if (isAggregateTypeForABI(RetTy))
2585 return ABIArgInfo::getIndirect(0);
2586 return ABIArgInfo::getDirect();
2587}
2588
2589ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2590 if (isAggregateTypeForABI(Ty))
2591 return ABIArgInfo::getIndirect(0);
2592
2593 return ABIArgInfo::getDirect();
2594}
2595
2596void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2597 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2598 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2599 it != ie; ++it)
2600 it->info = classifyArgumentType(it->type);
2601
2602 // Always honor user-specified calling convention.
2603 if (FI.getCallingConvention() != llvm::CallingConv::C)
2604 return;
2605
2606 // Calling convention as default by an ABI.
2607 llvm::CallingConv::ID DefaultCC;
2608 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2609 if (Env == "device")
2610 DefaultCC = llvm::CallingConv::PTX_Device;
2611 else
2612 DefaultCC = llvm::CallingConv::PTX_Kernel;
2613
2614 FI.setEffectiveCallingConvention(DefaultCC);
2615}
2616
2617llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2618 CodeGenFunction &CFG) const {
2619 llvm_unreachable("PTX does not support varargs");
2620 return 0;
2621}
2622
2623}
2624
2625//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002626// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002627//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002628
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002629namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002630
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002631class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002632public:
2633 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2634
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002635 bool isPromotableIntegerType(QualType Ty) const;
2636
Chris Lattner458b2aa2010-07-29 02:16:43 +00002637 ABIArgInfo classifyReturnType(QualType RetTy) const;
2638 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002639
Chris Lattner22326a12010-07-29 02:31:05 +00002640 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002641 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002642 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2643 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002644 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002645 }
2646
2647 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2648 CodeGenFunction &CGF) const;
2649};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002650
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002651class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2652public:
Chris Lattner2b037972010-07-29 02:01:43 +00002653 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2654 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002655};
2656
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002657}
2658
2659bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2660 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002661 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002662 switch (BT->getKind()) {
2663 case BuiltinType::Bool:
2664 case BuiltinType::Char_S:
2665 case BuiltinType::Char_U:
2666 case BuiltinType::SChar:
2667 case BuiltinType::UChar:
2668 case BuiltinType::Short:
2669 case BuiltinType::UShort:
2670 case BuiltinType::Int:
2671 case BuiltinType::UInt:
2672 return true;
2673 default:
2674 return false;
2675 }
2676 return false;
2677}
2678
2679llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2680 CodeGenFunction &CGF) const {
2681 // FIXME: Implement
2682 return 0;
2683}
2684
2685
Chris Lattner458b2aa2010-07-29 02:16:43 +00002686ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2687 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002688 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002689 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002690 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002691
2692 return (isPromotableIntegerType(RetTy) ?
2693 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002694}
2695
Chris Lattner458b2aa2010-07-29 02:16:43 +00002696ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002697 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002698 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002699
2700 return (isPromotableIntegerType(Ty) ?
2701 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002702}
2703
Chris Lattner0cf24192010-06-28 20:05:43 +00002704//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00002705// MBlaze ABI Implementation
2706//===----------------------------------------------------------------------===//
2707
2708namespace {
2709
2710class MBlazeABIInfo : public ABIInfo {
2711public:
2712 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2713
2714 bool isPromotableIntegerType(QualType Ty) const;
2715
2716 ABIArgInfo classifyReturnType(QualType RetTy) const;
2717 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2718
2719 virtual void computeInfo(CGFunctionInfo &FI) const {
2720 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2721 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2722 it != ie; ++it)
2723 it->info = classifyArgumentType(it->type);
2724 }
2725
2726 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2727 CodeGenFunction &CGF) const;
2728};
2729
2730class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2731public:
2732 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2733 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2734 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2735 CodeGen::CodeGenModule &M) const;
2736};
2737
2738}
2739
2740bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2741 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2742 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2743 switch (BT->getKind()) {
2744 case BuiltinType::Bool:
2745 case BuiltinType::Char_S:
2746 case BuiltinType::Char_U:
2747 case BuiltinType::SChar:
2748 case BuiltinType::UChar:
2749 case BuiltinType::Short:
2750 case BuiltinType::UShort:
2751 return true;
2752 default:
2753 return false;
2754 }
2755 return false;
2756}
2757
2758llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2759 CodeGenFunction &CGF) const {
2760 // FIXME: Implement
2761 return 0;
2762}
2763
2764
2765ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2766 if (RetTy->isVoidType())
2767 return ABIArgInfo::getIgnore();
2768 if (isAggregateTypeForABI(RetTy))
2769 return ABIArgInfo::getIndirect(0);
2770
2771 return (isPromotableIntegerType(RetTy) ?
2772 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2773}
2774
2775ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2776 if (isAggregateTypeForABI(Ty))
2777 return ABIArgInfo::getIndirect(0);
2778
2779 return (isPromotableIntegerType(Ty) ?
2780 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2781}
2782
2783void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2784 llvm::GlobalValue *GV,
2785 CodeGen::CodeGenModule &M)
2786 const {
2787 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2788 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002789
Wesley Peck36a1f682010-12-19 19:57:51 +00002790 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2791 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2792 CC = llvm::CallingConv::MBLAZE_INTR;
2793 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2794 CC = llvm::CallingConv::MBLAZE_SVOL;
2795
2796 if (CC != llvm::CallingConv::C) {
2797 // Handle 'interrupt_handler' attribute:
2798 llvm::Function *F = cast<llvm::Function>(GV);
2799
2800 // Step 1: Set ISR calling convention.
2801 F->setCallingConv(CC);
2802
2803 // Step 2: Add attributes goodness.
2804 F->addFnAttr(llvm::Attribute::NoInline);
2805 }
2806
2807 // Step 3: Emit _interrupt_handler alias.
2808 if (CC == llvm::CallingConv::MBLAZE_INTR)
2809 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2810 "_interrupt_handler", GV, &M.getModule());
2811}
2812
2813
2814//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002815// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002816//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002817
2818namespace {
2819
2820class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2821public:
Chris Lattner2b037972010-07-29 02:01:43 +00002822 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2823 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002824 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2825 CodeGen::CodeGenModule &M) const;
2826};
2827
2828}
2829
2830void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2831 llvm::GlobalValue *GV,
2832 CodeGen::CodeGenModule &M) const {
2833 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2834 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2835 // Handle 'interrupt' attribute:
2836 llvm::Function *F = cast<llvm::Function>(GV);
2837
2838 // Step 1: Set ISR calling convention.
2839 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2840
2841 // Step 2: Add attributes goodness.
2842 F->addFnAttr(llvm::Attribute::NoInline);
2843
2844 // Step 3: Emit ISR vector alias.
2845 unsigned Num = attr->getNumber() + 0xffe0;
2846 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Benjamin Kramer8c173cc2010-11-12 15:42:18 +00002847 "vector_" + llvm::Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002848 GV, &M.getModule());
2849 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002850 }
2851}
2852
Chris Lattner0cf24192010-06-28 20:05:43 +00002853//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002854// MIPS ABI Implementation. This works for both little-endian and
2855// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002856//===----------------------------------------------------------------------===//
2857
John McCall943fae92010-05-27 06:19:26 +00002858namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00002859class MipsABIInfo : public ABIInfo {
2860public:
2861 MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2862
2863 ABIArgInfo classifyReturnType(QualType RetTy) const;
2864 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2865 virtual void computeInfo(CGFunctionInfo &FI) const;
2866 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2867 CodeGenFunction &CGF) const;
2868};
2869
John McCall943fae92010-05-27 06:19:26 +00002870class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2871public:
Chris Lattner2b037972010-07-29 02:01:43 +00002872 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
Akira Hatanakab579fe52011-06-02 00:09:17 +00002873 : TargetCodeGenInfo(new MipsABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002874
2875 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2876 return 29;
2877 }
2878
2879 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002880 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002881};
2882}
2883
Akira Hatanakab579fe52011-06-02 00:09:17 +00002884ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
2885 if (isAggregateTypeForABI(Ty)) {
2886 // Ignore empty aggregates.
2887 if (getContext().getTypeSize(Ty) == 0)
2888 return ABIArgInfo::getIgnore();
2889
2890 return ABIArgInfo::getIndirect(0);
2891 }
2892
2893 // Treat an enum type as its underlying type.
2894 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2895 Ty = EnumTy->getDecl()->getIntegerType();
2896
2897 return (Ty->isPromotableIntegerType() ?
2898 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2899}
2900
2901ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
2902 if (RetTy->isVoidType())
2903 return ABIArgInfo::getIgnore();
2904
2905 if (isAggregateTypeForABI(RetTy)) {
2906 if (RetTy->isAnyComplexType())
2907 return ABIArgInfo::getDirect();
2908
2909 return ABIArgInfo::getIndirect(0);
2910 }
2911
2912 // Treat an enum type as its underlying type.
2913 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2914 RetTy = EnumTy->getDecl()->getIntegerType();
2915
2916 return (RetTy->isPromotableIntegerType() ?
2917 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2918}
2919
2920void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
2921 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2922 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2923 it != ie; ++it)
2924 it->info = classifyArgumentType(it->type);
2925}
2926
2927llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2928 CodeGenFunction &CGF) const {
2929 return 0;
2930}
2931
John McCall943fae92010-05-27 06:19:26 +00002932bool
2933MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2934 llvm::Value *Address) const {
2935 // This information comes from gcc's implementation, which seems to
2936 // as canonical as it gets.
2937
2938 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2939 llvm::LLVMContext &Context = CGF.getLLVMContext();
2940
2941 // Everything on MIPS is 4 bytes. Double-precision FP registers
2942 // are aliased to pairs of single-precision FP registers.
2943 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2944 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2945
2946 // 0-31 are the general purpose registers, $0 - $31.
2947 // 32-63 are the floating-point registers, $f0 - $f31.
2948 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2949 // 66 is the (notional, I think) register for signal-handler return.
2950 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2951
2952 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2953 // They are one bit wide and ignored here.
2954
2955 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2956 // (coprocessor 1 is the FP unit)
2957 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2958 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2959 // 176-181 are the DSP accumulator registers.
2960 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2961
2962 return false;
2963}
2964
2965
Chris Lattner2b037972010-07-29 02:01:43 +00002966const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002967 if (TheTargetCodeGenInfo)
2968 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002969
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002970 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2971 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002972
Chris Lattner22a931e2010-06-29 06:01:59 +00002973 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002974 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002975 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002976 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002977
John McCall943fae92010-05-27 06:19:26 +00002978 case llvm::Triple::mips:
2979 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002980 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002981
Daniel Dunbard59655c2009-09-12 00:59:49 +00002982 case llvm::Triple::arm:
2983 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00002984 {
2985 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002986
Sandeep Patel45df3dd2011-04-05 00:23:47 +00002987 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
2988 Kind = ARMABIInfo::APCS;
2989 else if (CodeGenOpts.FloatABI == "hard")
2990 Kind = ARMABIInfo::AAPCS_VFP;
2991
2992 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
2993 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00002994
John McCallea8d8bb2010-03-11 00:10:12 +00002995 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002996 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002997
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002998 case llvm::Triple::ptx32:
2999 case llvm::Triple::ptx64:
3000 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3001
Daniel Dunbard59655c2009-09-12 00:59:49 +00003002 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00003003 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003004
Wesley Peck36a1f682010-12-19 19:57:51 +00003005 case llvm::Triple::mblaze:
3006 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3007
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003008 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003009 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003010
Eli Friedman33465822011-07-08 23:31:17 +00003011 case llvm::Triple::x86: {
3012 bool DisableMMX = strcmp(getContext().Target.getABI(), "no-mmx") == 0;
3013
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003014 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003015 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003016 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003017
3018 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003019 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003020 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003021 case llvm::Triple::AuroraUX:
3022 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003023 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003024 case llvm::Triple::OpenBSD:
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003025 case llvm::Triple::NetBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003026 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003027 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003028
3029 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003030 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003031 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003032 }
Eli Friedman33465822011-07-08 23:31:17 +00003033 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003034
Daniel Dunbare3532f82009-08-24 08:52:16 +00003035 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00003036 switch (Triple.getOS()) {
3037 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003038 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003039 case llvm::Triple::Cygwin:
3040 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3041 default:
3042 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
3043 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003044 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003045}