blob: 3ee4d1ad6df9d4b669e39d81be4995933ad7642b [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar626f1d82009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000091
92/// isEmptyField - Return true iff a the field is "empty", that is it
93/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000100
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000101 // Constant arrays of empty records count as empty, strip them off.
102 if (AllowArrays)
103 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
104 FT = AT->getElementType();
105
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000106 const RecordType *RT = FT->getAs<RecordType>();
107 if (!RT)
108 return false;
109
110 // C++ record fields are never empty, at least in the Itanium ABI.
111 //
112 // FIXME: We should use a predicate for whether this behavior is true in the
113 // current ABI.
114 if (isa<CXXRecordDecl>(RT->getDecl()))
115 return false;
116
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000118}
119
120/// isEmptyRecord - Return true iff a structure contains only empty
121/// fields. Note that a structure with a flexible array member is not
122/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000125 if (!RT)
126 return 0;
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000130
131 // If this is a C++ record, check the bases first.
132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
133 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
134 e = CXXRD->bases_end(); i != e; ++i)
135 if (!isEmptyRecord(Context, i->getType(), true))
136 return false;
137
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson20759ad2009-09-16 15:53:40 +0000145/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
146/// a non-trivial destructor or a non-trivial copy constructor.
147static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
148 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
149 if (!RD)
150 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000151
Anders Carlsson20759ad2009-09-16 15:53:40 +0000152 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
153}
154
155/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
156/// a record type with either a non-trivial destructor or a non-trivial copy
157/// constructor.
158static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
159 const RecordType *RT = T->getAs<RecordType>();
160 if (!RT)
161 return false;
162
163 return hasNonTrivialDestructorOrCopyConstructor(RT);
164}
165
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000166/// isSingleElementStruct - Determine if a structure is a "single
167/// element struct", i.e. it has exactly one non-empty field or
168/// exactly one field which is itself a single element
169/// struct. Structures with flexible array members are never
170/// considered single element structs.
171///
172/// \return The field declaration for the single non-empty field, if
173/// it exists.
174static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
175 const RecordType *RT = T->getAsStructureType();
176 if (!RT)
177 return 0;
178
179 const RecordDecl *RD = RT->getDecl();
180 if (RD->hasFlexibleArrayMember())
181 return 0;
182
183 const Type *Found = 0;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000184
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000185 // If this is a C++ record, check the bases first.
186 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
187 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
188 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000191 continue;
192
193 // If we already found an element then this isn't a single-element struct.
194 if (Found)
195 return 0;
196
197 // If this is non-empty and not a single element struct, the composite
198 // cannot be a single element struct.
199 Found = isSingleElementStruct(i->getType(), Context);
200 if (!Found)
201 return 0;
202 }
203 }
204
205 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000213 continue;
214
215 // If we already found an element then this isn't a single-element
216 // struct.
217 if (Found)
218 return 0;
219
220 // Treat single element arrays as the element.
221 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
222 if (AT->getSize().getZExtValue() != 1)
223 break;
224 FT = AT->getElementType();
225 }
226
John McCalla1dee5302010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000228 Found = FT.getTypePtr();
229 } else {
230 Found = isSingleElementStruct(FT, Context);
231 if (!Found)
232 return 0;
233 }
234 }
235
236 return Found;
237}
238
239static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000243 return false;
244
245 uint64_t Size = Context.getTypeSize(Ty);
246 return Size == 32 || Size == 64;
247}
248
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000249/// canExpandIndirectArgument - Test whether an argument type which is to be
250/// passed indirectly (on the stack) would have the equivalent layout if it was
251/// expanded into separate arguments. If so, we prefer to do the latter to avoid
252/// inhibiting optimizations.
253///
254// FIXME: This predicate is missing many cases, currently it just follows
255// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
256// should probably make this smarter, or better yet make the LLVM backend
257// capable of handling it.
258static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
259 // We can only expand structure types.
260 const RecordType *RT = Ty->getAs<RecordType>();
261 if (!RT)
262 return false;
263
264 // We can only expand (C) structures.
265 //
266 // FIXME: This needs to be generalized to handle classes as well.
267 const RecordDecl *RD = RT->getDecl();
268 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
269 return false;
270
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000273 const FieldDecl *FD = *i;
274
275 if (!is32Or64BitBasicType(FD->getType(), Context))
276 return false;
277
278 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
279 // how to expand them yet, and the predicate for telling if a bitfield still
280 // counts as "basic" is more complicated than what we were doing previously.
281 if (FD->isBitField())
282 return false;
283 }
284
285 return true;
286}
287
288namespace {
289/// DefaultABIInfo - The default implementation for ABI specific
290/// details. This implementation provides information which results in
291/// self-consistent and sensible LLVM IR generation, but does not
292/// conform to any particular ABI.
293class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000296
Chris Lattner458b2aa2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299
Chris Lattner22326a12010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattner2b037972010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000315};
316
317llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const {
319 return 0;
320}
321
Chris Lattner458b2aa2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000325
Chris Lattner9723d6c2010-03-11 18:19:55 +0000326 // Treat an enum type as its underlying type.
327 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
328 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000329
Chris Lattner9723d6c2010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000332}
333
Chris Lattner0cf24192010-06-28 20:05:43 +0000334//===----------------------------------------------------------------------===//
335// X86-32 ABI Implementation
336//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000337
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000338/// X86_32ABIInfo - The X86-32 ABI information.
339class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000340 static const unsigned MinABIStackAlignInBytes = 4;
341
David Chisnallde3a0692009-08-17 23:08:21 +0000342 bool IsDarwinVectorABI;
343 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000344
345 static bool isRegisterSize(unsigned Size) {
346 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
347 }
348
349 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
350
Daniel Dunbar557893d2010-04-21 19:10:51 +0000351 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
352 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000353 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000354
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000355 /// \brief Return the alignment to use for the given type on the stack.
356 unsigned getTypeStackAlignInBytes(QualType Ty) const;
357
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000358public:
Chris Lattner2b037972010-07-29 02:01:43 +0000359
Chris Lattner458b2aa2010-07-29 02:16:43 +0000360 ABIArgInfo classifyReturnType(QualType RetTy) const;
361 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000362
Chris Lattner22326a12010-07-29 02:31:05 +0000363 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000364 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000365 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
366 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000367 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000368 }
369
370 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
371 CodeGenFunction &CGF) const;
372
Chris Lattner2b037972010-07-29 02:01:43 +0000373 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
374 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000375};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000376
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000377class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
378public:
Chris Lattner2b037972010-07-29 02:01:43 +0000379 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
380 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000381
382 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
383 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000384
385 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
386 // Darwin uses different dwarf register numbers for EH.
387 if (CGM.isTargetDarwin()) return 5;
388
389 return 4;
390 }
391
392 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
393 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000394};
395
396}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000397
398/// shouldReturnTypeInRegister - Determine if the given type should be
399/// passed in a register (for the Darwin ABI).
400bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
401 ASTContext &Context) {
402 uint64_t Size = Context.getTypeSize(Ty);
403
404 // Type must be register sized.
405 if (!isRegisterSize(Size))
406 return false;
407
408 if (Ty->isVectorType()) {
409 // 64- and 128- bit vectors inside structures are not returned in
410 // registers.
411 if (Size == 64 || Size == 128)
412 return false;
413
414 return true;
415 }
416
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000417 // If this is a builtin, pointer, enum, complex type, member pointer, or
418 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000419 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000420 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000421 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000422 return true;
423
424 // Arrays are treated like records.
425 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
426 return shouldReturnTypeInRegister(AT->getElementType(), Context);
427
428 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000429 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000430 if (!RT) return false;
431
Anders Carlsson40446e82010-01-27 03:25:19 +0000432 // FIXME: Traverse bases here too.
433
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000434 // Structure types are passed in register if all fields would be
435 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000436 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
437 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000438 const FieldDecl *FD = *i;
439
440 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000441 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000442 continue;
443
444 // Check fields recursively.
445 if (!shouldReturnTypeInRegister(FD->getType(), Context))
446 return false;
447 }
448
449 return true;
450}
451
Chris Lattner458b2aa2010-07-29 02:16:43 +0000452ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
453 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000455
Chris Lattner458b2aa2010-07-29 02:16:43 +0000456 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000457 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000458 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000459 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460
461 // 128-bit vectors are a special case; they are returned in
462 // registers and we need to make sure to pick a type the LLVM
463 // backend will like.
464 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000465 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000466 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000467
468 // Always return in register if it fits in a general purpose
469 // register, or if it is 64 bits and has a single element.
470 if ((Size == 8 || Size == 16 || Size == 32) ||
471 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000472 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000473 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000474
475 return ABIArgInfo::getIndirect(0);
476 }
477
478 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000479 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000480
John McCalla1dee5302010-08-22 10:59:02 +0000481 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000482 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000483 // Structures with either a non-trivial destructor or a non-trivial
484 // copy constructor are always indirect.
485 if (hasNonTrivialDestructorOrCopyConstructor(RT))
486 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000487
Anders Carlsson5789c492009-10-20 22:07:59 +0000488 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000489 if (RT->getDecl()->hasFlexibleArrayMember())
490 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000491 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000492
David Chisnallde3a0692009-08-17 23:08:21 +0000493 // If specified, structs and unions are always indirect.
494 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000495 return ABIArgInfo::getIndirect(0);
496
497 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000498 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000499 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000500 if (BT->isIntegerType()) {
501 // We need to use the size of the structure, padding
502 // bit-fields can adjust that to be larger than the single
503 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000504 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000505 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000506 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
507 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000508
Chris Lattner458b2aa2010-07-29 02:16:43 +0000509 if (BT->getKind() == BuiltinType::Float) {
510 assert(getContext().getTypeSize(RetTy) ==
511 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000513 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000514 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000515
Chris Lattner458b2aa2010-07-29 02:16:43 +0000516 if (BT->getKind() == BuiltinType::Double) {
517 assert(getContext().getTypeSize(RetTy) ==
518 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000519 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000520 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000521 }
522 } else if (SeltTy->isPointerType()) {
523 // FIXME: It would be really nice if this could come out as the proper
524 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000525 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000526 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000527 } else if (SeltTy->isVectorType()) {
528 // 64- and 128-bit vectors are never returned in a
529 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000530 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000531 if (Size == 64 || Size == 128)
532 return ABIArgInfo::getIndirect(0);
533
Chris Lattner458b2aa2010-07-29 02:16:43 +0000534 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000535 }
536 }
537
538 // Small structures which are register sized are generally returned
539 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000540 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
541 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000542 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000543 }
544
545 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000546 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000547
Chris Lattner458b2aa2010-07-29 02:16:43 +0000548 // Treat an enum type as its underlying type.
549 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
550 RetTy = EnumTy->getDecl()->getIntegerType();
551
552 return (RetTy->isPromotableIntegerType() ?
553 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000554}
555
Daniel Dunbared23de32010-09-16 20:42:00 +0000556static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
557 const RecordType *RT = Ty->getAs<RecordType>();
558 if (!RT)
559 return 0;
560 const RecordDecl *RD = RT->getDecl();
561
562 // If this is a C++ record, check the bases first.
563 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
564 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
565 e = CXXRD->bases_end(); i != e; ++i)
566 if (!isRecordWithSSEVectorType(Context, i->getType()))
567 return false;
568
569 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
570 i != e; ++i) {
571 QualType FT = i->getType();
572
573 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
574 return true;
575
576 if (isRecordWithSSEVectorType(Context, FT))
577 return true;
578 }
579
580 return false;
581}
582
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000583unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty) const {
584 // On non-Darwin, the stack type alignment is always 4.
585 if (!IsDarwinVectorABI)
586 return MinABIStackAlignInBytes;
587
588 // Otherwise, if the alignment is less than or equal to 4, use the minimum ABI
589 // alignment.
590 unsigned Align = getContext().getTypeAlign(Ty) / 8;
591 if (Align <= MinABIStackAlignInBytes)
592 return MinABIStackAlignInBytes;
593
Daniel Dunbared23de32010-09-16 20:42:00 +0000594 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
595 if (isRecordWithSSEVectorType(getContext(), Ty))
596 return 16;
597
598 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000599}
600
Chris Lattner458b2aa2010-07-29 02:16:43 +0000601ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000602 if (!ByVal)
603 return ABIArgInfo::getIndirect(0, false);
604
605 // Compute the byval alignment. We trust the back-end to honor the
606 // minimum ABI alignment for byval, to make cleaner IR.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000607 unsigned Align = getTypeStackAlignInBytes(Ty);
608 if (Align > MinABIStackAlignInBytes)
Daniel Dunbar53fac692010-04-21 19:49:55 +0000609 return ABIArgInfo::getIndirect(Align);
610 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000611}
612
Chris Lattner458b2aa2010-07-29 02:16:43 +0000613ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000614 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000615 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000616 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000617 if (const RecordType *RT = Ty->getAs<RecordType>()) {
618 // Structures with either a non-trivial destructor or a non-trivial
619 // copy constructor are always indirect.
620 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000621 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000622
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000623 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000624 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000625 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000626
627 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000628 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000629 return ABIArgInfo::getIgnore();
630
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000631 // Expand small (<= 128-bit) record types when we know that the stack layout
632 // of those arguments will match the struct. This is important because the
633 // LLVM backend isn't smart enough to remove byval, which inhibits many
634 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000635 if (getContext().getTypeSize(Ty) <= 4*32 &&
636 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000637 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000638
Chris Lattner458b2aa2010-07-29 02:16:43 +0000639 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000640 }
641
Chris Lattnerd774ae92010-08-26 20:05:13 +0000642 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000643 // On Darwin, some vectors are passed in memory, we handle this by passing
644 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000645 if (IsDarwinVectorABI) {
646 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000647 if ((Size == 8 || Size == 16 || Size == 32) ||
648 (Size == 64 && VT->getNumElements() == 1))
649 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
650 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000651 }
652
653 return ABIArgInfo::getDirect();
654 }
655
656
Chris Lattner458b2aa2010-07-29 02:16:43 +0000657 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
658 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000659
Chris Lattner458b2aa2010-07-29 02:16:43 +0000660 return (Ty->isPromotableIntegerType() ?
661 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000662}
663
664llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
665 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000666 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000667 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000668
669 CGBuilderTy &Builder = CGF.Builder;
670 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
671 "ap");
672 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
673 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000674 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000675 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
676
677 uint64_t Offset =
678 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
679 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000680 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000681 "ap.next");
682 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
683
684 return AddrTyped;
685}
686
Charles Davis4ea31ab2010-02-13 15:54:06 +0000687void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
688 llvm::GlobalValue *GV,
689 CodeGen::CodeGenModule &CGM) const {
690 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
691 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
692 // Get the LLVM function.
693 llvm::Function *Fn = cast<llvm::Function>(GV);
694
695 // Now add the 'alignstack' attribute with a value of 16.
696 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
697 }
698 }
699}
700
John McCallbeec5a02010-03-06 00:35:14 +0000701bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
702 CodeGen::CodeGenFunction &CGF,
703 llvm::Value *Address) const {
704 CodeGen::CGBuilderTy &Builder = CGF.Builder;
705 llvm::LLVMContext &Context = CGF.getLLVMContext();
706
707 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
708 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000709
John McCallbeec5a02010-03-06 00:35:14 +0000710 // 0-7 are the eight integer registers; the order is different
711 // on Darwin (for EH), but the range is the same.
712 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000713 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000714
715 if (CGF.CGM.isTargetDarwin()) {
716 // 12-16 are st(0..4). Not sure why we stop at 4.
717 // These have size 16, which is sizeof(long double) on
718 // platforms with 8-byte alignment for that type.
719 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000720 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000721
John McCallbeec5a02010-03-06 00:35:14 +0000722 } else {
723 // 9 is %eflags, which doesn't get a size on Darwin for some
724 // reason.
725 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
726
727 // 11-16 are st(0..5). Not sure why we stop at 5.
728 // These have size 12, which is sizeof(long double) on
729 // platforms with 4-byte alignment for that type.
730 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000731 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
732 }
John McCallbeec5a02010-03-06 00:35:14 +0000733
734 return false;
735}
736
Chris Lattner0cf24192010-06-28 20:05:43 +0000737//===----------------------------------------------------------------------===//
738// X86-64 ABI Implementation
739//===----------------------------------------------------------------------===//
740
741
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000742namespace {
743/// X86_64ABIInfo - The X86_64 ABI information.
744class X86_64ABIInfo : public ABIInfo {
745 enum Class {
746 Integer = 0,
747 SSE,
748 SSEUp,
749 X87,
750 X87Up,
751 ComplexX87,
752 NoClass,
753 Memory
754 };
755
756 /// merge - Implement the X86_64 ABI merging algorithm.
757 ///
758 /// Merge an accumulating classification \arg Accum with a field
759 /// classification \arg Field.
760 ///
761 /// \param Accum - The accumulating classification. This should
762 /// always be either NoClass or the result of a previous merge
763 /// call. In addition, this should never be Memory (the caller
764 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000765 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000766
767 /// classify - Determine the x86_64 register classes in which the
768 /// given type T should be passed.
769 ///
770 /// \param Lo - The classification for the parts of the type
771 /// residing in the low word of the containing object.
772 ///
773 /// \param Hi - The classification for the parts of the type
774 /// residing in the high word of the containing object.
775 ///
776 /// \param OffsetBase - The bit offset of this type in the
777 /// containing object. Some parameters are classified different
778 /// depending on whether they straddle an eightbyte boundary.
779 ///
780 /// If a word is unused its result will be NoClass; if a type should
781 /// be passed in Memory then at least the classification of \arg Lo
782 /// will be Memory.
783 ///
784 /// The \arg Lo class will be NoClass iff the argument is ignored.
785 ///
786 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
787 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000788 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000789
Chris Lattner4200fe42010-07-29 04:56:46 +0000790 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000791 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000792 unsigned IROffset, QualType SourceTy,
793 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000794 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
795 unsigned IROffset, QualType SourceTy,
796 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000797
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000798 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000799 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000800 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000801
802 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000803 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000804 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000805
Chris Lattner458b2aa2010-07-29 02:16:43 +0000806 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000807
Chris Lattner029c0f12010-07-29 04:41:05 +0000808 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
809 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000810
811public:
Chris Lattner2b037972010-07-29 02:01:43 +0000812 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000813
Chris Lattner22326a12010-07-29 02:31:05 +0000814 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000815
816 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
817 CodeGenFunction &CGF) const;
818};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000819
Chris Lattner04dc9572010-08-31 16:44:54 +0000820/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
821class WinX86_64ABIInfo : public X86_64ABIInfo {
822public:
823 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
824
825 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
826 CodeGenFunction &CGF) const;
827};
828
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000829class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
830public:
Chris Lattner2b037972010-07-29 02:01:43 +0000831 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
832 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000833
834 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
835 return 7;
836 }
837
838 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
839 llvm::Value *Address) const {
840 CodeGen::CGBuilderTy &Builder = CGF.Builder;
841 llvm::LLVMContext &Context = CGF.getLLVMContext();
842
843 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
844 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000845
John McCall943fae92010-05-27 06:19:26 +0000846 // 0-15 are the 16 integer registers.
847 // 16 is %rip.
848 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000849
850 return false;
851 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000852};
853
Chris Lattner04dc9572010-08-31 16:44:54 +0000854class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
855public:
856 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
857 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
858
859 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
860 return 7;
861 }
862
863 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
864 llvm::Value *Address) const {
865 CodeGen::CGBuilderTy &Builder = CGF.Builder;
866 llvm::LLVMContext &Context = CGF.getLLVMContext();
867
868 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
869 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
870
871 // 0-15 are the 16 integer registers.
872 // 16 is %rip.
873 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
874
875 return false;
876 }
877};
878
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000879}
880
Chris Lattnerd776fb12010-06-28 21:43:59 +0000881X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000882 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
883 // classified recursively so that always two fields are
884 // considered. The resulting class is calculated according to
885 // the classes of the fields in the eightbyte:
886 //
887 // (a) If both classes are equal, this is the resulting class.
888 //
889 // (b) If one of the classes is NO_CLASS, the resulting class is
890 // the other class.
891 //
892 // (c) If one of the classes is MEMORY, the result is the MEMORY
893 // class.
894 //
895 // (d) If one of the classes is INTEGER, the result is the
896 // INTEGER.
897 //
898 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
899 // MEMORY is used as class.
900 //
901 // (f) Otherwise class SSE is used.
902
903 // Accum should never be memory (we should have returned) or
904 // ComplexX87 (because this cannot be passed in a structure).
905 assert((Accum != Memory && Accum != ComplexX87) &&
906 "Invalid accumulated classification during merge.");
907 if (Accum == Field || Field == NoClass)
908 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000909 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000910 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000911 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000912 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000913 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000914 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000915 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
916 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000917 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000918 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000919}
920
Chris Lattner5c740f12010-06-30 19:14:05 +0000921void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000922 Class &Lo, Class &Hi) const {
923 // FIXME: This code can be simplified by introducing a simple value class for
924 // Class pairs with appropriate constructor methods for the various
925 // situations.
926
927 // FIXME: Some of the split computations are wrong; unaligned vectors
928 // shouldn't be passed in registers for example, so there is no chance they
929 // can straddle an eightbyte. Verify & simplify.
930
931 Lo = Hi = NoClass;
932
933 Class &Current = OffsetBase < 64 ? Lo : Hi;
934 Current = Memory;
935
John McCall9dd450b2009-09-21 23:43:11 +0000936 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000937 BuiltinType::Kind k = BT->getKind();
938
939 if (k == BuiltinType::Void) {
940 Current = NoClass;
941 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
942 Lo = Integer;
943 Hi = Integer;
944 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
945 Current = Integer;
946 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
947 Current = SSE;
948 } else if (k == BuiltinType::LongDouble) {
949 Lo = X87;
950 Hi = X87Up;
951 }
952 // FIXME: _Decimal32 and _Decimal64 are SSE.
953 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000954 return;
955 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000956
Chris Lattnerd776fb12010-06-28 21:43:59 +0000957 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000958 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000959 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000960 return;
961 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000962
Chris Lattnerd776fb12010-06-28 21:43:59 +0000963 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000964 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000965 return;
966 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000967
Chris Lattnerd776fb12010-06-28 21:43:59 +0000968 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000969 if (Ty->isMemberFunctionPointerType())
970 Lo = Hi = Integer;
971 else
972 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000973 return;
974 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000975
Chris Lattnerd776fb12010-06-28 21:43:59 +0000976 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000977 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000978 if (Size == 32) {
979 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
980 // float> as integer.
981 Current = Integer;
982
983 // If this type crosses an eightbyte boundary, it should be
984 // split.
985 uint64_t EB_Real = (OffsetBase) / 64;
986 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
987 if (EB_Real != EB_Imag)
988 Hi = Lo;
989 } else if (Size == 64) {
990 // gcc passes <1 x double> in memory. :(
991 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
992 return;
993
994 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +0000995 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +0000996 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
997 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
998 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000999 Current = Integer;
1000 else
1001 Current = SSE;
1002
1003 // If this type crosses an eightbyte boundary, it should be
1004 // split.
1005 if (OffsetBase && OffsetBase != 64)
1006 Hi = Lo;
1007 } else if (Size == 128) {
1008 Lo = SSE;
1009 Hi = SSEUp;
1010 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001011 return;
1012 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001013
Chris Lattnerd776fb12010-06-28 21:43:59 +00001014 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001015 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001016
Chris Lattner2b037972010-07-29 02:01:43 +00001017 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001018 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001019 if (Size <= 64)
1020 Current = Integer;
1021 else if (Size <= 128)
1022 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001023 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001024 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001025 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001026 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001027 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001028 Current = ComplexX87;
1029
1030 // If this complex type crosses an eightbyte boundary then it
1031 // should be split.
1032 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001033 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001034 if (Hi == NoClass && EB_Real != EB_Imag)
1035 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001036
Chris Lattnerd776fb12010-06-28 21:43:59 +00001037 return;
1038 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001039
Chris Lattner2b037972010-07-29 02:01:43 +00001040 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001041 // Arrays are treated like structures.
1042
Chris Lattner2b037972010-07-29 02:01:43 +00001043 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001044
1045 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1046 // than two eightbytes, ..., it has class MEMORY.
1047 if (Size > 128)
1048 return;
1049
1050 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1051 // fields, it has class MEMORY.
1052 //
1053 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001054 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001055 return;
1056
1057 // Otherwise implement simplified merge. We could be smarter about
1058 // this, but it isn't worth it and would be harder to verify.
1059 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001060 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001061 uint64_t ArraySize = AT->getSize().getZExtValue();
1062 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1063 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001064 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001065 Lo = merge(Lo, FieldLo);
1066 Hi = merge(Hi, FieldHi);
1067 if (Lo == Memory || Hi == Memory)
1068 break;
1069 }
1070
1071 // Do post merger cleanup (see below). Only case we worry about is Memory.
1072 if (Hi == Memory)
1073 Lo = Memory;
1074 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001075 return;
1076 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001077
Chris Lattnerd776fb12010-06-28 21:43:59 +00001078 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001079 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001080
1081 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1082 // than two eightbytes, ..., it has class MEMORY.
1083 if (Size > 128)
1084 return;
1085
Anders Carlsson20759ad2009-09-16 15:53:40 +00001086 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1087 // copy constructor or a non-trivial destructor, it is passed by invisible
1088 // reference.
1089 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1090 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001091
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001092 const RecordDecl *RD = RT->getDecl();
1093
1094 // Assume variable sized types are passed in memory.
1095 if (RD->hasFlexibleArrayMember())
1096 return;
1097
Chris Lattner2b037972010-07-29 02:01:43 +00001098 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001099
1100 // Reset Lo class, this will be recomputed.
1101 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001102
1103 // If this is a C++ record, classify the bases first.
1104 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1105 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1106 e = CXXRD->bases_end(); i != e; ++i) {
1107 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1108 "Unexpected base class!");
1109 const CXXRecordDecl *Base =
1110 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1111
1112 // Classify this field.
1113 //
1114 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1115 // single eightbyte, each is classified separately. Each eightbyte gets
1116 // initialized to class NO_CLASS.
1117 Class FieldLo, FieldHi;
1118 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001119 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001120 Lo = merge(Lo, FieldLo);
1121 Hi = merge(Hi, FieldHi);
1122 if (Lo == Memory || Hi == Memory)
1123 break;
1124 }
1125 }
1126
1127 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001128 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001129 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1130 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001131 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1132 bool BitField = i->isBitField();
1133
1134 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1135 // fields, it has class MEMORY.
1136 //
1137 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001138 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001139 Lo = Memory;
1140 return;
1141 }
1142
1143 // Classify this field.
1144 //
1145 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1146 // exceeds a single eightbyte, each is classified
1147 // separately. Each eightbyte gets initialized to class
1148 // NO_CLASS.
1149 Class FieldLo, FieldHi;
1150
1151 // Bit-fields require special handling, they do not force the
1152 // structure to be passed in memory even if unaligned, and
1153 // therefore they can straddle an eightbyte.
1154 if (BitField) {
1155 // Ignore padding bit-fields.
1156 if (i->isUnnamedBitfield())
1157 continue;
1158
1159 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001160 uint64_t Size =
1161 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001162
1163 uint64_t EB_Lo = Offset / 64;
1164 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1165 FieldLo = FieldHi = NoClass;
1166 if (EB_Lo) {
1167 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1168 FieldLo = NoClass;
1169 FieldHi = Integer;
1170 } else {
1171 FieldLo = Integer;
1172 FieldHi = EB_Hi ? Integer : NoClass;
1173 }
1174 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001175 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001176 Lo = merge(Lo, FieldLo);
1177 Hi = merge(Hi, FieldHi);
1178 if (Lo == Memory || Hi == Memory)
1179 break;
1180 }
1181
1182 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1183 //
1184 // (a) If one of the classes is MEMORY, the whole argument is
1185 // passed in memory.
1186 //
1187 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1188
1189 // The first of these conditions is guaranteed by how we implement
1190 // the merge (just bail).
1191 //
1192 // The second condition occurs in the case of unions; for example
1193 // union { _Complex double; unsigned; }.
1194 if (Hi == Memory)
1195 Lo = Memory;
1196 if (Hi == SSEUp && Lo != SSE)
1197 Hi = SSE;
1198 }
1199}
1200
Chris Lattner22a931e2010-06-29 06:01:59 +00001201ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001202 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1203 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001204 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001205 // Treat an enum type as its underlying type.
1206 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1207 Ty = EnumTy->getDecl()->getIntegerType();
1208
1209 return (Ty->isPromotableIntegerType() ?
1210 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1211 }
1212
1213 return ABIArgInfo::getIndirect(0);
1214}
1215
Chris Lattner22a931e2010-06-29 06:01:59 +00001216ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001217 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1218 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001219 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001220 // Treat an enum type as its underlying type.
1221 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1222 Ty = EnumTy->getDecl()->getIntegerType();
1223
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001224 return (Ty->isPromotableIntegerType() ?
1225 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001226 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001227
Daniel Dunbar53fac692010-04-21 19:49:55 +00001228 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1229 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001230
Daniel Dunbar53fac692010-04-21 19:49:55 +00001231 // Compute the byval alignment. We trust the back-end to honor the
1232 // minimum ABI alignment for byval, to make cleaner IR.
1233 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001234 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001235 if (Align > MinABIAlign)
1236 return ABIArgInfo::getIndirect(Align);
1237 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001238}
1239
Chris Lattner4200fe42010-07-29 04:56:46 +00001240/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1241/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1242/// vector register.
1243const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001244 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001245
Chris Lattner9fa15c32010-07-29 05:02:29 +00001246 // Wrapper structs that just contain vectors are passed just like vectors,
1247 // strip them off if present.
1248 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1249 while (STy && STy->getNumElements() == 1) {
1250 IRType = STy->getElementType(0);
1251 STy = dyn_cast<llvm::StructType>(IRType);
1252 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001253
Chris Lattner4200fe42010-07-29 04:56:46 +00001254 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001255 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001256 const llvm::Type *EltTy = VT->getElementType();
1257 if (VT->getBitWidth() == 128 &&
1258 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1259 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1260 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1261 EltTy->isIntegerTy(128)))
1262 return VT;
1263 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001264
Chris Lattner4200fe42010-07-29 04:56:46 +00001265 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1266}
1267
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001268/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1269/// is known to either be off the end of the specified type or being in
1270/// alignment padding. The user type specified is known to be at most 128 bits
1271/// in size, and have passed through X86_64ABIInfo::classify with a successful
1272/// classification that put one of the two halves in the INTEGER class.
1273///
1274/// It is conservatively correct to return false.
1275static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1276 unsigned EndBit, ASTContext &Context) {
1277 // If the bytes being queried are off the end of the type, there is no user
1278 // data hiding here. This handles analysis of builtins, vectors and other
1279 // types that don't contain interesting padding.
1280 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1281 if (TySize <= StartBit)
1282 return true;
1283
Chris Lattner98076a22010-07-29 07:43:55 +00001284 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1285 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1286 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1287
1288 // Check each element to see if the element overlaps with the queried range.
1289 for (unsigned i = 0; i != NumElts; ++i) {
1290 // If the element is after the span we care about, then we're done..
1291 unsigned EltOffset = i*EltSize;
1292 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001293
Chris Lattner98076a22010-07-29 07:43:55 +00001294 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1295 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1296 EndBit-EltOffset, Context))
1297 return false;
1298 }
1299 // If it overlaps no elements, then it is safe to process as padding.
1300 return true;
1301 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001302
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001303 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1304 const RecordDecl *RD = RT->getDecl();
1305 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001306
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001307 // If this is a C++ record, check the bases first.
1308 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1309 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1310 e = CXXRD->bases_end(); i != e; ++i) {
1311 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1312 "Unexpected base class!");
1313 const CXXRecordDecl *Base =
1314 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001315
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001316 // If the base is after the span we care about, ignore it.
1317 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1318 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001319
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001320 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1321 if (!BitsContainNoUserData(i->getType(), BaseStart,
1322 EndBit-BaseOffset, Context))
1323 return false;
1324 }
1325 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001326
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001327 // Verify that no field has data that overlaps the region of interest. Yes
1328 // this could be sped up a lot by being smarter about queried fields,
1329 // however we're only looking at structs up to 16 bytes, so we don't care
1330 // much.
1331 unsigned idx = 0;
1332 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1333 i != e; ++i, ++idx) {
1334 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001335
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001336 // If we found a field after the region we care about, then we're done.
1337 if (FieldOffset >= EndBit) break;
1338
1339 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1340 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1341 Context))
1342 return false;
1343 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001344
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001345 // If nothing in this record overlapped the area of interest, then we're
1346 // clean.
1347 return true;
1348 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001349
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001350 return false;
1351}
1352
Chris Lattnere556a712010-07-29 18:39:32 +00001353/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1354/// float member at the specified offset. For example, {int,{float}} has a
1355/// float at offset 4. It is conservatively correct for this routine to return
1356/// false.
1357static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1358 const llvm::TargetData &TD) {
1359 // Base case if we find a float.
1360 if (IROffset == 0 && IRType->isFloatTy())
1361 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001362
Chris Lattnere556a712010-07-29 18:39:32 +00001363 // If this is a struct, recurse into the field at the specified offset.
1364 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1365 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1366 unsigned Elt = SL->getElementContainingOffset(IROffset);
1367 IROffset -= SL->getElementOffset(Elt);
1368 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1369 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001370
Chris Lattnere556a712010-07-29 18:39:32 +00001371 // If this is an array, recurse into the field at the specified offset.
1372 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1373 const llvm::Type *EltTy = ATy->getElementType();
1374 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1375 IROffset -= IROffset/EltSize*EltSize;
1376 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1377 }
1378
1379 return false;
1380}
1381
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001382
1383/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1384/// low 8 bytes of an XMM register, corresponding to the SSE class.
1385const llvm::Type *X86_64ABIInfo::
1386GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1387 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001388 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001389 // pass as float if the last 4 bytes is just padding. This happens for
1390 // structs that contain 3 floats.
1391 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1392 SourceOffset*8+64, getContext()))
1393 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001394
Chris Lattnere556a712010-07-29 18:39:32 +00001395 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1396 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1397 // case.
1398 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001399 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1400 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001401
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001402 return llvm::Type::getDoubleTy(getVMContext());
1403}
1404
1405
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001406/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1407/// an 8-byte GPR. This means that we either have a scalar or we are talking
1408/// about the high or low part of an up-to-16-byte struct. This routine picks
1409/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001410/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1411/// etc).
1412///
1413/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1414/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1415/// the 8-byte value references. PrefType may be null.
1416///
1417/// SourceTy is the source level type for the entire argument. SourceOffset is
1418/// an offset into this that we're processing (which is always either 0 or 8).
1419///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001420const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001421GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1422 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001423 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1424 // returning an 8-byte unit starting with it. See if we can safely use it.
1425 if (IROffset == 0) {
1426 // Pointers and int64's always fill the 8-byte unit.
1427 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1428 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001429
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001430 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1431 // goodness in the source type is just tail padding. This is allowed to
1432 // kick in for struct {double,int} on the int, but not on
1433 // struct{double,int,int} because we wouldn't return the second int. We
1434 // have to do this analysis on the source type because we can't depend on
1435 // unions being lowered a specific way etc.
1436 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1437 IRType->isIntegerTy(32)) {
1438 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001439
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001440 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1441 SourceOffset*8+64, getContext()))
1442 return IRType;
1443 }
1444 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001445
Chris Lattnerce1bd752010-07-29 04:51:12 +00001446 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001447 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001448 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001449 if (IROffset < SL->getSizeInBytes()) {
1450 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1451 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001452
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001453 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1454 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001455 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001456 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001457
Chris Lattner98076a22010-07-29 07:43:55 +00001458 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1459 const llvm::Type *EltTy = ATy->getElementType();
1460 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1461 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001462 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1463 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001464 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001465
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001466 // Okay, we don't have any better idea of what to pass, so we pass this in an
1467 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001468 unsigned TySizeInBytes =
1469 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001470
Chris Lattner3f763422010-07-29 17:34:39 +00001471 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001472
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001473 // It is always safe to classify this as an integer type up to i64 that
1474 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001475 return llvm::IntegerType::get(getVMContext(),
1476 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001477}
1478
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001479
1480/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1481/// be used as elements of a two register pair to pass or return, return a
1482/// first class aggregate to represent them. For example, if the low part of
1483/// a by-value argument should be passed as i32* and the high part as float,
1484/// return {i32*, float}.
1485static const llvm::Type *
1486GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1487 const llvm::TargetData &TD) {
1488 // In order to correctly satisfy the ABI, we need to the high part to start
1489 // at offset 8. If the high and low parts we inferred are both 4-byte types
1490 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1491 // the second element at offset 8. Check for this:
1492 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1493 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1494 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1495 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
1496
1497 // To handle this, we have to increase the size of the low part so that the
1498 // second element will start at an 8 byte offset. We can't increase the size
1499 // of the second element because it might make us access off the end of the
1500 // struct.
1501 if (HiStart != 8) {
1502 // There are only two sorts of types the ABI generation code can produce for
1503 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1504 // Promote these to a larger type.
1505 if (Lo->isFloatTy())
1506 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1507 else {
1508 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1509 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1510 }
1511 }
1512
1513 const llvm::StructType *Result =
1514 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
1515
1516
1517 // Verify that the second element is at an 8-byte offset.
1518 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1519 "Invalid x86-64 argument pair!");
1520 return Result;
1521}
1522
Chris Lattner31faff52010-07-28 23:06:14 +00001523ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001524classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001525 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1526 // classification algorithm.
1527 X86_64ABIInfo::Class Lo, Hi;
1528 classify(RetTy, 0, Lo, Hi);
1529
1530 // Check some invariants.
1531 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001532 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1533
1534 const llvm::Type *ResType = 0;
1535 switch (Lo) {
1536 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001537 if (Hi == NoClass)
1538 return ABIArgInfo::getIgnore();
1539 // If the low part is just padding, it takes no register, leave ResType
1540 // null.
1541 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1542 "Unknown missing lo part");
1543 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001544
1545 case SSEUp:
1546 case X87Up:
1547 assert(0 && "Invalid classification for lo word.");
1548
1549 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1550 // hidden argument.
1551 case Memory:
1552 return getIndirectReturnResult(RetTy);
1553
1554 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1555 // available register of the sequence %rax, %rdx is used.
1556 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001557 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1558 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001559
Chris Lattner1f3a0632010-07-29 21:42:50 +00001560 // If we have a sign or zero extended integer, make sure to return Extend
1561 // so that the parameter gets the right LLVM IR attributes.
1562 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1563 // Treat an enum type as its underlying type.
1564 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1565 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001566
Chris Lattner1f3a0632010-07-29 21:42:50 +00001567 if (RetTy->isIntegralOrEnumerationType() &&
1568 RetTy->isPromotableIntegerType())
1569 return ABIArgInfo::getExtend();
1570 }
Chris Lattner31faff52010-07-28 23:06:14 +00001571 break;
1572
1573 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1574 // available SSE register of the sequence %xmm0, %xmm1 is used.
1575 case SSE:
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001576 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001577 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001578
1579 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1580 // returned on the X87 stack in %st0 as 80-bit x87 number.
1581 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001582 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001583 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001584
1585 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1586 // part of the value is returned in %st0 and the imaginary part in
1587 // %st1.
1588 case ComplexX87:
1589 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001590 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001591 llvm::Type::getX86_FP80Ty(getVMContext()),
1592 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001593 NULL);
1594 break;
1595 }
1596
Chris Lattner52b3c132010-09-01 00:20:33 +00001597 const llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001598 switch (Hi) {
1599 // Memory was handled previously and X87 should
1600 // never occur as a hi class.
1601 case Memory:
1602 case X87:
1603 assert(0 && "Invalid classification for hi word.");
1604
1605 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001606 case NoClass:
1607 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001608
Chris Lattner52b3c132010-09-01 00:20:33 +00001609 case Integer:
1610 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1611 8, RetTy, 8);
1612 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1613 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001614 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001615 case SSE:
1616 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1617 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1618 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001619 break;
1620
1621 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1622 // is passed in the upper half of the last used SSE register.
1623 //
1624 // SSEUP should always be preceeded by SSE, just widen.
1625 case SSEUp:
1626 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001627 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001628 break;
1629
1630 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1631 // returned together with the previous X87 value in %st0.
1632 case X87Up:
1633 // If X87Up is preceeded by X87, we don't need to do
1634 // anything. However, in some cases with unions it may not be
1635 // preceeded by X87. In such situations we follow gcc and pass the
1636 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001637 if (Lo != X87) {
Chris Lattner52b3c132010-09-01 00:20:33 +00001638 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1639 8, RetTy, 8);
1640 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1641 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001642 }
Chris Lattner31faff52010-07-28 23:06:14 +00001643 break;
1644 }
Chris Lattner52b3c132010-09-01 00:20:33 +00001645
1646 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001647 // known to pass in the high eightbyte of the result. We do this by forming a
1648 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001649 if (HighPart)
1650 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001651
Chris Lattner1f3a0632010-07-29 21:42:50 +00001652 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001653}
1654
Chris Lattner458b2aa2010-07-29 02:16:43 +00001655ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001656 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001657 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001658 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001659
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001660 // Check some invariants.
1661 // FIXME: Enforce these by construction.
1662 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001663 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1664
1665 neededInt = 0;
1666 neededSSE = 0;
1667 const llvm::Type *ResType = 0;
1668 switch (Lo) {
1669 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001670 if (Hi == NoClass)
1671 return ABIArgInfo::getIgnore();
1672 // If the low part is just padding, it takes no register, leave ResType
1673 // null.
1674 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1675 "Unknown missing lo part");
1676 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001677
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001678 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1679 // on the stack.
1680 case Memory:
1681
1682 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1683 // COMPLEX_X87, it is passed in memory.
1684 case X87:
1685 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001686 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001687
1688 case SSEUp:
1689 case X87Up:
1690 assert(0 && "Invalid classification for lo word.");
1691
1692 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1693 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1694 // and %r9 is used.
1695 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001696 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001697
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001698 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001699 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001700
1701 // If we have a sign or zero extended integer, make sure to return Extend
1702 // so that the parameter gets the right LLVM IR attributes.
1703 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1704 // Treat an enum type as its underlying type.
1705 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1706 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001707
Chris Lattner1f3a0632010-07-29 21:42:50 +00001708 if (Ty->isIntegralOrEnumerationType() &&
1709 Ty->isPromotableIntegerType())
1710 return ABIArgInfo::getExtend();
1711 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001712
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001713 break;
1714
1715 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1716 // available SSE register is used, the registers are taken in the
1717 // order from %xmm0 to %xmm7.
1718 case SSE:
1719 ++neededSSE;
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001720 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001721 break;
1722 }
1723
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001724 const llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001725 switch (Hi) {
1726 // Memory was handled previously, ComplexX87 and X87 should
1727 // never occur as hi classes, and X87Up must be preceed by X87,
1728 // which is passed in memory.
1729 case Memory:
1730 case X87:
1731 case ComplexX87:
1732 assert(0 && "Invalid classification for hi word.");
1733 break;
1734
1735 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001736
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001737 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001738 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001739 // Pick an 8-byte type based on the preferred type.
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001740 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001741
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001742 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1743 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001744 break;
1745
1746 // X87Up generally doesn't occur here (long double is passed in
1747 // memory), except in situations involving unions.
1748 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001749 case SSE:
1750 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001751
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001752 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1753 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001754
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001755 ++neededSSE;
1756 break;
1757
1758 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1759 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001760 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001761 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001762 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001763 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001764 break;
1765 }
1766
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001767 // If a high part was specified, merge it together with the low part. It is
1768 // known to pass in the high eightbyte of the result. We do this by forming a
1769 // first class struct aggregate with the high and low part: {low, high}
1770 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001771 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001772
Chris Lattner1f3a0632010-07-29 21:42:50 +00001773 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001774}
1775
Chris Lattner22326a12010-07-29 02:31:05 +00001776void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001777
Chris Lattner458b2aa2010-07-29 02:16:43 +00001778 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001779
1780 // Keep track of the number of assigned registers.
1781 unsigned freeIntRegs = 6, freeSSERegs = 8;
1782
1783 // If the return value is indirect, then the hidden argument is consuming one
1784 // integer register.
1785 if (FI.getReturnInfo().isIndirect())
1786 --freeIntRegs;
1787
1788 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1789 // get assigned (in left-to-right order) for passing as follows...
1790 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1791 it != ie; ++it) {
1792 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001793 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001794
1795 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1796 // eightbyte of an argument, the whole argument is passed on the
1797 // stack. If registers have already been assigned for some
1798 // eightbytes of such an argument, the assignments get reverted.
1799 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1800 freeIntRegs -= neededInt;
1801 freeSSERegs -= neededSSE;
1802 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001803 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001804 }
1805 }
1806}
1807
1808static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1809 QualType Ty,
1810 CodeGenFunction &CGF) {
1811 llvm::Value *overflow_arg_area_p =
1812 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1813 llvm::Value *overflow_arg_area =
1814 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1815
1816 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1817 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1818 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1819 if (Align > 8) {
1820 // Note that we follow the ABI & gcc here, even though the type
1821 // could in theory have an alignment greater than 16. This case
1822 // shouldn't ever matter in practice.
1823
1824 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001825 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001826 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001827 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1828 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001829 CGF.Int64Ty);
1830 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001831 overflow_arg_area =
1832 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1833 overflow_arg_area->getType(),
1834 "overflow_arg_area.align");
1835 }
1836
1837 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1838 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1839 llvm::Value *Res =
1840 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001841 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001842
1843 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1844 // l->overflow_arg_area + sizeof(type).
1845 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1846 // an 8 byte boundary.
1847
1848 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001849 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001850 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001851 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1852 "overflow_arg_area.next");
1853 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1854
1855 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1856 return Res;
1857}
1858
1859llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1860 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001861 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001862
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001863 // Assume that va_list type is correct; should be pointer to LLVM type:
1864 // struct {
1865 // i32 gp_offset;
1866 // i32 fp_offset;
1867 // i8* overflow_arg_area;
1868 // i8* reg_save_area;
1869 // };
1870 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001871
Chris Lattner9723d6c2010-03-11 18:19:55 +00001872 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001873 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001874
1875 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1876 // in the registers. If not go to step 7.
1877 if (!neededInt && !neededSSE)
1878 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1879
1880 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1881 // general purpose registers needed to pass type and num_fp to hold
1882 // the number of floating point registers needed.
1883
1884 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1885 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1886 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1887 //
1888 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1889 // register save space).
1890
1891 llvm::Value *InRegs = 0;
1892 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1893 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1894 if (neededInt) {
1895 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1896 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001897 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1898 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001899 }
1900
1901 if (neededSSE) {
1902 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1903 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1904 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001905 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1906 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001907 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1908 }
1909
1910 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1911 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1912 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1913 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1914
1915 // Emit code to load the value if it was passed in registers.
1916
1917 CGF.EmitBlock(InRegBlock);
1918
1919 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1920 // an offset of l->gp_offset and/or l->fp_offset. This may require
1921 // copying to a temporary location in case the parameter is passed
1922 // in different register classes or requires an alignment greater
1923 // than 8 for general purpose registers and 16 for XMM registers.
1924 //
1925 // FIXME: This really results in shameful code when we end up needing to
1926 // collect arguments from different places; often what should result in a
1927 // simple assembling of a structure from scattered addresses has many more
1928 // loads than necessary. Can we clean this up?
1929 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1930 llvm::Value *RegAddr =
1931 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1932 "reg_save_area");
1933 if (neededInt && neededSSE) {
1934 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001935 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001936 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1937 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1938 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1939 const llvm::Type *TyLo = ST->getElementType(0);
1940 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00001941 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001942 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001943 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1944 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001945 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1946 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001947 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1948 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001949 llvm::Value *V =
1950 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1951 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1952 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1953 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1954
Owen Anderson170229f2009-07-14 23:10:40 +00001955 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001956 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001957 } else if (neededInt) {
1958 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1959 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001960 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001961 } else if (neededSSE == 1) {
1962 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1963 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1964 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001965 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001966 assert(neededSSE == 2 && "Invalid number of needed registers!");
1967 // SSE registers are spaced 16 bytes apart in the register save
1968 // area, we need to collect the two eightbytes together.
1969 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001970 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001971 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1972 const llvm::Type *DblPtrTy =
1973 llvm::PointerType::getUnqual(DoubleTy);
1974 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1975 DoubleTy, NULL);
1976 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1977 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1978 DblPtrTy));
1979 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1980 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1981 DblPtrTy));
1982 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1983 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1984 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001985 }
1986
1987 // AMD64-ABI 3.5.7p5: Step 5. Set:
1988 // l->gp_offset = l->gp_offset + num_gp * 8
1989 // l->fp_offset = l->fp_offset + num_fp * 16.
1990 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001991 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001992 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1993 gp_offset_p);
1994 }
1995 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001996 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001997 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1998 fp_offset_p);
1999 }
2000 CGF.EmitBranch(ContBlock);
2001
2002 // Emit code to load the value if it was passed in memory.
2003
2004 CGF.EmitBlock(InMemBlock);
2005 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2006
2007 // Return the appropriate result.
2008
2009 CGF.EmitBlock(ContBlock);
2010 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2011 "vaarg.addr");
2012 ResAddr->reserveOperandSpace(2);
2013 ResAddr->addIncoming(RegAddr, InRegBlock);
2014 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002015 return ResAddr;
2016}
2017
Chris Lattner04dc9572010-08-31 16:44:54 +00002018llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2019 CodeGenFunction &CGF) const {
2020 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2021 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002022
Chris Lattner04dc9572010-08-31 16:44:54 +00002023 CGBuilderTy &Builder = CGF.Builder;
2024 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2025 "ap");
2026 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2027 llvm::Type *PTy =
2028 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2029 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2030
2031 uint64_t Offset =
2032 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2033 llvm::Value *NextAddr =
2034 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2035 "ap.next");
2036 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2037
2038 return AddrTyped;
2039}
Chris Lattner0cf24192010-06-28 20:05:43 +00002040
2041//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002042// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002043//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002044
2045namespace {
2046
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002047class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002048public:
2049 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002050
Chris Lattner458b2aa2010-07-29 02:16:43 +00002051 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002052
Chris Lattner458b2aa2010-07-29 02:16:43 +00002053 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002054
Chris Lattner22326a12010-07-29 02:31:05 +00002055 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002056 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002057 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2058 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002059 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002060 }
2061
2062 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2063 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002064};
2065
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002066class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
2067public:
Chris Lattner2b037972010-07-29 02:01:43 +00002068 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
2069 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002070};
2071
Daniel Dunbard59655c2009-09-12 00:59:49 +00002072}
2073
Chris Lattner458b2aa2010-07-29 02:16:43 +00002074ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002075 if (RetTy->isVoidType()) {
2076 return ABIArgInfo::getIgnore();
2077 } else {
2078 return ABIArgInfo::getDirect();
2079 }
2080}
2081
Chris Lattner458b2aa2010-07-29 02:16:43 +00002082ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002083 return ABIArgInfo::getDirect();
2084}
2085
2086llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002087 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00002088 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00002089 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
2090
2091 CGBuilderTy &Builder = CGF.Builder;
2092 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2093 "ap");
2094 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2095 llvm::Type *PTy =
2096 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2097 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2098
2099 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
2100
2101 llvm::Value *NextAddr =
2102 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
2103 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
2104 "ap.next");
2105 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2106
2107 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002108}
2109
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00002110
John McCallea8d8bb2010-03-11 00:10:12 +00002111// PowerPC-32
2112
2113namespace {
2114class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2115public:
Chris Lattner2b037972010-07-29 02:01:43 +00002116 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002117
John McCallea8d8bb2010-03-11 00:10:12 +00002118 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2119 // This is recovered from gcc output.
2120 return 1; // r1 is the dedicated stack pointer
2121 }
2122
2123 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002124 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002125};
2126
2127}
2128
2129bool
2130PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2131 llvm::Value *Address) const {
2132 // This is calculated from the LLVM and GCC tables and verified
2133 // against gcc output. AFAIK all ABIs use the same encoding.
2134
2135 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2136 llvm::LLVMContext &Context = CGF.getLLVMContext();
2137
2138 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2139 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2140 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2141 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2142
2143 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002144 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002145
2146 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002147 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002148
2149 // 64-76 are various 4-byte special-purpose registers:
2150 // 64: mq
2151 // 65: lr
2152 // 66: ctr
2153 // 67: ap
2154 // 68-75 cr0-7
2155 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002156 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002157
2158 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002159 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002160
2161 // 109: vrsave
2162 // 110: vscr
2163 // 111: spe_acc
2164 // 112: spefscr
2165 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002166 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002167
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002168 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002169}
2170
2171
Chris Lattner0cf24192010-06-28 20:05:43 +00002172//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002173// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002174//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002175
2176namespace {
2177
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002178class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002179public:
2180 enum ABIKind {
2181 APCS = 0,
2182 AAPCS = 1,
2183 AAPCS_VFP
2184 };
2185
2186private:
2187 ABIKind Kind;
2188
2189public:
Chris Lattner2b037972010-07-29 02:01:43 +00002190 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002191
2192private:
2193 ABIKind getABIKind() const { return Kind; }
2194
Chris Lattner458b2aa2010-07-29 02:16:43 +00002195 ABIArgInfo classifyReturnType(QualType RetTy) const;
2196 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002197
Chris Lattner22326a12010-07-29 02:31:05 +00002198 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002199
2200 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2201 CodeGenFunction &CGF) const;
2202};
2203
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002204class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2205public:
Chris Lattner2b037972010-07-29 02:01:43 +00002206 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2207 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002208
2209 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2210 return 13;
2211 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002212};
2213
Daniel Dunbard59655c2009-09-12 00:59:49 +00002214}
2215
Chris Lattner22326a12010-07-29 02:31:05 +00002216void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002217 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002218 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002219 it != ie; ++it)
2220 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002221
Chris Lattner458b2aa2010-07-29 02:16:43 +00002222 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002223 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002224 if (Triple.getEnvironmentName() == "gnueabi" ||
2225 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002226 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002227 else
2228 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002229
Daniel Dunbar020daa92009-09-12 01:00:39 +00002230 switch (getABIKind()) {
2231 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002232 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2233 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002234 break;
2235
2236 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002237 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2238 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002239 break;
2240
2241 case AAPCS_VFP:
2242 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2243 break;
2244 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002245}
2246
Chris Lattner458b2aa2010-07-29 02:16:43 +00002247ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002248 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002249 // Treat an enum type as its underlying type.
2250 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2251 Ty = EnumTy->getDecl()->getIntegerType();
2252
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002253 return (Ty->isPromotableIntegerType() ?
2254 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002255 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002256
Daniel Dunbar09d33622009-09-14 21:54:03 +00002257 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002258 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002259 return ABIArgInfo::getIgnore();
2260
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002261 // Structures with either a non-trivial destructor or a non-trivial
2262 // copy constructor are always indirect.
2263 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2264 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2265
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002266 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2267 // backend doesn't support byval.
2268 // FIXME: This doesn't handle alignment > 64 bits.
2269 const llvm::Type* ElemTy;
2270 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002271 if (getContext().getTypeAlign(Ty) > 32) {
2272 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2273 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002274 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002275 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2276 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002277 }
2278 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002279 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002280 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2281 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002282 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002283}
2284
Chris Lattner458b2aa2010-07-29 02:16:43 +00002285static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002286 llvm::LLVMContext &VMContext) {
2287 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2288 // is called integer-like if its size is less than or equal to one word, and
2289 // the offset of each of its addressable sub-fields is zero.
2290
2291 uint64_t Size = Context.getTypeSize(Ty);
2292
2293 // Check that the type fits in a word.
2294 if (Size > 32)
2295 return false;
2296
2297 // FIXME: Handle vector types!
2298 if (Ty->isVectorType())
2299 return false;
2300
Daniel Dunbard53bac72009-09-14 02:20:34 +00002301 // Float types are never treated as "integer like".
2302 if (Ty->isRealFloatingType())
2303 return false;
2304
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002305 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002306 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002307 return true;
2308
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002309 // Small complex integer types are "integer like".
2310 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2311 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002312
2313 // Single element and zero sized arrays should be allowed, by the definition
2314 // above, but they are not.
2315
2316 // Otherwise, it must be a record type.
2317 const RecordType *RT = Ty->getAs<RecordType>();
2318 if (!RT) return false;
2319
2320 // Ignore records with flexible arrays.
2321 const RecordDecl *RD = RT->getDecl();
2322 if (RD->hasFlexibleArrayMember())
2323 return false;
2324
2325 // Check that all sub-fields are at offset 0, and are themselves "integer
2326 // like".
2327 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2328
2329 bool HadField = false;
2330 unsigned idx = 0;
2331 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2332 i != e; ++i, ++idx) {
2333 const FieldDecl *FD = *i;
2334
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002335 // Bit-fields are not addressable, we only need to verify they are "integer
2336 // like". We still have to disallow a subsequent non-bitfield, for example:
2337 // struct { int : 0; int x }
2338 // is non-integer like according to gcc.
2339 if (FD->isBitField()) {
2340 if (!RD->isUnion())
2341 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002342
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002343 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2344 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002345
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002346 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002347 }
2348
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002349 // Check if this field is at offset 0.
2350 if (Layout.getFieldOffset(idx) != 0)
2351 return false;
2352
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002353 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2354 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002355
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002356 // Only allow at most one field in a structure. This doesn't match the
2357 // wording above, but follows gcc in situations with a field following an
2358 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002359 if (!RD->isUnion()) {
2360 if (HadField)
2361 return false;
2362
2363 HadField = true;
2364 }
2365 }
2366
2367 return true;
2368}
2369
Chris Lattner458b2aa2010-07-29 02:16:43 +00002370ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002371 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002372 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002373
John McCalla1dee5302010-08-22 10:59:02 +00002374 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002375 // Treat an enum type as its underlying type.
2376 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2377 RetTy = EnumTy->getDecl()->getIntegerType();
2378
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002379 return (RetTy->isPromotableIntegerType() ?
2380 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002381 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002382
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002383 // Structures with either a non-trivial destructor or a non-trivial
2384 // copy constructor are always indirect.
2385 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2386 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2387
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002388 // Are we following APCS?
2389 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002390 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002391 return ABIArgInfo::getIgnore();
2392
Daniel Dunbareedf1512010-02-01 23:31:19 +00002393 // Complex types are all returned as packed integers.
2394 //
2395 // FIXME: Consider using 2 x vector types if the back end handles them
2396 // correctly.
2397 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002398 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002399 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002400
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002401 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002402 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002403 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002404 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002405 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002406 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002407 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002408 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2409 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002410 }
2411
2412 // Otherwise return in memory.
2413 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002414 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002415
2416 // Otherwise this is an AAPCS variant.
2417
Chris Lattner458b2aa2010-07-29 02:16:43 +00002418 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002419 return ABIArgInfo::getIgnore();
2420
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002421 // Aggregates <= 4 bytes are returned in r0; other aggregates
2422 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002423 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002424 if (Size <= 32) {
2425 // Return in the smallest viable integer type.
2426 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002427 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002428 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002429 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2430 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002431 }
2432
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002433 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002434}
2435
2436llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002437 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002438 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002439 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002440 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002441
2442 CGBuilderTy &Builder = CGF.Builder;
2443 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2444 "ap");
2445 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2446 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002447 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002448 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2449
2450 uint64_t Offset =
2451 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2452 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002453 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002454 "ap.next");
2455 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2456
2457 return AddrTyped;
2458}
2459
Chris Lattner458b2aa2010-07-29 02:16:43 +00002460ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2461 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002462 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002463
John McCalla1dee5302010-08-22 10:59:02 +00002464 if (isAggregateTypeForABI(RetTy))
Chris Lattner458b2aa2010-07-29 02:16:43 +00002465 return ABIArgInfo::getIndirect(0);
2466
2467 // Treat an enum type as its underlying type.
2468 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2469 RetTy = EnumTy->getDecl()->getIntegerType();
2470
2471 return (RetTy->isPromotableIntegerType() ?
2472 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002473}
2474
Chris Lattner0cf24192010-06-28 20:05:43 +00002475//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002476// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002477//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002478
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002479namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002480
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002481class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002482public:
2483 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2484
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002485 bool isPromotableIntegerType(QualType Ty) const;
2486
Chris Lattner458b2aa2010-07-29 02:16:43 +00002487 ABIArgInfo classifyReturnType(QualType RetTy) const;
2488 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002489
Chris Lattner22326a12010-07-29 02:31:05 +00002490 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002491 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002492 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2493 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002494 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002495 }
2496
2497 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2498 CodeGenFunction &CGF) const;
2499};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002500
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002501class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2502public:
Chris Lattner2b037972010-07-29 02:01:43 +00002503 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2504 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002505};
2506
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002507}
2508
2509bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2510 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002511 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002512 switch (BT->getKind()) {
2513 case BuiltinType::Bool:
2514 case BuiltinType::Char_S:
2515 case BuiltinType::Char_U:
2516 case BuiltinType::SChar:
2517 case BuiltinType::UChar:
2518 case BuiltinType::Short:
2519 case BuiltinType::UShort:
2520 case BuiltinType::Int:
2521 case BuiltinType::UInt:
2522 return true;
2523 default:
2524 return false;
2525 }
2526 return false;
2527}
2528
2529llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2530 CodeGenFunction &CGF) const {
2531 // FIXME: Implement
2532 return 0;
2533}
2534
2535
Chris Lattner458b2aa2010-07-29 02:16:43 +00002536ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2537 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002538 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002539 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002540 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002541
2542 return (isPromotableIntegerType(RetTy) ?
2543 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002544}
2545
Chris Lattner458b2aa2010-07-29 02:16:43 +00002546ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002547 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002548 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002549
2550 return (isPromotableIntegerType(Ty) ?
2551 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002552}
2553
Chris Lattner0cf24192010-06-28 20:05:43 +00002554//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002555// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002556//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002557
2558namespace {
2559
2560class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2561public:
Chris Lattner2b037972010-07-29 02:01:43 +00002562 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2563 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002564 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2565 CodeGen::CodeGenModule &M) const;
2566};
2567
2568}
2569
2570void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2571 llvm::GlobalValue *GV,
2572 CodeGen::CodeGenModule &M) const {
2573 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2574 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2575 // Handle 'interrupt' attribute:
2576 llvm::Function *F = cast<llvm::Function>(GV);
2577
2578 // Step 1: Set ISR calling convention.
2579 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2580
2581 // Step 2: Add attributes goodness.
2582 F->addFnAttr(llvm::Attribute::NoInline);
2583
2584 // Step 3: Emit ISR vector alias.
2585 unsigned Num = attr->getNumber() + 0xffe0;
2586 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2587 "vector_" +
2588 llvm::LowercaseString(llvm::utohexstr(Num)),
2589 GV, &M.getModule());
2590 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002591 }
2592}
2593
Chris Lattner0cf24192010-06-28 20:05:43 +00002594//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002595// MIPS ABI Implementation. This works for both little-endian and
2596// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002597//===----------------------------------------------------------------------===//
2598
John McCall943fae92010-05-27 06:19:26 +00002599namespace {
2600class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2601public:
Chris Lattner2b037972010-07-29 02:01:43 +00002602 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2603 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002604
2605 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2606 return 29;
2607 }
2608
2609 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002610 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002611};
2612}
2613
2614bool
2615MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2616 llvm::Value *Address) const {
2617 // This information comes from gcc's implementation, which seems to
2618 // as canonical as it gets.
2619
2620 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2621 llvm::LLVMContext &Context = CGF.getLLVMContext();
2622
2623 // Everything on MIPS is 4 bytes. Double-precision FP registers
2624 // are aliased to pairs of single-precision FP registers.
2625 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2626 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2627
2628 // 0-31 are the general purpose registers, $0 - $31.
2629 // 32-63 are the floating-point registers, $f0 - $f31.
2630 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2631 // 66 is the (notional, I think) register for signal-handler return.
2632 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2633
2634 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2635 // They are one bit wide and ignored here.
2636
2637 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2638 // (coprocessor 1 is the FP unit)
2639 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2640 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2641 // 176-181 are the DSP accumulator registers.
2642 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2643
2644 return false;
2645}
2646
2647
Chris Lattner2b037972010-07-29 02:01:43 +00002648const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002649 if (TheTargetCodeGenInfo)
2650 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002651
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002652 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2653 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002654
Chris Lattner22a931e2010-06-29 06:01:59 +00002655 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002656 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002657 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002658 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002659
John McCall943fae92010-05-27 06:19:26 +00002660 case llvm::Triple::mips:
2661 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002662 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002663
Daniel Dunbard59655c2009-09-12 00:59:49 +00002664 case llvm::Triple::arm:
2665 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002666 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002667 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002668 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002669 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002670
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002671 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002672 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002673
2674 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002675 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002676
John McCallea8d8bb2010-03-11 00:10:12 +00002677 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002678 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002679
Daniel Dunbard59655c2009-09-12 00:59:49 +00002680 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002681 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002682
2683 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002684 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002685
Daniel Dunbar40165182009-08-24 09:10:05 +00002686 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002687 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002688 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002689 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002690 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002691 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002692 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002693 case llvm::Triple::AuroraUX:
2694 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002695 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002696 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002697 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002698 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002699
2700 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002701 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002702 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002703 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002704
Daniel Dunbare3532f82009-08-24 08:52:16 +00002705 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00002706 switch (Triple.getOS()) {
2707 case llvm::Triple::Win32:
2708 case llvm::Triple::MinGW64:
2709 case llvm::Triple::Cygwin:
2710 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2711 default:
2712 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2713 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00002714 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002715}