blob: 1e66f046b0cd10f5b517ae0e97824a98ee4353f3 [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,
722 unsigned IROffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000723 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
724 unsigned IROffset, QualType SourceTy,
725 unsigned SourceOffset) const;
Chris Lattnerc11301c2010-07-29 02:20:19 +0000726
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000727 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
728 /// to coerce to, chose the best way to pass Ty in the same place
729 /// that \arg CoerceTo would be passed, but while keeping the
730 /// emitted code as simple as possible.
731 ///
732 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
733 /// the ways we might want to pass things, instead of constructing an LLVM
734 /// type. This makes this code more explicit, and it makes it clearer that we
735 /// are also doing this for correctness in the case of passing scalar types.
736 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000737 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000738
739 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000740 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000741 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000742
743 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000744 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000745 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000746
Chris Lattner458b2aa2010-07-29 02:16:43 +0000747 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000748
Chris Lattner029c0f12010-07-29 04:41:05 +0000749 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
750 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000751
752public:
Chris Lattner2b037972010-07-29 02:01:43 +0000753 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000754
Chris Lattner22326a12010-07-29 02:31:05 +0000755 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000756
757 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
758 CodeGenFunction &CGF) const;
759};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000760
761class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
762public:
Chris Lattner2b037972010-07-29 02:01:43 +0000763 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
764 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000765
766 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
767 return 7;
768 }
769
770 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
771 llvm::Value *Address) const {
772 CodeGen::CGBuilderTy &Builder = CGF.Builder;
773 llvm::LLVMContext &Context = CGF.getLLVMContext();
774
775 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
776 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
777
John McCall943fae92010-05-27 06:19:26 +0000778 // 0-15 are the 16 integer registers.
779 // 16 is %rip.
780 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000781
782 return false;
783 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000784};
785
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000786}
787
Chris Lattnerd776fb12010-06-28 21:43:59 +0000788X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000789 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
790 // classified recursively so that always two fields are
791 // considered. The resulting class is calculated according to
792 // the classes of the fields in the eightbyte:
793 //
794 // (a) If both classes are equal, this is the resulting class.
795 //
796 // (b) If one of the classes is NO_CLASS, the resulting class is
797 // the other class.
798 //
799 // (c) If one of the classes is MEMORY, the result is the MEMORY
800 // class.
801 //
802 // (d) If one of the classes is INTEGER, the result is the
803 // INTEGER.
804 //
805 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
806 // MEMORY is used as class.
807 //
808 // (f) Otherwise class SSE is used.
809
810 // Accum should never be memory (we should have returned) or
811 // ComplexX87 (because this cannot be passed in a structure).
812 assert((Accum != Memory && Accum != ComplexX87) &&
813 "Invalid accumulated classification during merge.");
814 if (Accum == Field || Field == NoClass)
815 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000816 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000817 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000818 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000819 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000820 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000821 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000822 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
823 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000824 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000825 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000826}
827
Chris Lattner5c740f12010-06-30 19:14:05 +0000828void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000829 Class &Lo, Class &Hi) const {
830 // FIXME: This code can be simplified by introducing a simple value class for
831 // Class pairs with appropriate constructor methods for the various
832 // situations.
833
834 // FIXME: Some of the split computations are wrong; unaligned vectors
835 // shouldn't be passed in registers for example, so there is no chance they
836 // can straddle an eightbyte. Verify & simplify.
837
838 Lo = Hi = NoClass;
839
840 Class &Current = OffsetBase < 64 ? Lo : Hi;
841 Current = Memory;
842
John McCall9dd450b2009-09-21 23:43:11 +0000843 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000844 BuiltinType::Kind k = BT->getKind();
845
846 if (k == BuiltinType::Void) {
847 Current = NoClass;
848 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
849 Lo = Integer;
850 Hi = Integer;
851 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
852 Current = Integer;
853 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
854 Current = SSE;
855 } else if (k == BuiltinType::LongDouble) {
856 Lo = X87;
857 Hi = X87Up;
858 }
859 // FIXME: _Decimal32 and _Decimal64 are SSE.
860 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000861 return;
862 }
863
864 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000865 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000866 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000867 return;
868 }
869
870 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000871 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000872 return;
873 }
874
875 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000876 if (Ty->isMemberFunctionPointerType())
877 Lo = Hi = Integer;
878 else
879 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000880 return;
881 }
882
883 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000884 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000885 if (Size == 32) {
886 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
887 // float> as integer.
888 Current = Integer;
889
890 // If this type crosses an eightbyte boundary, it should be
891 // split.
892 uint64_t EB_Real = (OffsetBase) / 64;
893 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
894 if (EB_Real != EB_Imag)
895 Hi = Lo;
896 } else if (Size == 64) {
897 // gcc passes <1 x double> in memory. :(
898 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
899 return;
900
901 // gcc passes <1 x long long> as INTEGER.
902 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
903 Current = Integer;
904 else
905 Current = SSE;
906
907 // If this type crosses an eightbyte boundary, it should be
908 // split.
909 if (OffsetBase && OffsetBase != 64)
910 Hi = Lo;
911 } else if (Size == 128) {
912 Lo = SSE;
913 Hi = SSEUp;
914 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000915 return;
916 }
917
918 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000919 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000920
Chris Lattner2b037972010-07-29 02:01:43 +0000921 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000922 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000923 if (Size <= 64)
924 Current = Integer;
925 else if (Size <= 128)
926 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000927 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000928 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000929 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000930 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000931 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000932 Current = ComplexX87;
933
934 // If this complex type crosses an eightbyte boundary then it
935 // should be split.
936 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000937 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000938 if (Hi == NoClass && EB_Real != EB_Imag)
939 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000940
941 return;
942 }
943
Chris Lattner2b037972010-07-29 02:01:43 +0000944 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000945 // Arrays are treated like structures.
946
Chris Lattner2b037972010-07-29 02:01:43 +0000947 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000948
949 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
950 // than two eightbytes, ..., it has class MEMORY.
951 if (Size > 128)
952 return;
953
954 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
955 // fields, it has class MEMORY.
956 //
957 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000958 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000959 return;
960
961 // Otherwise implement simplified merge. We could be smarter about
962 // this, but it isn't worth it and would be harder to verify.
963 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000964 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965 uint64_t ArraySize = AT->getSize().getZExtValue();
966 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
967 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000968 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000969 Lo = merge(Lo, FieldLo);
970 Hi = merge(Hi, FieldHi);
971 if (Lo == Memory || Hi == Memory)
972 break;
973 }
974
975 // Do post merger cleanup (see below). Only case we worry about is Memory.
976 if (Hi == Memory)
977 Lo = Memory;
978 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000979 return;
980 }
981
982 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000983 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000984
985 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
986 // than two eightbytes, ..., it has class MEMORY.
987 if (Size > 128)
988 return;
989
Anders Carlsson20759ad2009-09-16 15:53:40 +0000990 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
991 // copy constructor or a non-trivial destructor, it is passed by invisible
992 // reference.
993 if (hasNonTrivialDestructorOrCopyConstructor(RT))
994 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000995
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000996 const RecordDecl *RD = RT->getDecl();
997
998 // Assume variable sized types are passed in memory.
999 if (RD->hasFlexibleArrayMember())
1000 return;
1001
Chris Lattner2b037972010-07-29 02:01:43 +00001002 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001003
1004 // Reset Lo class, this will be recomputed.
1005 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001006
1007 // If this is a C++ record, classify the bases first.
1008 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1009 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1010 e = CXXRD->bases_end(); i != e; ++i) {
1011 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1012 "Unexpected base class!");
1013 const CXXRecordDecl *Base =
1014 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1015
1016 // Classify this field.
1017 //
1018 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1019 // single eightbyte, each is classified separately. Each eightbyte gets
1020 // initialized to class NO_CLASS.
1021 Class FieldLo, FieldHi;
1022 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001023 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001024 Lo = merge(Lo, FieldLo);
1025 Hi = merge(Hi, FieldHi);
1026 if (Lo == Memory || Hi == Memory)
1027 break;
1028 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001029
Chris Lattnercd840842010-07-29 17:04:54 +00001030 // If this record has no fields, no bases, no vtable, but isn't empty,
1031 // classify as INTEGER.
1032 if (CXXRD->isEmpty() && Size)
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001033 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001034 }
1035
1036 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001037 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001038 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1039 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001040 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1041 bool BitField = i->isBitField();
1042
1043 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1044 // fields, it has class MEMORY.
1045 //
1046 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001047 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001048 Lo = Memory;
1049 return;
1050 }
1051
1052 // Classify this field.
1053 //
1054 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1055 // exceeds a single eightbyte, each is classified
1056 // separately. Each eightbyte gets initialized to class
1057 // NO_CLASS.
1058 Class FieldLo, FieldHi;
1059
1060 // Bit-fields require special handling, they do not force the
1061 // structure to be passed in memory even if unaligned, and
1062 // therefore they can straddle an eightbyte.
1063 if (BitField) {
1064 // Ignore padding bit-fields.
1065 if (i->isUnnamedBitfield())
1066 continue;
1067
1068 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001069 uint64_t Size =
1070 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001071
1072 uint64_t EB_Lo = Offset / 64;
1073 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1074 FieldLo = FieldHi = NoClass;
1075 if (EB_Lo) {
1076 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1077 FieldLo = NoClass;
1078 FieldHi = Integer;
1079 } else {
1080 FieldLo = Integer;
1081 FieldHi = EB_Hi ? Integer : NoClass;
1082 }
1083 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001084 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001085 Lo = merge(Lo, FieldLo);
1086 Hi = merge(Hi, FieldHi);
1087 if (Lo == Memory || Hi == Memory)
1088 break;
1089 }
1090
1091 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1092 //
1093 // (a) If one of the classes is MEMORY, the whole argument is
1094 // passed in memory.
1095 //
1096 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1097
1098 // The first of these conditions is guaranteed by how we implement
1099 // the merge (just bail).
1100 //
1101 // The second condition occurs in the case of unions; for example
1102 // union { _Complex double; unsigned; }.
1103 if (Hi == Memory)
1104 Lo = Memory;
1105 if (Hi == SSEUp && Lo != SSE)
1106 Hi = SSE;
1107 }
1108}
1109
1110ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001111 const llvm::Type *CoerceTo) const {
Chris Lattner4c1e4842010-07-28 22:15:08 +00001112 // If this is a pointer passed as a pointer, just pass it directly.
1113 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1114 Ty->hasPointerRepresentation())
1115 return ABIArgInfo::getExtend();
1116
1117 if (isa<llvm::IntegerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001118 // Integer and pointer types will end up in a general purpose
1119 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001120
1121 // Treat an enum type as its underlying type.
1122 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1123 Ty = EnumTy->getDecl()->getIntegerType();
1124
Chris Lattner4c1e4842010-07-28 22:15:08 +00001125 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001126 return (Ty->isPromotableIntegerType() ?
1127 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001128
Chris Lattnerfa20e952010-06-26 21:52:32 +00001129 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001130 assert(Ty.isCanonical() && "should always have a canonical type here");
1131 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001132
1133 // Float and double end up in a single SSE reg.
Chris Lattner2b037972010-07-29 02:01:43 +00001134 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001135 return ABIArgInfo::getDirect();
1136
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001137 // If this is a 32-bit structure that is passed as a double, then it will be
1138 // passed in the low 32-bits of the XMM register, which is the same as how a
1139 // float is passed. Coerce to a float instead of a double.
Chris Lattner2b037972010-07-29 02:01:43 +00001140 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001141 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001142 }
1143
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001144 return ABIArgInfo::getDirect(CoerceTo);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001145}
1146
Chris Lattner22a931e2010-06-29 06:01:59 +00001147ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001148 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1149 // place naturally.
1150 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1151 // Treat an enum type as its underlying type.
1152 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1153 Ty = EnumTy->getDecl()->getIntegerType();
1154
1155 return (Ty->isPromotableIntegerType() ?
1156 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1157 }
1158
1159 return ABIArgInfo::getIndirect(0);
1160}
1161
Chris Lattner22a931e2010-06-29 06:01:59 +00001162ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001163 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1164 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001165 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1166 // Treat an enum type as its underlying type.
1167 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1168 Ty = EnumTy->getDecl()->getIntegerType();
1169
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001170 return (Ty->isPromotableIntegerType() ?
1171 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001172 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001173
Daniel Dunbar53fac692010-04-21 19:49:55 +00001174 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1175 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001176
Daniel Dunbar53fac692010-04-21 19:49:55 +00001177 // Compute the byval alignment. We trust the back-end to honor the
1178 // minimum ABI alignment for byval, to make cleaner IR.
1179 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001180 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001181 if (Align > MinABIAlign)
1182 return ABIArgInfo::getIndirect(Align);
1183 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001184}
1185
Chris Lattner4200fe42010-07-29 04:56:46 +00001186/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1187/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1188/// vector register.
1189const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001190 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
1191
1192 // Wrapper structs that just contain vectors are passed just like vectors,
1193 // strip them off if present.
1194 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1195 while (STy && STy->getNumElements() == 1) {
1196 IRType = STy->getElementType(0);
1197 STy = dyn_cast<llvm::StructType>(IRType);
1198 }
1199
Chris Lattner4200fe42010-07-29 04:56:46 +00001200 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001201 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001202 const llvm::Type *EltTy = VT->getElementType();
1203 if (VT->getBitWidth() == 128 &&
1204 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1205 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1206 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1207 EltTy->isIntegerTy(128)))
1208 return VT;
1209 }
1210
1211 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1212}
1213
1214
Chris Lattnerc95a3982010-07-29 17:49:08 +00001215/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1216/// low 8 bytes of an XMM register, corresponding to the SSE class.
1217const llvm::Type *X86_64ABIInfo::GetSSETypeAtOffset(const llvm::Type *IRType,
1218 unsigned IROffset) const {
1219 // The only two choices we have are either double or <2 x float>.
1220
1221 // FIXME: <2 x float> doesn't pass as one XMM register yet. Don't enable this
1222 // code until it does.
1223
1224 return llvm::Type::getDoubleTy(getVMContext());
1225}
1226
1227
1228
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001229/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1230/// is known to either be off the end of the specified type or being in
1231/// alignment padding. The user type specified is known to be at most 128 bits
1232/// in size, and have passed through X86_64ABIInfo::classify with a successful
1233/// classification that put one of the two halves in the INTEGER class.
1234///
1235/// It is conservatively correct to return false.
1236static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1237 unsigned EndBit, ASTContext &Context) {
1238 // If the bytes being queried are off the end of the type, there is no user
1239 // data hiding here. This handles analysis of builtins, vectors and other
1240 // types that don't contain interesting padding.
1241 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1242 if (TySize <= StartBit)
1243 return true;
1244
Chris Lattner98076a22010-07-29 07:43:55 +00001245 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1246 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1247 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1248
1249 // Check each element to see if the element overlaps with the queried range.
1250 for (unsigned i = 0; i != NumElts; ++i) {
1251 // If the element is after the span we care about, then we're done..
1252 unsigned EltOffset = i*EltSize;
1253 if (EltOffset >= EndBit) break;
1254
1255 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1256 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1257 EndBit-EltOffset, Context))
1258 return false;
1259 }
1260 // If it overlaps no elements, then it is safe to process as padding.
1261 return true;
1262 }
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001263
1264 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1265 const RecordDecl *RD = RT->getDecl();
1266 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1267
1268 // If this is a C++ record, check the bases first.
1269 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1270 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1271 e = CXXRD->bases_end(); i != e; ++i) {
1272 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1273 "Unexpected base class!");
1274 const CXXRecordDecl *Base =
1275 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1276
1277 // If the base is after the span we care about, ignore it.
1278 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1279 if (BaseOffset >= EndBit) continue;
1280
1281 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1282 if (!BitsContainNoUserData(i->getType(), BaseStart,
1283 EndBit-BaseOffset, Context))
1284 return false;
1285 }
1286 }
1287
1288 // Verify that no field has data that overlaps the region of interest. Yes
1289 // this could be sped up a lot by being smarter about queried fields,
1290 // however we're only looking at structs up to 16 bytes, so we don't care
1291 // much.
1292 unsigned idx = 0;
1293 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1294 i != e; ++i, ++idx) {
1295 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
1296
1297 // If we found a field after the region we care about, then we're done.
1298 if (FieldOffset >= EndBit) break;
1299
1300 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1301 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1302 Context))
1303 return false;
1304 }
1305
1306 // If nothing in this record overlapped the area of interest, then we're
1307 // clean.
1308 return true;
1309 }
1310
1311 return false;
1312}
1313
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001314/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1315/// an 8-byte GPR. This means that we either have a scalar or we are talking
1316/// about the high or low part of an up-to-16-byte struct. This routine picks
1317/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001318/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1319/// etc).
1320///
1321/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1322/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1323/// the 8-byte value references. PrefType may be null.
1324///
1325/// SourceTy is the source level type for the entire argument. SourceOffset is
1326/// an offset into this that we're processing (which is always either 0 or 8).
1327///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001328const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001329GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1330 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001331 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1332 // returning an 8-byte unit starting with it. See if we can safely use it.
1333 if (IROffset == 0) {
1334 // Pointers and int64's always fill the 8-byte unit.
1335 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1336 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001337
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001338 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1339 // goodness in the source type is just tail padding. This is allowed to
1340 // kick in for struct {double,int} on the int, but not on
1341 // struct{double,int,int} because we wouldn't return the second int. We
1342 // have to do this analysis on the source type because we can't depend on
1343 // unions being lowered a specific way etc.
1344 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1345 IRType->isIntegerTy(32)) {
1346 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
1347
1348 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1349 SourceOffset*8+64, getContext()))
1350 return IRType;
1351 }
1352 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001353
Chris Lattnerce1bd752010-07-29 04:51:12 +00001354 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001355 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001356 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001357 if (IROffset < SL->getSizeInBytes()) {
1358 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1359 IROffset -= SL->getElementOffset(FieldIdx);
1360
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001361 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1362 SourceTy, SourceOffset);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001363 }
1364 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001365
Chris Lattner98076a22010-07-29 07:43:55 +00001366 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1367 const llvm::Type *EltTy = ATy->getElementType();
1368 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1369 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001370 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1371 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001372 }
1373
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001374 // Okay, we don't have any better idea of what to pass, so we pass this in an
1375 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001376 unsigned TySizeInBytes =
1377 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001378
Chris Lattner3f763422010-07-29 17:34:39 +00001379 assert(TySizeInBytes != SourceOffset && "Empty field?");
1380
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001381 // It is always safe to classify this as an integer type up to i64 that
1382 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001383 return llvm::IntegerType::get(getVMContext(),
1384 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001385}
1386
Chris Lattner31faff52010-07-28 23:06:14 +00001387ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001388classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001389 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1390 // classification algorithm.
1391 X86_64ABIInfo::Class Lo, Hi;
1392 classify(RetTy, 0, Lo, Hi);
1393
1394 // Check some invariants.
1395 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1396 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1397 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1398
1399 const llvm::Type *ResType = 0;
1400 switch (Lo) {
1401 case NoClass:
1402 return ABIArgInfo::getIgnore();
1403
1404 case SSEUp:
1405 case X87Up:
1406 assert(0 && "Invalid classification for lo word.");
1407
1408 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1409 // hidden argument.
1410 case Memory:
1411 return getIndirectReturnResult(RetTy);
1412
1413 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1414 // available register of the sequence %rax, %rdx is used.
1415 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001416 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1417 RetTy, 0);
Chris Lattner31faff52010-07-28 23:06:14 +00001418 break;
1419
1420 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1421 // available SSE register of the sequence %xmm0, %xmm1 is used.
1422 case SSE:
Chris Lattnerc95a3982010-07-29 17:49:08 +00001423 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001424 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001425
1426 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1427 // returned on the X87 stack in %st0 as 80-bit x87 number.
1428 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001429 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001430 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001431
1432 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1433 // part of the value is returned in %st0 and the imaginary part in
1434 // %st1.
1435 case ComplexX87:
1436 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001437 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001438 llvm::Type::getX86_FP80Ty(getVMContext()),
1439 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001440 NULL);
1441 break;
1442 }
1443
1444 switch (Hi) {
1445 // Memory was handled previously and X87 should
1446 // never occur as a hi class.
1447 case Memory:
1448 case X87:
1449 assert(0 && "Invalid classification for hi word.");
1450
1451 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001452 case NoClass:
1453 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001454
1455 case Integer: {
Chris Lattnerce1bd752010-07-29 04:51:12 +00001456 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001457 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001458 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001459 break;
1460 }
Chris Lattnerc95a3982010-07-29 17:49:08 +00001461 case SSE: {
1462 const llvm::Type *HiType =
1463 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8);
1464 ResType = llvm::StructType::get(getVMContext(), ResType, HiType,NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001465 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001466 }
Chris Lattner31faff52010-07-28 23:06:14 +00001467
1468 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1469 // is passed in the upper half of the last used SSE register.
1470 //
1471 // SSEUP should always be preceeded by SSE, just widen.
1472 case SSEUp:
1473 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001474 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001475 break;
1476
1477 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1478 // returned together with the previous X87 value in %st0.
1479 case X87Up:
1480 // If X87Up is preceeded by X87, we don't need to do
1481 // anything. However, in some cases with unions it may not be
1482 // preceeded by X87. In such situations we follow gcc and pass the
1483 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001484 if (Lo != X87) {
1485 const llvm::Type *HiType =
1486 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8);
1487 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1488 }
Chris Lattner31faff52010-07-28 23:06:14 +00001489 break;
1490 }
1491
1492 return getCoerceResult(RetTy, ResType);
1493}
1494
Chris Lattner458b2aa2010-07-29 02:16:43 +00001495ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001496 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001497 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001498 classify(Ty, 0, Lo, Hi);
Chris Lattner029c0f12010-07-29 04:41:05 +00001499
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001500 // Check some invariants.
1501 // FIXME: Enforce these by construction.
1502 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1503 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1504 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1505
1506 neededInt = 0;
1507 neededSSE = 0;
1508 const llvm::Type *ResType = 0;
1509 switch (Lo) {
1510 case NoClass:
1511 return ABIArgInfo::getIgnore();
1512
1513 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1514 // on the stack.
1515 case Memory:
1516
1517 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1518 // COMPLEX_X87, it is passed in memory.
1519 case X87:
1520 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001521 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001522
1523 case SSEUp:
1524 case X87Up:
1525 assert(0 && "Invalid classification for lo word.");
1526
1527 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1528 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1529 // and %r9 is used.
1530 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001531 ++neededInt;
Chris Lattner029c0f12010-07-29 04:41:05 +00001532
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001533 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001534 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001535 break;
1536
1537 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1538 // available SSE register is used, the registers are taken in the
1539 // order from %xmm0 to %xmm7.
1540 case SSE:
1541 ++neededSSE;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001542 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001543 break;
1544 }
1545
1546 switch (Hi) {
1547 // Memory was handled previously, ComplexX87 and X87 should
1548 // never occur as hi classes, and X87Up must be preceed by X87,
1549 // which is passed in memory.
1550 case Memory:
1551 case X87:
1552 case ComplexX87:
1553 assert(0 && "Invalid classification for hi word.");
1554 break;
1555
1556 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001557
1558 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001559 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001560 // Pick an 8-byte type based on the preferred type.
Chris Lattnerce1bd752010-07-29 04:51:12 +00001561 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001562 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001563 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001564 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001565 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001566
1567 // X87Up generally doesn't occur here (long double is passed in
1568 // memory), except in situations involving unions.
1569 case X87Up:
Chris Lattnerc95a3982010-07-29 17:49:08 +00001570 case SSE: {
1571 const llvm::Type *HiType =
1572 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8);
1573 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001574 ++neededSSE;
1575 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001576 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001577
1578 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1579 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001580 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001581 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001582 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001583 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001584 break;
1585 }
1586
Chris Lattner22a931e2010-06-29 06:01:59 +00001587 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001588}
1589
Chris Lattner22326a12010-07-29 02:31:05 +00001590void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner029c0f12010-07-29 04:41:05 +00001591
Chris Lattner458b2aa2010-07-29 02:16:43 +00001592 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001593
1594 // Keep track of the number of assigned registers.
1595 unsigned freeIntRegs = 6, freeSSERegs = 8;
1596
1597 // If the return value is indirect, then the hidden argument is consuming one
1598 // integer register.
1599 if (FI.getReturnInfo().isIndirect())
1600 --freeIntRegs;
1601
1602 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1603 // get assigned (in left-to-right order) for passing as follows...
1604 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1605 it != ie; ++it) {
1606 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001607 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001608
1609 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1610 // eightbyte of an argument, the whole argument is passed on the
1611 // stack. If registers have already been assigned for some
1612 // eightbytes of such an argument, the assignments get reverted.
1613 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1614 freeIntRegs -= neededInt;
1615 freeSSERegs -= neededSSE;
1616 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001617 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001618 }
1619 }
1620}
1621
1622static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1623 QualType Ty,
1624 CodeGenFunction &CGF) {
1625 llvm::Value *overflow_arg_area_p =
1626 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1627 llvm::Value *overflow_arg_area =
1628 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1629
1630 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1631 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1632 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1633 if (Align > 8) {
1634 // Note that we follow the ABI & gcc here, even though the type
1635 // could in theory have an alignment greater than 16. This case
1636 // shouldn't ever matter in practice.
1637
1638 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001639 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001640 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001641 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1642 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001643 CGF.Int64Ty);
1644 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001645 overflow_arg_area =
1646 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1647 overflow_arg_area->getType(),
1648 "overflow_arg_area.align");
1649 }
1650
1651 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1652 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1653 llvm::Value *Res =
1654 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001655 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001656
1657 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1658 // l->overflow_arg_area + sizeof(type).
1659 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1660 // an 8 byte boundary.
1661
1662 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001663 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001664 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001665 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1666 "overflow_arg_area.next");
1667 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1668
1669 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1670 return Res;
1671}
1672
1673llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1674 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001675 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001676
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001677 // Assume that va_list type is correct; should be pointer to LLVM type:
1678 // struct {
1679 // i32 gp_offset;
1680 // i32 fp_offset;
1681 // i8* overflow_arg_area;
1682 // i8* reg_save_area;
1683 // };
1684 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001685
1686 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001687 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001688
1689 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1690 // in the registers. If not go to step 7.
1691 if (!neededInt && !neededSSE)
1692 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1693
1694 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1695 // general purpose registers needed to pass type and num_fp to hold
1696 // the number of floating point registers needed.
1697
1698 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1699 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1700 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1701 //
1702 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1703 // register save space).
1704
1705 llvm::Value *InRegs = 0;
1706 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1707 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1708 if (neededInt) {
1709 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1710 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001711 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1712 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001713 }
1714
1715 if (neededSSE) {
1716 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1717 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1718 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001719 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1720 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001721 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1722 }
1723
1724 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1725 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1726 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1727 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1728
1729 // Emit code to load the value if it was passed in registers.
1730
1731 CGF.EmitBlock(InRegBlock);
1732
1733 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1734 // an offset of l->gp_offset and/or l->fp_offset. This may require
1735 // copying to a temporary location in case the parameter is passed
1736 // in different register classes or requires an alignment greater
1737 // than 8 for general purpose registers and 16 for XMM registers.
1738 //
1739 // FIXME: This really results in shameful code when we end up needing to
1740 // collect arguments from different places; often what should result in a
1741 // simple assembling of a structure from scattered addresses has many more
1742 // loads than necessary. Can we clean this up?
1743 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1744 llvm::Value *RegAddr =
1745 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1746 "reg_save_area");
1747 if (neededInt && neededSSE) {
1748 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001749 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001750 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1751 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1752 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1753 const llvm::Type *TyLo = ST->getElementType(0);
1754 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001755 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001756 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001757 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1758 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001759 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1760 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001761 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1762 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001763 llvm::Value *V =
1764 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1765 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1766 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1767 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1768
Owen Anderson170229f2009-07-14 23:10:40 +00001769 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001770 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001771 } else if (neededInt) {
1772 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1773 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001774 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001775 } else if (neededSSE == 1) {
1776 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1777 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1778 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001779 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001780 assert(neededSSE == 2 && "Invalid number of needed registers!");
1781 // SSE registers are spaced 16 bytes apart in the register save
1782 // area, we need to collect the two eightbytes together.
1783 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001784 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001785 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1786 const llvm::Type *DblPtrTy =
1787 llvm::PointerType::getUnqual(DoubleTy);
1788 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1789 DoubleTy, NULL);
1790 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1791 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1792 DblPtrTy));
1793 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1794 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1795 DblPtrTy));
1796 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1797 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1798 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001799 }
1800
1801 // AMD64-ABI 3.5.7p5: Step 5. Set:
1802 // l->gp_offset = l->gp_offset + num_gp * 8
1803 // l->fp_offset = l->fp_offset + num_fp * 16.
1804 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001805 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001806 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1807 gp_offset_p);
1808 }
1809 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001810 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001811 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1812 fp_offset_p);
1813 }
1814 CGF.EmitBranch(ContBlock);
1815
1816 // Emit code to load the value if it was passed in memory.
1817
1818 CGF.EmitBlock(InMemBlock);
1819 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1820
1821 // Return the appropriate result.
1822
1823 CGF.EmitBlock(ContBlock);
1824 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1825 "vaarg.addr");
1826 ResAddr->reserveOperandSpace(2);
1827 ResAddr->addIncoming(RegAddr, InRegBlock);
1828 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001829 return ResAddr;
1830}
1831
Chris Lattner0cf24192010-06-28 20:05:43 +00001832
1833
1834//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001835// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001836//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001837
1838namespace {
1839
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001840class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001841public:
1842 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1843
Chris Lattner458b2aa2010-07-29 02:16:43 +00001844 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001845
Chris Lattner458b2aa2010-07-29 02:16:43 +00001846 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001847
Chris Lattner22326a12010-07-29 02:31:05 +00001848 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001849 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001850 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1851 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001852 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001853 }
1854
1855 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1856 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001857};
1858
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001859class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1860public:
Chris Lattner2b037972010-07-29 02:01:43 +00001861 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1862 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001863};
1864
Daniel Dunbard59655c2009-09-12 00:59:49 +00001865}
1866
Chris Lattner458b2aa2010-07-29 02:16:43 +00001867ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001868 if (RetTy->isVoidType()) {
1869 return ABIArgInfo::getIgnore();
1870 } else {
1871 return ABIArgInfo::getDirect();
1872 }
1873}
1874
Chris Lattner458b2aa2010-07-29 02:16:43 +00001875ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001876 return ABIArgInfo::getDirect();
1877}
1878
1879llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001880 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001881 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001882 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1883
1884 CGBuilderTy &Builder = CGF.Builder;
1885 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1886 "ap");
1887 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1888 llvm::Type *PTy =
1889 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1890 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1891
1892 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1893
1894 llvm::Value *NextAddr =
1895 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1896 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1897 "ap.next");
1898 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1899
1900 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001901}
1902
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001903
John McCallea8d8bb2010-03-11 00:10:12 +00001904// PowerPC-32
1905
1906namespace {
1907class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1908public:
Chris Lattner2b037972010-07-29 02:01:43 +00001909 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1910
John McCallea8d8bb2010-03-11 00:10:12 +00001911 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1912 // This is recovered from gcc output.
1913 return 1; // r1 is the dedicated stack pointer
1914 }
1915
1916 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1917 llvm::Value *Address) const;
1918};
1919
1920}
1921
1922bool
1923PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1924 llvm::Value *Address) const {
1925 // This is calculated from the LLVM and GCC tables and verified
1926 // against gcc output. AFAIK all ABIs use the same encoding.
1927
1928 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1929 llvm::LLVMContext &Context = CGF.getLLVMContext();
1930
1931 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1932 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1933 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1934 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1935
1936 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001937 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001938
1939 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001940 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001941
1942 // 64-76 are various 4-byte special-purpose registers:
1943 // 64: mq
1944 // 65: lr
1945 // 66: ctr
1946 // 67: ap
1947 // 68-75 cr0-7
1948 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001949 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001950
1951 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001952 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001953
1954 // 109: vrsave
1955 // 110: vscr
1956 // 111: spe_acc
1957 // 112: spefscr
1958 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001959 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001960
1961 return false;
1962}
1963
1964
Chris Lattner0cf24192010-06-28 20:05:43 +00001965//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001966// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001967//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001968
1969namespace {
1970
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001971class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001972public:
1973 enum ABIKind {
1974 APCS = 0,
1975 AAPCS = 1,
1976 AAPCS_VFP
1977 };
1978
1979private:
1980 ABIKind Kind;
1981
1982public:
Chris Lattner2b037972010-07-29 02:01:43 +00001983 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00001984
1985private:
1986 ABIKind getABIKind() const { return Kind; }
1987
Chris Lattner458b2aa2010-07-29 02:16:43 +00001988 ABIArgInfo classifyReturnType(QualType RetTy) const;
1989 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001990
Chris Lattner22326a12010-07-29 02:31:05 +00001991 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001992
1993 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1994 CodeGenFunction &CGF) const;
1995};
1996
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001997class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1998public:
Chris Lattner2b037972010-07-29 02:01:43 +00001999 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2000 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002001
2002 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2003 return 13;
2004 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002005};
2006
Daniel Dunbard59655c2009-09-12 00:59:49 +00002007}
2008
Chris Lattner22326a12010-07-29 02:31:05 +00002009void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002010 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002011 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002012 it != ie; ++it)
2013 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002014
Chris Lattner458b2aa2010-07-29 02:16:43 +00002015 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002016 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002017 if (Triple.getEnvironmentName() == "gnueabi" ||
2018 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002019 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002020 else
2021 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002022
Daniel Dunbar020daa92009-09-12 01:00:39 +00002023 switch (getABIKind()) {
2024 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002025 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2026 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002027 break;
2028
2029 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002030 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2031 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002032 break;
2033
2034 case AAPCS_VFP:
2035 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2036 break;
2037 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002038}
2039
Chris Lattner458b2aa2010-07-29 02:16:43 +00002040ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00002041 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
2042 // Treat an enum type as its underlying type.
2043 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2044 Ty = EnumTy->getDecl()->getIntegerType();
2045
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002046 return (Ty->isPromotableIntegerType() ?
2047 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002048 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002049
Daniel Dunbar09d33622009-09-14 21:54:03 +00002050 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002051 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002052 return ABIArgInfo::getIgnore();
2053
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002054 // Structures with either a non-trivial destructor or a non-trivial
2055 // copy constructor are always indirect.
2056 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2057 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2058
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002059 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2060 // backend doesn't support byval.
2061 // FIXME: This doesn't handle alignment > 64 bits.
2062 const llvm::Type* ElemTy;
2063 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002064 if (getContext().getTypeAlign(Ty) > 32) {
2065 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2066 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002067 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002068 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2069 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002070 }
2071 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002072 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002073 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2074 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002075 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002076}
2077
Chris Lattner458b2aa2010-07-29 02:16:43 +00002078static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002079 llvm::LLVMContext &VMContext) {
2080 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2081 // is called integer-like if its size is less than or equal to one word, and
2082 // the offset of each of its addressable sub-fields is zero.
2083
2084 uint64_t Size = Context.getTypeSize(Ty);
2085
2086 // Check that the type fits in a word.
2087 if (Size > 32)
2088 return false;
2089
2090 // FIXME: Handle vector types!
2091 if (Ty->isVectorType())
2092 return false;
2093
Daniel Dunbard53bac72009-09-14 02:20:34 +00002094 // Float types are never treated as "integer like".
2095 if (Ty->isRealFloatingType())
2096 return false;
2097
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002098 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002099 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002100 return true;
2101
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002102 // Small complex integer types are "integer like".
2103 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2104 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002105
2106 // Single element and zero sized arrays should be allowed, by the definition
2107 // above, but they are not.
2108
2109 // Otherwise, it must be a record type.
2110 const RecordType *RT = Ty->getAs<RecordType>();
2111 if (!RT) return false;
2112
2113 // Ignore records with flexible arrays.
2114 const RecordDecl *RD = RT->getDecl();
2115 if (RD->hasFlexibleArrayMember())
2116 return false;
2117
2118 // Check that all sub-fields are at offset 0, and are themselves "integer
2119 // like".
2120 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2121
2122 bool HadField = false;
2123 unsigned idx = 0;
2124 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2125 i != e; ++i, ++idx) {
2126 const FieldDecl *FD = *i;
2127
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002128 // Bit-fields are not addressable, we only need to verify they are "integer
2129 // like". We still have to disallow a subsequent non-bitfield, for example:
2130 // struct { int : 0; int x }
2131 // is non-integer like according to gcc.
2132 if (FD->isBitField()) {
2133 if (!RD->isUnion())
2134 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002135
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002136 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2137 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002138
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002139 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002140 }
2141
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002142 // Check if this field is at offset 0.
2143 if (Layout.getFieldOffset(idx) != 0)
2144 return false;
2145
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002146 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2147 return false;
2148
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002149 // Only allow at most one field in a structure. This doesn't match the
2150 // wording above, but follows gcc in situations with a field following an
2151 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002152 if (!RD->isUnion()) {
2153 if (HadField)
2154 return false;
2155
2156 HadField = true;
2157 }
2158 }
2159
2160 return true;
2161}
2162
Chris Lattner458b2aa2010-07-29 02:16:43 +00002163ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002164 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002165 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002166
Douglas Gregora71cc152010-02-02 20:10:50 +00002167 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2168 // Treat an enum type as its underlying type.
2169 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2170 RetTy = EnumTy->getDecl()->getIntegerType();
2171
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002172 return (RetTy->isPromotableIntegerType() ?
2173 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002174 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002175
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002176 // Structures with either a non-trivial destructor or a non-trivial
2177 // copy constructor are always indirect.
2178 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2179 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2180
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002181 // Are we following APCS?
2182 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002183 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002184 return ABIArgInfo::getIgnore();
2185
Daniel Dunbareedf1512010-02-01 23:31:19 +00002186 // Complex types are all returned as packed integers.
2187 //
2188 // FIXME: Consider using 2 x vector types if the back end handles them
2189 // correctly.
2190 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002191 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002192 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002193
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002194 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002195 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002196 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002197 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002198 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002199 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002200 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002201 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2202 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002203 }
2204
2205 // Otherwise return in memory.
2206 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002207 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002208
2209 // Otherwise this is an AAPCS variant.
2210
Chris Lattner458b2aa2010-07-29 02:16:43 +00002211 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002212 return ABIArgInfo::getIgnore();
2213
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002214 // Aggregates <= 4 bytes are returned in r0; other aggregates
2215 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002216 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002217 if (Size <= 32) {
2218 // Return in the smallest viable integer type.
2219 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002220 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002221 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002222 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2223 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002224 }
2225
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002226 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002227}
2228
2229llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002230 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002231 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002232 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002233 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002234
2235 CGBuilderTy &Builder = CGF.Builder;
2236 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2237 "ap");
2238 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2239 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002240 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002241 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2242
2243 uint64_t Offset =
2244 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2245 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002246 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002247 "ap.next");
2248 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2249
2250 return AddrTyped;
2251}
2252
Chris Lattner458b2aa2010-07-29 02:16:43 +00002253ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2254 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002255 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002256
Chris Lattner458b2aa2010-07-29 02:16:43 +00002257 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2258 return ABIArgInfo::getIndirect(0);
2259
2260 // Treat an enum type as its underlying type.
2261 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2262 RetTy = EnumTy->getDecl()->getIntegerType();
2263
2264 return (RetTy->isPromotableIntegerType() ?
2265 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002266}
2267
Chris Lattner0cf24192010-06-28 20:05:43 +00002268//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002269// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002270//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002271
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002272namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002273
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002274class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002275public:
2276 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2277
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002278 bool isPromotableIntegerType(QualType Ty) const;
2279
Chris Lattner458b2aa2010-07-29 02:16:43 +00002280 ABIArgInfo classifyReturnType(QualType RetTy) const;
2281 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002282
Chris Lattner22326a12010-07-29 02:31:05 +00002283 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002285 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2286 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002287 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002288 }
2289
2290 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2291 CodeGenFunction &CGF) const;
2292};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002293
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002294class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2295public:
Chris Lattner2b037972010-07-29 02:01:43 +00002296 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2297 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002298};
2299
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002300}
2301
2302bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2303 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002304 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002305 switch (BT->getKind()) {
2306 case BuiltinType::Bool:
2307 case BuiltinType::Char_S:
2308 case BuiltinType::Char_U:
2309 case BuiltinType::SChar:
2310 case BuiltinType::UChar:
2311 case BuiltinType::Short:
2312 case BuiltinType::UShort:
2313 case BuiltinType::Int:
2314 case BuiltinType::UInt:
2315 return true;
2316 default:
2317 return false;
2318 }
2319 return false;
2320}
2321
2322llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2323 CodeGenFunction &CGF) const {
2324 // FIXME: Implement
2325 return 0;
2326}
2327
2328
Chris Lattner458b2aa2010-07-29 02:16:43 +00002329ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2330 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002331 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002332 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002333 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002334
2335 return (isPromotableIntegerType(RetTy) ?
2336 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002337}
2338
Chris Lattner458b2aa2010-07-29 02:16:43 +00002339ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2340 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002341 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002342
2343 return (isPromotableIntegerType(Ty) ?
2344 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002345}
2346
Chris Lattner0cf24192010-06-28 20:05:43 +00002347//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002348// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002349//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002350
2351namespace {
2352
2353class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2354public:
Chris Lattner2b037972010-07-29 02:01:43 +00002355 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2356 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002357 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2358 CodeGen::CodeGenModule &M) const;
2359};
2360
2361}
2362
2363void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2364 llvm::GlobalValue *GV,
2365 CodeGen::CodeGenModule &M) const {
2366 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2367 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2368 // Handle 'interrupt' attribute:
2369 llvm::Function *F = cast<llvm::Function>(GV);
2370
2371 // Step 1: Set ISR calling convention.
2372 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2373
2374 // Step 2: Add attributes goodness.
2375 F->addFnAttr(llvm::Attribute::NoInline);
2376
2377 // Step 3: Emit ISR vector alias.
2378 unsigned Num = attr->getNumber() + 0xffe0;
2379 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2380 "vector_" +
2381 llvm::LowercaseString(llvm::utohexstr(Num)),
2382 GV, &M.getModule());
2383 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002384 }
2385}
2386
Chris Lattner0cf24192010-06-28 20:05:43 +00002387//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002388// MIPS ABI Implementation. This works for both little-endian and
2389// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002390//===----------------------------------------------------------------------===//
2391
John McCall943fae92010-05-27 06:19:26 +00002392namespace {
2393class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2394public:
Chris Lattner2b037972010-07-29 02:01:43 +00002395 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2396 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002397
2398 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2399 return 29;
2400 }
2401
2402 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2403 llvm::Value *Address) const;
2404};
2405}
2406
2407bool
2408MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2409 llvm::Value *Address) const {
2410 // This information comes from gcc's implementation, which seems to
2411 // as canonical as it gets.
2412
2413 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2414 llvm::LLVMContext &Context = CGF.getLLVMContext();
2415
2416 // Everything on MIPS is 4 bytes. Double-precision FP registers
2417 // are aliased to pairs of single-precision FP registers.
2418 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2419 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2420
2421 // 0-31 are the general purpose registers, $0 - $31.
2422 // 32-63 are the floating-point registers, $f0 - $f31.
2423 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2424 // 66 is the (notional, I think) register for signal-handler return.
2425 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2426
2427 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2428 // They are one bit wide and ignored here.
2429
2430 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2431 // (coprocessor 1 is the FP unit)
2432 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2433 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2434 // 176-181 are the DSP accumulator registers.
2435 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2436
2437 return false;
2438}
2439
2440
Chris Lattner2b037972010-07-29 02:01:43 +00002441const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002442 if (TheTargetCodeGenInfo)
2443 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002444
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002445 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2446 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002447
Chris Lattner22a931e2010-06-29 06:01:59 +00002448 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002449 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002450 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002451 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002452
John McCall943fae92010-05-27 06:19:26 +00002453 case llvm::Triple::mips:
2454 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002455 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002456
Daniel Dunbard59655c2009-09-12 00:59:49 +00002457 case llvm::Triple::arm:
2458 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002459 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002460 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002461 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002462 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002463
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002464 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002465 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002466
2467 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002468 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002469
John McCallea8d8bb2010-03-11 00:10:12 +00002470 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002471 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002472
Daniel Dunbard59655c2009-09-12 00:59:49 +00002473 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002474 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002475
2476 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002477 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002478
Daniel Dunbar40165182009-08-24 09:10:05 +00002479 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002480 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002481 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002482 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002483 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002484 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002485 case llvm::Triple::MinGW32:
2486 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002487 case llvm::Triple::AuroraUX:
2488 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002489 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002490 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002491 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002492 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002493
2494 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002495 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002496 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002497 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002498
Daniel Dunbare3532f82009-08-24 08:52:16 +00002499 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002500 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002501 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002502}