blob: 6ce824ba1896479b72e06d5b88dede557b48d8e0 [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
Anton Korobeynikov244360d2009-06-05 22:08:42 +000039ABIInfo::~ABIInfo() {}
40
Chris Lattner2b037972010-07-29 02:01:43 +000041ASTContext &ABIInfo::getContext() const {
42 return CGT.getContext();
43}
44
45llvm::LLVMContext &ABIInfo::getVMContext() const {
46 return CGT.getLLVMContext();
47}
48
49const llvm::TargetData &ABIInfo::getTargetData() const {
50 return CGT.getTargetData();
51}
52
53
Anton Korobeynikov244360d2009-06-05 22:08:42 +000054void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000055 llvm::raw_ostream &OS = llvm::errs();
56 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000057 switch (TheKind) {
58 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000059 OS << "Direct Type=";
60 if (const llvm::Type *Ty = getCoerceToType())
61 Ty->print(OS);
62 else
63 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000065 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000066 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000067 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000068 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000069 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000070 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000071 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000072 OS << "Indirect Align=" << getIndirectAlign()
73 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000074 break;
75 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000076 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000077 break;
78 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000079 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080}
81
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000082TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
83
Daniel Dunbar626f1d82009-09-13 08:03:58 +000084static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000085
86/// isEmptyField - Return true iff a the field is "empty", that is it
87/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000088static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
89 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090 if (FD->isUnnamedBitfield())
91 return true;
92
93 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000094
Daniel Dunbar626f1d82009-09-13 08:03:58 +000095 // Constant arrays of empty records count as empty, strip them off.
96 if (AllowArrays)
97 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
98 FT = AT->getElementType();
99
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000100 const RecordType *RT = FT->getAs<RecordType>();
101 if (!RT)
102 return false;
103
104 // C++ record fields are never empty, at least in the Itanium ABI.
105 //
106 // FIXME: We should use a predicate for whether this behavior is true in the
107 // current ABI.
108 if (isa<CXXRecordDecl>(RT->getDecl()))
109 return false;
110
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000111 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000112}
113
114/// isEmptyRecord - Return true iff a structure contains only empty
115/// fields. Note that a structure with a flexible array member is not
116/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000117static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000118 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000119 if (!RT)
120 return 0;
121 const RecordDecl *RD = RT->getDecl();
122 if (RD->hasFlexibleArrayMember())
123 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000124
125 // If this is a C++ record, check the bases first.
126 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
127 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
128 e = CXXRD->bases_end(); i != e; ++i)
129 if (!isEmptyRecord(Context, i->getType(), true))
130 return false;
131
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000132 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000134 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000135 return false;
136 return true;
137}
138
Anders Carlsson20759ad2009-09-16 15:53:40 +0000139/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
140/// a non-trivial destructor or a non-trivial copy constructor.
141static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
142 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
143 if (!RD)
144 return false;
145
146 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
147}
148
149/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
150/// a record type with either a non-trivial destructor or a non-trivial copy
151/// constructor.
152static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
153 const RecordType *RT = T->getAs<RecordType>();
154 if (!RT)
155 return false;
156
157 return hasNonTrivialDestructorOrCopyConstructor(RT);
158}
159
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000160/// isSingleElementStruct - Determine if a structure is a "single
161/// element struct", i.e. it has exactly one non-empty field or
162/// exactly one field which is itself a single element
163/// struct. Structures with flexible array members are never
164/// considered single element structs.
165///
166/// \return The field declaration for the single non-empty field, if
167/// it exists.
168static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
169 const RecordType *RT = T->getAsStructureType();
170 if (!RT)
171 return 0;
172
173 const RecordDecl *RD = RT->getDecl();
174 if (RD->hasFlexibleArrayMember())
175 return 0;
176
177 const Type *Found = 0;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000178
179 // If this is a C++ record, check the bases first.
180 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
181 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
182 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000183 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000184 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000185 continue;
186
187 // If we already found an element then this isn't a single-element struct.
188 if (Found)
189 return 0;
190
191 // If this is non-empty and not a single element struct, the composite
192 // cannot be a single element struct.
193 Found = isSingleElementStruct(i->getType(), Context);
194 if (!Found)
195 return 0;
196 }
197 }
198
199 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000200 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
201 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000202 const FieldDecl *FD = *i;
203 QualType FT = FD->getType();
204
205 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000206 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000207 continue;
208
209 // If we already found an element then this isn't a single-element
210 // struct.
211 if (Found)
212 return 0;
213
214 // Treat single element arrays as the element.
215 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
216 if (AT->getSize().getZExtValue() != 1)
217 break;
218 FT = AT->getElementType();
219 }
220
221 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
222 Found = FT.getTypePtr();
223 } else {
224 Found = isSingleElementStruct(FT, Context);
225 if (!Found)
226 return 0;
227 }
228 }
229
230 return Found;
231}
232
233static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000234 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000235 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
236 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000237 return false;
238
239 uint64_t Size = Context.getTypeSize(Ty);
240 return Size == 32 || Size == 64;
241}
242
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000243/// canExpandIndirectArgument - Test whether an argument type which is to be
244/// passed indirectly (on the stack) would have the equivalent layout if it was
245/// expanded into separate arguments. If so, we prefer to do the latter to avoid
246/// inhibiting optimizations.
247///
248// FIXME: This predicate is missing many cases, currently it just follows
249// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
250// should probably make this smarter, or better yet make the LLVM backend
251// capable of handling it.
252static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
253 // We can only expand structure types.
254 const RecordType *RT = Ty->getAs<RecordType>();
255 if (!RT)
256 return false;
257
258 // We can only expand (C) structures.
259 //
260 // FIXME: This needs to be generalized to handle classes as well.
261 const RecordDecl *RD = RT->getDecl();
262 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
263 return false;
264
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000265 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
266 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000267 const FieldDecl *FD = *i;
268
269 if (!is32Or64BitBasicType(FD->getType(), Context))
270 return false;
271
272 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
273 // how to expand them yet, and the predicate for telling if a bitfield still
274 // counts as "basic" is more complicated than what we were doing previously.
275 if (FD->isBitField())
276 return false;
277 }
278
279 return true;
280}
281
282namespace {
283/// DefaultABIInfo - The default implementation for ABI specific
284/// details. This implementation provides information which results in
285/// self-consistent and sensible LLVM IR generation, but does not
286/// conform to any particular ABI.
287class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000288public:
289 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
290
Chris Lattner458b2aa2010-07-29 02:16:43 +0000291 ABIArgInfo classifyReturnType(QualType RetTy) const;
292 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000293
Chris Lattner22326a12010-07-29 02:31:05 +0000294 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000295 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000296 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
297 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000298 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299 }
300
301 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
302 CodeGenFunction &CGF) const;
303};
304
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000305class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
306public:
Chris Lattner2b037972010-07-29 02:01:43 +0000307 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
308 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000309};
310
311llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
312 CodeGenFunction &CGF) const {
313 return 0;
314}
315
Chris Lattner458b2aa2010-07-29 02:16:43 +0000316ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000317 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000318 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000319
Chris Lattner9723d6c2010-03-11 18:19:55 +0000320 // Treat an enum type as its underlying type.
321 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
322 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000323
Chris Lattner9723d6c2010-03-11 18:19:55 +0000324 return (Ty->isPromotableIntegerType() ?
325 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000326}
327
Chris Lattner0cf24192010-06-28 20:05:43 +0000328//===----------------------------------------------------------------------===//
329// X86-32 ABI Implementation
330//===----------------------------------------------------------------------===//
331
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000332/// X86_32ABIInfo - The X86-32 ABI information.
333class X86_32ABIInfo : public ABIInfo {
David Chisnallde3a0692009-08-17 23:08:21 +0000334 bool IsDarwinVectorABI;
335 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000336
337 static bool isRegisterSize(unsigned Size) {
338 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
339 }
340
341 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
342
Daniel Dunbar557893d2010-04-21 19:10:51 +0000343 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
344 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000345 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000346
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000347public:
Chris Lattner2b037972010-07-29 02:01:43 +0000348
Chris Lattner458b2aa2010-07-29 02:16:43 +0000349 ABIArgInfo classifyReturnType(QualType RetTy) const;
350 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000351
Chris Lattner22326a12010-07-29 02:31:05 +0000352 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000353 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000354 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
355 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000356 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000357 }
358
359 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
360 CodeGenFunction &CGF) const;
361
Chris Lattner2b037972010-07-29 02:01:43 +0000362 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
363 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000364};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000365
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000366class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
367public:
Chris Lattner2b037972010-07-29 02:01:43 +0000368 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
369 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000370
371 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
372 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000373
374 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
375 // Darwin uses different dwarf register numbers for EH.
376 if (CGM.isTargetDarwin()) return 5;
377
378 return 4;
379 }
380
381 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
382 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000383};
384
385}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000386
387/// shouldReturnTypeInRegister - Determine if the given type should be
388/// passed in a register (for the Darwin ABI).
389bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
390 ASTContext &Context) {
391 uint64_t Size = Context.getTypeSize(Ty);
392
393 // Type must be register sized.
394 if (!isRegisterSize(Size))
395 return false;
396
397 if (Ty->isVectorType()) {
398 // 64- and 128- bit vectors inside structures are not returned in
399 // registers.
400 if (Size == 64 || Size == 128)
401 return false;
402
403 return true;
404 }
405
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000406 // If this is a builtin, pointer, enum, complex type, member pointer, or
407 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000408 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000409 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000410 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000411 return true;
412
413 // Arrays are treated like records.
414 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
415 return shouldReturnTypeInRegister(AT->getElementType(), Context);
416
417 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000418 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000419 if (!RT) return false;
420
Anders Carlsson40446e82010-01-27 03:25:19 +0000421 // FIXME: Traverse bases here too.
422
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000423 // Structure types are passed in register if all fields would be
424 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000425 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
426 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000427 const FieldDecl *FD = *i;
428
429 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000430 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000431 continue;
432
433 // Check fields recursively.
434 if (!shouldReturnTypeInRegister(FD->getType(), Context))
435 return false;
436 }
437
438 return true;
439}
440
Chris Lattner458b2aa2010-07-29 02:16:43 +0000441ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
442 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000443 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000444
445 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000446 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000447 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000448 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449
450 // 128-bit vectors are a special case; they are returned in
451 // registers and we need to make sure to pick a type the LLVM
452 // backend will like.
453 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000454 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000455 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000456
457 // Always return in register if it fits in a general purpose
458 // register, or if it is 64 bits and has a single element.
459 if ((Size == 8 || Size == 16 || Size == 32) ||
460 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000461 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000462 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000463
464 return ABIArgInfo::getIndirect(0);
465 }
466
467 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000468 }
469
470 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000471 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000472 // Structures with either a non-trivial destructor or a non-trivial
473 // copy constructor are always indirect.
474 if (hasNonTrivialDestructorOrCopyConstructor(RT))
475 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
476
477 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000478 if (RT->getDecl()->hasFlexibleArrayMember())
479 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000480 }
481
David Chisnallde3a0692009-08-17 23:08:21 +0000482 // If specified, structs and unions are always indirect.
483 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000484 return ABIArgInfo::getIndirect(0);
485
486 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000487 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000488 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000489 if (BT->isIntegerType()) {
490 // We need to use the size of the structure, padding
491 // bit-fields can adjust that to be larger than the single
492 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000493 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000494 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000495 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
496 }
497
498 if (BT->getKind() == BuiltinType::Float) {
499 assert(getContext().getTypeSize(RetTy) ==
500 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000501 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000502 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000503 }
504
505 if (BT->getKind() == BuiltinType::Double) {
506 assert(getContext().getTypeSize(RetTy) ==
507 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000508 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000509 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000510 }
511 } else if (SeltTy->isPointerType()) {
512 // FIXME: It would be really nice if this could come out as the proper
513 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000514 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000515 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000516 } else if (SeltTy->isVectorType()) {
517 // 64- and 128-bit vectors are never returned in a
518 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000519 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000520 if (Size == 64 || Size == 128)
521 return ABIArgInfo::getIndirect(0);
522
Chris Lattner458b2aa2010-07-29 02:16:43 +0000523 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000524 }
525 }
526
527 // Small structures which are register sized are generally returned
528 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000529 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
530 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000531 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000532 }
533
534 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000535 }
Chris Lattner458b2aa2010-07-29 02:16:43 +0000536
537 // Treat an enum type as its underlying type.
538 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
539 RetTy = EnumTy->getDecl()->getIntegerType();
540
541 return (RetTy->isPromotableIntegerType() ?
542 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000543}
544
Chris Lattner458b2aa2010-07-29 02:16:43 +0000545ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000546 if (!ByVal)
547 return ABIArgInfo::getIndirect(0, false);
548
549 // Compute the byval alignment. We trust the back-end to honor the
550 // minimum ABI alignment for byval, to make cleaner IR.
551 const unsigned MinABIAlign = 4;
Chris Lattner458b2aa2010-07-29 02:16:43 +0000552 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000553 if (Align > MinABIAlign)
554 return ABIArgInfo::getIndirect(Align);
555 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000556}
557
Chris Lattner458b2aa2010-07-29 02:16:43 +0000558ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000559 // FIXME: Set alignment on indirect arguments.
560 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
561 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000562 if (const RecordType *RT = Ty->getAs<RecordType>()) {
563 // Structures with either a non-trivial destructor or a non-trivial
564 // copy constructor are always indirect.
565 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000566 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000567
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000568 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000569 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000570 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000571
572 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000573 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000574 return ABIArgInfo::getIgnore();
575
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000576 // Expand small (<= 128-bit) record types when we know that the stack layout
577 // of those arguments will match the struct. This is important because the
578 // LLVM backend isn't smart enough to remove byval, which inhibits many
579 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000580 if (getContext().getTypeSize(Ty) <= 4*32 &&
581 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000582 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000583
Chris Lattner458b2aa2010-07-29 02:16:43 +0000584 return getIndirectResult(Ty);
585 }
586
587 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
588 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000589
Chris Lattner458b2aa2010-07-29 02:16:43 +0000590 return (Ty->isPromotableIntegerType() ?
591 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000592}
593
594llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
595 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000596 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000597 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000598
599 CGBuilderTy &Builder = CGF.Builder;
600 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
601 "ap");
602 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
603 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000604 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000605 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
606
607 uint64_t Offset =
608 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
609 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000610 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000611 "ap.next");
612 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
613
614 return AddrTyped;
615}
616
Charles Davis4ea31ab2010-02-13 15:54:06 +0000617void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
618 llvm::GlobalValue *GV,
619 CodeGen::CodeGenModule &CGM) const {
620 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
621 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
622 // Get the LLVM function.
623 llvm::Function *Fn = cast<llvm::Function>(GV);
624
625 // Now add the 'alignstack' attribute with a value of 16.
626 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
627 }
628 }
629}
630
John McCallbeec5a02010-03-06 00:35:14 +0000631bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
632 CodeGen::CodeGenFunction &CGF,
633 llvm::Value *Address) const {
634 CodeGen::CGBuilderTy &Builder = CGF.Builder;
635 llvm::LLVMContext &Context = CGF.getLLVMContext();
636
637 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
638 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
639
640 // 0-7 are the eight integer registers; the order is different
641 // on Darwin (for EH), but the range is the same.
642 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000643 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000644
645 if (CGF.CGM.isTargetDarwin()) {
646 // 12-16 are st(0..4). Not sure why we stop at 4.
647 // These have size 16, which is sizeof(long double) on
648 // platforms with 8-byte alignment for that type.
649 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000650 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000651
652 } else {
653 // 9 is %eflags, which doesn't get a size on Darwin for some
654 // reason.
655 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
656
657 // 11-16 are st(0..5). Not sure why we stop at 5.
658 // These have size 12, which is sizeof(long double) on
659 // platforms with 4-byte alignment for that type.
660 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000661 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
662 }
John McCallbeec5a02010-03-06 00:35:14 +0000663
664 return false;
665}
666
Chris Lattner0cf24192010-06-28 20:05:43 +0000667//===----------------------------------------------------------------------===//
668// X86-64 ABI Implementation
669//===----------------------------------------------------------------------===//
670
671
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000672namespace {
673/// X86_64ABIInfo - The X86_64 ABI information.
674class X86_64ABIInfo : public ABIInfo {
675 enum Class {
676 Integer = 0,
677 SSE,
678 SSEUp,
679 X87,
680 X87Up,
681 ComplexX87,
682 NoClass,
683 Memory
684 };
685
686 /// merge - Implement the X86_64 ABI merging algorithm.
687 ///
688 /// Merge an accumulating classification \arg Accum with a field
689 /// classification \arg Field.
690 ///
691 /// \param Accum - The accumulating classification. This should
692 /// always be either NoClass or the result of a previous merge
693 /// call. In addition, this should never be Memory (the caller
694 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000695 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000696
697 /// classify - Determine the x86_64 register classes in which the
698 /// given type T should be passed.
699 ///
700 /// \param Lo - The classification for the parts of the type
701 /// residing in the low word of the containing object.
702 ///
703 /// \param Hi - The classification for the parts of the type
704 /// residing in the high word of the containing object.
705 ///
706 /// \param OffsetBase - The bit offset of this type in the
707 /// containing object. Some parameters are classified different
708 /// depending on whether they straddle an eightbyte boundary.
709 ///
710 /// If a word is unused its result will be NoClass; if a type should
711 /// be passed in Memory then at least the classification of \arg Lo
712 /// will be Memory.
713 ///
714 /// The \arg Lo class will be NoClass iff the argument is ignored.
715 ///
716 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
717 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000718 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000719
Chris Lattner4200fe42010-07-29 04:56:46 +0000720 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000721 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000722 unsigned IROffset, QualType SourceTy,
723 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000724 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
725 unsigned IROffset, QualType SourceTy,
726 unsigned SourceOffset) const;
Chris Lattnerc11301c2010-07-29 02:20:19 +0000727
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000728 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
729 /// to coerce to, chose the best way to pass Ty in the same place
730 /// that \arg CoerceTo would be passed, but while keeping the
731 /// emitted code as simple as possible.
732 ///
733 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
734 /// the ways we might want to pass things, instead of constructing an LLVM
735 /// type. This makes this code more explicit, and it makes it clearer that we
736 /// are also doing this for correctness in the case of passing scalar types.
737 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000738 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000739
740 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000741 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000742 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000743
744 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000745 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000746 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000747
Chris Lattner458b2aa2010-07-29 02:16:43 +0000748 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000749
Chris Lattner029c0f12010-07-29 04:41:05 +0000750 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
751 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000752
753public:
Chris Lattner2b037972010-07-29 02:01:43 +0000754 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000755
Chris Lattner22326a12010-07-29 02:31:05 +0000756 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000757
758 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
759 CodeGenFunction &CGF) const;
760};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000761
762class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
763public:
Chris Lattner2b037972010-07-29 02:01:43 +0000764 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
765 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000766
767 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
768 return 7;
769 }
770
771 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
772 llvm::Value *Address) const {
773 CodeGen::CGBuilderTy &Builder = CGF.Builder;
774 llvm::LLVMContext &Context = CGF.getLLVMContext();
775
776 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
777 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
778
John McCall943fae92010-05-27 06:19:26 +0000779 // 0-15 are the 16 integer registers.
780 // 16 is %rip.
781 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000782
783 return false;
784 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000785};
786
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000787}
788
Chris Lattnerd776fb12010-06-28 21:43:59 +0000789X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000790 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
791 // classified recursively so that always two fields are
792 // considered. The resulting class is calculated according to
793 // the classes of the fields in the eightbyte:
794 //
795 // (a) If both classes are equal, this is the resulting class.
796 //
797 // (b) If one of the classes is NO_CLASS, the resulting class is
798 // the other class.
799 //
800 // (c) If one of the classes is MEMORY, the result is the MEMORY
801 // class.
802 //
803 // (d) If one of the classes is INTEGER, the result is the
804 // INTEGER.
805 //
806 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
807 // MEMORY is used as class.
808 //
809 // (f) Otherwise class SSE is used.
810
811 // Accum should never be memory (we should have returned) or
812 // ComplexX87 (because this cannot be passed in a structure).
813 assert((Accum != Memory && Accum != ComplexX87) &&
814 "Invalid accumulated classification during merge.");
815 if (Accum == Field || Field == NoClass)
816 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000817 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000818 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000819 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000820 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000821 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000822 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000823 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
824 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000825 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000826 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000827}
828
Chris Lattner5c740f12010-06-30 19:14:05 +0000829void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000830 Class &Lo, Class &Hi) const {
831 // FIXME: This code can be simplified by introducing a simple value class for
832 // Class pairs with appropriate constructor methods for the various
833 // situations.
834
835 // FIXME: Some of the split computations are wrong; unaligned vectors
836 // shouldn't be passed in registers for example, so there is no chance they
837 // can straddle an eightbyte. Verify & simplify.
838
839 Lo = Hi = NoClass;
840
841 Class &Current = OffsetBase < 64 ? Lo : Hi;
842 Current = Memory;
843
John McCall9dd450b2009-09-21 23:43:11 +0000844 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000845 BuiltinType::Kind k = BT->getKind();
846
847 if (k == BuiltinType::Void) {
848 Current = NoClass;
849 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
850 Lo = Integer;
851 Hi = Integer;
852 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
853 Current = Integer;
854 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
855 Current = SSE;
856 } else if (k == BuiltinType::LongDouble) {
857 Lo = X87;
858 Hi = X87Up;
859 }
860 // FIXME: _Decimal32 and _Decimal64 are SSE.
861 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000862 return;
863 }
864
865 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000866 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000867 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000868 return;
869 }
870
871 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000872 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000873 return;
874 }
875
876 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000877 if (Ty->isMemberFunctionPointerType())
878 Lo = Hi = Integer;
879 else
880 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000881 return;
882 }
883
884 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000885 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000886 if (Size == 32) {
887 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
888 // float> as integer.
889 Current = Integer;
890
891 // If this type crosses an eightbyte boundary, it should be
892 // split.
893 uint64_t EB_Real = (OffsetBase) / 64;
894 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
895 if (EB_Real != EB_Imag)
896 Hi = Lo;
897 } else if (Size == 64) {
898 // gcc passes <1 x double> in memory. :(
899 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
900 return;
901
902 // gcc passes <1 x long long> as INTEGER.
903 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
904 Current = Integer;
905 else
906 Current = SSE;
907
908 // If this type crosses an eightbyte boundary, it should be
909 // split.
910 if (OffsetBase && OffsetBase != 64)
911 Hi = Lo;
912 } else if (Size == 128) {
913 Lo = SSE;
914 Hi = SSEUp;
915 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000916 return;
917 }
918
919 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000920 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000921
Chris Lattner2b037972010-07-29 02:01:43 +0000922 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000923 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000924 if (Size <= 64)
925 Current = Integer;
926 else if (Size <= 128)
927 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000928 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000930 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000931 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000932 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000933 Current = ComplexX87;
934
935 // If this complex type crosses an eightbyte boundary then it
936 // should be split.
937 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000938 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000939 if (Hi == NoClass && EB_Real != EB_Imag)
940 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000941
942 return;
943 }
944
Chris Lattner2b037972010-07-29 02:01:43 +0000945 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000946 // Arrays are treated like structures.
947
Chris Lattner2b037972010-07-29 02:01:43 +0000948 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000949
950 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
951 // than two eightbytes, ..., it has class MEMORY.
952 if (Size > 128)
953 return;
954
955 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
956 // fields, it has class MEMORY.
957 //
958 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000959 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000960 return;
961
962 // Otherwise implement simplified merge. We could be smarter about
963 // this, but it isn't worth it and would be harder to verify.
964 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000965 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000966 uint64_t ArraySize = AT->getSize().getZExtValue();
967 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
968 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000969 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000970 Lo = merge(Lo, FieldLo);
971 Hi = merge(Hi, FieldHi);
972 if (Lo == Memory || Hi == Memory)
973 break;
974 }
975
976 // Do post merger cleanup (see below). Only case we worry about is Memory.
977 if (Hi == Memory)
978 Lo = Memory;
979 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000980 return;
981 }
982
983 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000984 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000985
986 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
987 // than two eightbytes, ..., it has class MEMORY.
988 if (Size > 128)
989 return;
990
Anders Carlsson20759ad2009-09-16 15:53:40 +0000991 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
992 // copy constructor or a non-trivial destructor, it is passed by invisible
993 // reference.
994 if (hasNonTrivialDestructorOrCopyConstructor(RT))
995 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000996
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000997 const RecordDecl *RD = RT->getDecl();
998
999 // Assume variable sized types are passed in memory.
1000 if (RD->hasFlexibleArrayMember())
1001 return;
1002
Chris Lattner2b037972010-07-29 02:01:43 +00001003 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001004
1005 // Reset Lo class, this will be recomputed.
1006 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001007
1008 // If this is a C++ record, classify the bases first.
1009 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1010 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1011 e = CXXRD->bases_end(); i != e; ++i) {
1012 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1013 "Unexpected base class!");
1014 const CXXRecordDecl *Base =
1015 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1016
1017 // Classify this field.
1018 //
1019 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1020 // single eightbyte, each is classified separately. Each eightbyte gets
1021 // initialized to class NO_CLASS.
1022 Class FieldLo, FieldHi;
1023 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001024 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001025 Lo = merge(Lo, FieldLo);
1026 Hi = merge(Hi, FieldHi);
1027 if (Lo == Memory || Hi == Memory)
1028 break;
1029 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001030
Chris Lattnercd840842010-07-29 17:04:54 +00001031 // If this record has no fields, no bases, no vtable, but isn't empty,
1032 // classify as INTEGER.
1033 if (CXXRD->isEmpty() && Size)
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001034 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001035 }
1036
1037 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001038 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001039 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1040 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001041 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1042 bool BitField = i->isBitField();
1043
1044 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1045 // fields, it has class MEMORY.
1046 //
1047 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001048 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001049 Lo = Memory;
1050 return;
1051 }
1052
1053 // Classify this field.
1054 //
1055 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1056 // exceeds a single eightbyte, each is classified
1057 // separately. Each eightbyte gets initialized to class
1058 // NO_CLASS.
1059 Class FieldLo, FieldHi;
1060
1061 // Bit-fields require special handling, they do not force the
1062 // structure to be passed in memory even if unaligned, and
1063 // therefore they can straddle an eightbyte.
1064 if (BitField) {
1065 // Ignore padding bit-fields.
1066 if (i->isUnnamedBitfield())
1067 continue;
1068
1069 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001070 uint64_t Size =
1071 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001072
1073 uint64_t EB_Lo = Offset / 64;
1074 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1075 FieldLo = FieldHi = NoClass;
1076 if (EB_Lo) {
1077 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1078 FieldLo = NoClass;
1079 FieldHi = Integer;
1080 } else {
1081 FieldLo = Integer;
1082 FieldHi = EB_Hi ? Integer : NoClass;
1083 }
1084 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001085 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001086 Lo = merge(Lo, FieldLo);
1087 Hi = merge(Hi, FieldHi);
1088 if (Lo == Memory || Hi == Memory)
1089 break;
1090 }
1091
1092 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1093 //
1094 // (a) If one of the classes is MEMORY, the whole argument is
1095 // passed in memory.
1096 //
1097 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1098
1099 // The first of these conditions is guaranteed by how we implement
1100 // the merge (just bail).
1101 //
1102 // The second condition occurs in the case of unions; for example
1103 // union { _Complex double; unsigned; }.
1104 if (Hi == Memory)
1105 Lo = Memory;
1106 if (Hi == SSEUp && Lo != SSE)
1107 Hi = SSE;
1108 }
1109}
1110
1111ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001112 const llvm::Type *CoerceTo) const {
Chris Lattner4c1e4842010-07-28 22:15:08 +00001113 // If this is a pointer passed as a pointer, just pass it directly.
1114 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1115 Ty->hasPointerRepresentation())
1116 return ABIArgInfo::getExtend();
1117
1118 if (isa<llvm::IntegerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001119 // Integer and pointer types will end up in a general purpose
1120 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001121
1122 // Treat an enum type as its underlying type.
1123 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1124 Ty = EnumTy->getDecl()->getIntegerType();
1125
Chris Lattner4c1e4842010-07-28 22:15:08 +00001126 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001127 return (Ty->isPromotableIntegerType() ?
1128 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001129
Chris Lattnerfa20e952010-06-26 21:52:32 +00001130 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001131 assert(Ty.isCanonical() && "should always have a canonical type here");
1132 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001133
1134 // Float and double end up in a single SSE reg.
Chris Lattner2b037972010-07-29 02:01:43 +00001135 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001136 return ABIArgInfo::getDirect();
1137
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001138 // If this is a 32-bit structure that is passed as a double, then it will be
1139 // passed in the low 32-bits of the XMM register, which is the same as how a
1140 // float is passed. Coerce to a float instead of a double.
Chris Lattner2b037972010-07-29 02:01:43 +00001141 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001142 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001143 }
1144
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001145 return ABIArgInfo::getDirect(CoerceTo);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001146}
1147
Chris Lattner22a931e2010-06-29 06:01:59 +00001148ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001149 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1150 // place naturally.
1151 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1152 // Treat an enum type as its underlying type.
1153 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1154 Ty = EnumTy->getDecl()->getIntegerType();
1155
1156 return (Ty->isPromotableIntegerType() ?
1157 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1158 }
1159
1160 return ABIArgInfo::getIndirect(0);
1161}
1162
Chris Lattner22a931e2010-06-29 06:01:59 +00001163ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001164 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1165 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001166 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1167 // Treat an enum type as its underlying type.
1168 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1169 Ty = EnumTy->getDecl()->getIntegerType();
1170
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001171 return (Ty->isPromotableIntegerType() ?
1172 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001173 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001174
Daniel Dunbar53fac692010-04-21 19:49:55 +00001175 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1176 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001177
Daniel Dunbar53fac692010-04-21 19:49:55 +00001178 // Compute the byval alignment. We trust the back-end to honor the
1179 // minimum ABI alignment for byval, to make cleaner IR.
1180 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001181 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001182 if (Align > MinABIAlign)
1183 return ABIArgInfo::getIndirect(Align);
1184 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001185}
1186
Chris Lattner4200fe42010-07-29 04:56:46 +00001187/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1188/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1189/// vector register.
1190const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001191 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
1192
1193 // Wrapper structs that just contain vectors are passed just like vectors,
1194 // strip them off if present.
1195 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1196 while (STy && STy->getNumElements() == 1) {
1197 IRType = STy->getElementType(0);
1198 STy = dyn_cast<llvm::StructType>(IRType);
1199 }
1200
Chris Lattner4200fe42010-07-29 04:56:46 +00001201 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001202 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001203 const llvm::Type *EltTy = VT->getElementType();
1204 if (VT->getBitWidth() == 128 &&
1205 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1206 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1207 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1208 EltTy->isIntegerTy(128)))
1209 return VT;
1210 }
1211
1212 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1213}
1214
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001215/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1216/// is known to either be off the end of the specified type or being in
1217/// alignment padding. The user type specified is known to be at most 128 bits
1218/// in size, and have passed through X86_64ABIInfo::classify with a successful
1219/// classification that put one of the two halves in the INTEGER class.
1220///
1221/// It is conservatively correct to return false.
1222static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1223 unsigned EndBit, ASTContext &Context) {
1224 // If the bytes being queried are off the end of the type, there is no user
1225 // data hiding here. This handles analysis of builtins, vectors and other
1226 // types that don't contain interesting padding.
1227 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1228 if (TySize <= StartBit)
1229 return true;
1230
Chris Lattner98076a22010-07-29 07:43:55 +00001231 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1232 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1233 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1234
1235 // Check each element to see if the element overlaps with the queried range.
1236 for (unsigned i = 0; i != NumElts; ++i) {
1237 // If the element is after the span we care about, then we're done..
1238 unsigned EltOffset = i*EltSize;
1239 if (EltOffset >= EndBit) break;
1240
1241 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1242 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1243 EndBit-EltOffset, Context))
1244 return false;
1245 }
1246 // If it overlaps no elements, then it is safe to process as padding.
1247 return true;
1248 }
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001249
1250 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1251 const RecordDecl *RD = RT->getDecl();
1252 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1253
1254 // If this is a C++ record, check the bases first.
1255 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1256 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1257 e = CXXRD->bases_end(); i != e; ++i) {
1258 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1259 "Unexpected base class!");
1260 const CXXRecordDecl *Base =
1261 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1262
1263 // If the base is after the span we care about, ignore it.
1264 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1265 if (BaseOffset >= EndBit) continue;
1266
1267 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1268 if (!BitsContainNoUserData(i->getType(), BaseStart,
1269 EndBit-BaseOffset, Context))
1270 return false;
1271 }
1272 }
1273
1274 // Verify that no field has data that overlaps the region of interest. Yes
1275 // this could be sped up a lot by being smarter about queried fields,
1276 // however we're only looking at structs up to 16 bytes, so we don't care
1277 // much.
1278 unsigned idx = 0;
1279 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1280 i != e; ++i, ++idx) {
1281 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
1282
1283 // If we found a field after the region we care about, then we're done.
1284 if (FieldOffset >= EndBit) break;
1285
1286 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1287 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1288 Context))
1289 return false;
1290 }
1291
1292 // If nothing in this record overlapped the area of interest, then we're
1293 // clean.
1294 return true;
1295 }
1296
1297 return false;
1298}
1299
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001300
1301/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1302/// low 8 bytes of an XMM register, corresponding to the SSE class.
1303const llvm::Type *X86_64ABIInfo::
1304GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1305 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001306 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001307 // pass as float if the last 4 bytes is just padding. This happens for
1308 // structs that contain 3 floats.
1309 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1310 SourceOffset*8+64, getContext()))
1311 return llvm::Type::getFloatTy(getVMContext());
1312
1313 // FIXME: <2 x float> doesn't pass as one XMM register yet. Don't enable this
1314 // code until it does.
1315 //return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
1316
1317 return llvm::Type::getDoubleTy(getVMContext());
1318}
1319
1320
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001321/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1322/// an 8-byte GPR. This means that we either have a scalar or we are talking
1323/// about the high or low part of an up-to-16-byte struct. This routine picks
1324/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001325/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1326/// etc).
1327///
1328/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1329/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1330/// the 8-byte value references. PrefType may be null.
1331///
1332/// SourceTy is the source level type for the entire argument. SourceOffset is
1333/// an offset into this that we're processing (which is always either 0 or 8).
1334///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001335const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001336GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1337 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001338 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1339 // returning an 8-byte unit starting with it. See if we can safely use it.
1340 if (IROffset == 0) {
1341 // Pointers and int64's always fill the 8-byte unit.
1342 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1343 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001344
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001345 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1346 // goodness in the source type is just tail padding. This is allowed to
1347 // kick in for struct {double,int} on the int, but not on
1348 // struct{double,int,int} because we wouldn't return the second int. We
1349 // have to do this analysis on the source type because we can't depend on
1350 // unions being lowered a specific way etc.
1351 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1352 IRType->isIntegerTy(32)) {
1353 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
1354
1355 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1356 SourceOffset*8+64, getContext()))
1357 return IRType;
1358 }
1359 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001360
Chris Lattnerce1bd752010-07-29 04:51:12 +00001361 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001362 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001363 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001364 if (IROffset < SL->getSizeInBytes()) {
1365 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1366 IROffset -= SL->getElementOffset(FieldIdx);
1367
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001368 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1369 SourceTy, SourceOffset);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001370 }
1371 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001372
Chris Lattner98076a22010-07-29 07:43:55 +00001373 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1374 const llvm::Type *EltTy = ATy->getElementType();
1375 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1376 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001377 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1378 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001379 }
1380
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001381 // Okay, we don't have any better idea of what to pass, so we pass this in an
1382 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001383 unsigned TySizeInBytes =
1384 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001385
Chris Lattner3f763422010-07-29 17:34:39 +00001386 assert(TySizeInBytes != SourceOffset && "Empty field?");
1387
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001388 // It is always safe to classify this as an integer type up to i64 that
1389 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001390 return llvm::IntegerType::get(getVMContext(),
1391 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001392}
1393
Chris Lattner31faff52010-07-28 23:06:14 +00001394ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001395classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001396 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1397 // classification algorithm.
1398 X86_64ABIInfo::Class Lo, Hi;
1399 classify(RetTy, 0, Lo, Hi);
1400
1401 // Check some invariants.
1402 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1403 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1404 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1405
1406 const llvm::Type *ResType = 0;
1407 switch (Lo) {
1408 case NoClass:
1409 return ABIArgInfo::getIgnore();
1410
1411 case SSEUp:
1412 case X87Up:
1413 assert(0 && "Invalid classification for lo word.");
1414
1415 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1416 // hidden argument.
1417 case Memory:
1418 return getIndirectReturnResult(RetTy);
1419
1420 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1421 // available register of the sequence %rax, %rdx is used.
1422 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001423 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1424 RetTy, 0);
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 Lattner458b2aa2010-07-29 02:16:43 +00001465 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001466 break;
1467 }
Chris Lattnerc95a3982010-07-29 17:49:08 +00001468 case SSE: {
1469 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001470 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001471 ResType = llvm::StructType::get(getVMContext(), ResType, HiType,NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001472 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001473 }
Chris Lattner31faff52010-07-28 23:06:14 +00001474
1475 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1476 // is passed in the upper half of the last used SSE register.
1477 //
1478 // SSEUP should always be preceeded by SSE, just widen.
1479 case SSEUp:
1480 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001481 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001482 break;
1483
1484 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1485 // returned together with the previous X87 value in %st0.
1486 case X87Up:
1487 // If X87Up is preceeded by X87, we don't need to do
1488 // anything. However, in some cases with unions it may not be
1489 // preceeded by X87. In such situations we follow gcc and pass the
1490 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001491 if (Lo != X87) {
1492 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001493 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001494 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1495 }
Chris Lattner31faff52010-07-28 23:06:14 +00001496 break;
1497 }
1498
1499 return getCoerceResult(RetTy, ResType);
1500}
1501
Chris Lattner458b2aa2010-07-29 02:16:43 +00001502ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001503 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001504 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001505 classify(Ty, 0, Lo, Hi);
Chris Lattner029c0f12010-07-29 04:41:05 +00001506
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001507 // Check some invariants.
1508 // FIXME: Enforce these by construction.
1509 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1510 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1511 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1512
1513 neededInt = 0;
1514 neededSSE = 0;
1515 const llvm::Type *ResType = 0;
1516 switch (Lo) {
1517 case NoClass:
1518 return ABIArgInfo::getIgnore();
1519
1520 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1521 // on the stack.
1522 case Memory:
1523
1524 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1525 // COMPLEX_X87, it is passed in memory.
1526 case X87:
1527 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001528 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001529
1530 case SSEUp:
1531 case X87Up:
1532 assert(0 && "Invalid classification for lo word.");
1533
1534 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1535 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1536 // and %r9 is used.
1537 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001538 ++neededInt;
Chris Lattner029c0f12010-07-29 04:41:05 +00001539
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001540 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001541 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001542 break;
1543
1544 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1545 // available SSE register is used, the registers are taken in the
1546 // order from %xmm0 to %xmm7.
1547 case SSE:
1548 ++neededSSE;
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001549 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001550 break;
1551 }
1552
1553 switch (Hi) {
1554 // Memory was handled previously, ComplexX87 and X87 should
1555 // never occur as hi classes, and X87Up must be preceed by X87,
1556 // which is passed in memory.
1557 case Memory:
1558 case X87:
1559 case ComplexX87:
1560 assert(0 && "Invalid classification for hi word.");
1561 break;
1562
1563 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001564
1565 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001566 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001567 // Pick an 8-byte type based on the preferred type.
Chris Lattnerce1bd752010-07-29 04:51:12 +00001568 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001569 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001570 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001571 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001572 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001573
1574 // X87Up generally doesn't occur here (long double is passed in
1575 // memory), except in situations involving unions.
1576 case X87Up:
Chris Lattnerc95a3982010-07-29 17:49:08 +00001577 case SSE: {
1578 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001579 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001580 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001581 ++neededSSE;
1582 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001583 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001584
1585 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1586 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001587 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001588 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001589 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001590 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001591 break;
1592 }
1593
Chris Lattner22a931e2010-06-29 06:01:59 +00001594 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001595}
1596
Chris Lattner22326a12010-07-29 02:31:05 +00001597void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner029c0f12010-07-29 04:41:05 +00001598
Chris Lattner458b2aa2010-07-29 02:16:43 +00001599 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001600
1601 // Keep track of the number of assigned registers.
1602 unsigned freeIntRegs = 6, freeSSERegs = 8;
1603
1604 // If the return value is indirect, then the hidden argument is consuming one
1605 // integer register.
1606 if (FI.getReturnInfo().isIndirect())
1607 --freeIntRegs;
1608
1609 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1610 // get assigned (in left-to-right order) for passing as follows...
1611 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1612 it != ie; ++it) {
1613 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001614 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001615
1616 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1617 // eightbyte of an argument, the whole argument is passed on the
1618 // stack. If registers have already been assigned for some
1619 // eightbytes of such an argument, the assignments get reverted.
1620 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1621 freeIntRegs -= neededInt;
1622 freeSSERegs -= neededSSE;
1623 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001624 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001625 }
1626 }
1627}
1628
1629static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1630 QualType Ty,
1631 CodeGenFunction &CGF) {
1632 llvm::Value *overflow_arg_area_p =
1633 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1634 llvm::Value *overflow_arg_area =
1635 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1636
1637 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1638 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1639 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1640 if (Align > 8) {
1641 // Note that we follow the ABI & gcc here, even though the type
1642 // could in theory have an alignment greater than 16. This case
1643 // shouldn't ever matter in practice.
1644
1645 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001646 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001647 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001648 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1649 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001650 CGF.Int64Ty);
1651 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001652 overflow_arg_area =
1653 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1654 overflow_arg_area->getType(),
1655 "overflow_arg_area.align");
1656 }
1657
1658 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1659 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1660 llvm::Value *Res =
1661 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001662 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001663
1664 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1665 // l->overflow_arg_area + sizeof(type).
1666 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1667 // an 8 byte boundary.
1668
1669 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001670 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001671 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001672 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1673 "overflow_arg_area.next");
1674 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1675
1676 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1677 return Res;
1678}
1679
1680llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1681 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001682 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001683
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001684 // Assume that va_list type is correct; should be pointer to LLVM type:
1685 // struct {
1686 // i32 gp_offset;
1687 // i32 fp_offset;
1688 // i8* overflow_arg_area;
1689 // i8* reg_save_area;
1690 // };
1691 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001692
1693 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001694 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001695
1696 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1697 // in the registers. If not go to step 7.
1698 if (!neededInt && !neededSSE)
1699 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1700
1701 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1702 // general purpose registers needed to pass type and num_fp to hold
1703 // the number of floating point registers needed.
1704
1705 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1706 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1707 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1708 //
1709 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1710 // register save space).
1711
1712 llvm::Value *InRegs = 0;
1713 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1714 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1715 if (neededInt) {
1716 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1717 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001718 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1719 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001720 }
1721
1722 if (neededSSE) {
1723 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1724 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1725 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001726 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1727 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001728 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1729 }
1730
1731 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1732 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1733 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1734 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1735
1736 // Emit code to load the value if it was passed in registers.
1737
1738 CGF.EmitBlock(InRegBlock);
1739
1740 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1741 // an offset of l->gp_offset and/or l->fp_offset. This may require
1742 // copying to a temporary location in case the parameter is passed
1743 // in different register classes or requires an alignment greater
1744 // than 8 for general purpose registers and 16 for XMM registers.
1745 //
1746 // FIXME: This really results in shameful code when we end up needing to
1747 // collect arguments from different places; often what should result in a
1748 // simple assembling of a structure from scattered addresses has many more
1749 // loads than necessary. Can we clean this up?
1750 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1751 llvm::Value *RegAddr =
1752 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1753 "reg_save_area");
1754 if (neededInt && neededSSE) {
1755 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001756 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001757 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1758 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1759 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1760 const llvm::Type *TyLo = ST->getElementType(0);
1761 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001762 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001763 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001764 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1765 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001766 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1767 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001768 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1769 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001770 llvm::Value *V =
1771 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1772 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1773 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1774 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1775
Owen Anderson170229f2009-07-14 23:10:40 +00001776 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001777 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001778 } else if (neededInt) {
1779 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1780 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001781 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001782 } else if (neededSSE == 1) {
1783 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1784 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1785 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001786 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001787 assert(neededSSE == 2 && "Invalid number of needed registers!");
1788 // SSE registers are spaced 16 bytes apart in the register save
1789 // area, we need to collect the two eightbytes together.
1790 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001791 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001792 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1793 const llvm::Type *DblPtrTy =
1794 llvm::PointerType::getUnqual(DoubleTy);
1795 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1796 DoubleTy, NULL);
1797 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1798 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1799 DblPtrTy));
1800 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1801 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1802 DblPtrTy));
1803 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1804 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1805 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001806 }
1807
1808 // AMD64-ABI 3.5.7p5: Step 5. Set:
1809 // l->gp_offset = l->gp_offset + num_gp * 8
1810 // l->fp_offset = l->fp_offset + num_fp * 16.
1811 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001812 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001813 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1814 gp_offset_p);
1815 }
1816 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001817 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001818 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1819 fp_offset_p);
1820 }
1821 CGF.EmitBranch(ContBlock);
1822
1823 // Emit code to load the value if it was passed in memory.
1824
1825 CGF.EmitBlock(InMemBlock);
1826 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1827
1828 // Return the appropriate result.
1829
1830 CGF.EmitBlock(ContBlock);
1831 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1832 "vaarg.addr");
1833 ResAddr->reserveOperandSpace(2);
1834 ResAddr->addIncoming(RegAddr, InRegBlock);
1835 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001836 return ResAddr;
1837}
1838
Chris Lattner0cf24192010-06-28 20:05:43 +00001839
1840
1841//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001842// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001843//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001844
1845namespace {
1846
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001847class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001848public:
1849 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1850
Chris Lattner458b2aa2010-07-29 02:16:43 +00001851 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001852
Chris Lattner458b2aa2010-07-29 02:16:43 +00001853 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001854
Chris Lattner22326a12010-07-29 02:31:05 +00001855 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001856 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001857 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1858 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001859 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001860 }
1861
1862 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1863 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001864};
1865
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001866class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1867public:
Chris Lattner2b037972010-07-29 02:01:43 +00001868 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1869 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001870};
1871
Daniel Dunbard59655c2009-09-12 00:59:49 +00001872}
1873
Chris Lattner458b2aa2010-07-29 02:16:43 +00001874ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001875 if (RetTy->isVoidType()) {
1876 return ABIArgInfo::getIgnore();
1877 } else {
1878 return ABIArgInfo::getDirect();
1879 }
1880}
1881
Chris Lattner458b2aa2010-07-29 02:16:43 +00001882ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001883 return ABIArgInfo::getDirect();
1884}
1885
1886llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001887 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001888 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001889 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1890
1891 CGBuilderTy &Builder = CGF.Builder;
1892 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1893 "ap");
1894 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1895 llvm::Type *PTy =
1896 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1897 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1898
1899 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1900
1901 llvm::Value *NextAddr =
1902 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1903 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1904 "ap.next");
1905 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1906
1907 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001908}
1909
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001910
John McCallea8d8bb2010-03-11 00:10:12 +00001911// PowerPC-32
1912
1913namespace {
1914class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1915public:
Chris Lattner2b037972010-07-29 02:01:43 +00001916 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1917
John McCallea8d8bb2010-03-11 00:10:12 +00001918 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1919 // This is recovered from gcc output.
1920 return 1; // r1 is the dedicated stack pointer
1921 }
1922
1923 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1924 llvm::Value *Address) const;
1925};
1926
1927}
1928
1929bool
1930PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1931 llvm::Value *Address) const {
1932 // This is calculated from the LLVM and GCC tables and verified
1933 // against gcc output. AFAIK all ABIs use the same encoding.
1934
1935 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1936 llvm::LLVMContext &Context = CGF.getLLVMContext();
1937
1938 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1939 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1940 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1941 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1942
1943 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001944 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001945
1946 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001947 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001948
1949 // 64-76 are various 4-byte special-purpose registers:
1950 // 64: mq
1951 // 65: lr
1952 // 66: ctr
1953 // 67: ap
1954 // 68-75 cr0-7
1955 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001956 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001957
1958 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001959 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001960
1961 // 109: vrsave
1962 // 110: vscr
1963 // 111: spe_acc
1964 // 112: spefscr
1965 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001966 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001967
1968 return false;
1969}
1970
1971
Chris Lattner0cf24192010-06-28 20:05:43 +00001972//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001973// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001974//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001975
1976namespace {
1977
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001978class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001979public:
1980 enum ABIKind {
1981 APCS = 0,
1982 AAPCS = 1,
1983 AAPCS_VFP
1984 };
1985
1986private:
1987 ABIKind Kind;
1988
1989public:
Chris Lattner2b037972010-07-29 02:01:43 +00001990 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00001991
1992private:
1993 ABIKind getABIKind() const { return Kind; }
1994
Chris Lattner458b2aa2010-07-29 02:16:43 +00001995 ABIArgInfo classifyReturnType(QualType RetTy) const;
1996 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001997
Chris Lattner22326a12010-07-29 02:31:05 +00001998 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001999
2000 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2001 CodeGenFunction &CGF) const;
2002};
2003
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002004class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2005public:
Chris Lattner2b037972010-07-29 02:01:43 +00002006 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2007 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002008
2009 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2010 return 13;
2011 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002012};
2013
Daniel Dunbard59655c2009-09-12 00:59:49 +00002014}
2015
Chris Lattner22326a12010-07-29 02:31:05 +00002016void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002017 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002018 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002019 it != ie; ++it)
2020 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002021
Chris Lattner458b2aa2010-07-29 02:16:43 +00002022 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002023 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002024 if (Triple.getEnvironmentName() == "gnueabi" ||
2025 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002026 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002027 else
2028 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002029
Daniel Dunbar020daa92009-09-12 01:00:39 +00002030 switch (getABIKind()) {
2031 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002032 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2033 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002034 break;
2035
2036 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002037 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2038 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002039 break;
2040
2041 case AAPCS_VFP:
2042 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2043 break;
2044 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002045}
2046
Chris Lattner458b2aa2010-07-29 02:16:43 +00002047ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00002048 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
2049 // Treat an enum type as its underlying type.
2050 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2051 Ty = EnumTy->getDecl()->getIntegerType();
2052
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002053 return (Ty->isPromotableIntegerType() ?
2054 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002055 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002056
Daniel Dunbar09d33622009-09-14 21:54:03 +00002057 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002058 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002059 return ABIArgInfo::getIgnore();
2060
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002061 // Structures with either a non-trivial destructor or a non-trivial
2062 // copy constructor are always indirect.
2063 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2064 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2065
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002066 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2067 // backend doesn't support byval.
2068 // FIXME: This doesn't handle alignment > 64 bits.
2069 const llvm::Type* ElemTy;
2070 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002071 if (getContext().getTypeAlign(Ty) > 32) {
2072 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2073 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002074 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002075 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2076 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002077 }
2078 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002079 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002080 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2081 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002082 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002083}
2084
Chris Lattner458b2aa2010-07-29 02:16:43 +00002085static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002086 llvm::LLVMContext &VMContext) {
2087 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2088 // is called integer-like if its size is less than or equal to one word, and
2089 // the offset of each of its addressable sub-fields is zero.
2090
2091 uint64_t Size = Context.getTypeSize(Ty);
2092
2093 // Check that the type fits in a word.
2094 if (Size > 32)
2095 return false;
2096
2097 // FIXME: Handle vector types!
2098 if (Ty->isVectorType())
2099 return false;
2100
Daniel Dunbard53bac72009-09-14 02:20:34 +00002101 // Float types are never treated as "integer like".
2102 if (Ty->isRealFloatingType())
2103 return false;
2104
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002105 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002106 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002107 return true;
2108
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002109 // Small complex integer types are "integer like".
2110 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2111 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002112
2113 // Single element and zero sized arrays should be allowed, by the definition
2114 // above, but they are not.
2115
2116 // Otherwise, it must be a record type.
2117 const RecordType *RT = Ty->getAs<RecordType>();
2118 if (!RT) return false;
2119
2120 // Ignore records with flexible arrays.
2121 const RecordDecl *RD = RT->getDecl();
2122 if (RD->hasFlexibleArrayMember())
2123 return false;
2124
2125 // Check that all sub-fields are at offset 0, and are themselves "integer
2126 // like".
2127 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2128
2129 bool HadField = false;
2130 unsigned idx = 0;
2131 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2132 i != e; ++i, ++idx) {
2133 const FieldDecl *FD = *i;
2134
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002135 // Bit-fields are not addressable, we only need to verify they are "integer
2136 // like". We still have to disallow a subsequent non-bitfield, for example:
2137 // struct { int : 0; int x }
2138 // is non-integer like according to gcc.
2139 if (FD->isBitField()) {
2140 if (!RD->isUnion())
2141 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002142
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002143 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2144 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002145
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002146 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002147 }
2148
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002149 // Check if this field is at offset 0.
2150 if (Layout.getFieldOffset(idx) != 0)
2151 return false;
2152
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002153 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2154 return false;
2155
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002156 // Only allow at most one field in a structure. This doesn't match the
2157 // wording above, but follows gcc in situations with a field following an
2158 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002159 if (!RD->isUnion()) {
2160 if (HadField)
2161 return false;
2162
2163 HadField = true;
2164 }
2165 }
2166
2167 return true;
2168}
2169
Chris Lattner458b2aa2010-07-29 02:16:43 +00002170ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002171 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002172 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002173
Douglas Gregora71cc152010-02-02 20:10:50 +00002174 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2175 // Treat an enum type as its underlying type.
2176 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2177 RetTy = EnumTy->getDecl()->getIntegerType();
2178
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002179 return (RetTy->isPromotableIntegerType() ?
2180 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002181 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002182
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002183 // Structures with either a non-trivial destructor or a non-trivial
2184 // copy constructor are always indirect.
2185 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2186 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2187
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002188 // Are we following APCS?
2189 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002190 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002191 return ABIArgInfo::getIgnore();
2192
Daniel Dunbareedf1512010-02-01 23:31:19 +00002193 // Complex types are all returned as packed integers.
2194 //
2195 // FIXME: Consider using 2 x vector types if the back end handles them
2196 // correctly.
2197 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002198 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002199 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002200
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002201 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002202 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002203 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002204 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002205 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002206 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002207 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002208 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2209 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002210 }
2211
2212 // Otherwise return in memory.
2213 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002214 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002215
2216 // Otherwise this is an AAPCS variant.
2217
Chris Lattner458b2aa2010-07-29 02:16:43 +00002218 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002219 return ABIArgInfo::getIgnore();
2220
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002221 // Aggregates <= 4 bytes are returned in r0; other aggregates
2222 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002223 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002224 if (Size <= 32) {
2225 // Return in the smallest viable integer type.
2226 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002227 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002228 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002229 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2230 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002231 }
2232
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002233 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002234}
2235
2236llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002237 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002238 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002239 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002240 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002241
2242 CGBuilderTy &Builder = CGF.Builder;
2243 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2244 "ap");
2245 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2246 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002247 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002248 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2249
2250 uint64_t Offset =
2251 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2252 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002253 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002254 "ap.next");
2255 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2256
2257 return AddrTyped;
2258}
2259
Chris Lattner458b2aa2010-07-29 02:16:43 +00002260ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2261 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002262 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002263
Chris Lattner458b2aa2010-07-29 02:16:43 +00002264 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2265 return ABIArgInfo::getIndirect(0);
2266
2267 // Treat an enum type as its underlying type.
2268 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2269 RetTy = EnumTy->getDecl()->getIntegerType();
2270
2271 return (RetTy->isPromotableIntegerType() ?
2272 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002273}
2274
Chris Lattner0cf24192010-06-28 20:05:43 +00002275//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002276// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002277//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002278
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002279namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002280
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002281class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002282public:
2283 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2284
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002285 bool isPromotableIntegerType(QualType Ty) const;
2286
Chris Lattner458b2aa2010-07-29 02:16:43 +00002287 ABIArgInfo classifyReturnType(QualType RetTy) const;
2288 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002289
Chris Lattner22326a12010-07-29 02:31:05 +00002290 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002291 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002292 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2293 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002294 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002295 }
2296
2297 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2298 CodeGenFunction &CGF) const;
2299};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002300
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002301class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2302public:
Chris Lattner2b037972010-07-29 02:01:43 +00002303 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2304 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002305};
2306
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002307}
2308
2309bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2310 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002311 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002312 switch (BT->getKind()) {
2313 case BuiltinType::Bool:
2314 case BuiltinType::Char_S:
2315 case BuiltinType::Char_U:
2316 case BuiltinType::SChar:
2317 case BuiltinType::UChar:
2318 case BuiltinType::Short:
2319 case BuiltinType::UShort:
2320 case BuiltinType::Int:
2321 case BuiltinType::UInt:
2322 return true;
2323 default:
2324 return false;
2325 }
2326 return false;
2327}
2328
2329llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2330 CodeGenFunction &CGF) const {
2331 // FIXME: Implement
2332 return 0;
2333}
2334
2335
Chris Lattner458b2aa2010-07-29 02:16:43 +00002336ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2337 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002338 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002339 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002340 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002341
2342 return (isPromotableIntegerType(RetTy) ?
2343 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002344}
2345
Chris Lattner458b2aa2010-07-29 02:16:43 +00002346ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2347 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002348 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002349
2350 return (isPromotableIntegerType(Ty) ?
2351 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002352}
2353
Chris Lattner0cf24192010-06-28 20:05:43 +00002354//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002355// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002356//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002357
2358namespace {
2359
2360class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2361public:
Chris Lattner2b037972010-07-29 02:01:43 +00002362 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2363 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002364 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2365 CodeGen::CodeGenModule &M) const;
2366};
2367
2368}
2369
2370void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2371 llvm::GlobalValue *GV,
2372 CodeGen::CodeGenModule &M) const {
2373 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2374 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2375 // Handle 'interrupt' attribute:
2376 llvm::Function *F = cast<llvm::Function>(GV);
2377
2378 // Step 1: Set ISR calling convention.
2379 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2380
2381 // Step 2: Add attributes goodness.
2382 F->addFnAttr(llvm::Attribute::NoInline);
2383
2384 // Step 3: Emit ISR vector alias.
2385 unsigned Num = attr->getNumber() + 0xffe0;
2386 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2387 "vector_" +
2388 llvm::LowercaseString(llvm::utohexstr(Num)),
2389 GV, &M.getModule());
2390 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002391 }
2392}
2393
Chris Lattner0cf24192010-06-28 20:05:43 +00002394//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002395// MIPS ABI Implementation. This works for both little-endian and
2396// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002397//===----------------------------------------------------------------------===//
2398
John McCall943fae92010-05-27 06:19:26 +00002399namespace {
2400class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2401public:
Chris Lattner2b037972010-07-29 02:01:43 +00002402 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2403 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002404
2405 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2406 return 29;
2407 }
2408
2409 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2410 llvm::Value *Address) const;
2411};
2412}
2413
2414bool
2415MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2416 llvm::Value *Address) const {
2417 // This information comes from gcc's implementation, which seems to
2418 // as canonical as it gets.
2419
2420 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2421 llvm::LLVMContext &Context = CGF.getLLVMContext();
2422
2423 // Everything on MIPS is 4 bytes. Double-precision FP registers
2424 // are aliased to pairs of single-precision FP registers.
2425 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2426 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2427
2428 // 0-31 are the general purpose registers, $0 - $31.
2429 // 32-63 are the floating-point registers, $f0 - $f31.
2430 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2431 // 66 is the (notional, I think) register for signal-handler return.
2432 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2433
2434 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2435 // They are one bit wide and ignored here.
2436
2437 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2438 // (coprocessor 1 is the FP unit)
2439 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2440 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2441 // 176-181 are the DSP accumulator registers.
2442 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2443
2444 return false;
2445}
2446
2447
Chris Lattner2b037972010-07-29 02:01:43 +00002448const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002449 if (TheTargetCodeGenInfo)
2450 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002451
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002452 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2453 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002454
Chris Lattner22a931e2010-06-29 06:01:59 +00002455 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002456 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002457 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002458 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002459
John McCall943fae92010-05-27 06:19:26 +00002460 case llvm::Triple::mips:
2461 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002462 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002463
Daniel Dunbard59655c2009-09-12 00:59:49 +00002464 case llvm::Triple::arm:
2465 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002466 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002467 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002468 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002469 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002470
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002471 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002472 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002473
2474 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002475 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002476
John McCallea8d8bb2010-03-11 00:10:12 +00002477 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002478 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002479
Daniel Dunbard59655c2009-09-12 00:59:49 +00002480 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002481 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002482
2483 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002484 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002485
Daniel Dunbar40165182009-08-24 09:10:05 +00002486 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002487 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002488 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002489 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002490 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002491 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002492 case llvm::Triple::MinGW32:
2493 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002494 case llvm::Triple::AuroraUX:
2495 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002496 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002497 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002498 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002499 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002500
2501 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002502 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002503 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002504 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002505
Daniel Dunbare3532f82009-08-24 08:52:16 +00002506 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002507 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002508 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002509}