blob: 05bea0cf85972daa2a71c0258b008b7a029e5471 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
78 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000079 break;
80 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000081 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000082 break;
83 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000084 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000085}
86
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000087TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
88
Daniel Dunbar626f1d82009-09-13 08:03:58 +000089static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090
91/// isEmptyField - Return true iff a the field is "empty", that is it
92/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000093static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
94 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000095 if (FD->isUnnamedBitfield())
96 return true;
97
98 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000099
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000100 // Constant arrays of empty records count as empty, strip them off.
101 if (AllowArrays)
102 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
103 FT = AT->getElementType();
104
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000105 const RecordType *RT = FT->getAs<RecordType>();
106 if (!RT)
107 return false;
108
109 // C++ record fields are never empty, at least in the Itanium ABI.
110 //
111 // FIXME: We should use a predicate for whether this behavior is true in the
112 // current ABI.
113 if (isa<CXXRecordDecl>(RT->getDecl()))
114 return false;
115
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000116 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000117}
118
119/// isEmptyRecord - Return true iff a structure contains only empty
120/// fields. Note that a structure with a flexible array member is not
121/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000122static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000123 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000124 if (!RT)
125 return 0;
126 const RecordDecl *RD = RT->getDecl();
127 if (RD->hasFlexibleArrayMember())
128 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000129
130 // If this is a C++ record, check the bases first.
131 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
132 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
133 e = CXXRD->bases_end(); i != e; ++i)
134 if (!isEmptyRecord(Context, i->getType(), true))
135 return false;
136
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000137 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
138 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000139 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000140 return false;
141 return true;
142}
143
Anders Carlsson20759ad2009-09-16 15:53:40 +0000144/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
145/// a non-trivial destructor or a non-trivial copy constructor.
146static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
147 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
148 if (!RD)
149 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000150
Anders Carlsson20759ad2009-09-16 15:53:40 +0000151 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
152}
153
154/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
155/// a record type with either a non-trivial destructor or a non-trivial copy
156/// constructor.
157static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
158 const RecordType *RT = T->getAs<RecordType>();
159 if (!RT)
160 return false;
161
162 return hasNonTrivialDestructorOrCopyConstructor(RT);
163}
164
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000165/// isSingleElementStruct - Determine if a structure is a "single
166/// element struct", i.e. it has exactly one non-empty field or
167/// exactly one field which is itself a single element
168/// struct. Structures with flexible array members are never
169/// considered single element structs.
170///
171/// \return The field declaration for the single non-empty field, if
172/// it exists.
173static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
174 const RecordType *RT = T->getAsStructureType();
175 if (!RT)
176 return 0;
177
178 const RecordDecl *RD = RT->getDecl();
179 if (RD->hasFlexibleArrayMember())
180 return 0;
181
182 const Type *Found = 0;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000183
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000184 // If this is a C++ record, check the bases first.
185 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
186 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
187 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000188 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000189 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000190 continue;
191
192 // If we already found an element then this isn't a single-element struct.
193 if (Found)
194 return 0;
195
196 // If this is non-empty and not a single element struct, the composite
197 // cannot be a single element struct.
198 Found = isSingleElementStruct(i->getType(), Context);
199 if (!Found)
200 return 0;
201 }
202 }
203
204 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
206 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000207 const FieldDecl *FD = *i;
208 QualType FT = FD->getType();
209
210 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000211 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000212 continue;
213
214 // If we already found an element then this isn't a single-element
215 // struct.
216 if (Found)
217 return 0;
218
219 // Treat single element arrays as the element.
220 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
221 if (AT->getSize().getZExtValue() != 1)
222 break;
223 FT = AT->getElementType();
224 }
225
John McCalla1dee5302010-08-22 10:59:02 +0000226 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000227 Found = FT.getTypePtr();
228 } else {
229 Found = isSingleElementStruct(FT, Context);
230 if (!Found)
231 return 0;
232 }
233 }
234
235 return Found;
236}
237
238static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000239 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000240 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
241 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000242 return false;
243
244 uint64_t Size = Context.getTypeSize(Ty);
245 return Size == 32 || Size == 64;
246}
247
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000248/// canExpandIndirectArgument - Test whether an argument type which is to be
249/// passed indirectly (on the stack) would have the equivalent layout if it was
250/// expanded into separate arguments. If so, we prefer to do the latter to avoid
251/// inhibiting optimizations.
252///
253// FIXME: This predicate is missing many cases, currently it just follows
254// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
255// should probably make this smarter, or better yet make the LLVM backend
256// capable of handling it.
257static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
258 // We can only expand structure types.
259 const RecordType *RT = Ty->getAs<RecordType>();
260 if (!RT)
261 return false;
262
263 // We can only expand (C) structures.
264 //
265 // FIXME: This needs to be generalized to handle classes as well.
266 const RecordDecl *RD = RT->getDecl();
267 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
268 return false;
269
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000272 const FieldDecl *FD = *i;
273
274 if (!is32Or64BitBasicType(FD->getType(), Context))
275 return false;
276
277 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
278 // how to expand them yet, and the predicate for telling if a bitfield still
279 // counts as "basic" is more complicated than what we were doing previously.
280 if (FD->isBitField())
281 return false;
282 }
283
284 return true;
285}
286
287namespace {
288/// DefaultABIInfo - The default implementation for ABI specific
289/// details. This implementation provides information which results in
290/// self-consistent and sensible LLVM IR generation, but does not
291/// conform to any particular ABI.
292class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000293public:
294 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000295
Chris Lattner458b2aa2010-07-29 02:16:43 +0000296 ABIArgInfo classifyReturnType(QualType RetTy) const;
297 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000298
Chris Lattner22326a12010-07-29 02:31:05 +0000299 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
302 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000303 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000304 }
305
306 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
307 CodeGenFunction &CGF) const;
308};
309
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000310class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
311public:
Chris Lattner2b037972010-07-29 02:01:43 +0000312 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
313 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000314};
315
316llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
317 CodeGenFunction &CGF) const {
318 return 0;
319}
320
Chris Lattner458b2aa2010-07-29 02:16:43 +0000321ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000322 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000323 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000324
Chris Lattner9723d6c2010-03-11 18:19:55 +0000325 // Treat an enum type as its underlying type.
326 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
327 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000328
Chris Lattner9723d6c2010-03-11 18:19:55 +0000329 return (Ty->isPromotableIntegerType() ?
330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000331}
332
Chris Lattner0cf24192010-06-28 20:05:43 +0000333//===----------------------------------------------------------------------===//
334// X86-32 ABI Implementation
335//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000336
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000337/// X86_32ABIInfo - The X86-32 ABI information.
338class X86_32ABIInfo : public ABIInfo {
David Chisnallde3a0692009-08-17 23:08:21 +0000339 bool IsDarwinVectorABI;
340 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000341
342 static bool isRegisterSize(unsigned Size) {
343 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
344 }
345
346 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
347
Daniel Dunbar557893d2010-04-21 19:10:51 +0000348 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
349 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000350 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000351
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000352public:
Chris Lattner2b037972010-07-29 02:01:43 +0000353
Chris Lattner458b2aa2010-07-29 02:16:43 +0000354 ABIArgInfo classifyReturnType(QualType RetTy) const;
355 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000356
Chris Lattner22326a12010-07-29 02:31:05 +0000357 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000358 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000359 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
360 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000361 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000362 }
363
364 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
365 CodeGenFunction &CGF) const;
366
Chris Lattner2b037972010-07-29 02:01:43 +0000367 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
368 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000369};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000370
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000371class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
372public:
Chris Lattner2b037972010-07-29 02:01:43 +0000373 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
374 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000375
376 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
377 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000378
379 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
380 // Darwin uses different dwarf register numbers for EH.
381 if (CGM.isTargetDarwin()) return 5;
382
383 return 4;
384 }
385
386 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
387 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000388};
389
390}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000391
392/// shouldReturnTypeInRegister - Determine if the given type should be
393/// passed in a register (for the Darwin ABI).
394bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
395 ASTContext &Context) {
396 uint64_t Size = Context.getTypeSize(Ty);
397
398 // Type must be register sized.
399 if (!isRegisterSize(Size))
400 return false;
401
402 if (Ty->isVectorType()) {
403 // 64- and 128- bit vectors inside structures are not returned in
404 // registers.
405 if (Size == 64 || Size == 128)
406 return false;
407
408 return true;
409 }
410
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000411 // If this is a builtin, pointer, enum, complex type, member pointer, or
412 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000413 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000414 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000415 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000416 return true;
417
418 // Arrays are treated like records.
419 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
420 return shouldReturnTypeInRegister(AT->getElementType(), Context);
421
422 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000423 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000424 if (!RT) return false;
425
Anders Carlsson40446e82010-01-27 03:25:19 +0000426 // FIXME: Traverse bases here too.
427
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000428 // Structure types are passed in register if all fields would be
429 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000430 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
431 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000432 const FieldDecl *FD = *i;
433
434 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000435 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000436 continue;
437
438 // Check fields recursively.
439 if (!shouldReturnTypeInRegister(FD->getType(), Context))
440 return false;
441 }
442
443 return true;
444}
445
Chris Lattner458b2aa2010-07-29 02:16:43 +0000446ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
447 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000448 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000449
Chris Lattner458b2aa2010-07-29 02:16:43 +0000450 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000451 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000452 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000453 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454
455 // 128-bit vectors are a special case; they are returned in
456 // registers and we need to make sure to pick a type the LLVM
457 // backend will like.
458 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000459 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000460 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000461
462 // Always return in register if it fits in a general purpose
463 // register, or if it is 64 bits and has a single element.
464 if ((Size == 8 || Size == 16 || Size == 32) ||
465 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000466 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000467 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000468
469 return ABIArgInfo::getIndirect(0);
470 }
471
472 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000473 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000474
John McCalla1dee5302010-08-22 10:59:02 +0000475 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000476 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000477 // Structures with either a non-trivial destructor or a non-trivial
478 // copy constructor are always indirect.
479 if (hasNonTrivialDestructorOrCopyConstructor(RT))
480 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000481
Anders Carlsson5789c492009-10-20 22:07:59 +0000482 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000483 if (RT->getDecl()->hasFlexibleArrayMember())
484 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000485 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000486
David Chisnallde3a0692009-08-17 23:08:21 +0000487 // If specified, structs and unions are always indirect.
488 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000489 return ABIArgInfo::getIndirect(0);
490
491 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000492 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000493 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000494 if (BT->isIntegerType()) {
495 // We need to use the size of the structure, padding
496 // bit-fields can adjust that to be larger than the single
497 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000498 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000499 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000500 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
501 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000502
Chris Lattner458b2aa2010-07-29 02:16:43 +0000503 if (BT->getKind() == BuiltinType::Float) {
504 assert(getContext().getTypeSize(RetTy) ==
505 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000506 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000507 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000508 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000509
Chris Lattner458b2aa2010-07-29 02:16:43 +0000510 if (BT->getKind() == BuiltinType::Double) {
511 assert(getContext().getTypeSize(RetTy) ==
512 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000513 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000514 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000515 }
516 } else if (SeltTy->isPointerType()) {
517 // FIXME: It would be really nice if this could come out as the proper
518 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000519 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000520 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000521 } else if (SeltTy->isVectorType()) {
522 // 64- and 128-bit vectors are never returned in a
523 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000524 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000525 if (Size == 64 || Size == 128)
526 return ABIArgInfo::getIndirect(0);
527
Chris Lattner458b2aa2010-07-29 02:16:43 +0000528 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 }
530 }
531
532 // Small structures which are register sized are generally returned
533 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000534 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
535 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000536 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 }
538
539 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000540 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000541
Chris Lattner458b2aa2010-07-29 02:16:43 +0000542 // Treat an enum type as its underlying type.
543 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
544 RetTy = EnumTy->getDecl()->getIntegerType();
545
546 return (RetTy->isPromotableIntegerType() ?
547 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000548}
549
Chris Lattner458b2aa2010-07-29 02:16:43 +0000550ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000551 if (!ByVal)
552 return ABIArgInfo::getIndirect(0, false);
553
554 // Compute the byval alignment. We trust the back-end to honor the
555 // minimum ABI alignment for byval, to make cleaner IR.
556 const unsigned MinABIAlign = 4;
Chris Lattner458b2aa2010-07-29 02:16:43 +0000557 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000558 if (Align > MinABIAlign)
559 return ABIArgInfo::getIndirect(Align);
560 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000561}
562
Chris Lattner458b2aa2010-07-29 02:16:43 +0000563ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000564 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000565 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000566 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000567 if (const RecordType *RT = Ty->getAs<RecordType>()) {
568 // Structures with either a non-trivial destructor or a non-trivial
569 // copy constructor are always indirect.
570 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000571 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000572
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000573 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000574 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000575 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000576
577 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000578 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000579 return ABIArgInfo::getIgnore();
580
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000581 // Expand small (<= 128-bit) record types when we know that the stack layout
582 // of those arguments will match the struct. This is important because the
583 // LLVM backend isn't smart enough to remove byval, which inhibits many
584 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000585 if (getContext().getTypeSize(Ty) <= 4*32 &&
586 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000587 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000588
Chris Lattner458b2aa2010-07-29 02:16:43 +0000589 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000590 }
591
Chris Lattnerd774ae92010-08-26 20:05:13 +0000592 if (const VectorType *VT = Ty->getAs<VectorType>()) {
593 // On Darwin, some vectors are returned in registers.
594 if (IsDarwinVectorABI) {
595 uint64_t Size = getContext().getTypeSize(Ty);
596
597 // Always return in register if it fits in a general purpose
598 // register, or if it is 64 bits and has a single element.
599 if ((Size == 8 || Size == 16 || Size == 32) ||
600 (Size == 64 && VT->getNumElements() == 1))
601 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
602 Size));
603
604 return ABIArgInfo::getIndirect(0);
605 }
606
607 return ABIArgInfo::getDirect();
608 }
609
610
Chris Lattner458b2aa2010-07-29 02:16:43 +0000611 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
612 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000613
Chris Lattner458b2aa2010-07-29 02:16:43 +0000614 return (Ty->isPromotableIntegerType() ?
615 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000616}
617
618llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
619 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000620 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000621 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000622
623 CGBuilderTy &Builder = CGF.Builder;
624 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
625 "ap");
626 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
627 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000628 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000629 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
630
631 uint64_t Offset =
632 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
633 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000634 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000635 "ap.next");
636 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
637
638 return AddrTyped;
639}
640
Charles Davis4ea31ab2010-02-13 15:54:06 +0000641void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
642 llvm::GlobalValue *GV,
643 CodeGen::CodeGenModule &CGM) const {
644 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
645 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
646 // Get the LLVM function.
647 llvm::Function *Fn = cast<llvm::Function>(GV);
648
649 // Now add the 'alignstack' attribute with a value of 16.
650 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
651 }
652 }
653}
654
John McCallbeec5a02010-03-06 00:35:14 +0000655bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
656 CodeGen::CodeGenFunction &CGF,
657 llvm::Value *Address) const {
658 CodeGen::CGBuilderTy &Builder = CGF.Builder;
659 llvm::LLVMContext &Context = CGF.getLLVMContext();
660
661 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
662 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000663
John McCallbeec5a02010-03-06 00:35:14 +0000664 // 0-7 are the eight integer registers; the order is different
665 // on Darwin (for EH), but the range is the same.
666 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000667 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000668
669 if (CGF.CGM.isTargetDarwin()) {
670 // 12-16 are st(0..4). Not sure why we stop at 4.
671 // These have size 16, which is sizeof(long double) on
672 // platforms with 8-byte alignment for that type.
673 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000674 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000675
John McCallbeec5a02010-03-06 00:35:14 +0000676 } else {
677 // 9 is %eflags, which doesn't get a size on Darwin for some
678 // reason.
679 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
680
681 // 11-16 are st(0..5). Not sure why we stop at 5.
682 // These have size 12, which is sizeof(long double) on
683 // platforms with 4-byte alignment for that type.
684 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000685 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
686 }
John McCallbeec5a02010-03-06 00:35:14 +0000687
688 return false;
689}
690
Chris Lattner0cf24192010-06-28 20:05:43 +0000691//===----------------------------------------------------------------------===//
692// X86-64 ABI Implementation
693//===----------------------------------------------------------------------===//
694
695
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000696namespace {
697/// X86_64ABIInfo - The X86_64 ABI information.
698class X86_64ABIInfo : public ABIInfo {
699 enum Class {
700 Integer = 0,
701 SSE,
702 SSEUp,
703 X87,
704 X87Up,
705 ComplexX87,
706 NoClass,
707 Memory
708 };
709
710 /// merge - Implement the X86_64 ABI merging algorithm.
711 ///
712 /// Merge an accumulating classification \arg Accum with a field
713 /// classification \arg Field.
714 ///
715 /// \param Accum - The accumulating classification. This should
716 /// always be either NoClass or the result of a previous merge
717 /// call. In addition, this should never be Memory (the caller
718 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000719 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000720
721 /// classify - Determine the x86_64 register classes in which the
722 /// given type T should be passed.
723 ///
724 /// \param Lo - The classification for the parts of the type
725 /// residing in the low word of the containing object.
726 ///
727 /// \param Hi - The classification for the parts of the type
728 /// residing in the high word of the containing object.
729 ///
730 /// \param OffsetBase - The bit offset of this type in the
731 /// containing object. Some parameters are classified different
732 /// depending on whether they straddle an eightbyte boundary.
733 ///
734 /// If a word is unused its result will be NoClass; if a type should
735 /// be passed in Memory then at least the classification of \arg Lo
736 /// will be Memory.
737 ///
738 /// The \arg Lo class will be NoClass iff the argument is ignored.
739 ///
740 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
741 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000742 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000743
Chris Lattner4200fe42010-07-29 04:56:46 +0000744 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000745 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000746 unsigned IROffset, QualType SourceTy,
747 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000748 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
749 unsigned IROffset, QualType SourceTy,
750 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000751
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000752 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000753 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000754 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000755
756 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000757 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000758 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000759
Chris Lattner458b2aa2010-07-29 02:16:43 +0000760 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000761
Chris Lattner029c0f12010-07-29 04:41:05 +0000762 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
763 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000764
765public:
Chris Lattner2b037972010-07-29 02:01:43 +0000766 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000767
Chris Lattner22326a12010-07-29 02:31:05 +0000768 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000769
770 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
771 CodeGenFunction &CGF) const;
772};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000773
774class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
775public:
Chris Lattner2b037972010-07-29 02:01:43 +0000776 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
777 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000778
779 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
780 return 7;
781 }
782
783 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
784 llvm::Value *Address) const {
785 CodeGen::CGBuilderTy &Builder = CGF.Builder;
786 llvm::LLVMContext &Context = CGF.getLLVMContext();
787
788 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
789 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000790
John McCall943fae92010-05-27 06:19:26 +0000791 // 0-15 are the 16 integer registers.
792 // 16 is %rip.
793 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000794
795 return false;
796 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000797};
798
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000799}
800
Chris Lattnerd776fb12010-06-28 21:43:59 +0000801X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000802 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
803 // classified recursively so that always two fields are
804 // considered. The resulting class is calculated according to
805 // the classes of the fields in the eightbyte:
806 //
807 // (a) If both classes are equal, this is the resulting class.
808 //
809 // (b) If one of the classes is NO_CLASS, the resulting class is
810 // the other class.
811 //
812 // (c) If one of the classes is MEMORY, the result is the MEMORY
813 // class.
814 //
815 // (d) If one of the classes is INTEGER, the result is the
816 // INTEGER.
817 //
818 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
819 // MEMORY is used as class.
820 //
821 // (f) Otherwise class SSE is used.
822
823 // Accum should never be memory (we should have returned) or
824 // ComplexX87 (because this cannot be passed in a structure).
825 assert((Accum != Memory && Accum != ComplexX87) &&
826 "Invalid accumulated classification during merge.");
827 if (Accum == Field || Field == NoClass)
828 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000829 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000830 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000831 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000832 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000833 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000834 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000835 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
836 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000837 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000838 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000839}
840
Chris Lattner5c740f12010-06-30 19:14:05 +0000841void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000842 Class &Lo, Class &Hi) const {
843 // FIXME: This code can be simplified by introducing a simple value class for
844 // Class pairs with appropriate constructor methods for the various
845 // situations.
846
847 // FIXME: Some of the split computations are wrong; unaligned vectors
848 // shouldn't be passed in registers for example, so there is no chance they
849 // can straddle an eightbyte. Verify & simplify.
850
851 Lo = Hi = NoClass;
852
853 Class &Current = OffsetBase < 64 ? Lo : Hi;
854 Current = Memory;
855
John McCall9dd450b2009-09-21 23:43:11 +0000856 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000857 BuiltinType::Kind k = BT->getKind();
858
859 if (k == BuiltinType::Void) {
860 Current = NoClass;
861 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
862 Lo = Integer;
863 Hi = Integer;
864 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
865 Current = Integer;
866 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
867 Current = SSE;
868 } else if (k == BuiltinType::LongDouble) {
869 Lo = X87;
870 Hi = X87Up;
871 }
872 // FIXME: _Decimal32 and _Decimal64 are SSE.
873 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000874 return;
875 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000876
Chris Lattnerd776fb12010-06-28 21:43:59 +0000877 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000878 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000879 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000880 return;
881 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000882
Chris Lattnerd776fb12010-06-28 21:43:59 +0000883 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000884 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000885 return;
886 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000887
Chris Lattnerd776fb12010-06-28 21:43:59 +0000888 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000889 if (Ty->isMemberFunctionPointerType())
890 Lo = Hi = Integer;
891 else
892 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000893 return;
894 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000895
Chris Lattnerd776fb12010-06-28 21:43:59 +0000896 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000897 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000898 if (Size == 32) {
899 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
900 // float> as integer.
901 Current = Integer;
902
903 // If this type crosses an eightbyte boundary, it should be
904 // split.
905 uint64_t EB_Real = (OffsetBase) / 64;
906 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
907 if (EB_Real != EB_Imag)
908 Hi = Lo;
909 } else if (Size == 64) {
910 // gcc passes <1 x double> in memory. :(
911 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
912 return;
913
914 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +0000915 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +0000916 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
917 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
918 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000919 Current = Integer;
920 else
921 Current = SSE;
922
923 // If this type crosses an eightbyte boundary, it should be
924 // split.
925 if (OffsetBase && OffsetBase != 64)
926 Hi = Lo;
927 } else if (Size == 128) {
928 Lo = SSE;
929 Hi = SSEUp;
930 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000931 return;
932 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000933
Chris Lattnerd776fb12010-06-28 21:43:59 +0000934 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000935 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000936
Chris Lattner2b037972010-07-29 02:01:43 +0000937 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000938 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000939 if (Size <= 64)
940 Current = Integer;
941 else if (Size <= 128)
942 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +0000943 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000944 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000945 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000946 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +0000947 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000948 Current = ComplexX87;
949
950 // If this complex type crosses an eightbyte boundary then it
951 // should be split.
952 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +0000953 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000954 if (Hi == NoClass && EB_Real != EB_Imag)
955 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000956
Chris Lattnerd776fb12010-06-28 21:43:59 +0000957 return;
958 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000959
Chris Lattner2b037972010-07-29 02:01:43 +0000960 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000961 // Arrays are treated like structures.
962
Chris Lattner2b037972010-07-29 02:01:43 +0000963 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000964
965 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
966 // than two eightbytes, ..., it has class MEMORY.
967 if (Size > 128)
968 return;
969
970 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
971 // fields, it has class MEMORY.
972 //
973 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +0000974 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000975 return;
976
977 // Otherwise implement simplified merge. We could be smarter about
978 // this, but it isn't worth it and would be harder to verify.
979 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +0000980 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000981 uint64_t ArraySize = AT->getSize().getZExtValue();
982 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
983 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000984 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000985 Lo = merge(Lo, FieldLo);
986 Hi = merge(Hi, FieldHi);
987 if (Lo == Memory || Hi == Memory)
988 break;
989 }
990
991 // Do post merger cleanup (see below). Only case we worry about is Memory.
992 if (Hi == Memory)
993 Lo = Memory;
994 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000995 return;
996 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000997
Chris Lattnerd776fb12010-06-28 21:43:59 +0000998 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000999 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001000
1001 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1002 // than two eightbytes, ..., it has class MEMORY.
1003 if (Size > 128)
1004 return;
1005
Anders Carlsson20759ad2009-09-16 15:53:40 +00001006 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1007 // copy constructor or a non-trivial destructor, it is passed by invisible
1008 // reference.
1009 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1010 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001011
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001012 const RecordDecl *RD = RT->getDecl();
1013
1014 // Assume variable sized types are passed in memory.
1015 if (RD->hasFlexibleArrayMember())
1016 return;
1017
Chris Lattner2b037972010-07-29 02:01:43 +00001018 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001019
1020 // Reset Lo class, this will be recomputed.
1021 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001022
1023 // If this is a C++ record, classify the bases first.
1024 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1025 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1026 e = CXXRD->bases_end(); i != e; ++i) {
1027 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1028 "Unexpected base class!");
1029 const CXXRecordDecl *Base =
1030 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1031
1032 // Classify this field.
1033 //
1034 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1035 // single eightbyte, each is classified separately. Each eightbyte gets
1036 // initialized to class NO_CLASS.
1037 Class FieldLo, FieldHi;
1038 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001039 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001040 Lo = merge(Lo, FieldLo);
1041 Hi = merge(Hi, FieldHi);
1042 if (Lo == Memory || Hi == Memory)
1043 break;
1044 }
1045 }
1046
1047 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001048 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001049 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1050 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001051 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1052 bool BitField = i->isBitField();
1053
1054 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1055 // fields, it has class MEMORY.
1056 //
1057 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001058 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001059 Lo = Memory;
1060 return;
1061 }
1062
1063 // Classify this field.
1064 //
1065 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1066 // exceeds a single eightbyte, each is classified
1067 // separately. Each eightbyte gets initialized to class
1068 // NO_CLASS.
1069 Class FieldLo, FieldHi;
1070
1071 // Bit-fields require special handling, they do not force the
1072 // structure to be passed in memory even if unaligned, and
1073 // therefore they can straddle an eightbyte.
1074 if (BitField) {
1075 // Ignore padding bit-fields.
1076 if (i->isUnnamedBitfield())
1077 continue;
1078
1079 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001080 uint64_t Size =
1081 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001082
1083 uint64_t EB_Lo = Offset / 64;
1084 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1085 FieldLo = FieldHi = NoClass;
1086 if (EB_Lo) {
1087 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1088 FieldLo = NoClass;
1089 FieldHi = Integer;
1090 } else {
1091 FieldLo = Integer;
1092 FieldHi = EB_Hi ? Integer : NoClass;
1093 }
1094 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001095 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001096 Lo = merge(Lo, FieldLo);
1097 Hi = merge(Hi, FieldHi);
1098 if (Lo == Memory || Hi == Memory)
1099 break;
1100 }
1101
1102 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1103 //
1104 // (a) If one of the classes is MEMORY, the whole argument is
1105 // passed in memory.
1106 //
1107 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1108
1109 // The first of these conditions is guaranteed by how we implement
1110 // the merge (just bail).
1111 //
1112 // The second condition occurs in the case of unions; for example
1113 // union { _Complex double; unsigned; }.
1114 if (Hi == Memory)
1115 Lo = Memory;
1116 if (Hi == SSEUp && Lo != SSE)
1117 Hi = SSE;
1118 }
1119}
1120
Chris Lattner22a931e2010-06-29 06:01:59 +00001121ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001122 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1123 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001124 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001125 // Treat an enum type as its underlying type.
1126 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1127 Ty = EnumTy->getDecl()->getIntegerType();
1128
1129 return (Ty->isPromotableIntegerType() ?
1130 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1131 }
1132
1133 return ABIArgInfo::getIndirect(0);
1134}
1135
Chris Lattner22a931e2010-06-29 06:01:59 +00001136ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001137 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1138 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001139 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001140 // Treat an enum type as its underlying type.
1141 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1142 Ty = EnumTy->getDecl()->getIntegerType();
1143
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001144 return (Ty->isPromotableIntegerType() ?
1145 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001146 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001147
Daniel Dunbar53fac692010-04-21 19:49:55 +00001148 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1149 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001150
Daniel Dunbar53fac692010-04-21 19:49:55 +00001151 // Compute the byval alignment. We trust the back-end to honor the
1152 // minimum ABI alignment for byval, to make cleaner IR.
1153 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001154 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001155 if (Align > MinABIAlign)
1156 return ABIArgInfo::getIndirect(Align);
1157 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001158}
1159
Chris Lattner4200fe42010-07-29 04:56:46 +00001160/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1161/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1162/// vector register.
1163const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001164 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001165
Chris Lattner9fa15c32010-07-29 05:02:29 +00001166 // Wrapper structs that just contain vectors are passed just like vectors,
1167 // strip them off if present.
1168 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1169 while (STy && STy->getNumElements() == 1) {
1170 IRType = STy->getElementType(0);
1171 STy = dyn_cast<llvm::StructType>(IRType);
1172 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001173
Chris Lattner4200fe42010-07-29 04:56:46 +00001174 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001175 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001176 const llvm::Type *EltTy = VT->getElementType();
1177 if (VT->getBitWidth() == 128 &&
1178 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1179 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1180 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1181 EltTy->isIntegerTy(128)))
1182 return VT;
1183 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001184
Chris Lattner4200fe42010-07-29 04:56:46 +00001185 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1186}
1187
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001188/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1189/// is known to either be off the end of the specified type or being in
1190/// alignment padding. The user type specified is known to be at most 128 bits
1191/// in size, and have passed through X86_64ABIInfo::classify with a successful
1192/// classification that put one of the two halves in the INTEGER class.
1193///
1194/// It is conservatively correct to return false.
1195static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1196 unsigned EndBit, ASTContext &Context) {
1197 // If the bytes being queried are off the end of the type, there is no user
1198 // data hiding here. This handles analysis of builtins, vectors and other
1199 // types that don't contain interesting padding.
1200 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1201 if (TySize <= StartBit)
1202 return true;
1203
Chris Lattner98076a22010-07-29 07:43:55 +00001204 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1205 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1206 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1207
1208 // Check each element to see if the element overlaps with the queried range.
1209 for (unsigned i = 0; i != NumElts; ++i) {
1210 // If the element is after the span we care about, then we're done..
1211 unsigned EltOffset = i*EltSize;
1212 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001213
Chris Lattner98076a22010-07-29 07:43:55 +00001214 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1215 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1216 EndBit-EltOffset, Context))
1217 return false;
1218 }
1219 // If it overlaps no elements, then it is safe to process as padding.
1220 return true;
1221 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001222
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001223 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1224 const RecordDecl *RD = RT->getDecl();
1225 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001226
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001227 // If this is a C++ record, check the bases first.
1228 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1229 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1230 e = CXXRD->bases_end(); i != e; ++i) {
1231 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1232 "Unexpected base class!");
1233 const CXXRecordDecl *Base =
1234 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001235
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001236 // If the base is after the span we care about, ignore it.
1237 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1238 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001239
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001240 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1241 if (!BitsContainNoUserData(i->getType(), BaseStart,
1242 EndBit-BaseOffset, Context))
1243 return false;
1244 }
1245 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001246
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001247 // Verify that no field has data that overlaps the region of interest. Yes
1248 // this could be sped up a lot by being smarter about queried fields,
1249 // however we're only looking at structs up to 16 bytes, so we don't care
1250 // much.
1251 unsigned idx = 0;
1252 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1253 i != e; ++i, ++idx) {
1254 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001255
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001256 // If we found a field after the region we care about, then we're done.
1257 if (FieldOffset >= EndBit) break;
1258
1259 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1260 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1261 Context))
1262 return false;
1263 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001264
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001265 // If nothing in this record overlapped the area of interest, then we're
1266 // clean.
1267 return true;
1268 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001269
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001270 return false;
1271}
1272
Chris Lattnere556a712010-07-29 18:39:32 +00001273/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1274/// float member at the specified offset. For example, {int,{float}} has a
1275/// float at offset 4. It is conservatively correct for this routine to return
1276/// false.
1277static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1278 const llvm::TargetData &TD) {
1279 // Base case if we find a float.
1280 if (IROffset == 0 && IRType->isFloatTy())
1281 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001282
Chris Lattnere556a712010-07-29 18:39:32 +00001283 // If this is a struct, recurse into the field at the specified offset.
1284 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1285 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1286 unsigned Elt = SL->getElementContainingOffset(IROffset);
1287 IROffset -= SL->getElementOffset(Elt);
1288 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1289 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001290
Chris Lattnere556a712010-07-29 18:39:32 +00001291 // If this is an array, recurse into the field at the specified offset.
1292 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1293 const llvm::Type *EltTy = ATy->getElementType();
1294 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1295 IROffset -= IROffset/EltSize*EltSize;
1296 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1297 }
1298
1299 return false;
1300}
1301
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001302
1303/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1304/// low 8 bytes of an XMM register, corresponding to the SSE class.
1305const llvm::Type *X86_64ABIInfo::
1306GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1307 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001308 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001309 // pass as float if the last 4 bytes is just padding. This happens for
1310 // structs that contain 3 floats.
1311 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1312 SourceOffset*8+64, getContext()))
1313 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001314
Chris Lattnere556a712010-07-29 18:39:32 +00001315 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1316 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1317 // case.
1318 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001319 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1320 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001321
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001322 return llvm::Type::getDoubleTy(getVMContext());
1323}
1324
1325
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001326/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1327/// an 8-byte GPR. This means that we either have a scalar or we are talking
1328/// about the high or low part of an up-to-16-byte struct. This routine picks
1329/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001330/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1331/// etc).
1332///
1333/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1334/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1335/// the 8-byte value references. PrefType may be null.
1336///
1337/// SourceTy is the source level type for the entire argument. SourceOffset is
1338/// an offset into this that we're processing (which is always either 0 or 8).
1339///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001340const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001341GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1342 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001343 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1344 // returning an 8-byte unit starting with it. See if we can safely use it.
1345 if (IROffset == 0) {
1346 // Pointers and int64's always fill the 8-byte unit.
1347 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1348 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001349
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001350 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1351 // goodness in the source type is just tail padding. This is allowed to
1352 // kick in for struct {double,int} on the int, but not on
1353 // struct{double,int,int} because we wouldn't return the second int. We
1354 // have to do this analysis on the source type because we can't depend on
1355 // unions being lowered a specific way etc.
1356 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1357 IRType->isIntegerTy(32)) {
1358 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001359
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001360 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1361 SourceOffset*8+64, getContext()))
1362 return IRType;
1363 }
1364 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001365
Chris Lattnerce1bd752010-07-29 04:51:12 +00001366 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001367 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001368 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001369 if (IROffset < SL->getSizeInBytes()) {
1370 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1371 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001372
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001373 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1374 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001375 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001376 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001377
Chris Lattner98076a22010-07-29 07:43:55 +00001378 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1379 const llvm::Type *EltTy = ATy->getElementType();
1380 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1381 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001382 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1383 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001384 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001385
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001386 // Okay, we don't have any better idea of what to pass, so we pass this in an
1387 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001388 unsigned TySizeInBytes =
1389 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001390
Chris Lattner3f763422010-07-29 17:34:39 +00001391 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001392
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001393 // It is always safe to classify this as an integer type up to i64 that
1394 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001395 return llvm::IntegerType::get(getVMContext(),
1396 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001397}
1398
Chris Lattner31faff52010-07-28 23:06:14 +00001399ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001400classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001401 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1402 // classification algorithm.
1403 X86_64ABIInfo::Class Lo, Hi;
1404 classify(RetTy, 0, Lo, Hi);
1405
1406 // Check some invariants.
1407 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001408 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1409
1410 const llvm::Type *ResType = 0;
1411 switch (Lo) {
1412 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001413 if (Hi == NoClass)
1414 return ABIArgInfo::getIgnore();
1415 // If the low part is just padding, it takes no register, leave ResType
1416 // null.
1417 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1418 "Unknown missing lo part");
1419 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001420
1421 case SSEUp:
1422 case X87Up:
1423 assert(0 && "Invalid classification for lo word.");
1424
1425 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1426 // hidden argument.
1427 case Memory:
1428 return getIndirectReturnResult(RetTy);
1429
1430 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1431 // available register of the sequence %rax, %rdx is used.
1432 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001433 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1434 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001435
Chris Lattner1f3a0632010-07-29 21:42:50 +00001436 // If we have a sign or zero extended integer, make sure to return Extend
1437 // so that the parameter gets the right LLVM IR attributes.
1438 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1439 // Treat an enum type as its underlying type.
1440 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1441 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001442
Chris Lattner1f3a0632010-07-29 21:42:50 +00001443 if (RetTy->isIntegralOrEnumerationType() &&
1444 RetTy->isPromotableIntegerType())
1445 return ABIArgInfo::getExtend();
1446 }
Chris Lattner31faff52010-07-28 23:06:14 +00001447 break;
1448
1449 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1450 // available SSE register of the sequence %xmm0, %xmm1 is used.
1451 case SSE:
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001452 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001453 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001454
1455 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1456 // returned on the X87 stack in %st0 as 80-bit x87 number.
1457 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001458 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001459 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001460
1461 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1462 // part of the value is returned in %st0 and the imaginary part in
1463 // %st1.
1464 case ComplexX87:
1465 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001466 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001467 llvm::Type::getX86_FP80Ty(getVMContext()),
1468 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001469 NULL);
1470 break;
1471 }
1472
1473 switch (Hi) {
1474 // Memory was handled previously and X87 should
1475 // never occur as a hi class.
1476 case Memory:
1477 case X87:
1478 assert(0 && "Invalid classification for hi word.");
1479
1480 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001481 case NoClass:
1482 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001483
1484 case Integer: {
Chris Lattnerce1bd752010-07-29 04:51:12 +00001485 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001486 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001487 if (Lo == NoClass) // Return HiType at offset 8 in memory.
1488 return ABIArgInfo::getDirect(HiType, 8);
1489
Chris Lattner458b2aa2010-07-29 02:16:43 +00001490 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001491 break;
1492 }
Chris Lattnerc95a3982010-07-29 17:49:08 +00001493 case SSE: {
1494 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001495 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001496 if (Lo == NoClass) // Return HiType at offset 8 in memory.
1497 return ABIArgInfo::getDirect(HiType, 8);
1498
Chris Lattnerc95a3982010-07-29 17:49:08 +00001499 ResType = llvm::StructType::get(getVMContext(), ResType, HiType,NULL);
Chris Lattner31faff52010-07-28 23:06:14 +00001500 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001501 }
Chris Lattner31faff52010-07-28 23:06:14 +00001502
1503 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1504 // is passed in the upper half of the last used SSE register.
1505 //
1506 // SSEUP should always be preceeded by SSE, just widen.
1507 case SSEUp:
1508 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001509 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001510 break;
1511
1512 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1513 // returned together with the previous X87 value in %st0.
1514 case X87Up:
1515 // If X87Up is preceeded by X87, we don't need to do
1516 // anything. However, in some cases with unions it may not be
1517 // preceeded by X87. In such situations we follow gcc and pass the
1518 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001519 if (Lo != X87) {
1520 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001521 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001522 if (Lo == NoClass) // Return HiType at offset 8 in memory.
1523 return ABIArgInfo::getDirect(HiType, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001524
Chris Lattnerc95a3982010-07-29 17:49:08 +00001525 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
1526 }
Chris Lattner31faff52010-07-28 23:06:14 +00001527 break;
1528 }
1529
Chris Lattner1f3a0632010-07-29 21:42:50 +00001530 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001531}
1532
Chris Lattner458b2aa2010-07-29 02:16:43 +00001533ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001534 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001535 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001536 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001537
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001538 // Check some invariants.
1539 // FIXME: Enforce these by construction.
1540 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001541 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1542
1543 neededInt = 0;
1544 neededSSE = 0;
1545 const llvm::Type *ResType = 0;
1546 switch (Lo) {
1547 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001548 if (Hi == NoClass)
1549 return ABIArgInfo::getIgnore();
1550 // If the low part is just padding, it takes no register, leave ResType
1551 // null.
1552 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1553 "Unknown missing lo part");
1554 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001555
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001556 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1557 // on the stack.
1558 case Memory:
1559
1560 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1561 // COMPLEX_X87, it is passed in memory.
1562 case X87:
1563 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001564 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001565
1566 case SSEUp:
1567 case X87Up:
1568 assert(0 && "Invalid classification for lo word.");
1569
1570 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1571 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1572 // and %r9 is used.
1573 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001574 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001575
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001576 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001577 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001578
1579 // If we have a sign or zero extended integer, make sure to return Extend
1580 // so that the parameter gets the right LLVM IR attributes.
1581 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1582 // Treat an enum type as its underlying type.
1583 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1584 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001585
Chris Lattner1f3a0632010-07-29 21:42:50 +00001586 if (Ty->isIntegralOrEnumerationType() &&
1587 Ty->isPromotableIntegerType())
1588 return ABIArgInfo::getExtend();
1589 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001590
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001591 break;
1592
1593 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1594 // available SSE register is used, the registers are taken in the
1595 // order from %xmm0 to %xmm7.
1596 case SSE:
1597 ++neededSSE;
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001598 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001599 break;
1600 }
1601
1602 switch (Hi) {
1603 // Memory was handled previously, ComplexX87 and X87 should
1604 // never occur as hi classes, and X87Up must be preceed by X87,
1605 // which is passed in memory.
1606 case Memory:
1607 case X87:
1608 case ComplexX87:
1609 assert(0 && "Invalid classification for hi word.");
1610 break;
1611
1612 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001613
Chris Lattner22a931e2010-06-29 06:01:59 +00001614 case Integer: {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001615 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001616 // Pick an 8-byte type based on the preferred type.
Chris Lattnerce1bd752010-07-29 04:51:12 +00001617 const llvm::Type *HiType =
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001618 GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001619
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001620 if (Lo == NoClass) // Pass HiType at offset 8 in memory.
1621 return ABIArgInfo::getDirect(HiType, 8);
1622
Chris Lattner458b2aa2010-07-29 02:16:43 +00001623 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001624 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001625 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001626
1627 // X87Up generally doesn't occur here (long double is passed in
1628 // memory), except in situations involving unions.
1629 case X87Up:
Chris Lattnerc95a3982010-07-29 17:49:08 +00001630 case SSE: {
1631 const llvm::Type *HiType =
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001632 GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001633
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001634 if (Lo == NoClass) // Pass HiType at offset 8 in memory.
1635 return ABIArgInfo::getDirect(HiType, 8);
1636
Chris Lattnerc95a3982010-07-29 17:49:08 +00001637 ResType = llvm::StructType::get(getVMContext(), ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001638 ++neededSSE;
1639 break;
Chris Lattnerc95a3982010-07-29 17:49:08 +00001640 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001641
1642 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1643 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001644 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001645 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001646 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001647 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001648 break;
1649 }
1650
Chris Lattner1f3a0632010-07-29 21:42:50 +00001651 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001652}
1653
Chris Lattner22326a12010-07-29 02:31:05 +00001654void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001655
Chris Lattner458b2aa2010-07-29 02:16:43 +00001656 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001657
1658 // Keep track of the number of assigned registers.
1659 unsigned freeIntRegs = 6, freeSSERegs = 8;
1660
1661 // If the return value is indirect, then the hidden argument is consuming one
1662 // integer register.
1663 if (FI.getReturnInfo().isIndirect())
1664 --freeIntRegs;
1665
1666 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1667 // get assigned (in left-to-right order) for passing as follows...
1668 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1669 it != ie; ++it) {
1670 unsigned neededInt, neededSSE;
Chris Lattner029c0f12010-07-29 04:41:05 +00001671 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001672
1673 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1674 // eightbyte of an argument, the whole argument is passed on the
1675 // stack. If registers have already been assigned for some
1676 // eightbytes of such an argument, the assignments get reverted.
1677 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1678 freeIntRegs -= neededInt;
1679 freeSSERegs -= neededSSE;
1680 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001681 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001682 }
1683 }
1684}
1685
1686static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1687 QualType Ty,
1688 CodeGenFunction &CGF) {
1689 llvm::Value *overflow_arg_area_p =
1690 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1691 llvm::Value *overflow_arg_area =
1692 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1693
1694 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1695 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1696 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1697 if (Align > 8) {
1698 // Note that we follow the ABI & gcc here, even though the type
1699 // could in theory have an alignment greater than 16. This case
1700 // shouldn't ever matter in practice.
1701
1702 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001703 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001704 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001705 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1706 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001707 CGF.Int64Ty);
1708 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001709 overflow_arg_area =
1710 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1711 overflow_arg_area->getType(),
1712 "overflow_arg_area.align");
1713 }
1714
1715 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1716 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1717 llvm::Value *Res =
1718 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001719 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001720
1721 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1722 // l->overflow_arg_area + sizeof(type).
1723 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1724 // an 8 byte boundary.
1725
1726 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001727 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001728 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001729 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1730 "overflow_arg_area.next");
1731 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1732
1733 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1734 return Res;
1735}
1736
1737llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1738 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001739 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001740
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001741 // Assume that va_list type is correct; should be pointer to LLVM type:
1742 // struct {
1743 // i32 gp_offset;
1744 // i32 fp_offset;
1745 // i8* overflow_arg_area;
1746 // i8* reg_save_area;
1747 // };
1748 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001749
Chris Lattner9723d6c2010-03-11 18:19:55 +00001750 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001751 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001752
1753 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1754 // in the registers. If not go to step 7.
1755 if (!neededInt && !neededSSE)
1756 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1757
1758 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1759 // general purpose registers needed to pass type and num_fp to hold
1760 // the number of floating point registers needed.
1761
1762 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1763 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1764 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1765 //
1766 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1767 // register save space).
1768
1769 llvm::Value *InRegs = 0;
1770 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1771 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1772 if (neededInt) {
1773 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1774 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001775 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1776 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001777 }
1778
1779 if (neededSSE) {
1780 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1781 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1782 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001783 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1784 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001785 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1786 }
1787
1788 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1789 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1790 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1791 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1792
1793 // Emit code to load the value if it was passed in registers.
1794
1795 CGF.EmitBlock(InRegBlock);
1796
1797 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1798 // an offset of l->gp_offset and/or l->fp_offset. This may require
1799 // copying to a temporary location in case the parameter is passed
1800 // in different register classes or requires an alignment greater
1801 // than 8 for general purpose registers and 16 for XMM registers.
1802 //
1803 // FIXME: This really results in shameful code when we end up needing to
1804 // collect arguments from different places; often what should result in a
1805 // simple assembling of a structure from scattered addresses has many more
1806 // loads than necessary. Can we clean this up?
1807 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1808 llvm::Value *RegAddr =
1809 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1810 "reg_save_area");
1811 if (neededInt && neededSSE) {
1812 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001813 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001814 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1815 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1816 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1817 const llvm::Type *TyLo = ST->getElementType(0);
1818 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00001819 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001820 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001821 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1822 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001823 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1824 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001825 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1826 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001827 llvm::Value *V =
1828 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1829 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1830 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1831 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1832
Owen Anderson170229f2009-07-14 23:10:40 +00001833 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001834 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001835 } else if (neededInt) {
1836 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1837 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001838 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001839 } else if (neededSSE == 1) {
1840 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1841 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1842 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001843 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001844 assert(neededSSE == 2 && "Invalid number of needed registers!");
1845 // SSE registers are spaced 16 bytes apart in the register save
1846 // area, we need to collect the two eightbytes together.
1847 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001848 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001849 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1850 const llvm::Type *DblPtrTy =
1851 llvm::PointerType::getUnqual(DoubleTy);
1852 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1853 DoubleTy, NULL);
1854 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1855 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1856 DblPtrTy));
1857 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1858 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1859 DblPtrTy));
1860 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1861 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1862 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001863 }
1864
1865 // AMD64-ABI 3.5.7p5: Step 5. Set:
1866 // l->gp_offset = l->gp_offset + num_gp * 8
1867 // l->fp_offset = l->fp_offset + num_fp * 16.
1868 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001869 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001870 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1871 gp_offset_p);
1872 }
1873 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001874 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001875 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1876 fp_offset_p);
1877 }
1878 CGF.EmitBranch(ContBlock);
1879
1880 // Emit code to load the value if it was passed in memory.
1881
1882 CGF.EmitBlock(InMemBlock);
1883 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1884
1885 // Return the appropriate result.
1886
1887 CGF.EmitBlock(ContBlock);
1888 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1889 "vaarg.addr");
1890 ResAddr->reserveOperandSpace(2);
1891 ResAddr->addIncoming(RegAddr, InRegBlock);
1892 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001893 return ResAddr;
1894}
1895
Chris Lattner0cf24192010-06-28 20:05:43 +00001896
1897
1898//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001899// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001900//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001901
1902namespace {
1903
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001904class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00001905public:
1906 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001907
Chris Lattner458b2aa2010-07-29 02:16:43 +00001908 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001909
Chris Lattner458b2aa2010-07-29 02:16:43 +00001910 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001911
Chris Lattner22326a12010-07-29 02:31:05 +00001912 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00001913 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001914 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1915 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00001916 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001917 }
1918
1919 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1920 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001921};
1922
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001923class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1924public:
Chris Lattner2b037972010-07-29 02:01:43 +00001925 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
1926 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001927};
1928
Daniel Dunbard59655c2009-09-12 00:59:49 +00001929}
1930
Chris Lattner458b2aa2010-07-29 02:16:43 +00001931ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001932 if (RetTy->isVoidType()) {
1933 return ABIArgInfo::getIgnore();
1934 } else {
1935 return ABIArgInfo::getDirect();
1936 }
1937}
1938
Chris Lattner458b2aa2010-07-29 02:16:43 +00001939ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001940 return ABIArgInfo::getDirect();
1941}
1942
1943llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001944 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001945 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001946 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1947
1948 CGBuilderTy &Builder = CGF.Builder;
1949 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1950 "ap");
1951 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1952 llvm::Type *PTy =
1953 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1954 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1955
1956 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1957
1958 llvm::Value *NextAddr =
1959 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1960 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1961 "ap.next");
1962 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1963
1964 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001965}
1966
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001967
John McCallea8d8bb2010-03-11 00:10:12 +00001968// PowerPC-32
1969
1970namespace {
1971class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1972public:
Chris Lattner2b037972010-07-29 02:01:43 +00001973 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001974
John McCallea8d8bb2010-03-11 00:10:12 +00001975 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1976 // This is recovered from gcc output.
1977 return 1; // r1 is the dedicated stack pointer
1978 }
1979
1980 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001981 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00001982};
1983
1984}
1985
1986bool
1987PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1988 llvm::Value *Address) const {
1989 // This is calculated from the LLVM and GCC tables and verified
1990 // against gcc output. AFAIK all ABIs use the same encoding.
1991
1992 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1993 llvm::LLVMContext &Context = CGF.getLLVMContext();
1994
1995 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1996 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1997 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1998 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1999
2000 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002001 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002002
2003 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002004 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002005
2006 // 64-76 are various 4-byte special-purpose registers:
2007 // 64: mq
2008 // 65: lr
2009 // 66: ctr
2010 // 67: ap
2011 // 68-75 cr0-7
2012 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002013 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002014
2015 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002016 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002017
2018 // 109: vrsave
2019 // 110: vscr
2020 // 111: spe_acc
2021 // 112: spefscr
2022 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002023 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002024
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002025 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002026}
2027
2028
Chris Lattner0cf24192010-06-28 20:05:43 +00002029//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002030// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002031//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002032
2033namespace {
2034
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002035class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002036public:
2037 enum ABIKind {
2038 APCS = 0,
2039 AAPCS = 1,
2040 AAPCS_VFP
2041 };
2042
2043private:
2044 ABIKind Kind;
2045
2046public:
Chris Lattner2b037972010-07-29 02:01:43 +00002047 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002048
2049private:
2050 ABIKind getABIKind() const { return Kind; }
2051
Chris Lattner458b2aa2010-07-29 02:16:43 +00002052 ABIArgInfo classifyReturnType(QualType RetTy) const;
2053 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002054
Chris Lattner22326a12010-07-29 02:31:05 +00002055 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002056
2057 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2058 CodeGenFunction &CGF) const;
2059};
2060
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002061class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2062public:
Chris Lattner2b037972010-07-29 02:01:43 +00002063 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2064 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002065
2066 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2067 return 13;
2068 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002069};
2070
Daniel Dunbard59655c2009-09-12 00:59:49 +00002071}
2072
Chris Lattner22326a12010-07-29 02:31:05 +00002073void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002074 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002075 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002076 it != ie; ++it)
2077 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002078
Chris Lattner458b2aa2010-07-29 02:16:43 +00002079 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002080 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002081 if (Triple.getEnvironmentName() == "gnueabi" ||
2082 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002083 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002084 else
2085 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002086
Daniel Dunbar020daa92009-09-12 01:00:39 +00002087 switch (getABIKind()) {
2088 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002089 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2090 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002091 break;
2092
2093 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002094 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2095 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002096 break;
2097
2098 case AAPCS_VFP:
2099 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2100 break;
2101 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002102}
2103
Chris Lattner458b2aa2010-07-29 02:16:43 +00002104ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002105 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002106 // Treat an enum type as its underlying type.
2107 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2108 Ty = EnumTy->getDecl()->getIntegerType();
2109
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002110 return (Ty->isPromotableIntegerType() ?
2111 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002112 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002113
Daniel Dunbar09d33622009-09-14 21:54:03 +00002114 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002115 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002116 return ABIArgInfo::getIgnore();
2117
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002118 // Structures with either a non-trivial destructor or a non-trivial
2119 // copy constructor are always indirect.
2120 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2121 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2122
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002123 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2124 // backend doesn't support byval.
2125 // FIXME: This doesn't handle alignment > 64 bits.
2126 const llvm::Type* ElemTy;
2127 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002128 if (getContext().getTypeAlign(Ty) > 32) {
2129 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2130 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002131 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002132 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2133 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002134 }
2135 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002136 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002137 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2138 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002139 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002140}
2141
Chris Lattner458b2aa2010-07-29 02:16:43 +00002142static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002143 llvm::LLVMContext &VMContext) {
2144 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2145 // is called integer-like if its size is less than or equal to one word, and
2146 // the offset of each of its addressable sub-fields is zero.
2147
2148 uint64_t Size = Context.getTypeSize(Ty);
2149
2150 // Check that the type fits in a word.
2151 if (Size > 32)
2152 return false;
2153
2154 // FIXME: Handle vector types!
2155 if (Ty->isVectorType())
2156 return false;
2157
Daniel Dunbard53bac72009-09-14 02:20:34 +00002158 // Float types are never treated as "integer like".
2159 if (Ty->isRealFloatingType())
2160 return false;
2161
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002162 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002163 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002164 return true;
2165
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002166 // Small complex integer types are "integer like".
2167 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2168 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002169
2170 // Single element and zero sized arrays should be allowed, by the definition
2171 // above, but they are not.
2172
2173 // Otherwise, it must be a record type.
2174 const RecordType *RT = Ty->getAs<RecordType>();
2175 if (!RT) return false;
2176
2177 // Ignore records with flexible arrays.
2178 const RecordDecl *RD = RT->getDecl();
2179 if (RD->hasFlexibleArrayMember())
2180 return false;
2181
2182 // Check that all sub-fields are at offset 0, and are themselves "integer
2183 // like".
2184 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2185
2186 bool HadField = false;
2187 unsigned idx = 0;
2188 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2189 i != e; ++i, ++idx) {
2190 const FieldDecl *FD = *i;
2191
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002192 // Bit-fields are not addressable, we only need to verify they are "integer
2193 // like". We still have to disallow a subsequent non-bitfield, for example:
2194 // struct { int : 0; int x }
2195 // is non-integer like according to gcc.
2196 if (FD->isBitField()) {
2197 if (!RD->isUnion())
2198 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002199
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002200 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2201 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002202
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002203 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002204 }
2205
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002206 // Check if this field is at offset 0.
2207 if (Layout.getFieldOffset(idx) != 0)
2208 return false;
2209
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002210 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2211 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002212
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002213 // Only allow at most one field in a structure. This doesn't match the
2214 // wording above, but follows gcc in situations with a field following an
2215 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002216 if (!RD->isUnion()) {
2217 if (HadField)
2218 return false;
2219
2220 HadField = true;
2221 }
2222 }
2223
2224 return true;
2225}
2226
Chris Lattner458b2aa2010-07-29 02:16:43 +00002227ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002228 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002229 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002230
John McCalla1dee5302010-08-22 10:59:02 +00002231 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002232 // Treat an enum type as its underlying type.
2233 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2234 RetTy = EnumTy->getDecl()->getIntegerType();
2235
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002236 return (RetTy->isPromotableIntegerType() ?
2237 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002238 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002239
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002240 // Structures with either a non-trivial destructor or a non-trivial
2241 // copy constructor are always indirect.
2242 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2243 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2244
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002245 // Are we following APCS?
2246 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002247 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002248 return ABIArgInfo::getIgnore();
2249
Daniel Dunbareedf1512010-02-01 23:31:19 +00002250 // Complex types are all returned as packed integers.
2251 //
2252 // FIXME: Consider using 2 x vector types if the back end handles them
2253 // correctly.
2254 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002255 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002256 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002257
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002258 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002259 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002260 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002261 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002262 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002263 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002264 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002265 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2266 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002267 }
2268
2269 // Otherwise return in memory.
2270 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002271 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002272
2273 // Otherwise this is an AAPCS variant.
2274
Chris Lattner458b2aa2010-07-29 02:16:43 +00002275 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002276 return ABIArgInfo::getIgnore();
2277
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002278 // Aggregates <= 4 bytes are returned in r0; other aggregates
2279 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002280 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002281 if (Size <= 32) {
2282 // Return in the smallest viable integer type.
2283 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002284 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002285 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002286 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2287 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002288 }
2289
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002290 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002291}
2292
2293llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002294 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002295 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002296 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002297 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002298
2299 CGBuilderTy &Builder = CGF.Builder;
2300 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2301 "ap");
2302 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2303 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002304 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002305 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2306
2307 uint64_t Offset =
2308 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2309 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002310 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002311 "ap.next");
2312 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2313
2314 return AddrTyped;
2315}
2316
Chris Lattner458b2aa2010-07-29 02:16:43 +00002317ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2318 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002319 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002320
John McCalla1dee5302010-08-22 10:59:02 +00002321 if (isAggregateTypeForABI(RetTy))
Chris Lattner458b2aa2010-07-29 02:16:43 +00002322 return ABIArgInfo::getIndirect(0);
2323
2324 // Treat an enum type as its underlying type.
2325 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2326 RetTy = EnumTy->getDecl()->getIntegerType();
2327
2328 return (RetTy->isPromotableIntegerType() ?
2329 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002330}
2331
Chris Lattner0cf24192010-06-28 20:05:43 +00002332//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002333// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002334//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002335
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002336namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002337
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002338class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002339public:
2340 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2341
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002342 bool isPromotableIntegerType(QualType Ty) const;
2343
Chris Lattner458b2aa2010-07-29 02:16:43 +00002344 ABIArgInfo classifyReturnType(QualType RetTy) const;
2345 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002346
Chris Lattner22326a12010-07-29 02:31:05 +00002347 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002348 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002349 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2350 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002351 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002352 }
2353
2354 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2355 CodeGenFunction &CGF) const;
2356};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002357
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002358class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2359public:
Chris Lattner2b037972010-07-29 02:01:43 +00002360 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2361 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002362};
2363
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002364}
2365
2366bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2367 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002368 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002369 switch (BT->getKind()) {
2370 case BuiltinType::Bool:
2371 case BuiltinType::Char_S:
2372 case BuiltinType::Char_U:
2373 case BuiltinType::SChar:
2374 case BuiltinType::UChar:
2375 case BuiltinType::Short:
2376 case BuiltinType::UShort:
2377 case BuiltinType::Int:
2378 case BuiltinType::UInt:
2379 return true;
2380 default:
2381 return false;
2382 }
2383 return false;
2384}
2385
2386llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2387 CodeGenFunction &CGF) const {
2388 // FIXME: Implement
2389 return 0;
2390}
2391
2392
Chris Lattner458b2aa2010-07-29 02:16:43 +00002393ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2394 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002395 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002396 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002397 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002398
2399 return (isPromotableIntegerType(RetTy) ?
2400 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002401}
2402
Chris Lattner458b2aa2010-07-29 02:16:43 +00002403ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002404 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002405 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002406
2407 return (isPromotableIntegerType(Ty) ?
2408 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002409}
2410
Chris Lattner0cf24192010-06-28 20:05:43 +00002411//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002412// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002413//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002414
2415namespace {
2416
2417class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2418public:
Chris Lattner2b037972010-07-29 02:01:43 +00002419 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2420 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002421 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2422 CodeGen::CodeGenModule &M) const;
2423};
2424
2425}
2426
2427void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2428 llvm::GlobalValue *GV,
2429 CodeGen::CodeGenModule &M) const {
2430 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2431 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2432 // Handle 'interrupt' attribute:
2433 llvm::Function *F = cast<llvm::Function>(GV);
2434
2435 // Step 1: Set ISR calling convention.
2436 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2437
2438 // Step 2: Add attributes goodness.
2439 F->addFnAttr(llvm::Attribute::NoInline);
2440
2441 // Step 3: Emit ISR vector alias.
2442 unsigned Num = attr->getNumber() + 0xffe0;
2443 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2444 "vector_" +
2445 llvm::LowercaseString(llvm::utohexstr(Num)),
2446 GV, &M.getModule());
2447 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002448 }
2449}
2450
Chris Lattner0cf24192010-06-28 20:05:43 +00002451//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002452// MIPS ABI Implementation. This works for both little-endian and
2453// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002454//===----------------------------------------------------------------------===//
2455
John McCall943fae92010-05-27 06:19:26 +00002456namespace {
2457class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2458public:
Chris Lattner2b037972010-07-29 02:01:43 +00002459 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2460 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002461
2462 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2463 return 29;
2464 }
2465
2466 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002467 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002468};
2469}
2470
2471bool
2472MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2473 llvm::Value *Address) const {
2474 // This information comes from gcc's implementation, which seems to
2475 // as canonical as it gets.
2476
2477 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2478 llvm::LLVMContext &Context = CGF.getLLVMContext();
2479
2480 // Everything on MIPS is 4 bytes. Double-precision FP registers
2481 // are aliased to pairs of single-precision FP registers.
2482 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2483 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2484
2485 // 0-31 are the general purpose registers, $0 - $31.
2486 // 32-63 are the floating-point registers, $f0 - $f31.
2487 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2488 // 66 is the (notional, I think) register for signal-handler return.
2489 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2490
2491 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2492 // They are one bit wide and ignored here.
2493
2494 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2495 // (coprocessor 1 is the FP unit)
2496 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2497 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2498 // 176-181 are the DSP accumulator registers.
2499 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2500
2501 return false;
2502}
2503
2504
Chris Lattner2b037972010-07-29 02:01:43 +00002505const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002506 if (TheTargetCodeGenInfo)
2507 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002508
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002509 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2510 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002511
Chris Lattner22a931e2010-06-29 06:01:59 +00002512 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002513 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002514 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002515 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002516
John McCall943fae92010-05-27 06:19:26 +00002517 case llvm::Triple::mips:
2518 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002519 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002520
Daniel Dunbard59655c2009-09-12 00:59:49 +00002521 case llvm::Triple::arm:
2522 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002523 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002524 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002525 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002526 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002527
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002528 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002529 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002530
2531 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002532 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002533
John McCallea8d8bb2010-03-11 00:10:12 +00002534 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002535 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002536
Daniel Dunbard59655c2009-09-12 00:59:49 +00002537 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002538 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002539
2540 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002541 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002542
Daniel Dunbar40165182009-08-24 09:10:05 +00002543 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002544 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002545 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002546 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002547 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002548 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002549 case llvm::Triple::MinGW32:
2550 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002551 case llvm::Triple::AuroraUX:
2552 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002553 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002554 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002555 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002556 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002557
2558 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002559 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002560 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002561 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002562
Daniel Dunbare3532f82009-08-24 08:52:16 +00002563 case llvm::Triple::x86_64:
Chris Lattner2b037972010-07-29 02:01:43 +00002564 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002565 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002566}