blob: ab6f7c7dd0c858fc8ae1076e89dd7508dce44064 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
78 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000079 break;
80 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000081 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000082 break;
83 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000084 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000085}
86
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000087TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
88
Daniel Dunbar626f1d82009-09-13 08:03:58 +000089static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +000093static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
94 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000095 if (FD->isUnnamedBitfield())
96 return true;
97
98 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000099
Daniel Dunbar626f1d82009-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 Dunbarcd20ce12010-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 Dunbar626f1d82009-09-13 08:03:58 +0000116 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +0000122static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000123 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Dunbarcd20ce12010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000137 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
138 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000139 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000140 return false;
141 return true;
142}
143
Anders Carlsson20759ad2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000150
Anders Carlsson20759ad2009-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 Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000183
Daniel Dunbar12ebb472010-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 Dunbar12ebb472010-05-11 21:15:36 +0000188 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000189 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
206 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000207 const FieldDecl *FD = *i;
208 QualType FT = FD->getType();
209
210 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000211 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 McCalla1dee5302010-08-22 10:59:02 +0000226 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-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 Dunbar6b45b672010-05-14 03:40:53 +0000239 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000240 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
241 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-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 Dunbar11c08c82009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +0000293public:
294 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000295
Chris Lattner458b2aa2010-07-29 02:16:43 +0000296 ABIArgInfo classifyReturnType(QualType RetTy) const;
297 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000298
Chris Lattner22326a12010-07-29 02:31:05 +0000299 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
302 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000303 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000304 }
305
306 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
307 CodeGenFunction &CGF) const;
308};
309
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000310class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
311public:
Chris Lattner2b037972010-07-29 02:01:43 +0000312 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
313 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-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 Lattner458b2aa2010-07-29 02:16:43 +0000321ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000322 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000323 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000324
Chris Lattner9723d6c2010-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 Gregora71cc152010-02-02 20:10:50 +0000328
Chris Lattner9723d6c2010-03-11 18:19:55 +0000329 return (Ty->isPromotableIntegerType() ?
330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000331}
332
Chris Lattner0cf24192010-06-28 20:05:43 +0000333//===----------------------------------------------------------------------===//
334// X86-32 ABI Implementation
335//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000336
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000337/// X86_32ABIInfo - The X86-32 ABI information.
338class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000339 static const unsigned MinABIStackAlignInBytes = 4;
340
David Chisnallde3a0692009-08-17 23:08:21 +0000341 bool IsDarwinVectorABI;
342 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000343
344 static bool isRegisterSize(unsigned Size) {
345 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
346 }
347
348 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
349
Daniel Dunbar557893d2010-04-21 19:10:51 +0000350 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
351 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000352 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000353
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000354 /// \brief Return the alignment to use for the given type on the stack.
355 unsigned getTypeStackAlignInBytes(QualType Ty) const;
356
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000357public:
Chris Lattner2b037972010-07-29 02:01:43 +0000358
Chris Lattner458b2aa2010-07-29 02:16:43 +0000359 ABIArgInfo classifyReturnType(QualType RetTy) const;
360 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000361
Chris Lattner22326a12010-07-29 02:31:05 +0000362 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000363 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000364 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
365 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000366 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000367 }
368
369 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
370 CodeGenFunction &CGF) const;
371
Chris Lattner2b037972010-07-29 02:01:43 +0000372 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
373 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000374};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000375
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000376class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
377public:
Chris Lattner2b037972010-07-29 02:01:43 +0000378 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
379 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000380
381 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
382 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000383
384 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
385 // Darwin uses different dwarf register numbers for EH.
386 if (CGM.isTargetDarwin()) return 5;
387
388 return 4;
389 }
390
391 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
392 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000393};
394
395}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000396
397/// shouldReturnTypeInRegister - Determine if the given type should be
398/// passed in a register (for the Darwin ABI).
399bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
400 ASTContext &Context) {
401 uint64_t Size = Context.getTypeSize(Ty);
402
403 // Type must be register sized.
404 if (!isRegisterSize(Size))
405 return false;
406
407 if (Ty->isVectorType()) {
408 // 64- and 128- bit vectors inside structures are not returned in
409 // registers.
410 if (Size == 64 || Size == 128)
411 return false;
412
413 return true;
414 }
415
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000416 // If this is a builtin, pointer, enum, complex type, member pointer, or
417 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000418 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000419 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000420 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000421 return true;
422
423 // Arrays are treated like records.
424 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
425 return shouldReturnTypeInRegister(AT->getElementType(), Context);
426
427 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000428 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000429 if (!RT) return false;
430
Anders Carlsson40446e82010-01-27 03:25:19 +0000431 // FIXME: Traverse bases here too.
432
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000433 // Structure types are passed in register if all fields would be
434 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000435 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
436 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000437 const FieldDecl *FD = *i;
438
439 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000440 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000441 continue;
442
443 // Check fields recursively.
444 if (!shouldReturnTypeInRegister(FD->getType(), Context))
445 return false;
446 }
447
448 return true;
449}
450
Chris Lattner458b2aa2010-07-29 02:16:43 +0000451ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
452 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000453 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000454
Chris Lattner458b2aa2010-07-29 02:16:43 +0000455 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000456 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000457 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000458 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000459
460 // 128-bit vectors are a special case; they are returned in
461 // registers and we need to make sure to pick a type the LLVM
462 // backend will like.
463 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000464 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000465 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000466
467 // Always return in register if it fits in a general purpose
468 // register, or if it is 64 bits and has a single element.
469 if ((Size == 8 || Size == 16 || Size == 32) ||
470 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000471 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000472 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000473
474 return ABIArgInfo::getIndirect(0);
475 }
476
477 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000478 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000479
John McCalla1dee5302010-08-22 10:59:02 +0000480 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000481 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000482 // Structures with either a non-trivial destructor or a non-trivial
483 // copy constructor are always indirect.
484 if (hasNonTrivialDestructorOrCopyConstructor(RT))
485 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000486
Anders Carlsson5789c492009-10-20 22:07:59 +0000487 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000488 if (RT->getDecl()->hasFlexibleArrayMember())
489 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000490 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000491
David Chisnallde3a0692009-08-17 23:08:21 +0000492 // If specified, structs and unions are always indirect.
493 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000494 return ABIArgInfo::getIndirect(0);
495
496 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000497 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000498 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000499 if (BT->isIntegerType()) {
500 // We need to use the size of the structure, padding
501 // bit-fields can adjust that to be larger than the single
502 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000503 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000504 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000505 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
506 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000507
Chris Lattner458b2aa2010-07-29 02:16:43 +0000508 if (BT->getKind() == BuiltinType::Float) {
509 assert(getContext().getTypeSize(RetTy) ==
510 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000511 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000512 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000513 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000514
Chris Lattner458b2aa2010-07-29 02:16:43 +0000515 if (BT->getKind() == BuiltinType::Double) {
516 assert(getContext().getTypeSize(RetTy) ==
517 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000518 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000519 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000520 }
521 } else if (SeltTy->isPointerType()) {
522 // FIXME: It would be really nice if this could come out as the proper
523 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000524 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000525 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000526 } else if (SeltTy->isVectorType()) {
527 // 64- and 128-bit vectors are never returned in a
528 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000529 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000530 if (Size == 64 || Size == 128)
531 return ABIArgInfo::getIndirect(0);
532
Chris Lattner458b2aa2010-07-29 02:16:43 +0000533 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000534 }
535 }
536
537 // Small structures which are register sized are generally returned
538 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000539 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
540 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000541 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000542 }
543
544 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000545 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000546
Chris Lattner458b2aa2010-07-29 02:16:43 +0000547 // Treat an enum type as its underlying type.
548 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
549 RetTy = EnumTy->getDecl()->getIntegerType();
550
551 return (RetTy->isPromotableIntegerType() ?
552 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000553}
554
Daniel Dunbared23de32010-09-16 20:42:00 +0000555static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
556 const RecordType *RT = Ty->getAs<RecordType>();
557 if (!RT)
558 return 0;
559 const RecordDecl *RD = RT->getDecl();
560
561 // If this is a C++ record, check the bases first.
562 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
563 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
564 e = CXXRD->bases_end(); i != e; ++i)
565 if (!isRecordWithSSEVectorType(Context, i->getType()))
566 return false;
567
568 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
569 i != e; ++i) {
570 QualType FT = i->getType();
571
572 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
573 return true;
574
575 if (isRecordWithSSEVectorType(Context, FT))
576 return true;
577 }
578
579 return false;
580}
581
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000582unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty) const {
583 // On non-Darwin, the stack type alignment is always 4.
584 if (!IsDarwinVectorABI)
585 return MinABIStackAlignInBytes;
586
587 // Otherwise, if the alignment is less than or equal to 4, use the minimum ABI
588 // alignment.
589 unsigned Align = getContext().getTypeAlign(Ty) / 8;
590 if (Align <= MinABIStackAlignInBytes)
591 return MinABIStackAlignInBytes;
592
Daniel Dunbared23de32010-09-16 20:42:00 +0000593 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
594 if (isRecordWithSSEVectorType(getContext(), Ty))
595 return 16;
596
597 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000598}
599
Chris Lattner458b2aa2010-07-29 02:16:43 +0000600ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000601 if (!ByVal)
602 return ABIArgInfo::getIndirect(0, false);
603
604 // Compute the byval alignment. We trust the back-end to honor the
605 // minimum ABI alignment for byval, to make cleaner IR.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000606 unsigned Align = getTypeStackAlignInBytes(Ty);
607 if (Align > MinABIStackAlignInBytes)
Daniel Dunbar53fac692010-04-21 19:49:55 +0000608 return ABIArgInfo::getIndirect(Align);
609 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000610}
611
Chris Lattner458b2aa2010-07-29 02:16:43 +0000612ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000613 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000614 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000615 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000616 if (const RecordType *RT = Ty->getAs<RecordType>()) {
617 // Structures with either a non-trivial destructor or a non-trivial
618 // copy constructor are always indirect.
619 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000620 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000621
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000622 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000623 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000624 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000625
626 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000627 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000628 return ABIArgInfo::getIgnore();
629
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000630 // Expand small (<= 128-bit) record types when we know that the stack layout
631 // of those arguments will match the struct. This is important because the
632 // LLVM backend isn't smart enough to remove byval, which inhibits many
633 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000634 if (getContext().getTypeSize(Ty) <= 4*32 &&
635 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000636 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000637
Chris Lattner458b2aa2010-07-29 02:16:43 +0000638 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000639 }
640
Chris Lattnerd774ae92010-08-26 20:05:13 +0000641 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000642 // On Darwin, some vectors are passed in memory, we handle this by passing
643 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000644 if (IsDarwinVectorABI) {
645 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000646 if ((Size == 8 || Size == 16 || Size == 32) ||
647 (Size == 64 && VT->getNumElements() == 1))
648 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
649 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000650 }
651
652 return ABIArgInfo::getDirect();
653 }
654
655
Chris Lattner458b2aa2010-07-29 02:16:43 +0000656 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
657 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000658
Chris Lattner458b2aa2010-07-29 02:16:43 +0000659 return (Ty->isPromotableIntegerType() ?
660 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000661}
662
663llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
664 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000665 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000666 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000667
668 CGBuilderTy &Builder = CGF.Builder;
669 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
670 "ap");
671 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
672 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000673 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000674 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
675
676 uint64_t Offset =
677 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
678 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000679 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000680 "ap.next");
681 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
682
683 return AddrTyped;
684}
685
Charles Davis4ea31ab2010-02-13 15:54:06 +0000686void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
687 llvm::GlobalValue *GV,
688 CodeGen::CodeGenModule &CGM) const {
689 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
690 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
691 // Get the LLVM function.
692 llvm::Function *Fn = cast<llvm::Function>(GV);
693
694 // Now add the 'alignstack' attribute with a value of 16.
695 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
696 }
697 }
698}
699
John McCallbeec5a02010-03-06 00:35:14 +0000700bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
701 CodeGen::CodeGenFunction &CGF,
702 llvm::Value *Address) const {
703 CodeGen::CGBuilderTy &Builder = CGF.Builder;
704 llvm::LLVMContext &Context = CGF.getLLVMContext();
705
706 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
707 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000708
John McCallbeec5a02010-03-06 00:35:14 +0000709 // 0-7 are the eight integer registers; the order is different
710 // on Darwin (for EH), but the range is the same.
711 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000712 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000713
714 if (CGF.CGM.isTargetDarwin()) {
715 // 12-16 are st(0..4). Not sure why we stop at 4.
716 // These have size 16, which is sizeof(long double) on
717 // platforms with 8-byte alignment for that type.
718 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000719 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000720
John McCallbeec5a02010-03-06 00:35:14 +0000721 } else {
722 // 9 is %eflags, which doesn't get a size on Darwin for some
723 // reason.
724 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
725
726 // 11-16 are st(0..5). Not sure why we stop at 5.
727 // These have size 12, which is sizeof(long double) on
728 // platforms with 4-byte alignment for that type.
729 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000730 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
731 }
John McCallbeec5a02010-03-06 00:35:14 +0000732
733 return false;
734}
735
Chris Lattner0cf24192010-06-28 20:05:43 +0000736//===----------------------------------------------------------------------===//
737// X86-64 ABI Implementation
738//===----------------------------------------------------------------------===//
739
740
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000741namespace {
742/// X86_64ABIInfo - The X86_64 ABI information.
743class X86_64ABIInfo : public ABIInfo {
744 enum Class {
745 Integer = 0,
746 SSE,
747 SSEUp,
748 X87,
749 X87Up,
750 ComplexX87,
751 NoClass,
752 Memory
753 };
754
755 /// merge - Implement the X86_64 ABI merging algorithm.
756 ///
757 /// Merge an accumulating classification \arg Accum with a field
758 /// classification \arg Field.
759 ///
760 /// \param Accum - The accumulating classification. This should
761 /// always be either NoClass or the result of a previous merge
762 /// call. In addition, this should never be Memory (the caller
763 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000764 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000765
766 /// classify - Determine the x86_64 register classes in which the
767 /// given type T should be passed.
768 ///
769 /// \param Lo - The classification for the parts of the type
770 /// residing in the low word of the containing object.
771 ///
772 /// \param Hi - The classification for the parts of the type
773 /// residing in the high word of the containing object.
774 ///
775 /// \param OffsetBase - The bit offset of this type in the
776 /// containing object. Some parameters are classified different
777 /// depending on whether they straddle an eightbyte boundary.
778 ///
779 /// If a word is unused its result will be NoClass; if a type should
780 /// be passed in Memory then at least the classification of \arg Lo
781 /// will be Memory.
782 ///
783 /// The \arg Lo class will be NoClass iff the argument is ignored.
784 ///
785 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
786 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000787 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000788
Chris Lattner4200fe42010-07-29 04:56:46 +0000789 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000790 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000791 unsigned IROffset, QualType SourceTy,
792 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000793 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
794 unsigned IROffset, QualType SourceTy,
795 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000796
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000797 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000798 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000799 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000800
801 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000802 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000803 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000804
Chris Lattner458b2aa2010-07-29 02:16:43 +0000805 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000806
Chris Lattner029c0f12010-07-29 04:41:05 +0000807 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
808 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000809
810public:
Chris Lattner2b037972010-07-29 02:01:43 +0000811 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000812
Chris Lattner22326a12010-07-29 02:31:05 +0000813 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000814
815 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
816 CodeGenFunction &CGF) const;
817};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000818
Chris Lattner04dc9572010-08-31 16:44:54 +0000819/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
820class WinX86_64ABIInfo : public X86_64ABIInfo {
821public:
822 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
823
824 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
825 CodeGenFunction &CGF) const;
826};
827
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000828class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
829public:
Chris Lattner2b037972010-07-29 02:01:43 +0000830 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
831 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000832
833 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
834 return 7;
835 }
836
837 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
838 llvm::Value *Address) const {
839 CodeGen::CGBuilderTy &Builder = CGF.Builder;
840 llvm::LLVMContext &Context = CGF.getLLVMContext();
841
842 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
843 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000844
John McCall943fae92010-05-27 06:19:26 +0000845 // 0-15 are the 16 integer registers.
846 // 16 is %rip.
847 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000848
849 return false;
850 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000851};
852
Chris Lattner04dc9572010-08-31 16:44:54 +0000853class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
854public:
855 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
856 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
857
858 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
859 return 7;
860 }
861
862 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
863 llvm::Value *Address) const {
864 CodeGen::CGBuilderTy &Builder = CGF.Builder;
865 llvm::LLVMContext &Context = CGF.getLLVMContext();
866
867 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
868 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
869
870 // 0-15 are the 16 integer registers.
871 // 16 is %rip.
872 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
873
874 return false;
875 }
876};
877
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000878}
879
Chris Lattnerd776fb12010-06-28 21:43:59 +0000880X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000881 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
882 // classified recursively so that always two fields are
883 // considered. The resulting class is calculated according to
884 // the classes of the fields in the eightbyte:
885 //
886 // (a) If both classes are equal, this is the resulting class.
887 //
888 // (b) If one of the classes is NO_CLASS, the resulting class is
889 // the other class.
890 //
891 // (c) If one of the classes is MEMORY, the result is the MEMORY
892 // class.
893 //
894 // (d) If one of the classes is INTEGER, the result is the
895 // INTEGER.
896 //
897 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
898 // MEMORY is used as class.
899 //
900 // (f) Otherwise class SSE is used.
901
902 // Accum should never be memory (we should have returned) or
903 // ComplexX87 (because this cannot be passed in a structure).
904 assert((Accum != Memory && Accum != ComplexX87) &&
905 "Invalid accumulated classification during merge.");
906 if (Accum == Field || Field == NoClass)
907 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000908 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000909 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000910 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000911 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000912 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000913 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000914 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
915 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000916 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000917 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000918}
919
Chris Lattner5c740f12010-06-30 19:14:05 +0000920void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000921 Class &Lo, Class &Hi) const {
922 // FIXME: This code can be simplified by introducing a simple value class for
923 // Class pairs with appropriate constructor methods for the various
924 // situations.
925
926 // FIXME: Some of the split computations are wrong; unaligned vectors
927 // shouldn't be passed in registers for example, so there is no chance they
928 // can straddle an eightbyte. Verify & simplify.
929
930 Lo = Hi = NoClass;
931
932 Class &Current = OffsetBase < 64 ? Lo : Hi;
933 Current = Memory;
934
John McCall9dd450b2009-09-21 23:43:11 +0000935 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000936 BuiltinType::Kind k = BT->getKind();
937
938 if (k == BuiltinType::Void) {
939 Current = NoClass;
940 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
941 Lo = Integer;
942 Hi = Integer;
943 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
944 Current = Integer;
945 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
946 Current = SSE;
947 } else if (k == BuiltinType::LongDouble) {
948 Lo = X87;
949 Hi = X87Up;
950 }
951 // FIXME: _Decimal32 and _Decimal64 are SSE.
952 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000953 return;
954 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000955
Chris Lattnerd776fb12010-06-28 21:43:59 +0000956 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000957 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000958 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000959 return;
960 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000961
Chris Lattnerd776fb12010-06-28 21:43:59 +0000962 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000963 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000964 return;
965 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000966
Chris Lattnerd776fb12010-06-28 21:43:59 +0000967 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000968 if (Ty->isMemberFunctionPointerType())
969 Lo = Hi = Integer;
970 else
971 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000972 return;
973 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000974
Chris Lattnerd776fb12010-06-28 21:43:59 +0000975 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000976 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000977 if (Size == 32) {
978 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
979 // float> as integer.
980 Current = Integer;
981
982 // If this type crosses an eightbyte boundary, it should be
983 // split.
984 uint64_t EB_Real = (OffsetBase) / 64;
985 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
986 if (EB_Real != EB_Imag)
987 Hi = Lo;
988 } else if (Size == 64) {
989 // gcc passes <1 x double> in memory. :(
990 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
991 return;
992
993 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +0000994 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +0000995 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
996 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
997 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000998 Current = Integer;
999 else
1000 Current = SSE;
1001
1002 // If this type crosses an eightbyte boundary, it should be
1003 // split.
1004 if (OffsetBase && OffsetBase != 64)
1005 Hi = Lo;
1006 } else if (Size == 128) {
1007 Lo = SSE;
1008 Hi = SSEUp;
1009 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001010 return;
1011 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001012
Chris Lattnerd776fb12010-06-28 21:43:59 +00001013 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001014 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001015
Chris Lattner2b037972010-07-29 02:01:43 +00001016 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001017 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001018 if (Size <= 64)
1019 Current = Integer;
1020 else if (Size <= 128)
1021 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001022 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001023 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001024 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001025 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001026 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001027 Current = ComplexX87;
1028
1029 // If this complex type crosses an eightbyte boundary then it
1030 // should be split.
1031 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001032 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001033 if (Hi == NoClass && EB_Real != EB_Imag)
1034 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001035
Chris Lattnerd776fb12010-06-28 21:43:59 +00001036 return;
1037 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001038
Chris Lattner2b037972010-07-29 02:01:43 +00001039 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001040 // Arrays are treated like structures.
1041
Chris Lattner2b037972010-07-29 02:01:43 +00001042 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001043
1044 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1045 // than two eightbytes, ..., it has class MEMORY.
1046 if (Size > 128)
1047 return;
1048
1049 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1050 // fields, it has class MEMORY.
1051 //
1052 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001053 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001054 return;
1055
1056 // Otherwise implement simplified merge. We could be smarter about
1057 // this, but it isn't worth it and would be harder to verify.
1058 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001059 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001060 uint64_t ArraySize = AT->getSize().getZExtValue();
1061 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1062 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001063 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001064 Lo = merge(Lo, FieldLo);
1065 Hi = merge(Hi, FieldHi);
1066 if (Lo == Memory || Hi == Memory)
1067 break;
1068 }
1069
1070 // Do post merger cleanup (see below). Only case we worry about is Memory.
1071 if (Hi == Memory)
1072 Lo = Memory;
1073 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001074 return;
1075 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001076
Chris Lattnerd776fb12010-06-28 21:43:59 +00001077 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001078 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001079
1080 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1081 // than two eightbytes, ..., it has class MEMORY.
1082 if (Size > 128)
1083 return;
1084
Anders Carlsson20759ad2009-09-16 15:53:40 +00001085 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1086 // copy constructor or a non-trivial destructor, it is passed by invisible
1087 // reference.
1088 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1089 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001090
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001091 const RecordDecl *RD = RT->getDecl();
1092
1093 // Assume variable sized types are passed in memory.
1094 if (RD->hasFlexibleArrayMember())
1095 return;
1096
Chris Lattner2b037972010-07-29 02:01:43 +00001097 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001098
1099 // Reset Lo class, this will be recomputed.
1100 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001101
1102 // If this is a C++ record, classify the bases first.
1103 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1104 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1105 e = CXXRD->bases_end(); i != e; ++i) {
1106 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1107 "Unexpected base class!");
1108 const CXXRecordDecl *Base =
1109 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1110
1111 // Classify this field.
1112 //
1113 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1114 // single eightbyte, each is classified separately. Each eightbyte gets
1115 // initialized to class NO_CLASS.
1116 Class FieldLo, FieldHi;
1117 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001118 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001119 Lo = merge(Lo, FieldLo);
1120 Hi = merge(Hi, FieldHi);
1121 if (Lo == Memory || Hi == Memory)
1122 break;
1123 }
1124 }
1125
1126 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001127 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001128 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1129 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001130 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1131 bool BitField = i->isBitField();
1132
1133 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1134 // fields, it has class MEMORY.
1135 //
1136 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001137 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001138 Lo = Memory;
1139 return;
1140 }
1141
1142 // Classify this field.
1143 //
1144 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1145 // exceeds a single eightbyte, each is classified
1146 // separately. Each eightbyte gets initialized to class
1147 // NO_CLASS.
1148 Class FieldLo, FieldHi;
1149
1150 // Bit-fields require special handling, they do not force the
1151 // structure to be passed in memory even if unaligned, and
1152 // therefore they can straddle an eightbyte.
1153 if (BitField) {
1154 // Ignore padding bit-fields.
1155 if (i->isUnnamedBitfield())
1156 continue;
1157
1158 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001159 uint64_t Size =
1160 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001161
1162 uint64_t EB_Lo = Offset / 64;
1163 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1164 FieldLo = FieldHi = NoClass;
1165 if (EB_Lo) {
1166 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1167 FieldLo = NoClass;
1168 FieldHi = Integer;
1169 } else {
1170 FieldLo = Integer;
1171 FieldHi = EB_Hi ? Integer : NoClass;
1172 }
1173 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001174 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001175 Lo = merge(Lo, FieldLo);
1176 Hi = merge(Hi, FieldHi);
1177 if (Lo == Memory || Hi == Memory)
1178 break;
1179 }
1180
1181 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1182 //
1183 // (a) If one of the classes is MEMORY, the whole argument is
1184 // passed in memory.
1185 //
1186 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1187
1188 // The first of these conditions is guaranteed by how we implement
1189 // the merge (just bail).
1190 //
1191 // The second condition occurs in the case of unions; for example
1192 // union { _Complex double; unsigned; }.
1193 if (Hi == Memory)
1194 Lo = Memory;
1195 if (Hi == SSEUp && Lo != SSE)
1196 Hi = SSE;
1197 }
1198}
1199
Chris Lattner22a931e2010-06-29 06:01:59 +00001200ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001201 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1202 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001203 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001204 // Treat an enum type as its underlying type.
1205 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1206 Ty = EnumTy->getDecl()->getIntegerType();
1207
1208 return (Ty->isPromotableIntegerType() ?
1209 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1210 }
1211
1212 return ABIArgInfo::getIndirect(0);
1213}
1214
Chris Lattner22a931e2010-06-29 06:01:59 +00001215ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001216 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1217 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001218 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001219 // Treat an enum type as its underlying type.
1220 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1221 Ty = EnumTy->getDecl()->getIntegerType();
1222
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001223 return (Ty->isPromotableIntegerType() ?
1224 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001225 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001226
Daniel Dunbar53fac692010-04-21 19:49:55 +00001227 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1228 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001229
Daniel Dunbar53fac692010-04-21 19:49:55 +00001230 // Compute the byval alignment. We trust the back-end to honor the
1231 // minimum ABI alignment for byval, to make cleaner IR.
1232 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001233 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001234 if (Align > MinABIAlign)
1235 return ABIArgInfo::getIndirect(Align);
1236 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001237}
1238
Chris Lattner4200fe42010-07-29 04:56:46 +00001239/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1240/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1241/// vector register.
1242const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001243 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001244
Chris Lattner9fa15c32010-07-29 05:02:29 +00001245 // Wrapper structs that just contain vectors are passed just like vectors,
1246 // strip them off if present.
1247 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1248 while (STy && STy->getNumElements() == 1) {
1249 IRType = STy->getElementType(0);
1250 STy = dyn_cast<llvm::StructType>(IRType);
1251 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001252
Chris Lattner4200fe42010-07-29 04:56:46 +00001253 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001254 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001255 const llvm::Type *EltTy = VT->getElementType();
1256 if (VT->getBitWidth() == 128 &&
1257 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1258 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1259 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1260 EltTy->isIntegerTy(128)))
1261 return VT;
1262 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001263
Chris Lattner4200fe42010-07-29 04:56:46 +00001264 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1265}
1266
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001267/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1268/// is known to either be off the end of the specified type or being in
1269/// alignment padding. The user type specified is known to be at most 128 bits
1270/// in size, and have passed through X86_64ABIInfo::classify with a successful
1271/// classification that put one of the two halves in the INTEGER class.
1272///
1273/// It is conservatively correct to return false.
1274static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1275 unsigned EndBit, ASTContext &Context) {
1276 // If the bytes being queried are off the end of the type, there is no user
1277 // data hiding here. This handles analysis of builtins, vectors and other
1278 // types that don't contain interesting padding.
1279 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1280 if (TySize <= StartBit)
1281 return true;
1282
Chris Lattner98076a22010-07-29 07:43:55 +00001283 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1284 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1285 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1286
1287 // Check each element to see if the element overlaps with the queried range.
1288 for (unsigned i = 0; i != NumElts; ++i) {
1289 // If the element is after the span we care about, then we're done..
1290 unsigned EltOffset = i*EltSize;
1291 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001292
Chris Lattner98076a22010-07-29 07:43:55 +00001293 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1294 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1295 EndBit-EltOffset, Context))
1296 return false;
1297 }
1298 // If it overlaps no elements, then it is safe to process as padding.
1299 return true;
1300 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001301
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001302 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1303 const RecordDecl *RD = RT->getDecl();
1304 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001305
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001306 // If this is a C++ record, check the bases first.
1307 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1308 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1309 e = CXXRD->bases_end(); i != e; ++i) {
1310 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1311 "Unexpected base class!");
1312 const CXXRecordDecl *Base =
1313 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001314
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001315 // If the base is after the span we care about, ignore it.
1316 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1317 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001318
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001319 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1320 if (!BitsContainNoUserData(i->getType(), BaseStart,
1321 EndBit-BaseOffset, Context))
1322 return false;
1323 }
1324 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001325
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001326 // Verify that no field has data that overlaps the region of interest. Yes
1327 // this could be sped up a lot by being smarter about queried fields,
1328 // however we're only looking at structs up to 16 bytes, so we don't care
1329 // much.
1330 unsigned idx = 0;
1331 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1332 i != e; ++i, ++idx) {
1333 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001334
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001335 // If we found a field after the region we care about, then we're done.
1336 if (FieldOffset >= EndBit) break;
1337
1338 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1339 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1340 Context))
1341 return false;
1342 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001343
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001344 // If nothing in this record overlapped the area of interest, then we're
1345 // clean.
1346 return true;
1347 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001348
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001349 return false;
1350}
1351
Chris Lattnere556a712010-07-29 18:39:32 +00001352/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1353/// float member at the specified offset. For example, {int,{float}} has a
1354/// float at offset 4. It is conservatively correct for this routine to return
1355/// false.
1356static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1357 const llvm::TargetData &TD) {
1358 // Base case if we find a float.
1359 if (IROffset == 0 && IRType->isFloatTy())
1360 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001361
Chris Lattnere556a712010-07-29 18:39:32 +00001362 // If this is a struct, recurse into the field at the specified offset.
1363 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1364 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1365 unsigned Elt = SL->getElementContainingOffset(IROffset);
1366 IROffset -= SL->getElementOffset(Elt);
1367 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1368 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001369
Chris Lattnere556a712010-07-29 18:39:32 +00001370 // If this is an array, recurse into the field at the specified offset.
1371 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1372 const llvm::Type *EltTy = ATy->getElementType();
1373 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1374 IROffset -= IROffset/EltSize*EltSize;
1375 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1376 }
1377
1378 return false;
1379}
1380
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001381
1382/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1383/// low 8 bytes of an XMM register, corresponding to the SSE class.
1384const llvm::Type *X86_64ABIInfo::
1385GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1386 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001387 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001388 // pass as float if the last 4 bytes is just padding. This happens for
1389 // structs that contain 3 floats.
1390 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1391 SourceOffset*8+64, getContext()))
1392 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001393
Chris Lattnere556a712010-07-29 18:39:32 +00001394 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1395 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1396 // case.
1397 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001398 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1399 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001400
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001401 return llvm::Type::getDoubleTy(getVMContext());
1402}
1403
1404
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001405/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1406/// an 8-byte GPR. This means that we either have a scalar or we are talking
1407/// about the high or low part of an up-to-16-byte struct. This routine picks
1408/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001409/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1410/// etc).
1411///
1412/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1413/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1414/// the 8-byte value references. PrefType may be null.
1415///
1416/// SourceTy is the source level type for the entire argument. SourceOffset is
1417/// an offset into this that we're processing (which is always either 0 or 8).
1418///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001419const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001420GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1421 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001422 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1423 // returning an 8-byte unit starting with it. See if we can safely use it.
1424 if (IROffset == 0) {
1425 // Pointers and int64's always fill the 8-byte unit.
1426 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1427 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001428
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001429 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1430 // goodness in the source type is just tail padding. This is allowed to
1431 // kick in for struct {double,int} on the int, but not on
1432 // struct{double,int,int} because we wouldn't return the second int. We
1433 // have to do this analysis on the source type because we can't depend on
1434 // unions being lowered a specific way etc.
1435 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1436 IRType->isIntegerTy(32)) {
1437 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001438
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001439 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1440 SourceOffset*8+64, getContext()))
1441 return IRType;
1442 }
1443 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001444
Chris Lattnerce1bd752010-07-29 04:51:12 +00001445 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001446 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001447 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001448 if (IROffset < SL->getSizeInBytes()) {
1449 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1450 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001451
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001452 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1453 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001454 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001455 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001456
Chris Lattner98076a22010-07-29 07:43:55 +00001457 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1458 const llvm::Type *EltTy = ATy->getElementType();
1459 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1460 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001461 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1462 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001463 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001464
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001465 // Okay, we don't have any better idea of what to pass, so we pass this in an
1466 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001467 unsigned TySizeInBytes =
1468 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001469
Chris Lattner3f763422010-07-29 17:34:39 +00001470 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001471
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001472 // It is always safe to classify this as an integer type up to i64 that
1473 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001474 return llvm::IntegerType::get(getVMContext(),
1475 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001476}
1477
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001478
1479/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1480/// be used as elements of a two register pair to pass or return, return a
1481/// first class aggregate to represent them. For example, if the low part of
1482/// a by-value argument should be passed as i32* and the high part as float,
1483/// return {i32*, float}.
1484static const llvm::Type *
1485GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1486 const llvm::TargetData &TD) {
1487 // In order to correctly satisfy the ABI, we need to the high part to start
1488 // at offset 8. If the high and low parts we inferred are both 4-byte types
1489 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1490 // the second element at offset 8. Check for this:
1491 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1492 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1493 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1494 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
1495
1496 // To handle this, we have to increase the size of the low part so that the
1497 // second element will start at an 8 byte offset. We can't increase the size
1498 // of the second element because it might make us access off the end of the
1499 // struct.
1500 if (HiStart != 8) {
1501 // There are only two sorts of types the ABI generation code can produce for
1502 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1503 // Promote these to a larger type.
1504 if (Lo->isFloatTy())
1505 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1506 else {
1507 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1508 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1509 }
1510 }
1511
1512 const llvm::StructType *Result =
1513 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
1514
1515
1516 // Verify that the second element is at an 8-byte offset.
1517 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1518 "Invalid x86-64 argument pair!");
1519 return Result;
1520}
1521
Chris Lattner31faff52010-07-28 23:06:14 +00001522ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001523classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001524 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1525 // classification algorithm.
1526 X86_64ABIInfo::Class Lo, Hi;
1527 classify(RetTy, 0, Lo, Hi);
1528
1529 // Check some invariants.
1530 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001531 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1532
1533 const llvm::Type *ResType = 0;
1534 switch (Lo) {
1535 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001536 if (Hi == NoClass)
1537 return ABIArgInfo::getIgnore();
1538 // If the low part is just padding, it takes no register, leave ResType
1539 // null.
1540 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1541 "Unknown missing lo part");
1542 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001543
1544 case SSEUp:
1545 case X87Up:
1546 assert(0 && "Invalid classification for lo word.");
1547
1548 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1549 // hidden argument.
1550 case Memory:
1551 return getIndirectReturnResult(RetTy);
1552
1553 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1554 // available register of the sequence %rax, %rdx is used.
1555 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001556 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1557 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001558
Chris Lattner1f3a0632010-07-29 21:42:50 +00001559 // If we have a sign or zero extended integer, make sure to return Extend
1560 // so that the parameter gets the right LLVM IR attributes.
1561 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1562 // Treat an enum type as its underlying type.
1563 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1564 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001565
Chris Lattner1f3a0632010-07-29 21:42:50 +00001566 if (RetTy->isIntegralOrEnumerationType() &&
1567 RetTy->isPromotableIntegerType())
1568 return ABIArgInfo::getExtend();
1569 }
Chris Lattner31faff52010-07-28 23:06:14 +00001570 break;
1571
1572 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1573 // available SSE register of the sequence %xmm0, %xmm1 is used.
1574 case SSE:
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001575 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001576 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001577
1578 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1579 // returned on the X87 stack in %st0 as 80-bit x87 number.
1580 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001581 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001582 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001583
1584 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1585 // part of the value is returned in %st0 and the imaginary part in
1586 // %st1.
1587 case ComplexX87:
1588 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001589 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001590 llvm::Type::getX86_FP80Ty(getVMContext()),
1591 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001592 NULL);
1593 break;
1594 }
1595
Chris Lattner52b3c132010-09-01 00:20:33 +00001596 const llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001597 switch (Hi) {
1598 // Memory was handled previously and X87 should
1599 // never occur as a hi class.
1600 case Memory:
1601 case X87:
1602 assert(0 && "Invalid classification for hi word.");
1603
1604 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001605 case NoClass:
1606 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001607
Chris Lattner52b3c132010-09-01 00:20:33 +00001608 case Integer:
1609 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1610 8, RetTy, 8);
1611 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1612 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001613 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001614 case SSE:
1615 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1616 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1617 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001618 break;
1619
1620 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1621 // is passed in the upper half of the last used SSE register.
1622 //
1623 // SSEUP should always be preceeded by SSE, just widen.
1624 case SSEUp:
1625 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001626 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001627 break;
1628
1629 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1630 // returned together with the previous X87 value in %st0.
1631 case X87Up:
1632 // If X87Up is preceeded by X87, we don't need to do
1633 // anything. However, in some cases with unions it may not be
1634 // preceeded by X87. In such situations we follow gcc and pass the
1635 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001636 if (Lo != X87) {
Chris Lattner52b3c132010-09-01 00:20:33 +00001637 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1638 8, RetTy, 8);
1639 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1640 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001641 }
Chris Lattner31faff52010-07-28 23:06:14 +00001642 break;
1643 }
Chris Lattner52b3c132010-09-01 00:20:33 +00001644
1645 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001646 // known to pass in the high eightbyte of the result. We do this by forming a
1647 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001648 if (HighPart)
1649 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001650
Chris Lattner1f3a0632010-07-29 21:42:50 +00001651 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001652}
1653
Chris Lattner458b2aa2010-07-29 02:16:43 +00001654ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001655 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001656 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001657 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001658
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001659 // Check some invariants.
1660 // FIXME: Enforce these by construction.
1661 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001662 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1663
1664 neededInt = 0;
1665 neededSSE = 0;
1666 const llvm::Type *ResType = 0;
1667 switch (Lo) {
1668 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001669 if (Hi == NoClass)
1670 return ABIArgInfo::getIgnore();
1671 // If the low part is just padding, it takes no register, leave ResType
1672 // null.
1673 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1674 "Unknown missing lo part");
1675 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001676
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001677 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1678 // on the stack.
1679 case Memory:
1680
1681 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1682 // COMPLEX_X87, it is passed in memory.
1683 case X87:
1684 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001685 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001686
1687 case SSEUp:
1688 case X87Up:
1689 assert(0 && "Invalid classification for lo word.");
1690
1691 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1692 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1693 // and %r9 is used.
1694 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001695 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001696
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001697 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001698 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001699
1700 // If we have a sign or zero extended integer, make sure to return Extend
1701 // so that the parameter gets the right LLVM IR attributes.
1702 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1703 // Treat an enum type as its underlying type.
1704 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1705 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001706
Chris Lattner1f3a0632010-07-29 21:42:50 +00001707 if (Ty->isIntegralOrEnumerationType() &&
1708 Ty->isPromotableIntegerType())
1709 return ABIArgInfo::getExtend();
1710 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001711
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001712 break;
1713
1714 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1715 // available SSE register is used, the registers are taken in the
1716 // order from %xmm0 to %xmm7.
1717 case SSE:
1718 ++neededSSE;
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001719 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001720 break;
1721 }
1722
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001723 const llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001724 switch (Hi) {
1725 // Memory was handled previously, ComplexX87 and X87 should
1726 // never occur as hi classes, and X87Up must be preceed by X87,
1727 // which is passed in memory.
1728 case Memory:
1729 case X87:
1730 case ComplexX87:
1731 assert(0 && "Invalid classification for hi word.");
1732 break;
1733
1734 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001735
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001736 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001737 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001738 // Pick an 8-byte type based on the preferred type.
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001739 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001740
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001741 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1742 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001743 break;
1744
1745 // X87Up generally doesn't occur here (long double is passed in
1746 // memory), except in situations involving unions.
1747 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001748 case SSE:
1749 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001750
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001751 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1752 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001753
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001754 ++neededSSE;
1755 break;
1756
1757 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1758 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001759 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001760 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001761 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001762 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001763 break;
1764 }
1765
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001766 // If a high part was specified, merge it together with the low part. It is
1767 // known to pass in the high eightbyte of the result. We do this by forming a
1768 // first class struct aggregate with the high and low part: {low, high}
1769 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001770 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001771
Chris Lattner1f3a0632010-07-29 21:42:50 +00001772 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001773}
1774
Chris Lattner22326a12010-07-29 02:31:05 +00001775void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001776
Chris Lattner458b2aa2010-07-29 02:16:43 +00001777 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001778
1779 // Keep track of the number of assigned registers.
1780 unsigned freeIntRegs = 6, freeSSERegs = 8;
1781
1782 // If the return value is indirect, then the hidden argument is consuming one
1783 // integer register.
1784 if (FI.getReturnInfo().isIndirect())
1785 --freeIntRegs;
1786
1787 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1788 // get assigned (in left-to-right order) for passing as follows...
1789 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1790 it != ie; ++it) {
1791 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001792 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001793
1794 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1795 // eightbyte of an argument, the whole argument is passed on the
1796 // stack. If registers have already been assigned for some
1797 // eightbytes of such an argument, the assignments get reverted.
1798 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1799 freeIntRegs -= neededInt;
1800 freeSSERegs -= neededSSE;
1801 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001802 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001803 }
1804 }
1805}
1806
1807static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1808 QualType Ty,
1809 CodeGenFunction &CGF) {
1810 llvm::Value *overflow_arg_area_p =
1811 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1812 llvm::Value *overflow_arg_area =
1813 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1814
1815 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1816 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1817 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1818 if (Align > 8) {
1819 // Note that we follow the ABI & gcc here, even though the type
1820 // could in theory have an alignment greater than 16. This case
1821 // shouldn't ever matter in practice.
1822
1823 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001824 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001825 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001826 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1827 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001828 CGF.Int64Ty);
1829 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001830 overflow_arg_area =
1831 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1832 overflow_arg_area->getType(),
1833 "overflow_arg_area.align");
1834 }
1835
1836 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1837 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1838 llvm::Value *Res =
1839 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001840 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001841
1842 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1843 // l->overflow_arg_area + sizeof(type).
1844 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1845 // an 8 byte boundary.
1846
1847 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001848 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001849 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001850 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1851 "overflow_arg_area.next");
1852 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1853
1854 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1855 return Res;
1856}
1857
1858llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1859 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001860 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001861
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001862 // Assume that va_list type is correct; should be pointer to LLVM type:
1863 // struct {
1864 // i32 gp_offset;
1865 // i32 fp_offset;
1866 // i8* overflow_arg_area;
1867 // i8* reg_save_area;
1868 // };
1869 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001870
Chris Lattner9723d6c2010-03-11 18:19:55 +00001871 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001872 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001873
1874 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1875 // in the registers. If not go to step 7.
1876 if (!neededInt && !neededSSE)
1877 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1878
1879 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1880 // general purpose registers needed to pass type and num_fp to hold
1881 // the number of floating point registers needed.
1882
1883 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1884 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1885 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1886 //
1887 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1888 // register save space).
1889
1890 llvm::Value *InRegs = 0;
1891 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1892 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1893 if (neededInt) {
1894 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1895 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001896 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1897 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001898 }
1899
1900 if (neededSSE) {
1901 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1902 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1903 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001904 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1905 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001906 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1907 }
1908
1909 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1910 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1911 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1912 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1913
1914 // Emit code to load the value if it was passed in registers.
1915
1916 CGF.EmitBlock(InRegBlock);
1917
1918 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1919 // an offset of l->gp_offset and/or l->fp_offset. This may require
1920 // copying to a temporary location in case the parameter is passed
1921 // in different register classes or requires an alignment greater
1922 // than 8 for general purpose registers and 16 for XMM registers.
1923 //
1924 // FIXME: This really results in shameful code when we end up needing to
1925 // collect arguments from different places; often what should result in a
1926 // simple assembling of a structure from scattered addresses has many more
1927 // loads than necessary. Can we clean this up?
1928 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1929 llvm::Value *RegAddr =
1930 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1931 "reg_save_area");
1932 if (neededInt && neededSSE) {
1933 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001934 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001935 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1936 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1937 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1938 const llvm::Type *TyLo = ST->getElementType(0);
1939 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00001940 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001941 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001942 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1943 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001944 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1945 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001946 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1947 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001948 llvm::Value *V =
1949 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1950 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1951 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1952 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1953
Owen Anderson170229f2009-07-14 23:10:40 +00001954 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001955 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001956 } else if (neededInt) {
1957 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1958 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001959 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001960 } else if (neededSSE == 1) {
1961 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1962 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1963 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001964 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001965 assert(neededSSE == 2 && "Invalid number of needed registers!");
1966 // SSE registers are spaced 16 bytes apart in the register save
1967 // area, we need to collect the two eightbytes together.
1968 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001969 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001970 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1971 const llvm::Type *DblPtrTy =
1972 llvm::PointerType::getUnqual(DoubleTy);
1973 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1974 DoubleTy, NULL);
1975 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1976 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1977 DblPtrTy));
1978 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1979 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1980 DblPtrTy));
1981 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1982 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1983 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001984 }
1985
1986 // AMD64-ABI 3.5.7p5: Step 5. Set:
1987 // l->gp_offset = l->gp_offset + num_gp * 8
1988 // l->fp_offset = l->fp_offset + num_fp * 16.
1989 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001990 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001991 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1992 gp_offset_p);
1993 }
1994 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001995 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001996 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1997 fp_offset_p);
1998 }
1999 CGF.EmitBranch(ContBlock);
2000
2001 // Emit code to load the value if it was passed in memory.
2002
2003 CGF.EmitBlock(InMemBlock);
2004 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2005
2006 // Return the appropriate result.
2007
2008 CGF.EmitBlock(ContBlock);
2009 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2010 "vaarg.addr");
2011 ResAddr->reserveOperandSpace(2);
2012 ResAddr->addIncoming(RegAddr, InRegBlock);
2013 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002014 return ResAddr;
2015}
2016
Chris Lattner04dc9572010-08-31 16:44:54 +00002017llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2018 CodeGenFunction &CGF) const {
2019 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2020 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002021
Chris Lattner04dc9572010-08-31 16:44:54 +00002022 CGBuilderTy &Builder = CGF.Builder;
2023 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2024 "ap");
2025 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2026 llvm::Type *PTy =
2027 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2028 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2029
2030 uint64_t Offset =
2031 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2032 llvm::Value *NextAddr =
2033 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2034 "ap.next");
2035 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2036
2037 return AddrTyped;
2038}
Chris Lattner0cf24192010-06-28 20:05:43 +00002039
2040//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002041// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002042//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002043
2044namespace {
2045
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002046class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002047public:
2048 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002049
Chris Lattner458b2aa2010-07-29 02:16:43 +00002050 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002051
Chris Lattner458b2aa2010-07-29 02:16:43 +00002052 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002053
Chris Lattner22326a12010-07-29 02:31:05 +00002054 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002055 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002056 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2057 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002058 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002059 }
2060
2061 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2062 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002063};
2064
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002065class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
2066public:
Chris Lattner2b037972010-07-29 02:01:43 +00002067 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
2068 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002069};
2070
Daniel Dunbard59655c2009-09-12 00:59:49 +00002071}
2072
Chris Lattner458b2aa2010-07-29 02:16:43 +00002073ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002074 if (RetTy->isVoidType()) {
2075 return ABIArgInfo::getIgnore();
2076 } else {
2077 return ABIArgInfo::getDirect();
2078 }
2079}
2080
Chris Lattner458b2aa2010-07-29 02:16:43 +00002081ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002082 return ABIArgInfo::getDirect();
2083}
2084
2085llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002086 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00002087 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00002088 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
2089
2090 CGBuilderTy &Builder = CGF.Builder;
2091 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2092 "ap");
2093 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2094 llvm::Type *PTy =
2095 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2096 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2097
2098 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
2099
2100 llvm::Value *NextAddr =
2101 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
2102 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
2103 "ap.next");
2104 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2105
2106 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002107}
2108
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00002109
John McCallea8d8bb2010-03-11 00:10:12 +00002110// PowerPC-32
2111
2112namespace {
2113class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2114public:
Chris Lattner2b037972010-07-29 02:01:43 +00002115 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002116
John McCallea8d8bb2010-03-11 00:10:12 +00002117 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2118 // This is recovered from gcc output.
2119 return 1; // r1 is the dedicated stack pointer
2120 }
2121
2122 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002123 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002124};
2125
2126}
2127
2128bool
2129PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2130 llvm::Value *Address) const {
2131 // This is calculated from the LLVM and GCC tables and verified
2132 // against gcc output. AFAIK all ABIs use the same encoding.
2133
2134 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2135 llvm::LLVMContext &Context = CGF.getLLVMContext();
2136
2137 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2138 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2139 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2140 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2141
2142 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002143 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002144
2145 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002146 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002147
2148 // 64-76 are various 4-byte special-purpose registers:
2149 // 64: mq
2150 // 65: lr
2151 // 66: ctr
2152 // 67: ap
2153 // 68-75 cr0-7
2154 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002155 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002156
2157 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002158 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002159
2160 // 109: vrsave
2161 // 110: vscr
2162 // 111: spe_acc
2163 // 112: spefscr
2164 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002165 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002166
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002167 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002168}
2169
2170
Chris Lattner0cf24192010-06-28 20:05:43 +00002171//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002172// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002173//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002174
2175namespace {
2176
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002177class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002178public:
2179 enum ABIKind {
2180 APCS = 0,
2181 AAPCS = 1,
2182 AAPCS_VFP
2183 };
2184
2185private:
2186 ABIKind Kind;
2187
2188public:
Chris Lattner2b037972010-07-29 02:01:43 +00002189 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002190
2191private:
2192 ABIKind getABIKind() const { return Kind; }
2193
Chris Lattner458b2aa2010-07-29 02:16:43 +00002194 ABIArgInfo classifyReturnType(QualType RetTy) const;
2195 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002196
Chris Lattner22326a12010-07-29 02:31:05 +00002197 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002198
2199 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2200 CodeGenFunction &CGF) const;
2201};
2202
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002203class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2204public:
Chris Lattner2b037972010-07-29 02:01:43 +00002205 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2206 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002207
2208 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2209 return 13;
2210 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002211};
2212
Daniel Dunbard59655c2009-09-12 00:59:49 +00002213}
2214
Chris Lattner22326a12010-07-29 02:31:05 +00002215void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002216 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002217 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002218 it != ie; ++it)
2219 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002220
Chris Lattner458b2aa2010-07-29 02:16:43 +00002221 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002222 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002223 if (Triple.getEnvironmentName() == "gnueabi" ||
2224 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002225 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002226 else
2227 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002228
Daniel Dunbar020daa92009-09-12 01:00:39 +00002229 switch (getABIKind()) {
2230 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002231 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2232 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002233 break;
2234
2235 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002236 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2237 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002238 break;
2239
2240 case AAPCS_VFP:
2241 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2242 break;
2243 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002244}
2245
Chris Lattner458b2aa2010-07-29 02:16:43 +00002246ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002247 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002248 // Treat an enum type as its underlying type.
2249 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2250 Ty = EnumTy->getDecl()->getIntegerType();
2251
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002252 return (Ty->isPromotableIntegerType() ?
2253 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002254 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002255
Daniel Dunbar09d33622009-09-14 21:54:03 +00002256 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002257 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002258 return ABIArgInfo::getIgnore();
2259
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002260 // Structures with either a non-trivial destructor or a non-trivial
2261 // copy constructor are always indirect.
2262 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2263 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2264
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002265 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2266 // backend doesn't support byval.
2267 // FIXME: This doesn't handle alignment > 64 bits.
2268 const llvm::Type* ElemTy;
2269 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002270 if (getContext().getTypeAlign(Ty) > 32) {
2271 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2272 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002273 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002274 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2275 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002276 }
2277 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002278 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002279 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2280 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002281 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002282}
2283
Chris Lattner458b2aa2010-07-29 02:16:43 +00002284static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002285 llvm::LLVMContext &VMContext) {
2286 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2287 // is called integer-like if its size is less than or equal to one word, and
2288 // the offset of each of its addressable sub-fields is zero.
2289
2290 uint64_t Size = Context.getTypeSize(Ty);
2291
2292 // Check that the type fits in a word.
2293 if (Size > 32)
2294 return false;
2295
2296 // FIXME: Handle vector types!
2297 if (Ty->isVectorType())
2298 return false;
2299
Daniel Dunbard53bac72009-09-14 02:20:34 +00002300 // Float types are never treated as "integer like".
2301 if (Ty->isRealFloatingType())
2302 return false;
2303
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002304 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002305 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002306 return true;
2307
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002308 // Small complex integer types are "integer like".
2309 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2310 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002311
2312 // Single element and zero sized arrays should be allowed, by the definition
2313 // above, but they are not.
2314
2315 // Otherwise, it must be a record type.
2316 const RecordType *RT = Ty->getAs<RecordType>();
2317 if (!RT) return false;
2318
2319 // Ignore records with flexible arrays.
2320 const RecordDecl *RD = RT->getDecl();
2321 if (RD->hasFlexibleArrayMember())
2322 return false;
2323
2324 // Check that all sub-fields are at offset 0, and are themselves "integer
2325 // like".
2326 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2327
2328 bool HadField = false;
2329 unsigned idx = 0;
2330 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2331 i != e; ++i, ++idx) {
2332 const FieldDecl *FD = *i;
2333
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002334 // Bit-fields are not addressable, we only need to verify they are "integer
2335 // like". We still have to disallow a subsequent non-bitfield, for example:
2336 // struct { int : 0; int x }
2337 // is non-integer like according to gcc.
2338 if (FD->isBitField()) {
2339 if (!RD->isUnion())
2340 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002341
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002342 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2343 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002344
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002345 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002346 }
2347
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002348 // Check if this field is at offset 0.
2349 if (Layout.getFieldOffset(idx) != 0)
2350 return false;
2351
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002352 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2353 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002354
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002355 // Only allow at most one field in a structure. This doesn't match the
2356 // wording above, but follows gcc in situations with a field following an
2357 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002358 if (!RD->isUnion()) {
2359 if (HadField)
2360 return false;
2361
2362 HadField = true;
2363 }
2364 }
2365
2366 return true;
2367}
2368
Chris Lattner458b2aa2010-07-29 02:16:43 +00002369ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002370 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002371 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002372
John McCalla1dee5302010-08-22 10:59:02 +00002373 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002374 // Treat an enum type as its underlying type.
2375 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2376 RetTy = EnumTy->getDecl()->getIntegerType();
2377
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002378 return (RetTy->isPromotableIntegerType() ?
2379 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002380 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002381
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002382 // Structures with either a non-trivial destructor or a non-trivial
2383 // copy constructor are always indirect.
2384 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2385 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2386
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002387 // Are we following APCS?
2388 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002389 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002390 return ABIArgInfo::getIgnore();
2391
Daniel Dunbareedf1512010-02-01 23:31:19 +00002392 // Complex types are all returned as packed integers.
2393 //
2394 // FIXME: Consider using 2 x vector types if the back end handles them
2395 // correctly.
2396 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002397 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002398 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002399
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002400 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002401 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002402 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002403 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002404 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002405 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002406 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002407 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2408 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002409 }
2410
2411 // Otherwise return in memory.
2412 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002413 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002414
2415 // Otherwise this is an AAPCS variant.
2416
Chris Lattner458b2aa2010-07-29 02:16:43 +00002417 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002418 return ABIArgInfo::getIgnore();
2419
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002420 // Aggregates <= 4 bytes are returned in r0; other aggregates
2421 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002422 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002423 if (Size <= 32) {
2424 // Return in the smallest viable integer type.
2425 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002426 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002427 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002428 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2429 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002430 }
2431
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002432 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002433}
2434
2435llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002436 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002437 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002438 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002439 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002440
2441 CGBuilderTy &Builder = CGF.Builder;
2442 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2443 "ap");
2444 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2445 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002446 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002447 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2448
2449 uint64_t Offset =
2450 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2451 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002452 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002453 "ap.next");
2454 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2455
2456 return AddrTyped;
2457}
2458
Chris Lattner458b2aa2010-07-29 02:16:43 +00002459ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2460 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002461 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002462
John McCalla1dee5302010-08-22 10:59:02 +00002463 if (isAggregateTypeForABI(RetTy))
Chris Lattner458b2aa2010-07-29 02:16:43 +00002464 return ABIArgInfo::getIndirect(0);
2465
2466 // Treat an enum type as its underlying type.
2467 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2468 RetTy = EnumTy->getDecl()->getIntegerType();
2469
2470 return (RetTy->isPromotableIntegerType() ?
2471 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002472}
2473
Chris Lattner0cf24192010-06-28 20:05:43 +00002474//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002475// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002476//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002477
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002478namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002479
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002480class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002481public:
2482 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2483
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002484 bool isPromotableIntegerType(QualType Ty) const;
2485
Chris Lattner458b2aa2010-07-29 02:16:43 +00002486 ABIArgInfo classifyReturnType(QualType RetTy) const;
2487 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002488
Chris Lattner22326a12010-07-29 02:31:05 +00002489 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002490 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002491 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2492 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002493 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002494 }
2495
2496 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2497 CodeGenFunction &CGF) const;
2498};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002499
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002500class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2501public:
Chris Lattner2b037972010-07-29 02:01:43 +00002502 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2503 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002504};
2505
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002506}
2507
2508bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2509 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002510 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002511 switch (BT->getKind()) {
2512 case BuiltinType::Bool:
2513 case BuiltinType::Char_S:
2514 case BuiltinType::Char_U:
2515 case BuiltinType::SChar:
2516 case BuiltinType::UChar:
2517 case BuiltinType::Short:
2518 case BuiltinType::UShort:
2519 case BuiltinType::Int:
2520 case BuiltinType::UInt:
2521 return true;
2522 default:
2523 return false;
2524 }
2525 return false;
2526}
2527
2528llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2529 CodeGenFunction &CGF) const {
2530 // FIXME: Implement
2531 return 0;
2532}
2533
2534
Chris Lattner458b2aa2010-07-29 02:16:43 +00002535ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2536 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002537 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002538 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002539 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002540
2541 return (isPromotableIntegerType(RetTy) ?
2542 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002543}
2544
Chris Lattner458b2aa2010-07-29 02:16:43 +00002545ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002546 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002547 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002548
2549 return (isPromotableIntegerType(Ty) ?
2550 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002551}
2552
Chris Lattner0cf24192010-06-28 20:05:43 +00002553//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002554// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002555//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002556
2557namespace {
2558
2559class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2560public:
Chris Lattner2b037972010-07-29 02:01:43 +00002561 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2562 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002563 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2564 CodeGen::CodeGenModule &M) const;
2565};
2566
2567}
2568
2569void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2570 llvm::GlobalValue *GV,
2571 CodeGen::CodeGenModule &M) const {
2572 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2573 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2574 // Handle 'interrupt' attribute:
2575 llvm::Function *F = cast<llvm::Function>(GV);
2576
2577 // Step 1: Set ISR calling convention.
2578 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2579
2580 // Step 2: Add attributes goodness.
2581 F->addFnAttr(llvm::Attribute::NoInline);
2582
2583 // Step 3: Emit ISR vector alias.
2584 unsigned Num = attr->getNumber() + 0xffe0;
2585 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2586 "vector_" +
2587 llvm::LowercaseString(llvm::utohexstr(Num)),
2588 GV, &M.getModule());
2589 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002590 }
2591}
2592
Chris Lattner0cf24192010-06-28 20:05:43 +00002593//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002594// MIPS ABI Implementation. This works for both little-endian and
2595// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002596//===----------------------------------------------------------------------===//
2597
John McCall943fae92010-05-27 06:19:26 +00002598namespace {
2599class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2600public:
Chris Lattner2b037972010-07-29 02:01:43 +00002601 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2602 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002603
2604 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2605 return 29;
2606 }
2607
2608 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002609 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002610};
2611}
2612
2613bool
2614MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2615 llvm::Value *Address) const {
2616 // This information comes from gcc's implementation, which seems to
2617 // as canonical as it gets.
2618
2619 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2620 llvm::LLVMContext &Context = CGF.getLLVMContext();
2621
2622 // Everything on MIPS is 4 bytes. Double-precision FP registers
2623 // are aliased to pairs of single-precision FP registers.
2624 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2625 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2626
2627 // 0-31 are the general purpose registers, $0 - $31.
2628 // 32-63 are the floating-point registers, $f0 - $f31.
2629 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2630 // 66 is the (notional, I think) register for signal-handler return.
2631 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2632
2633 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2634 // They are one bit wide and ignored here.
2635
2636 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2637 // (coprocessor 1 is the FP unit)
2638 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2639 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2640 // 176-181 are the DSP accumulator registers.
2641 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2642
2643 return false;
2644}
2645
2646
Chris Lattner2b037972010-07-29 02:01:43 +00002647const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002648 if (TheTargetCodeGenInfo)
2649 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002650
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002651 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2652 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002653
Chris Lattner22a931e2010-06-29 06:01:59 +00002654 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002655 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002656 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002657 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002658
John McCall943fae92010-05-27 06:19:26 +00002659 case llvm::Triple::mips:
2660 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002661 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002662
Daniel Dunbard59655c2009-09-12 00:59:49 +00002663 case llvm::Triple::arm:
2664 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002665 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002666 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002667 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002668 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002669
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002670 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002671 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002672
2673 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002674 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002675
John McCallea8d8bb2010-03-11 00:10:12 +00002676 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002677 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002678
Daniel Dunbard59655c2009-09-12 00:59:49 +00002679 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002680 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002681
2682 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002683 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002684
Daniel Dunbar40165182009-08-24 09:10:05 +00002685 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002686 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002687 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002688 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002689 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002690 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002691 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002692 case llvm::Triple::AuroraUX:
2693 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002694 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002695 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002696 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002697 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002698
2699 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002700 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002701 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002702 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002703
Daniel Dunbare3532f82009-08-24 08:52:16 +00002704 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00002705 switch (Triple.getOS()) {
2706 case llvm::Triple::Win32:
2707 case llvm::Triple::MinGW64:
2708 case llvm::Triple::Cygwin:
2709 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2710 default:
2711 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2712 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00002713 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002714}