blob: c5b858dbfabc14dbc761e3c170150b6dbcc0028f [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
78 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000079 break;
80 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000081 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000082 break;
83 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000084 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000085}
86
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000087TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
88
Daniel Dunbar626f1d82009-09-13 08:03:58 +000089static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090
91/// isEmptyField - Return true iff a the field is "empty", that is it
92/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000093static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
94 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000095 if (FD->isUnnamedBitfield())
96 return true;
97
98 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000099
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000100 // Constant arrays of empty records count as empty, strip them off.
101 if (AllowArrays)
102 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
103 FT = AT->getElementType();
104
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000105 const RecordType *RT = FT->getAs<RecordType>();
106 if (!RT)
107 return false;
108
109 // C++ record fields are never empty, at least in the Itanium ABI.
110 //
111 // FIXME: We should use a predicate for whether this behavior is true in the
112 // current ABI.
113 if (isa<CXXRecordDecl>(RT->getDecl()))
114 return false;
115
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000116 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000117}
118
119/// isEmptyRecord - Return true iff a structure contains only empty
120/// fields. Note that a structure with a flexible array member is not
121/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000122static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000123 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000124 if (!RT)
125 return 0;
126 const RecordDecl *RD = RT->getDecl();
127 if (RD->hasFlexibleArrayMember())
128 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000129
130 // If this is a C++ record, check the bases first.
131 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
132 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
133 e = CXXRD->bases_end(); i != e; ++i)
134 if (!isEmptyRecord(Context, i->getType(), true))
135 return false;
136
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000137 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
138 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000139 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000140 return false;
141 return true;
142}
143
Anders Carlsson20759ad2009-09-16 15:53:40 +0000144/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
145/// a non-trivial destructor or a non-trivial copy constructor.
146static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
147 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
148 if (!RD)
149 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000150
Anders Carlsson20759ad2009-09-16 15:53:40 +0000151 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
152}
153
154/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
155/// a record type with either a non-trivial destructor or a non-trivial copy
156/// constructor.
157static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
158 const RecordType *RT = T->getAs<RecordType>();
159 if (!RT)
160 return false;
161
162 return hasNonTrivialDestructorOrCopyConstructor(RT);
163}
164
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000165/// isSingleElementStruct - Determine if a structure is a "single
166/// element struct", i.e. it has exactly one non-empty field or
167/// exactly one field which is itself a single element
168/// struct. Structures with flexible array members are never
169/// considered single element structs.
170///
171/// \return The field declaration for the single non-empty field, if
172/// it exists.
173static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
174 const RecordType *RT = T->getAsStructureType();
175 if (!RT)
176 return 0;
177
178 const RecordDecl *RD = RT->getDecl();
179 if (RD->hasFlexibleArrayMember())
180 return 0;
181
182 const Type *Found = 0;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000183
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000184 // If this is a C++ record, check the bases first.
185 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
186 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
187 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000188 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000189 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000190 continue;
191
192 // If we already found an element then this isn't a single-element struct.
193 if (Found)
194 return 0;
195
196 // If this is non-empty and not a single element struct, the composite
197 // cannot be a single element struct.
198 Found = isSingleElementStruct(i->getType(), Context);
199 if (!Found)
200 return 0;
201 }
202 }
203
204 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
206 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000207 const FieldDecl *FD = *i;
208 QualType FT = FD->getType();
209
210 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000211 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000212 continue;
213
214 // If we already found an element then this isn't a single-element
215 // struct.
216 if (Found)
217 return 0;
218
219 // Treat single element arrays as the element.
220 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
221 if (AT->getSize().getZExtValue() != 1)
222 break;
223 FT = AT->getElementType();
224 }
225
John McCalla1dee5302010-08-22 10:59:02 +0000226 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000227 Found = FT.getTypePtr();
228 } else {
229 Found = isSingleElementStruct(FT, Context);
230 if (!Found)
231 return 0;
232 }
233 }
234
235 return Found;
236}
237
238static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000239 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000240 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
241 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000242 return false;
243
244 uint64_t Size = Context.getTypeSize(Ty);
245 return Size == 32 || Size == 64;
246}
247
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000248/// canExpandIndirectArgument - Test whether an argument type which is to be
249/// passed indirectly (on the stack) would have the equivalent layout if it was
250/// expanded into separate arguments. If so, we prefer to do the latter to avoid
251/// inhibiting optimizations.
252///
253// FIXME: This predicate is missing many cases, currently it just follows
254// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
255// should probably make this smarter, or better yet make the LLVM backend
256// capable of handling it.
257static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
258 // We can only expand structure types.
259 const RecordType *RT = Ty->getAs<RecordType>();
260 if (!RT)
261 return false;
262
263 // We can only expand (C) structures.
264 //
265 // FIXME: This needs to be generalized to handle classes as well.
266 const RecordDecl *RD = RT->getDecl();
267 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
268 return false;
269
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000272 const FieldDecl *FD = *i;
273
274 if (!is32Or64BitBasicType(FD->getType(), Context))
275 return false;
276
277 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
278 // how to expand them yet, and the predicate for telling if a bitfield still
279 // counts as "basic" is more complicated than what we were doing previously.
280 if (FD->isBitField())
281 return false;
282 }
283
284 return true;
285}
286
287namespace {
288/// DefaultABIInfo - The default implementation for ABI specific
289/// details. This implementation provides information which results in
290/// self-consistent and sensible LLVM IR generation, but does not
291/// conform to any particular ABI.
292class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000293public:
294 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000295
Chris Lattner458b2aa2010-07-29 02:16:43 +0000296 ABIArgInfo classifyReturnType(QualType RetTy) const;
297 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000298
Chris Lattner22326a12010-07-29 02:31:05 +0000299 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
302 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000303 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000304 }
305
306 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
307 CodeGenFunction &CGF) const;
308};
309
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000310class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
311public:
Chris Lattner2b037972010-07-29 02:01:43 +0000312 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
313 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000314};
315
316llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
317 CodeGenFunction &CGF) const {
318 return 0;
319}
320
Chris Lattner458b2aa2010-07-29 02:16:43 +0000321ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000322 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000323 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000324
Chris Lattner9723d6c2010-03-11 18:19:55 +0000325 // Treat an enum type as its underlying type.
326 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
327 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000328
Chris Lattner9723d6c2010-03-11 18:19:55 +0000329 return (Ty->isPromotableIntegerType() ?
330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000331}
332
Chris Lattner0cf24192010-06-28 20:05:43 +0000333//===----------------------------------------------------------------------===//
334// X86-32 ABI Implementation
335//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000336
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000337/// X86_32ABIInfo - The X86-32 ABI information.
338class X86_32ABIInfo : public ABIInfo {
David Chisnallde3a0692009-08-17 23:08:21 +0000339 bool IsDarwinVectorABI;
340 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000341
342 static bool isRegisterSize(unsigned Size) {
343 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
344 }
345
346 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
347
Daniel Dunbar557893d2010-04-21 19:10:51 +0000348 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
349 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000350 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000351
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000352public:
Chris Lattner2b037972010-07-29 02:01:43 +0000353
Chris Lattner458b2aa2010-07-29 02:16:43 +0000354 ABIArgInfo classifyReturnType(QualType RetTy) const;
355 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000356
Chris Lattner22326a12010-07-29 02:31:05 +0000357 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000358 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000359 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
360 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000361 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000362 }
363
364 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
365 CodeGenFunction &CGF) const;
366
Chris Lattner2b037972010-07-29 02:01:43 +0000367 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
368 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000369};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000370
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000371class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
372public:
Chris Lattner2b037972010-07-29 02:01:43 +0000373 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
374 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000375
376 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
377 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000378
379 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
380 // Darwin uses different dwarf register numbers for EH.
381 if (CGM.isTargetDarwin()) return 5;
382
383 return 4;
384 }
385
386 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
387 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000388};
389
390}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000391
392/// shouldReturnTypeInRegister - Determine if the given type should be
393/// passed in a register (for the Darwin ABI).
394bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
395 ASTContext &Context) {
396 uint64_t Size = Context.getTypeSize(Ty);
397
398 // Type must be register sized.
399 if (!isRegisterSize(Size))
400 return false;
401
402 if (Ty->isVectorType()) {
403 // 64- and 128- bit vectors inside structures are not returned in
404 // registers.
405 if (Size == 64 || Size == 128)
406 return false;
407
408 return true;
409 }
410
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000411 // If this is a builtin, pointer, enum, complex type, member pointer, or
412 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000413 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000414 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000415 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000416 return true;
417
418 // Arrays are treated like records.
419 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
420 return shouldReturnTypeInRegister(AT->getElementType(), Context);
421
422 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000423 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000424 if (!RT) return false;
425
Anders Carlsson40446e82010-01-27 03:25:19 +0000426 // FIXME: Traverse bases here too.
427
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000428 // Structure types are passed in register if all fields would be
429 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000430 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
431 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000432 const FieldDecl *FD = *i;
433
434 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000435 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000436 continue;
437
438 // Check fields recursively.
439 if (!shouldReturnTypeInRegister(FD->getType(), Context))
440 return false;
441 }
442
443 return true;
444}
445
Chris Lattner458b2aa2010-07-29 02:16:43 +0000446ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
447 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000448 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000449
Chris Lattner458b2aa2010-07-29 02:16:43 +0000450 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000451 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000452 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000453 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454
455 // 128-bit vectors are a special case; they are returned in
456 // registers and we need to make sure to pick a type the LLVM
457 // backend will like.
458 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000459 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000460 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000461
462 // Always return in register if it fits in a general purpose
463 // register, or if it is 64 bits and has a single element.
464 if ((Size == 8 || Size == 16 || Size == 32) ||
465 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000466 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000467 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000468
469 return ABIArgInfo::getIndirect(0);
470 }
471
472 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000473 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000474
John McCalla1dee5302010-08-22 10:59:02 +0000475 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000476 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000477 // Structures with either a non-trivial destructor or a non-trivial
478 // copy constructor are always indirect.
479 if (hasNonTrivialDestructorOrCopyConstructor(RT))
480 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000481
Anders Carlsson5789c492009-10-20 22:07:59 +0000482 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000483 if (RT->getDecl()->hasFlexibleArrayMember())
484 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000485 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000486
David Chisnallde3a0692009-08-17 23:08:21 +0000487 // If specified, structs and unions are always indirect.
488 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000489 return ABIArgInfo::getIndirect(0);
490
491 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000492 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000493 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000494 if (BT->isIntegerType()) {
495 // We need to use the size of the structure, padding
496 // bit-fields can adjust that to be larger than the single
497 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000498 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000499 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000500 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
501 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000502
Chris Lattner458b2aa2010-07-29 02:16:43 +0000503 if (BT->getKind() == BuiltinType::Float) {
504 assert(getContext().getTypeSize(RetTy) ==
505 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000506 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000507 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000508 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000509
Chris Lattner458b2aa2010-07-29 02:16:43 +0000510 if (BT->getKind() == BuiltinType::Double) {
511 assert(getContext().getTypeSize(RetTy) ==
512 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000513 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000514 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000515 }
516 } else if (SeltTy->isPointerType()) {
517 // FIXME: It would be really nice if this could come out as the proper
518 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000519 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000520 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000521 } else if (SeltTy->isVectorType()) {
522 // 64- and 128-bit vectors are never returned in a
523 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000524 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000525 if (Size == 64 || Size == 128)
526 return ABIArgInfo::getIndirect(0);
527
Chris Lattner458b2aa2010-07-29 02:16:43 +0000528 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 }
530 }
531
532 // Small structures which are register sized are generally returned
533 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000534 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
535 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000536 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 }
538
539 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000540 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000541
Chris Lattner458b2aa2010-07-29 02:16:43 +0000542 // Treat an enum type as its underlying type.
543 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
544 RetTy = EnumTy->getDecl()->getIntegerType();
545
546 return (RetTy->isPromotableIntegerType() ?
547 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000548}
549
Chris Lattner458b2aa2010-07-29 02:16:43 +0000550ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000551 if (!ByVal)
552 return ABIArgInfo::getIndirect(0, false);
553
554 // Compute the byval alignment. We trust the back-end to honor the
555 // minimum ABI alignment for byval, to make cleaner IR.
556 const unsigned MinABIAlign = 4;
Chris Lattner458b2aa2010-07-29 02:16:43 +0000557 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000558 if (Align > MinABIAlign)
559 return ABIArgInfo::getIndirect(Align);
560 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000561}
562
Chris Lattner458b2aa2010-07-29 02:16:43 +0000563ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000564 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000565 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000566 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000567 if (const RecordType *RT = Ty->getAs<RecordType>()) {
568 // Structures with either a non-trivial destructor or a non-trivial
569 // copy constructor are always indirect.
570 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000571 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000572
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000573 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000574 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000575 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000576
577 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000578 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000579 return ABIArgInfo::getIgnore();
580
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000581 // Expand small (<= 128-bit) record types when we know that the stack layout
582 // of those arguments will match the struct. This is important because the
583 // LLVM backend isn't smart enough to remove byval, which inhibits many
584 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000585 if (getContext().getTypeSize(Ty) <= 4*32 &&
586 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000587 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000588
Chris Lattner458b2aa2010-07-29 02:16:43 +0000589 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000590 }
591
Chris Lattner458b2aa2010-07-29 02:16:43 +0000592 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
593 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000594
Chris Lattner458b2aa2010-07-29 02:16:43 +0000595 return (Ty->isPromotableIntegerType() ?
596 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000597}
598
599llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
600 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000601 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000602 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000603
604 CGBuilderTy &Builder = CGF.Builder;
605 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
606 "ap");
607 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
608 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000609 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000610 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
611
612 uint64_t Offset =
613 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
614 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000615 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000616 "ap.next");
617 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
618
619 return AddrTyped;
620}
621
Charles Davis4ea31ab2010-02-13 15:54:06 +0000622void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
623 llvm::GlobalValue *GV,
624 CodeGen::CodeGenModule &CGM) const {
625 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
626 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
627 // Get the LLVM function.
628 llvm::Function *Fn = cast<llvm::Function>(GV);
629
630 // Now add the 'alignstack' attribute with a value of 16.
631 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
632 }
633 }
634}
635
John McCallbeec5a02010-03-06 00:35:14 +0000636bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
637 CodeGen::CodeGenFunction &CGF,
638 llvm::Value *Address) const {
639 CodeGen::CGBuilderTy &Builder = CGF.Builder;
640 llvm::LLVMContext &Context = CGF.getLLVMContext();
641
642 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
643 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000644
John McCallbeec5a02010-03-06 00:35:14 +0000645 // 0-7 are the eight integer registers; the order is different
646 // on Darwin (for EH), but the range is the same.
647 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000648 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000649
650 if (CGF.CGM.isTargetDarwin()) {
651 // 12-16 are st(0..4). Not sure why we stop at 4.
652 // These have size 16, which is sizeof(long double) on
653 // platforms with 8-byte alignment for that type.
654 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000655 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000656
John McCallbeec5a02010-03-06 00:35:14 +0000657 } else {
658 // 9 is %eflags, which doesn't get a size on Darwin for some
659 // reason.
660 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
661
662 // 11-16 are st(0..5). Not sure why we stop at 5.
663 // These have size 12, which is sizeof(long double) on
664 // platforms with 4-byte alignment for that type.
665 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000666 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
667 }
John McCallbeec5a02010-03-06 00:35:14 +0000668
669 return false;
670}
671
Chris Lattner0cf24192010-06-28 20:05:43 +0000672//===----------------------------------------------------------------------===//
673// X86-64 ABI Implementation
674//===----------------------------------------------------------------------===//
675
676
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000677namespace {
678/// X86_64ABIInfo - The X86_64 ABI information.
679class X86_64ABIInfo : public ABIInfo {
680 enum Class {
681 Integer = 0,
682 SSE,
683 SSEUp,
684 X87,
685 X87Up,
686 ComplexX87,
687 NoClass,
688 Memory
689 };
690
691 /// merge - Implement the X86_64 ABI merging algorithm.
692 ///
693 /// Merge an accumulating classification \arg Accum with a field
694 /// classification \arg Field.
695 ///
696 /// \param Accum - The accumulating classification. This should
697 /// always be either NoClass or the result of a previous merge
698 /// call. In addition, this should never be Memory (the caller
699 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000700 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000701
702 /// classify - Determine the x86_64 register classes in which the
703 /// given type T should be passed.
704 ///
705 /// \param Lo - The classification for the parts of the type
706 /// residing in the low word of the containing object.
707 ///
708 /// \param Hi - The classification for the parts of the type
709 /// residing in the high word of the containing object.
710 ///
711 /// \param OffsetBase - The bit offset of this type in the
712 /// containing object. Some parameters are classified different
713 /// depending on whether they straddle an eightbyte boundary.
714 ///
715 /// If a word is unused its result will be NoClass; if a type should
716 /// be passed in Memory then at least the classification of \arg Lo
717 /// will be Memory.
718 ///
719 /// The \arg Lo class will be NoClass iff the argument is ignored.
720 ///
721 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
722 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000723 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000724
Chris Lattner4200fe42010-07-29 04:56:46 +0000725 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000726 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000727 unsigned IROffset, QualType SourceTy,
728 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000729 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
730 unsigned IROffset, QualType SourceTy,
731 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000732
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000733 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000734 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000735 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000736
737 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000738 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000739 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000740
Chris Lattner458b2aa2010-07-29 02:16:43 +0000741 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000742
Chris Lattner029c0f12010-07-29 04:41:05 +0000743 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
744 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000745
746public:
Chris Lattner2b037972010-07-29 02:01:43 +0000747 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000748
Chris Lattner22326a12010-07-29 02:31:05 +0000749 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750
751 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
752 CodeGenFunction &CGF) const;
753};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000754
755class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
756public:
Chris Lattner2b037972010-07-29 02:01:43 +0000757 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
758 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000759
760 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
761 return 7;
762 }
763
764 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
765 llvm::Value *Address) const {
766 CodeGen::CGBuilderTy &Builder = CGF.Builder;
767 llvm::LLVMContext &Context = CGF.getLLVMContext();
768
769 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
770 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000771
John McCall943fae92010-05-27 06:19:26 +0000772 // 0-15 are the 16 integer registers.
773 // 16 is %rip.
774 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000775
776 return false;
777 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000778};
779
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000780}
781
Chris Lattnerd776fb12010-06-28 21:43:59 +0000782X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000783 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
784 // classified recursively so that always two fields are
785 // considered. The resulting class is calculated according to
786 // the classes of the fields in the eightbyte:
787 //
788 // (a) If both classes are equal, this is the resulting class.
789 //
790 // (b) If one of the classes is NO_CLASS, the resulting class is
791 // the other class.
792 //
793 // (c) If one of the classes is MEMORY, the result is the MEMORY
794 // class.
795 //
796 // (d) If one of the classes is INTEGER, the result is the
797 // INTEGER.
798 //
799 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
800 // MEMORY is used as class.
801 //
802 // (f) Otherwise class SSE is used.
803
804 // Accum should never be memory (we should have returned) or
805 // ComplexX87 (because this cannot be passed in a structure).
806 assert((Accum != Memory && Accum != ComplexX87) &&
807 "Invalid accumulated classification during merge.");
808 if (Accum == Field || Field == NoClass)
809 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000810 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000811 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000812 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000813 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000814 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000815 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000816 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
817 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000818 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000819 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000820}
821
Chris Lattner5c740f12010-06-30 19:14:05 +0000822void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000823 Class &Lo, Class &Hi) const {
824 // FIXME: This code can be simplified by introducing a simple value class for
825 // Class pairs with appropriate constructor methods for the various
826 // situations.
827
828 // FIXME: Some of the split computations are wrong; unaligned vectors
829 // shouldn't be passed in registers for example, so there is no chance they
830 // can straddle an eightbyte. Verify & simplify.
831
832 Lo = Hi = NoClass;
833
834 Class &Current = OffsetBase < 64 ? Lo : Hi;
835 Current = Memory;
836
John McCall9dd450b2009-09-21 23:43:11 +0000837 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000838 BuiltinType::Kind k = BT->getKind();
839
840 if (k == BuiltinType::Void) {
841 Current = NoClass;
842 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
843 Lo = Integer;
844 Hi = Integer;
845 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
846 Current = Integer;
847 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
848 Current = SSE;
849 } else if (k == BuiltinType::LongDouble) {
850 Lo = X87;
851 Hi = X87Up;
852 }
853 // FIXME: _Decimal32 and _Decimal64 are SSE.
854 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000855 return;
856 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000857
Chris Lattnerd776fb12010-06-28 21:43:59 +0000858 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000859 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000860 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000861 return;
862 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000863
Chris Lattnerd776fb12010-06-28 21:43:59 +0000864 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000865 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000866 return;
867 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000868
Chris Lattnerd776fb12010-06-28 21:43:59 +0000869 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000870 if (Ty->isMemberFunctionPointerType())
871 Lo = Hi = Integer;
872 else
873 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000874 return;
875 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000876
Chris Lattnerd776fb12010-06-28 21:43:59 +0000877 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000878 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000879 if (Size == 32) {
880 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
881 // float> as integer.
882 Current = Integer;
883
884 // If this type crosses an eightbyte boundary, it should be
885 // split.
886 uint64_t EB_Real = (OffsetBase) / 64;
887 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
888 if (EB_Real != EB_Imag)
889 Hi = Lo;
890 } else if (Size == 64) {
891 // gcc passes <1 x double> in memory. :(
892 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
893 return;
894
895 // gcc passes <1 x long long> as INTEGER.
896 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
897 Current = Integer;
898 else
899 Current = SSE;
900
901 // If this type crosses an eightbyte boundary, it should be
902 // split.
903 if (OffsetBase && OffsetBase != 64)
904 Hi = Lo;
905 } else if (Size == 128) {
906 Lo = SSE;
907 Hi = SSEUp;
908 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000909 return;
910 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000911
Chris Lattnerd776fb12010-06-28 21:43:59 +0000912 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000913 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000914
Chris Lattner2b037972010-07-29 02:01:43 +0000915 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000916 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000917 if (Size <= 64)
918 Current = Integer;
919 else if (Size <= 128)
920 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000921 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000922 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000923 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000924 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000925 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000926 Current = ComplexX87;
927
928 // If this complex type crosses an eightbyte boundary then it
929 // should be split.
930 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000931 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000932 if (Hi == NoClass && EB_Real != EB_Imag)
933 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000934
Chris Lattnerd776fb12010-06-28 21:43:59 +0000935 return;
936 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000937
Chris Lattner2b037972010-07-29 02:01:43 +0000938 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000939 // Arrays are treated like structures.
940
Chris Lattner2b037972010-07-29 02:01:43 +0000941 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000942
943 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
944 // than two eightbytes, ..., it has class MEMORY.
945 if (Size > 128)
946 return;
947
948 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
949 // fields, it has class MEMORY.
950 //
951 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000952 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000953 return;
954
955 // Otherwise implement simplified merge. We could be smarter about
956 // this, but it isn't worth it and would be harder to verify.
957 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000958 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000959 uint64_t ArraySize = AT->getSize().getZExtValue();
960 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
961 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000962 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000963 Lo = merge(Lo, FieldLo);
964 Hi = merge(Hi, FieldHi);
965 if (Lo == Memory || Hi == Memory)
966 break;
967 }
968
969 // Do post merger cleanup (see below). Only case we worry about is Memory.
970 if (Hi == Memory)
971 Lo = Memory;
972 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
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 RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000977 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000978
979 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
980 // than two eightbytes, ..., it has class MEMORY.
981 if (Size > 128)
982 return;
983
Anders Carlsson20759ad2009-09-16 15:53:40 +0000984 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
985 // copy constructor or a non-trivial destructor, it is passed by invisible
986 // reference.
987 if (hasNonTrivialDestructorOrCopyConstructor(RT))
988 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000989
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000990 const RecordDecl *RD = RT->getDecl();
991
992 // Assume variable sized types are passed in memory.
993 if (RD->hasFlexibleArrayMember())
994 return;
995
Chris Lattner2b037972010-07-29 02:01:43 +0000996 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000997
998 // Reset Lo class, this will be recomputed.
999 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001000
1001 // If this is a C++ record, classify the bases first.
1002 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1003 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1004 e = CXXRD->bases_end(); i != e; ++i) {
1005 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1006 "Unexpected base class!");
1007 const CXXRecordDecl *Base =
1008 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1009
1010 // Classify this field.
1011 //
1012 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1013 // single eightbyte, each is classified separately. Each eightbyte gets
1014 // initialized to class NO_CLASS.
1015 Class FieldLo, FieldHi;
1016 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001017 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001018 Lo = merge(Lo, FieldLo);
1019 Hi = merge(Hi, FieldHi);
1020 if (Lo == Memory || Hi == Memory)
1021 break;
1022 }
1023 }
1024
1025 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001026 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001027 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1028 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001029 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1030 bool BitField = i->isBitField();
1031
1032 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1033 // fields, it has class MEMORY.
1034 //
1035 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001036 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001037 Lo = Memory;
1038 return;
1039 }
1040
1041 // Classify this field.
1042 //
1043 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1044 // exceeds a single eightbyte, each is classified
1045 // separately. Each eightbyte gets initialized to class
1046 // NO_CLASS.
1047 Class FieldLo, FieldHi;
1048
1049 // Bit-fields require special handling, they do not force the
1050 // structure to be passed in memory even if unaligned, and
1051 // therefore they can straddle an eightbyte.
1052 if (BitField) {
1053 // Ignore padding bit-fields.
1054 if (i->isUnnamedBitfield())
1055 continue;
1056
1057 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001058 uint64_t Size =
1059 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001060
1061 uint64_t EB_Lo = Offset / 64;
1062 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1063 FieldLo = FieldHi = NoClass;
1064 if (EB_Lo) {
1065 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1066 FieldLo = NoClass;
1067 FieldHi = Integer;
1068 } else {
1069 FieldLo = Integer;
1070 FieldHi = EB_Hi ? Integer : NoClass;
1071 }
1072 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001073 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001074 Lo = merge(Lo, FieldLo);
1075 Hi = merge(Hi, FieldHi);
1076 if (Lo == Memory || Hi == Memory)
1077 break;
1078 }
1079
1080 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1081 //
1082 // (a) If one of the classes is MEMORY, the whole argument is
1083 // passed in memory.
1084 //
1085 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1086
1087 // The first of these conditions is guaranteed by how we implement
1088 // the merge (just bail).
1089 //
1090 // The second condition occurs in the case of unions; for example
1091 // union { _Complex double; unsigned; }.
1092 if (Hi == Memory)
1093 Lo = Memory;
1094 if (Hi == SSEUp && Lo != SSE)
1095 Hi = SSE;
1096 }
1097}
1098
Chris Lattner22a931e2010-06-29 06:01:59 +00001099ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001100 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1101 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001102 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001103 // Treat an enum type as its underlying type.
1104 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1105 Ty = EnumTy->getDecl()->getIntegerType();
1106
1107 return (Ty->isPromotableIntegerType() ?
1108 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1109 }
1110
1111 return ABIArgInfo::getIndirect(0);
1112}
1113
Chris Lattner22a931e2010-06-29 06:01:59 +00001114ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001115 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1116 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001117 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001118 // Treat an enum type as its underlying type.
1119 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1120 Ty = EnumTy->getDecl()->getIntegerType();
1121
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001122 return (Ty->isPromotableIntegerType() ?
1123 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001124 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001125
Daniel Dunbar53fac692010-04-21 19:49:55 +00001126 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1127 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001128
Daniel Dunbar53fac692010-04-21 19:49:55 +00001129 // Compute the byval alignment. We trust the back-end to honor the
1130 // minimum ABI alignment for byval, to make cleaner IR.
1131 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001132 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001133 if (Align > MinABIAlign)
1134 return ABIArgInfo::getIndirect(Align);
1135 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001136}
1137
Chris Lattner4200fe42010-07-29 04:56:46 +00001138/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1139/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1140/// vector register.
1141const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001142 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001143
Chris Lattner9fa15c32010-07-29 05:02:29 +00001144 // Wrapper structs that just contain vectors are passed just like vectors,
1145 // strip them off if present.
1146 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1147 while (STy && STy->getNumElements() == 1) {
1148 IRType = STy->getElementType(0);
1149 STy = dyn_cast<llvm::StructType>(IRType);
1150 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001151
Chris Lattner4200fe42010-07-29 04:56:46 +00001152 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001153 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001154 const llvm::Type *EltTy = VT->getElementType();
1155 if (VT->getBitWidth() == 128 &&
1156 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1157 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1158 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1159 EltTy->isIntegerTy(128)))
1160 return VT;
1161 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001162
Chris Lattner4200fe42010-07-29 04:56:46 +00001163 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1164}
1165
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001166/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1167/// is known to either be off the end of the specified type or being in
1168/// alignment padding. The user type specified is known to be at most 128 bits
1169/// in size, and have passed through X86_64ABIInfo::classify with a successful
1170/// classification that put one of the two halves in the INTEGER class.
1171///
1172/// It is conservatively correct to return false.
1173static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1174 unsigned EndBit, ASTContext &Context) {
1175 // If the bytes being queried are off the end of the type, there is no user
1176 // data hiding here. This handles analysis of builtins, vectors and other
1177 // types that don't contain interesting padding.
1178 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1179 if (TySize <= StartBit)
1180 return true;
1181
Chris Lattner98076a22010-07-29 07:43:55 +00001182 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1183 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1184 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1185
1186 // Check each element to see if the element overlaps with the queried range.
1187 for (unsigned i = 0; i != NumElts; ++i) {
1188 // If the element is after the span we care about, then we're done..
1189 unsigned EltOffset = i*EltSize;
1190 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001191
Chris Lattner98076a22010-07-29 07:43:55 +00001192 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1193 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1194 EndBit-EltOffset, Context))
1195 return false;
1196 }
1197 // If it overlaps no elements, then it is safe to process as padding.
1198 return true;
1199 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001200
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001201 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1202 const RecordDecl *RD = RT->getDecl();
1203 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001204
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001205 // If this is a C++ record, check the bases first.
1206 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1207 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1208 e = CXXRD->bases_end(); i != e; ++i) {
1209 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1210 "Unexpected base class!");
1211 const CXXRecordDecl *Base =
1212 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001213
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001214 // If the base is after the span we care about, ignore it.
1215 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1216 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001217
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001218 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1219 if (!BitsContainNoUserData(i->getType(), BaseStart,
1220 EndBit-BaseOffset, Context))
1221 return false;
1222 }
1223 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001224
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001225 // Verify that no field has data that overlaps the region of interest. Yes
1226 // this could be sped up a lot by being smarter about queried fields,
1227 // however we're only looking at structs up to 16 bytes, so we don't care
1228 // much.
1229 unsigned idx = 0;
1230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1231 i != e; ++i, ++idx) {
1232 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001233
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001234 // If we found a field after the region we care about, then we're done.
1235 if (FieldOffset >= EndBit) break;
1236
1237 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1238 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1239 Context))
1240 return false;
1241 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001242
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001243 // If nothing in this record overlapped the area of interest, then we're
1244 // clean.
1245 return true;
1246 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001247
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001248 return false;
1249}
1250
Chris Lattnere556a712010-07-29 18:39:32 +00001251/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1252/// float member at the specified offset. For example, {int,{float}} has a
1253/// float at offset 4. It is conservatively correct for this routine to return
1254/// false.
1255static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1256 const llvm::TargetData &TD) {
1257 // Base case if we find a float.
1258 if (IROffset == 0 && IRType->isFloatTy())
1259 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001260
Chris Lattnere556a712010-07-29 18:39:32 +00001261 // If this is a struct, recurse into the field at the specified offset.
1262 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1263 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1264 unsigned Elt = SL->getElementContainingOffset(IROffset);
1265 IROffset -= SL->getElementOffset(Elt);
1266 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1267 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001268
Chris Lattnere556a712010-07-29 18:39:32 +00001269 // If this is an array, recurse into the field at the specified offset.
1270 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1271 const llvm::Type *EltTy = ATy->getElementType();
1272 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1273 IROffset -= IROffset/EltSize*EltSize;
1274 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1275 }
1276
1277 return false;
1278}
1279
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001280
1281/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1282/// low 8 bytes of an XMM register, corresponding to the SSE class.
1283const llvm::Type *X86_64ABIInfo::
1284GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1285 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001286 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001287 // pass as float if the last 4 bytes is just padding. This happens for
1288 // structs that contain 3 floats.
1289 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1290 SourceOffset*8+64, getContext()))
1291 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001292
Chris Lattnere556a712010-07-29 18:39:32 +00001293 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1294 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1295 // case.
1296 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001297 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1298 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001299
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001300 return llvm::Type::getDoubleTy(getVMContext());
1301}
1302
1303
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001304/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1305/// an 8-byte GPR. This means that we either have a scalar or we are talking
1306/// about the high or low part of an up-to-16-byte struct. This routine picks
1307/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001308/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1309/// etc).
1310///
1311/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1312/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1313/// the 8-byte value references. PrefType may be null.
1314///
1315/// SourceTy is the source level type for the entire argument. SourceOffset is
1316/// an offset into this that we're processing (which is always either 0 or 8).
1317///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001318const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001319GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1320 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001321 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1322 // returning an 8-byte unit starting with it. See if we can safely use it.
1323 if (IROffset == 0) {
1324 // Pointers and int64's always fill the 8-byte unit.
1325 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1326 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001327
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001328 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1329 // goodness in the source type is just tail padding. This is allowed to
1330 // kick in for struct {double,int} on the int, but not on
1331 // struct{double,int,int} because we wouldn't return the second int. We
1332 // have to do this analysis on the source type because we can't depend on
1333 // unions being lowered a specific way etc.
1334 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1335 IRType->isIntegerTy(32)) {
1336 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001337
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001338 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1339 SourceOffset*8+64, getContext()))
1340 return IRType;
1341 }
1342 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001343
Chris Lattnerce1bd752010-07-29 04:51:12 +00001344 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001345 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001346 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001347 if (IROffset < SL->getSizeInBytes()) {
1348 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1349 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001350
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001351 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1352 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001353 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001354 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001355
Chris Lattner98076a22010-07-29 07:43:55 +00001356 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1357 const llvm::Type *EltTy = ATy->getElementType();
1358 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1359 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001360 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1361 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001362 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001363
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001364 // Okay, we don't have any better idea of what to pass, so we pass this in an
1365 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001366 unsigned TySizeInBytes =
1367 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001368
Chris Lattner3f763422010-07-29 17:34:39 +00001369 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001370
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001371 // It is always safe to classify this as an integer type up to i64 that
1372 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001373 return llvm::IntegerType::get(getVMContext(),
1374 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001375}
1376
Chris Lattner31faff52010-07-28 23:06:14 +00001377ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001378classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001379 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1380 // classification algorithm.
1381 X86_64ABIInfo::Class Lo, Hi;
1382 classify(RetTy, 0, Lo, Hi);
1383
1384 // Check some invariants.
1385 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001386 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1387
1388 const llvm::Type *ResType = 0;
1389 switch (Lo) {
1390 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001391 if (Hi == NoClass)
1392 return ABIArgInfo::getIgnore();
1393 // If the low part is just padding, it takes no register, leave ResType
1394 // null.
1395 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1396 "Unknown missing lo part");
1397 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001398
1399 case SSEUp:
1400 case X87Up:
1401 assert(0 && "Invalid classification for lo word.");
1402
1403 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1404 // hidden argument.
1405 case Memory:
1406 return getIndirectReturnResult(RetTy);
1407
1408 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1409 // available register of the sequence %rax, %rdx is used.
1410 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001411 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1412 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001413
Chris Lattner1f3a0632010-07-29 21:42:50 +00001414 // If we have a sign or zero extended integer, make sure to return Extend
1415 // so that the parameter gets the right LLVM IR attributes.
1416 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1417 // Treat an enum type as its underlying type.
1418 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1419 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001420
Chris Lattner1f3a0632010-07-29 21:42:50 +00001421 if (RetTy->isIntegralOrEnumerationType() &&
1422 RetTy->isPromotableIntegerType())
1423 return ABIArgInfo::getExtend();
1424 }
Chris Lattner31faff52010-07-28 23:06:14 +00001425 break;
1426
1427 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1428 // available SSE register of the sequence %xmm0, %xmm1 is used.
1429 case SSE:
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001430 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001431 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001432
1433 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1434 // returned on the X87 stack in %st0 as 80-bit x87 number.
1435 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001436 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001437 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001438
1439 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1440 // part of the value is returned in %st0 and the imaginary part in
1441 // %st1.
1442 case ComplexX87:
1443 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001444 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001445 llvm::Type::getX86_FP80Ty(getVMContext()),
1446 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001447 NULL);
1448 break;
1449 }
1450
1451 switch (Hi) {
1452 // Memory was handled previously and X87 should
1453 // never occur as a hi class.
1454 case Memory:
1455 case X87:
1456 assert(0 && "Invalid classification for hi word.");
1457
1458 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001459 case NoClass:
1460 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001461
1462 case Integer: {
Chris Lattnerce1bd752010-07-29 04:51:12 +00001463 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001464 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001465 if (Lo == NoClass) // Return HiType at offset 8 in memory.
1466 return ABIArgInfo::getDirect(HiType, 8);
1467
Chris Lattner458b2aa2010-07-29 02:16:43 +00001468 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001469 break;
1470 }
Chris Lattnerc95a3982010-07-29 17:49:08 +00001471 case SSE: {
1472 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001473 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001474 if (Lo == NoClass) // Return HiType at offset 8 in memory.
1475 return ABIArgInfo::getDirect(HiType, 8);
1476
Chris Lattnerc95a3982010-07-29 17:49:08 +00001477 ResType = llvm::StructType::get(getVMContext(), ResType, HiType,NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001478 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001479 }
Chris Lattner31faff52010-07-28 23:06:14 +00001480
1481 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1482 // is passed in the upper half of the last used SSE register.
1483 //
1484 // SSEUP should always be preceeded by SSE, just widen.
1485 case SSEUp:
1486 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001487 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001488 break;
1489
1490 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1491 // returned together with the previous X87 value in %st0.
1492 case X87Up:
1493 // If X87Up is preceeded by X87, we don't need to do
1494 // anything. However, in some cases with unions it may not be
1495 // preceeded by X87. In such situations we follow gcc and pass the
1496 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001497 if (Lo != X87) {
1498 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001499 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001500 if (Lo == NoClass) // Return HiType at offset 8 in memory.
1501 return ABIArgInfo::getDirect(HiType, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001502
Chris Lattnerc95a3982010-07-29 17:49:08 +00001503 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1504 }
Chris Lattner31faff52010-07-28 23:06:14 +00001505 break;
1506 }
1507
Chris Lattner1f3a0632010-07-29 21:42:50 +00001508 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001509}
1510
Chris Lattner458b2aa2010-07-29 02:16:43 +00001511ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001512 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001513 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001514 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001515
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001516 // Check some invariants.
1517 // FIXME: Enforce these by construction.
1518 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001519 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1520
1521 neededInt = 0;
1522 neededSSE = 0;
1523 const llvm::Type *ResType = 0;
1524 switch (Lo) {
1525 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001526 if (Hi == NoClass)
1527 return ABIArgInfo::getIgnore();
1528 // If the low part is just padding, it takes no register, leave ResType
1529 // null.
1530 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1531 "Unknown missing lo part");
1532 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001533
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001534 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1535 // on the stack.
1536 case Memory:
1537
1538 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1539 // COMPLEX_X87, it is passed in memory.
1540 case X87:
1541 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001542 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001543
1544 case SSEUp:
1545 case X87Up:
1546 assert(0 && "Invalid classification for lo word.");
1547
1548 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1549 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1550 // and %r9 is used.
1551 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001552 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001553
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001554 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001555 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001556
1557 // If we have a sign or zero extended integer, make sure to return Extend
1558 // so that the parameter gets the right LLVM IR attributes.
1559 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1560 // Treat an enum type as its underlying type.
1561 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1562 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001563
Chris Lattner1f3a0632010-07-29 21:42:50 +00001564 if (Ty->isIntegralOrEnumerationType() &&
1565 Ty->isPromotableIntegerType())
1566 return ABIArgInfo::getExtend();
1567 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001568
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001569 break;
1570
1571 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1572 // available SSE register is used, the registers are taken in the
1573 // order from %xmm0 to %xmm7.
1574 case SSE:
1575 ++neededSSE;
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001576 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001577 break;
1578 }
1579
1580 switch (Hi) {
1581 // Memory was handled previously, ComplexX87 and X87 should
1582 // never occur as hi classes, and X87Up must be preceed by X87,
1583 // which is passed in memory.
1584 case Memory:
1585 case X87:
1586 case ComplexX87:
1587 assert(0 && "Invalid classification for hi word.");
1588 break;
1589
1590 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001591
Chris Lattner22a931e2010-06-29 06:01:59 +00001592 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001593 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001594 // Pick an 8-byte type based on the preferred type.
Chris Lattnerce1bd752010-07-29 04:51:12 +00001595 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001596 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001597
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001598 if (Lo == NoClass) // Pass HiType at offset 8 in memory.
1599 return ABIArgInfo::getDirect(HiType, 8);
1600
Chris Lattner458b2aa2010-07-29 02:16:43 +00001601 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001602 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001603 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001604
1605 // X87Up generally doesn't occur here (long double is passed in
1606 // memory), except in situations involving unions.
1607 case X87Up:
Chris Lattnerc95a3982010-07-29 17:49:08 +00001608 case SSE: {
1609 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001610 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001611
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001612 if (Lo == NoClass) // Pass HiType at offset 8 in memory.
1613 return ABIArgInfo::getDirect(HiType, 8);
1614
Chris Lattnerc95a3982010-07-29 17:49:08 +00001615 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001616 ++neededSSE;
1617 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001618 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001619
1620 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1621 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001622 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001623 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001624 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001625 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001626 break;
1627 }
1628
Chris Lattner1f3a0632010-07-29 21:42:50 +00001629 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001630}
1631
Chris Lattner22326a12010-07-29 02:31:05 +00001632void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001633
Chris Lattner458b2aa2010-07-29 02:16:43 +00001634 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001635
1636 // Keep track of the number of assigned registers.
1637 unsigned freeIntRegs = 6, freeSSERegs = 8;
1638
1639 // If the return value is indirect, then the hidden argument is consuming one
1640 // integer register.
1641 if (FI.getReturnInfo().isIndirect())
1642 --freeIntRegs;
1643
1644 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1645 // get assigned (in left-to-right order) for passing as follows...
1646 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1647 it != ie; ++it) {
1648 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001649 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001650
1651 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1652 // eightbyte of an argument, the whole argument is passed on the
1653 // stack. If registers have already been assigned for some
1654 // eightbytes of such an argument, the assignments get reverted.
1655 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1656 freeIntRegs -= neededInt;
1657 freeSSERegs -= neededSSE;
1658 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001659 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001660 }
1661 }
1662}
1663
1664static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1665 QualType Ty,
1666 CodeGenFunction &CGF) {
1667 llvm::Value *overflow_arg_area_p =
1668 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1669 llvm::Value *overflow_arg_area =
1670 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1671
1672 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1673 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1674 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1675 if (Align > 8) {
1676 // Note that we follow the ABI & gcc here, even though the type
1677 // could in theory have an alignment greater than 16. This case
1678 // shouldn't ever matter in practice.
1679
1680 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001681 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001682 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001683 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1684 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001685 CGF.Int64Ty);
1686 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001687 overflow_arg_area =
1688 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1689 overflow_arg_area->getType(),
1690 "overflow_arg_area.align");
1691 }
1692
1693 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1694 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1695 llvm::Value *Res =
1696 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001697 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001698
1699 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1700 // l->overflow_arg_area + sizeof(type).
1701 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1702 // an 8 byte boundary.
1703
1704 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001705 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001706 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001707 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1708 "overflow_arg_area.next");
1709 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1710
1711 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1712 return Res;
1713}
1714
1715llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1716 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001717 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001718
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001719 // Assume that va_list type is correct; should be pointer to LLVM type:
1720 // struct {
1721 // i32 gp_offset;
1722 // i32 fp_offset;
1723 // i8* overflow_arg_area;
1724 // i8* reg_save_area;
1725 // };
1726 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001727
Chris Lattner9723d6c2010-03-11 18:19:55 +00001728 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001729 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001730
1731 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1732 // in the registers. If not go to step 7.
1733 if (!neededInt && !neededSSE)
1734 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1735
1736 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1737 // general purpose registers needed to pass type and num_fp to hold
1738 // the number of floating point registers needed.
1739
1740 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1741 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1742 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1743 //
1744 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1745 // register save space).
1746
1747 llvm::Value *InRegs = 0;
1748 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1749 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1750 if (neededInt) {
1751 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1752 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001753 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1754 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001755 }
1756
1757 if (neededSSE) {
1758 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1759 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1760 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001761 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1762 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001763 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1764 }
1765
1766 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1767 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1768 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1769 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1770
1771 // Emit code to load the value if it was passed in registers.
1772
1773 CGF.EmitBlock(InRegBlock);
1774
1775 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1776 // an offset of l->gp_offset and/or l->fp_offset. This may require
1777 // copying to a temporary location in case the parameter is passed
1778 // in different register classes or requires an alignment greater
1779 // than 8 for general purpose registers and 16 for XMM registers.
1780 //
1781 // FIXME: This really results in shameful code when we end up needing to
1782 // collect arguments from different places; often what should result in a
1783 // simple assembling of a structure from scattered addresses has many more
1784 // loads than necessary. Can we clean this up?
1785 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1786 llvm::Value *RegAddr =
1787 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1788 "reg_save_area");
1789 if (neededInt && neededSSE) {
1790 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001791 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001792 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1793 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1794 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1795 const llvm::Type *TyLo = ST->getElementType(0);
1796 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001797 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001798 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001799 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1800 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001801 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1802 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001803 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1804 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001805 llvm::Value *V =
1806 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1807 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1808 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1809 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1810
Owen Anderson170229f2009-07-14 23:10:40 +00001811 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001812 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001813 } else if (neededInt) {
1814 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1815 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001816 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001817 } else if (neededSSE == 1) {
1818 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1819 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1820 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001821 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001822 assert(neededSSE == 2 && "Invalid number of needed registers!");
1823 // SSE registers are spaced 16 bytes apart in the register save
1824 // area, we need to collect the two eightbytes together.
1825 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001826 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001827 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1828 const llvm::Type *DblPtrTy =
1829 llvm::PointerType::getUnqual(DoubleTy);
1830 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1831 DoubleTy, NULL);
1832 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1833 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1834 DblPtrTy));
1835 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1836 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1837 DblPtrTy));
1838 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1839 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1840 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001841 }
1842
1843 // AMD64-ABI 3.5.7p5: Step 5. Set:
1844 // l->gp_offset = l->gp_offset + num_gp * 8
1845 // l->fp_offset = l->fp_offset + num_fp * 16.
1846 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001847 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001848 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1849 gp_offset_p);
1850 }
1851 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001852 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001853 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1854 fp_offset_p);
1855 }
1856 CGF.EmitBranch(ContBlock);
1857
1858 // Emit code to load the value if it was passed in memory.
1859
1860 CGF.EmitBlock(InMemBlock);
1861 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1862
1863 // Return the appropriate result.
1864
1865 CGF.EmitBlock(ContBlock);
1866 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1867 "vaarg.addr");
1868 ResAddr->reserveOperandSpace(2);
1869 ResAddr->addIncoming(RegAddr, InRegBlock);
1870 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001871 return ResAddr;
1872}
1873
Chris Lattner0cf24192010-06-28 20:05:43 +00001874
1875
1876//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001877// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001878//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001879
1880namespace {
1881
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001882class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001883public:
1884 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001885
Chris Lattner458b2aa2010-07-29 02:16:43 +00001886 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001887
Chris Lattner458b2aa2010-07-29 02:16:43 +00001888 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001889
Chris Lattner22326a12010-07-29 02:31:05 +00001890 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001891 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001892 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1893 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001894 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001895 }
1896
1897 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1898 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001899};
1900
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001901class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1902public:
Chris Lattner2b037972010-07-29 02:01:43 +00001903 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1904 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001905};
1906
Daniel Dunbard59655c2009-09-12 00:59:49 +00001907}
1908
Chris Lattner458b2aa2010-07-29 02:16:43 +00001909ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001910 if (RetTy->isVoidType()) {
1911 return ABIArgInfo::getIgnore();
1912 } else {
1913 return ABIArgInfo::getDirect();
1914 }
1915}
1916
Chris Lattner458b2aa2010-07-29 02:16:43 +00001917ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001918 return ABIArgInfo::getDirect();
1919}
1920
1921llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001922 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001923 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001924 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1925
1926 CGBuilderTy &Builder = CGF.Builder;
1927 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1928 "ap");
1929 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1930 llvm::Type *PTy =
1931 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1932 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1933
1934 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1935
1936 llvm::Value *NextAddr =
1937 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1938 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1939 "ap.next");
1940 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1941
1942 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001943}
1944
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001945
John McCallea8d8bb2010-03-11 00:10:12 +00001946// PowerPC-32
1947
1948namespace {
1949class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1950public:
Chris Lattner2b037972010-07-29 02:01:43 +00001951 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001952
John McCallea8d8bb2010-03-11 00:10:12 +00001953 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1954 // This is recovered from gcc output.
1955 return 1; // r1 is the dedicated stack pointer
1956 }
1957
1958 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001959 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00001960};
1961
1962}
1963
1964bool
1965PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1966 llvm::Value *Address) const {
1967 // This is calculated from the LLVM and GCC tables and verified
1968 // against gcc output. AFAIK all ABIs use the same encoding.
1969
1970 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1971 llvm::LLVMContext &Context = CGF.getLLVMContext();
1972
1973 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1974 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1975 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1976 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1977
1978 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001979 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001980
1981 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001982 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001983
1984 // 64-76 are various 4-byte special-purpose registers:
1985 // 64: mq
1986 // 65: lr
1987 // 66: ctr
1988 // 67: ap
1989 // 68-75 cr0-7
1990 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001991 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001992
1993 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001994 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001995
1996 // 109: vrsave
1997 // 110: vscr
1998 // 111: spe_acc
1999 // 112: spefscr
2000 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002001 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002002
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002003 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002004}
2005
2006
Chris Lattner0cf24192010-06-28 20:05:43 +00002007//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002008// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002009//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002010
2011namespace {
2012
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002013class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002014public:
2015 enum ABIKind {
2016 APCS = 0,
2017 AAPCS = 1,
2018 AAPCS_VFP
2019 };
2020
2021private:
2022 ABIKind Kind;
2023
2024public:
Chris Lattner2b037972010-07-29 02:01:43 +00002025 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002026
2027private:
2028 ABIKind getABIKind() const { return Kind; }
2029
Chris Lattner458b2aa2010-07-29 02:16:43 +00002030 ABIArgInfo classifyReturnType(QualType RetTy) const;
2031 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002032
Chris Lattner22326a12010-07-29 02:31:05 +00002033 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002034
2035 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2036 CodeGenFunction &CGF) const;
2037};
2038
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002039class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2040public:
Chris Lattner2b037972010-07-29 02:01:43 +00002041 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2042 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002043
2044 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2045 return 13;
2046 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002047};
2048
Daniel Dunbard59655c2009-09-12 00:59:49 +00002049}
2050
Chris Lattner22326a12010-07-29 02:31:05 +00002051void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002052 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002053 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002054 it != ie; ++it)
2055 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002056
Chris Lattner458b2aa2010-07-29 02:16:43 +00002057 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002058 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002059 if (Triple.getEnvironmentName() == "gnueabi" ||
2060 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002061 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002062 else
2063 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002064
Daniel Dunbar020daa92009-09-12 01:00:39 +00002065 switch (getABIKind()) {
2066 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002067 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2068 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002069 break;
2070
2071 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002072 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2073 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002074 break;
2075
2076 case AAPCS_VFP:
2077 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2078 break;
2079 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002080}
2081
Chris Lattner458b2aa2010-07-29 02:16:43 +00002082ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002083 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002084 // Treat an enum type as its underlying type.
2085 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2086 Ty = EnumTy->getDecl()->getIntegerType();
2087
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002088 return (Ty->isPromotableIntegerType() ?
2089 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002090 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002091
Daniel Dunbar09d33622009-09-14 21:54:03 +00002092 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002093 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002094 return ABIArgInfo::getIgnore();
2095
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002096 // Structures with either a non-trivial destructor or a non-trivial
2097 // copy constructor are always indirect.
2098 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2099 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2100
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002101 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2102 // backend doesn't support byval.
2103 // FIXME: This doesn't handle alignment > 64 bits.
2104 const llvm::Type* ElemTy;
2105 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002106 if (getContext().getTypeAlign(Ty) > 32) {
2107 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2108 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002109 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002110 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2111 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002112 }
2113 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002114 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002115 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2116 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002117 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002118}
2119
Chris Lattner458b2aa2010-07-29 02:16:43 +00002120static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002121 llvm::LLVMContext &VMContext) {
2122 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2123 // is called integer-like if its size is less than or equal to one word, and
2124 // the offset of each of its addressable sub-fields is zero.
2125
2126 uint64_t Size = Context.getTypeSize(Ty);
2127
2128 // Check that the type fits in a word.
2129 if (Size > 32)
2130 return false;
2131
2132 // FIXME: Handle vector types!
2133 if (Ty->isVectorType())
2134 return false;
2135
Daniel Dunbard53bac72009-09-14 02:20:34 +00002136 // Float types are never treated as "integer like".
2137 if (Ty->isRealFloatingType())
2138 return false;
2139
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002140 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002141 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002142 return true;
2143
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002144 // Small complex integer types are "integer like".
2145 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2146 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002147
2148 // Single element and zero sized arrays should be allowed, by the definition
2149 // above, but they are not.
2150
2151 // Otherwise, it must be a record type.
2152 const RecordType *RT = Ty->getAs<RecordType>();
2153 if (!RT) return false;
2154
2155 // Ignore records with flexible arrays.
2156 const RecordDecl *RD = RT->getDecl();
2157 if (RD->hasFlexibleArrayMember())
2158 return false;
2159
2160 // Check that all sub-fields are at offset 0, and are themselves "integer
2161 // like".
2162 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2163
2164 bool HadField = false;
2165 unsigned idx = 0;
2166 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2167 i != e; ++i, ++idx) {
2168 const FieldDecl *FD = *i;
2169
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002170 // Bit-fields are not addressable, we only need to verify they are "integer
2171 // like". We still have to disallow a subsequent non-bitfield, for example:
2172 // struct { int : 0; int x }
2173 // is non-integer like according to gcc.
2174 if (FD->isBitField()) {
2175 if (!RD->isUnion())
2176 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002177
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002178 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2179 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002180
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002181 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002182 }
2183
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002184 // Check if this field is at offset 0.
2185 if (Layout.getFieldOffset(idx) != 0)
2186 return false;
2187
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002188 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2189 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002190
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002191 // Only allow at most one field in a structure. This doesn't match the
2192 // wording above, but follows gcc in situations with a field following an
2193 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002194 if (!RD->isUnion()) {
2195 if (HadField)
2196 return false;
2197
2198 HadField = true;
2199 }
2200 }
2201
2202 return true;
2203}
2204
Chris Lattner458b2aa2010-07-29 02:16:43 +00002205ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002206 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002207 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002208
John McCalla1dee5302010-08-22 10:59:02 +00002209 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002210 // Treat an enum type as its underlying type.
2211 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2212 RetTy = EnumTy->getDecl()->getIntegerType();
2213
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002214 return (RetTy->isPromotableIntegerType() ?
2215 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002216 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002217
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002218 // Structures with either a non-trivial destructor or a non-trivial
2219 // copy constructor are always indirect.
2220 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2221 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2222
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002223 // Are we following APCS?
2224 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002225 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002226 return ABIArgInfo::getIgnore();
2227
Daniel Dunbareedf1512010-02-01 23:31:19 +00002228 // Complex types are all returned as packed integers.
2229 //
2230 // FIXME: Consider using 2 x vector types if the back end handles them
2231 // correctly.
2232 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002233 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002234 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002235
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002236 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002237 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002238 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002239 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002240 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002241 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002242 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002243 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2244 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002245 }
2246
2247 // Otherwise return in memory.
2248 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002249 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002250
2251 // Otherwise this is an AAPCS variant.
2252
Chris Lattner458b2aa2010-07-29 02:16:43 +00002253 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002254 return ABIArgInfo::getIgnore();
2255
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002256 // Aggregates <= 4 bytes are returned in r0; other aggregates
2257 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002258 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002259 if (Size <= 32) {
2260 // Return in the smallest viable integer type.
2261 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002262 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002263 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002264 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2265 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002266 }
2267
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002268 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002269}
2270
2271llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002272 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002273 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002274 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002275 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002276
2277 CGBuilderTy &Builder = CGF.Builder;
2278 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2279 "ap");
2280 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2281 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002282 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002283 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2284
2285 uint64_t Offset =
2286 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2287 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002288 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002289 "ap.next");
2290 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2291
2292 return AddrTyped;
2293}
2294
Chris Lattner458b2aa2010-07-29 02:16:43 +00002295ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2296 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002297 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002298
John McCalla1dee5302010-08-22 10:59:02 +00002299 if (isAggregateTypeForABI(RetTy))
Chris Lattner458b2aa2010-07-29 02:16:43 +00002300 return ABIArgInfo::getIndirect(0);
2301
2302 // Treat an enum type as its underlying type.
2303 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2304 RetTy = EnumTy->getDecl()->getIntegerType();
2305
2306 return (RetTy->isPromotableIntegerType() ?
2307 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002308}
2309
Chris Lattner0cf24192010-06-28 20:05:43 +00002310//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002311// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002312//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002313
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002314namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002315
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002316class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002317public:
2318 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2319
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002320 bool isPromotableIntegerType(QualType Ty) const;
2321
Chris Lattner458b2aa2010-07-29 02:16:43 +00002322 ABIArgInfo classifyReturnType(QualType RetTy) const;
2323 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002324
Chris Lattner22326a12010-07-29 02:31:05 +00002325 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002326 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002327 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2328 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002329 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002330 }
2331
2332 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2333 CodeGenFunction &CGF) const;
2334};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002335
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002336class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2337public:
Chris Lattner2b037972010-07-29 02:01:43 +00002338 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2339 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002340};
2341
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002342}
2343
2344bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2345 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002346 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002347 switch (BT->getKind()) {
2348 case BuiltinType::Bool:
2349 case BuiltinType::Char_S:
2350 case BuiltinType::Char_U:
2351 case BuiltinType::SChar:
2352 case BuiltinType::UChar:
2353 case BuiltinType::Short:
2354 case BuiltinType::UShort:
2355 case BuiltinType::Int:
2356 case BuiltinType::UInt:
2357 return true;
2358 default:
2359 return false;
2360 }
2361 return false;
2362}
2363
2364llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2365 CodeGenFunction &CGF) const {
2366 // FIXME: Implement
2367 return 0;
2368}
2369
2370
Chris Lattner458b2aa2010-07-29 02:16:43 +00002371ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2372 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002373 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002374 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002375 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002376
2377 return (isPromotableIntegerType(RetTy) ?
2378 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002379}
2380
Chris Lattner458b2aa2010-07-29 02:16:43 +00002381ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002382 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002383 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002384
2385 return (isPromotableIntegerType(Ty) ?
2386 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002387}
2388
Chris Lattner0cf24192010-06-28 20:05:43 +00002389//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002390// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002391//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002392
2393namespace {
2394
2395class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2396public:
Chris Lattner2b037972010-07-29 02:01:43 +00002397 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2398 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002399 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2400 CodeGen::CodeGenModule &M) const;
2401};
2402
2403}
2404
2405void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2406 llvm::GlobalValue *GV,
2407 CodeGen::CodeGenModule &M) const {
2408 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2409 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2410 // Handle 'interrupt' attribute:
2411 llvm::Function *F = cast<llvm::Function>(GV);
2412
2413 // Step 1: Set ISR calling convention.
2414 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2415
2416 // Step 2: Add attributes goodness.
2417 F->addFnAttr(llvm::Attribute::NoInline);
2418
2419 // Step 3: Emit ISR vector alias.
2420 unsigned Num = attr->getNumber() + 0xffe0;
2421 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2422 "vector_" +
2423 llvm::LowercaseString(llvm::utohexstr(Num)),
2424 GV, &M.getModule());
2425 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002426 }
2427}
2428
Chris Lattner0cf24192010-06-28 20:05:43 +00002429//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002430// MIPS ABI Implementation. This works for both little-endian and
2431// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002432//===----------------------------------------------------------------------===//
2433
John McCall943fae92010-05-27 06:19:26 +00002434namespace {
2435class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2436public:
Chris Lattner2b037972010-07-29 02:01:43 +00002437 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2438 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002439
2440 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2441 return 29;
2442 }
2443
2444 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002445 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002446};
2447}
2448
2449bool
2450MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2451 llvm::Value *Address) const {
2452 // This information comes from gcc's implementation, which seems to
2453 // as canonical as it gets.
2454
2455 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2456 llvm::LLVMContext &Context = CGF.getLLVMContext();
2457
2458 // Everything on MIPS is 4 bytes. Double-precision FP registers
2459 // are aliased to pairs of single-precision FP registers.
2460 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2461 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2462
2463 // 0-31 are the general purpose registers, $0 - $31.
2464 // 32-63 are the floating-point registers, $f0 - $f31.
2465 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2466 // 66 is the (notional, I think) register for signal-handler return.
2467 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2468
2469 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2470 // They are one bit wide and ignored here.
2471
2472 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2473 // (coprocessor 1 is the FP unit)
2474 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2475 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2476 // 176-181 are the DSP accumulator registers.
2477 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2478
2479 return false;
2480}
2481
2482
Chris Lattner2b037972010-07-29 02:01:43 +00002483const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002484 if (TheTargetCodeGenInfo)
2485 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002486
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002487 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2488 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002489
Chris Lattner22a931e2010-06-29 06:01:59 +00002490 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002491 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002492 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002493 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002494
John McCall943fae92010-05-27 06:19:26 +00002495 case llvm::Triple::mips:
2496 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002497 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002498
Daniel Dunbard59655c2009-09-12 00:59:49 +00002499 case llvm::Triple::arm:
2500 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002501 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002502 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002503 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002504 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002505
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002506 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002507 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002508
2509 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002510 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002511
John McCallea8d8bb2010-03-11 00:10:12 +00002512 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002513 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002514
Daniel Dunbard59655c2009-09-12 00:59:49 +00002515 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002516 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002517
2518 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002519 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002520
Daniel Dunbar40165182009-08-24 09:10:05 +00002521 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002522 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002523 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002524 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002525 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002526 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002527 case llvm::Triple::MinGW32:
2528 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002529 case llvm::Triple::AuroraUX:
2530 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002531 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002532 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002533 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002534 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002535
2536 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002537 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002538 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002539 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002540
Daniel Dunbare3532f82009-08-24 08:52:16 +00002541 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002542 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002543 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002544}