blob: 2ba19f10963d3038bb58ff1d5f8f8040b3cb3888 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
Anton Korobeynikov244360d2009-06-05 22:08:42 +000039ABIInfo::~ABIInfo() {}
40
41void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000042 llvm::raw_ostream &OS = llvm::errs();
43 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044 switch (TheKind) {
45 case Direct:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000046 OS << "Direct";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000047 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000048 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000049 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000050 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000051 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000052 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000053 break;
54 case Coerce:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000055 OS << "Coerce Type=";
56 getCoerceToType()->print(OS);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000057 break;
58 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000059 OS << "Indirect Align=" << getIndirectAlign()
60 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000061 break;
62 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000063 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064 break;
65 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000066 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000067}
68
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000069TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
70
Daniel Dunbar626f1d82009-09-13 08:03:58 +000071static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000072
73/// isEmptyField - Return true iff a the field is "empty", that is it
74/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000075static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
76 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000077 if (FD->isUnnamedBitfield())
78 return true;
79
80 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000081
Daniel Dunbar626f1d82009-09-13 08:03:58 +000082 // Constant arrays of empty records count as empty, strip them off.
83 if (AllowArrays)
84 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
85 FT = AT->getElementType();
86
Daniel Dunbarcd20ce12010-05-17 16:46:00 +000087 const RecordType *RT = FT->getAs<RecordType>();
88 if (!RT)
89 return false;
90
91 // C++ record fields are never empty, at least in the Itanium ABI.
92 //
93 // FIXME: We should use a predicate for whether this behavior is true in the
94 // current ABI.
95 if (isa<CXXRecordDecl>(RT->getDecl()))
96 return false;
97
Daniel Dunbar626f1d82009-09-13 08:03:58 +000098 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000099}
100
101/// isEmptyRecord - Return true iff a structure contains only empty
102/// fields. Note that a structure with a flexible array member is not
103/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000104static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000105 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000106 if (!RT)
107 return 0;
108 const RecordDecl *RD = RT->getDecl();
109 if (RD->hasFlexibleArrayMember())
110 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000111
112 // If this is a C++ record, check the bases first.
113 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
114 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
115 e = CXXRD->bases_end(); i != e; ++i)
116 if (!isEmptyRecord(Context, i->getType(), true))
117 return false;
118
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000119 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
120 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000121 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000122 return false;
123 return true;
124}
125
Anders Carlsson20759ad2009-09-16 15:53:40 +0000126/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
127/// a non-trivial destructor or a non-trivial copy constructor.
128static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
129 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
130 if (!RD)
131 return false;
132
133 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
134}
135
136/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
137/// a record type with either a non-trivial destructor or a non-trivial copy
138/// constructor.
139static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
140 const RecordType *RT = T->getAs<RecordType>();
141 if (!RT)
142 return false;
143
144 return hasNonTrivialDestructorOrCopyConstructor(RT);
145}
146
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000147/// isSingleElementStruct - Determine if a structure is a "single
148/// element struct", i.e. it has exactly one non-empty field or
149/// exactly one field which is itself a single element
150/// struct. Structures with flexible array members are never
151/// considered single element structs.
152///
153/// \return The field declaration for the single non-empty field, if
154/// it exists.
155static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
156 const RecordType *RT = T->getAsStructureType();
157 if (!RT)
158 return 0;
159
160 const RecordDecl *RD = RT->getDecl();
161 if (RD->hasFlexibleArrayMember())
162 return 0;
163
164 const Type *Found = 0;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000165
166 // If this is a C++ record, check the bases first.
167 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
168 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
169 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000170 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000171 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000172 continue;
173
174 // If we already found an element then this isn't a single-element struct.
175 if (Found)
176 return 0;
177
178 // If this is non-empty and not a single element struct, the composite
179 // cannot be a single element struct.
180 Found = isSingleElementStruct(i->getType(), Context);
181 if (!Found)
182 return 0;
183 }
184 }
185
186 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000187 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
188 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000189 const FieldDecl *FD = *i;
190 QualType FT = FD->getType();
191
192 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000193 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000194 continue;
195
196 // If we already found an element then this isn't a single-element
197 // struct.
198 if (Found)
199 return 0;
200
201 // Treat single element arrays as the element.
202 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
203 if (AT->getSize().getZExtValue() != 1)
204 break;
205 FT = AT->getElementType();
206 }
207
208 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
209 Found = FT.getTypePtr();
210 } else {
211 Found = isSingleElementStruct(FT, Context);
212 if (!Found)
213 return 0;
214 }
215 }
216
217 return Found;
218}
219
220static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000221 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000222 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
223 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000224 return false;
225
226 uint64_t Size = Context.getTypeSize(Ty);
227 return Size == 32 || Size == 64;
228}
229
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000230/// canExpandIndirectArgument - Test whether an argument type which is to be
231/// passed indirectly (on the stack) would have the equivalent layout if it was
232/// expanded into separate arguments. If so, we prefer to do the latter to avoid
233/// inhibiting optimizations.
234///
235// FIXME: This predicate is missing many cases, currently it just follows
236// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
237// should probably make this smarter, or better yet make the LLVM backend
238// capable of handling it.
239static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
240 // We can only expand structure types.
241 const RecordType *RT = Ty->getAs<RecordType>();
242 if (!RT)
243 return false;
244
245 // We can only expand (C) structures.
246 //
247 // FIXME: This needs to be generalized to handle classes as well.
248 const RecordDecl *RD = RT->getDecl();
249 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
250 return false;
251
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000252 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
253 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000254 const FieldDecl *FD = *i;
255
256 if (!is32Or64BitBasicType(FD->getType(), Context))
257 return false;
258
259 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
260 // how to expand them yet, and the predicate for telling if a bitfield still
261 // counts as "basic" is more complicated than what we were doing previously.
262 if (FD->isBitField())
263 return false;
264 }
265
266 return true;
267}
268
269namespace {
270/// DefaultABIInfo - The default implementation for ABI specific
271/// details. This implementation provides information which results in
272/// self-consistent and sensible LLVM IR generation, but does not
273/// conform to any particular ABI.
274class DefaultABIInfo : public ABIInfo {
275 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000276 ASTContext &Context,
277 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000278
279 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000280 ASTContext &Context,
281 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000282
Owen Anderson170229f2009-07-14 23:10:40 +0000283 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000284 llvm::LLVMContext &VMContext,
285 const llvm::Type *const *PrefTypes,
286 unsigned NumPrefTypes) const {
Owen Anderson170229f2009-07-14 23:10:40 +0000287 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
288 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000289 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
290 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000291 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000292 }
293
294 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
295 CodeGenFunction &CGF) const;
296};
297
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000298class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
299public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000300 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000301};
302
303llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
304 CodeGenFunction &CGF) const {
305 return 0;
306}
307
308ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
309 ASTContext &Context,
310 llvm::LLVMContext &VMContext) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000311 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000312 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000313
Chris Lattner9723d6c2010-03-11 18:19:55 +0000314 // Treat an enum type as its underlying type.
315 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
316 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000317
Chris Lattner9723d6c2010-03-11 18:19:55 +0000318 return (Ty->isPromotableIntegerType() ?
319 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000320}
321
Chris Lattner0cf24192010-06-28 20:05:43 +0000322//===----------------------------------------------------------------------===//
323// X86-32 ABI Implementation
324//===----------------------------------------------------------------------===//
325
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000326/// X86_32ABIInfo - The X86-32 ABI information.
327class X86_32ABIInfo : public ABIInfo {
328 ASTContext &Context;
David Chisnallde3a0692009-08-17 23:08:21 +0000329 bool IsDarwinVectorABI;
330 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000331
332 static bool isRegisterSize(unsigned Size) {
333 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
334 }
335
336 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
337
Daniel Dunbar557893d2010-04-21 19:10:51 +0000338 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
339 /// such that the argument will be passed in memory.
340 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
341 bool ByVal = true) const;
342
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000343public:
344 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000345 ASTContext &Context,
346 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000347
348 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000349 ASTContext &Context,
350 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000351
Owen Anderson170229f2009-07-14 23:10:40 +0000352 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000353 llvm::LLVMContext &VMContext,
354 const llvm::Type *const *PrefTypes,
355 unsigned NumPrefTypes) const {
Owen Anderson170229f2009-07-14 23:10:40 +0000356 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
357 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000358 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
359 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000360 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000361 }
362
363 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
364 CodeGenFunction &CGF) const;
365
David Chisnallde3a0692009-08-17 23:08:21 +0000366 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000367 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000368 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:
373 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor0599df12010-01-22 15:41:14 +0000374 :TargetCodeGenInfo(new X86_32ABIInfo(Context, 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
446ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000447 ASTContext &Context,
448 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449 if (RetTy->isVoidType()) {
450 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000451 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000452 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000453 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454 uint64_t Size = Context.getTypeSize(RetTy);
455
456 // 128-bit vectors are a special case; they are returned in
457 // registers and we need to make sure to pick a type the LLVM
458 // backend will like.
459 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000460 return ABIArgInfo::getCoerce(llvm::VectorType::get(
461 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000462
463 // Always return in register if it fits in a general purpose
464 // register, or if it is 64 bits and has a single element.
465 if ((Size == 8 || Size == 16 || Size == 32) ||
466 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000467 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000468
469 return ABIArgInfo::getIndirect(0);
470 }
471
472 return ABIArgInfo::getDirect();
473 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000474 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000475 // Structures with either a non-trivial destructor or a non-trivial
476 // copy constructor are always indirect.
477 if (hasNonTrivialDestructorOrCopyConstructor(RT))
478 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
479
480 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000481 if (RT->getDecl()->hasFlexibleArrayMember())
482 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000483 }
484
David Chisnallde3a0692009-08-17 23:08:21 +0000485 // If specified, structs and unions are always indirect.
486 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000487 return ABIArgInfo::getIndirect(0);
488
489 // Classify "single element" structs as their element type.
490 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000491 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000492 if (BT->isIntegerType()) {
493 // We need to use the size of the structure, padding
494 // bit-fields can adjust that to be larger than the single
495 // element type.
496 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000497 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000498 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000499 } else if (BT->getKind() == BuiltinType::Float) {
500 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
501 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000502 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000503 } else if (BT->getKind() == BuiltinType::Double) {
504 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
505 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000506 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000507 }
508 } else if (SeltTy->isPointerType()) {
509 // FIXME: It would be really nice if this could come out as the proper
510 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000511 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512 return ABIArgInfo::getCoerce(PtrTy);
513 } else if (SeltTy->isVectorType()) {
514 // 64- and 128-bit vectors are never returned in a
515 // register when inside a structure.
516 uint64_t Size = Context.getTypeSize(RetTy);
517 if (Size == 64 || Size == 128)
518 return ABIArgInfo::getIndirect(0);
519
Owen Anderson170229f2009-07-14 23:10:40 +0000520 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000521 }
522 }
523
524 // Small structures which are register sized are generally returned
525 // in a register.
526 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
527 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000528 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 }
530
531 return ABIArgInfo::getIndirect(0);
532 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000533 // Treat an enum type as its underlying type.
534 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
535 RetTy = EnumTy->getDecl()->getIntegerType();
536
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000537 return (RetTy->isPromotableIntegerType() ?
538 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000539 }
540}
541
Daniel Dunbar557893d2010-04-21 19:10:51 +0000542ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
543 ASTContext &Context,
544 bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000545 if (!ByVal)
546 return ABIArgInfo::getIndirect(0, false);
547
548 // Compute the byval alignment. We trust the back-end to honor the
549 // minimum ABI alignment for byval, to make cleaner IR.
550 const unsigned MinABIAlign = 4;
551 unsigned Align = Context.getTypeAlign(Ty) / 8;
552 if (Align > MinABIAlign)
553 return ABIArgInfo::getIndirect(Align);
554 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000555}
556
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000557ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000558 ASTContext &Context,
559 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000560 // FIXME: Set alignment on indirect arguments.
561 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
562 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000563 if (const RecordType *RT = Ty->getAs<RecordType>()) {
564 // Structures with either a non-trivial destructor or a non-trivial
565 // copy constructor are always indirect.
566 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbar557893d2010-04-21 19:10:51 +0000567 return getIndirectResult(Ty, Context, /*ByVal=*/false);
568
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000569 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar557893d2010-04-21 19:10:51 +0000570 return getIndirectResult(Ty, Context);
Anders Carlsson40446e82010-01-27 03:25:19 +0000571 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000572
573 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000574 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000575 return ABIArgInfo::getIgnore();
576
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000577 // Expand small (<= 128-bit) record types when we know that the stack layout
578 // of those arguments will match the struct. This is important because the
579 // LLVM backend isn't smart enough to remove byval, which inhibits many
580 // optimizations.
581 if (Context.getTypeSize(Ty) <= 4*32 &&
582 canExpandIndirectArgument(Ty, Context))
583 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000584
Daniel Dunbar557893d2010-04-21 19:10:51 +0000585 return getIndirectResult(Ty, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000586 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000587 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
588 Ty = EnumTy->getDecl()->getIntegerType();
589
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000590 return (Ty->isPromotableIntegerType() ?
591 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000592 }
593}
594
595llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
596 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000597 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000598 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000599
600 CGBuilderTy &Builder = CGF.Builder;
601 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
602 "ap");
603 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
604 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000605 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000606 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
607
608 uint64_t Offset =
609 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
610 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000611 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000612 "ap.next");
613 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
614
615 return AddrTyped;
616}
617
Charles Davis4ea31ab2010-02-13 15:54:06 +0000618void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
619 llvm::GlobalValue *GV,
620 CodeGen::CodeGenModule &CGM) const {
621 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
622 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
623 // Get the LLVM function.
624 llvm::Function *Fn = cast<llvm::Function>(GV);
625
626 // Now add the 'alignstack' attribute with a value of 16.
627 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
628 }
629 }
630}
631
John McCallbeec5a02010-03-06 00:35:14 +0000632bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
633 CodeGen::CodeGenFunction &CGF,
634 llvm::Value *Address) const {
635 CodeGen::CGBuilderTy &Builder = CGF.Builder;
636 llvm::LLVMContext &Context = CGF.getLLVMContext();
637
638 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
639 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
640
641 // 0-7 are the eight integer registers; the order is different
642 // on Darwin (for EH), but the range is the same.
643 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000644 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000645
646 if (CGF.CGM.isTargetDarwin()) {
647 // 12-16 are st(0..4). Not sure why we stop at 4.
648 // These have size 16, which is sizeof(long double) on
649 // platforms with 8-byte alignment for that type.
650 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000651 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000652
653 } else {
654 // 9 is %eflags, which doesn't get a size on Darwin for some
655 // reason.
656 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
657
658 // 11-16 are st(0..5). Not sure why we stop at 5.
659 // These have size 12, which is sizeof(long double) on
660 // platforms with 4-byte alignment for that type.
661 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000662 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
663 }
John McCallbeec5a02010-03-06 00:35:14 +0000664
665 return false;
666}
667
Chris Lattner0cf24192010-06-28 20:05:43 +0000668//===----------------------------------------------------------------------===//
669// X86-64 ABI Implementation
670//===----------------------------------------------------------------------===//
671
672
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000673namespace {
674/// X86_64ABIInfo - The X86_64 ABI information.
675class X86_64ABIInfo : public ABIInfo {
Chris Lattner22a931e2010-06-29 06:01:59 +0000676 ASTContext &Context;
677 const llvm::TargetData &TD;
678
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000679 enum Class {
680 Integer = 0,
681 SSE,
682 SSEUp,
683 X87,
684 X87Up,
685 ComplexX87,
686 NoClass,
687 Memory
688 };
689
690 /// merge - Implement the X86_64 ABI merging algorithm.
691 ///
692 /// Merge an accumulating classification \arg Accum with a field
693 /// classification \arg Field.
694 ///
695 /// \param Accum - The accumulating classification. This should
696 /// always be either NoClass or the result of a previous merge
697 /// call. In addition, this should never be Memory (the caller
698 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000699 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000700
701 /// classify - Determine the x86_64 register classes in which the
702 /// given type T should be passed.
703 ///
704 /// \param Lo - The classification for the parts of the type
705 /// residing in the low word of the containing object.
706 ///
707 /// \param Hi - The classification for the parts of the type
708 /// residing in the high word of the containing object.
709 ///
710 /// \param OffsetBase - The bit offset of this type in the
711 /// containing object. Some parameters are classified different
712 /// depending on whether they straddle an eightbyte boundary.
713 ///
714 /// If a word is unused its result will be NoClass; if a type should
715 /// be passed in Memory then at least the classification of \arg Lo
716 /// will be Memory.
717 ///
718 /// The \arg Lo class will be NoClass iff the argument is ignored.
719 ///
720 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
721 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000722 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000723
724 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
725 /// to coerce to, chose the best way to pass Ty in the same place
726 /// that \arg CoerceTo would be passed, but while keeping the
727 /// emitted code as simple as possible.
728 ///
729 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
730 /// the ways we might want to pass things, instead of constructing an LLVM
731 /// type. This makes this code more explicit, and it makes it clearer that we
732 /// are also doing this for correctness in the case of passing scalar types.
733 ABIArgInfo getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +0000734 const llvm::Type *CoerceTo) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000735
736 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000737 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000738 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000739
740 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000741 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000742 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000743
744 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000745 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000746
747 ABIArgInfo classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000748 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000749 unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +0000750 unsigned &neededSSE,
751 const llvm::Type *PrefType) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000752
753public:
Chris Lattner22a931e2010-06-29 06:01:59 +0000754 X86_64ABIInfo(ASTContext &Ctx, const llvm::TargetData &td)
755 : Context(Ctx), TD(td) {}
756
Owen Anderson170229f2009-07-14 23:10:40 +0000757 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000758 llvm::LLVMContext &VMContext,
759 const llvm::Type *const *PrefTypes,
760 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000761
762 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
763 CodeGenFunction &CGF) const;
764};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000765
766class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
767public:
Chris Lattner22a931e2010-06-29 06:01:59 +0000768 X86_64TargetCodeGenInfo(ASTContext &Ctx, const llvm::TargetData &TD)
769 : TargetCodeGenInfo(new X86_64ABIInfo(Ctx, TD)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000770
771 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
772 return 7;
773 }
774
775 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
776 llvm::Value *Address) const {
777 CodeGen::CGBuilderTy &Builder = CGF.Builder;
778 llvm::LLVMContext &Context = CGF.getLLVMContext();
779
780 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
781 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
782
John McCall943fae92010-05-27 06:19:26 +0000783 // 0-15 are the 16 integer registers.
784 // 16 is %rip.
785 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000786
787 return false;
788 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000789};
790
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000791}
792
Chris Lattnerd776fb12010-06-28 21:43:59 +0000793X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000794 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
795 // classified recursively so that always two fields are
796 // considered. The resulting class is calculated according to
797 // the classes of the fields in the eightbyte:
798 //
799 // (a) If both classes are equal, this is the resulting class.
800 //
801 // (b) If one of the classes is NO_CLASS, the resulting class is
802 // the other class.
803 //
804 // (c) If one of the classes is MEMORY, the result is the MEMORY
805 // class.
806 //
807 // (d) If one of the classes is INTEGER, the result is the
808 // INTEGER.
809 //
810 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
811 // MEMORY is used as class.
812 //
813 // (f) Otherwise class SSE is used.
814
815 // Accum should never be memory (we should have returned) or
816 // ComplexX87 (because this cannot be passed in a structure).
817 assert((Accum != Memory && Accum != ComplexX87) &&
818 "Invalid accumulated classification during merge.");
819 if (Accum == Field || Field == NoClass)
820 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000821 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000822 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000823 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000824 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000825 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000826 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000827 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
828 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000829 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000830 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000831}
832
833void X86_64ABIInfo::classify(QualType Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000834 uint64_t OffsetBase,
835 Class &Lo, Class &Hi) const {
836 // FIXME: This code can be simplified by introducing a simple value class for
837 // Class pairs with appropriate constructor methods for the various
838 // situations.
839
840 // FIXME: Some of the split computations are wrong; unaligned vectors
841 // shouldn't be passed in registers for example, so there is no chance they
842 // can straddle an eightbyte. Verify & simplify.
843
844 Lo = Hi = NoClass;
845
846 Class &Current = OffsetBase < 64 ? Lo : Hi;
847 Current = Memory;
848
John McCall9dd450b2009-09-21 23:43:11 +0000849 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000850 BuiltinType::Kind k = BT->getKind();
851
852 if (k == BuiltinType::Void) {
853 Current = NoClass;
854 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
855 Lo = Integer;
856 Hi = Integer;
857 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
858 Current = Integer;
859 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
860 Current = SSE;
861 } else if (k == BuiltinType::LongDouble) {
862 Lo = X87;
863 Hi = X87Up;
864 }
865 // FIXME: _Decimal32 and _Decimal64 are SSE.
866 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000867 return;
868 }
869
870 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000871 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000872 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000873 return;
874 }
875
876 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000877 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000878 return;
879 }
880
881 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000882 if (Ty->isMemberFunctionPointerType())
883 Lo = Hi = Integer;
884 else
885 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000886 return;
887 }
888
889 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000890 uint64_t Size = Context.getTypeSize(VT);
891 if (Size == 32) {
892 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
893 // float> as integer.
894 Current = Integer;
895
896 // If this type crosses an eightbyte boundary, it should be
897 // split.
898 uint64_t EB_Real = (OffsetBase) / 64;
899 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
900 if (EB_Real != EB_Imag)
901 Hi = Lo;
902 } else if (Size == 64) {
903 // gcc passes <1 x double> in memory. :(
904 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
905 return;
906
907 // gcc passes <1 x long long> as INTEGER.
908 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
909 Current = Integer;
910 else
911 Current = SSE;
912
913 // If this type crosses an eightbyte boundary, it should be
914 // split.
915 if (OffsetBase && OffsetBase != 64)
916 Hi = Lo;
917 } else if (Size == 128) {
918 Lo = SSE;
919 Hi = SSEUp;
920 }
Chris Lattnerd776fb12010-06-28 21:43:59 +0000921 return;
922 }
923
924 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000925 QualType ET = Context.getCanonicalType(CT->getElementType());
926
927 uint64_t Size = Context.getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +0000928 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000929 if (Size <= 64)
930 Current = Integer;
931 else if (Size <= 128)
932 Lo = Hi = Integer;
933 } else if (ET == Context.FloatTy)
934 Current = SSE;
935 else if (ET == Context.DoubleTy)
936 Lo = Hi = SSE;
937 else if (ET == Context.LongDoubleTy)
938 Current = ComplexX87;
939
940 // If this complex type crosses an eightbyte boundary then it
941 // should be split.
942 uint64_t EB_Real = (OffsetBase) / 64;
943 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
944 if (Hi == NoClass && EB_Real != EB_Imag)
945 Hi = Lo;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000946
947 return;
948 }
949
950 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000951 // Arrays are treated like structures.
952
953 uint64_t Size = Context.getTypeSize(Ty);
954
955 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
956 // than two eightbytes, ..., it has class MEMORY.
957 if (Size > 128)
958 return;
959
960 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
961 // fields, it has class MEMORY.
962 //
963 // Only need to check alignment of array base.
964 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
965 return;
966
967 // Otherwise implement simplified merge. We could be smarter about
968 // this, but it isn't worth it and would be harder to verify.
969 Current = NoClass;
970 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
971 uint64_t ArraySize = AT->getSize().getZExtValue();
972 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
973 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +0000974 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000975 Lo = merge(Lo, FieldLo);
976 Hi = merge(Hi, FieldHi);
977 if (Lo == Memory || Hi == Memory)
978 break;
979 }
980
981 // Do post merger cleanup (see below). Only case we worry about is Memory.
982 if (Hi == Memory)
983 Lo = Memory;
984 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +0000985 return;
986 }
987
988 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000989 uint64_t Size = Context.getTypeSize(Ty);
990
991 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
992 // than two eightbytes, ..., it has class MEMORY.
993 if (Size > 128)
994 return;
995
Anders Carlsson20759ad2009-09-16 15:53:40 +0000996 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
997 // copy constructor or a non-trivial destructor, it is passed by invisible
998 // reference.
999 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1000 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001001
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001002 const RecordDecl *RD = RT->getDecl();
1003
1004 // Assume variable sized types are passed in memory.
1005 if (RD->hasFlexibleArrayMember())
1006 return;
1007
1008 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1009
1010 // Reset Lo class, this will be recomputed.
1011 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001012
1013 // If this is a C++ record, classify the bases first.
1014 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1015 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1016 e = CXXRD->bases_end(); i != e; ++i) {
1017 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1018 "Unexpected base class!");
1019 const CXXRecordDecl *Base =
1020 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1021
1022 // Classify this field.
1023 //
1024 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1025 // single eightbyte, each is classified separately. Each eightbyte gets
1026 // initialized to class NO_CLASS.
1027 Class FieldLo, FieldHi;
1028 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001029 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001030 Lo = merge(Lo, FieldLo);
1031 Hi = merge(Hi, FieldHi);
1032 if (Lo == Memory || Hi == Memory)
1033 break;
1034 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +00001035
1036 // If this record has no fields but isn't empty, classify as INTEGER.
1037 if (RD->field_empty() && Size)
1038 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001039 }
1040
1041 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001042 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001043 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1044 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001045 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1046 bool BitField = i->isBitField();
1047
1048 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1049 // fields, it has class MEMORY.
1050 //
1051 // Note, skip this test for bit-fields, see below.
1052 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1053 Lo = Memory;
1054 return;
1055 }
1056
1057 // Classify this field.
1058 //
1059 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1060 // exceeds a single eightbyte, each is classified
1061 // separately. Each eightbyte gets initialized to class
1062 // NO_CLASS.
1063 Class FieldLo, FieldHi;
1064
1065 // Bit-fields require special handling, they do not force the
1066 // structure to be passed in memory even if unaligned, and
1067 // therefore they can straddle an eightbyte.
1068 if (BitField) {
1069 // Ignore padding bit-fields.
1070 if (i->isUnnamedBitfield())
1071 continue;
1072
1073 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1074 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1075
1076 uint64_t EB_Lo = Offset / 64;
1077 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1078 FieldLo = FieldHi = NoClass;
1079 if (EB_Lo) {
1080 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1081 FieldLo = NoClass;
1082 FieldHi = Integer;
1083 } else {
1084 FieldLo = Integer;
1085 FieldHi = EB_Hi ? Integer : NoClass;
1086 }
1087 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001088 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001089 Lo = merge(Lo, FieldLo);
1090 Hi = merge(Hi, FieldHi);
1091 if (Lo == Memory || Hi == Memory)
1092 break;
1093 }
1094
1095 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1096 //
1097 // (a) If one of the classes is MEMORY, the whole argument is
1098 // passed in memory.
1099 //
1100 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1101
1102 // The first of these conditions is guaranteed by how we implement
1103 // the merge (just bail).
1104 //
1105 // The second condition occurs in the case of unions; for example
1106 // union { _Complex double; unsigned; }.
1107 if (Hi == Memory)
1108 Lo = Memory;
1109 if (Hi == SSEUp && Lo != SSE)
1110 Hi = SSE;
1111 }
1112}
1113
1114ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
Chris Lattner22a931e2010-06-29 06:01:59 +00001115 const llvm::Type *CoerceTo) const {
1116 if (CoerceTo->isIntegerTy(64) || isa<llvm::PointerType>(CoerceTo)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001117 // Integer and pointer types will end up in a general purpose
1118 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001119
1120 // Treat an enum type as its underlying type.
1121 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1122 Ty = EnumTy->getDecl()->getIntegerType();
1123
Douglas Gregor6972a622010-06-16 00:35:25 +00001124 if (Ty->isIntegralOrEnumerationType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001125 return (Ty->isPromotableIntegerType() ?
1126 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001127
Chris Lattner93af3322010-06-28 21:59:07 +00001128 // If this is a 8/16/32-bit structure that is passed as an int64, then it
1129 // will be passed in the low 8/16/32-bits of a 64-bit GPR, which is the same
1130 // as how an i8/i16/i32 is passed. Coerce to a i8/i16/i32 instead of a i64.
1131 switch (Context.getTypeSizeInChars(Ty).getQuantity()) {
1132 default: break;
1133 case 1: CoerceTo = llvm::Type::getInt8Ty(CoerceTo->getContext()); break;
1134 case 2: CoerceTo = llvm::Type::getInt16Ty(CoerceTo->getContext()); break;
1135 case 4: CoerceTo = llvm::Type::getInt32Ty(CoerceTo->getContext()); break;
1136 }
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001137
Chris Lattnerfa20e952010-06-26 21:52:32 +00001138 } else if (CoerceTo->isDoubleTy()) {
John McCall8ee376f2010-02-24 07:14:12 +00001139 assert(Ty.isCanonical() && "should always have a canonical type here");
1140 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001141
1142 // Float and double end up in a single SSE reg.
John McCall8ee376f2010-02-24 07:14:12 +00001143 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001144 return ABIArgInfo::getDirect();
1145
Chris Lattnera7d81ab2010-06-28 19:56:59 +00001146 // If this is a 32-bit structure that is passed as a double, then it will be
1147 // passed in the low 32-bits of the XMM register, which is the same as how a
1148 // float is passed. Coerce to a float instead of a double.
1149 if (Context.getTypeSizeInChars(Ty).getQuantity() == 4)
1150 CoerceTo = llvm::Type::getFloatTy(CoerceTo->getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001151 }
1152
1153 return ABIArgInfo::getCoerce(CoerceTo);
1154}
1155
Chris Lattner22a931e2010-06-29 06:01:59 +00001156ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001157 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1158 // place naturally.
1159 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1160 // Treat an enum type as its underlying type.
1161 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1162 Ty = EnumTy->getDecl()->getIntegerType();
1163
1164 return (Ty->isPromotableIntegerType() ?
1165 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1166 }
1167
1168 return ABIArgInfo::getIndirect(0);
1169}
1170
Chris Lattner22a931e2010-06-29 06:01:59 +00001171ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001172 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1173 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001174 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1175 // Treat an enum type as its underlying type.
1176 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1177 Ty = EnumTy->getDecl()->getIntegerType();
1178
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001179 return (Ty->isPromotableIntegerType() ?
1180 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001181 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001182
Daniel Dunbar53fac692010-04-21 19:49:55 +00001183 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1184 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001185
Daniel Dunbar53fac692010-04-21 19:49:55 +00001186 // Compute the byval alignment. We trust the back-end to honor the
1187 // minimum ABI alignment for byval, to make cleaner IR.
1188 const unsigned MinABIAlign = 8;
1189 unsigned Align = Context.getTypeAlign(Ty) / 8;
1190 if (Align > MinABIAlign)
1191 return ABIArgInfo::getIndirect(Align);
1192 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001193}
1194
Chris Lattnerd776fb12010-06-28 21:43:59 +00001195ABIArgInfo X86_64ABIInfo::
Chris Lattner22a931e2010-06-29 06:01:59 +00001196classifyReturnType(QualType RetTy, llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001197 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1198 // classification algorithm.
1199 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001200 classify(RetTy, 0, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001201
1202 // Check some invariants.
1203 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1204 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1205 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1206
1207 const llvm::Type *ResType = 0;
1208 switch (Lo) {
1209 case NoClass:
1210 return ABIArgInfo::getIgnore();
1211
1212 case SSEUp:
1213 case X87Up:
1214 assert(0 && "Invalid classification for lo word.");
1215
1216 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1217 // hidden argument.
1218 case Memory:
Chris Lattner22a931e2010-06-29 06:01:59 +00001219 return getIndirectReturnResult(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001220
1221 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1222 // available register of the sequence %rax, %rdx is used.
1223 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +00001224 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001225
1226 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1227 // available SSE register of the sequence %xmm0, %xmm1 is used.
1228 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +00001229 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001230
1231 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1232 // returned on the X87 stack in %st0 as 80-bit x87 number.
1233 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +00001234 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001235
1236 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1237 // part of the value is returned in %st0 and the imaginary part in
1238 // %st1.
1239 case ComplexX87:
1240 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001241 ResType = llvm::StructType::get(VMContext,
1242 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson41a75022009-08-13 21:57:51 +00001243 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001244 NULL);
1245 break;
1246 }
1247
1248 switch (Hi) {
1249 // Memory was handled previously and X87 should
1250 // never occur as a hi class.
1251 case Memory:
1252 case X87:
1253 assert(0 && "Invalid classification for hi word.");
1254
1255 case ComplexX87: // Previously handled.
1256 case NoClass: break;
1257
1258 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001259 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001260 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001261 break;
1262 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001263 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001264 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001265 break;
1266
1267 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1268 // is passed in the upper half of the last used SSE register.
1269 //
1270 // SSEUP should always be preceeded by SSE, just widen.
1271 case SSEUp:
1272 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001273 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001274 break;
1275
1276 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1277 // returned together with the previous X87 value in %st0.
1278 case X87Up:
1279 // If X87Up is preceeded by X87, we don't need to do
1280 // anything. However, in some cases with unions it may not be
1281 // preceeded by X87. In such situations we follow gcc and pass the
1282 // extra bits in an SSE reg.
1283 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +00001284 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001285 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001286 break;
1287 }
1288
Chris Lattner22a931e2010-06-29 06:01:59 +00001289 return getCoerceResult(RetTy, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001290}
1291
Chris Lattner22a931e2010-06-29 06:01:59 +00001292static const llvm::Type *Get8ByteTypeAtOffset(const llvm::Type *PrefType,
1293 unsigned Offset,
1294 const llvm::TargetData &TD) {
1295 if (PrefType == 0) return 0;
1296
1297 // Pointers are always 8-bytes at offset 0.
1298 if (Offset == 0 && isa<llvm::PointerType>(PrefType))
1299 return PrefType;
1300
1301 // TODO: 1/2/4/8 byte integers are also interesting, but we have to know that
1302 // the "hole" is not used in the containing struct (just undef padding).
1303 const llvm::StructType *STy = dyn_cast<llvm::StructType>(PrefType);
1304 if (STy == 0) return 0;
1305
1306 // If this is a struct, recurse into the field at the specified offset.
1307 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1308 if (Offset >= SL->getSizeInBytes()) return 0;
1309
1310 unsigned FieldIdx = SL->getElementContainingOffset(Offset);
1311 Offset -= SL->getElementOffset(FieldIdx);
1312
1313 return Get8ByteTypeAtOffset(STy->getElementType(FieldIdx), Offset, TD);
1314}
1315
1316ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001317 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001318 unsigned &neededInt,
Chris Lattner399d22a2010-06-29 01:14:09 +00001319 unsigned &neededSSE,
1320 const llvm::Type *PrefType)const{
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001321 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001322 classify(Ty, 0, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001323
1324 // Check some invariants.
1325 // FIXME: Enforce these by construction.
1326 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1327 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1328 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1329
1330 neededInt = 0;
1331 neededSSE = 0;
1332 const llvm::Type *ResType = 0;
1333 switch (Lo) {
1334 case NoClass:
1335 return ABIArgInfo::getIgnore();
1336
1337 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1338 // on the stack.
1339 case Memory:
1340
1341 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1342 // COMPLEX_X87, it is passed in memory.
1343 case X87:
1344 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001345 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001346
1347 case SSEUp:
1348 case X87Up:
1349 assert(0 && "Invalid classification for lo word.");
1350
1351 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1352 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1353 // and %r9 is used.
1354 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001355 // It is always safe to classify this as an i64 argument.
Owen Anderson41a75022009-08-13 21:57:51 +00001356 ResType = llvm::Type::getInt64Ty(VMContext);
Chris Lattner22a931e2010-06-29 06:01:59 +00001357 ++neededInt;
1358
1359 // If we can choose a better 8-byte type based on the preferred type, and if
1360 // that type is still passed in a GPR, use it.
1361 if (const llvm::Type *PrefTypeLo = Get8ByteTypeAtOffset(PrefType, 0, TD))
1362 if (isa<llvm::IntegerType>(PrefTypeLo) ||
1363 isa<llvm::PointerType>(PrefTypeLo))
1364 ResType = PrefTypeLo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001365 break;
1366
1367 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1368 // available SSE register is used, the registers are taken in the
1369 // order from %xmm0 to %xmm7.
1370 case SSE:
1371 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001372 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001373 break;
1374 }
1375
1376 switch (Hi) {
1377 // Memory was handled previously, ComplexX87 and X87 should
1378 // never occur as hi classes, and X87Up must be preceed by X87,
1379 // which is passed in memory.
1380 case Memory:
1381 case X87:
1382 case ComplexX87:
1383 assert(0 && "Invalid classification for hi word.");
1384 break;
1385
1386 case NoClass: break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001387
1388 case Integer: {
1389 // It is always safe to classify this as an i64 argument.
1390 const llvm::Type *HiType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001391 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001392
1393 // If we can choose a better 8-byte type based on the preferred type, and if
1394 // that type is still passed in a GPR, use it.
1395 if (const llvm::Type *PrefTypeHi = Get8ByteTypeAtOffset(PrefType, 8, TD))
1396 if (isa<llvm::IntegerType>(PrefTypeHi) ||
1397 isa<llvm::PointerType>(PrefTypeHi))
1398 HiType = PrefTypeHi;
1399
1400 ResType = llvm::StructType::get(VMContext, ResType, HiType, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001401 break;
Chris Lattner22a931e2010-06-29 06:01:59 +00001402 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001403
1404 // X87Up generally doesn't occur here (long double is passed in
1405 // memory), except in situations involving unions.
1406 case X87Up:
1407 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001408 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001409 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001410 ++neededSSE;
1411 break;
1412
1413 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1414 // eightbyte is passed in the upper half of the last used SSE
1415 // register.
1416 case SSEUp:
1417 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001418 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001419 break;
1420 }
1421
Chris Lattner22a931e2010-06-29 06:01:59 +00001422 return getCoerceResult(Ty, ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001423}
1424
Owen Anderson170229f2009-07-14 23:10:40 +00001425void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001426 llvm::LLVMContext &VMContext,
1427 const llvm::Type *const *PrefTypes,
1428 unsigned NumPrefTypes) const {
Chris Lattner22a931e2010-06-29 06:01:59 +00001429 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001430
1431 // Keep track of the number of assigned registers.
1432 unsigned freeIntRegs = 6, freeSSERegs = 8;
1433
1434 // If the return value is indirect, then the hidden argument is consuming one
1435 // integer register.
1436 if (FI.getReturnInfo().isIndirect())
1437 --freeIntRegs;
1438
1439 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1440 // get assigned (in left-to-right order) for passing as follows...
1441 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1442 it != ie; ++it) {
Chris Lattner399d22a2010-06-29 01:14:09 +00001443 // If the client specified a preferred IR type to use, pass it down to
1444 // classifyArgumentType.
1445 const llvm::Type *PrefType = 0;
1446 if (NumPrefTypes) {
1447 PrefType = *PrefTypes++;
1448 --NumPrefTypes;
1449 }
1450
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001451 unsigned neededInt, neededSSE;
Chris Lattner22a931e2010-06-29 06:01:59 +00001452 it->info = classifyArgumentType(it->type, VMContext,
Chris Lattner399d22a2010-06-29 01:14:09 +00001453 neededInt, neededSSE, PrefType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001454
1455 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1456 // eightbyte of an argument, the whole argument is passed on the
1457 // stack. If registers have already been assigned for some
1458 // eightbytes of such an argument, the assignments get reverted.
1459 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1460 freeIntRegs -= neededInt;
1461 freeSSERegs -= neededSSE;
1462 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001463 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001464 }
1465 }
1466}
1467
1468static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1469 QualType Ty,
1470 CodeGenFunction &CGF) {
1471 llvm::Value *overflow_arg_area_p =
1472 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1473 llvm::Value *overflow_arg_area =
1474 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1475
1476 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1477 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1478 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1479 if (Align > 8) {
1480 // Note that we follow the ABI & gcc here, even though the type
1481 // could in theory have an alignment greater than 16. This case
1482 // shouldn't ever matter in practice.
1483
1484 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001485 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001486 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001487 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1488 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001489 CGF.Int64Ty);
1490 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001491 overflow_arg_area =
1492 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1493 overflow_arg_area->getType(),
1494 "overflow_arg_area.align");
1495 }
1496
1497 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1498 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1499 llvm::Value *Res =
1500 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001501 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001502
1503 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1504 // l->overflow_arg_area + sizeof(type).
1505 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1506 // an 8 byte boundary.
1507
1508 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001509 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001510 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001511 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1512 "overflow_arg_area.next");
1513 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1514
1515 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1516 return Res;
1517}
1518
1519llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1520 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001521 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001522
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001523 // Assume that va_list type is correct; should be pointer to LLVM type:
1524 // struct {
1525 // i32 gp_offset;
1526 // i32 fp_offset;
1527 // i8* overflow_arg_area;
1528 // i8* reg_save_area;
1529 // };
1530 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001531
1532 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner22a931e2010-06-29 06:01:59 +00001533 ABIArgInfo AI = classifyArgumentType(Ty, VMContext, neededInt, neededSSE, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001534
1535 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1536 // in the registers. If not go to step 7.
1537 if (!neededInt && !neededSSE)
1538 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1539
1540 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1541 // general purpose registers needed to pass type and num_fp to hold
1542 // the number of floating point registers needed.
1543
1544 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1545 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1546 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1547 //
1548 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1549 // register save space).
1550
1551 llvm::Value *InRegs = 0;
1552 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1553 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1554 if (neededInt) {
1555 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1556 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001557 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1558 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001559 }
1560
1561 if (neededSSE) {
1562 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1563 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1564 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001565 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1566 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001567 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1568 }
1569
1570 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1571 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1572 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1573 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1574
1575 // Emit code to load the value if it was passed in registers.
1576
1577 CGF.EmitBlock(InRegBlock);
1578
1579 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1580 // an offset of l->gp_offset and/or l->fp_offset. This may require
1581 // copying to a temporary location in case the parameter is passed
1582 // in different register classes or requires an alignment greater
1583 // than 8 for general purpose registers and 16 for XMM registers.
1584 //
1585 // FIXME: This really results in shameful code when we end up needing to
1586 // collect arguments from different places; often what should result in a
1587 // simple assembling of a structure from scattered addresses has many more
1588 // loads than necessary. Can we clean this up?
1589 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1590 llvm::Value *RegAddr =
1591 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1592 "reg_save_area");
1593 if (neededInt && neededSSE) {
1594 // FIXME: Cleanup.
1595 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1596 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1597 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1598 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1599 const llvm::Type *TyLo = ST->getElementType(0);
1600 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001601 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001602 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001603 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1604 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001605 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1606 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001607 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1608 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001609 llvm::Value *V =
1610 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1611 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1612 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1613 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1614
Owen Anderson170229f2009-07-14 23:10:40 +00001615 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001616 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001617 } else if (neededInt) {
1618 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1619 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001620 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00001621 } else if (neededSSE == 1) {
1622 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1623 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1624 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001625 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00001626 assert(neededSSE == 2 && "Invalid number of needed registers!");
1627 // SSE registers are spaced 16 bytes apart in the register save
1628 // area, we need to collect the two eightbytes together.
1629 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001630 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00001631 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1632 const llvm::Type *DblPtrTy =
1633 llvm::PointerType::getUnqual(DoubleTy);
1634 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1635 DoubleTy, NULL);
1636 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1637 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1638 DblPtrTy));
1639 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1640 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1641 DblPtrTy));
1642 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1643 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1644 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001645 }
1646
1647 // AMD64-ABI 3.5.7p5: Step 5. Set:
1648 // l->gp_offset = l->gp_offset + num_gp * 8
1649 // l->fp_offset = l->fp_offset + num_fp * 16.
1650 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001651 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001652 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1653 gp_offset_p);
1654 }
1655 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00001656 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001657 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1658 fp_offset_p);
1659 }
1660 CGF.EmitBranch(ContBlock);
1661
1662 // Emit code to load the value if it was passed in memory.
1663
1664 CGF.EmitBlock(InMemBlock);
1665 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1666
1667 // Return the appropriate result.
1668
1669 CGF.EmitBlock(ContBlock);
1670 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1671 "vaarg.addr");
1672 ResAddr->reserveOperandSpace(2);
1673 ResAddr->addIncoming(RegAddr, InRegBlock);
1674 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001675 return ResAddr;
1676}
1677
Chris Lattner0cf24192010-06-28 20:05:43 +00001678
1679
1680//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001681// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001682//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001683
1684namespace {
1685
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001686class PIC16ABIInfo : public ABIInfo {
1687 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001688 ASTContext &Context,
1689 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001690
1691 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001692 ASTContext &Context,
1693 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001694
Owen Anderson170229f2009-07-14 23:10:40 +00001695 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001696 llvm::LLVMContext &VMContext,
1697 const llvm::Type *const *PrefTypes,
1698 unsigned NumPrefTypes) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001699 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1700 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001701 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1702 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001703 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001704 }
1705
1706 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1707 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001708};
1709
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001710class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1711public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001712 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001713};
1714
Daniel Dunbard59655c2009-09-12 00:59:49 +00001715}
1716
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001717ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001718 ASTContext &Context,
1719 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001720 if (RetTy->isVoidType()) {
1721 return ABIArgInfo::getIgnore();
1722 } else {
1723 return ABIArgInfo::getDirect();
1724 }
1725}
1726
1727ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001728 ASTContext &Context,
1729 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001730 return ABIArgInfo::getDirect();
1731}
1732
1733llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001734 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001735 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001736 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1737
1738 CGBuilderTy &Builder = CGF.Builder;
1739 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1740 "ap");
1741 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1742 llvm::Type *PTy =
1743 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1744 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1745
1746 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1747
1748 llvm::Value *NextAddr =
1749 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1750 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1751 "ap.next");
1752 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1753
1754 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001755}
1756
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001757
John McCallea8d8bb2010-03-11 00:10:12 +00001758// PowerPC-32
1759
1760namespace {
1761class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1762public:
1763 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1764 // This is recovered from gcc output.
1765 return 1; // r1 is the dedicated stack pointer
1766 }
1767
1768 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1769 llvm::Value *Address) const;
1770};
1771
1772}
1773
1774bool
1775PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1776 llvm::Value *Address) const {
1777 // This is calculated from the LLVM and GCC tables and verified
1778 // against gcc output. AFAIK all ABIs use the same encoding.
1779
1780 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1781 llvm::LLVMContext &Context = CGF.getLLVMContext();
1782
1783 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1784 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1785 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1786 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1787
1788 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00001789 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00001790
1791 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00001792 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00001793
1794 // 64-76 are various 4-byte special-purpose registers:
1795 // 64: mq
1796 // 65: lr
1797 // 66: ctr
1798 // 67: ap
1799 // 68-75 cr0-7
1800 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00001801 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00001802
1803 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00001804 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00001805
1806 // 109: vrsave
1807 // 110: vscr
1808 // 111: spe_acc
1809 // 112: spefscr
1810 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00001811 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00001812
1813 return false;
1814}
1815
1816
Chris Lattner0cf24192010-06-28 20:05:43 +00001817//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001818// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00001819//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00001820
1821namespace {
1822
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001823class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001824public:
1825 enum ABIKind {
1826 APCS = 0,
1827 AAPCS = 1,
1828 AAPCS_VFP
1829 };
1830
1831private:
1832 ABIKind Kind;
1833
1834public:
1835 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1836
1837private:
1838 ABIKind getABIKind() const { return Kind; }
1839
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001840 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001841 ASTContext &Context,
1842 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001843
1844 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001845 ASTContext &Context,
1846 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001847
Owen Anderson170229f2009-07-14 23:10:40 +00001848 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001849 llvm::LLVMContext &VMContext,
1850 const llvm::Type *const *PrefTypes,
1851 unsigned NumPrefTypes) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001852
1853 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1854 CodeGenFunction &CGF) const;
1855};
1856
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001857class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1858public:
1859 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor0599df12010-01-22 15:41:14 +00001860 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001861
1862 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1863 return 13;
1864 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001865};
1866
Daniel Dunbard59655c2009-09-12 00:59:49 +00001867}
1868
Owen Anderson170229f2009-07-14 23:10:40 +00001869void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00001870 llvm::LLVMContext &VMContext,
1871 const llvm::Type *const *PrefTypes,
1872 unsigned NumPrefTypes) const {
Mike Stump11289f42009-09-09 15:08:12 +00001873 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001874 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001875 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1876 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001877 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001878 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001879
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001880 const llvm::Triple &Triple(Context.Target.getTriple());
1881 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001882 if (Triple.getEnvironmentName() == "gnueabi" ||
1883 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001884 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00001885 else
1886 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001887
Daniel Dunbar020daa92009-09-12 01:00:39 +00001888 switch (getABIKind()) {
1889 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001890 if (DefaultCC != llvm::CallingConv::ARM_APCS)
1891 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001892 break;
1893
1894 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00001895 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
1896 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00001897 break;
1898
1899 case AAPCS_VFP:
1900 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1901 break;
1902 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001903}
1904
1905ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001906 ASTContext &Context,
1907 llvm::LLVMContext &VMContext) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001908 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1909 // Treat an enum type as its underlying type.
1910 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1911 Ty = EnumTy->getDecl()->getIntegerType();
1912
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001913 return (Ty->isPromotableIntegerType() ?
1914 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001915 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001916
Daniel Dunbar09d33622009-09-14 21:54:03 +00001917 // Ignore empty records.
1918 if (isEmptyRecord(Context, Ty, true))
1919 return ABIArgInfo::getIgnore();
1920
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00001921 // Structures with either a non-trivial destructor or a non-trivial
1922 // copy constructor are always indirect.
1923 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1924 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
1925
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001926 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1927 // backend doesn't support byval.
1928 // FIXME: This doesn't handle alignment > 64 bits.
1929 const llvm::Type* ElemTy;
1930 unsigned SizeRegs;
1931 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001932 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001933 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1934 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001935 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001936 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1937 }
1938 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001939 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001940 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001941 return ABIArgInfo::getCoerce(STy);
1942}
1943
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001944static bool isIntegerLikeType(QualType Ty,
1945 ASTContext &Context,
1946 llvm::LLVMContext &VMContext) {
1947 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1948 // is called integer-like if its size is less than or equal to one word, and
1949 // the offset of each of its addressable sub-fields is zero.
1950
1951 uint64_t Size = Context.getTypeSize(Ty);
1952
1953 // Check that the type fits in a word.
1954 if (Size > 32)
1955 return false;
1956
1957 // FIXME: Handle vector types!
1958 if (Ty->isVectorType())
1959 return false;
1960
Daniel Dunbard53bac72009-09-14 02:20:34 +00001961 // Float types are never treated as "integer like".
1962 if (Ty->isRealFloatingType())
1963 return false;
1964
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001965 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001966 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001967 return true;
1968
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001969 // Small complex integer types are "integer like".
1970 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1971 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001972
1973 // Single element and zero sized arrays should be allowed, by the definition
1974 // above, but they are not.
1975
1976 // Otherwise, it must be a record type.
1977 const RecordType *RT = Ty->getAs<RecordType>();
1978 if (!RT) return false;
1979
1980 // Ignore records with flexible arrays.
1981 const RecordDecl *RD = RT->getDecl();
1982 if (RD->hasFlexibleArrayMember())
1983 return false;
1984
1985 // Check that all sub-fields are at offset 0, and are themselves "integer
1986 // like".
1987 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1988
1989 bool HadField = false;
1990 unsigned idx = 0;
1991 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1992 i != e; ++i, ++idx) {
1993 const FieldDecl *FD = *i;
1994
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001995 // Bit-fields are not addressable, we only need to verify they are "integer
1996 // like". We still have to disallow a subsequent non-bitfield, for example:
1997 // struct { int : 0; int x }
1998 // is non-integer like according to gcc.
1999 if (FD->isBitField()) {
2000 if (!RD->isUnion())
2001 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002002
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002003 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2004 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002005
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002006 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002007 }
2008
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002009 // Check if this field is at offset 0.
2010 if (Layout.getFieldOffset(idx) != 0)
2011 return false;
2012
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002013 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2014 return false;
2015
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002016 // Only allow at most one field in a structure. This doesn't match the
2017 // wording above, but follows gcc in situations with a field following an
2018 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002019 if (!RD->isUnion()) {
2020 if (HadField)
2021 return false;
2022
2023 HadField = true;
2024 }
2025 }
2026
2027 return true;
2028}
2029
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002030ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00002031 ASTContext &Context,
2032 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002033 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002034 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002035
Douglas Gregora71cc152010-02-02 20:10:50 +00002036 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2037 // Treat an enum type as its underlying type.
2038 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2039 RetTy = EnumTy->getDecl()->getIntegerType();
2040
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002041 return (RetTy->isPromotableIntegerType() ?
2042 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002043 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002044
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002045 // Structures with either a non-trivial destructor or a non-trivial
2046 // copy constructor are always indirect.
2047 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2048 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2049
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002050 // Are we following APCS?
2051 if (getABIKind() == APCS) {
2052 if (isEmptyRecord(Context, RetTy, false))
2053 return ABIArgInfo::getIgnore();
2054
Daniel Dunbareedf1512010-02-01 23:31:19 +00002055 // Complex types are all returned as packed integers.
2056 //
2057 // FIXME: Consider using 2 x vector types if the back end handles them
2058 // correctly.
2059 if (RetTy->isAnyComplexType())
2060 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
2061 VMContext, Context.getTypeSize(RetTy)));
2062
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002063 // Integer like structures are returned in r0.
2064 if (isIntegerLikeType(RetTy, Context, VMContext)) {
2065 // Return in the smallest viable integer type.
2066 uint64_t Size = Context.getTypeSize(RetTy);
2067 if (Size <= 8)
2068 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2069 if (Size <= 16)
2070 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
2071 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
2072 }
2073
2074 // Otherwise return in memory.
2075 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002076 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002077
2078 // Otherwise this is an AAPCS variant.
2079
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002080 if (isEmptyRecord(Context, RetTy, true))
2081 return ABIArgInfo::getIgnore();
2082
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002083 // Aggregates <= 4 bytes are returned in r0; other aggregates
2084 // are returned indirectly.
2085 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002086 if (Size <= 32) {
2087 // Return in the smallest viable integer type.
2088 if (Size <= 8)
2089 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
2090 if (Size <= 16)
2091 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002092 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002093 }
2094
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002095 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002096}
2097
2098llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002099 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002100 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002101 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002102 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002103
2104 CGBuilderTy &Builder = CGF.Builder;
2105 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2106 "ap");
2107 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2108 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002109 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002110 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2111
2112 uint64_t Offset =
2113 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2114 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002115 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002116 "ap.next");
2117 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2118
2119 return AddrTyped;
2120}
2121
2122ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00002123 ASTContext &Context,
2124 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002125 if (RetTy->isVoidType()) {
2126 return ABIArgInfo::getIgnore();
2127 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2128 return ABIArgInfo::getIndirect(0);
2129 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +00002130 // Treat an enum type as its underlying type.
2131 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2132 RetTy = EnumTy->getDecl()->getIntegerType();
2133
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002134 return (RetTy->isPromotableIntegerType() ?
2135 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002136 }
2137}
2138
Chris Lattner0cf24192010-06-28 20:05:43 +00002139//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002140// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002141//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002142
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002143namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002144
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002145class SystemZABIInfo : public ABIInfo {
2146 bool isPromotableIntegerType(QualType Ty) const;
2147
2148 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2149 llvm::LLVMContext &VMContext) const;
2150
2151 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2152 llvm::LLVMContext &VMContext) const;
2153
2154 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
Chris Lattner1d7c9f72010-06-29 01:08:48 +00002155 llvm::LLVMContext &VMContext,
2156 const llvm::Type *const *PrefTypes,
2157 unsigned NumPrefTypes) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002158 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2159 Context, VMContext);
2160 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2161 it != ie; ++it)
2162 it->info = classifyArgumentType(it->type, Context, VMContext);
2163 }
2164
2165 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2166 CodeGenFunction &CGF) const;
2167};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002168
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002169class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2170public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002171 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002172};
2173
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002174}
2175
2176bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2177 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002178 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002179 switch (BT->getKind()) {
2180 case BuiltinType::Bool:
2181 case BuiltinType::Char_S:
2182 case BuiltinType::Char_U:
2183 case BuiltinType::SChar:
2184 case BuiltinType::UChar:
2185 case BuiltinType::Short:
2186 case BuiltinType::UShort:
2187 case BuiltinType::Int:
2188 case BuiltinType::UInt:
2189 return true;
2190 default:
2191 return false;
2192 }
2193 return false;
2194}
2195
2196llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2197 CodeGenFunction &CGF) const {
2198 // FIXME: Implement
2199 return 0;
2200}
2201
2202
2203ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2204 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002205 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002206 if (RetTy->isVoidType()) {
2207 return ABIArgInfo::getIgnore();
2208 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2209 return ABIArgInfo::getIndirect(0);
2210 } else {
2211 return (isPromotableIntegerType(RetTy) ?
2212 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2213 }
2214}
2215
2216ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2217 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002218 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002219 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2220 return ABIArgInfo::getIndirect(0);
2221 } else {
2222 return (isPromotableIntegerType(Ty) ?
2223 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2224 }
2225}
2226
Chris Lattner0cf24192010-06-28 20:05:43 +00002227//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002228// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002229//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002230
2231namespace {
2232
2233class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2234public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002235 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002236 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2237 CodeGen::CodeGenModule &M) const;
2238};
2239
2240}
2241
2242void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2243 llvm::GlobalValue *GV,
2244 CodeGen::CodeGenModule &M) const {
2245 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2246 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2247 // Handle 'interrupt' attribute:
2248 llvm::Function *F = cast<llvm::Function>(GV);
2249
2250 // Step 1: Set ISR calling convention.
2251 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2252
2253 // Step 2: Add attributes goodness.
2254 F->addFnAttr(llvm::Attribute::NoInline);
2255
2256 // Step 3: Emit ISR vector alias.
2257 unsigned Num = attr->getNumber() + 0xffe0;
2258 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2259 "vector_" +
2260 llvm::LowercaseString(llvm::utohexstr(Num)),
2261 GV, &M.getModule());
2262 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002263 }
2264}
2265
Chris Lattner0cf24192010-06-28 20:05:43 +00002266//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002267// MIPS ABI Implementation. This works for both little-endian and
2268// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002269//===----------------------------------------------------------------------===//
2270
John McCall943fae92010-05-27 06:19:26 +00002271namespace {
2272class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2273public:
2274 MIPSTargetCodeGenInfo(): TargetCodeGenInfo(new DefaultABIInfo()) {}
2275
2276 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2277 return 29;
2278 }
2279
2280 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2281 llvm::Value *Address) const;
2282};
2283}
2284
2285bool
2286MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2287 llvm::Value *Address) const {
2288 // This information comes from gcc's implementation, which seems to
2289 // as canonical as it gets.
2290
2291 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2292 llvm::LLVMContext &Context = CGF.getLLVMContext();
2293
2294 // Everything on MIPS is 4 bytes. Double-precision FP registers
2295 // are aliased to pairs of single-precision FP registers.
2296 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2297 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2298
2299 // 0-31 are the general purpose registers, $0 - $31.
2300 // 32-63 are the floating-point registers, $f0 - $f31.
2301 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2302 // 66 is the (notional, I think) register for signal-handler return.
2303 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2304
2305 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2306 // They are one bit wide and ignored here.
2307
2308 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2309 // (coprocessor 1 is the FP unit)
2310 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2311 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2312 // 176-181 are the DSP accumulator registers.
2313 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2314
2315 return false;
2316}
2317
2318
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002319const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2320 if (TheTargetCodeGenInfo)
2321 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002322
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002323 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2324 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002325
Chris Lattner22a931e2010-06-29 06:01:59 +00002326 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002327 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002328 default:
Chris Lattner22a931e2010-06-29 06:01:59 +00002329 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00002330
John McCall943fae92010-05-27 06:19:26 +00002331 case llvm::Triple::mips:
2332 case llvm::Triple::mipsel:
2333 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo());
2334
Daniel Dunbard59655c2009-09-12 00:59:49 +00002335 case llvm::Triple::arm:
2336 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002337 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002338 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002339 return *(TheTargetCodeGenInfo =
2340 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002341
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002342 return *(TheTargetCodeGenInfo =
2343 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002344
2345 case llvm::Triple::pic16:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002346 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002347
John McCallea8d8bb2010-03-11 00:10:12 +00002348 case llvm::Triple::ppc:
2349 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2350
Daniel Dunbard59655c2009-09-12 00:59:49 +00002351 case llvm::Triple::systemz:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002352 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2353
2354 case llvm::Triple::msp430:
2355 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002356
Daniel Dunbar40165182009-08-24 09:10:05 +00002357 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002358 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002359 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002360 return *(TheTargetCodeGenInfo =
2361 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002362 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002363 case llvm::Triple::MinGW32:
2364 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002365 case llvm::Triple::AuroraUX:
2366 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002367 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002368 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002369 return *(TheTargetCodeGenInfo =
2370 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002371
2372 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002373 return *(TheTargetCodeGenInfo =
2374 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002375 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002376
Daniel Dunbare3532f82009-08-24 08:52:16 +00002377 case llvm::Triple::x86_64:
Chris Lattner22a931e2010-06-29 06:01:59 +00002378 return *(TheTargetCodeGenInfo =
2379 new X86_64TargetCodeGenInfo(Context, TheTargetData));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002380 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002381}