blob: 67f8a2014406f91d25145110ba5b00729fc4ec85 [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;
721
Chris Lattner029c0f12010-07-29 04:41:05 +0000722 const llvm::Type *Get8ByteTypeAtOffset(const llvm::Type *IRType,
723 unsigned IROffset, QualType SourceTy,
Chris Lattnerc11301c2010-07-29 02:20:19 +0000724 unsigned SourceOffset) const;
725
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000726 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
727 /// to coerce to, chose the best way to pass Ty in the same place
728 /// that \arg CoerceTo would be passed, but while keeping the
729 /// emitted code as simple as possible.
730 ///
731 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
732 /// the ways we might want to pass things, instead of constructing an LLVM
733 /// type. This makes this code more explicit, and it makes it clearer that we
734 /// are also doing this for correctness in the case of passing scalar types.
735 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000736 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000737
738 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000739 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000740 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000741
742 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000743 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000744 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000745
Chris Lattner458b2aa2010-07-29 02:16:43 +0000746 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000747
Chris Lattner029c0f12010-07-29 04:41:05 +0000748 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
749 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750
751public:
Chris Lattner2b037972010-07-29 02:01:43 +0000752 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000753
Chris Lattner22326a12010-07-29 02:31:05 +0000754 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000755
756 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
757 CodeGenFunction &CGF) const;
758};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000759
760class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
761public:
Chris Lattner2b037972010-07-29 02:01:43 +0000762 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
763 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000764
765 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
766 return 7;
767 }
768
769 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
770 llvm::Value *Address) const {
771 CodeGen::CGBuilderTy &Builder = CGF.Builder;
772 llvm::LLVMContext &Context = CGF.getLLVMContext();
773
774 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
775 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
776
John McCall943fae92010-05-27 06:19:26 +0000777 // 0-15 are the 16 integer registers.
778 // 16 is %rip.
779 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000780
781 return false;
782 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000783};
784
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000785}
786
Chris Lattnerd776fb12010-06-28 21:43:59 +0000787X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000788 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
789 // classified recursively so that always two fields are
790 // considered. The resulting class is calculated according to
791 // the classes of the fields in the eightbyte:
792 //
793 // (a) If both classes are equal, this is the resulting class.
794 //
795 // (b) If one of the classes is NO_CLASS, the resulting class is
796 // the other class.
797 //
798 // (c) If one of the classes is MEMORY, the result is the MEMORY
799 // class.
800 //
801 // (d) If one of the classes is INTEGER, the result is the
802 // INTEGER.
803 //
804 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
805 // MEMORY is used as class.
806 //
807 // (f) Otherwise class SSE is used.
808
809 // Accum should never be memory (we should have returned) or
810 // ComplexX87 (because this cannot be passed in a structure).
811 assert((Accum != Memory && Accum != ComplexX87) &&
812 "Invalid accumulated classification during merge.");
813 if (Accum == Field || Field == NoClass)
814 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000815 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000816 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000817 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000818 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000819 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000820 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000821 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
822 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000823 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000824 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000825}
826
Chris Lattner5c740f12010-06-30 19:14:05 +0000827void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000828 Class &Lo, Class &Hi) const {
829 // FIXME: This code can be simplified by introducing a simple value class for
830 // Class pairs with appropriate constructor methods for the various
831 // situations.
832
833 // FIXME: Some of the split computations are wrong; unaligned vectors
834 // shouldn't be passed in registers for example, so there is no chance they
835 // can straddle an eightbyte. Verify & simplify.
836
837 Lo = Hi = NoClass;
838
839 Class &Current = OffsetBase < 64 ? Lo : Hi;
840 Current = Memory;
841
John McCall9dd450b2009-09-21 23:43:11 +0000842 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000843 BuiltinType::Kind k = BT->getKind();
844
845 if (k == BuiltinType::Void) {
846 Current = NoClass;
847 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
848 Lo = Integer;
849 Hi = Integer;
850 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
851 Current = Integer;
852 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
853 Current = SSE;
854 } else if (k == BuiltinType::LongDouble) {
855 Lo = X87;
856 Hi = X87Up;
857 }
858 // FIXME: _Decimal32 and _Decimal64 are SSE.
859 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000860 return;
861 }
862
863 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000864 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000865 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000866 return;
867 }
868
869 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000870 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000871 return;
872 }
873
874 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000875 if (Ty->isMemberFunctionPointerType())
876 Lo = Hi = Integer;
877 else
878 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000879 return;
880 }
881
882 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000883 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000884 if (Size == 32) {
885 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
886 // float> as integer.
887 Current = Integer;
888
889 // If this type crosses an eightbyte boundary, it should be
890 // split.
891 uint64_t EB_Real = (OffsetBase) / 64;
892 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
893 if (EB_Real != EB_Imag)
894 Hi = Lo;
895 } else if (Size == 64) {
896 // gcc passes <1 x double> in memory. :(
897 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
898 return;
899
900 // gcc passes <1 x long long> as INTEGER.
901 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
902 Current = Integer;
903 else
904 Current = SSE;
905
906 // If this type crosses an eightbyte boundary, it should be
907 // split.
908 if (OffsetBase && OffsetBase != 64)
909 Hi = Lo;
910 } else if (Size == 128) {
911 Lo = SSE;
912 Hi = SSEUp;
913 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000914 return;
915 }
916
917 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000918 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000919
Chris Lattner2b037972010-07-29 02:01:43 +0000920 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000921 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000922 if (Size <= 64)
923 Current = Integer;
924 else if (Size <= 128)
925 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000926 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000927 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000928 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000930 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000931 Current = ComplexX87;
932
933 // If this complex type crosses an eightbyte boundary then it
934 // should be split.
935 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000936 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000937 if (Hi == NoClass && EB_Real != EB_Imag)
938 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000939
940 return;
941 }
942
Chris Lattner2b037972010-07-29 02:01:43 +0000943 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000944 // Arrays are treated like structures.
945
Chris Lattner2b037972010-07-29 02:01:43 +0000946 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000947
948 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
949 // than two eightbytes, ..., it has class MEMORY.
950 if (Size > 128)
951 return;
952
953 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
954 // fields, it has class MEMORY.
955 //
956 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000957 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000958 return;
959
960 // Otherwise implement simplified merge. We could be smarter about
961 // this, but it isn't worth it and would be harder to verify.
962 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000963 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000964 uint64_t ArraySize = AT->getSize().getZExtValue();
965 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
966 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000967 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000968 Lo = merge(Lo, FieldLo);
969 Hi = merge(Hi, FieldHi);
970 if (Lo == Memory || Hi == Memory)
971 break;
972 }
973
974 // Do post merger cleanup (see below). Only case we worry about is Memory.
975 if (Hi == Memory)
976 Lo = Memory;
977 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000978 return;
979 }
980
981 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000982 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000983
984 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
985 // than two eightbytes, ..., it has class MEMORY.
986 if (Size > 128)
987 return;
988
Anders Carlsson20759ad2009-09-16 15:53:40 +0000989 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
990 // copy constructor or a non-trivial destructor, it is passed by invisible
991 // reference.
992 if (hasNonTrivialDestructorOrCopyConstructor(RT))
993 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000994
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000995 const RecordDecl *RD = RT->getDecl();
996
997 // Assume variable sized types are passed in memory.
998 if (RD->hasFlexibleArrayMember())
999 return;
1000
Chris Lattner2b037972010-07-29 02:01:43 +00001001 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001002
1003 // Reset Lo class, this will be recomputed.
1004 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001005
1006 // If this is a C++ record, classify the bases first.
1007 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1008 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1009 e = CXXRD->bases_end(); i != e; ++i) {
1010 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1011 "Unexpected base class!");
1012 const CXXRecordDecl *Base =
1013 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1014
1015 // Classify this field.
1016 //
1017 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1018 // single eightbyte, each is classified separately. Each eightbyte gets
1019 // initialized to class NO_CLASS.
1020 Class FieldLo, FieldHi;
1021 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001022 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001023 Lo = merge(Lo, FieldLo);
1024 Hi = merge(Hi, FieldHi);
1025 if (Lo == Memory || Hi == Memory)
1026 break;
1027 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001028
Chris Lattnercd840842010-07-29 17:04:54 +00001029 // If this record has no fields, no bases, no vtable, but isn't empty,
1030 // classify as INTEGER.
1031 if (CXXRD->isEmpty() && Size)
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001032 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001033 }
1034
1035 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001036 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001037 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1038 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001039 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1040 bool BitField = i->isBitField();
1041
1042 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1043 // fields, it has class MEMORY.
1044 //
1045 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001046 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001047 Lo = Memory;
1048 return;
1049 }
1050
1051 // Classify this field.
1052 //
1053 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1054 // exceeds a single eightbyte, each is classified
1055 // separately. Each eightbyte gets initialized to class
1056 // NO_CLASS.
1057 Class FieldLo, FieldHi;
1058
1059 // Bit-fields require special handling, they do not force the
1060 // structure to be passed in memory even if unaligned, and
1061 // therefore they can straddle an eightbyte.
1062 if (BitField) {
1063 // Ignore padding bit-fields.
1064 if (i->isUnnamedBitfield())
1065 continue;
1066
1067 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001068 uint64_t Size =
1069 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001070
1071 uint64_t EB_Lo = Offset / 64;
1072 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1073 FieldLo = FieldHi = NoClass;
1074 if (EB_Lo) {
1075 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1076 FieldLo = NoClass;
1077 FieldHi = Integer;
1078 } else {
1079 FieldLo = Integer;
1080 FieldHi = EB_Hi ? Integer : NoClass;
1081 }
1082 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001083 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001084 Lo = merge(Lo, FieldLo);
1085 Hi = merge(Hi, FieldHi);
1086 if (Lo == Memory || Hi == Memory)
1087 break;
1088 }
1089
1090 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1091 //
1092 // (a) If one of the classes is MEMORY, the whole argument is
1093 // passed in memory.
1094 //
1095 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1096
1097 // The first of these conditions is guaranteed by how we implement
1098 // the merge (just bail).
1099 //
1100 // The second condition occurs in the case of unions; for example
1101 // union { _Complex double; unsigned; }.
1102 if (Hi == Memory)
1103 Lo = Memory;
1104 if (Hi == SSEUp && Lo != SSE)
1105 Hi = SSE;
1106 }
1107}
1108
1109ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001110 const llvm::Type *CoerceTo) const {
Chris Lattner4c1e4842010-07-28 22:15:08 +00001111 // If this is a pointer passed as a pointer, just pass it directly.
1112 if ((isa<llvm::PointerType>(CoerceTo) || CoerceTo->isIntegerTy(64)) &&
1113 Ty->hasPointerRepresentation())
1114 return ABIArgInfo::getExtend();
1115
1116 if (isa<llvm::IntegerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001117 // Integer and pointer types will end up in a general purpose
1118 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001119
1120 // Treat an enum type as its underlying type.
1121 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1122 Ty = EnumTy->getDecl()->getIntegerType();
1123
Chris Lattner4c1e4842010-07-28 22:15:08 +00001124 if (Ty->isIntegralOrEnumerationType())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001125 return (Ty->isPromotableIntegerType() ?
1126 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001127
Chris Lattnerfa20e952010-06-26 21:52:32 +00001128 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001129 assert(Ty.isCanonical() && "should always have a canonical type here");
1130 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001131
1132 // Float and double end up in a single SSE reg.
Chris Lattner2b037972010-07-29 02:01:43 +00001133 if (Ty == getContext().FloatTy || Ty == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001134 return ABIArgInfo::getDirect();
1135
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001136 // If this is a 32-bit structure that is passed as a double, then it will be
1137 // passed in the low 32-bits of the XMM register, which is the same as how a
1138 // float is passed. Coerce to a float instead of a double.
Chris Lattner2b037972010-07-29 02:01:43 +00001139 if (getContext().getTypeSizeInChars(Ty).getQuantity() == 4)
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001140 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001141 }
1142
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001143 return ABIArgInfo::getDirect(CoerceTo);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001144}
1145
Chris Lattner22a931e2010-06-29 06:01:59 +00001146ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001147 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1148 // place naturally.
1149 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1150 // Treat an enum type as its underlying type.
1151 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1152 Ty = EnumTy->getDecl()->getIntegerType();
1153
1154 return (Ty->isPromotableIntegerType() ?
1155 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1156 }
1157
1158 return ABIArgInfo::getIndirect(0);
1159}
1160
Chris Lattner22a931e2010-06-29 06:01:59 +00001161ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001162 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1163 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001164 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1165 // Treat an enum type as its underlying type.
1166 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1167 Ty = EnumTy->getDecl()->getIntegerType();
1168
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001169 return (Ty->isPromotableIntegerType() ?
1170 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001171 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001172
Daniel Dunbar53fac692010-04-21 19:49:55 +00001173 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1174 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001175
Daniel Dunbar53fac692010-04-21 19:49:55 +00001176 // Compute the byval alignment. We trust the back-end to honor the
1177 // minimum ABI alignment for byval, to make cleaner IR.
1178 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001179 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001180 if (Align > MinABIAlign)
1181 return ABIArgInfo::getIndirect(Align);
1182 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001183}
1184
Chris Lattner4200fe42010-07-29 04:56:46 +00001185/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1186/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1187/// vector register.
1188const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001189 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
1190
1191 // Wrapper structs that just contain vectors are passed just like vectors,
1192 // strip them off if present.
1193 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1194 while (STy && STy->getNumElements() == 1) {
1195 IRType = STy->getElementType(0);
1196 STy = dyn_cast<llvm::StructType>(IRType);
1197 }
1198
Chris Lattner4200fe42010-07-29 04:56:46 +00001199 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001200 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001201 const llvm::Type *EltTy = VT->getElementType();
1202 if (VT->getBitWidth() == 128 &&
1203 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1204 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1205 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1206 EltTy->isIntegerTy(128)))
1207 return VT;
1208 }
1209
1210 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1211}
1212
1213
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001214/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1215/// is known to either be off the end of the specified type or being in
1216/// alignment padding. The user type specified is known to be at most 128 bits
1217/// in size, and have passed through X86_64ABIInfo::classify with a successful
1218/// classification that put one of the two halves in the INTEGER class.
1219///
1220/// It is conservatively correct to return false.
1221static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1222 unsigned EndBit, ASTContext &Context) {
1223 // If the bytes being queried are off the end of the type, there is no user
1224 // data hiding here. This handles analysis of builtins, vectors and other
1225 // types that don't contain interesting padding.
1226 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1227 if (TySize <= StartBit)
1228 return true;
1229
Chris Lattner98076a22010-07-29 07:43:55 +00001230 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1231 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1232 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1233
1234 // Check each element to see if the element overlaps with the queried range.
1235 for (unsigned i = 0; i != NumElts; ++i) {
1236 // If the element is after the span we care about, then we're done..
1237 unsigned EltOffset = i*EltSize;
1238 if (EltOffset >= EndBit) break;
1239
1240 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1241 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1242 EndBit-EltOffset, Context))
1243 return false;
1244 }
1245 // If it overlaps no elements, then it is safe to process as padding.
1246 return true;
1247 }
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001248
1249 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1250 const RecordDecl *RD = RT->getDecl();
1251 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1252
1253 // If this is a C++ record, check the bases first.
1254 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1255 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1256 e = CXXRD->bases_end(); i != e; ++i) {
1257 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1258 "Unexpected base class!");
1259 const CXXRecordDecl *Base =
1260 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1261
1262 // If the base is after the span we care about, ignore it.
1263 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1264 if (BaseOffset >= EndBit) continue;
1265
1266 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1267 if (!BitsContainNoUserData(i->getType(), BaseStart,
1268 EndBit-BaseOffset, Context))
1269 return false;
1270 }
1271 }
1272
1273 // Verify that no field has data that overlaps the region of interest. Yes
1274 // this could be sped up a lot by being smarter about queried fields,
1275 // however we're only looking at structs up to 16 bytes, so we don't care
1276 // much.
1277 unsigned idx = 0;
1278 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1279 i != e; ++i, ++idx) {
1280 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
1281
1282 // If we found a field after the region we care about, then we're done.
1283 if (FieldOffset >= EndBit) break;
1284
1285 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1286 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1287 Context))
1288 return false;
1289 }
1290
1291 // If nothing in this record overlapped the area of interest, then we're
1292 // clean.
1293 return true;
1294 }
1295
1296 return false;
1297}
1298
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001299/// Get8ByteTypeAtOffset - The ABI specifies that a value should be passed in an
1300/// 8-byte GPR. This means that we either have a scalar or we are talking about
1301/// the high or low part of an up-to-16-byte struct. This routine picks the
1302/// best LLVM IR type to represent this, which may be i64 or may be anything
1303/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1304/// etc).
1305///
1306/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1307/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1308/// the 8-byte value references. PrefType may be null.
1309///
1310/// SourceTy is the source level type for the entire argument. SourceOffset is
1311/// an offset into this that we're processing (which is always either 0 or 8).
1312///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001313const llvm::Type *X86_64ABIInfo::
Chris Lattner029c0f12010-07-29 04:41:05 +00001314Get8ByteTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
Chris Lattnerc11301c2010-07-29 02:20:19 +00001315 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001316 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1317 // returning an 8-byte unit starting with it. See if we can safely use it.
1318 if (IROffset == 0) {
1319 // Pointers and int64's always fill the 8-byte unit.
1320 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1321 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001322
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001323 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1324 // goodness in the source type is just tail padding. This is allowed to
1325 // kick in for struct {double,int} on the int, but not on
1326 // struct{double,int,int} because we wouldn't return the second int. We
1327 // have to do this analysis on the source type because we can't depend on
1328 // unions being lowered a specific way etc.
1329 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1330 IRType->isIntegerTy(32)) {
1331 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
1332
1333 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1334 SourceOffset*8+64, getContext()))
1335 return IRType;
1336 }
1337 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001338
Chris Lattnerce1bd752010-07-29 04:51:12 +00001339 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001340 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001341 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001342 if (IROffset < SL->getSizeInBytes()) {
1343 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1344 IROffset -= SL->getElementOffset(FieldIdx);
1345
1346 return Get8ByteTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
Chris Lattnerc11301c2010-07-29 02:20:19 +00001347 SourceTy, SourceOffset);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001348 }
1349 }
Chris Lattner22a931e2010-06-29 06:01:59 +00001350
Chris Lattner98076a22010-07-29 07:43:55 +00001351 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1352 const llvm::Type *EltTy = ATy->getElementType();
1353 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1354 unsigned EltOffset = IROffset/EltSize*EltSize;
1355 return Get8ByteTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1356 SourceOffset);
1357 }
1358
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001359 // Okay, we don't have any better idea of what to pass, so we pass this in an
1360 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001361 unsigned TySizeInBytes =
1362 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001363
Chris Lattner3f763422010-07-29 17:34:39 +00001364 assert(TySizeInBytes != SourceOffset && "Empty field?");
1365
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001366 // It is always safe to classify this as an integer type up to i64 that
1367 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001368 return llvm::IntegerType::get(getVMContext(),
1369 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001370}
1371
Chris Lattner31faff52010-07-28 23:06:14 +00001372ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001373classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001374 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1375 // classification algorithm.
1376 X86_64ABIInfo::Class Lo, Hi;
1377 classify(RetTy, 0, Lo, Hi);
1378
1379 // Check some invariants.
1380 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1381 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1382 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1383
1384 const llvm::Type *ResType = 0;
1385 switch (Lo) {
1386 case NoClass:
1387 return ABIArgInfo::getIgnore();
1388
1389 case SSEUp:
1390 case X87Up:
1391 assert(0 && "Invalid classification for lo word.");
1392
1393 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1394 // hidden argument.
1395 case Memory:
1396 return getIndirectReturnResult(RetTy);
1397
1398 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1399 // available register of the sequence %rax, %rdx is used.
1400 case Integer:
Chris Lattnerce1bd752010-07-29 04:51:12 +00001401 ResType = Get8ByteTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy,0);
Chris Lattner31faff52010-07-28 23:06:14 +00001402 break;
1403
1404 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1405 // available SSE register of the sequence %xmm0, %xmm1 is used.
1406 case SSE:
Chris Lattner2b037972010-07-29 02:01:43 +00001407 ResType = llvm::Type::getDoubleTy(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001408 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001409
1410 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1411 // returned on the X87 stack in %st0 as 80-bit x87 number.
1412 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001413 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001414 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001415
1416 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1417 // part of the value is returned in %st0 and the imaginary part in
1418 // %st1.
1419 case ComplexX87:
1420 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001421 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001422 llvm::Type::getX86_FP80Ty(getVMContext()),
1423 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001424 NULL);
1425 break;
1426 }
1427
1428 switch (Hi) {
1429 // Memory was handled previously and X87 should
1430 // never occur as a hi class.
1431 case Memory:
1432 case X87:
1433 assert(0 && "Invalid classification for hi word.");
1434
1435 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001436 case NoClass:
1437 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001438
1439 case Integer: {
Chris Lattnerce1bd752010-07-29 04:51:12 +00001440 const llvm::Type *HiType =
1441 Get8ByteTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001442 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001443 break;
1444 }
1445 case SSE:
Chris Lattner458b2aa2010-07-29 02:16:43 +00001446 ResType = llvm::StructType::get(getVMContext(), ResType,
1447 llvm::Type::getDoubleTy(getVMContext()),
1448 NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001449 break;
1450
1451 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1452 // is passed in the upper half of the last used SSE register.
1453 //
1454 // SSEUP should always be preceeded by SSE, just widen.
1455 case SSEUp:
1456 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001457 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001458 break;
1459
1460 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1461 // returned together with the previous X87 value in %st0.
1462 case X87Up:
1463 // If X87Up is preceeded by X87, we don't need to do
1464 // anything. However, in some cases with unions it may not be
1465 // preceeded by X87. In such situations we follow gcc and pass the
1466 // extra bits in an SSE reg.
1467 if (Lo != X87)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001468 ResType = llvm::StructType::get(getVMContext(), ResType,
1469 llvm::Type::getDoubleTy(getVMContext()),
1470 NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001471 break;
1472 }
1473
1474 return getCoerceResult(RetTy, ResType);
1475}
1476
Chris Lattner458b2aa2010-07-29 02:16:43 +00001477ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001478 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001479 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001480 classify(Ty, 0, Lo, Hi);
Chris Lattner029c0f12010-07-29 04:41:05 +00001481
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001482 // Check some invariants.
1483 // FIXME: Enforce these by construction.
1484 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1485 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1486 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1487
1488 neededInt = 0;
1489 neededSSE = 0;
1490 const llvm::Type *ResType = 0;
1491 switch (Lo) {
1492 case NoClass:
1493 return ABIArgInfo::getIgnore();
1494
1495 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1496 // on the stack.
1497 case Memory:
1498
1499 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1500 // COMPLEX_X87, it is passed in memory.
1501 case X87:
1502 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001503 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001504
1505 case SSEUp:
1506 case X87Up:
1507 assert(0 && "Invalid classification for lo word.");
1508
1509 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1510 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1511 // and %r9 is used.
1512 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001513 ++neededInt;
Chris Lattner029c0f12010-07-29 04:41:05 +00001514
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001515 // Pick an 8-byte type based on the preferred type.
Chris Lattnerce1bd752010-07-29 04:51:12 +00001516 ResType = Get8ByteTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001517 break;
1518
1519 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1520 // available SSE register is used, the registers are taken in the
1521 // order from %xmm0 to %xmm7.
1522 case SSE:
1523 ++neededSSE;
Chris Lattner458b2aa2010-07-29 02:16:43 +00001524 ResType = llvm::Type::getDoubleTy(getVMContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001525 break;
1526 }
1527
1528 switch (Hi) {
1529 // Memory was handled previously, ComplexX87 and X87 should
1530 // never occur as hi classes, and X87Up must be preceed by X87,
1531 // which is passed in memory.
1532 case Memory:
1533 case X87:
1534 case ComplexX87:
1535 assert(0 && "Invalid classification for hi word.");
1536 break;
1537
1538 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001539
1540 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001541 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001542 // Pick an 8-byte type based on the preferred type.
Chris Lattnerce1bd752010-07-29 04:51:12 +00001543 const llvm::Type *HiType =
1544 Get8ByteTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Chris Lattner458b2aa2010-07-29 02:16:43 +00001545 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001546 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001547 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001548
1549 // X87Up generally doesn't occur here (long double is passed in
1550 // memory), except in situations involving unions.
1551 case X87Up:
1552 case SSE:
Chris Lattner458b2aa2010-07-29 02:16:43 +00001553 ResType = llvm::StructType::get(getVMContext(), ResType,
1554 llvm::Type::getDoubleTy(getVMContext()),
1555 NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001556 ++neededSSE;
1557 break;
1558
1559 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1560 // eightbyte is passed in the upper half of the last used SSE
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001561 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001562 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001563 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001564 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001565 break;
1566 }
1567
Chris Lattner22a931e2010-06-29 06:01:59 +00001568 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001569}
1570
Chris Lattner22326a12010-07-29 02:31:05 +00001571void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner029c0f12010-07-29 04:41:05 +00001572
Chris Lattner458b2aa2010-07-29 02:16:43 +00001573 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001574
1575 // Keep track of the number of assigned registers.
1576 unsigned freeIntRegs = 6, freeSSERegs = 8;
1577
1578 // If the return value is indirect, then the hidden argument is consuming one
1579 // integer register.
1580 if (FI.getReturnInfo().isIndirect())
1581 --freeIntRegs;
1582
1583 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1584 // get assigned (in left-to-right order) for passing as follows...
1585 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1586 it != ie; ++it) {
1587 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001588 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001589
1590 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1591 // eightbyte of an argument, the whole argument is passed on the
1592 // stack. If registers have already been assigned for some
1593 // eightbytes of such an argument, the assignments get reverted.
1594 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1595 freeIntRegs -= neededInt;
1596 freeSSERegs -= neededSSE;
1597 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001598 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001599 }
1600 }
1601}
1602
1603static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1604 QualType Ty,
1605 CodeGenFunction &CGF) {
1606 llvm::Value *overflow_arg_area_p =
1607 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1608 llvm::Value *overflow_arg_area =
1609 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1610
1611 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1612 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1613 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1614 if (Align > 8) {
1615 // Note that we follow the ABI & gcc here, even though the type
1616 // could in theory have an alignment greater than 16. This case
1617 // shouldn't ever matter in practice.
1618
1619 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001620 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001621 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001622 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1623 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001624 CGF.Int64Ty);
1625 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001626 overflow_arg_area =
1627 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1628 overflow_arg_area->getType(),
1629 "overflow_arg_area.align");
1630 }
1631
1632 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1633 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1634 llvm::Value *Res =
1635 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001636 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001637
1638 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1639 // l->overflow_arg_area + sizeof(type).
1640 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1641 // an 8 byte boundary.
1642
1643 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001644 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001645 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001646 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1647 "overflow_arg_area.next");
1648 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1649
1650 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1651 return Res;
1652}
1653
1654llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1655 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001656 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001657
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001658 // Assume that va_list type is correct; should be pointer to LLVM type:
1659 // struct {
1660 // i32 gp_offset;
1661 // i32 fp_offset;
1662 // i8* overflow_arg_area;
1663 // i8* reg_save_area;
1664 // };
1665 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001666
1667 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001668 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001669
1670 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1671 // in the registers. If not go to step 7.
1672 if (!neededInt && !neededSSE)
1673 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1674
1675 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1676 // general purpose registers needed to pass type and num_fp to hold
1677 // the number of floating point registers needed.
1678
1679 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1680 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1681 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1682 //
1683 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1684 // register save space).
1685
1686 llvm::Value *InRegs = 0;
1687 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1688 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1689 if (neededInt) {
1690 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1691 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001692 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1693 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001694 }
1695
1696 if (neededSSE) {
1697 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1698 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1699 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001700 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1701 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001702 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1703 }
1704
1705 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1706 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1707 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1708 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1709
1710 // Emit code to load the value if it was passed in registers.
1711
1712 CGF.EmitBlock(InRegBlock);
1713
1714 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1715 // an offset of l->gp_offset and/or l->fp_offset. This may require
1716 // copying to a temporary location in case the parameter is passed
1717 // in different register classes or requires an alignment greater
1718 // than 8 for general purpose registers and 16 for XMM registers.
1719 //
1720 // FIXME: This really results in shameful code when we end up needing to
1721 // collect arguments from different places; often what should result in a
1722 // simple assembling of a structure from scattered addresses has many more
1723 // loads than necessary. Can we clean this up?
1724 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1725 llvm::Value *RegAddr =
1726 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1727 "reg_save_area");
1728 if (neededInt && neededSSE) {
1729 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001730 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001731 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1732 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1733 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1734 const llvm::Type *TyLo = ST->getElementType(0);
1735 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001736 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001737 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001738 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1739 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001740 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1741 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001742 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1743 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001744 llvm::Value *V =
1745 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1746 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1747 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1748 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1749
Owen Anderson170229f2009-07-14 23:10:40 +00001750 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001751 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001752 } else if (neededInt) {
1753 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1754 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001755 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001756 } else if (neededSSE == 1) {
1757 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1758 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1759 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001760 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001761 assert(neededSSE == 2 && "Invalid number of needed registers!");
1762 // SSE registers are spaced 16 bytes apart in the register save
1763 // area, we need to collect the two eightbytes together.
1764 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001765 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001766 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1767 const llvm::Type *DblPtrTy =
1768 llvm::PointerType::getUnqual(DoubleTy);
1769 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1770 DoubleTy, NULL);
1771 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1772 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1773 DblPtrTy));
1774 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1775 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1776 DblPtrTy));
1777 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1778 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1779 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001780 }
1781
1782 // AMD64-ABI 3.5.7p5: Step 5. Set:
1783 // l->gp_offset = l->gp_offset + num_gp * 8
1784 // l->fp_offset = l->fp_offset + num_fp * 16.
1785 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001786 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001787 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1788 gp_offset_p);
1789 }
1790 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001791 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001792 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1793 fp_offset_p);
1794 }
1795 CGF.EmitBranch(ContBlock);
1796
1797 // Emit code to load the value if it was passed in memory.
1798
1799 CGF.EmitBlock(InMemBlock);
1800 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1801
1802 // Return the appropriate result.
1803
1804 CGF.EmitBlock(ContBlock);
1805 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1806 "vaarg.addr");
1807 ResAddr->reserveOperandSpace(2);
1808 ResAddr->addIncoming(RegAddr, InRegBlock);
1809 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001810 return ResAddr;
1811}
1812
Chris Lattner0cf24192010-06-28 20:05:43 +00001813
1814
1815//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001816// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001817//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001818
1819namespace {
1820
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001821class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001822public:
1823 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
1824
Chris Lattner458b2aa2010-07-29 02:16:43 +00001825 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001826
Chris Lattner458b2aa2010-07-29 02:16:43 +00001827 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001828
Chris Lattner22326a12010-07-29 02:31:05 +00001829 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001830 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001831 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1832 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001833 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001834 }
1835
1836 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1837 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001838};
1839
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001840class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1841public:
Chris Lattner2b037972010-07-29 02:01:43 +00001842 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1843 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001844};
1845
Daniel Dunbard59655c2009-09-12 00:59:49 +00001846}
1847
Chris Lattner458b2aa2010-07-29 02:16:43 +00001848ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001849 if (RetTy->isVoidType()) {
1850 return ABIArgInfo::getIgnore();
1851 } else {
1852 return ABIArgInfo::getDirect();
1853 }
1854}
1855
Chris Lattner458b2aa2010-07-29 02:16:43 +00001856ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001857 return ABIArgInfo::getDirect();
1858}
1859
1860llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001861 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001862 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001863 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1864
1865 CGBuilderTy &Builder = CGF.Builder;
1866 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1867 "ap");
1868 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1869 llvm::Type *PTy =
1870 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1871 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1872
1873 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1874
1875 llvm::Value *NextAddr =
1876 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1877 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1878 "ap.next");
1879 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1880
1881 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001882}
1883
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001884
John McCallea8d8bb2010-03-11 00:10:12 +00001885// PowerPC-32
1886
1887namespace {
1888class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1889public:
Chris Lattner2b037972010-07-29 02:01:43 +00001890 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
1891
John McCallea8d8bb2010-03-11 00:10:12 +00001892 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1893 // This is recovered from gcc output.
1894 return 1; // r1 is the dedicated stack pointer
1895 }
1896
1897 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1898 llvm::Value *Address) const;
1899};
1900
1901}
1902
1903bool
1904PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1905 llvm::Value *Address) const {
1906 // This is calculated from the LLVM and GCC tables and verified
1907 // against gcc output. AFAIK all ABIs use the same encoding.
1908
1909 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1910 llvm::LLVMContext &Context = CGF.getLLVMContext();
1911
1912 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1913 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1914 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1915 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1916
1917 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001918 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001919
1920 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001921 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001922
1923 // 64-76 are various 4-byte special-purpose registers:
1924 // 64: mq
1925 // 65: lr
1926 // 66: ctr
1927 // 67: ap
1928 // 68-75 cr0-7
1929 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001930 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001931
1932 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001933 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001934
1935 // 109: vrsave
1936 // 110: vscr
1937 // 111: spe_acc
1938 // 112: spefscr
1939 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001940 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001941
1942 return false;
1943}
1944
1945
Chris Lattner0cf24192010-06-28 20:05:43 +00001946//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001947// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001948//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001949
1950namespace {
1951
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001952class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001953public:
1954 enum ABIKind {
1955 APCS = 0,
1956 AAPCS = 1,
1957 AAPCS_VFP
1958 };
1959
1960private:
1961 ABIKind Kind;
1962
1963public:
Chris Lattner2b037972010-07-29 02:01:43 +00001964 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00001965
1966private:
1967 ABIKind getABIKind() const { return Kind; }
1968
Chris Lattner458b2aa2010-07-29 02:16:43 +00001969 ABIArgInfo classifyReturnType(QualType RetTy) const;
1970 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001971
Chris Lattner22326a12010-07-29 02:31:05 +00001972 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001973
1974 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1975 CodeGenFunction &CGF) const;
1976};
1977
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001978class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1979public:
Chris Lattner2b037972010-07-29 02:01:43 +00001980 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
1981 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001982
1983 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1984 return 13;
1985 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001986};
1987
Daniel Dunbard59655c2009-09-12 00:59:49 +00001988}
1989
Chris Lattner22326a12010-07-29 02:31:05 +00001990void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001991 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001992 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00001993 it != ie; ++it)
1994 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001995
Chris Lattner458b2aa2010-07-29 02:16:43 +00001996 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001997 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001998 if (Triple.getEnvironmentName() == "gnueabi" ||
1999 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002000 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002001 else
2002 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002003
Daniel Dunbar020daa92009-09-12 01:00:39 +00002004 switch (getABIKind()) {
2005 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002006 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2007 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002008 break;
2009
2010 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002011 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2012 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002013 break;
2014
2015 case AAPCS_VFP:
2016 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2017 break;
2018 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002019}
2020
Chris Lattner458b2aa2010-07-29 02:16:43 +00002021ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00002022 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
2023 // Treat an enum type as its underlying type.
2024 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2025 Ty = EnumTy->getDecl()->getIntegerType();
2026
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002027 return (Ty->isPromotableIntegerType() ?
2028 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002029 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002030
Daniel Dunbar09d33622009-09-14 21:54:03 +00002031 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002032 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002033 return ABIArgInfo::getIgnore();
2034
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002035 // Structures with either a non-trivial destructor or a non-trivial
2036 // copy constructor are always indirect.
2037 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2038 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2039
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002040 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2041 // backend doesn't support byval.
2042 // FIXME: This doesn't handle alignment > 64 bits.
2043 const llvm::Type* ElemTy;
2044 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002045 if (getContext().getTypeAlign(Ty) > 32) {
2046 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2047 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002048 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002049 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2050 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002051 }
2052 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002053 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002054 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2055 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002056 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002057}
2058
Chris Lattner458b2aa2010-07-29 02:16:43 +00002059static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002060 llvm::LLVMContext &VMContext) {
2061 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2062 // is called integer-like if its size is less than or equal to one word, and
2063 // the offset of each of its addressable sub-fields is zero.
2064
2065 uint64_t Size = Context.getTypeSize(Ty);
2066
2067 // Check that the type fits in a word.
2068 if (Size > 32)
2069 return false;
2070
2071 // FIXME: Handle vector types!
2072 if (Ty->isVectorType())
2073 return false;
2074
Daniel Dunbard53bac72009-09-14 02:20:34 +00002075 // Float types are never treated as "integer like".
2076 if (Ty->isRealFloatingType())
2077 return false;
2078
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002079 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002080 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002081 return true;
2082
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002083 // Small complex integer types are "integer like".
2084 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2085 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002086
2087 // Single element and zero sized arrays should be allowed, by the definition
2088 // above, but they are not.
2089
2090 // Otherwise, it must be a record type.
2091 const RecordType *RT = Ty->getAs<RecordType>();
2092 if (!RT) return false;
2093
2094 // Ignore records with flexible arrays.
2095 const RecordDecl *RD = RT->getDecl();
2096 if (RD->hasFlexibleArrayMember())
2097 return false;
2098
2099 // Check that all sub-fields are at offset 0, and are themselves "integer
2100 // like".
2101 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2102
2103 bool HadField = false;
2104 unsigned idx = 0;
2105 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2106 i != e; ++i, ++idx) {
2107 const FieldDecl *FD = *i;
2108
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002109 // Bit-fields are not addressable, we only need to verify they are "integer
2110 // like". We still have to disallow a subsequent non-bitfield, for example:
2111 // struct { int : 0; int x }
2112 // is non-integer like according to gcc.
2113 if (FD->isBitField()) {
2114 if (!RD->isUnion())
2115 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002116
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002117 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2118 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002119
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002120 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002121 }
2122
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002123 // Check if this field is at offset 0.
2124 if (Layout.getFieldOffset(idx) != 0)
2125 return false;
2126
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002127 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2128 return false;
2129
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002130 // Only allow at most one field in a structure. This doesn't match the
2131 // wording above, but follows gcc in situations with a field following an
2132 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002133 if (!RD->isUnion()) {
2134 if (HadField)
2135 return false;
2136
2137 HadField = true;
2138 }
2139 }
2140
2141 return true;
2142}
2143
Chris Lattner458b2aa2010-07-29 02:16:43 +00002144ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002145 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002146 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002147
Douglas Gregora71cc152010-02-02 20:10:50 +00002148 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2149 // Treat an enum type as its underlying type.
2150 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2151 RetTy = EnumTy->getDecl()->getIntegerType();
2152
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002153 return (RetTy->isPromotableIntegerType() ?
2154 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002155 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002156
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002157 // Structures with either a non-trivial destructor or a non-trivial
2158 // copy constructor are always indirect.
2159 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2160 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2161
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002162 // Are we following APCS?
2163 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002164 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002165 return ABIArgInfo::getIgnore();
2166
Daniel Dunbareedf1512010-02-01 23:31:19 +00002167 // Complex types are all returned as packed integers.
2168 //
2169 // FIXME: Consider using 2 x vector types if the back end handles them
2170 // correctly.
2171 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002172 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002173 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002174
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002175 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002176 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002177 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002178 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002179 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002180 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002181 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002182 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2183 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002184 }
2185
2186 // Otherwise return in memory.
2187 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002188 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002189
2190 // Otherwise this is an AAPCS variant.
2191
Chris Lattner458b2aa2010-07-29 02:16:43 +00002192 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002193 return ABIArgInfo::getIgnore();
2194
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002195 // Aggregates <= 4 bytes are returned in r0; other aggregates
2196 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002197 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002198 if (Size <= 32) {
2199 // Return in the smallest viable integer type.
2200 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002201 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002202 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002203 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2204 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002205 }
2206
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002207 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002208}
2209
2210llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002211 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002212 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002213 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002214 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002215
2216 CGBuilderTy &Builder = CGF.Builder;
2217 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2218 "ap");
2219 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2220 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002221 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002222 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2223
2224 uint64_t Offset =
2225 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2226 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002227 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002228 "ap.next");
2229 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2230
2231 return AddrTyped;
2232}
2233
Chris Lattner458b2aa2010-07-29 02:16:43 +00002234ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2235 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002236 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002237
Chris Lattner458b2aa2010-07-29 02:16:43 +00002238 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2239 return ABIArgInfo::getIndirect(0);
2240
2241 // Treat an enum type as its underlying type.
2242 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2243 RetTy = EnumTy->getDecl()->getIntegerType();
2244
2245 return (RetTy->isPromotableIntegerType() ?
2246 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002247}
2248
Chris Lattner0cf24192010-06-28 20:05:43 +00002249//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002250// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002251//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002252
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002253namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002254
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002255class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002256public:
2257 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2258
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002259 bool isPromotableIntegerType(QualType Ty) const;
2260
Chris Lattner458b2aa2010-07-29 02:16:43 +00002261 ABIArgInfo classifyReturnType(QualType RetTy) const;
2262 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002263
Chris Lattner22326a12010-07-29 02:31:05 +00002264 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002265 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002266 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2267 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002268 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002269 }
2270
2271 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2272 CodeGenFunction &CGF) const;
2273};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002274
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002275class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2276public:
Chris Lattner2b037972010-07-29 02:01:43 +00002277 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2278 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002279};
2280
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002281}
2282
2283bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2284 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002285 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002286 switch (BT->getKind()) {
2287 case BuiltinType::Bool:
2288 case BuiltinType::Char_S:
2289 case BuiltinType::Char_U:
2290 case BuiltinType::SChar:
2291 case BuiltinType::UChar:
2292 case BuiltinType::Short:
2293 case BuiltinType::UShort:
2294 case BuiltinType::Int:
2295 case BuiltinType::UInt:
2296 return true;
2297 default:
2298 return false;
2299 }
2300 return false;
2301}
2302
2303llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2304 CodeGenFunction &CGF) const {
2305 // FIXME: Implement
2306 return 0;
2307}
2308
2309
Chris Lattner458b2aa2010-07-29 02:16:43 +00002310ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2311 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002312 return ABIArgInfo::getIgnore();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002313 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002314 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002315
2316 return (isPromotableIntegerType(RetTy) ?
2317 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002318}
2319
Chris Lattner458b2aa2010-07-29 02:16:43 +00002320ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
2321 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002322 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002323
2324 return (isPromotableIntegerType(Ty) ?
2325 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002326}
2327
Chris Lattner0cf24192010-06-28 20:05:43 +00002328//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002329// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002330//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002331
2332namespace {
2333
2334class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2335public:
Chris Lattner2b037972010-07-29 02:01:43 +00002336 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2337 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002338 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2339 CodeGen::CodeGenModule &M) const;
2340};
2341
2342}
2343
2344void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2345 llvm::GlobalValue *GV,
2346 CodeGen::CodeGenModule &M) const {
2347 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2348 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2349 // Handle 'interrupt' attribute:
2350 llvm::Function *F = cast<llvm::Function>(GV);
2351
2352 // Step 1: Set ISR calling convention.
2353 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2354
2355 // Step 2: Add attributes goodness.
2356 F->addFnAttr(llvm::Attribute::NoInline);
2357
2358 // Step 3: Emit ISR vector alias.
2359 unsigned Num = attr->getNumber() + 0xffe0;
2360 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2361 "vector_" +
2362 llvm::LowercaseString(llvm::utohexstr(Num)),
2363 GV, &M.getModule());
2364 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002365 }
2366}
2367
Chris Lattner0cf24192010-06-28 20:05:43 +00002368//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002369// MIPS ABI Implementation. This works for both little-endian and
2370// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002371//===----------------------------------------------------------------------===//
2372
John McCall943fae92010-05-27 06:19:26 +00002373namespace {
2374class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2375public:
Chris Lattner2b037972010-07-29 02:01:43 +00002376 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2377 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002378
2379 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2380 return 29;
2381 }
2382
2383 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2384 llvm::Value *Address) const;
2385};
2386}
2387
2388bool
2389MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2390 llvm::Value *Address) const {
2391 // This information comes from gcc's implementation, which seems to
2392 // as canonical as it gets.
2393
2394 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2395 llvm::LLVMContext &Context = CGF.getLLVMContext();
2396
2397 // Everything on MIPS is 4 bytes. Double-precision FP registers
2398 // are aliased to pairs of single-precision FP registers.
2399 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2400 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2401
2402 // 0-31 are the general purpose registers, $0 - $31.
2403 // 32-63 are the floating-point registers, $f0 - $f31.
2404 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2405 // 66 is the (notional, I think) register for signal-handler return.
2406 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2407
2408 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2409 // They are one bit wide and ignored here.
2410
2411 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2412 // (coprocessor 1 is the FP unit)
2413 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2414 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2415 // 176-181 are the DSP accumulator registers.
2416 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2417
2418 return false;
2419}
2420
2421
Chris Lattner2b037972010-07-29 02:01:43 +00002422const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002423 if (TheTargetCodeGenInfo)
2424 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002425
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002426 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2427 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002428
Chris Lattner22a931e2010-06-29 06:01:59 +00002429 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002430 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002431 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002432 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002433
John McCall943fae92010-05-27 06:19:26 +00002434 case llvm::Triple::mips:
2435 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002436 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002437
Daniel Dunbard59655c2009-09-12 00:59:49 +00002438 case llvm::Triple::arm:
2439 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002440 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002441 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002442 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002443 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002444
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002445 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002446 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002447
2448 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002449 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002450
John McCallea8d8bb2010-03-11 00:10:12 +00002451 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002452 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002453
Daniel Dunbard59655c2009-09-12 00:59:49 +00002454 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002455 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002456
2457 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002458 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002459
Daniel Dunbar40165182009-08-24 09:10:05 +00002460 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002461 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002462 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002463 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002464 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002465 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002466 case llvm::Triple::MinGW32:
2467 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002468 case llvm::Triple::AuroraUX:
2469 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002470 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002471 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002472 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002473 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002474
2475 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002476 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002477 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002478 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002479
Daniel Dunbare3532f82009-08-24 08:52:16 +00002480 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002481 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002482 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002483}