blob: c28f71a304e5b556d3957117a9c88af777ce1258 [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"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.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
131 // If this is a C++ record, check the bases first.
132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
133 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;
137
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
Bill Wendling5cd41c42010-10-18 03:41:31 +0000334/// UseX86_MMXType - Return true if this is an MMX type that should use the special
335/// x86_mmx type.
336bool UseX86_MMXType(const llvm::Type *IRType) {
337 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
338 // special x86_mmx type.
339 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
340 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
341 IRType->getScalarSizeInBits() != 64;
342}
343
Chris Lattner0cf24192010-06-28 20:05:43 +0000344//===----------------------------------------------------------------------===//
345// X86-32 ABI Implementation
346//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000347
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000348/// X86_32ABIInfo - The X86-32 ABI information.
349class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000350 static const unsigned MinABIStackAlignInBytes = 4;
351
David Chisnallde3a0692009-08-17 23:08:21 +0000352 bool IsDarwinVectorABI;
353 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000354
355 static bool isRegisterSize(unsigned Size) {
356 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
357 }
358
359 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
360
Daniel Dunbar557893d2010-04-21 19:10:51 +0000361 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
362 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000363 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000364
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000365 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000366 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000367
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000368public:
Chris Lattner2b037972010-07-29 02:01:43 +0000369
Chris Lattner458b2aa2010-07-29 02:16:43 +0000370 ABIArgInfo classifyReturnType(QualType RetTy) const;
371 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000372
Chris Lattner22326a12010-07-29 02:31:05 +0000373 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000374 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000375 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
376 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000377 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000378 }
379
380 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
381 CodeGenFunction &CGF) const;
382
Chris Lattner2b037972010-07-29 02:01:43 +0000383 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
384 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000385};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000386
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000387class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
388public:
Chris Lattner2b037972010-07-29 02:01:43 +0000389 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
390 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000391
392 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
393 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000394
395 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
396 // Darwin uses different dwarf register numbers for EH.
397 if (CGM.isTargetDarwin()) return 5;
398
399 return 4;
400 }
401
402 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
403 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000404};
405
406}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000407
408/// shouldReturnTypeInRegister - Determine if the given type should be
409/// passed in a register (for the Darwin ABI).
410bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
411 ASTContext &Context) {
412 uint64_t Size = Context.getTypeSize(Ty);
413
414 // Type must be register sized.
415 if (!isRegisterSize(Size))
416 return false;
417
418 if (Ty->isVectorType()) {
419 // 64- and 128- bit vectors inside structures are not returned in
420 // registers.
421 if (Size == 64 || Size == 128)
422 return false;
423
424 return true;
425 }
426
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000427 // If this is a builtin, pointer, enum, complex type, member pointer, or
428 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000429 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000430 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000431 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000432 return true;
433
434 // Arrays are treated like records.
435 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
436 return shouldReturnTypeInRegister(AT->getElementType(), Context);
437
438 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000439 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000440 if (!RT) return false;
441
Anders Carlsson40446e82010-01-27 03:25:19 +0000442 // FIXME: Traverse bases here too.
443
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000444 // Structure types are passed in register if all fields would be
445 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000446 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
447 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000448 const FieldDecl *FD = *i;
449
450 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000451 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000452 continue;
453
454 // Check fields recursively.
455 if (!shouldReturnTypeInRegister(FD->getType(), Context))
456 return false;
457 }
458
459 return true;
460}
461
Chris Lattner458b2aa2010-07-29 02:16:43 +0000462ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
463 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000464 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000465
Chris Lattner458b2aa2010-07-29 02:16:43 +0000466 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000467 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000468 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000469 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000470
471 // 128-bit vectors are a special case; they are returned in
472 // registers and we need to make sure to pick a type the LLVM
473 // backend will like.
474 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000475 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000476 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000477
478 // Always return in register if it fits in a general purpose
479 // register, or if it is 64 bits and has a single element.
480 if ((Size == 8 || Size == 16 || Size == 32) ||
481 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000482 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000483 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000484
485 return ABIArgInfo::getIndirect(0);
486 }
487
488 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000489 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000490
John McCalla1dee5302010-08-22 10:59:02 +0000491 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000492 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000493 // Structures with either a non-trivial destructor or a non-trivial
494 // copy constructor are always indirect.
495 if (hasNonTrivialDestructorOrCopyConstructor(RT))
496 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000497
Anders Carlsson5789c492009-10-20 22:07:59 +0000498 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000499 if (RT->getDecl()->hasFlexibleArrayMember())
500 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000501 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000502
David Chisnallde3a0692009-08-17 23:08:21 +0000503 // If specified, structs and unions are always indirect.
504 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000505 return ABIArgInfo::getIndirect(0);
506
507 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000508 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000509 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000510 if (BT->isIntegerType()) {
511 // We need to use the size of the structure, padding
512 // bit-fields can adjust that to be larger than the single
513 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000514 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000515 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000516 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
517 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000518
Chris Lattner458b2aa2010-07-29 02:16:43 +0000519 if (BT->getKind() == BuiltinType::Float) {
520 assert(getContext().getTypeSize(RetTy) ==
521 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000522 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000523 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000524 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000525
Chris Lattner458b2aa2010-07-29 02:16:43 +0000526 if (BT->getKind() == BuiltinType::Double) {
527 assert(getContext().getTypeSize(RetTy) ==
528 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000530 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000531 }
532 } else if (SeltTy->isPointerType()) {
533 // FIXME: It would be really nice if this could come out as the proper
534 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000535 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000536 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 } else if (SeltTy->isVectorType()) {
538 // 64- and 128-bit vectors are never returned in a
539 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000540 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000541 if (Size == 64 || Size == 128)
542 return ABIArgInfo::getIndirect(0);
543
Chris Lattner458b2aa2010-07-29 02:16:43 +0000544 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000545 }
546 }
547
548 // Small structures which are register sized are generally returned
549 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000550 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
551 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000552 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000553 }
554
555 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000556 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000557
Chris Lattner458b2aa2010-07-29 02:16:43 +0000558 // Treat an enum type as its underlying type.
559 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
560 RetTy = EnumTy->getDecl()->getIntegerType();
561
562 return (RetTy->isPromotableIntegerType() ?
563 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000564}
565
Daniel Dunbared23de32010-09-16 20:42:00 +0000566static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
567 const RecordType *RT = Ty->getAs<RecordType>();
568 if (!RT)
569 return 0;
570 const RecordDecl *RD = RT->getDecl();
571
572 // If this is a C++ record, check the bases first.
573 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
574 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
575 e = CXXRD->bases_end(); i != e; ++i)
576 if (!isRecordWithSSEVectorType(Context, i->getType()))
577 return false;
578
579 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
580 i != e; ++i) {
581 QualType FT = i->getType();
582
583 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
584 return true;
585
586 if (isRecordWithSSEVectorType(Context, FT))
587 return true;
588 }
589
590 return false;
591}
592
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000593unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
594 unsigned Align) const {
595 // Otherwise, if the alignment is less than or equal to the minimum ABI
596 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000597 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000598 return 0; // Use default alignment.
599
600 // On non-Darwin, the stack type alignment is always 4.
601 if (!IsDarwinVectorABI) {
602 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000603 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000604 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000605
Daniel Dunbared23de32010-09-16 20:42:00 +0000606 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
607 if (isRecordWithSSEVectorType(getContext(), Ty))
608 return 16;
609
610 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000611}
612
Chris Lattner458b2aa2010-07-29 02:16:43 +0000613ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000614 if (!ByVal)
615 return ABIArgInfo::getIndirect(0, false);
616
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000617 // Compute the byval alignment.
618 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
619 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
620 if (StackAlign == 0)
621 return ABIArgInfo::getIndirect(0);
622
623 // If the stack alignment is less than the type alignment, realign the
624 // argument.
625 if (StackAlign < TypeAlign)
626 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
627 /*Realign=*/true);
628
629 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000630}
631
Chris Lattner458b2aa2010-07-29 02:16:43 +0000632ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000633 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000634 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000635 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000636 if (const RecordType *RT = Ty->getAs<RecordType>()) {
637 // Structures with either a non-trivial destructor or a non-trivial
638 // copy constructor are always indirect.
639 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000640 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000641
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000642 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000643 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000644 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000645
646 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000647 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000648 return ABIArgInfo::getIgnore();
649
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000650 // Expand small (<= 128-bit) record types when we know that the stack layout
651 // of those arguments will match the struct. This is important because the
652 // LLVM backend isn't smart enough to remove byval, which inhibits many
653 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000654 if (getContext().getTypeSize(Ty) <= 4*32 &&
655 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000656 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000657
Chris Lattner458b2aa2010-07-29 02:16:43 +0000658 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000659 }
660
Chris Lattnerd774ae92010-08-26 20:05:13 +0000661 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000662 // On Darwin, some vectors are passed in memory, we handle this by passing
663 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000664 if (IsDarwinVectorABI) {
665 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000666 if ((Size == 8 || Size == 16 || Size == 32) ||
667 (Size == 64 && VT->getNumElements() == 1))
668 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
669 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000670 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000671
672 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
673 if (UseX86_MMXType(IRType)) {
674 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
675 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
676 return AAI;
677 }
Chris Lattnerd774ae92010-08-26 20:05:13 +0000678
679 return ABIArgInfo::getDirect();
680 }
681
682
Chris Lattner458b2aa2010-07-29 02:16:43 +0000683 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
684 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000685
Chris Lattner458b2aa2010-07-29 02:16:43 +0000686 return (Ty->isPromotableIntegerType() ?
687 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000688}
689
690llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
691 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000692 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000693 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000694
695 CGBuilderTy &Builder = CGF.Builder;
696 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
697 "ap");
698 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
699 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000700 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000701 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
702
703 uint64_t Offset =
704 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
705 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000706 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000707 "ap.next");
708 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
709
710 return AddrTyped;
711}
712
Charles Davis4ea31ab2010-02-13 15:54:06 +0000713void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
714 llvm::GlobalValue *GV,
715 CodeGen::CodeGenModule &CGM) const {
716 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
717 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
718 // Get the LLVM function.
719 llvm::Function *Fn = cast<llvm::Function>(GV);
720
721 // Now add the 'alignstack' attribute with a value of 16.
722 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
723 }
724 }
725}
726
John McCallbeec5a02010-03-06 00:35:14 +0000727bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
728 CodeGen::CodeGenFunction &CGF,
729 llvm::Value *Address) const {
730 CodeGen::CGBuilderTy &Builder = CGF.Builder;
731 llvm::LLVMContext &Context = CGF.getLLVMContext();
732
733 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
734 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000735
John McCallbeec5a02010-03-06 00:35:14 +0000736 // 0-7 are the eight integer registers; the order is different
737 // on Darwin (for EH), but the range is the same.
738 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000739 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000740
741 if (CGF.CGM.isTargetDarwin()) {
742 // 12-16 are st(0..4). Not sure why we stop at 4.
743 // These have size 16, which is sizeof(long double) on
744 // platforms with 8-byte alignment for that type.
745 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000746 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000747
John McCallbeec5a02010-03-06 00:35:14 +0000748 } else {
749 // 9 is %eflags, which doesn't get a size on Darwin for some
750 // reason.
751 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
752
753 // 11-16 are st(0..5). Not sure why we stop at 5.
754 // These have size 12, which is sizeof(long double) on
755 // platforms with 4-byte alignment for that type.
756 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000757 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
758 }
John McCallbeec5a02010-03-06 00:35:14 +0000759
760 return false;
761}
762
Chris Lattner0cf24192010-06-28 20:05:43 +0000763//===----------------------------------------------------------------------===//
764// X86-64 ABI Implementation
765//===----------------------------------------------------------------------===//
766
767
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000768namespace {
769/// X86_64ABIInfo - The X86_64 ABI information.
770class X86_64ABIInfo : public ABIInfo {
771 enum Class {
772 Integer = 0,
773 SSE,
774 SSEUp,
775 X87,
776 X87Up,
777 ComplexX87,
778 NoClass,
779 Memory
780 };
781
782 /// merge - Implement the X86_64 ABI merging algorithm.
783 ///
784 /// Merge an accumulating classification \arg Accum with a field
785 /// classification \arg Field.
786 ///
787 /// \param Accum - The accumulating classification. This should
788 /// always be either NoClass or the result of a previous merge
789 /// call. In addition, this should never be Memory (the caller
790 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000791 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000792
793 /// classify - Determine the x86_64 register classes in which the
794 /// given type T should be passed.
795 ///
796 /// \param Lo - The classification for the parts of the type
797 /// residing in the low word of the containing object.
798 ///
799 /// \param Hi - The classification for the parts of the type
800 /// residing in the high word of the containing object.
801 ///
802 /// \param OffsetBase - The bit offset of this type in the
803 /// containing object. Some parameters are classified different
804 /// depending on whether they straddle an eightbyte boundary.
805 ///
806 /// If a word is unused its result will be NoClass; if a type should
807 /// be passed in Memory then at least the classification of \arg Lo
808 /// will be Memory.
809 ///
810 /// The \arg Lo class will be NoClass iff the argument is ignored.
811 ///
812 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
813 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000814 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000815
Chris Lattner4200fe42010-07-29 04:56:46 +0000816 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000817 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000818 unsigned IROffset, QualType SourceTy,
819 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000820 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
821 unsigned IROffset, QualType SourceTy,
822 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000823
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000824 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000825 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000826 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000827
828 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000829 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000830 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000831
Chris Lattner458b2aa2010-07-29 02:16:43 +0000832 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000833
Bill Wendling5cd41c42010-10-18 03:41:31 +0000834 ABIArgInfo classifyArgumentType(QualType Ty,
835 unsigned &neededInt,
836 unsigned &neededSSE,
837 unsigned &neededMMX) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000838
839public:
Chris Lattner2b037972010-07-29 02:01:43 +0000840 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000841
Chris Lattner22326a12010-07-29 02:31:05 +0000842 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000843
844 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
845 CodeGenFunction &CGF) const;
846};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000847
Chris Lattner04dc9572010-08-31 16:44:54 +0000848/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
849class WinX86_64ABIInfo : public X86_64ABIInfo {
850public:
851 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
852
853 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
854 CodeGenFunction &CGF) const;
855};
856
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000857class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
858public:
Chris Lattner2b037972010-07-29 02:01:43 +0000859 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
860 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000861
862 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
863 return 7;
864 }
865
866 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
867 llvm::Value *Address) const {
868 CodeGen::CGBuilderTy &Builder = CGF.Builder;
869 llvm::LLVMContext &Context = CGF.getLLVMContext();
870
871 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
872 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000873
John McCall943fae92010-05-27 06:19:26 +0000874 // 0-15 are the 16 integer registers.
875 // 16 is %rip.
876 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000877
878 return false;
879 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000880};
881
Chris Lattner04dc9572010-08-31 16:44:54 +0000882class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
883public:
884 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
885 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
886
887 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
888 return 7;
889 }
890
891 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
892 llvm::Value *Address) const {
893 CodeGen::CGBuilderTy &Builder = CGF.Builder;
894 llvm::LLVMContext &Context = CGF.getLLVMContext();
895
896 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
897 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
898
899 // 0-15 are the 16 integer registers.
900 // 16 is %rip.
901 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
902
903 return false;
904 }
905};
906
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000907}
908
Chris Lattnerd776fb12010-06-28 21:43:59 +0000909X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000910 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
911 // classified recursively so that always two fields are
912 // considered. The resulting class is calculated according to
913 // the classes of the fields in the eightbyte:
914 //
915 // (a) If both classes are equal, this is the resulting class.
916 //
917 // (b) If one of the classes is NO_CLASS, the resulting class is
918 // the other class.
919 //
920 // (c) If one of the classes is MEMORY, the result is the MEMORY
921 // class.
922 //
923 // (d) If one of the classes is INTEGER, the result is the
924 // INTEGER.
925 //
926 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
927 // MEMORY is used as class.
928 //
929 // (f) Otherwise class SSE is used.
930
931 // Accum should never be memory (we should have returned) or
932 // ComplexX87 (because this cannot be passed in a structure).
933 assert((Accum != Memory && Accum != ComplexX87) &&
934 "Invalid accumulated classification during merge.");
935 if (Accum == Field || Field == NoClass)
936 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000937 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000938 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000939 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000940 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000941 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000942 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000943 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
944 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000945 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000946 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000947}
948
Chris Lattner5c740f12010-06-30 19:14:05 +0000949void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000950 Class &Lo, Class &Hi) const {
951 // FIXME: This code can be simplified by introducing a simple value class for
952 // Class pairs with appropriate constructor methods for the various
953 // situations.
954
955 // FIXME: Some of the split computations are wrong; unaligned vectors
956 // shouldn't be passed in registers for example, so there is no chance they
957 // can straddle an eightbyte. Verify & simplify.
958
959 Lo = Hi = NoClass;
960
961 Class &Current = OffsetBase < 64 ? Lo : Hi;
962 Current = Memory;
963
John McCall9dd450b2009-09-21 23:43:11 +0000964 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965 BuiltinType::Kind k = BT->getKind();
966
967 if (k == BuiltinType::Void) {
968 Current = NoClass;
969 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
970 Lo = Integer;
971 Hi = Integer;
972 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
973 Current = Integer;
974 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
975 Current = SSE;
976 } else if (k == BuiltinType::LongDouble) {
977 Lo = X87;
978 Hi = X87Up;
979 }
980 // FIXME: _Decimal32 and _Decimal64 are SSE.
981 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000982 return;
983 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000984
Chris Lattnerd776fb12010-06-28 21:43:59 +0000985 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000986 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000987 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000988 return;
989 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000990
Chris Lattnerd776fb12010-06-28 21:43:59 +0000991 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000992 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000993 return;
994 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000995
Chris Lattnerd776fb12010-06-28 21:43:59 +0000996 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000997 if (Ty->isMemberFunctionPointerType())
998 Lo = Hi = Integer;
999 else
1000 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001001 return;
1002 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001003
Chris Lattnerd776fb12010-06-28 21:43:59 +00001004 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001005 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001006 if (Size == 32) {
1007 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1008 // float> as integer.
1009 Current = Integer;
1010
1011 // If this type crosses an eightbyte boundary, it should be
1012 // split.
1013 uint64_t EB_Real = (OffsetBase) / 64;
1014 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1015 if (EB_Real != EB_Imag)
1016 Hi = Lo;
1017 } else if (Size == 64) {
1018 // gcc passes <1 x double> in memory. :(
1019 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1020 return;
1021
1022 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001023 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001024 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1025 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1026 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001027 Current = Integer;
1028 else
1029 Current = SSE;
1030
1031 // If this type crosses an eightbyte boundary, it should be
1032 // split.
1033 if (OffsetBase && OffsetBase != 64)
1034 Hi = Lo;
1035 } else if (Size == 128) {
1036 Lo = SSE;
1037 Hi = SSEUp;
1038 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001039 return;
1040 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001041
Chris Lattnerd776fb12010-06-28 21:43:59 +00001042 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001043 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001044
Chris Lattner2b037972010-07-29 02:01:43 +00001045 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001046 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001047 if (Size <= 64)
1048 Current = Integer;
1049 else if (Size <= 128)
1050 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001051 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001052 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001053 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001054 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001055 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001056 Current = ComplexX87;
1057
1058 // If this complex type crosses an eightbyte boundary then it
1059 // should be split.
1060 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001061 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001062 if (Hi == NoClass && EB_Real != EB_Imag)
1063 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001064
Chris Lattnerd776fb12010-06-28 21:43:59 +00001065 return;
1066 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001067
Chris Lattner2b037972010-07-29 02:01:43 +00001068 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001069 // Arrays are treated like structures.
1070
Chris Lattner2b037972010-07-29 02:01:43 +00001071 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001072
1073 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1074 // than two eightbytes, ..., it has class MEMORY.
1075 if (Size > 128)
1076 return;
1077
1078 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1079 // fields, it has class MEMORY.
1080 //
1081 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001082 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001083 return;
1084
1085 // Otherwise implement simplified merge. We could be smarter about
1086 // this, but it isn't worth it and would be harder to verify.
1087 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001088 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001089 uint64_t ArraySize = AT->getSize().getZExtValue();
1090 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1091 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001092 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001093 Lo = merge(Lo, FieldLo);
1094 Hi = merge(Hi, FieldHi);
1095 if (Lo == Memory || Hi == Memory)
1096 break;
1097 }
1098
1099 // Do post merger cleanup (see below). Only case we worry about is Memory.
1100 if (Hi == Memory)
1101 Lo = Memory;
1102 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001103 return;
1104 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001105
Chris Lattnerd776fb12010-06-28 21:43:59 +00001106 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001107 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001108
1109 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1110 // than two eightbytes, ..., it has class MEMORY.
1111 if (Size > 128)
1112 return;
1113
Anders Carlsson20759ad2009-09-16 15:53:40 +00001114 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1115 // copy constructor or a non-trivial destructor, it is passed by invisible
1116 // reference.
1117 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1118 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001119
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001120 const RecordDecl *RD = RT->getDecl();
1121
1122 // Assume variable sized types are passed in memory.
1123 if (RD->hasFlexibleArrayMember())
1124 return;
1125
Chris Lattner2b037972010-07-29 02:01:43 +00001126 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001127
1128 // Reset Lo class, this will be recomputed.
1129 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001130
1131 // If this is a C++ record, classify the bases first.
1132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1133 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1134 e = CXXRD->bases_end(); i != e; ++i) {
1135 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1136 "Unexpected base class!");
1137 const CXXRecordDecl *Base =
1138 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1139
1140 // Classify this field.
1141 //
1142 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1143 // single eightbyte, each is classified separately. Each eightbyte gets
1144 // initialized to class NO_CLASS.
1145 Class FieldLo, FieldHi;
1146 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001147 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001148 Lo = merge(Lo, FieldLo);
1149 Hi = merge(Hi, FieldHi);
1150 if (Lo == Memory || Hi == Memory)
1151 break;
1152 }
1153 }
1154
1155 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001156 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001157 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1158 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001159 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1160 bool BitField = i->isBitField();
1161
1162 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1163 // fields, it has class MEMORY.
1164 //
1165 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001166 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001167 Lo = Memory;
1168 return;
1169 }
1170
1171 // Classify this field.
1172 //
1173 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1174 // exceeds a single eightbyte, each is classified
1175 // separately. Each eightbyte gets initialized to class
1176 // NO_CLASS.
1177 Class FieldLo, FieldHi;
1178
1179 // Bit-fields require special handling, they do not force the
1180 // structure to be passed in memory even if unaligned, and
1181 // therefore they can straddle an eightbyte.
1182 if (BitField) {
1183 // Ignore padding bit-fields.
1184 if (i->isUnnamedBitfield())
1185 continue;
1186
1187 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001188 uint64_t Size =
1189 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001190
1191 uint64_t EB_Lo = Offset / 64;
1192 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1193 FieldLo = FieldHi = NoClass;
1194 if (EB_Lo) {
1195 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1196 FieldLo = NoClass;
1197 FieldHi = Integer;
1198 } else {
1199 FieldLo = Integer;
1200 FieldHi = EB_Hi ? Integer : NoClass;
1201 }
1202 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001203 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001204 Lo = merge(Lo, FieldLo);
1205 Hi = merge(Hi, FieldHi);
1206 if (Lo == Memory || Hi == Memory)
1207 break;
1208 }
1209
1210 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1211 //
1212 // (a) If one of the classes is MEMORY, the whole argument is
1213 // passed in memory.
1214 //
1215 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1216
1217 // The first of these conditions is guaranteed by how we implement
1218 // the merge (just bail).
1219 //
1220 // The second condition occurs in the case of unions; for example
1221 // union { _Complex double; unsigned; }.
1222 if (Hi == Memory)
1223 Lo = Memory;
1224 if (Hi == SSEUp && Lo != SSE)
1225 Hi = SSE;
1226 }
1227}
1228
Chris Lattner22a931e2010-06-29 06:01:59 +00001229ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001230 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1231 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001232 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001233 // Treat an enum type as its underlying type.
1234 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1235 Ty = EnumTy->getDecl()->getIntegerType();
1236
1237 return (Ty->isPromotableIntegerType() ?
1238 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1239 }
1240
1241 return ABIArgInfo::getIndirect(0);
1242}
1243
Chris Lattner22a931e2010-06-29 06:01:59 +00001244ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001245 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1246 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001247 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001248 // Treat an enum type as its underlying type.
1249 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1250 Ty = EnumTy->getDecl()->getIntegerType();
1251
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001252 return (Ty->isPromotableIntegerType() ?
1253 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001254 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001255
Daniel Dunbar53fac692010-04-21 19:49:55 +00001256 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1257 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001258
Daniel Dunbar53fac692010-04-21 19:49:55 +00001259 // Compute the byval alignment. We trust the back-end to honor the
1260 // minimum ABI alignment for byval, to make cleaner IR.
1261 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001262 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001263 if (Align > MinABIAlign)
1264 return ABIArgInfo::getIndirect(Align);
1265 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001266}
1267
Chris Lattner4200fe42010-07-29 04:56:46 +00001268/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1269/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1270/// vector register.
1271const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001272 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001273
Chris Lattner9fa15c32010-07-29 05:02:29 +00001274 // Wrapper structs that just contain vectors are passed just like vectors,
1275 // strip them off if present.
1276 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1277 while (STy && STy->getNumElements() == 1) {
1278 IRType = STy->getElementType(0);
1279 STy = dyn_cast<llvm::StructType>(IRType);
1280 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001281
Chris Lattner4200fe42010-07-29 04:56:46 +00001282 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001283 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001284 const llvm::Type *EltTy = VT->getElementType();
1285 if (VT->getBitWidth() == 128 &&
1286 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1287 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1288 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1289 EltTy->isIntegerTy(128)))
1290 return VT;
1291 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001292
Chris Lattner4200fe42010-07-29 04:56:46 +00001293 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1294}
1295
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001296/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1297/// is known to either be off the end of the specified type or being in
1298/// alignment padding. The user type specified is known to be at most 128 bits
1299/// in size, and have passed through X86_64ABIInfo::classify with a successful
1300/// classification that put one of the two halves in the INTEGER class.
1301///
1302/// It is conservatively correct to return false.
1303static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1304 unsigned EndBit, ASTContext &Context) {
1305 // If the bytes being queried are off the end of the type, there is no user
1306 // data hiding here. This handles analysis of builtins, vectors and other
1307 // types that don't contain interesting padding.
1308 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1309 if (TySize <= StartBit)
1310 return true;
1311
Chris Lattner98076a22010-07-29 07:43:55 +00001312 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1313 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1314 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1315
1316 // Check each element to see if the element overlaps with the queried range.
1317 for (unsigned i = 0; i != NumElts; ++i) {
1318 // If the element is after the span we care about, then we're done..
1319 unsigned EltOffset = i*EltSize;
1320 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001321
Chris Lattner98076a22010-07-29 07:43:55 +00001322 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1323 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1324 EndBit-EltOffset, Context))
1325 return false;
1326 }
1327 // If it overlaps no elements, then it is safe to process as padding.
1328 return true;
1329 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001330
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001331 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1332 const RecordDecl *RD = RT->getDecl();
1333 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001334
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001335 // If this is a C++ record, check the bases first.
1336 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1337 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1338 e = CXXRD->bases_end(); i != e; ++i) {
1339 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1340 "Unexpected base class!");
1341 const CXXRecordDecl *Base =
1342 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001343
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001344 // If the base is after the span we care about, ignore it.
1345 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1346 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001347
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001348 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1349 if (!BitsContainNoUserData(i->getType(), BaseStart,
1350 EndBit-BaseOffset, Context))
1351 return false;
1352 }
1353 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001354
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001355 // Verify that no field has data that overlaps the region of interest. Yes
1356 // this could be sped up a lot by being smarter about queried fields,
1357 // however we're only looking at structs up to 16 bytes, so we don't care
1358 // much.
1359 unsigned idx = 0;
1360 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1361 i != e; ++i, ++idx) {
1362 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001363
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001364 // If we found a field after the region we care about, then we're done.
1365 if (FieldOffset >= EndBit) break;
1366
1367 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1368 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1369 Context))
1370 return false;
1371 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001372
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001373 // If nothing in this record overlapped the area of interest, then we're
1374 // clean.
1375 return true;
1376 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001377
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001378 return false;
1379}
1380
Chris Lattnere556a712010-07-29 18:39:32 +00001381/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1382/// float member at the specified offset. For example, {int,{float}} has a
1383/// float at offset 4. It is conservatively correct for this routine to return
1384/// false.
1385static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1386 const llvm::TargetData &TD) {
1387 // Base case if we find a float.
1388 if (IROffset == 0 && IRType->isFloatTy())
1389 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001390
Chris Lattnere556a712010-07-29 18:39:32 +00001391 // If this is a struct, recurse into the field at the specified offset.
1392 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1393 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1394 unsigned Elt = SL->getElementContainingOffset(IROffset);
1395 IROffset -= SL->getElementOffset(Elt);
1396 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1397 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001398
Chris Lattnere556a712010-07-29 18:39:32 +00001399 // If this is an array, recurse into the field at the specified offset.
1400 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1401 const llvm::Type *EltTy = ATy->getElementType();
1402 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1403 IROffset -= IROffset/EltSize*EltSize;
1404 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1405 }
1406
1407 return false;
1408}
1409
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001410
1411/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1412/// low 8 bytes of an XMM register, corresponding to the SSE class.
1413const llvm::Type *X86_64ABIInfo::
1414GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1415 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001416 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001417 // pass as float if the last 4 bytes is just padding. This happens for
1418 // structs that contain 3 floats.
1419 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1420 SourceOffset*8+64, getContext()))
1421 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001422
Chris Lattnere556a712010-07-29 18:39:32 +00001423 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1424 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1425 // case.
1426 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001427 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1428 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001429
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001430 return llvm::Type::getDoubleTy(getVMContext());
1431}
1432
1433
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001434/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1435/// an 8-byte GPR. This means that we either have a scalar or we are talking
1436/// about the high or low part of an up-to-16-byte struct. This routine picks
1437/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001438/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1439/// etc).
1440///
1441/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1442/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1443/// the 8-byte value references. PrefType may be null.
1444///
1445/// SourceTy is the source level type for the entire argument. SourceOffset is
1446/// an offset into this that we're processing (which is always either 0 or 8).
1447///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001448const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001449GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1450 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001451 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1452 // returning an 8-byte unit starting with it. See if we can safely use it.
1453 if (IROffset == 0) {
1454 // Pointers and int64's always fill the 8-byte unit.
1455 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1456 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001457
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001458 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1459 // goodness in the source type is just tail padding. This is allowed to
1460 // kick in for struct {double,int} on the int, but not on
1461 // struct{double,int,int} because we wouldn't return the second int. We
1462 // have to do this analysis on the source type because we can't depend on
1463 // unions being lowered a specific way etc.
1464 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1465 IRType->isIntegerTy(32)) {
1466 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001467
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001468 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1469 SourceOffset*8+64, getContext()))
1470 return IRType;
1471 }
1472 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001473
Chris Lattnerce1bd752010-07-29 04:51:12 +00001474 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001475 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001476 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001477 if (IROffset < SL->getSizeInBytes()) {
1478 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1479 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001480
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001481 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1482 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001483 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001484 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001485
Chris Lattner98076a22010-07-29 07:43:55 +00001486 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1487 const llvm::Type *EltTy = ATy->getElementType();
1488 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1489 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001490 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1491 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001492 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001493
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001494 // Okay, we don't have any better idea of what to pass, so we pass this in an
1495 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001496 unsigned TySizeInBytes =
1497 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001498
Chris Lattner3f763422010-07-29 17:34:39 +00001499 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001500
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001501 // It is always safe to classify this as an integer type up to i64 that
1502 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001503 return llvm::IntegerType::get(getVMContext(),
1504 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001505}
1506
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001507
1508/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1509/// be used as elements of a two register pair to pass or return, return a
1510/// first class aggregate to represent them. For example, if the low part of
1511/// a by-value argument should be passed as i32* and the high part as float,
1512/// return {i32*, float}.
1513static const llvm::Type *
1514GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1515 const llvm::TargetData &TD) {
1516 // In order to correctly satisfy the ABI, we need to the high part to start
1517 // at offset 8. If the high and low parts we inferred are both 4-byte types
1518 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1519 // the second element at offset 8. Check for this:
1520 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1521 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1522 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1523 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
1524
1525 // To handle this, we have to increase the size of the low part so that the
1526 // second element will start at an 8 byte offset. We can't increase the size
1527 // of the second element because it might make us access off the end of the
1528 // struct.
1529 if (HiStart != 8) {
1530 // There are only two sorts of types the ABI generation code can produce for
1531 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1532 // Promote these to a larger type.
1533 if (Lo->isFloatTy())
1534 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1535 else {
1536 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1537 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1538 }
1539 }
1540
1541 const llvm::StructType *Result =
1542 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
1543
1544
1545 // Verify that the second element is at an 8-byte offset.
1546 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1547 "Invalid x86-64 argument pair!");
1548 return Result;
1549}
1550
Chris Lattner31faff52010-07-28 23:06:14 +00001551ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001552classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001553 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1554 // classification algorithm.
1555 X86_64ABIInfo::Class Lo, Hi;
1556 classify(RetTy, 0, Lo, Hi);
1557
1558 // Check some invariants.
1559 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001560 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1561
1562 const llvm::Type *ResType = 0;
1563 switch (Lo) {
1564 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001565 if (Hi == NoClass)
1566 return ABIArgInfo::getIgnore();
1567 // If the low part is just padding, it takes no register, leave ResType
1568 // null.
1569 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1570 "Unknown missing lo part");
1571 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001572
1573 case SSEUp:
1574 case X87Up:
1575 assert(0 && "Invalid classification for lo word.");
1576
1577 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1578 // hidden argument.
1579 case Memory:
1580 return getIndirectReturnResult(RetTy);
1581
1582 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1583 // available register of the sequence %rax, %rdx is used.
1584 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001585 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1586 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001587
Chris Lattner1f3a0632010-07-29 21:42:50 +00001588 // If we have a sign or zero extended integer, make sure to return Extend
1589 // so that the parameter gets the right LLVM IR attributes.
1590 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1591 // Treat an enum type as its underlying type.
1592 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1593 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001594
Chris Lattner1f3a0632010-07-29 21:42:50 +00001595 if (RetTy->isIntegralOrEnumerationType() &&
1596 RetTy->isPromotableIntegerType())
1597 return ABIArgInfo::getExtend();
1598 }
Chris Lattner31faff52010-07-28 23:06:14 +00001599 break;
1600
1601 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1602 // available SSE register of the sequence %xmm0, %xmm1 is used.
1603 case SSE:
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001604 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001605 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001606
1607 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1608 // returned on the X87 stack in %st0 as 80-bit x87 number.
1609 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001610 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001611 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001612
1613 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1614 // part of the value is returned in %st0 and the imaginary part in
1615 // %st1.
1616 case ComplexX87:
1617 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001618 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001619 llvm::Type::getX86_FP80Ty(getVMContext()),
1620 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001621 NULL);
1622 break;
1623 }
1624
Chris Lattner52b3c132010-09-01 00:20:33 +00001625 const llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001626 switch (Hi) {
1627 // Memory was handled previously and X87 should
1628 // never occur as a hi class.
1629 case Memory:
1630 case X87:
1631 assert(0 && "Invalid classification for hi word.");
1632
1633 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001634 case NoClass:
1635 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001636
Chris Lattner52b3c132010-09-01 00:20:33 +00001637 case Integer:
1638 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1639 8, RetTy, 8);
1640 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1641 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001642 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001643 case SSE:
1644 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1645 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1646 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001647 break;
1648
1649 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1650 // is passed in the upper half of the last used SSE register.
1651 //
1652 // SSEUP should always be preceeded by SSE, just widen.
1653 case SSEUp:
1654 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001655 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001656 break;
1657
1658 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1659 // returned together with the previous X87 value in %st0.
1660 case X87Up:
1661 // If X87Up is preceeded by X87, we don't need to do
1662 // anything. However, in some cases with unions it may not be
1663 // preceeded by X87. In such situations we follow gcc and pass the
1664 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001665 if (Lo != X87) {
Chris Lattner52b3c132010-09-01 00:20:33 +00001666 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1667 8, RetTy, 8);
1668 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1669 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001670 }
Chris Lattner31faff52010-07-28 23:06:14 +00001671 break;
1672 }
Chris Lattner52b3c132010-09-01 00:20:33 +00001673
1674 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001675 // known to pass in the high eightbyte of the result. We do this by forming a
1676 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001677 if (HighPart)
1678 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001679
Chris Lattner1f3a0632010-07-29 21:42:50 +00001680 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001681}
1682
Chris Lattner458b2aa2010-07-29 02:16:43 +00001683ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling5cd41c42010-10-18 03:41:31 +00001684 unsigned &neededSSE,
1685 unsigned &neededMMX) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001686 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001687 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001688
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001689 // Check some invariants.
1690 // FIXME: Enforce these by construction.
1691 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001692 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1693
1694 neededInt = 0;
1695 neededSSE = 0;
Bill Wendling5cd41c42010-10-18 03:41:31 +00001696 neededMMX = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001697 const llvm::Type *ResType = 0;
1698 switch (Lo) {
1699 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001700 if (Hi == NoClass)
1701 return ABIArgInfo::getIgnore();
1702 // If the low part is just padding, it takes no register, leave ResType
1703 // null.
1704 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1705 "Unknown missing lo part");
1706 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001707
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001708 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1709 // on the stack.
1710 case Memory:
1711
1712 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1713 // COMPLEX_X87, it is passed in memory.
1714 case X87:
1715 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001716 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001717
1718 case SSEUp:
1719 case X87Up:
1720 assert(0 && "Invalid classification for lo word.");
1721
1722 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1723 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1724 // and %r9 is used.
1725 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001726 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001727
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001728 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001729 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001730
1731 // If we have a sign or zero extended integer, make sure to return Extend
1732 // so that the parameter gets the right LLVM IR attributes.
1733 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1734 // Treat an enum type as its underlying type.
1735 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1736 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001737
Chris Lattner1f3a0632010-07-29 21:42:50 +00001738 if (Ty->isIntegralOrEnumerationType() &&
1739 Ty->isPromotableIntegerType())
1740 return ABIArgInfo::getExtend();
1741 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001742
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001743 break;
1744
1745 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1746 // available SSE register is used, the registers are taken in the
1747 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001748 case SSE: {
1749 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
1750 if (Hi != NoClass || !UseX86_MMXType(IRType)) {
1751 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
1752 ++neededSSE;
1753 } else {
1754 // This is an MMX type. Treat it as such.
1755 ResType = llvm::Type::getX86_MMXTy(getVMContext());
1756 ++neededMMX;
1757 }
1758
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001759 break;
1760 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001761 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001762
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001763 const llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001764 switch (Hi) {
1765 // Memory was handled previously, ComplexX87 and X87 should
1766 // never occur as hi classes, and X87Up must be preceed by X87,
1767 // which is passed in memory.
1768 case Memory:
1769 case X87:
1770 case ComplexX87:
1771 assert(0 && "Invalid classification for hi word.");
1772 break;
1773
1774 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001775
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001776 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001777 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001778 // Pick an 8-byte type based on the preferred type.
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001779 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001780
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001781 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1782 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001783 break;
1784
1785 // X87Up generally doesn't occur here (long double is passed in
1786 // memory), except in situations involving unions.
1787 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001788 case SSE:
1789 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001790
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001791 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1792 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001793
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001794 ++neededSSE;
1795 break;
1796
1797 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1798 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001799 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001800 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001801 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001802 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001803 break;
1804 }
1805
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001806 // If a high part was specified, merge it together with the low part. It is
1807 // known to pass in the high eightbyte of the result. We do this by forming a
1808 // first class struct aggregate with the high and low part: {low, high}
1809 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001810 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001811
Chris Lattner1f3a0632010-07-29 21:42:50 +00001812 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001813}
1814
Chris Lattner22326a12010-07-29 02:31:05 +00001815void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001816
Chris Lattner458b2aa2010-07-29 02:16:43 +00001817 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001818
1819 // Keep track of the number of assigned registers.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001820 unsigned freeIntRegs = 6, freeSSERegs = 8, freeMMXRegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001821
1822 // If the return value is indirect, then the hidden argument is consuming one
1823 // integer register.
1824 if (FI.getReturnInfo().isIndirect())
1825 --freeIntRegs;
1826
1827 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1828 // get assigned (in left-to-right order) for passing as follows...
1829 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1830 it != ie; ++it) {
Bill Wendling5cd41c42010-10-18 03:41:31 +00001831 unsigned neededInt, neededSSE, neededMMX;
1832 it->info = classifyArgumentType(it->type, neededInt, neededSSE, neededMMX);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001833
1834 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1835 // eightbyte of an argument, the whole argument is passed on the
1836 // stack. If registers have already been assigned for some
1837 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001838 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE &&
1839 freeMMXRegs >= neededMMX) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001840 freeIntRegs -= neededInt;
1841 freeSSERegs -= neededSSE;
Bill Wendling5cd41c42010-10-18 03:41:31 +00001842 freeMMXRegs -= neededMMX;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001843 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001844 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001845 }
1846 }
1847}
1848
1849static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1850 QualType Ty,
1851 CodeGenFunction &CGF) {
1852 llvm::Value *overflow_arg_area_p =
1853 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1854 llvm::Value *overflow_arg_area =
1855 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1856
1857 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1858 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1859 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1860 if (Align > 8) {
1861 // Note that we follow the ABI & gcc here, even though the type
1862 // could in theory have an alignment greater than 16. This case
1863 // shouldn't ever matter in practice.
1864
1865 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001866 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001867 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001868 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1869 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001870 CGF.Int64Ty);
1871 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001872 overflow_arg_area =
1873 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1874 overflow_arg_area->getType(),
1875 "overflow_arg_area.align");
1876 }
1877
1878 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1879 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1880 llvm::Value *Res =
1881 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001882 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001883
1884 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1885 // l->overflow_arg_area + sizeof(type).
1886 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1887 // an 8 byte boundary.
1888
1889 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001890 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001891 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001892 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1893 "overflow_arg_area.next");
1894 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1895
1896 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1897 return Res;
1898}
1899
1900llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1901 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001902 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001903
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001904 // Assume that va_list type is correct; should be pointer to LLVM type:
1905 // struct {
1906 // i32 gp_offset;
1907 // i32 fp_offset;
1908 // i8* overflow_arg_area;
1909 // i8* reg_save_area;
1910 // };
Bill Wendling5cd41c42010-10-18 03:41:31 +00001911 unsigned neededInt, neededSSE, neededMMX;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001912
Chris Lattner9723d6c2010-03-11 18:19:55 +00001913 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling5cd41c42010-10-18 03:41:31 +00001914 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE, neededMMX);
1915
1916 // Lump the MMX in with SSE.
1917 neededSSE += neededMMX;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001918
1919 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1920 // in the registers. If not go to step 7.
1921 if (!neededInt && !neededSSE)
1922 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1923
1924 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1925 // general purpose registers needed to pass type and num_fp to hold
1926 // the number of floating point registers needed.
1927
1928 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1929 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1930 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1931 //
1932 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1933 // register save space).
1934
1935 llvm::Value *InRegs = 0;
1936 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1937 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1938 if (neededInt) {
1939 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1940 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001941 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1942 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001943 }
1944
1945 if (neededSSE) {
1946 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1947 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1948 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001949 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1950 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1952 }
1953
1954 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1955 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1956 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1957 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1958
1959 // Emit code to load the value if it was passed in registers.
1960
1961 CGF.EmitBlock(InRegBlock);
1962
1963 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1964 // an offset of l->gp_offset and/or l->fp_offset. This may require
1965 // copying to a temporary location in case the parameter is passed
1966 // in different register classes or requires an alignment greater
1967 // than 8 for general purpose registers and 16 for XMM registers.
1968 //
1969 // FIXME: This really results in shameful code when we end up needing to
1970 // collect arguments from different places; often what should result in a
1971 // simple assembling of a structure from scattered addresses has many more
1972 // loads than necessary. Can we clean this up?
1973 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1974 llvm::Value *RegAddr =
1975 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1976 "reg_save_area");
1977 if (neededInt && neededSSE) {
1978 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001979 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001980 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1981 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1982 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1983 const llvm::Type *TyLo = ST->getElementType(0);
1984 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00001985 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001986 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001987 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1988 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001989 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1990 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001991 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1992 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001993 llvm::Value *V =
1994 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1995 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1996 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1997 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1998
Owen Anderson170229f2009-07-14 23:10:40 +00001999 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002000 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002001 } else if (neededInt) {
2002 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2003 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002004 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002005 } else if (neededSSE == 1) {
2006 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2007 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2008 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002009 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002010 assert(neededSSE == 2 && "Invalid number of needed registers!");
2011 // SSE registers are spaced 16 bytes apart in the register save
2012 // area, we need to collect the two eightbytes together.
2013 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002014 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00002015 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
2016 const llvm::Type *DblPtrTy =
2017 llvm::PointerType::getUnqual(DoubleTy);
2018 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
2019 DoubleTy, NULL);
2020 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2021 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2022 DblPtrTy));
2023 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2024 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2025 DblPtrTy));
2026 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2027 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2028 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002029 }
2030
2031 // AMD64-ABI 3.5.7p5: Step 5. Set:
2032 // l->gp_offset = l->gp_offset + num_gp * 8
2033 // l->fp_offset = l->fp_offset + num_fp * 16.
2034 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002035 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002036 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2037 gp_offset_p);
2038 }
2039 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002040 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002041 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2042 fp_offset_p);
2043 }
2044 CGF.EmitBranch(ContBlock);
2045
2046 // Emit code to load the value if it was passed in memory.
2047
2048 CGF.EmitBlock(InMemBlock);
2049 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2050
2051 // Return the appropriate result.
2052
2053 CGF.EmitBlock(ContBlock);
2054 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2055 "vaarg.addr");
2056 ResAddr->reserveOperandSpace(2);
2057 ResAddr->addIncoming(RegAddr, InRegBlock);
2058 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002059 return ResAddr;
2060}
2061
Chris Lattner04dc9572010-08-31 16:44:54 +00002062llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2063 CodeGenFunction &CGF) const {
2064 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2065 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002066
Chris Lattner04dc9572010-08-31 16:44:54 +00002067 CGBuilderTy &Builder = CGF.Builder;
2068 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2069 "ap");
2070 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2071 llvm::Type *PTy =
2072 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2073 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2074
2075 uint64_t Offset =
2076 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2077 llvm::Value *NextAddr =
2078 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2079 "ap.next");
2080 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2081
2082 return AddrTyped;
2083}
Chris Lattner0cf24192010-06-28 20:05:43 +00002084
John McCallea8d8bb2010-03-11 00:10:12 +00002085// PowerPC-32
2086
2087namespace {
2088class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2089public:
Chris Lattner2b037972010-07-29 02:01:43 +00002090 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002091
John McCallea8d8bb2010-03-11 00:10:12 +00002092 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2093 // This is recovered from gcc output.
2094 return 1; // r1 is the dedicated stack pointer
2095 }
2096
2097 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002098 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002099};
2100
2101}
2102
2103bool
2104PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2105 llvm::Value *Address) const {
2106 // This is calculated from the LLVM and GCC tables and verified
2107 // against gcc output. AFAIK all ABIs use the same encoding.
2108
2109 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2110 llvm::LLVMContext &Context = CGF.getLLVMContext();
2111
2112 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2113 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2114 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2115 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2116
2117 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002118 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002119
2120 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002121 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002122
2123 // 64-76 are various 4-byte special-purpose registers:
2124 // 64: mq
2125 // 65: lr
2126 // 66: ctr
2127 // 67: ap
2128 // 68-75 cr0-7
2129 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002130 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002131
2132 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002133 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002134
2135 // 109: vrsave
2136 // 110: vscr
2137 // 111: spe_acc
2138 // 112: spefscr
2139 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002140 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002141
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002142 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002143}
2144
2145
Chris Lattner0cf24192010-06-28 20:05:43 +00002146//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002147// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002148//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002149
2150namespace {
2151
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002152class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002153public:
2154 enum ABIKind {
2155 APCS = 0,
2156 AAPCS = 1,
2157 AAPCS_VFP
2158 };
2159
2160private:
2161 ABIKind Kind;
2162
2163public:
Chris Lattner2b037972010-07-29 02:01:43 +00002164 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002165
2166private:
2167 ABIKind getABIKind() const { return Kind; }
2168
Chris Lattner458b2aa2010-07-29 02:16:43 +00002169 ABIArgInfo classifyReturnType(QualType RetTy) const;
2170 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002171
Chris Lattner22326a12010-07-29 02:31:05 +00002172 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002173
2174 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2175 CodeGenFunction &CGF) const;
2176};
2177
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002178class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2179public:
Chris Lattner2b037972010-07-29 02:01:43 +00002180 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2181 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002182
2183 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2184 return 13;
2185 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002186};
2187
Daniel Dunbard59655c2009-09-12 00:59:49 +00002188}
2189
Chris Lattner22326a12010-07-29 02:31:05 +00002190void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002191 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002192 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002193 it != ie; ++it)
2194 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002195
Chris Lattner458b2aa2010-07-29 02:16:43 +00002196 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002197 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002198 if (Triple.getEnvironmentName() == "gnueabi" ||
2199 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002200 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002201 else
2202 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002203
Daniel Dunbar020daa92009-09-12 01:00:39 +00002204 switch (getABIKind()) {
2205 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002206 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2207 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002208 break;
2209
2210 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002211 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2212 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002213 break;
2214
2215 case AAPCS_VFP:
2216 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2217 break;
2218 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002219}
2220
Chris Lattner458b2aa2010-07-29 02:16:43 +00002221ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002222 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002223 // Treat an enum type as its underlying type.
2224 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2225 Ty = EnumTy->getDecl()->getIntegerType();
2226
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002227 return (Ty->isPromotableIntegerType() ?
2228 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002229 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002230
Daniel Dunbar09d33622009-09-14 21:54:03 +00002231 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002232 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002233 return ABIArgInfo::getIgnore();
2234
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002235 // Structures with either a non-trivial destructor or a non-trivial
2236 // copy constructor are always indirect.
2237 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2238 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2239
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002240 // NEON vectors are implemented as (theoretically) opaque structures wrapping
2241 // the underlying vector type. We trust the backend to pass the underlying
2242 // vectors appropriately, so we can unwrap the structs which generally will
2243 // lead to much cleaner IR.
2244 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) {
2245 if (SeltTy->isVectorType())
2246 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
2247 }
2248
2249 // Otherwise, pass by coercing to a structure of the appropriate size.
2250 //
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002251 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2252 // backend doesn't support byval.
2253 // FIXME: This doesn't handle alignment > 64 bits.
2254 const llvm::Type* ElemTy;
2255 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002256 if (getContext().getTypeAlign(Ty) > 32) {
2257 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2258 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002259 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002260 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2261 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002262 }
2263 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002264 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002265 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2266 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002267 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002268}
2269
Chris Lattner458b2aa2010-07-29 02:16:43 +00002270static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002271 llvm::LLVMContext &VMContext) {
2272 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2273 // is called integer-like if its size is less than or equal to one word, and
2274 // the offset of each of its addressable sub-fields is zero.
2275
2276 uint64_t Size = Context.getTypeSize(Ty);
2277
2278 // Check that the type fits in a word.
2279 if (Size > 32)
2280 return false;
2281
2282 // FIXME: Handle vector types!
2283 if (Ty->isVectorType())
2284 return false;
2285
Daniel Dunbard53bac72009-09-14 02:20:34 +00002286 // Float types are never treated as "integer like".
2287 if (Ty->isRealFloatingType())
2288 return false;
2289
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002290 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002291 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002292 return true;
2293
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002294 // Small complex integer types are "integer like".
2295 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2296 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002297
2298 // Single element and zero sized arrays should be allowed, by the definition
2299 // above, but they are not.
2300
2301 // Otherwise, it must be a record type.
2302 const RecordType *RT = Ty->getAs<RecordType>();
2303 if (!RT) return false;
2304
2305 // Ignore records with flexible arrays.
2306 const RecordDecl *RD = RT->getDecl();
2307 if (RD->hasFlexibleArrayMember())
2308 return false;
2309
2310 // Check that all sub-fields are at offset 0, and are themselves "integer
2311 // like".
2312 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2313
2314 bool HadField = false;
2315 unsigned idx = 0;
2316 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2317 i != e; ++i, ++idx) {
2318 const FieldDecl *FD = *i;
2319
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002320 // Bit-fields are not addressable, we only need to verify they are "integer
2321 // like". We still have to disallow a subsequent non-bitfield, for example:
2322 // struct { int : 0; int x }
2323 // is non-integer like according to gcc.
2324 if (FD->isBitField()) {
2325 if (!RD->isUnion())
2326 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002327
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002328 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2329 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002330
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002331 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002332 }
2333
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002334 // Check if this field is at offset 0.
2335 if (Layout.getFieldOffset(idx) != 0)
2336 return false;
2337
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002338 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2339 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002340
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002341 // Only allow at most one field in a structure. This doesn't match the
2342 // wording above, but follows gcc in situations with a field following an
2343 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002344 if (!RD->isUnion()) {
2345 if (HadField)
2346 return false;
2347
2348 HadField = true;
2349 }
2350 }
2351
2352 return true;
2353}
2354
Chris Lattner458b2aa2010-07-29 02:16:43 +00002355ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002356 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002357 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002358
Daniel Dunbar19964db2010-09-23 01:54:32 +00002359 // Large vector types should be returned via memory.
2360 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2361 return ABIArgInfo::getIndirect(0);
2362
John McCalla1dee5302010-08-22 10:59:02 +00002363 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002364 // Treat an enum type as its underlying type.
2365 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2366 RetTy = EnumTy->getDecl()->getIntegerType();
2367
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002368 return (RetTy->isPromotableIntegerType() ?
2369 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002370 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002371
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002372 // Structures with either a non-trivial destructor or a non-trivial
2373 // copy constructor are always indirect.
2374 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2375 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2376
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002377 // Are we following APCS?
2378 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002379 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002380 return ABIArgInfo::getIgnore();
2381
Daniel Dunbareedf1512010-02-01 23:31:19 +00002382 // Complex types are all returned as packed integers.
2383 //
2384 // FIXME: Consider using 2 x vector types if the back end handles them
2385 // correctly.
2386 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002387 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002388 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002389
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002390 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002391 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002392 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002393 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002394 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002395 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002396 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002397 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2398 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002399 }
2400
2401 // Otherwise return in memory.
2402 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002403 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002404
2405 // Otherwise this is an AAPCS variant.
2406
Chris Lattner458b2aa2010-07-29 02:16:43 +00002407 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002408 return ABIArgInfo::getIgnore();
2409
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002410 // Aggregates <= 4 bytes are returned in r0; other aggregates
2411 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002412 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002413 if (Size <= 32) {
2414 // Return in the smallest viable integer type.
2415 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002416 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002417 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002418 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2419 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002420 }
2421
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002422 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002423}
2424
2425llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002426 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002427 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002428 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002429 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002430
2431 CGBuilderTy &Builder = CGF.Builder;
2432 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2433 "ap");
2434 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2435 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002436 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002437 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2438
2439 uint64_t Offset =
2440 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2441 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002442 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002443 "ap.next");
2444 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2445
2446 return AddrTyped;
2447}
2448
Chris Lattner458b2aa2010-07-29 02:16:43 +00002449ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2450 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002451 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002452
John McCalla1dee5302010-08-22 10:59:02 +00002453 if (isAggregateTypeForABI(RetTy))
Chris Lattner458b2aa2010-07-29 02:16:43 +00002454 return ABIArgInfo::getIndirect(0);
2455
2456 // Treat an enum type as its underlying type.
2457 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2458 RetTy = EnumTy->getDecl()->getIntegerType();
2459
2460 return (RetTy->isPromotableIntegerType() ?
2461 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002462}
2463
Chris Lattner0cf24192010-06-28 20:05:43 +00002464//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002465// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002466//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002467
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002468namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002469
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002470class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002471public:
2472 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2473
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002474 bool isPromotableIntegerType(QualType Ty) const;
2475
Chris Lattner458b2aa2010-07-29 02:16:43 +00002476 ABIArgInfo classifyReturnType(QualType RetTy) const;
2477 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002478
Chris Lattner22326a12010-07-29 02:31:05 +00002479 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002480 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002481 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2482 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002483 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002484 }
2485
2486 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2487 CodeGenFunction &CGF) const;
2488};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002489
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002490class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2491public:
Chris Lattner2b037972010-07-29 02:01:43 +00002492 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2493 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002494};
2495
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002496}
2497
2498bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2499 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002500 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002501 switch (BT->getKind()) {
2502 case BuiltinType::Bool:
2503 case BuiltinType::Char_S:
2504 case BuiltinType::Char_U:
2505 case BuiltinType::SChar:
2506 case BuiltinType::UChar:
2507 case BuiltinType::Short:
2508 case BuiltinType::UShort:
2509 case BuiltinType::Int:
2510 case BuiltinType::UInt:
2511 return true;
2512 default:
2513 return false;
2514 }
2515 return false;
2516}
2517
2518llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2519 CodeGenFunction &CGF) const {
2520 // FIXME: Implement
2521 return 0;
2522}
2523
2524
Chris Lattner458b2aa2010-07-29 02:16:43 +00002525ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2526 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002527 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002528 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002529 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002530
2531 return (isPromotableIntegerType(RetTy) ?
2532 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002533}
2534
Chris Lattner458b2aa2010-07-29 02:16:43 +00002535ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002536 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002537 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002538
2539 return (isPromotableIntegerType(Ty) ?
2540 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002541}
2542
Chris Lattner0cf24192010-06-28 20:05:43 +00002543//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002544// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002545//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002546
2547namespace {
2548
2549class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2550public:
Chris Lattner2b037972010-07-29 02:01:43 +00002551 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2552 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002553 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2554 CodeGen::CodeGenModule &M) const;
2555};
2556
2557}
2558
2559void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2560 llvm::GlobalValue *GV,
2561 CodeGen::CodeGenModule &M) const {
2562 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2563 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2564 // Handle 'interrupt' attribute:
2565 llvm::Function *F = cast<llvm::Function>(GV);
2566
2567 // Step 1: Set ISR calling convention.
2568 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2569
2570 // Step 2: Add attributes goodness.
2571 F->addFnAttr(llvm::Attribute::NoInline);
2572
2573 // Step 3: Emit ISR vector alias.
2574 unsigned Num = attr->getNumber() + 0xffe0;
2575 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2576 "vector_" +
2577 llvm::LowercaseString(llvm::utohexstr(Num)),
2578 GV, &M.getModule());
2579 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002580 }
2581}
2582
Chris Lattner0cf24192010-06-28 20:05:43 +00002583//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002584// MIPS ABI Implementation. This works for both little-endian and
2585// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002586//===----------------------------------------------------------------------===//
2587
John McCall943fae92010-05-27 06:19:26 +00002588namespace {
2589class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2590public:
Chris Lattner2b037972010-07-29 02:01:43 +00002591 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2592 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002593
2594 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2595 return 29;
2596 }
2597
2598 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002599 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002600};
2601}
2602
2603bool
2604MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2605 llvm::Value *Address) const {
2606 // This information comes from gcc's implementation, which seems to
2607 // as canonical as it gets.
2608
2609 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2610 llvm::LLVMContext &Context = CGF.getLLVMContext();
2611
2612 // Everything on MIPS is 4 bytes. Double-precision FP registers
2613 // are aliased to pairs of single-precision FP registers.
2614 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2615 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2616
2617 // 0-31 are the general purpose registers, $0 - $31.
2618 // 32-63 are the floating-point registers, $f0 - $f31.
2619 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2620 // 66 is the (notional, I think) register for signal-handler return.
2621 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2622
2623 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2624 // They are one bit wide and ignored here.
2625
2626 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2627 // (coprocessor 1 is the FP unit)
2628 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2629 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2630 // 176-181 are the DSP accumulator registers.
2631 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2632
2633 return false;
2634}
2635
2636
Chris Lattner2b037972010-07-29 02:01:43 +00002637const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002638 if (TheTargetCodeGenInfo)
2639 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002640
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002641 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2642 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002643
Chris Lattner22a931e2010-06-29 06:01:59 +00002644 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002645 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002646 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002647 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002648
John McCall943fae92010-05-27 06:19:26 +00002649 case llvm::Triple::mips:
2650 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002651 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002652
Daniel Dunbard59655c2009-09-12 00:59:49 +00002653 case llvm::Triple::arm:
2654 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002655 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002656 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002657 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002658 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002659
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002660 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002661 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002662
John McCallea8d8bb2010-03-11 00:10:12 +00002663 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002664 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002665
Daniel Dunbard59655c2009-09-12 00:59:49 +00002666 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002667 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002668
2669 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002670 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002671
Daniel Dunbar40165182009-08-24 09:10:05 +00002672 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002673 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002674 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002675 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002676 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002677 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002678 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002679 case llvm::Triple::AuroraUX:
2680 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002681 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002682 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002683 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002684 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002685
2686 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002687 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002688 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002689 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002690
Daniel Dunbare3532f82009-08-24 08:52:16 +00002691 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00002692 switch (Triple.getOS()) {
2693 case llvm::Triple::Win32:
2694 case llvm::Triple::MinGW64:
2695 case llvm::Triple::Cygwin:
2696 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2697 default:
2698 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2699 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00002700 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002701}