blob: f81d7f85a88ba36e81a239de65bee5a637d6318c [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar98303b92009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000091
92/// isEmptyField - Return true iff a the field is "empty", that is it
93/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000100
Daniel Dunbar98303b92009-09-13 08:03:58 +0000101 // Constant arrays of empty records count as empty, strip them off.
102 if (AllowArrays)
103 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
104 FT = AT->getElementType();
105
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000106 const RecordType *RT = FT->getAs<RecordType>();
107 if (!RT)
108 return false;
109
110 // C++ record fields are never empty, at least in the Itanium ABI.
111 //
112 // FIXME: We should use a predicate for whether this behavior is true in the
113 // current ABI.
114 if (isa<CXXRecordDecl>(RT->getDecl()))
115 return false;
116
Daniel Dunbar98303b92009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118}
119
120/// isEmptyRecord - Return true iff a structure contains only empty
121/// fields. Note that a structure with a flexible array member is not
122/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000125 if (!RT)
126 return 0;
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130
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 Kyrtzidis17945a02009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000145/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
146/// a non-trivial destructor or a non-trivial copy constructor.
147static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
148 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
149 if (!RD)
150 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000151
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000152 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
153}
154
155/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
156/// a record type with either a non-trivial destructor or a non-trivial copy
157/// constructor.
158static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
159 const RecordType *RT = T->getAs<RecordType>();
160 if (!RT)
161 return false;
162
163 return hasNonTrivialDestructorOrCopyConstructor(RT);
164}
165
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166/// isSingleElementStruct - Determine if a structure is a "single
167/// element struct", i.e. it has exactly one non-empty field or
168/// exactly one field which is itself a single element
169/// struct. Structures with flexible array members are never
170/// considered single element structs.
171///
172/// \return The field declaration for the single non-empty field, if
173/// it exists.
174static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
175 const RecordType *RT = T->getAsStructureType();
176 if (!RT)
177 return 0;
178
179 const RecordDecl *RD = RT->getDecl();
180 if (RD->hasFlexibleArrayMember())
181 return 0;
182
183 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000184
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000185 // If this is a C++ record, check the bases first.
186 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
187 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
188 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000191 continue;
192
193 // If we already found an element then this isn't a single-element struct.
194 if (Found)
195 return 0;
196
197 // If this is non-empty and not a single element struct, the composite
198 // cannot be a single element struct.
199 Found = isSingleElementStruct(i->getType(), Context);
200 if (!Found)
201 return 0;
202 }
203 }
204
205 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000213 continue;
214
215 // If we already found an element then this isn't a single-element
216 // struct.
217 if (Found)
218 return 0;
219
220 // Treat single element arrays as the element.
221 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
222 if (AT->getSize().getZExtValue() != 1)
223 break;
224 FT = AT->getElementType();
225 }
226
John McCalld608cdb2010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 Found = FT.getTypePtr();
229 } else {
230 Found = isSingleElementStruct(FT, Context);
231 if (!Found)
232 return 0;
233 }
234 }
235
236 return Found;
237}
238
239static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000243 return false;
244
245 uint64_t Size = Context.getTypeSize(Ty);
246 return Size == 32 || Size == 64;
247}
248
Daniel Dunbar53012f42009-11-09 01:33:53 +0000249/// canExpandIndirectArgument - Test whether an argument type which is to be
250/// passed indirectly (on the stack) would have the equivalent layout if it was
251/// expanded into separate arguments. If so, we prefer to do the latter to avoid
252/// inhibiting optimizations.
253///
254// FIXME: This predicate is missing many cases, currently it just follows
255// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
256// should probably make this smarter, or better yet make the LLVM backend
257// capable of handling it.
258static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
259 // We can only expand structure types.
260 const RecordType *RT = Ty->getAs<RecordType>();
261 if (!RT)
262 return false;
263
264 // We can only expand (C) structures.
265 //
266 // FIXME: This needs to be generalized to handle classes as well.
267 const RecordDecl *RD = RT->getDecl();
268 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
269 return false;
270
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000273 const FieldDecl *FD = *i;
274
275 if (!is32Or64BitBasicType(FD->getType(), Context))
276 return false;
277
278 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
279 // how to expand them yet, and the predicate for telling if a bitfield still
280 // counts as "basic" is more complicated than what we were doing previously.
281 if (FD->isBitField())
282 return false;
283 }
284
285 return true;
286}
287
288namespace {
289/// DefaultABIInfo - The default implementation for ABI specific
290/// details. This implementation provides information which results in
291/// self-consistent and sensible LLVM IR generation, but does not
292/// conform to any particular ABI.
293class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000296
Chris Lattnera3c109b2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000299
Chris Lattneree5dcd02010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattnerea044322010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000315};
316
317llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const {
319 return 0;
320}
321
Chris Lattnera3c109b2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000325
Chris Lattnera14db752010-03-11 18:19:55 +0000326 // Treat an enum type as its underlying type.
327 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
328 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000329
Chris Lattnera14db752010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000332}
333
Bill Wendlingbb465d72010-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 Lattnerdce5ad02010-06-28 20:05:43 +0000344//===----------------------------------------------------------------------===//
345// X86-32 ABI Implementation
346//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000347
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000348/// X86_32ABIInfo - The X86-32 ABI information.
349class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000350 static const unsigned MinABIStackAlignInBytes = 4;
351
David Chisnall1e4249c2009-08-17 23:08:21 +0000352 bool IsDarwinVectorABI;
353 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-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 Dunbardc6d5742010-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 Lattnera3c109b2010-07-29 02:16:43 +0000363 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000364
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000365 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000366 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000367
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000368public:
Chris Lattnerea044322010-07-29 02:01:43 +0000369
Chris Lattnera3c109b2010-07-29 02:16:43 +0000370 ABIArgInfo classifyReturnType(QualType RetTy) const;
371 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000372
Chris Lattneree5dcd02010-07-29 02:31:05 +0000373 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000374 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000375 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
376 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000377 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000378 }
379
380 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
381 CodeGenFunction &CGF) const;
382
Chris Lattnerea044322010-07-29 02:01:43 +0000383 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
384 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000385};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000386
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000387class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
388public:
Chris Lattnerea044322010-07-29 02:01:43 +0000389 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
390 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000391
392 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
393 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-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 Korobeynikov82d0a412010-01-10 12:58:08 +0000404};
405
406}
Anton Korobeynikovc4a59eb2009-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 Dunbar77115232010-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 Dunbara1842d32010-05-14 03:40:53 +0000429 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000430 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000431 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-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 Kremenek6217b802009-07-29 21:53:49 +0000439 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000440 if (!RT) return false;
441
Anders Carlssona8874232010-01-27 03:25:19 +0000442 // FIXME: Traverse bases here too.
443
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000444 // Structure types are passed in register if all fields would be
445 // passed in a register.
Argyrios Kyrtzidis17945a02009-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 Korobeynikovc4a59eb2009-06-05 22:08:42 +0000448 const FieldDecl *FD = *i;
449
450 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000451 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-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 Lattnera3c109b2010-07-29 02:16:43 +0000462ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
463 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000464 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000465
Chris Lattnera3c109b2010-07-29 02:16:43 +0000466 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000467 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000468 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000469 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-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 Lattner800588f2010-07-29 06:26:06 +0000475 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000476 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-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 Lattner800588f2010-07-29 06:26:06 +0000482 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000483 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000484
485 return ABIArgInfo::getIndirect(0);
486 }
487
488 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000489 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000490
John McCalld608cdb2010-08-22 10:59:02 +0000491 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000492 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-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. Spencer8bea82f2010-08-25 18:17:27 +0000497
Anders Carlsson40092972009-10-20 22:07:59 +0000498 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000499 if (RT->getDecl()->hasFlexibleArrayMember())
500 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000501 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000502
David Chisnall1e4249c2009-08-17 23:08:21 +0000503 // If specified, structs and unions are always indirect.
504 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000505 return ABIArgInfo::getIndirect(0);
506
507 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000508 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000509 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-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 Lattnera3c109b2010-07-29 02:16:43 +0000514 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000515 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000516 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
517 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000518
Chris Lattnera3c109b2010-07-29 02:16:43 +0000519 if (BT->getKind() == BuiltinType::Float) {
520 assert(getContext().getTypeSize(RetTy) ==
521 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000523 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000524 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000525
Chris Lattnera3c109b2010-07-29 02:16:43 +0000526 if (BT->getKind() == BuiltinType::Double) {
527 assert(getContext().getTypeSize(RetTy) ==
528 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000529 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000530 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-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 Lattnera3c109b2010-07-29 02:16:43 +0000535 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000536 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-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 Lattnera3c109b2010-07-29 02:16:43 +0000540 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541 if (Size == 64 || Size == 128)
542 return ABIArgInfo::getIndirect(0);
543
Chris Lattnera3c109b2010-07-29 02:16:43 +0000544 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545 }
546 }
547
548 // Small structures which are register sized are generally returned
549 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000550 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
551 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000552 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000553 }
554
555 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000556 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000557
Chris Lattnera3c109b2010-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 Korobeynikovc4a59eb2009-06-05 22:08:42 +0000564}
565
Daniel Dunbar93ae9472010-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 Dunbare59d8582010-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 Dunbarfb67d6c2010-09-16 20:41:56 +0000597 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-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 Dunbarfb67d6c2010-09-16 20:41:56 +0000603 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000604 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000605
Daniel Dunbar93ae9472010-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 Dunbarfb67d6c2010-09-16 20:41:56 +0000611}
612
Chris Lattnera3c109b2010-07-29 02:16:43 +0000613ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000614 if (!ByVal)
615 return ABIArgInfo::getIndirect(0, false);
616
Daniel Dunbare59d8582010-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 Dunbardc6d5742010-04-21 19:10:51 +0000630}
631
Chris Lattnera3c109b2010-07-29 02:16:43 +0000632ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000633 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000634 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000635 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-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 Lattnera3c109b2010-07-29 02:16:43 +0000640 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000641
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000643 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000644 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645
646 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000647 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648 return ABIArgInfo::getIgnore();
649
Daniel Dunbar53012f42009-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 Lattnera3c109b2010-07-29 02:16:43 +0000654 if (getContext().getTypeSize(Ty) <= 4*32 &&
655 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000656 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000657
Chris Lattnera3c109b2010-07-29 02:16:43 +0000658 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000659 }
660
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000661 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-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 Lattnerbbae8b42010-08-26 20:05:13 +0000664 if (IsDarwinVectorABI) {
665 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-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 Lattnerbbae8b42010-08-26 20:05:13 +0000670 }
Bill Wendlingbb465d72010-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 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000678
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000679 return ABIArgInfo::getDirect();
680 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000681
682
Chris Lattnera3c109b2010-07-29 02:16:43 +0000683 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
684 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000685
Chris Lattnera3c109b2010-07-29 02:16:43 +0000686 return (Ty->isPromotableIntegerType() ?
687 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000688}
689
690llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
691 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000692 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000693 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-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 Anderson96e0fc72009-07-29 22:16:19 +0000700 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-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 Lattner77b89b82010-06-27 07:15:29 +0000706 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 "ap.next");
708 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
709
710 return AddrTyped;
711}
712
Charles Davis74f72932010-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 McCall6374c332010-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. Spencer8bea82f2010-08-25 18:17:27 +0000735
John McCall6374c332010-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 McCallaeeb7012010-05-27 06:19:26 +0000739 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-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 McCallaeeb7012010-05-27 06:19:26 +0000746 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000747
John McCall6374c332010-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 McCallaeeb7012010-05-27 06:19:26 +0000757 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
758 }
John McCall6374c332010-03-06 00:35:14 +0000759
760 return false;
761}
762
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000763//===----------------------------------------------------------------------===//
764// X86-64 ABI Implementation
765//===----------------------------------------------------------------------===//
766
767
Anton Korobeynikovc4a59eb2009-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 Lattner1090a9b2010-06-28 21:43:59 +0000791 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-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 Lattner9c254f02010-06-29 06:01:59 +0000814 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000815
Chris Lattner0f408f52010-07-29 04:56:46 +0000816 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattner603519d2010-07-29 17:49:08 +0000817 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattnerf47c9442010-07-29 18:13:09 +0000818 unsigned IROffset, QualType SourceTy,
819 unsigned SourceOffset) const;
Chris Lattner0d2656d2010-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. Spencer8bea82f2010-08-25 18:17:27 +0000823
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000824 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000825 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000826 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000827
828 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000829 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000830 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000831
Chris Lattnera3c109b2010-07-29 02:16:43 +0000832 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000833
Bill Wendlingbb465d72010-10-18 03:41:31 +0000834 ABIArgInfo classifyArgumentType(QualType Ty,
835 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000836 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000837
838public:
Chris Lattnerea044322010-07-29 02:01:43 +0000839 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000840
Chris Lattneree5dcd02010-07-29 02:31:05 +0000841 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000842
843 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
844 CodeGenFunction &CGF) const;
845};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000846
Chris Lattnerf13721d2010-08-31 16:44:54 +0000847/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
848class WinX86_64ABIInfo : public X86_64ABIInfo {
849public:
850 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
851
852 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
853 CodeGenFunction &CGF) const;
854};
855
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000856class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
857public:
Chris Lattnerea044322010-07-29 02:01:43 +0000858 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
859 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000860
861 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
862 return 7;
863 }
864
865 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
866 llvm::Value *Address) const {
867 CodeGen::CGBuilderTy &Builder = CGF.Builder;
868 llvm::LLVMContext &Context = CGF.getLLVMContext();
869
870 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
871 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000872
John McCallaeeb7012010-05-27 06:19:26 +0000873 // 0-15 are the 16 integer registers.
874 // 16 is %rip.
875 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000876
877 return false;
878 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000879};
880
Chris Lattnerf13721d2010-08-31 16:44:54 +0000881class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
882public:
883 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
884 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
885
886 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
887 return 7;
888 }
889
890 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
891 llvm::Value *Address) const {
892 CodeGen::CGBuilderTy &Builder = CGF.Builder;
893 llvm::LLVMContext &Context = CGF.getLLVMContext();
894
895 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
896 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000897
Chris Lattnerf13721d2010-08-31 16:44:54 +0000898 // 0-15 are the 16 integer registers.
899 // 16 is %rip.
900 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
901
902 return false;
903 }
904};
905
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000906}
907
Chris Lattner1090a9b2010-06-28 21:43:59 +0000908X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000909 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
910 // classified recursively so that always two fields are
911 // considered. The resulting class is calculated according to
912 // the classes of the fields in the eightbyte:
913 //
914 // (a) If both classes are equal, this is the resulting class.
915 //
916 // (b) If one of the classes is NO_CLASS, the resulting class is
917 // the other class.
918 //
919 // (c) If one of the classes is MEMORY, the result is the MEMORY
920 // class.
921 //
922 // (d) If one of the classes is INTEGER, the result is the
923 // INTEGER.
924 //
925 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
926 // MEMORY is used as class.
927 //
928 // (f) Otherwise class SSE is used.
929
930 // Accum should never be memory (we should have returned) or
931 // ComplexX87 (because this cannot be passed in a structure).
932 assert((Accum != Memory && Accum != ComplexX87) &&
933 "Invalid accumulated classification during merge.");
934 if (Accum == Field || Field == NoClass)
935 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000936 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000937 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000938 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000939 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000940 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000941 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000942 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
943 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000944 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000945 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000946}
947
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000948void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000949 Class &Lo, Class &Hi) const {
950 // FIXME: This code can be simplified by introducing a simple value class for
951 // Class pairs with appropriate constructor methods for the various
952 // situations.
953
954 // FIXME: Some of the split computations are wrong; unaligned vectors
955 // shouldn't be passed in registers for example, so there is no chance they
956 // can straddle an eightbyte. Verify & simplify.
957
958 Lo = Hi = NoClass;
959
960 Class &Current = OffsetBase < 64 ? Lo : Hi;
961 Current = Memory;
962
John McCall183700f2009-09-21 23:43:11 +0000963 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000964 BuiltinType::Kind k = BT->getKind();
965
966 if (k == BuiltinType::Void) {
967 Current = NoClass;
968 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
969 Lo = Integer;
970 Hi = Integer;
971 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
972 Current = Integer;
973 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
974 Current = SSE;
975 } else if (k == BuiltinType::LongDouble) {
976 Lo = X87;
977 Hi = X87Up;
978 }
979 // FIXME: _Decimal32 and _Decimal64 are SSE.
980 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000981 return;
982 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000983
Chris Lattner1090a9b2010-06-28 21:43:59 +0000984 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000985 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +0000986 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000987 return;
988 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000989
Chris Lattner1090a9b2010-06-28 21:43:59 +0000990 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000991 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000992 return;
993 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000994
Chris Lattner1090a9b2010-06-28 21:43:59 +0000995 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000996 if (Ty->isMemberFunctionPointerType())
997 Lo = Hi = Integer;
998 else
999 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001000 return;
1001 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001002
Chris Lattner1090a9b2010-06-28 21:43:59 +00001003 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001004 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 if (Size == 32) {
1006 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1007 // float> as integer.
1008 Current = Integer;
1009
1010 // If this type crosses an eightbyte boundary, it should be
1011 // split.
1012 uint64_t EB_Real = (OffsetBase) / 64;
1013 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1014 if (EB_Real != EB_Imag)
1015 Hi = Lo;
1016 } else if (Size == 64) {
1017 // gcc passes <1 x double> in memory. :(
1018 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1019 return;
1020
1021 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001022 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001023 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1024 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1025 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001026 Current = Integer;
1027 else
1028 Current = SSE;
1029
1030 // If this type crosses an eightbyte boundary, it should be
1031 // split.
1032 if (OffsetBase && OffsetBase != 64)
1033 Hi = Lo;
1034 } else if (Size == 128) {
1035 Lo = SSE;
1036 Hi = SSEUp;
1037 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001038 return;
1039 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001040
Chris Lattner1090a9b2010-06-28 21:43:59 +00001041 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001042 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001043
Chris Lattnerea044322010-07-29 02:01:43 +00001044 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001045 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001046 if (Size <= 64)
1047 Current = Integer;
1048 else if (Size <= 128)
1049 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001050 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001051 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001052 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001053 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001054 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001055 Current = ComplexX87;
1056
1057 // If this complex type crosses an eightbyte boundary then it
1058 // should be split.
1059 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001060 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001061 if (Hi == NoClass && EB_Real != EB_Imag)
1062 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001063
Chris Lattner1090a9b2010-06-28 21:43:59 +00001064 return;
1065 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001066
Chris Lattnerea044322010-07-29 02:01:43 +00001067 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001068 // Arrays are treated like structures.
1069
Chris Lattnerea044322010-07-29 02:01:43 +00001070 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001071
1072 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1073 // than two eightbytes, ..., it has class MEMORY.
1074 if (Size > 128)
1075 return;
1076
1077 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1078 // fields, it has class MEMORY.
1079 //
1080 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001081 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001082 return;
1083
1084 // Otherwise implement simplified merge. We could be smarter about
1085 // this, but it isn't worth it and would be harder to verify.
1086 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001087 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001088 uint64_t ArraySize = AT->getSize().getZExtValue();
1089 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1090 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001091 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001092 Lo = merge(Lo, FieldLo);
1093 Hi = merge(Hi, FieldHi);
1094 if (Lo == Memory || Hi == Memory)
1095 break;
1096 }
1097
1098 // Do post merger cleanup (see below). Only case we worry about is Memory.
1099 if (Hi == Memory)
1100 Lo = Memory;
1101 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001102 return;
1103 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001104
Chris Lattner1090a9b2010-06-28 21:43:59 +00001105 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001106 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001107
1108 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1109 // than two eightbytes, ..., it has class MEMORY.
1110 if (Size > 128)
1111 return;
1112
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001113 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1114 // copy constructor or a non-trivial destructor, it is passed by invisible
1115 // reference.
1116 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1117 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001118
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001119 const RecordDecl *RD = RT->getDecl();
1120
1121 // Assume variable sized types are passed in memory.
1122 if (RD->hasFlexibleArrayMember())
1123 return;
1124
Chris Lattnerea044322010-07-29 02:01:43 +00001125 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001126
1127 // Reset Lo class, this will be recomputed.
1128 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001129
1130 // If this is a C++ record, classify the bases first.
1131 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1132 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1133 e = CXXRD->bases_end(); i != e; ++i) {
1134 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1135 "Unexpected base class!");
1136 const CXXRecordDecl *Base =
1137 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1138
1139 // Classify this field.
1140 //
1141 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1142 // single eightbyte, each is classified separately. Each eightbyte gets
1143 // initialized to class NO_CLASS.
1144 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001145 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001146 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001147 Lo = merge(Lo, FieldLo);
1148 Hi = merge(Hi, FieldHi);
1149 if (Lo == Memory || Hi == Memory)
1150 break;
1151 }
1152 }
1153
1154 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001155 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001156 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1157 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001158 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1159 bool BitField = i->isBitField();
1160
1161 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1162 // fields, it has class MEMORY.
1163 //
1164 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001165 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001166 Lo = Memory;
1167 return;
1168 }
1169
1170 // Classify this field.
1171 //
1172 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1173 // exceeds a single eightbyte, each is classified
1174 // separately. Each eightbyte gets initialized to class
1175 // NO_CLASS.
1176 Class FieldLo, FieldHi;
1177
1178 // Bit-fields require special handling, they do not force the
1179 // structure to be passed in memory even if unaligned, and
1180 // therefore they can straddle an eightbyte.
1181 if (BitField) {
1182 // Ignore padding bit-fields.
1183 if (i->isUnnamedBitfield())
1184 continue;
1185
1186 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001187 uint64_t Size =
1188 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001189
1190 uint64_t EB_Lo = Offset / 64;
1191 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1192 FieldLo = FieldHi = NoClass;
1193 if (EB_Lo) {
1194 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1195 FieldLo = NoClass;
1196 FieldHi = Integer;
1197 } else {
1198 FieldLo = Integer;
1199 FieldHi = EB_Hi ? Integer : NoClass;
1200 }
1201 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001202 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001203 Lo = merge(Lo, FieldLo);
1204 Hi = merge(Hi, FieldHi);
1205 if (Lo == Memory || Hi == Memory)
1206 break;
1207 }
1208
1209 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1210 //
1211 // (a) If one of the classes is MEMORY, the whole argument is
1212 // passed in memory.
1213 //
1214 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1215
1216 // The first of these conditions is guaranteed by how we implement
1217 // the merge (just bail).
1218 //
1219 // The second condition occurs in the case of unions; for example
1220 // union { _Complex double; unsigned; }.
1221 if (Hi == Memory)
1222 Lo = Memory;
1223 if (Hi == SSEUp && Lo != SSE)
1224 Hi = SSE;
1225 }
1226}
1227
Chris Lattner9c254f02010-06-29 06:01:59 +00001228ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001229 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1230 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001231 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001232 // Treat an enum type as its underlying type.
1233 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1234 Ty = EnumTy->getDecl()->getIntegerType();
1235
1236 return (Ty->isPromotableIntegerType() ?
1237 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1238 }
1239
1240 return ABIArgInfo::getIndirect(0);
1241}
1242
Chris Lattner9c254f02010-06-29 06:01:59 +00001243ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001244 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1245 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001246 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001247 // Treat an enum type as its underlying type.
1248 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1249 Ty = EnumTy->getDecl()->getIntegerType();
1250
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001251 return (Ty->isPromotableIntegerType() ?
1252 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001253 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001254
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001255 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1256 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001257
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001258 // Compute the byval alignment. We trust the back-end to honor the
1259 // minimum ABI alignment for byval, to make cleaner IR.
1260 const unsigned MinABIAlign = 8;
Chris Lattnerea044322010-07-29 02:01:43 +00001261 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001262 if (Align > MinABIAlign)
1263 return ABIArgInfo::getIndirect(Align);
1264 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001265}
1266
Chris Lattner0f408f52010-07-29 04:56:46 +00001267/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1268/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1269/// vector register.
1270const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner15842bd2010-07-29 05:02:29 +00001271 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001272
Chris Lattner15842bd2010-07-29 05:02:29 +00001273 // Wrapper structs that just contain vectors are passed just like vectors,
1274 // strip them off if present.
1275 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1276 while (STy && STy->getNumElements() == 1) {
1277 IRType = STy->getElementType(0);
1278 STy = dyn_cast<llvm::StructType>(IRType);
1279 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001280
Chris Lattner0f408f52010-07-29 04:56:46 +00001281 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner15842bd2010-07-29 05:02:29 +00001282 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner0f408f52010-07-29 04:56:46 +00001283 const llvm::Type *EltTy = VT->getElementType();
1284 if (VT->getBitWidth() == 128 &&
1285 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1286 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1287 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1288 EltTy->isIntegerTy(128)))
1289 return VT;
1290 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001291
Chris Lattner0f408f52010-07-29 04:56:46 +00001292 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1293}
1294
Chris Lattnere2962be2010-07-29 07:30:00 +00001295/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1296/// is known to either be off the end of the specified type or being in
1297/// alignment padding. The user type specified is known to be at most 128 bits
1298/// in size, and have passed through X86_64ABIInfo::classify with a successful
1299/// classification that put one of the two halves in the INTEGER class.
1300///
1301/// It is conservatively correct to return false.
1302static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1303 unsigned EndBit, ASTContext &Context) {
1304 // If the bytes being queried are off the end of the type, there is no user
1305 // data hiding here. This handles analysis of builtins, vectors and other
1306 // types that don't contain interesting padding.
1307 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1308 if (TySize <= StartBit)
1309 return true;
1310
Chris Lattner021c3a32010-07-29 07:43:55 +00001311 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1312 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1313 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1314
1315 // Check each element to see if the element overlaps with the queried range.
1316 for (unsigned i = 0; i != NumElts; ++i) {
1317 // If the element is after the span we care about, then we're done..
1318 unsigned EltOffset = i*EltSize;
1319 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001320
Chris Lattner021c3a32010-07-29 07:43:55 +00001321 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1322 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1323 EndBit-EltOffset, Context))
1324 return false;
1325 }
1326 // If it overlaps no elements, then it is safe to process as padding.
1327 return true;
1328 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001329
Chris Lattnere2962be2010-07-29 07:30:00 +00001330 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1331 const RecordDecl *RD = RT->getDecl();
1332 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001333
Chris Lattnere2962be2010-07-29 07:30:00 +00001334 // If this is a C++ record, check the bases first.
1335 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1336 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1337 e = CXXRD->bases_end(); i != e; ++i) {
1338 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1339 "Unexpected base class!");
1340 const CXXRecordDecl *Base =
1341 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001342
Chris Lattnere2962be2010-07-29 07:30:00 +00001343 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001344 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001345 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001346
Chris Lattnere2962be2010-07-29 07:30:00 +00001347 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1348 if (!BitsContainNoUserData(i->getType(), BaseStart,
1349 EndBit-BaseOffset, Context))
1350 return false;
1351 }
1352 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001353
Chris Lattnere2962be2010-07-29 07:30:00 +00001354 // Verify that no field has data that overlaps the region of interest. Yes
1355 // this could be sped up a lot by being smarter about queried fields,
1356 // however we're only looking at structs up to 16 bytes, so we don't care
1357 // much.
1358 unsigned idx = 0;
1359 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1360 i != e; ++i, ++idx) {
1361 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001362
Chris Lattnere2962be2010-07-29 07:30:00 +00001363 // If we found a field after the region we care about, then we're done.
1364 if (FieldOffset >= EndBit) break;
1365
1366 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1367 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1368 Context))
1369 return false;
1370 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001371
Chris Lattnere2962be2010-07-29 07:30:00 +00001372 // If nothing in this record overlapped the area of interest, then we're
1373 // clean.
1374 return true;
1375 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001376
Chris Lattnere2962be2010-07-29 07:30:00 +00001377 return false;
1378}
1379
Chris Lattner0b362002010-07-29 18:39:32 +00001380/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1381/// float member at the specified offset. For example, {int,{float}} has a
1382/// float at offset 4. It is conservatively correct for this routine to return
1383/// false.
1384static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1385 const llvm::TargetData &TD) {
1386 // Base case if we find a float.
1387 if (IROffset == 0 && IRType->isFloatTy())
1388 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001389
Chris Lattner0b362002010-07-29 18:39:32 +00001390 // If this is a struct, recurse into the field at the specified offset.
1391 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1392 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1393 unsigned Elt = SL->getElementContainingOffset(IROffset);
1394 IROffset -= SL->getElementOffset(Elt);
1395 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1396 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001397
Chris Lattner0b362002010-07-29 18:39:32 +00001398 // If this is an array, recurse into the field at the specified offset.
1399 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1400 const llvm::Type *EltTy = ATy->getElementType();
1401 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1402 IROffset -= IROffset/EltSize*EltSize;
1403 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1404 }
1405
1406 return false;
1407}
1408
Chris Lattnerf47c9442010-07-29 18:13:09 +00001409
1410/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1411/// low 8 bytes of an XMM register, corresponding to the SSE class.
1412const llvm::Type *X86_64ABIInfo::
1413GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1414 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001415 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001416 // pass as float if the last 4 bytes is just padding. This happens for
1417 // structs that contain 3 floats.
1418 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1419 SourceOffset*8+64, getContext()))
1420 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001421
Chris Lattner0b362002010-07-29 18:39:32 +00001422 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1423 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1424 // case.
1425 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001426 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1427 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001428
Chris Lattnerf47c9442010-07-29 18:13:09 +00001429 return llvm::Type::getDoubleTy(getVMContext());
1430}
1431
1432
Chris Lattner0d2656d2010-07-29 17:40:35 +00001433/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1434/// an 8-byte GPR. This means that we either have a scalar or we are talking
1435/// about the high or low part of an up-to-16-byte struct. This routine picks
1436/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001437/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1438/// etc).
1439///
1440/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1441/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1442/// the 8-byte value references. PrefType may be null.
1443///
1444/// SourceTy is the source level type for the entire argument. SourceOffset is
1445/// an offset into this that we're processing (which is always either 0 or 8).
1446///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001447const llvm::Type *X86_64ABIInfo::
Chris Lattner0d2656d2010-07-29 17:40:35 +00001448GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1449 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001450 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1451 // returning an 8-byte unit starting with it. See if we can safely use it.
1452 if (IROffset == 0) {
1453 // Pointers and int64's always fill the 8-byte unit.
1454 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1455 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001456
Chris Lattnere2962be2010-07-29 07:30:00 +00001457 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1458 // goodness in the source type is just tail padding. This is allowed to
1459 // kick in for struct {double,int} on the int, but not on
1460 // struct{double,int,int} because we wouldn't return the second int. We
1461 // have to do this analysis on the source type because we can't depend on
1462 // unions being lowered a specific way etc.
1463 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1464 IRType->isIntegerTy(32)) {
1465 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001466
Chris Lattnere2962be2010-07-29 07:30:00 +00001467 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1468 SourceOffset*8+64, getContext()))
1469 return IRType;
1470 }
1471 }
Chris Lattner49382de2010-07-28 22:44:07 +00001472
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001473 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001474 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001475 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001476 if (IROffset < SL->getSizeInBytes()) {
1477 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1478 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001479
Chris Lattner0d2656d2010-07-29 17:40:35 +00001480 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1481 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001482 }
Chris Lattner49382de2010-07-28 22:44:07 +00001483 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001484
Chris Lattner021c3a32010-07-29 07:43:55 +00001485 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1486 const llvm::Type *EltTy = ATy->getElementType();
1487 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1488 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001489 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1490 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001491 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001492
Chris Lattner49382de2010-07-28 22:44:07 +00001493 // Okay, we don't have any better idea of what to pass, so we pass this in an
1494 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001495 unsigned TySizeInBytes =
1496 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001497
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001498 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001499
Chris Lattner49382de2010-07-28 22:44:07 +00001500 // It is always safe to classify this as an integer type up to i64 that
1501 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001502 return llvm::IntegerType::get(getVMContext(),
1503 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001504}
1505
Chris Lattner66e7b682010-09-01 00:50:20 +00001506
1507/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1508/// be used as elements of a two register pair to pass or return, return a
1509/// first class aggregate to represent them. For example, if the low part of
1510/// a by-value argument should be passed as i32* and the high part as float,
1511/// return {i32*, float}.
1512static const llvm::Type *
1513GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1514 const llvm::TargetData &TD) {
1515 // In order to correctly satisfy the ABI, we need to the high part to start
1516 // at offset 8. If the high and low parts we inferred are both 4-byte types
1517 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1518 // the second element at offset 8. Check for this:
1519 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1520 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1521 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1522 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001523
Chris Lattner66e7b682010-09-01 00:50:20 +00001524 // To handle this, we have to increase the size of the low part so that the
1525 // second element will start at an 8 byte offset. We can't increase the size
1526 // of the second element because it might make us access off the end of the
1527 // struct.
1528 if (HiStart != 8) {
1529 // There are only two sorts of types the ABI generation code can produce for
1530 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1531 // Promote these to a larger type.
1532 if (Lo->isFloatTy())
1533 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1534 else {
1535 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1536 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1537 }
1538 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001539
1540 const llvm::StructType *Result =
Chris Lattner66e7b682010-09-01 00:50:20 +00001541 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001542
1543
Chris Lattner66e7b682010-09-01 00:50:20 +00001544 // Verify that the second element is at an 8-byte offset.
1545 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1546 "Invalid x86-64 argument pair!");
1547 return Result;
1548}
1549
Chris Lattner519f68c2010-07-28 23:06:14 +00001550ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001551classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001552 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1553 // classification algorithm.
1554 X86_64ABIInfo::Class Lo, Hi;
1555 classify(RetTy, 0, Lo, Hi);
1556
1557 // Check some invariants.
1558 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001559 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1560
1561 const llvm::Type *ResType = 0;
1562 switch (Lo) {
1563 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001564 if (Hi == NoClass)
1565 return ABIArgInfo::getIgnore();
1566 // If the low part is just padding, it takes no register, leave ResType
1567 // null.
1568 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1569 "Unknown missing lo part");
1570 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001571
1572 case SSEUp:
1573 case X87Up:
1574 assert(0 && "Invalid classification for lo word.");
1575
1576 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1577 // hidden argument.
1578 case Memory:
1579 return getIndirectReturnResult(RetTy);
1580
1581 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1582 // available register of the sequence %rax, %rdx is used.
1583 case Integer:
Chris Lattner0d2656d2010-07-29 17:40:35 +00001584 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1585 RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001586
Chris Lattnereb518b42010-07-29 21:42:50 +00001587 // If we have a sign or zero extended integer, make sure to return Extend
1588 // so that the parameter gets the right LLVM IR attributes.
1589 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1590 // Treat an enum type as its underlying type.
1591 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1592 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001593
Chris Lattnereb518b42010-07-29 21:42:50 +00001594 if (RetTy->isIntegralOrEnumerationType() &&
1595 RetTy->isPromotableIntegerType())
1596 return ABIArgInfo::getExtend();
1597 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001598 break;
1599
1600 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1601 // available SSE register of the sequence %xmm0, %xmm1 is used.
1602 case SSE:
Chris Lattnerf47c9442010-07-29 18:13:09 +00001603 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001604 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001605
1606 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1607 // returned on the X87 stack in %st0 as 80-bit x87 number.
1608 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001609 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001610 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001611
1612 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1613 // part of the value is returned in %st0 and the imaginary part in
1614 // %st1.
1615 case ComplexX87:
1616 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001617 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001618 llvm::Type::getX86_FP80Ty(getVMContext()),
1619 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001620 NULL);
1621 break;
1622 }
1623
Chris Lattner3db4dde2010-09-01 00:20:33 +00001624 const llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001625 switch (Hi) {
1626 // Memory was handled previously and X87 should
1627 // never occur as a hi class.
1628 case Memory:
1629 case X87:
1630 assert(0 && "Invalid classification for hi word.");
1631
1632 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001633 case NoClass:
1634 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001635
Chris Lattner3db4dde2010-09-01 00:20:33 +00001636 case Integer:
1637 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1638 8, RetTy, 8);
1639 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1640 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001641 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001642 case SSE:
1643 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1644 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1645 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001646 break;
1647
1648 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1649 // is passed in the upper half of the last used SSE register.
1650 //
1651 // SSEUP should always be preceeded by SSE, just widen.
1652 case SSEUp:
1653 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner0f408f52010-07-29 04:56:46 +00001654 ResType = Get16ByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001655 break;
1656
1657 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1658 // returned together with the previous X87 value in %st0.
1659 case X87Up:
1660 // If X87Up is preceeded by X87, we don't need to do
1661 // anything. However, in some cases with unions it may not be
1662 // preceeded by X87. In such situations we follow gcc and pass the
1663 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001664 if (Lo != X87) {
Chris Lattner3db4dde2010-09-01 00:20:33 +00001665 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1666 8, RetTy, 8);
1667 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1668 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001669 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001670 break;
1671 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001672
Chris Lattner3db4dde2010-09-01 00:20:33 +00001673 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001674 // known to pass in the high eightbyte of the result. We do this by forming a
1675 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001676 if (HighPart)
1677 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001678
Chris Lattnereb518b42010-07-29 21:42:50 +00001679 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001680}
1681
Chris Lattnera3c109b2010-07-29 02:16:43 +00001682ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001683 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001684 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001685 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001686
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001687 // Check some invariants.
1688 // FIXME: Enforce these by construction.
1689 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001690 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1691
1692 neededInt = 0;
1693 neededSSE = 0;
1694 const llvm::Type *ResType = 0;
1695 switch (Lo) {
1696 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001697 if (Hi == NoClass)
1698 return ABIArgInfo::getIgnore();
1699 // If the low part is just padding, it takes no register, leave ResType
1700 // null.
1701 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1702 "Unknown missing lo part");
1703 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001704
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001705 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1706 // on the stack.
1707 case Memory:
1708
1709 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1710 // COMPLEX_X87, it is passed in memory.
1711 case X87:
1712 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001713 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001714
1715 case SSEUp:
1716 case X87Up:
1717 assert(0 && "Invalid classification for lo word.");
1718
1719 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1720 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1721 // and %r9 is used.
1722 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001723 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001724
Chris Lattner49382de2010-07-28 22:44:07 +00001725 // Pick an 8-byte type based on the preferred type.
Chris Lattner0d2656d2010-07-29 17:40:35 +00001726 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001727
1728 // If we have a sign or zero extended integer, make sure to return Extend
1729 // so that the parameter gets the right LLVM IR attributes.
1730 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1731 // Treat an enum type as its underlying type.
1732 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1733 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001734
Chris Lattnereb518b42010-07-29 21:42:50 +00001735 if (Ty->isIntegralOrEnumerationType() &&
1736 Ty->isPromotableIntegerType())
1737 return ABIArgInfo::getExtend();
1738 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001739
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001740 break;
1741
1742 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1743 // available SSE register is used, the registers are taken in the
1744 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001745 case SSE: {
1746 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001747 if (Hi != NoClass || !UseX86_MMXType(IRType))
Bill Wendlingbb465d72010-10-18 03:41:31 +00001748 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001749 else
Bill Wendlingbb465d72010-10-18 03:41:31 +00001750 // This is an MMX type. Treat it as such.
1751 ResType = llvm::Type::getX86_MMXTy(getVMContext());
Bill Wendlingbb465d72010-10-18 03:41:31 +00001752
Bill Wendling99aaae82010-10-18 23:51:38 +00001753 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001754 break;
1755 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001756 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001757
Chris Lattner645406a2010-09-01 00:24:35 +00001758 const llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001759 switch (Hi) {
1760 // Memory was handled previously, ComplexX87 and X87 should
1761 // never occur as hi classes, and X87Up must be preceed by X87,
1762 // which is passed in memory.
1763 case Memory:
1764 case X87:
1765 case ComplexX87:
1766 assert(0 && "Invalid classification for hi word.");
1767 break;
1768
1769 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001770
Chris Lattner645406a2010-09-01 00:24:35 +00001771 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001772 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001773 // Pick an 8-byte type based on the preferred type.
Chris Lattner645406a2010-09-01 00:24:35 +00001774 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001775
Chris Lattner645406a2010-09-01 00:24:35 +00001776 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1777 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001778 break;
1779
1780 // X87Up generally doesn't occur here (long double is passed in
1781 // memory), except in situations involving unions.
1782 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001783 case SSE:
1784 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001785
Chris Lattner645406a2010-09-01 00:24:35 +00001786 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1787 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001788
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001789 ++neededSSE;
1790 break;
1791
1792 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1793 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001794 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001795 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001796 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner0f408f52010-07-29 04:56:46 +00001797 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001798 break;
1799 }
1800
Chris Lattner645406a2010-09-01 00:24:35 +00001801 // If a high part was specified, merge it together with the low part. It is
1802 // known to pass in the high eightbyte of the result. We do this by forming a
1803 // first class struct aggregate with the high and low part: {low, high}
1804 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001805 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001806
Chris Lattnereb518b42010-07-29 21:42:50 +00001807 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001808}
1809
Chris Lattneree5dcd02010-07-29 02:31:05 +00001810void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001811
Chris Lattnera3c109b2010-07-29 02:16:43 +00001812 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001813
1814 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001815 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001816
1817 // If the return value is indirect, then the hidden argument is consuming one
1818 // integer register.
1819 if (FI.getReturnInfo().isIndirect())
1820 --freeIntRegs;
1821
1822 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1823 // get assigned (in left-to-right order) for passing as follows...
1824 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1825 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001826 unsigned neededInt, neededSSE;
1827 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001828
1829 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1830 // eightbyte of an argument, the whole argument is passed on the
1831 // stack. If registers have already been assigned for some
1832 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001833 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001834 freeIntRegs -= neededInt;
1835 freeSSERegs -= neededSSE;
1836 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001837 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001838 }
1839 }
1840}
1841
1842static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1843 QualType Ty,
1844 CodeGenFunction &CGF) {
1845 llvm::Value *overflow_arg_area_p =
1846 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1847 llvm::Value *overflow_arg_area =
1848 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1849
1850 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1851 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1852 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1853 if (Align > 8) {
1854 // Note that we follow the ABI & gcc here, even though the type
1855 // could in theory have an alignment greater than 16. This case
1856 // shouldn't ever matter in practice.
1857
1858 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001859 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001860 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001861 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1862 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001863 CGF.Int64Ty);
1864 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001865 overflow_arg_area =
1866 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1867 overflow_arg_area->getType(),
1868 "overflow_arg_area.align");
1869 }
1870
1871 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1872 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1873 llvm::Value *Res =
1874 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001875 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001876
1877 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1878 // l->overflow_arg_area + sizeof(type).
1879 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1880 // an 8 byte boundary.
1881
1882 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001883 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001884 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001885 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1886 "overflow_arg_area.next");
1887 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1888
1889 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1890 return Res;
1891}
1892
1893llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1894 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001895 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001897 // Assume that va_list type is correct; should be pointer to LLVM type:
1898 // struct {
1899 // i32 gp_offset;
1900 // i32 fp_offset;
1901 // i8* overflow_arg_area;
1902 // i8* reg_save_area;
1903 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00001904 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001905
Chris Lattnera14db752010-03-11 18:19:55 +00001906 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001907 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001908
1909 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1910 // in the registers. If not go to step 7.
1911 if (!neededInt && !neededSSE)
1912 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1913
1914 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1915 // general purpose registers needed to pass type and num_fp to hold
1916 // the number of floating point registers needed.
1917
1918 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1919 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1920 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1921 //
1922 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1923 // register save space).
1924
1925 llvm::Value *InRegs = 0;
1926 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1927 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1928 if (neededInt) {
1929 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1930 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001931 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1932 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001933 }
1934
1935 if (neededSSE) {
1936 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1937 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1938 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001939 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1940 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001941 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1942 }
1943
1944 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1945 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1946 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1947 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1948
1949 // Emit code to load the value if it was passed in registers.
1950
1951 CGF.EmitBlock(InRegBlock);
1952
1953 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1954 // an offset of l->gp_offset and/or l->fp_offset. This may require
1955 // copying to a temporary location in case the parameter is passed
1956 // in different register classes or requires an alignment greater
1957 // than 8 for general purpose registers and 16 for XMM registers.
1958 //
1959 // FIXME: This really results in shameful code when we end up needing to
1960 // collect arguments from different places; often what should result in a
1961 // simple assembling of a structure from scattered addresses has many more
1962 // loads than necessary. Can we clean this up?
1963 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1964 llvm::Value *RegAddr =
1965 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1966 "reg_save_area");
1967 if (neededInt && neededSSE) {
1968 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00001969 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001970 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1971 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1972 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1973 const llvm::Type *TyLo = ST->getElementType(0);
1974 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00001975 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001976 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001977 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1978 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001979 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1980 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001981 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1982 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001983 llvm::Value *V =
1984 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1985 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1986 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1987 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1988
Owen Andersona1cf15f2009-07-14 23:10:40 +00001989 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001990 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001991 } else if (neededInt) {
1992 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1993 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001994 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001995 } else if (neededSSE == 1) {
1996 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1997 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1998 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001999 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002000 assert(neededSSE == 2 && "Invalid number of needed registers!");
2001 // SSE registers are spaced 16 bytes apart in the register save
2002 // area, we need to collect the two eightbytes together.
2003 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002004 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002005 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
2006 const llvm::Type *DblPtrTy =
2007 llvm::PointerType::getUnqual(DoubleTy);
2008 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
2009 DoubleTy, NULL);
2010 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2011 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2012 DblPtrTy));
2013 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2014 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2015 DblPtrTy));
2016 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2017 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2018 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002019 }
2020
2021 // AMD64-ABI 3.5.7p5: Step 5. Set:
2022 // l->gp_offset = l->gp_offset + num_gp * 8
2023 // l->fp_offset = l->fp_offset + num_fp * 16.
2024 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002025 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002026 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2027 gp_offset_p);
2028 }
2029 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002030 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002031 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2032 fp_offset_p);
2033 }
2034 CGF.EmitBranch(ContBlock);
2035
2036 // Emit code to load the value if it was passed in memory.
2037
2038 CGF.EmitBlock(InMemBlock);
2039 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2040
2041 // Return the appropriate result.
2042
2043 CGF.EmitBlock(ContBlock);
2044 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2045 "vaarg.addr");
2046 ResAddr->reserveOperandSpace(2);
2047 ResAddr->addIncoming(RegAddr, InRegBlock);
2048 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002049 return ResAddr;
2050}
2051
Chris Lattnerf13721d2010-08-31 16:44:54 +00002052llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2053 CodeGenFunction &CGF) const {
2054 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2055 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002056
Chris Lattnerf13721d2010-08-31 16:44:54 +00002057 CGBuilderTy &Builder = CGF.Builder;
2058 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2059 "ap");
2060 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2061 llvm::Type *PTy =
2062 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2063 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2064
2065 uint64_t Offset =
2066 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2067 llvm::Value *NextAddr =
2068 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2069 "ap.next");
2070 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2071
2072 return AddrTyped;
2073}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002074
John McCallec853ba2010-03-11 00:10:12 +00002075// PowerPC-32
2076
2077namespace {
2078class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2079public:
Chris Lattnerea044322010-07-29 02:01:43 +00002080 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002081
John McCallec853ba2010-03-11 00:10:12 +00002082 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2083 // This is recovered from gcc output.
2084 return 1; // r1 is the dedicated stack pointer
2085 }
2086
2087 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002088 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002089};
2090
2091}
2092
2093bool
2094PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2095 llvm::Value *Address) const {
2096 // This is calculated from the LLVM and GCC tables and verified
2097 // against gcc output. AFAIK all ABIs use the same encoding.
2098
2099 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2100 llvm::LLVMContext &Context = CGF.getLLVMContext();
2101
2102 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2103 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2104 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2105 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2106
2107 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002108 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002109
2110 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002111 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002112
2113 // 64-76 are various 4-byte special-purpose registers:
2114 // 64: mq
2115 // 65: lr
2116 // 66: ctr
2117 // 67: ap
2118 // 68-75 cr0-7
2119 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002120 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002121
2122 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002123 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002124
2125 // 109: vrsave
2126 // 110: vscr
2127 // 111: spe_acc
2128 // 112: spefscr
2129 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002130 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002131
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002132 return false;
John McCallec853ba2010-03-11 00:10:12 +00002133}
2134
2135
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002136//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002137// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002138//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002139
2140namespace {
2141
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002142class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002143public:
2144 enum ABIKind {
2145 APCS = 0,
2146 AAPCS = 1,
2147 AAPCS_VFP
2148 };
2149
2150private:
2151 ABIKind Kind;
2152
2153public:
Chris Lattnerea044322010-07-29 02:01:43 +00002154 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002155
2156private:
2157 ABIKind getABIKind() const { return Kind; }
2158
Chris Lattnera3c109b2010-07-29 02:16:43 +00002159 ABIArgInfo classifyReturnType(QualType RetTy) const;
2160 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002161
Chris Lattneree5dcd02010-07-29 02:31:05 +00002162 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002163
2164 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2165 CodeGenFunction &CGF) const;
2166};
2167
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002168class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2169public:
Chris Lattnerea044322010-07-29 02:01:43 +00002170 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2171 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002172
2173 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2174 return 13;
2175 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002176};
2177
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002178}
2179
Chris Lattneree5dcd02010-07-29 02:31:05 +00002180void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002181 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002182 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002183 it != ie; ++it)
2184 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002185
Chris Lattnera3c109b2010-07-29 02:16:43 +00002186 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindola25117ab2010-06-16 16:13:39 +00002187 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002188 if (Triple.getEnvironmentName() == "gnueabi" ||
2189 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002190 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002191 else
2192 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002193
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002194 switch (getABIKind()) {
2195 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002196 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2197 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002198 break;
2199
2200 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002201 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2202 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002203 break;
2204
2205 case AAPCS_VFP:
2206 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2207 break;
2208 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002209}
2210
Chris Lattnera3c109b2010-07-29 02:16:43 +00002211ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002212 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002213 // Treat an enum type as its underlying type.
2214 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2215 Ty = EnumTy->getDecl()->getIntegerType();
2216
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002217 return (Ty->isPromotableIntegerType() ?
2218 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002219 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002220
Daniel Dunbar42025572009-09-14 21:54:03 +00002221 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002222 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002223 return ABIArgInfo::getIgnore();
2224
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002225 // Structures with either a non-trivial destructor or a non-trivial
2226 // copy constructor are always indirect.
2227 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2228 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2229
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002230 // NEON vectors are implemented as (theoretically) opaque structures wrapping
2231 // the underlying vector type. We trust the backend to pass the underlying
2232 // vectors appropriately, so we can unwrap the structs which generally will
2233 // lead to much cleaner IR.
2234 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) {
2235 if (SeltTy->isVectorType())
2236 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
2237 }
2238
2239 // Otherwise, pass by coercing to a structure of the appropriate size.
2240 //
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002241 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2242 // backend doesn't support byval.
2243 // FIXME: This doesn't handle alignment > 64 bits.
2244 const llvm::Type* ElemTy;
2245 unsigned SizeRegs;
Chris Lattnera3c109b2010-07-29 02:16:43 +00002246 if (getContext().getTypeAlign(Ty) > 32) {
2247 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2248 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002249 } else {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002250 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2251 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002252 }
2253 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00002254 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattnera3c109b2010-07-29 02:16:43 +00002255 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2256 true);
Chris Lattner800588f2010-07-29 06:26:06 +00002257 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002258}
2259
Chris Lattnera3c109b2010-07-29 02:16:43 +00002260static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002261 llvm::LLVMContext &VMContext) {
2262 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2263 // is called integer-like if its size is less than or equal to one word, and
2264 // the offset of each of its addressable sub-fields is zero.
2265
2266 uint64_t Size = Context.getTypeSize(Ty);
2267
2268 // Check that the type fits in a word.
2269 if (Size > 32)
2270 return false;
2271
2272 // FIXME: Handle vector types!
2273 if (Ty->isVectorType())
2274 return false;
2275
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002276 // Float types are never treated as "integer like".
2277 if (Ty->isRealFloatingType())
2278 return false;
2279
Daniel Dunbar98303b92009-09-13 08:03:58 +00002280 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002281 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002282 return true;
2283
Daniel Dunbar45815812010-02-01 23:31:26 +00002284 // Small complex integer types are "integer like".
2285 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2286 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002287
2288 // Single element and zero sized arrays should be allowed, by the definition
2289 // above, but they are not.
2290
2291 // Otherwise, it must be a record type.
2292 const RecordType *RT = Ty->getAs<RecordType>();
2293 if (!RT) return false;
2294
2295 // Ignore records with flexible arrays.
2296 const RecordDecl *RD = RT->getDecl();
2297 if (RD->hasFlexibleArrayMember())
2298 return false;
2299
2300 // Check that all sub-fields are at offset 0, and are themselves "integer
2301 // like".
2302 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2303
2304 bool HadField = false;
2305 unsigned idx = 0;
2306 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2307 i != e; ++i, ++idx) {
2308 const FieldDecl *FD = *i;
2309
Daniel Dunbar679855a2010-01-29 03:22:29 +00002310 // Bit-fields are not addressable, we only need to verify they are "integer
2311 // like". We still have to disallow a subsequent non-bitfield, for example:
2312 // struct { int : 0; int x }
2313 // is non-integer like according to gcc.
2314 if (FD->isBitField()) {
2315 if (!RD->isUnion())
2316 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002317
Daniel Dunbar679855a2010-01-29 03:22:29 +00002318 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2319 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002320
Daniel Dunbar679855a2010-01-29 03:22:29 +00002321 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002322 }
2323
Daniel Dunbar679855a2010-01-29 03:22:29 +00002324 // Check if this field is at offset 0.
2325 if (Layout.getFieldOffset(idx) != 0)
2326 return false;
2327
Daniel Dunbar98303b92009-09-13 08:03:58 +00002328 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2329 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002330
Daniel Dunbar679855a2010-01-29 03:22:29 +00002331 // Only allow at most one field in a structure. This doesn't match the
2332 // wording above, but follows gcc in situations with a field following an
2333 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002334 if (!RD->isUnion()) {
2335 if (HadField)
2336 return false;
2337
2338 HadField = true;
2339 }
2340 }
2341
2342 return true;
2343}
2344
Chris Lattnera3c109b2010-07-29 02:16:43 +00002345ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002346 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002347 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002348
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002349 // Large vector types should be returned via memory.
2350 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2351 return ABIArgInfo::getIndirect(0);
2352
John McCalld608cdb2010-08-22 10:59:02 +00002353 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002354 // Treat an enum type as its underlying type.
2355 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2356 RetTy = EnumTy->getDecl()->getIntegerType();
2357
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002358 return (RetTy->isPromotableIntegerType() ?
2359 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002360 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002361
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002362 // Structures with either a non-trivial destructor or a non-trivial
2363 // copy constructor are always indirect.
2364 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2365 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2366
Daniel Dunbar98303b92009-09-13 08:03:58 +00002367 // Are we following APCS?
2368 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002369 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002370 return ABIArgInfo::getIgnore();
2371
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002372 // Complex types are all returned as packed integers.
2373 //
2374 // FIXME: Consider using 2 x vector types if the back end handles them
2375 // correctly.
2376 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002377 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002378 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002379
Daniel Dunbar98303b92009-09-13 08:03:58 +00002380 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002381 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002382 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002383 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002384 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002385 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002386 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002387 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2388 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002389 }
2390
2391 // Otherwise return in memory.
2392 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002393 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002394
2395 // Otherwise this is an AAPCS variant.
2396
Chris Lattnera3c109b2010-07-29 02:16:43 +00002397 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002398 return ABIArgInfo::getIgnore();
2399
Daniel Dunbar98303b92009-09-13 08:03:58 +00002400 // Aggregates <= 4 bytes are returned in r0; other aggregates
2401 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002402 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002403 if (Size <= 32) {
2404 // Return in the smallest viable integer type.
2405 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002406 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002407 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002408 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2409 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002410 }
2411
Daniel Dunbar98303b92009-09-13 08:03:58 +00002412 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002413}
2414
2415llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002416 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002417 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002418 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002419 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002420
2421 CGBuilderTy &Builder = CGF.Builder;
2422 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2423 "ap");
2424 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2425 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002426 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002427 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2428
2429 uint64_t Offset =
2430 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2431 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002432 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002433 "ap.next");
2434 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2435
2436 return AddrTyped;
2437}
2438
Chris Lattnera3c109b2010-07-29 02:16:43 +00002439ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2440 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002441 return ABIArgInfo::getIgnore();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002442
John McCalld608cdb2010-08-22 10:59:02 +00002443 if (isAggregateTypeForABI(RetTy))
Chris Lattnera3c109b2010-07-29 02:16:43 +00002444 return ABIArgInfo::getIndirect(0);
2445
2446 // Treat an enum type as its underlying type.
2447 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2448 RetTy = EnumTy->getDecl()->getIntegerType();
2449
2450 return (RetTy->isPromotableIntegerType() ?
2451 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002452}
2453
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002454//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002455// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002456//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002457
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002458namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002459
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002460class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002461public:
2462 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2463
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002464 bool isPromotableIntegerType(QualType Ty) const;
2465
Chris Lattnera3c109b2010-07-29 02:16:43 +00002466 ABIArgInfo classifyReturnType(QualType RetTy) const;
2467 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002468
Chris Lattneree5dcd02010-07-29 02:31:05 +00002469 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002470 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002471 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2472 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002473 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002474 }
2475
2476 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2477 CodeGenFunction &CGF) const;
2478};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002479
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002480class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2481public:
Chris Lattnerea044322010-07-29 02:01:43 +00002482 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2483 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002484};
2485
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002486}
2487
2488bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2489 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002490 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002491 switch (BT->getKind()) {
2492 case BuiltinType::Bool:
2493 case BuiltinType::Char_S:
2494 case BuiltinType::Char_U:
2495 case BuiltinType::SChar:
2496 case BuiltinType::UChar:
2497 case BuiltinType::Short:
2498 case BuiltinType::UShort:
2499 case BuiltinType::Int:
2500 case BuiltinType::UInt:
2501 return true;
2502 default:
2503 return false;
2504 }
2505 return false;
2506}
2507
2508llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2509 CodeGenFunction &CGF) const {
2510 // FIXME: Implement
2511 return 0;
2512}
2513
2514
Chris Lattnera3c109b2010-07-29 02:16:43 +00002515ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2516 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002517 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002518 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002519 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002520
2521 return (isPromotableIntegerType(RetTy) ?
2522 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002523}
2524
Chris Lattnera3c109b2010-07-29 02:16:43 +00002525ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002526 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002527 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002528
2529 return (isPromotableIntegerType(Ty) ?
2530 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002531}
2532
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002533//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002534// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002535//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002536
2537namespace {
2538
2539class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2540public:
Chris Lattnerea044322010-07-29 02:01:43 +00002541 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2542 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002543 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2544 CodeGen::CodeGenModule &M) const;
2545};
2546
2547}
2548
2549void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2550 llvm::GlobalValue *GV,
2551 CodeGen::CodeGenModule &M) const {
2552 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2553 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2554 // Handle 'interrupt' attribute:
2555 llvm::Function *F = cast<llvm::Function>(GV);
2556
2557 // Step 1: Set ISR calling convention.
2558 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2559
2560 // Step 2: Add attributes goodness.
2561 F->addFnAttr(llvm::Attribute::NoInline);
2562
2563 // Step 3: Emit ISR vector alias.
2564 unsigned Num = attr->getNumber() + 0xffe0;
2565 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2566 "vector_" +
2567 llvm::LowercaseString(llvm::utohexstr(Num)),
2568 GV, &M.getModule());
2569 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002570 }
2571}
2572
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002573//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002574// MIPS ABI Implementation. This works for both little-endian and
2575// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002576//===----------------------------------------------------------------------===//
2577
John McCallaeeb7012010-05-27 06:19:26 +00002578namespace {
2579class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2580public:
Chris Lattnerea044322010-07-29 02:01:43 +00002581 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2582 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002583
2584 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2585 return 29;
2586 }
2587
2588 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002589 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002590};
2591}
2592
2593bool
2594MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2595 llvm::Value *Address) const {
2596 // This information comes from gcc's implementation, which seems to
2597 // as canonical as it gets.
2598
2599 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2600 llvm::LLVMContext &Context = CGF.getLLVMContext();
2601
2602 // Everything on MIPS is 4 bytes. Double-precision FP registers
2603 // are aliased to pairs of single-precision FP registers.
2604 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2605 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2606
2607 // 0-31 are the general purpose registers, $0 - $31.
2608 // 32-63 are the floating-point registers, $f0 - $f31.
2609 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2610 // 66 is the (notional, I think) register for signal-handler return.
2611 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2612
2613 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2614 // They are one bit wide and ignored here.
2615
2616 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2617 // (coprocessor 1 is the FP unit)
2618 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2619 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2620 // 176-181 are the DSP accumulator registers.
2621 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2622
2623 return false;
2624}
2625
2626
Chris Lattnerea044322010-07-29 02:01:43 +00002627const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002628 if (TheTargetCodeGenInfo)
2629 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002630
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002631 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2632 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002633
Chris Lattner9c254f02010-06-29 06:01:59 +00002634 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002635 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002636 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002637 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002638
John McCallaeeb7012010-05-27 06:19:26 +00002639 case llvm::Triple::mips:
2640 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002641 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002642
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002643 case llvm::Triple::arm:
2644 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002645 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002646 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002647 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002648 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002649
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002650 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002651 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002652
John McCallec853ba2010-03-11 00:10:12 +00002653 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00002654 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00002655
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002656 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00002657 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002658
2659 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00002660 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002661
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002662 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002663 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002664 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002665 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002666 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002667 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002668 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002669 case llvm::Triple::AuroraUX:
2670 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002671 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002672 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002673 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002674 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002675
2676 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002677 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002678 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002679 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002680
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002681 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00002682 switch (Triple.getOS()) {
2683 case llvm::Triple::Win32:
2684 case llvm::Triple::MinGW64:
2685 case llvm::Triple::Cygwin:
2686 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2687 default:
2688 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2689 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002690 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002691}