blob: 4234c6f3378c45ccd6a396bc13d0d1714bb3d079 [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"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
John McCallaeeb7012010-05-27 06:19:26 +000026static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
27 llvm::Value *Array,
28 llvm::Value *Value,
29 unsigned FirstIndex,
30 unsigned LastIndex) {
31 // Alternatively, we could emit this as a loop in the source.
32 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
33 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
34 Builder.CreateStore(Value, Cell);
35 }
36}
37
John McCalld608cdb2010-08-22 10:59:02 +000038static bool isAggregateTypeForABI(QualType T) {
39 return CodeGenFunction::hasAggregateLLVMType(T) ||
40 T->isMemberFunctionPointerType();
41}
42
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000043ABIInfo::~ABIInfo() {}
44
Chris Lattnerea044322010-07-29 02:01:43 +000045ASTContext &ABIInfo::getContext() const {
46 return CGT.getContext();
47}
48
49llvm::LLVMContext &ABIInfo::getVMContext() const {
50 return CGT.getLLVMContext();
51}
52
53const llvm::TargetData &ABIInfo::getTargetData() const {
54 return CGT.getTargetData();
55}
56
57
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000058void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000059 llvm::raw_ostream &OS = llvm::errs();
60 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000061 switch (TheKind) {
62 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000063 OS << "Direct Type=";
64 if (const llvm::Type *Ty = getCoerceToType())
65 Ty->print(OS);
66 else
67 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000068 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000069 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000070 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000071 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000072 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000073 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000074 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000076 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000077 << " Byal=" << getIndirectByVal()
78 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000079 break;
80 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000081 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000082 break;
83 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000084 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000085}
86
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000087TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
88
Daniel Dunbar98303b92009-09-13 08:03:58 +000089static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000090
91/// isEmptyField - Return true iff a the field is "empty", that is it
92/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000093static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
94 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000095 if (FD->isUnnamedBitfield())
96 return true;
97
98 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000099
Daniel Dunbar98303b92009-09-13 08:03:58 +0000100 // Constant arrays of empty records count as empty, strip them off.
101 if (AllowArrays)
102 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
103 FT = AT->getElementType();
104
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000105 const RecordType *RT = FT->getAs<RecordType>();
106 if (!RT)
107 return false;
108
109 // C++ record fields are never empty, at least in the Itanium ABI.
110 //
111 // FIXME: We should use a predicate for whether this behavior is true in the
112 // current ABI.
113 if (isa<CXXRecordDecl>(RT->getDecl()))
114 return false;
115
Daniel Dunbar98303b92009-09-13 08:03:58 +0000116 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000117}
118
119/// isEmptyRecord - Return true iff a structure contains only empty
120/// fields. Note that a structure with a flexible array member is not
121/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000122static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000123 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000124 if (!RT)
125 return 0;
126 const RecordDecl *RD = RT->getDecl();
127 if (RD->hasFlexibleArrayMember())
128 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000129
130 // If this is a C++ record, check the bases first.
131 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
132 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
133 e = CXXRD->bases_end(); i != e; ++i)
134 if (!isEmptyRecord(Context, i->getType(), true))
135 return false;
136
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000137 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
138 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000139 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000140 return false;
141 return true;
142}
143
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000144/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
145/// a non-trivial destructor or a non-trivial copy constructor.
146static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
147 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
148 if (!RD)
149 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000150
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000151 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
152}
153
154/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
155/// a record type with either a non-trivial destructor or a non-trivial copy
156/// constructor.
157static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
158 const RecordType *RT = T->getAs<RecordType>();
159 if (!RT)
160 return false;
161
162 return hasNonTrivialDestructorOrCopyConstructor(RT);
163}
164
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165/// isSingleElementStruct - Determine if a structure is a "single
166/// element struct", i.e. it has exactly one non-empty field or
167/// exactly one field which is itself a single element
168/// struct. Structures with flexible array members are never
169/// considered single element structs.
170///
171/// \return The field declaration for the single non-empty field, if
172/// it exists.
173static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
174 const RecordType *RT = T->getAsStructureType();
175 if (!RT)
176 return 0;
177
178 const RecordDecl *RD = RT->getDecl();
179 if (RD->hasFlexibleArrayMember())
180 return 0;
181
182 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000183
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000184 // If this is a C++ record, check the bases first.
185 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
186 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
187 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000188 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000189 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000190 continue;
191
192 // If we already found an element then this isn't a single-element struct.
193 if (Found)
194 return 0;
195
196 // If this is non-empty and not a single element struct, the composite
197 // cannot be a single element struct.
198 Found = isSingleElementStruct(i->getType(), Context);
199 if (!Found)
200 return 0;
201 }
202 }
203
204 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
206 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000207 const FieldDecl *FD = *i;
208 QualType FT = FD->getType();
209
210 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000211 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000212 continue;
213
214 // If we already found an element then this isn't a single-element
215 // struct.
216 if (Found)
217 return 0;
218
219 // Treat single element arrays as the element.
220 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
221 if (AT->getSize().getZExtValue() != 1)
222 break;
223 FT = AT->getElementType();
224 }
225
John McCalld608cdb2010-08-22 10:59:02 +0000226 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000227 Found = FT.getTypePtr();
228 } else {
229 Found = isSingleElementStruct(FT, Context);
230 if (!Found)
231 return 0;
232 }
233 }
234
235 return Found;
236}
237
238static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000239 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000240 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
241 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000242 return false;
243
244 uint64_t Size = Context.getTypeSize(Ty);
245 return Size == 32 || Size == 64;
246}
247
Daniel Dunbar53012f42009-11-09 01:33:53 +0000248/// canExpandIndirectArgument - Test whether an argument type which is to be
249/// passed indirectly (on the stack) would have the equivalent layout if it was
250/// expanded into separate arguments. If so, we prefer to do the latter to avoid
251/// inhibiting optimizations.
252///
253// FIXME: This predicate is missing many cases, currently it just follows
254// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
255// should probably make this smarter, or better yet make the LLVM backend
256// capable of handling it.
257static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
258 // We can only expand structure types.
259 const RecordType *RT = Ty->getAs<RecordType>();
260 if (!RT)
261 return false;
262
263 // We can only expand (C) structures.
264 //
265 // FIXME: This needs to be generalized to handle classes as well.
266 const RecordDecl *RD = RT->getDecl();
267 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
268 return false;
269
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 const FieldDecl *FD = *i;
273
274 if (!is32Or64BitBasicType(FD->getType(), Context))
275 return false;
276
277 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
278 // how to expand them yet, and the predicate for telling if a bitfield still
279 // counts as "basic" is more complicated than what we were doing previously.
280 if (FD->isBitField())
281 return false;
282 }
283
284 return true;
285}
286
287namespace {
288/// DefaultABIInfo - The default implementation for ABI specific
289/// details. This implementation provides information which results in
290/// self-consistent and sensible LLVM IR generation, but does not
291/// conform to any particular ABI.
292class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000293public:
294 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000295
Chris Lattnera3c109b2010-07-29 02:16:43 +0000296 ABIArgInfo classifyReturnType(QualType RetTy) const;
297 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000298
Chris Lattneree5dcd02010-07-29 02:31:05 +0000299 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
302 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000303 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000304 }
305
306 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
307 CodeGenFunction &CGF) const;
308};
309
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000310class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
311public:
Chris Lattnerea044322010-07-29 02:01:43 +0000312 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
313 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000314};
315
316llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
317 CodeGenFunction &CGF) const {
318 return 0;
319}
320
Chris Lattnera3c109b2010-07-29 02:16:43 +0000321ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000322 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000323 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000324
Chris Lattnera14db752010-03-11 18:19:55 +0000325 // Treat an enum type as its underlying type.
326 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
327 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000328
Chris Lattnera14db752010-03-11 18:19:55 +0000329 return (Ty->isPromotableIntegerType() ?
330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000331}
332
Bill Wendlingbb465d72010-10-18 03:41:31 +0000333/// UseX86_MMXType - Return true if this is an MMX type that should use the special
334/// x86_mmx type.
335bool UseX86_MMXType(const llvm::Type *IRType) {
336 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
337 // special x86_mmx type.
338 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
339 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
340 IRType->getScalarSizeInBits() != 64;
341}
342
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000343//===----------------------------------------------------------------------===//
344// X86-32 ABI Implementation
345//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000346
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000347/// X86_32ABIInfo - The X86-32 ABI information.
348class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000349 static const unsigned MinABIStackAlignInBytes = 4;
350
David Chisnall1e4249c2009-08-17 23:08:21 +0000351 bool IsDarwinVectorABI;
352 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000353
354 static bool isRegisterSize(unsigned Size) {
355 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
356 }
357
358 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
359
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000360 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
361 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000362 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000363
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000364 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000365 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000366
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000367public:
Chris Lattnerea044322010-07-29 02:01:43 +0000368
Chris Lattnera3c109b2010-07-29 02:16:43 +0000369 ABIArgInfo classifyReturnType(QualType RetTy) const;
370 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000371
Chris Lattneree5dcd02010-07-29 02:31:05 +0000372 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000373 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000374 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
375 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000376 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000377 }
378
379 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
380 CodeGenFunction &CGF) const;
381
Chris Lattnerea044322010-07-29 02:01:43 +0000382 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
383 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000384};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000385
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000386class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
387public:
Chris Lattnerea044322010-07-29 02:01:43 +0000388 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
389 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000390
391 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
392 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000393
394 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
395 // Darwin uses different dwarf register numbers for EH.
396 if (CGM.isTargetDarwin()) return 5;
397
398 return 4;
399 }
400
401 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
402 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000403};
404
405}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000406
407/// shouldReturnTypeInRegister - Determine if the given type should be
408/// passed in a register (for the Darwin ABI).
409bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
410 ASTContext &Context) {
411 uint64_t Size = Context.getTypeSize(Ty);
412
413 // Type must be register sized.
414 if (!isRegisterSize(Size))
415 return false;
416
417 if (Ty->isVectorType()) {
418 // 64- and 128- bit vectors inside structures are not returned in
419 // registers.
420 if (Size == 64 || Size == 128)
421 return false;
422
423 return true;
424 }
425
Daniel Dunbar77115232010-05-15 00:00:30 +0000426 // If this is a builtin, pointer, enum, complex type, member pointer, or
427 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000428 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000429 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000430 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000431 return true;
432
433 // Arrays are treated like records.
434 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
435 return shouldReturnTypeInRegister(AT->getElementType(), Context);
436
437 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000438 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000439 if (!RT) return false;
440
Anders Carlssona8874232010-01-27 03:25:19 +0000441 // FIXME: Traverse bases here too.
442
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000443 // Structure types are passed in register if all fields would be
444 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000445 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
446 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000447 const FieldDecl *FD = *i;
448
449 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000450 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000451 continue;
452
453 // Check fields recursively.
454 if (!shouldReturnTypeInRegister(FD->getType(), Context))
455 return false;
456 }
457
458 return true;
459}
460
Chris Lattnera3c109b2010-07-29 02:16:43 +0000461ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
462 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000464
Chris Lattnera3c109b2010-07-29 02:16:43 +0000465 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000466 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000467 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000468 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000469
470 // 128-bit vectors are a special case; they are returned in
471 // registers and we need to make sure to pick a type the LLVM
472 // backend will like.
473 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000474 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000475 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000476
477 // Always return in register if it fits in a general purpose
478 // register, or if it is 64 bits and has a single element.
479 if ((Size == 8 || Size == 16 || Size == 32) ||
480 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000481 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000482 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000483
484 return ABIArgInfo::getIndirect(0);
485 }
486
487 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000488 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000489
John McCalld608cdb2010-08-22 10:59:02 +0000490 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000491 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000492 // Structures with either a non-trivial destructor or a non-trivial
493 // copy constructor are always indirect.
494 if (hasNonTrivialDestructorOrCopyConstructor(RT))
495 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000496
Anders Carlsson40092972009-10-20 22:07:59 +0000497 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498 if (RT->getDecl()->hasFlexibleArrayMember())
499 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000500 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000501
David Chisnall1e4249c2009-08-17 23:08:21 +0000502 // If specified, structs and unions are always indirect.
503 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000504 return ABIArgInfo::getIndirect(0);
505
506 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000507 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000508 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000509 if (BT->isIntegerType()) {
510 // We need to use the size of the structure, padding
511 // bit-fields can adjust that to be larger than the single
512 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000513 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000514 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000515 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
516 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000517
Chris Lattnera3c109b2010-07-29 02:16:43 +0000518 if (BT->getKind() == BuiltinType::Float) {
519 assert(getContext().getTypeSize(RetTy) ==
520 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000521 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000522 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000523 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000524
Chris Lattnera3c109b2010-07-29 02:16:43 +0000525 if (BT->getKind() == BuiltinType::Double) {
526 assert(getContext().getTypeSize(RetTy) ==
527 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000528 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000529 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000530 }
531 } else if (SeltTy->isPointerType()) {
532 // FIXME: It would be really nice if this could come out as the proper
533 // pointer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000534 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000535 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000536 } else if (SeltTy->isVectorType()) {
537 // 64- and 128-bit vectors are never returned in a
538 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000539 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000540 if (Size == 64 || Size == 128)
541 return ABIArgInfo::getIndirect(0);
542
Chris Lattnera3c109b2010-07-29 02:16:43 +0000543 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000544 }
545 }
546
547 // Small structures which are register sized are generally returned
548 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000549 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
550 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000551 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552 }
553
554 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000555 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000556
Chris Lattnera3c109b2010-07-29 02:16:43 +0000557 // Treat an enum type as its underlying type.
558 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
559 RetTy = EnumTy->getDecl()->getIntegerType();
560
561 return (RetTy->isPromotableIntegerType() ?
562 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000563}
564
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000565static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
566 const RecordType *RT = Ty->getAs<RecordType>();
567 if (!RT)
568 return 0;
569 const RecordDecl *RD = RT->getDecl();
570
571 // If this is a C++ record, check the bases first.
572 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
573 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
574 e = CXXRD->bases_end(); i != e; ++i)
575 if (!isRecordWithSSEVectorType(Context, i->getType()))
576 return false;
577
578 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
579 i != e; ++i) {
580 QualType FT = i->getType();
581
582 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
583 return true;
584
585 if (isRecordWithSSEVectorType(Context, FT))
586 return true;
587 }
588
589 return false;
590}
591
Daniel Dunbare59d8582010-09-16 20:42:06 +0000592unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
593 unsigned Align) const {
594 // Otherwise, if the alignment is less than or equal to the minimum ABI
595 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000596 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000597 return 0; // Use default alignment.
598
599 // On non-Darwin, the stack type alignment is always 4.
600 if (!IsDarwinVectorABI) {
601 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000602 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000603 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000604
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000605 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
606 if (isRecordWithSSEVectorType(getContext(), Ty))
607 return 16;
608
609 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000610}
611
Chris Lattnera3c109b2010-07-29 02:16:43 +0000612ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000613 if (!ByVal)
614 return ABIArgInfo::getIndirect(0, false);
615
Daniel Dunbare59d8582010-09-16 20:42:06 +0000616 // Compute the byval alignment.
617 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
618 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
619 if (StackAlign == 0)
620 return ABIArgInfo::getIndirect(0);
621
622 // If the stack alignment is less than the type alignment, realign the
623 // argument.
624 if (StackAlign < TypeAlign)
625 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
626 /*Realign=*/true);
627
628 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000629}
630
Chris Lattnera3c109b2010-07-29 02:16:43 +0000631ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000632 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000633 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000635 if (const RecordType *RT = Ty->getAs<RecordType>()) {
636 // Structures with either a non-trivial destructor or a non-trivial
637 // copy constructor are always indirect.
638 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000639 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000640
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000641 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000642 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000643 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000644
645 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000646 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000647 return ABIArgInfo::getIgnore();
648
Daniel Dunbar53012f42009-11-09 01:33:53 +0000649 // Expand small (<= 128-bit) record types when we know that the stack layout
650 // of those arguments will match the struct. This is important because the
651 // LLVM backend isn't smart enough to remove byval, which inhibits many
652 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000653 if (getContext().getTypeSize(Ty) <= 4*32 &&
654 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000655 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000656
Chris Lattnera3c109b2010-07-29 02:16:43 +0000657 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000658 }
659
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000660 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000661 // On Darwin, some vectors are passed in memory, we handle this by passing
662 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000663 if (IsDarwinVectorABI) {
664 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000665 if ((Size == 8 || Size == 16 || Size == 32) ||
666 (Size == 64 && VT->getNumElements() == 1))
667 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
668 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000669 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000670
671 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
672 if (UseX86_MMXType(IRType)) {
673 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
674 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
675 return AAI;
676 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000677
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000678 return ABIArgInfo::getDirect();
679 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000680
681
Chris Lattnera3c109b2010-07-29 02:16:43 +0000682 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
683 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000684
Chris Lattnera3c109b2010-07-29 02:16:43 +0000685 return (Ty->isPromotableIntegerType() ?
686 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687}
688
689llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
690 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000691 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000692 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000693
694 CGBuilderTy &Builder = CGF.Builder;
695 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
696 "ap");
697 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
698 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000699 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000700 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
701
702 uint64_t Offset =
703 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
704 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000705 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000706 "ap.next");
707 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
708
709 return AddrTyped;
710}
711
Charles Davis74f72932010-02-13 15:54:06 +0000712void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
713 llvm::GlobalValue *GV,
714 CodeGen::CodeGenModule &CGM) const {
715 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
716 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
717 // Get the LLVM function.
718 llvm::Function *Fn = cast<llvm::Function>(GV);
719
720 // Now add the 'alignstack' attribute with a value of 16.
721 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
722 }
723 }
724}
725
John McCall6374c332010-03-06 00:35:14 +0000726bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
727 CodeGen::CodeGenFunction &CGF,
728 llvm::Value *Address) const {
729 CodeGen::CGBuilderTy &Builder = CGF.Builder;
730 llvm::LLVMContext &Context = CGF.getLLVMContext();
731
732 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
733 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000734
John McCall6374c332010-03-06 00:35:14 +0000735 // 0-7 are the eight integer registers; the order is different
736 // on Darwin (for EH), but the range is the same.
737 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000738 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000739
740 if (CGF.CGM.isTargetDarwin()) {
741 // 12-16 are st(0..4). Not sure why we stop at 4.
742 // These have size 16, which is sizeof(long double) on
743 // platforms with 8-byte alignment for that type.
744 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000745 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000746
John McCall6374c332010-03-06 00:35:14 +0000747 } else {
748 // 9 is %eflags, which doesn't get a size on Darwin for some
749 // reason.
750 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
751
752 // 11-16 are st(0..5). Not sure why we stop at 5.
753 // These have size 12, which is sizeof(long double) on
754 // platforms with 4-byte alignment for that type.
755 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000756 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
757 }
John McCall6374c332010-03-06 00:35:14 +0000758
759 return false;
760}
761
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000762//===----------------------------------------------------------------------===//
763// X86-64 ABI Implementation
764//===----------------------------------------------------------------------===//
765
766
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000767namespace {
768/// X86_64ABIInfo - The X86_64 ABI information.
769class X86_64ABIInfo : public ABIInfo {
770 enum Class {
771 Integer = 0,
772 SSE,
773 SSEUp,
774 X87,
775 X87Up,
776 ComplexX87,
777 NoClass,
778 Memory
779 };
780
781 /// merge - Implement the X86_64 ABI merging algorithm.
782 ///
783 /// Merge an accumulating classification \arg Accum with a field
784 /// classification \arg Field.
785 ///
786 /// \param Accum - The accumulating classification. This should
787 /// always be either NoClass or the result of a previous merge
788 /// call. In addition, this should never be Memory (the caller
789 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000790 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000791
792 /// classify - Determine the x86_64 register classes in which the
793 /// given type T should be passed.
794 ///
795 /// \param Lo - The classification for the parts of the type
796 /// residing in the low word of the containing object.
797 ///
798 /// \param Hi - The classification for the parts of the type
799 /// residing in the high word of the containing object.
800 ///
801 /// \param OffsetBase - The bit offset of this type in the
802 /// containing object. Some parameters are classified different
803 /// depending on whether they straddle an eightbyte boundary.
804 ///
805 /// If a word is unused its result will be NoClass; if a type should
806 /// be passed in Memory then at least the classification of \arg Lo
807 /// will be Memory.
808 ///
809 /// The \arg Lo class will be NoClass iff the argument is ignored.
810 ///
811 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
812 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000813 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000814
Chris Lattner0f408f52010-07-29 04:56:46 +0000815 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattner603519d2010-07-29 17:49:08 +0000816 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattnerf47c9442010-07-29 18:13:09 +0000817 unsigned IROffset, QualType SourceTy,
818 unsigned SourceOffset) const;
Chris Lattner0d2656d2010-07-29 17:40:35 +0000819 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
820 unsigned IROffset, QualType SourceTy,
821 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000822
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000823 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000824 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000825 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000826
827 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000828 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000829 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000830
Chris Lattnera3c109b2010-07-29 02:16:43 +0000831 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000832
Bill Wendlingbb465d72010-10-18 03:41:31 +0000833 ABIArgInfo classifyArgumentType(QualType Ty,
834 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000835 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000836
837public:
Chris Lattnerea044322010-07-29 02:01:43 +0000838 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000839
Chris Lattneree5dcd02010-07-29 02:31:05 +0000840 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000841
842 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
843 CodeGenFunction &CGF) const;
844};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000845
Chris Lattnerf13721d2010-08-31 16:44:54 +0000846/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
847class WinX86_64ABIInfo : public X86_64ABIInfo {
848public:
849 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
850
851 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
852 CodeGenFunction &CGF) const;
853};
854
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000855class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
856public:
Chris Lattnerea044322010-07-29 02:01:43 +0000857 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
858 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000859
860 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
861 return 7;
862 }
863
864 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
865 llvm::Value *Address) const {
866 CodeGen::CGBuilderTy &Builder = CGF.Builder;
867 llvm::LLVMContext &Context = CGF.getLLVMContext();
868
869 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
870 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000871
John McCallaeeb7012010-05-27 06:19:26 +0000872 // 0-15 are the 16 integer registers.
873 // 16 is %rip.
874 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000875
876 return false;
877 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000878};
879
Chris Lattnerf13721d2010-08-31 16:44:54 +0000880class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
881public:
882 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
883 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
884
885 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
886 return 7;
887 }
888
889 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
890 llvm::Value *Address) const {
891 CodeGen::CGBuilderTy &Builder = CGF.Builder;
892 llvm::LLVMContext &Context = CGF.getLLVMContext();
893
894 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
895 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000896
Chris Lattnerf13721d2010-08-31 16:44:54 +0000897 // 0-15 are the 16 integer registers.
898 // 16 is %rip.
899 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
900
901 return false;
902 }
903};
904
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000905}
906
Chris Lattner1090a9b2010-06-28 21:43:59 +0000907X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000908 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
909 // classified recursively so that always two fields are
910 // considered. The resulting class is calculated according to
911 // the classes of the fields in the eightbyte:
912 //
913 // (a) If both classes are equal, this is the resulting class.
914 //
915 // (b) If one of the classes is NO_CLASS, the resulting class is
916 // the other class.
917 //
918 // (c) If one of the classes is MEMORY, the result is the MEMORY
919 // class.
920 //
921 // (d) If one of the classes is INTEGER, the result is the
922 // INTEGER.
923 //
924 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
925 // MEMORY is used as class.
926 //
927 // (f) Otherwise class SSE is used.
928
929 // Accum should never be memory (we should have returned) or
930 // ComplexX87 (because this cannot be passed in a structure).
931 assert((Accum != Memory && Accum != ComplexX87) &&
932 "Invalid accumulated classification during merge.");
933 if (Accum == Field || Field == NoClass)
934 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000935 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000936 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000937 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000938 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000939 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000940 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000941 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
942 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000943 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000944 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000945}
946
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000947void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948 Class &Lo, Class &Hi) const {
949 // FIXME: This code can be simplified by introducing a simple value class for
950 // Class pairs with appropriate constructor methods for the various
951 // situations.
952
953 // FIXME: Some of the split computations are wrong; unaligned vectors
954 // shouldn't be passed in registers for example, so there is no chance they
955 // can straddle an eightbyte. Verify & simplify.
956
957 Lo = Hi = NoClass;
958
959 Class &Current = OffsetBase < 64 ? Lo : Hi;
960 Current = Memory;
961
John McCall183700f2009-09-21 23:43:11 +0000962 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000963 BuiltinType::Kind k = BT->getKind();
964
965 if (k == BuiltinType::Void) {
966 Current = NoClass;
967 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
968 Lo = Integer;
969 Hi = Integer;
970 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
971 Current = Integer;
972 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
973 Current = SSE;
974 } else if (k == BuiltinType::LongDouble) {
975 Lo = X87;
976 Hi = X87Up;
977 }
978 // FIXME: _Decimal32 and _Decimal64 are SSE.
979 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000980 return;
981 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000982
Chris Lattner1090a9b2010-06-28 21:43:59 +0000983 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000984 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +0000985 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000986 return;
987 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000988
Chris Lattner1090a9b2010-06-28 21:43:59 +0000989 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000990 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000991 return;
992 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000993
Chris Lattner1090a9b2010-06-28 21:43:59 +0000994 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000995 if (Ty->isMemberFunctionPointerType())
996 Lo = Hi = Integer;
997 else
998 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000999 return;
1000 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001001
Chris Lattner1090a9b2010-06-28 21:43:59 +00001002 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001003 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001004 if (Size == 32) {
1005 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1006 // float> as integer.
1007 Current = Integer;
1008
1009 // If this type crosses an eightbyte boundary, it should be
1010 // split.
1011 uint64_t EB_Real = (OffsetBase) / 64;
1012 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1013 if (EB_Real != EB_Imag)
1014 Hi = Lo;
1015 } else if (Size == 64) {
1016 // gcc passes <1 x double> in memory. :(
1017 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1018 return;
1019
1020 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001021 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001022 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1023 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1024 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001025 Current = Integer;
1026 else
1027 Current = SSE;
1028
1029 // If this type crosses an eightbyte boundary, it should be
1030 // split.
1031 if (OffsetBase && OffsetBase != 64)
1032 Hi = Lo;
1033 } else if (Size == 128) {
1034 Lo = SSE;
1035 Hi = SSEUp;
1036 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001037 return;
1038 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001039
Chris Lattner1090a9b2010-06-28 21:43:59 +00001040 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001041 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042
Chris Lattnerea044322010-07-29 02:01:43 +00001043 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001044 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001045 if (Size <= 64)
1046 Current = Integer;
1047 else if (Size <= 128)
1048 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001049 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001050 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001051 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001052 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001053 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001054 Current = ComplexX87;
1055
1056 // If this complex type crosses an eightbyte boundary then it
1057 // should be split.
1058 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001059 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001060 if (Hi == NoClass && EB_Real != EB_Imag)
1061 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001062
Chris Lattner1090a9b2010-06-28 21:43:59 +00001063 return;
1064 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001065
Chris Lattnerea044322010-07-29 02:01:43 +00001066 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001067 // Arrays are treated like structures.
1068
Chris Lattnerea044322010-07-29 02:01:43 +00001069 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001070
1071 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1072 // than two eightbytes, ..., it has class MEMORY.
1073 if (Size > 128)
1074 return;
1075
1076 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1077 // fields, it has class MEMORY.
1078 //
1079 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001080 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001081 return;
1082
1083 // Otherwise implement simplified merge. We could be smarter about
1084 // this, but it isn't worth it and would be harder to verify.
1085 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001086 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001087 uint64_t ArraySize = AT->getSize().getZExtValue();
1088 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1089 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001090 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001091 Lo = merge(Lo, FieldLo);
1092 Hi = merge(Hi, FieldHi);
1093 if (Lo == Memory || Hi == Memory)
1094 break;
1095 }
1096
1097 // Do post merger cleanup (see below). Only case we worry about is Memory.
1098 if (Hi == Memory)
1099 Lo = Memory;
1100 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001101 return;
1102 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001103
Chris Lattner1090a9b2010-06-28 21:43:59 +00001104 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001105 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001106
1107 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1108 // than two eightbytes, ..., it has class MEMORY.
1109 if (Size > 128)
1110 return;
1111
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001112 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1113 // copy constructor or a non-trivial destructor, it is passed by invisible
1114 // reference.
1115 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1116 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001117
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118 const RecordDecl *RD = RT->getDecl();
1119
1120 // Assume variable sized types are passed in memory.
1121 if (RD->hasFlexibleArrayMember())
1122 return;
1123
Chris Lattnerea044322010-07-29 02:01:43 +00001124 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001125
1126 // Reset Lo class, this will be recomputed.
1127 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001128
1129 // If this is a C++ record, classify the bases first.
1130 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1131 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1132 e = CXXRD->bases_end(); i != e; ++i) {
1133 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1134 "Unexpected base class!");
1135 const CXXRecordDecl *Base =
1136 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1137
1138 // Classify this field.
1139 //
1140 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1141 // single eightbyte, each is classified separately. Each eightbyte gets
1142 // initialized to class NO_CLASS.
1143 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001144 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001145 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001146 Lo = merge(Lo, FieldLo);
1147 Hi = merge(Hi, FieldHi);
1148 if (Lo == Memory || Hi == Memory)
1149 break;
1150 }
1151 }
1152
1153 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001154 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001155 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1156 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001157 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1158 bool BitField = i->isBitField();
1159
1160 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1161 // fields, it has class MEMORY.
1162 //
1163 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001164 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001165 Lo = Memory;
1166 return;
1167 }
1168
1169 // Classify this field.
1170 //
1171 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1172 // exceeds a single eightbyte, each is classified
1173 // separately. Each eightbyte gets initialized to class
1174 // NO_CLASS.
1175 Class FieldLo, FieldHi;
1176
1177 // Bit-fields require special handling, they do not force the
1178 // structure to be passed in memory even if unaligned, and
1179 // therefore they can straddle an eightbyte.
1180 if (BitField) {
1181 // Ignore padding bit-fields.
1182 if (i->isUnnamedBitfield())
1183 continue;
1184
1185 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001186 uint64_t Size =
1187 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001188
1189 uint64_t EB_Lo = Offset / 64;
1190 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1191 FieldLo = FieldHi = NoClass;
1192 if (EB_Lo) {
1193 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1194 FieldLo = NoClass;
1195 FieldHi = Integer;
1196 } else {
1197 FieldLo = Integer;
1198 FieldHi = EB_Hi ? Integer : NoClass;
1199 }
1200 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001201 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001202 Lo = merge(Lo, FieldLo);
1203 Hi = merge(Hi, FieldHi);
1204 if (Lo == Memory || Hi == Memory)
1205 break;
1206 }
1207
1208 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1209 //
1210 // (a) If one of the classes is MEMORY, the whole argument is
1211 // passed in memory.
1212 //
1213 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1214
1215 // The first of these conditions is guaranteed by how we implement
1216 // the merge (just bail).
1217 //
1218 // The second condition occurs in the case of unions; for example
1219 // union { _Complex double; unsigned; }.
1220 if (Hi == Memory)
1221 Lo = Memory;
1222 if (Hi == SSEUp && Lo != SSE)
1223 Hi = SSE;
1224 }
1225}
1226
Chris Lattner9c254f02010-06-29 06:01:59 +00001227ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001228 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1229 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001230 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001231 // Treat an enum type as its underlying type.
1232 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1233 Ty = EnumTy->getDecl()->getIntegerType();
1234
1235 return (Ty->isPromotableIntegerType() ?
1236 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1237 }
1238
1239 return ABIArgInfo::getIndirect(0);
1240}
1241
Chris Lattner9c254f02010-06-29 06:01:59 +00001242ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001243 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1244 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001245 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001246 // Treat an enum type as its underlying type.
1247 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1248 Ty = EnumTy->getDecl()->getIntegerType();
1249
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001250 return (Ty->isPromotableIntegerType() ?
1251 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001252 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001253
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001254 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1255 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001256
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001257 // Compute the byval alignment. We trust the back-end to honor the
1258 // minimum ABI alignment for byval, to make cleaner IR.
1259 const unsigned MinABIAlign = 8;
Chris Lattnerea044322010-07-29 02:01:43 +00001260 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001261 if (Align > MinABIAlign)
1262 return ABIArgInfo::getIndirect(Align);
1263 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001264}
1265
Chris Lattner0f408f52010-07-29 04:56:46 +00001266/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1267/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1268/// vector register.
1269const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner15842bd2010-07-29 05:02:29 +00001270 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001271
Chris Lattner15842bd2010-07-29 05:02:29 +00001272 // Wrapper structs that just contain vectors are passed just like vectors,
1273 // strip them off if present.
1274 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1275 while (STy && STy->getNumElements() == 1) {
1276 IRType = STy->getElementType(0);
1277 STy = dyn_cast<llvm::StructType>(IRType);
1278 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001279
Chris Lattner0f408f52010-07-29 04:56:46 +00001280 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner15842bd2010-07-29 05:02:29 +00001281 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner0f408f52010-07-29 04:56:46 +00001282 const llvm::Type *EltTy = VT->getElementType();
1283 if (VT->getBitWidth() == 128 &&
1284 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1285 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1286 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1287 EltTy->isIntegerTy(128)))
1288 return VT;
1289 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001290
Chris Lattner0f408f52010-07-29 04:56:46 +00001291 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1292}
1293
Chris Lattnere2962be2010-07-29 07:30:00 +00001294/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1295/// is known to either be off the end of the specified type or being in
1296/// alignment padding. The user type specified is known to be at most 128 bits
1297/// in size, and have passed through X86_64ABIInfo::classify with a successful
1298/// classification that put one of the two halves in the INTEGER class.
1299///
1300/// It is conservatively correct to return false.
1301static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1302 unsigned EndBit, ASTContext &Context) {
1303 // If the bytes being queried are off the end of the type, there is no user
1304 // data hiding here. This handles analysis of builtins, vectors and other
1305 // types that don't contain interesting padding.
1306 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1307 if (TySize <= StartBit)
1308 return true;
1309
Chris Lattner021c3a32010-07-29 07:43:55 +00001310 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1311 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1312 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1313
1314 // Check each element to see if the element overlaps with the queried range.
1315 for (unsigned i = 0; i != NumElts; ++i) {
1316 // If the element is after the span we care about, then we're done..
1317 unsigned EltOffset = i*EltSize;
1318 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001319
Chris Lattner021c3a32010-07-29 07:43:55 +00001320 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1321 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1322 EndBit-EltOffset, Context))
1323 return false;
1324 }
1325 // If it overlaps no elements, then it is safe to process as padding.
1326 return true;
1327 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001328
Chris Lattnere2962be2010-07-29 07:30:00 +00001329 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1330 const RecordDecl *RD = RT->getDecl();
1331 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001332
Chris Lattnere2962be2010-07-29 07:30:00 +00001333 // If this is a C++ record, check the bases first.
1334 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1335 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1336 e = CXXRD->bases_end(); i != e; ++i) {
1337 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1338 "Unexpected base class!");
1339 const CXXRecordDecl *Base =
1340 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001341
Chris Lattnere2962be2010-07-29 07:30:00 +00001342 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001343 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001344 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001345
Chris Lattnere2962be2010-07-29 07:30:00 +00001346 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1347 if (!BitsContainNoUserData(i->getType(), BaseStart,
1348 EndBit-BaseOffset, Context))
1349 return false;
1350 }
1351 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001352
Chris Lattnere2962be2010-07-29 07:30:00 +00001353 // Verify that no field has data that overlaps the region of interest. Yes
1354 // this could be sped up a lot by being smarter about queried fields,
1355 // however we're only looking at structs up to 16 bytes, so we don't care
1356 // much.
1357 unsigned idx = 0;
1358 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1359 i != e; ++i, ++idx) {
1360 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001361
Chris Lattnere2962be2010-07-29 07:30:00 +00001362 // If we found a field after the region we care about, then we're done.
1363 if (FieldOffset >= EndBit) break;
1364
1365 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1366 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1367 Context))
1368 return false;
1369 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001370
Chris Lattnere2962be2010-07-29 07:30:00 +00001371 // If nothing in this record overlapped the area of interest, then we're
1372 // clean.
1373 return true;
1374 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001375
Chris Lattnere2962be2010-07-29 07:30:00 +00001376 return false;
1377}
1378
Chris Lattner0b362002010-07-29 18:39:32 +00001379/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1380/// float member at the specified offset. For example, {int,{float}} has a
1381/// float at offset 4. It is conservatively correct for this routine to return
1382/// false.
1383static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1384 const llvm::TargetData &TD) {
1385 // Base case if we find a float.
1386 if (IROffset == 0 && IRType->isFloatTy())
1387 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001388
Chris Lattner0b362002010-07-29 18:39:32 +00001389 // If this is a struct, recurse into the field at the specified offset.
1390 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1391 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1392 unsigned Elt = SL->getElementContainingOffset(IROffset);
1393 IROffset -= SL->getElementOffset(Elt);
1394 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1395 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001396
Chris Lattner0b362002010-07-29 18:39:32 +00001397 // If this is an array, recurse into the field at the specified offset.
1398 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1399 const llvm::Type *EltTy = ATy->getElementType();
1400 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1401 IROffset -= IROffset/EltSize*EltSize;
1402 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1403 }
1404
1405 return false;
1406}
1407
Chris Lattnerf47c9442010-07-29 18:13:09 +00001408
1409/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1410/// low 8 bytes of an XMM register, corresponding to the SSE class.
1411const llvm::Type *X86_64ABIInfo::
1412GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1413 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001414 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001415 // pass as float if the last 4 bytes is just padding. This happens for
1416 // structs that contain 3 floats.
1417 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1418 SourceOffset*8+64, getContext()))
1419 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001420
Chris Lattner0b362002010-07-29 18:39:32 +00001421 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1422 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1423 // case.
1424 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001425 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1426 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001427
Chris Lattnerf47c9442010-07-29 18:13:09 +00001428 return llvm::Type::getDoubleTy(getVMContext());
1429}
1430
1431
Chris Lattner0d2656d2010-07-29 17:40:35 +00001432/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1433/// an 8-byte GPR. This means that we either have a scalar or we are talking
1434/// about the high or low part of an up-to-16-byte struct. This routine picks
1435/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001436/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1437/// etc).
1438///
1439/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1440/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1441/// the 8-byte value references. PrefType may be null.
1442///
1443/// SourceTy is the source level type for the entire argument. SourceOffset is
1444/// an offset into this that we're processing (which is always either 0 or 8).
1445///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001446const llvm::Type *X86_64ABIInfo::
Chris Lattner0d2656d2010-07-29 17:40:35 +00001447GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1448 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001449 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1450 // returning an 8-byte unit starting with it. See if we can safely use it.
1451 if (IROffset == 0) {
1452 // Pointers and int64's always fill the 8-byte unit.
1453 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1454 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001455
Chris Lattnere2962be2010-07-29 07:30:00 +00001456 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1457 // goodness in the source type is just tail padding. This is allowed to
1458 // kick in for struct {double,int} on the int, but not on
1459 // struct{double,int,int} because we wouldn't return the second int. We
1460 // have to do this analysis on the source type because we can't depend on
1461 // unions being lowered a specific way etc.
1462 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1463 IRType->isIntegerTy(32)) {
1464 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001465
Chris Lattnere2962be2010-07-29 07:30:00 +00001466 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1467 SourceOffset*8+64, getContext()))
1468 return IRType;
1469 }
1470 }
Chris Lattner49382de2010-07-28 22:44:07 +00001471
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001472 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001473 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001474 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001475 if (IROffset < SL->getSizeInBytes()) {
1476 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1477 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001478
Chris Lattner0d2656d2010-07-29 17:40:35 +00001479 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1480 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001481 }
Chris Lattner49382de2010-07-28 22:44:07 +00001482 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001483
Chris Lattner021c3a32010-07-29 07:43:55 +00001484 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1485 const llvm::Type *EltTy = ATy->getElementType();
1486 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1487 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001488 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1489 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001490 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001491
Chris Lattner49382de2010-07-28 22:44:07 +00001492 // Okay, we don't have any better idea of what to pass, so we pass this in an
1493 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001494 unsigned TySizeInBytes =
1495 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001496
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001497 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001498
Chris Lattner49382de2010-07-28 22:44:07 +00001499 // It is always safe to classify this as an integer type up to i64 that
1500 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001501 return llvm::IntegerType::get(getVMContext(),
1502 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001503}
1504
Chris Lattner66e7b682010-09-01 00:50:20 +00001505
1506/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1507/// be used as elements of a two register pair to pass or return, return a
1508/// first class aggregate to represent them. For example, if the low part of
1509/// a by-value argument should be passed as i32* and the high part as float,
1510/// return {i32*, float}.
1511static const llvm::Type *
1512GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1513 const llvm::TargetData &TD) {
1514 // In order to correctly satisfy the ABI, we need to the high part to start
1515 // at offset 8. If the high and low parts we inferred are both 4-byte types
1516 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1517 // the second element at offset 8. Check for this:
1518 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1519 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1520 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1521 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001522
Chris Lattner66e7b682010-09-01 00:50:20 +00001523 // To handle this, we have to increase the size of the low part so that the
1524 // second element will start at an 8 byte offset. We can't increase the size
1525 // of the second element because it might make us access off the end of the
1526 // struct.
1527 if (HiStart != 8) {
1528 // There are only two sorts of types the ABI generation code can produce for
1529 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1530 // Promote these to a larger type.
1531 if (Lo->isFloatTy())
1532 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1533 else {
1534 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1535 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1536 }
1537 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001538
1539 const llvm::StructType *Result =
Chris Lattner66e7b682010-09-01 00:50:20 +00001540 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001541
1542
Chris Lattner66e7b682010-09-01 00:50:20 +00001543 // Verify that the second element is at an 8-byte offset.
1544 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1545 "Invalid x86-64 argument pair!");
1546 return Result;
1547}
1548
Chris Lattner519f68c2010-07-28 23:06:14 +00001549ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001550classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001551 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1552 // classification algorithm.
1553 X86_64ABIInfo::Class Lo, Hi;
1554 classify(RetTy, 0, Lo, Hi);
1555
1556 // Check some invariants.
1557 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001558 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1559
1560 const llvm::Type *ResType = 0;
1561 switch (Lo) {
1562 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001563 if (Hi == NoClass)
1564 return ABIArgInfo::getIgnore();
1565 // If the low part is just padding, it takes no register, leave ResType
1566 // null.
1567 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1568 "Unknown missing lo part");
1569 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001570
1571 case SSEUp:
1572 case X87Up:
1573 assert(0 && "Invalid classification for lo word.");
1574
1575 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1576 // hidden argument.
1577 case Memory:
1578 return getIndirectReturnResult(RetTy);
1579
1580 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1581 // available register of the sequence %rax, %rdx is used.
1582 case Integer:
Chris Lattner0d2656d2010-07-29 17:40:35 +00001583 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1584 RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001585
Chris Lattnereb518b42010-07-29 21:42:50 +00001586 // If we have a sign or zero extended integer, make sure to return Extend
1587 // so that the parameter gets the right LLVM IR attributes.
1588 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1589 // Treat an enum type as its underlying type.
1590 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1591 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001592
Chris Lattnereb518b42010-07-29 21:42:50 +00001593 if (RetTy->isIntegralOrEnumerationType() &&
1594 RetTy->isPromotableIntegerType())
1595 return ABIArgInfo::getExtend();
1596 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001597 break;
1598
1599 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1600 // available SSE register of the sequence %xmm0, %xmm1 is used.
1601 case SSE:
Chris Lattnerf47c9442010-07-29 18:13:09 +00001602 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001603 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001604
1605 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1606 // returned on the X87 stack in %st0 as 80-bit x87 number.
1607 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001608 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001609 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001610
1611 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1612 // part of the value is returned in %st0 and the imaginary part in
1613 // %st1.
1614 case ComplexX87:
1615 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001616 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001617 llvm::Type::getX86_FP80Ty(getVMContext()),
1618 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001619 NULL);
1620 break;
1621 }
1622
Chris Lattner3db4dde2010-09-01 00:20:33 +00001623 const llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001624 switch (Hi) {
1625 // Memory was handled previously and X87 should
1626 // never occur as a hi class.
1627 case Memory:
1628 case X87:
1629 assert(0 && "Invalid classification for hi word.");
1630
1631 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001632 case NoClass:
1633 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001634
Chris Lattner3db4dde2010-09-01 00:20:33 +00001635 case Integer:
1636 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1637 8, RetTy, 8);
1638 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1639 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001640 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001641 case SSE:
1642 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1643 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1644 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001645 break;
1646
1647 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1648 // is passed in the upper half of the last used SSE register.
1649 //
1650 // SSEUP should always be preceeded by SSE, just widen.
1651 case SSEUp:
1652 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner0f408f52010-07-29 04:56:46 +00001653 ResType = Get16ByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001654 break;
1655
1656 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1657 // returned together with the previous X87 value in %st0.
1658 case X87Up:
1659 // If X87Up is preceeded by X87, we don't need to do
1660 // anything. However, in some cases with unions it may not be
1661 // preceeded by X87. In such situations we follow gcc and pass the
1662 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001663 if (Lo != X87) {
Chris Lattner3db4dde2010-09-01 00:20:33 +00001664 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1665 8, RetTy, 8);
1666 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1667 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001668 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001669 break;
1670 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001671
Chris Lattner3db4dde2010-09-01 00:20:33 +00001672 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001673 // known to pass in the high eightbyte of the result. We do this by forming a
1674 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001675 if (HighPart)
1676 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001677
Chris Lattnereb518b42010-07-29 21:42:50 +00001678 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001679}
1680
Chris Lattnera3c109b2010-07-29 02:16:43 +00001681ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001682 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001683 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001684 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001685
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001686 // Check some invariants.
1687 // FIXME: Enforce these by construction.
1688 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001689 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1690
1691 neededInt = 0;
1692 neededSSE = 0;
1693 const llvm::Type *ResType = 0;
1694 switch (Lo) {
1695 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001696 if (Hi == NoClass)
1697 return ABIArgInfo::getIgnore();
1698 // If the low part is just padding, it takes no register, leave ResType
1699 // null.
1700 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1701 "Unknown missing lo part");
1702 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001703
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001704 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1705 // on the stack.
1706 case Memory:
1707
1708 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1709 // COMPLEX_X87, it is passed in memory.
1710 case X87:
1711 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001712 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001713
1714 case SSEUp:
1715 case X87Up:
1716 assert(0 && "Invalid classification for lo word.");
1717
1718 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1719 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1720 // and %r9 is used.
1721 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001722 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001723
Chris Lattner49382de2010-07-28 22:44:07 +00001724 // Pick an 8-byte type based on the preferred type.
Chris Lattner0d2656d2010-07-29 17:40:35 +00001725 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001726
1727 // If we have a sign or zero extended integer, make sure to return Extend
1728 // so that the parameter gets the right LLVM IR attributes.
1729 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1730 // Treat an enum type as its underlying type.
1731 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1732 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001733
Chris Lattnereb518b42010-07-29 21:42:50 +00001734 if (Ty->isIntegralOrEnumerationType() &&
1735 Ty->isPromotableIntegerType())
1736 return ABIArgInfo::getExtend();
1737 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001738
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001739 break;
1740
1741 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1742 // available SSE register is used, the registers are taken in the
1743 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001744 case SSE: {
1745 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001746 if (Hi != NoClass || !UseX86_MMXType(IRType))
Bill Wendlingbb465d72010-10-18 03:41:31 +00001747 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001748 else
Bill Wendlingbb465d72010-10-18 03:41:31 +00001749 // This is an MMX type. Treat it as such.
1750 ResType = llvm::Type::getX86_MMXTy(getVMContext());
Bill Wendlingbb465d72010-10-18 03:41:31 +00001751
Bill Wendling99aaae82010-10-18 23:51:38 +00001752 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001753 break;
1754 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001755 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001756
Chris Lattner645406a2010-09-01 00:24:35 +00001757 const llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001758 switch (Hi) {
1759 // Memory was handled previously, ComplexX87 and X87 should
1760 // never occur as hi classes, and X87Up must be preceed by X87,
1761 // which is passed in memory.
1762 case Memory:
1763 case X87:
1764 case ComplexX87:
1765 assert(0 && "Invalid classification for hi word.");
1766 break;
1767
1768 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001769
Chris Lattner645406a2010-09-01 00:24:35 +00001770 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001771 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001772 // Pick an 8-byte type based on the preferred type.
Chris Lattner645406a2010-09-01 00:24:35 +00001773 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001774
Chris Lattner645406a2010-09-01 00:24:35 +00001775 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1776 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001777 break;
1778
1779 // X87Up generally doesn't occur here (long double is passed in
1780 // memory), except in situations involving unions.
1781 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001782 case SSE:
1783 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001784
Chris Lattner645406a2010-09-01 00:24:35 +00001785 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1786 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001787
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001788 ++neededSSE;
1789 break;
1790
1791 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1792 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001793 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001794 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001795 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner0f408f52010-07-29 04:56:46 +00001796 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001797 break;
1798 }
1799
Chris Lattner645406a2010-09-01 00:24:35 +00001800 // If a high part was specified, merge it together with the low part. It is
1801 // known to pass in the high eightbyte of the result. We do this by forming a
1802 // first class struct aggregate with the high and low part: {low, high}
1803 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001804 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001805
Chris Lattnereb518b42010-07-29 21:42:50 +00001806 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001807}
1808
Chris Lattneree5dcd02010-07-29 02:31:05 +00001809void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001810
Chris Lattnera3c109b2010-07-29 02:16:43 +00001811 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001812
1813 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001814 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001815
1816 // If the return value is indirect, then the hidden argument is consuming one
1817 // integer register.
1818 if (FI.getReturnInfo().isIndirect())
1819 --freeIntRegs;
1820
1821 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1822 // get assigned (in left-to-right order) for passing as follows...
1823 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1824 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001825 unsigned neededInt, neededSSE;
1826 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001827
1828 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1829 // eightbyte of an argument, the whole argument is passed on the
1830 // stack. If registers have already been assigned for some
1831 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001832 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001833 freeIntRegs -= neededInt;
1834 freeSSERegs -= neededSSE;
1835 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001836 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001837 }
1838 }
1839}
1840
1841static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1842 QualType Ty,
1843 CodeGenFunction &CGF) {
1844 llvm::Value *overflow_arg_area_p =
1845 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1846 llvm::Value *overflow_arg_area =
1847 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1848
1849 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1850 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1851 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1852 if (Align > 8) {
1853 // Note that we follow the ABI & gcc here, even though the type
1854 // could in theory have an alignment greater than 16. This case
1855 // shouldn't ever matter in practice.
1856
1857 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001858 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001859 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001860 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1861 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001862 CGF.Int64Ty);
1863 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001864 overflow_arg_area =
1865 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1866 overflow_arg_area->getType(),
1867 "overflow_arg_area.align");
1868 }
1869
1870 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1871 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1872 llvm::Value *Res =
1873 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001874 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001875
1876 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1877 // l->overflow_arg_area + sizeof(type).
1878 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1879 // an 8 byte boundary.
1880
1881 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001882 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001883 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001884 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1885 "overflow_arg_area.next");
1886 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1887
1888 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1889 return Res;
1890}
1891
1892llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1893 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001894 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001896 // Assume that va_list type is correct; should be pointer to LLVM type:
1897 // struct {
1898 // i32 gp_offset;
1899 // i32 fp_offset;
1900 // i8* overflow_arg_area;
1901 // i8* reg_save_area;
1902 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00001903 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001904
Chris Lattnera14db752010-03-11 18:19:55 +00001905 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001906 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001907
1908 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1909 // in the registers. If not go to step 7.
1910 if (!neededInt && !neededSSE)
1911 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1912
1913 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1914 // general purpose registers needed to pass type and num_fp to hold
1915 // the number of floating point registers needed.
1916
1917 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1918 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1919 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1920 //
1921 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1922 // register save space).
1923
1924 llvm::Value *InRegs = 0;
1925 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1926 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1927 if (neededInt) {
1928 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1929 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001930 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1931 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001932 }
1933
1934 if (neededSSE) {
1935 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1936 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1937 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001938 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1939 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001940 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1941 }
1942
1943 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1944 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1945 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1946 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1947
1948 // Emit code to load the value if it was passed in registers.
1949
1950 CGF.EmitBlock(InRegBlock);
1951
1952 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1953 // an offset of l->gp_offset and/or l->fp_offset. This may require
1954 // copying to a temporary location in case the parameter is passed
1955 // in different register classes or requires an alignment greater
1956 // than 8 for general purpose registers and 16 for XMM registers.
1957 //
1958 // FIXME: This really results in shameful code when we end up needing to
1959 // collect arguments from different places; often what should result in a
1960 // simple assembling of a structure from scattered addresses has many more
1961 // loads than necessary. Can we clean this up?
1962 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1963 llvm::Value *RegAddr =
1964 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1965 "reg_save_area");
1966 if (neededInt && neededSSE) {
1967 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00001968 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001969 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1970 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1971 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1972 const llvm::Type *TyLo = ST->getElementType(0);
1973 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00001974 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001975 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001976 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1977 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001978 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1979 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001980 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1981 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001982 llvm::Value *V =
1983 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1984 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1985 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1986 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1987
Owen Andersona1cf15f2009-07-14 23:10:40 +00001988 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001989 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001990 } else if (neededInt) {
1991 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1992 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001993 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001994 } else if (neededSSE == 1) {
1995 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1996 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1997 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001998 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001999 assert(neededSSE == 2 && "Invalid number of needed registers!");
2000 // SSE registers are spaced 16 bytes apart in the register save
2001 // area, we need to collect the two eightbytes together.
2002 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002003 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002004 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
2005 const llvm::Type *DblPtrTy =
2006 llvm::PointerType::getUnqual(DoubleTy);
2007 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
2008 DoubleTy, NULL);
2009 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2010 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2011 DblPtrTy));
2012 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2013 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2014 DblPtrTy));
2015 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2016 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2017 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002018 }
2019
2020 // AMD64-ABI 3.5.7p5: Step 5. Set:
2021 // l->gp_offset = l->gp_offset + num_gp * 8
2022 // l->fp_offset = l->fp_offset + num_fp * 16.
2023 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002024 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002025 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2026 gp_offset_p);
2027 }
2028 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002029 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002030 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2031 fp_offset_p);
2032 }
2033 CGF.EmitBranch(ContBlock);
2034
2035 // Emit code to load the value if it was passed in memory.
2036
2037 CGF.EmitBlock(InMemBlock);
2038 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2039
2040 // Return the appropriate result.
2041
2042 CGF.EmitBlock(ContBlock);
2043 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2044 "vaarg.addr");
2045 ResAddr->reserveOperandSpace(2);
2046 ResAddr->addIncoming(RegAddr, InRegBlock);
2047 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002048 return ResAddr;
2049}
2050
Chris Lattnerf13721d2010-08-31 16:44:54 +00002051llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2052 CodeGenFunction &CGF) const {
2053 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2054 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002055
Chris Lattnerf13721d2010-08-31 16:44:54 +00002056 CGBuilderTy &Builder = CGF.Builder;
2057 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2058 "ap");
2059 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2060 llvm::Type *PTy =
2061 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2062 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2063
2064 uint64_t Offset =
2065 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2066 llvm::Value *NextAddr =
2067 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2068 "ap.next");
2069 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2070
2071 return AddrTyped;
2072}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002073
John McCallec853ba2010-03-11 00:10:12 +00002074// PowerPC-32
2075
2076namespace {
2077class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2078public:
Chris Lattnerea044322010-07-29 02:01:43 +00002079 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002080
John McCallec853ba2010-03-11 00:10:12 +00002081 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2082 // This is recovered from gcc output.
2083 return 1; // r1 is the dedicated stack pointer
2084 }
2085
2086 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002087 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002088};
2089
2090}
2091
2092bool
2093PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2094 llvm::Value *Address) const {
2095 // This is calculated from the LLVM and GCC tables and verified
2096 // against gcc output. AFAIK all ABIs use the same encoding.
2097
2098 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2099 llvm::LLVMContext &Context = CGF.getLLVMContext();
2100
2101 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2102 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2103 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2104 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2105
2106 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002107 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002108
2109 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002110 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002111
2112 // 64-76 are various 4-byte special-purpose registers:
2113 // 64: mq
2114 // 65: lr
2115 // 66: ctr
2116 // 67: ap
2117 // 68-75 cr0-7
2118 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002119 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002120
2121 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002122 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002123
2124 // 109: vrsave
2125 // 110: vscr
2126 // 111: spe_acc
2127 // 112: spefscr
2128 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002129 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002130
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002131 return false;
John McCallec853ba2010-03-11 00:10:12 +00002132}
2133
2134
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002135//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002136// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002137//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002138
2139namespace {
2140
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002141class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002142public:
2143 enum ABIKind {
2144 APCS = 0,
2145 AAPCS = 1,
2146 AAPCS_VFP
2147 };
2148
2149private:
2150 ABIKind Kind;
2151
2152public:
Chris Lattnerea044322010-07-29 02:01:43 +00002153 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002154
2155private:
2156 ABIKind getABIKind() const { return Kind; }
2157
Chris Lattnera3c109b2010-07-29 02:16:43 +00002158 ABIArgInfo classifyReturnType(QualType RetTy) const;
2159 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002160
Chris Lattneree5dcd02010-07-29 02:31:05 +00002161 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002162
2163 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2164 CodeGenFunction &CGF) const;
2165};
2166
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002167class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2168public:
Chris Lattnerea044322010-07-29 02:01:43 +00002169 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2170 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002171
2172 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2173 return 13;
2174 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002175};
2176
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002177}
2178
Chris Lattneree5dcd02010-07-29 02:31:05 +00002179void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002180 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002182 it != ie; ++it)
2183 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002184
Chris Lattnera3c109b2010-07-29 02:16:43 +00002185 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindola25117ab2010-06-16 16:13:39 +00002186 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002187 if (Triple.getEnvironmentName() == "gnueabi" ||
2188 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002189 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002190 else
2191 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002192
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002193 switch (getABIKind()) {
2194 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002195 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2196 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002197 break;
2198
2199 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002200 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2201 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002202 break;
2203
2204 case AAPCS_VFP:
2205 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2206 break;
2207 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208}
2209
Chris Lattnera3c109b2010-07-29 02:16:43 +00002210ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002211 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002212 // Treat an enum type as its underlying type.
2213 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2214 Ty = EnumTy->getDecl()->getIntegerType();
2215
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002216 return (Ty->isPromotableIntegerType() ?
2217 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002218 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002219
Daniel Dunbar42025572009-09-14 21:54:03 +00002220 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002221 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002222 return ABIArgInfo::getIgnore();
2223
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002224 // Structures with either a non-trivial destructor or a non-trivial
2225 // copy constructor are always indirect.
2226 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2227 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2228
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002229 // NEON vectors are implemented as (theoretically) opaque structures wrapping
2230 // the underlying vector type. We trust the backend to pass the underlying
2231 // vectors appropriately, so we can unwrap the structs which generally will
2232 // lead to much cleaner IR.
2233 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) {
2234 if (SeltTy->isVectorType())
2235 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
2236 }
2237
2238 // Otherwise, pass by coercing to a structure of the appropriate size.
2239 //
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002240 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2241 // backend doesn't support byval.
2242 // FIXME: This doesn't handle alignment > 64 bits.
2243 const llvm::Type* ElemTy;
2244 unsigned SizeRegs;
Chris Lattnera3c109b2010-07-29 02:16:43 +00002245 if (getContext().getTypeAlign(Ty) > 32) {
2246 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2247 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002248 } else {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002249 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2250 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002251 }
2252 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00002253 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattnera3c109b2010-07-29 02:16:43 +00002254 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2255 true);
Chris Lattner800588f2010-07-29 06:26:06 +00002256 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002257}
2258
Chris Lattnera3c109b2010-07-29 02:16:43 +00002259static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002260 llvm::LLVMContext &VMContext) {
2261 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2262 // is called integer-like if its size is less than or equal to one word, and
2263 // the offset of each of its addressable sub-fields is zero.
2264
2265 uint64_t Size = Context.getTypeSize(Ty);
2266
2267 // Check that the type fits in a word.
2268 if (Size > 32)
2269 return false;
2270
2271 // FIXME: Handle vector types!
2272 if (Ty->isVectorType())
2273 return false;
2274
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002275 // Float types are never treated as "integer like".
2276 if (Ty->isRealFloatingType())
2277 return false;
2278
Daniel Dunbar98303b92009-09-13 08:03:58 +00002279 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002280 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002281 return true;
2282
Daniel Dunbar45815812010-02-01 23:31:26 +00002283 // Small complex integer types are "integer like".
2284 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2285 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002286
2287 // Single element and zero sized arrays should be allowed, by the definition
2288 // above, but they are not.
2289
2290 // Otherwise, it must be a record type.
2291 const RecordType *RT = Ty->getAs<RecordType>();
2292 if (!RT) return false;
2293
2294 // Ignore records with flexible arrays.
2295 const RecordDecl *RD = RT->getDecl();
2296 if (RD->hasFlexibleArrayMember())
2297 return false;
2298
2299 // Check that all sub-fields are at offset 0, and are themselves "integer
2300 // like".
2301 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2302
2303 bool HadField = false;
2304 unsigned idx = 0;
2305 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2306 i != e; ++i, ++idx) {
2307 const FieldDecl *FD = *i;
2308
Daniel Dunbar679855a2010-01-29 03:22:29 +00002309 // Bit-fields are not addressable, we only need to verify they are "integer
2310 // like". We still have to disallow a subsequent non-bitfield, for example:
2311 // struct { int : 0; int x }
2312 // is non-integer like according to gcc.
2313 if (FD->isBitField()) {
2314 if (!RD->isUnion())
2315 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002316
Daniel Dunbar679855a2010-01-29 03:22:29 +00002317 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2318 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002319
Daniel Dunbar679855a2010-01-29 03:22:29 +00002320 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002321 }
2322
Daniel Dunbar679855a2010-01-29 03:22:29 +00002323 // Check if this field is at offset 0.
2324 if (Layout.getFieldOffset(idx) != 0)
2325 return false;
2326
Daniel Dunbar98303b92009-09-13 08:03:58 +00002327 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2328 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002329
Daniel Dunbar679855a2010-01-29 03:22:29 +00002330 // Only allow at most one field in a structure. This doesn't match the
2331 // wording above, but follows gcc in situations with a field following an
2332 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002333 if (!RD->isUnion()) {
2334 if (HadField)
2335 return false;
2336
2337 HadField = true;
2338 }
2339 }
2340
2341 return true;
2342}
2343
Chris Lattnera3c109b2010-07-29 02:16:43 +00002344ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002345 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002346 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002347
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002348 // Large vector types should be returned via memory.
2349 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2350 return ABIArgInfo::getIndirect(0);
2351
John McCalld608cdb2010-08-22 10:59:02 +00002352 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002353 // Treat an enum type as its underlying type.
2354 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2355 RetTy = EnumTy->getDecl()->getIntegerType();
2356
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002357 return (RetTy->isPromotableIntegerType() ?
2358 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002359 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002360
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002361 // Structures with either a non-trivial destructor or a non-trivial
2362 // copy constructor are always indirect.
2363 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2364 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2365
Daniel Dunbar98303b92009-09-13 08:03:58 +00002366 // Are we following APCS?
2367 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002368 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002369 return ABIArgInfo::getIgnore();
2370
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002371 // Complex types are all returned as packed integers.
2372 //
2373 // FIXME: Consider using 2 x vector types if the back end handles them
2374 // correctly.
2375 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002376 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002377 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002378
Daniel Dunbar98303b92009-09-13 08:03:58 +00002379 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002380 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002381 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002382 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002383 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002384 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002385 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002386 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2387 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002388 }
2389
2390 // Otherwise return in memory.
2391 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002392 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002393
2394 // Otherwise this is an AAPCS variant.
2395
Chris Lattnera3c109b2010-07-29 02:16:43 +00002396 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002397 return ABIArgInfo::getIgnore();
2398
Daniel Dunbar98303b92009-09-13 08:03:58 +00002399 // Aggregates <= 4 bytes are returned in r0; other aggregates
2400 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002401 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002402 if (Size <= 32) {
2403 // Return in the smallest viable integer type.
2404 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002405 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002406 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002407 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2408 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002409 }
2410
Daniel Dunbar98303b92009-09-13 08:03:58 +00002411 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002412}
2413
2414llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002415 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002416 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002417 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002418 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002419
2420 CGBuilderTy &Builder = CGF.Builder;
2421 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2422 "ap");
2423 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2424 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002425 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002426 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2427
2428 uint64_t Offset =
2429 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2430 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002431 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002432 "ap.next");
2433 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2434
2435 return AddrTyped;
2436}
2437
Chris Lattnera3c109b2010-07-29 02:16:43 +00002438ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2439 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002440 return ABIArgInfo::getIgnore();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002441
John McCalld608cdb2010-08-22 10:59:02 +00002442 if (isAggregateTypeForABI(RetTy))
Chris Lattnera3c109b2010-07-29 02:16:43 +00002443 return ABIArgInfo::getIndirect(0);
2444
2445 // Treat an enum type as its underlying type.
2446 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2447 RetTy = EnumTy->getDecl()->getIntegerType();
2448
2449 return (RetTy->isPromotableIntegerType() ?
2450 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002451}
2452
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002453//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002454// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002455//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002456
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002457namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002458
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002459class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002460public:
2461 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2462
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002463 bool isPromotableIntegerType(QualType Ty) const;
2464
Chris Lattnera3c109b2010-07-29 02:16:43 +00002465 ABIArgInfo classifyReturnType(QualType RetTy) const;
2466 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002467
Chris Lattneree5dcd02010-07-29 02:31:05 +00002468 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002469 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002470 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2471 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002472 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002473 }
2474
2475 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2476 CodeGenFunction &CGF) const;
2477};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002478
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002479class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2480public:
Chris Lattnerea044322010-07-29 02:01:43 +00002481 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2482 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002483};
2484
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002485}
2486
2487bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2488 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002489 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002490 switch (BT->getKind()) {
2491 case BuiltinType::Bool:
2492 case BuiltinType::Char_S:
2493 case BuiltinType::Char_U:
2494 case BuiltinType::SChar:
2495 case BuiltinType::UChar:
2496 case BuiltinType::Short:
2497 case BuiltinType::UShort:
2498 case BuiltinType::Int:
2499 case BuiltinType::UInt:
2500 return true;
2501 default:
2502 return false;
2503 }
2504 return false;
2505}
2506
2507llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2508 CodeGenFunction &CGF) const {
2509 // FIXME: Implement
2510 return 0;
2511}
2512
2513
Chris Lattnera3c109b2010-07-29 02:16:43 +00002514ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2515 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002516 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002517 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002518 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002519
2520 return (isPromotableIntegerType(RetTy) ?
2521 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002522}
2523
Chris Lattnera3c109b2010-07-29 02:16:43 +00002524ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002525 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002526 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002527
2528 return (isPromotableIntegerType(Ty) ?
2529 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002530}
2531
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002532//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002533// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002534//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002535
2536namespace {
2537
2538class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2539public:
Chris Lattnerea044322010-07-29 02:01:43 +00002540 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2541 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002542 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2543 CodeGen::CodeGenModule &M) const;
2544};
2545
2546}
2547
2548void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2549 llvm::GlobalValue *GV,
2550 CodeGen::CodeGenModule &M) const {
2551 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2552 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2553 // Handle 'interrupt' attribute:
2554 llvm::Function *F = cast<llvm::Function>(GV);
2555
2556 // Step 1: Set ISR calling convention.
2557 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2558
2559 // Step 2: Add attributes goodness.
2560 F->addFnAttr(llvm::Attribute::NoInline);
2561
2562 // Step 3: Emit ISR vector alias.
2563 unsigned Num = attr->getNumber() + 0xffe0;
2564 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Benjamin Kramer77d66052010-11-12 15:42:18 +00002565 "vector_" + llvm::Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002566 GV, &M.getModule());
2567 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002568 }
2569}
2570
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002571//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002572// MIPS ABI Implementation. This works for both little-endian and
2573// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002574//===----------------------------------------------------------------------===//
2575
John McCallaeeb7012010-05-27 06:19:26 +00002576namespace {
2577class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2578public:
Chris Lattnerea044322010-07-29 02:01:43 +00002579 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2580 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002581
2582 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2583 return 29;
2584 }
2585
2586 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002587 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002588};
2589}
2590
2591bool
2592MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2593 llvm::Value *Address) const {
2594 // This information comes from gcc's implementation, which seems to
2595 // as canonical as it gets.
2596
2597 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2598 llvm::LLVMContext &Context = CGF.getLLVMContext();
2599
2600 // Everything on MIPS is 4 bytes. Double-precision FP registers
2601 // are aliased to pairs of single-precision FP registers.
2602 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2603 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2604
2605 // 0-31 are the general purpose registers, $0 - $31.
2606 // 32-63 are the floating-point registers, $f0 - $f31.
2607 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2608 // 66 is the (notional, I think) register for signal-handler return.
2609 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2610
2611 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2612 // They are one bit wide and ignored here.
2613
2614 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2615 // (coprocessor 1 is the FP unit)
2616 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2617 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2618 // 176-181 are the DSP accumulator registers.
2619 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2620
2621 return false;
2622}
2623
2624
Chris Lattnerea044322010-07-29 02:01:43 +00002625const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002626 if (TheTargetCodeGenInfo)
2627 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002628
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002629 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2630 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002631
Chris Lattner9c254f02010-06-29 06:01:59 +00002632 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002633 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002634 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002635 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002636
John McCallaeeb7012010-05-27 06:19:26 +00002637 case llvm::Triple::mips:
2638 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002639 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002640
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002641 case llvm::Triple::arm:
2642 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002643 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002644 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002645 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002646 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002647
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002648 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002649 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002650
John McCallec853ba2010-03-11 00:10:12 +00002651 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00002652 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00002653
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002654 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00002655 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002656
2657 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00002658 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002659
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002660 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002661 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002662 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002663 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002664 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002665 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002666 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002667 case llvm::Triple::AuroraUX:
2668 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002669 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002670 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002671 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002672 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002673
2674 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002675 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002676 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002677 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002678
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002679 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00002680 switch (Triple.getOS()) {
2681 case llvm::Triple::Win32:
2682 case llvm::Triple::MinGW64:
2683 case llvm::Triple::Cygwin:
2684 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2685 default:
2686 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2687 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002688 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002689}