blob: bd88192856cbf70044c13d62d40de0a2d76ec2f9 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-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 Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
John McCallaeeb7012010-05-27 06:19:26 +000026static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
27 llvm::Value *Array,
28 llvm::Value *Value,
29 unsigned FirstIndex,
30 unsigned LastIndex) {
31 // Alternatively, we could emit this as a loop in the source.
32 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
33 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
34 Builder.CreateStore(Value, Cell);
35 }
36}
37
John McCalld608cdb2010-08-22 10:59:02 +000038static bool isAggregateTypeForABI(QualType T) {
39 return CodeGenFunction::hasAggregateLLVMType(T) ||
40 T->isMemberFunctionPointerType();
41}
42
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000043ABIInfo::~ABIInfo() {}
44
Chris Lattnerea044322010-07-29 02:01:43 +000045ASTContext &ABIInfo::getContext() const {
46 return CGT.getContext();
47}
48
49llvm::LLVMContext &ABIInfo::getVMContext() const {
50 return CGT.getLLVMContext();
51}
52
53const llvm::TargetData &ABIInfo::getTargetData() const {
54 return CGT.getTargetData();
55}
56
57
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000058void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000059 llvm::raw_ostream &OS = llvm::errs();
60 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000061 switch (TheKind) {
62 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000063 OS << "Direct Type=";
64 if (const llvm::Type *Ty = getCoerceToType())
65 Ty->print(OS);
66 else
67 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000068 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000069 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000070 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000071 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000072 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000073 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000074 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000076 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000077 << " Byal=" << getIndirectByVal()
78 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000079 break;
80 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000081 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000082 break;
83 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000084 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000085}
86
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000087TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
88
Daniel Dunbar98303b92009-09-13 08:03:58 +000089static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000090
91/// isEmptyField - Return true iff a the field is "empty", that is it
92/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000093static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
94 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000095 if (FD->isUnnamedBitfield())
96 return true;
97
98 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000099
Daniel Dunbar98303b92009-09-13 08:03:58 +0000100 // Constant arrays of empty records count as empty, strip them off.
101 if (AllowArrays)
102 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
103 FT = AT->getElementType();
104
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000105 const RecordType *RT = FT->getAs<RecordType>();
106 if (!RT)
107 return false;
108
109 // C++ record fields are never empty, at least in the Itanium ABI.
110 //
111 // FIXME: We should use a predicate for whether this behavior is true in the
112 // current ABI.
113 if (isa<CXXRecordDecl>(RT->getDecl()))
114 return false;
115
Daniel Dunbar98303b92009-09-13 08:03:58 +0000116 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000117}
118
119/// isEmptyRecord - Return true iff a structure contains only empty
120/// fields. Note that a structure with a flexible array member is not
121/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000122static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000123 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000124 if (!RT)
125 return 0;
126 const RecordDecl *RD = RT->getDecl();
127 if (RD->hasFlexibleArrayMember())
128 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000129
130 // If this is a C++ record, check the bases first.
131 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
132 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
133 e = CXXRD->bases_end(); i != e; ++i)
134 if (!isEmptyRecord(Context, i->getType(), true))
135 return false;
136
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000137 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
138 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000139 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000140 return false;
141 return true;
142}
143
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000144/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
145/// a non-trivial destructor or a non-trivial copy constructor.
146static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
147 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
148 if (!RD)
149 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000150
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000151 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
152}
153
154/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
155/// a record type with either a non-trivial destructor or a non-trivial copy
156/// constructor.
157static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
158 const RecordType *RT = T->getAs<RecordType>();
159 if (!RT)
160 return false;
161
162 return hasNonTrivialDestructorOrCopyConstructor(RT);
163}
164
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165/// isSingleElementStruct - Determine if a structure is a "single
166/// element struct", i.e. it has exactly one non-empty field or
167/// exactly one field which is itself a single element
168/// struct. Structures with flexible array members are never
169/// considered single element structs.
170///
171/// \return The field declaration for the single non-empty field, if
172/// it exists.
173static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
174 const RecordType *RT = T->getAsStructureType();
175 if (!RT)
176 return 0;
177
178 const RecordDecl *RD = RT->getDecl();
179 if (RD->hasFlexibleArrayMember())
180 return 0;
181
182 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000183
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000184 // If this is a C++ record, check the bases first.
185 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
186 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
187 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000188 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000189 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000190 continue;
191
192 // If we already found an element then this isn't a single-element struct.
193 if (Found)
194 return 0;
195
196 // If this is non-empty and not a single element struct, the composite
197 // cannot be a single element struct.
198 Found = isSingleElementStruct(i->getType(), Context);
199 if (!Found)
200 return 0;
201 }
202 }
203
204 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000205 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
206 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000207 const FieldDecl *FD = *i;
208 QualType FT = FD->getType();
209
210 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000211 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000212 continue;
213
214 // If we already found an element then this isn't a single-element
215 // struct.
216 if (Found)
217 return 0;
218
219 // Treat single element arrays as the element.
220 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
221 if (AT->getSize().getZExtValue() != 1)
222 break;
223 FT = AT->getElementType();
224 }
225
John McCalld608cdb2010-08-22 10:59:02 +0000226 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000227 Found = FT.getTypePtr();
228 } else {
229 Found = isSingleElementStruct(FT, Context);
230 if (!Found)
231 return 0;
232 }
233 }
234
235 return Found;
236}
237
238static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000239 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000240 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
241 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000242 return false;
243
244 uint64_t Size = Context.getTypeSize(Ty);
245 return Size == 32 || Size == 64;
246}
247
Daniel Dunbar53012f42009-11-09 01:33:53 +0000248/// canExpandIndirectArgument - Test whether an argument type which is to be
249/// passed indirectly (on the stack) would have the equivalent layout if it was
250/// expanded into separate arguments. If so, we prefer to do the latter to avoid
251/// inhibiting optimizations.
252///
253// FIXME: This predicate is missing many cases, currently it just follows
254// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
255// should probably make this smarter, or better yet make the LLVM backend
256// capable of handling it.
257static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
258 // We can only expand structure types.
259 const RecordType *RT = Ty->getAs<RecordType>();
260 if (!RT)
261 return false;
262
263 // We can only expand (C) structures.
264 //
265 // FIXME: This needs to be generalized to handle classes as well.
266 const RecordDecl *RD = RT->getDecl();
267 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
268 return false;
269
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 const FieldDecl *FD = *i;
273
274 if (!is32Or64BitBasicType(FD->getType(), Context))
275 return false;
276
277 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
278 // how to expand them yet, and the predicate for telling if a bitfield still
279 // counts as "basic" is more complicated than what we were doing previously.
280 if (FD->isBitField())
281 return false;
282 }
283
284 return true;
285}
286
287namespace {
288/// DefaultABIInfo - The default implementation for ABI specific
289/// details. This implementation provides information which results in
290/// self-consistent and sensible LLVM IR generation, but does not
291/// conform to any particular ABI.
292class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000293public:
294 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000295
Chris Lattnera3c109b2010-07-29 02:16:43 +0000296 ABIArgInfo classifyReturnType(QualType RetTy) const;
297 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000298
Chris Lattneree5dcd02010-07-29 02:31:05 +0000299 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000300 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000301 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
302 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000303 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000304 }
305
306 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
307 CodeGenFunction &CGF) const;
308};
309
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000310class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
311public:
Chris Lattnerea044322010-07-29 02:01:43 +0000312 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
313 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000314};
315
316llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
317 CodeGenFunction &CGF) const {
318 return 0;
319}
320
Chris Lattnera3c109b2010-07-29 02:16:43 +0000321ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000322 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000323 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000324
Chris Lattnera14db752010-03-11 18:19:55 +0000325 // Treat an enum type as its underlying type.
326 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
327 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000328
Chris Lattnera14db752010-03-11 18:19:55 +0000329 return (Ty->isPromotableIntegerType() ?
330 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000331}
332
Bob Wilson0024f942011-01-10 23:54:17 +0000333ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
334 if (RetTy->isVoidType())
335 return ABIArgInfo::getIgnore();
336
337 if (isAggregateTypeForABI(RetTy))
338 return ABIArgInfo::getIndirect(0);
339
340 // Treat an enum type as its underlying type.
341 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
342 RetTy = EnumTy->getDecl()->getIntegerType();
343
344 return (RetTy->isPromotableIntegerType() ?
345 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
346}
347
Bill Wendlingbb465d72010-10-18 03:41:31 +0000348/// UseX86_MMXType - Return true if this is an MMX type that should use the special
349/// x86_mmx type.
350bool UseX86_MMXType(const llvm::Type *IRType) {
351 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
352 // special x86_mmx type.
353 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
354 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
355 IRType->getScalarSizeInBits() != 64;
356}
357
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000358static const llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
359 llvm::StringRef Constraint,
360 const llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000361 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000362 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
363 return Ty;
364}
365
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000366//===----------------------------------------------------------------------===//
367// X86-32 ABI Implementation
368//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000369
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000370/// X86_32ABIInfo - The X86-32 ABI information.
371class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000372 static const unsigned MinABIStackAlignInBytes = 4;
373
David Chisnall1e4249c2009-08-17 23:08:21 +0000374 bool IsDarwinVectorABI;
375 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000376
377 static bool isRegisterSize(unsigned Size) {
378 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
379 }
380
381 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
382
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000383 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
384 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000385 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000386
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000387 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000388 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000389
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000390public:
Chris Lattnerea044322010-07-29 02:01:43 +0000391
Chris Lattnera3c109b2010-07-29 02:16:43 +0000392 ABIArgInfo classifyReturnType(QualType RetTy) const;
393 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000394
Chris Lattneree5dcd02010-07-29 02:31:05 +0000395 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000396 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000397 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
398 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000399 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000400 }
401
402 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
403 CodeGenFunction &CGF) const;
404
Chris Lattnerea044322010-07-29 02:01:43 +0000405 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
406 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000407};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000408
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000409class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
410public:
Chris Lattnerea044322010-07-29 02:01:43 +0000411 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
412 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000413
414 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
415 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000416
417 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
418 // Darwin uses different dwarf register numbers for EH.
419 if (CGM.isTargetDarwin()) return 5;
420
421 return 4;
422 }
423
424 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
425 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000426
427 const llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
428 llvm::StringRef Constraint,
429 const llvm::Type* Ty) const {
430 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
431 }
432
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000433};
434
435}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000436
437/// shouldReturnTypeInRegister - Determine if the given type should be
438/// passed in a register (for the Darwin ABI).
439bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
440 ASTContext &Context) {
441 uint64_t Size = Context.getTypeSize(Ty);
442
443 // Type must be register sized.
444 if (!isRegisterSize(Size))
445 return false;
446
447 if (Ty->isVectorType()) {
448 // 64- and 128- bit vectors inside structures are not returned in
449 // registers.
450 if (Size == 64 || Size == 128)
451 return false;
452
453 return true;
454 }
455
Daniel Dunbar77115232010-05-15 00:00:30 +0000456 // If this is a builtin, pointer, enum, complex type, member pointer, or
457 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000458 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000459 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000460 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000461 return true;
462
463 // Arrays are treated like records.
464 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
465 return shouldReturnTypeInRegister(AT->getElementType(), Context);
466
467 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000468 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000469 if (!RT) return false;
470
Anders Carlssona8874232010-01-27 03:25:19 +0000471 // FIXME: Traverse bases here too.
472
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000473 // Structure types are passed in register if all fields would be
474 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000475 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
476 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000477 const FieldDecl *FD = *i;
478
479 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000480 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000481 continue;
482
483 // Check fields recursively.
484 if (!shouldReturnTypeInRegister(FD->getType(), Context))
485 return false;
486 }
487
488 return true;
489}
490
Chris Lattnera3c109b2010-07-29 02:16:43 +0000491ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
492 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000493 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000494
Chris Lattnera3c109b2010-07-29 02:16:43 +0000495 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000496 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000497 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000498 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000499
500 // 128-bit vectors are a special case; they are returned in
501 // registers and we need to make sure to pick a type the LLVM
502 // backend will like.
503 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000504 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000505 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000506
507 // Always return in register if it fits in a general purpose
508 // register, or if it is 64 bits and has a single element.
509 if ((Size == 8 || Size == 16 || Size == 32) ||
510 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000511 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000512 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000513
514 return ABIArgInfo::getIndirect(0);
515 }
516
517 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000518 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000519
John McCalld608cdb2010-08-22 10:59:02 +0000520 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000521 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000522 // Structures with either a non-trivial destructor or a non-trivial
523 // copy constructor are always indirect.
524 if (hasNonTrivialDestructorOrCopyConstructor(RT))
525 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000526
Anders Carlsson40092972009-10-20 22:07:59 +0000527 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000528 if (RT->getDecl()->hasFlexibleArrayMember())
529 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000530 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000531
David Chisnall1e4249c2009-08-17 23:08:21 +0000532 // If specified, structs and unions are always indirect.
533 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000534 return ABIArgInfo::getIndirect(0);
535
536 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000537 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000538 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000539 if (BT->isIntegerType()) {
540 // We need to use the size of the structure, padding
541 // bit-fields can adjust that to be larger than the single
542 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000543 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000544 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000545 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
546 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000547
Chris Lattnera3c109b2010-07-29 02:16:43 +0000548 if (BT->getKind() == BuiltinType::Float) {
549 assert(getContext().getTypeSize(RetTy) ==
550 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000551 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000552 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000553 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000554
Chris Lattnera3c109b2010-07-29 02:16:43 +0000555 if (BT->getKind() == BuiltinType::Double) {
556 assert(getContext().getTypeSize(RetTy) ==
557 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000558 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000559 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000560 }
561 } else if (SeltTy->isPointerType()) {
562 // FIXME: It would be really nice if this could come out as the proper
563 // pointer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000564 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000565 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000566 } else if (SeltTy->isVectorType()) {
567 // 64- and 128-bit vectors are never returned in a
568 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000569 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000570 if (Size == 64 || Size == 128)
571 return ABIArgInfo::getIndirect(0);
572
Chris Lattnera3c109b2010-07-29 02:16:43 +0000573 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000574 }
575 }
576
577 // Small structures which are register sized are generally returned
578 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000579 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
580 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000581 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000582 }
583
584 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000585 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000586
Chris Lattnera3c109b2010-07-29 02:16:43 +0000587 // Treat an enum type as its underlying type.
588 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
589 RetTy = EnumTy->getDecl()->getIntegerType();
590
591 return (RetTy->isPromotableIntegerType() ?
592 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000593}
594
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000595static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
596 const RecordType *RT = Ty->getAs<RecordType>();
597 if (!RT)
598 return 0;
599 const RecordDecl *RD = RT->getDecl();
600
601 // If this is a C++ record, check the bases first.
602 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
603 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
604 e = CXXRD->bases_end(); i != e; ++i)
605 if (!isRecordWithSSEVectorType(Context, i->getType()))
606 return false;
607
608 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
609 i != e; ++i) {
610 QualType FT = i->getType();
611
612 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
613 return true;
614
615 if (isRecordWithSSEVectorType(Context, FT))
616 return true;
617 }
618
619 return false;
620}
621
Daniel Dunbare59d8582010-09-16 20:42:06 +0000622unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
623 unsigned Align) const {
624 // Otherwise, if the alignment is less than or equal to the minimum ABI
625 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000626 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000627 return 0; // Use default alignment.
628
629 // On non-Darwin, the stack type alignment is always 4.
630 if (!IsDarwinVectorABI) {
631 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000632 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000633 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000634
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000635 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
636 if (isRecordWithSSEVectorType(getContext(), Ty))
637 return 16;
638
639 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000640}
641
Chris Lattnera3c109b2010-07-29 02:16:43 +0000642ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000643 if (!ByVal)
644 return ABIArgInfo::getIndirect(0, false);
645
Daniel Dunbare59d8582010-09-16 20:42:06 +0000646 // Compute the byval alignment.
647 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
648 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
649 if (StackAlign == 0)
650 return ABIArgInfo::getIndirect(0);
651
652 // If the stack alignment is less than the type alignment, realign the
653 // argument.
654 if (StackAlign < TypeAlign)
655 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
656 /*Realign=*/true);
657
658 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000659}
660
Chris Lattnera3c109b2010-07-29 02:16:43 +0000661ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000662 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000663 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000664 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000665 if (const RecordType *RT = Ty->getAs<RecordType>()) {
666 // Structures with either a non-trivial destructor or a non-trivial
667 // copy constructor are always indirect.
668 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000669 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000670
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000671 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000672 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000673 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000674
675 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000676 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677 return ABIArgInfo::getIgnore();
678
Daniel Dunbar53012f42009-11-09 01:33:53 +0000679 // Expand small (<= 128-bit) record types when we know that the stack layout
680 // of those arguments will match the struct. This is important because the
681 // LLVM backend isn't smart enough to remove byval, which inhibits many
682 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000683 if (getContext().getTypeSize(Ty) <= 4*32 &&
684 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000685 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000686
Chris Lattnera3c109b2010-07-29 02:16:43 +0000687 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000688 }
689
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000690 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000691 // On Darwin, some vectors are passed in memory, we handle this by passing
692 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000693 if (IsDarwinVectorABI) {
694 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000695 if ((Size == 8 || Size == 16 || Size == 32) ||
696 (Size == 64 && VT->getNumElements() == 1))
697 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
698 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000699 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000700
701 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
702 if (UseX86_MMXType(IRType)) {
703 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
704 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
705 return AAI;
706 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000707
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000708 return ABIArgInfo::getDirect();
709 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000710
711
Chris Lattnera3c109b2010-07-29 02:16:43 +0000712 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
713 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000714
Chris Lattnera3c109b2010-07-29 02:16:43 +0000715 return (Ty->isPromotableIntegerType() ?
716 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000717}
718
719llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
720 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000721 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000722 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000723
724 CGBuilderTy &Builder = CGF.Builder;
725 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
726 "ap");
727 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
728 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000729 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000730 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
731
732 uint64_t Offset =
733 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
734 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000735 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000736 "ap.next");
737 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
738
739 return AddrTyped;
740}
741
Charles Davis74f72932010-02-13 15:54:06 +0000742void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
743 llvm::GlobalValue *GV,
744 CodeGen::CodeGenModule &CGM) const {
745 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
746 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
747 // Get the LLVM function.
748 llvm::Function *Fn = cast<llvm::Function>(GV);
749
750 // Now add the 'alignstack' attribute with a value of 16.
751 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
752 }
753 }
754}
755
John McCall6374c332010-03-06 00:35:14 +0000756bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
757 CodeGen::CodeGenFunction &CGF,
758 llvm::Value *Address) const {
759 CodeGen::CGBuilderTy &Builder = CGF.Builder;
760 llvm::LLVMContext &Context = CGF.getLLVMContext();
761
762 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
763 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000764
John McCall6374c332010-03-06 00:35:14 +0000765 // 0-7 are the eight integer registers; the order is different
766 // on Darwin (for EH), but the range is the same.
767 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000768 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000769
770 if (CGF.CGM.isTargetDarwin()) {
771 // 12-16 are st(0..4). Not sure why we stop at 4.
772 // These have size 16, which is sizeof(long double) on
773 // platforms with 8-byte alignment for that type.
774 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000775 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000776
John McCall6374c332010-03-06 00:35:14 +0000777 } else {
778 // 9 is %eflags, which doesn't get a size on Darwin for some
779 // reason.
780 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
781
782 // 11-16 are st(0..5). Not sure why we stop at 5.
783 // These have size 12, which is sizeof(long double) on
784 // platforms with 4-byte alignment for that type.
785 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000786 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
787 }
John McCall6374c332010-03-06 00:35:14 +0000788
789 return false;
790}
791
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000792//===----------------------------------------------------------------------===//
793// X86-64 ABI Implementation
794//===----------------------------------------------------------------------===//
795
796
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000797namespace {
798/// X86_64ABIInfo - The X86_64 ABI information.
799class X86_64ABIInfo : public ABIInfo {
800 enum Class {
801 Integer = 0,
802 SSE,
803 SSEUp,
804 X87,
805 X87Up,
806 ComplexX87,
807 NoClass,
808 Memory
809 };
810
811 /// merge - Implement the X86_64 ABI merging algorithm.
812 ///
813 /// Merge an accumulating classification \arg Accum with a field
814 /// classification \arg Field.
815 ///
816 /// \param Accum - The accumulating classification. This should
817 /// always be either NoClass or the result of a previous merge
818 /// call. In addition, this should never be Memory (the caller
819 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000820 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000821
822 /// classify - Determine the x86_64 register classes in which the
823 /// given type T should be passed.
824 ///
825 /// \param Lo - The classification for the parts of the type
826 /// residing in the low word of the containing object.
827 ///
828 /// \param Hi - The classification for the parts of the type
829 /// residing in the high word of the containing object.
830 ///
831 /// \param OffsetBase - The bit offset of this type in the
832 /// containing object. Some parameters are classified different
833 /// depending on whether they straddle an eightbyte boundary.
834 ///
835 /// If a word is unused its result will be NoClass; if a type should
836 /// be passed in Memory then at least the classification of \arg Lo
837 /// will be Memory.
838 ///
839 /// The \arg Lo class will be NoClass iff the argument is ignored.
840 ///
841 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
842 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000843 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000844
Chris Lattner0f408f52010-07-29 04:56:46 +0000845 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattner603519d2010-07-29 17:49:08 +0000846 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattnerf47c9442010-07-29 18:13:09 +0000847 unsigned IROffset, QualType SourceTy,
848 unsigned SourceOffset) const;
Chris Lattner0d2656d2010-07-29 17:40:35 +0000849 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
850 unsigned IROffset, QualType SourceTy,
851 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000852
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000853 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000854 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000855 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000856
857 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000858 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000859 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000860
Chris Lattnera3c109b2010-07-29 02:16:43 +0000861 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862
Bill Wendlingbb465d72010-10-18 03:41:31 +0000863 ABIArgInfo classifyArgumentType(QualType Ty,
864 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000865 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000866
867public:
Chris Lattnerea044322010-07-29 02:01:43 +0000868 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000869
Chris Lattneree5dcd02010-07-29 02:31:05 +0000870 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000871
872 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
873 CodeGenFunction &CGF) const;
874};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000875
Chris Lattnerf13721d2010-08-31 16:44:54 +0000876/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000877class WinX86_64ABIInfo : public ABIInfo {
878
879 ABIArgInfo classify(QualType Ty) const;
880
Chris Lattnerf13721d2010-08-31 16:44:54 +0000881public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000882 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
883
884 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000885
886 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
887 CodeGenFunction &CGF) const;
888};
889
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000890class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
891public:
Chris Lattnerea044322010-07-29 02:01:43 +0000892 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
893 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000894
895 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
896 return 7;
897 }
898
899 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
900 llvm::Value *Address) const {
901 CodeGen::CGBuilderTy &Builder = CGF.Builder;
902 llvm::LLVMContext &Context = CGF.getLLVMContext();
903
904 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
905 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000906
John McCallaeeb7012010-05-27 06:19:26 +0000907 // 0-15 are the 16 integer registers.
908 // 16 is %rip.
909 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000910
911 return false;
912 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000913
914 const llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
915 llvm::StringRef Constraint,
916 const llvm::Type* Ty) const {
917 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
918 }
919
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000920};
921
Chris Lattnerf13721d2010-08-31 16:44:54 +0000922class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
923public:
924 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
925 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
926
927 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
928 return 7;
929 }
930
931 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
932 llvm::Value *Address) const {
933 CodeGen::CGBuilderTy &Builder = CGF.Builder;
934 llvm::LLVMContext &Context = CGF.getLLVMContext();
935
936 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
937 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000938
Chris Lattnerf13721d2010-08-31 16:44:54 +0000939 // 0-15 are the 16 integer registers.
940 // 16 is %rip.
941 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
942
943 return false;
944 }
945};
946
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000947}
948
Chris Lattner1090a9b2010-06-28 21:43:59 +0000949X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000950 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
951 // classified recursively so that always two fields are
952 // considered. The resulting class is calculated according to
953 // the classes of the fields in the eightbyte:
954 //
955 // (a) If both classes are equal, this is the resulting class.
956 //
957 // (b) If one of the classes is NO_CLASS, the resulting class is
958 // the other class.
959 //
960 // (c) If one of the classes is MEMORY, the result is the MEMORY
961 // class.
962 //
963 // (d) If one of the classes is INTEGER, the result is the
964 // INTEGER.
965 //
966 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
967 // MEMORY is used as class.
968 //
969 // (f) Otherwise class SSE is used.
970
971 // Accum should never be memory (we should have returned) or
972 // ComplexX87 (because this cannot be passed in a structure).
973 assert((Accum != Memory && Accum != ComplexX87) &&
974 "Invalid accumulated classification during merge.");
975 if (Accum == Field || Field == NoClass)
976 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000977 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000978 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000979 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000980 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000981 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000982 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000983 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
984 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000985 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000986 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000987}
988
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000989void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000990 Class &Lo, Class &Hi) const {
991 // FIXME: This code can be simplified by introducing a simple value class for
992 // Class pairs with appropriate constructor methods for the various
993 // situations.
994
995 // FIXME: Some of the split computations are wrong; unaligned vectors
996 // shouldn't be passed in registers for example, so there is no chance they
997 // can straddle an eightbyte. Verify & simplify.
998
999 Lo = Hi = NoClass;
1000
1001 Class &Current = OffsetBase < 64 ? Lo : Hi;
1002 Current = Memory;
1003
John McCall183700f2009-09-21 23:43:11 +00001004 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001005 BuiltinType::Kind k = BT->getKind();
1006
1007 if (k == BuiltinType::Void) {
1008 Current = NoClass;
1009 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1010 Lo = Integer;
1011 Hi = Integer;
1012 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1013 Current = Integer;
1014 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1015 Current = SSE;
1016 } else if (k == BuiltinType::LongDouble) {
1017 Lo = X87;
1018 Hi = X87Up;
1019 }
1020 // FIXME: _Decimal32 and _Decimal64 are SSE.
1021 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001022 return;
1023 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001024
Chris Lattner1090a9b2010-06-28 21:43:59 +00001025 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001026 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001027 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001028 return;
1029 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001030
Chris Lattner1090a9b2010-06-28 21:43:59 +00001031 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001032 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001033 return;
1034 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001035
Chris Lattner1090a9b2010-06-28 21:43:59 +00001036 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001037 if (Ty->isMemberFunctionPointerType())
1038 Lo = Hi = Integer;
1039 else
1040 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001041 return;
1042 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001043
Chris Lattner1090a9b2010-06-28 21:43:59 +00001044 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001045 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001046 if (Size == 32) {
1047 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1048 // float> as integer.
1049 Current = Integer;
1050
1051 // If this type crosses an eightbyte boundary, it should be
1052 // split.
1053 uint64_t EB_Real = (OffsetBase) / 64;
1054 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1055 if (EB_Real != EB_Imag)
1056 Hi = Lo;
1057 } else if (Size == 64) {
1058 // gcc passes <1 x double> in memory. :(
1059 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1060 return;
1061
1062 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001063 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001064 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1065 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1066 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001067 Current = Integer;
1068 else
1069 Current = SSE;
1070
1071 // If this type crosses an eightbyte boundary, it should be
1072 // split.
1073 if (OffsetBase && OffsetBase != 64)
1074 Hi = Lo;
1075 } else if (Size == 128) {
1076 Lo = SSE;
1077 Hi = SSEUp;
1078 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001079 return;
1080 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001081
Chris Lattner1090a9b2010-06-28 21:43:59 +00001082 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001083 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001084
Chris Lattnerea044322010-07-29 02:01:43 +00001085 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001086 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001087 if (Size <= 64)
1088 Current = Integer;
1089 else if (Size <= 128)
1090 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001091 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001092 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001093 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001094 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001095 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001096 Current = ComplexX87;
1097
1098 // If this complex type crosses an eightbyte boundary then it
1099 // should be split.
1100 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001101 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001102 if (Hi == NoClass && EB_Real != EB_Imag)
1103 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001104
Chris Lattner1090a9b2010-06-28 21:43:59 +00001105 return;
1106 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001107
Chris Lattnerea044322010-07-29 02:01:43 +00001108 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001109 // Arrays are treated like structures.
1110
Chris Lattnerea044322010-07-29 02:01:43 +00001111 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001112
1113 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1114 // than two eightbytes, ..., it has class MEMORY.
1115 if (Size > 128)
1116 return;
1117
1118 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1119 // fields, it has class MEMORY.
1120 //
1121 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001122 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001123 return;
1124
1125 // Otherwise implement simplified merge. We could be smarter about
1126 // this, but it isn't worth it and would be harder to verify.
1127 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001128 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001129 uint64_t ArraySize = AT->getSize().getZExtValue();
1130 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1131 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001132 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001133 Lo = merge(Lo, FieldLo);
1134 Hi = merge(Hi, FieldHi);
1135 if (Lo == Memory || Hi == Memory)
1136 break;
1137 }
1138
1139 // Do post merger cleanup (see below). Only case we worry about is Memory.
1140 if (Hi == Memory)
1141 Lo = Memory;
1142 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001143 return;
1144 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001145
Chris Lattner1090a9b2010-06-28 21:43:59 +00001146 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001147 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001148
1149 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1150 // than two eightbytes, ..., it has class MEMORY.
1151 if (Size > 128)
1152 return;
1153
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001154 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1155 // copy constructor or a non-trivial destructor, it is passed by invisible
1156 // reference.
1157 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1158 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001159
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001160 const RecordDecl *RD = RT->getDecl();
1161
1162 // Assume variable sized types are passed in memory.
1163 if (RD->hasFlexibleArrayMember())
1164 return;
1165
Chris Lattnerea044322010-07-29 02:01:43 +00001166 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001167
1168 // Reset Lo class, this will be recomputed.
1169 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001170
1171 // If this is a C++ record, classify the bases first.
1172 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1173 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1174 e = CXXRD->bases_end(); i != e; ++i) {
1175 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1176 "Unexpected base class!");
1177 const CXXRecordDecl *Base =
1178 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1179
1180 // Classify this field.
1181 //
1182 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1183 // single eightbyte, each is classified separately. Each eightbyte gets
1184 // initialized to class NO_CLASS.
1185 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001186 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001187 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001188 Lo = merge(Lo, FieldLo);
1189 Hi = merge(Hi, FieldHi);
1190 if (Lo == Memory || Hi == Memory)
1191 break;
1192 }
1193 }
1194
1195 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001196 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001197 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1198 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001199 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1200 bool BitField = i->isBitField();
1201
1202 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1203 // fields, it has class MEMORY.
1204 //
1205 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001206 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001207 Lo = Memory;
1208 return;
1209 }
1210
1211 // Classify this field.
1212 //
1213 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1214 // exceeds a single eightbyte, each is classified
1215 // separately. Each eightbyte gets initialized to class
1216 // NO_CLASS.
1217 Class FieldLo, FieldHi;
1218
1219 // Bit-fields require special handling, they do not force the
1220 // structure to be passed in memory even if unaligned, and
1221 // therefore they can straddle an eightbyte.
1222 if (BitField) {
1223 // Ignore padding bit-fields.
1224 if (i->isUnnamedBitfield())
1225 continue;
1226
1227 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001228 uint64_t Size =
1229 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001230
1231 uint64_t EB_Lo = Offset / 64;
1232 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1233 FieldLo = FieldHi = NoClass;
1234 if (EB_Lo) {
1235 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1236 FieldLo = NoClass;
1237 FieldHi = Integer;
1238 } else {
1239 FieldLo = Integer;
1240 FieldHi = EB_Hi ? Integer : NoClass;
1241 }
1242 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001243 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001244 Lo = merge(Lo, FieldLo);
1245 Hi = merge(Hi, FieldHi);
1246 if (Lo == Memory || Hi == Memory)
1247 break;
1248 }
1249
1250 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1251 //
1252 // (a) If one of the classes is MEMORY, the whole argument is
1253 // passed in memory.
1254 //
1255 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1256
1257 // The first of these conditions is guaranteed by how we implement
1258 // the merge (just bail).
1259 //
1260 // The second condition occurs in the case of unions; for example
1261 // union { _Complex double; unsigned; }.
1262 if (Hi == Memory)
1263 Lo = Memory;
1264 if (Hi == SSEUp && Lo != SSE)
1265 Hi = SSE;
1266 }
1267}
1268
Chris Lattner9c254f02010-06-29 06:01:59 +00001269ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001270 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1271 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001272 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001273 // Treat an enum type as its underlying type.
1274 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1275 Ty = EnumTy->getDecl()->getIntegerType();
1276
1277 return (Ty->isPromotableIntegerType() ?
1278 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1279 }
1280
1281 return ABIArgInfo::getIndirect(0);
1282}
1283
Chris Lattner9c254f02010-06-29 06:01:59 +00001284ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001285 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1286 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001287 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001288 // Treat an enum type as its underlying type.
1289 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1290 Ty = EnumTy->getDecl()->getIntegerType();
1291
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001292 return (Ty->isPromotableIntegerType() ?
1293 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001294 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001295
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001296 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1297 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001298
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001299 // Compute the byval alignment. We trust the back-end to honor the
1300 // minimum ABI alignment for byval, to make cleaner IR.
1301 const unsigned MinABIAlign = 8;
Chris Lattnerea044322010-07-29 02:01:43 +00001302 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001303 if (Align > MinABIAlign)
1304 return ABIArgInfo::getIndirect(Align);
1305 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306}
1307
Chris Lattner0f408f52010-07-29 04:56:46 +00001308/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1309/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1310/// vector register.
1311const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner15842bd2010-07-29 05:02:29 +00001312 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001313
Chris Lattner15842bd2010-07-29 05:02:29 +00001314 // Wrapper structs that just contain vectors are passed just like vectors,
1315 // strip them off if present.
1316 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1317 while (STy && STy->getNumElements() == 1) {
1318 IRType = STy->getElementType(0);
1319 STy = dyn_cast<llvm::StructType>(IRType);
1320 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001321
Chris Lattner0f408f52010-07-29 04:56:46 +00001322 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner15842bd2010-07-29 05:02:29 +00001323 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner0f408f52010-07-29 04:56:46 +00001324 const llvm::Type *EltTy = VT->getElementType();
1325 if (VT->getBitWidth() == 128 &&
1326 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1327 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1328 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1329 EltTy->isIntegerTy(128)))
1330 return VT;
1331 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001332
Chris Lattner0f408f52010-07-29 04:56:46 +00001333 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1334}
1335
Chris Lattnere2962be2010-07-29 07:30:00 +00001336/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1337/// is known to either be off the end of the specified type or being in
1338/// alignment padding. The user type specified is known to be at most 128 bits
1339/// in size, and have passed through X86_64ABIInfo::classify with a successful
1340/// classification that put one of the two halves in the INTEGER class.
1341///
1342/// It is conservatively correct to return false.
1343static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1344 unsigned EndBit, ASTContext &Context) {
1345 // If the bytes being queried are off the end of the type, there is no user
1346 // data hiding here. This handles analysis of builtins, vectors and other
1347 // types that don't contain interesting padding.
1348 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1349 if (TySize <= StartBit)
1350 return true;
1351
Chris Lattner021c3a32010-07-29 07:43:55 +00001352 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1353 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1354 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1355
1356 // Check each element to see if the element overlaps with the queried range.
1357 for (unsigned i = 0; i != NumElts; ++i) {
1358 // If the element is after the span we care about, then we're done..
1359 unsigned EltOffset = i*EltSize;
1360 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001361
Chris Lattner021c3a32010-07-29 07:43:55 +00001362 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1363 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1364 EndBit-EltOffset, Context))
1365 return false;
1366 }
1367 // If it overlaps no elements, then it is safe to process as padding.
1368 return true;
1369 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001370
Chris Lattnere2962be2010-07-29 07:30:00 +00001371 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1372 const RecordDecl *RD = RT->getDecl();
1373 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001374
Chris Lattnere2962be2010-07-29 07:30:00 +00001375 // If this is a C++ record, check the bases first.
1376 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1377 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1378 e = CXXRD->bases_end(); i != e; ++i) {
1379 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1380 "Unexpected base class!");
1381 const CXXRecordDecl *Base =
1382 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001383
Chris Lattnere2962be2010-07-29 07:30:00 +00001384 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001385 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001386 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001387
Chris Lattnere2962be2010-07-29 07:30:00 +00001388 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1389 if (!BitsContainNoUserData(i->getType(), BaseStart,
1390 EndBit-BaseOffset, Context))
1391 return false;
1392 }
1393 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001394
Chris Lattnere2962be2010-07-29 07:30:00 +00001395 // Verify that no field has data that overlaps the region of interest. Yes
1396 // this could be sped up a lot by being smarter about queried fields,
1397 // however we're only looking at structs up to 16 bytes, so we don't care
1398 // much.
1399 unsigned idx = 0;
1400 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1401 i != e; ++i, ++idx) {
1402 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001403
Chris Lattnere2962be2010-07-29 07:30:00 +00001404 // If we found a field after the region we care about, then we're done.
1405 if (FieldOffset >= EndBit) break;
1406
1407 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1408 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1409 Context))
1410 return false;
1411 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001412
Chris Lattnere2962be2010-07-29 07:30:00 +00001413 // If nothing in this record overlapped the area of interest, then we're
1414 // clean.
1415 return true;
1416 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001417
Chris Lattnere2962be2010-07-29 07:30:00 +00001418 return false;
1419}
1420
Chris Lattner0b362002010-07-29 18:39:32 +00001421/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1422/// float member at the specified offset. For example, {int,{float}} has a
1423/// float at offset 4. It is conservatively correct for this routine to return
1424/// false.
1425static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1426 const llvm::TargetData &TD) {
1427 // Base case if we find a float.
1428 if (IROffset == 0 && IRType->isFloatTy())
1429 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001430
Chris Lattner0b362002010-07-29 18:39:32 +00001431 // If this is a struct, recurse into the field at the specified offset.
1432 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1433 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1434 unsigned Elt = SL->getElementContainingOffset(IROffset);
1435 IROffset -= SL->getElementOffset(Elt);
1436 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1437 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001438
Chris Lattner0b362002010-07-29 18:39:32 +00001439 // If this is an array, recurse into the field at the specified offset.
1440 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1441 const llvm::Type *EltTy = ATy->getElementType();
1442 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1443 IROffset -= IROffset/EltSize*EltSize;
1444 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1445 }
1446
1447 return false;
1448}
1449
Chris Lattnerf47c9442010-07-29 18:13:09 +00001450
1451/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1452/// low 8 bytes of an XMM register, corresponding to the SSE class.
1453const llvm::Type *X86_64ABIInfo::
1454GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1455 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001456 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001457 // pass as float if the last 4 bytes is just padding. This happens for
1458 // structs that contain 3 floats.
1459 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1460 SourceOffset*8+64, getContext()))
1461 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001462
Chris Lattner0b362002010-07-29 18:39:32 +00001463 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1464 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1465 // case.
1466 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001467 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1468 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001469
Chris Lattnerf47c9442010-07-29 18:13:09 +00001470 return llvm::Type::getDoubleTy(getVMContext());
1471}
1472
1473
Chris Lattner0d2656d2010-07-29 17:40:35 +00001474/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1475/// an 8-byte GPR. This means that we either have a scalar or we are talking
1476/// about the high or low part of an up-to-16-byte struct. This routine picks
1477/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001478/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1479/// etc).
1480///
1481/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1482/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1483/// the 8-byte value references. PrefType may be null.
1484///
1485/// SourceTy is the source level type for the entire argument. SourceOffset is
1486/// an offset into this that we're processing (which is always either 0 or 8).
1487///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001488const llvm::Type *X86_64ABIInfo::
Chris Lattner0d2656d2010-07-29 17:40:35 +00001489GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1490 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001491 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1492 // returning an 8-byte unit starting with it. See if we can safely use it.
1493 if (IROffset == 0) {
1494 // Pointers and int64's always fill the 8-byte unit.
1495 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1496 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001497
Chris Lattnere2962be2010-07-29 07:30:00 +00001498 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1499 // goodness in the source type is just tail padding. This is allowed to
1500 // kick in for struct {double,int} on the int, but not on
1501 // struct{double,int,int} because we wouldn't return the second int. We
1502 // have to do this analysis on the source type because we can't depend on
1503 // unions being lowered a specific way etc.
1504 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1505 IRType->isIntegerTy(32)) {
1506 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001507
Chris Lattnere2962be2010-07-29 07:30:00 +00001508 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1509 SourceOffset*8+64, getContext()))
1510 return IRType;
1511 }
1512 }
Chris Lattner49382de2010-07-28 22:44:07 +00001513
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001514 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001515 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001516 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001517 if (IROffset < SL->getSizeInBytes()) {
1518 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1519 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001520
Chris Lattner0d2656d2010-07-29 17:40:35 +00001521 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1522 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001523 }
Chris Lattner49382de2010-07-28 22:44:07 +00001524 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001525
Chris Lattner021c3a32010-07-29 07:43:55 +00001526 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1527 const llvm::Type *EltTy = ATy->getElementType();
1528 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1529 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001530 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1531 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001532 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001533
Chris Lattner49382de2010-07-28 22:44:07 +00001534 // Okay, we don't have any better idea of what to pass, so we pass this in an
1535 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001536 unsigned TySizeInBytes =
1537 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001538
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001539 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001540
Chris Lattner49382de2010-07-28 22:44:07 +00001541 // It is always safe to classify this as an integer type up to i64 that
1542 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001543 return llvm::IntegerType::get(getVMContext(),
1544 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001545}
1546
Chris Lattner66e7b682010-09-01 00:50:20 +00001547
1548/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1549/// be used as elements of a two register pair to pass or return, return a
1550/// first class aggregate to represent them. For example, if the low part of
1551/// a by-value argument should be passed as i32* and the high part as float,
1552/// return {i32*, float}.
1553static const llvm::Type *
1554GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1555 const llvm::TargetData &TD) {
1556 // In order to correctly satisfy the ABI, we need to the high part to start
1557 // at offset 8. If the high and low parts we inferred are both 4-byte types
1558 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1559 // the second element at offset 8. Check for this:
1560 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1561 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1562 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1563 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001564
Chris Lattner66e7b682010-09-01 00:50:20 +00001565 // To handle this, we have to increase the size of the low part so that the
1566 // second element will start at an 8 byte offset. We can't increase the size
1567 // of the second element because it might make us access off the end of the
1568 // struct.
1569 if (HiStart != 8) {
1570 // There are only two sorts of types the ABI generation code can produce for
1571 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1572 // Promote these to a larger type.
1573 if (Lo->isFloatTy())
1574 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1575 else {
1576 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1577 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1578 }
1579 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001580
1581 const llvm::StructType *Result =
Chris Lattner66e7b682010-09-01 00:50:20 +00001582 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001583
1584
Chris Lattner66e7b682010-09-01 00:50:20 +00001585 // Verify that the second element is at an 8-byte offset.
1586 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1587 "Invalid x86-64 argument pair!");
1588 return Result;
1589}
1590
Chris Lattner519f68c2010-07-28 23:06:14 +00001591ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001592classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001593 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1594 // classification algorithm.
1595 X86_64ABIInfo::Class Lo, Hi;
1596 classify(RetTy, 0, Lo, Hi);
1597
1598 // Check some invariants.
1599 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001600 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1601
1602 const llvm::Type *ResType = 0;
1603 switch (Lo) {
1604 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001605 if (Hi == NoClass)
1606 return ABIArgInfo::getIgnore();
1607 // If the low part is just padding, it takes no register, leave ResType
1608 // null.
1609 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1610 "Unknown missing lo part");
1611 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001612
1613 case SSEUp:
1614 case X87Up:
1615 assert(0 && "Invalid classification for lo word.");
1616
1617 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1618 // hidden argument.
1619 case Memory:
1620 return getIndirectReturnResult(RetTy);
1621
1622 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1623 // available register of the sequence %rax, %rdx is used.
1624 case Integer:
Chris Lattner0d2656d2010-07-29 17:40:35 +00001625 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1626 RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001627
Chris Lattnereb518b42010-07-29 21:42:50 +00001628 // If we have a sign or zero extended integer, make sure to return Extend
1629 // so that the parameter gets the right LLVM IR attributes.
1630 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1631 // Treat an enum type as its underlying type.
1632 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1633 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001634
Chris Lattnereb518b42010-07-29 21:42:50 +00001635 if (RetTy->isIntegralOrEnumerationType() &&
1636 RetTy->isPromotableIntegerType())
1637 return ABIArgInfo::getExtend();
1638 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001639 break;
1640
1641 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1642 // available SSE register of the sequence %xmm0, %xmm1 is used.
1643 case SSE:
Chris Lattnerf47c9442010-07-29 18:13:09 +00001644 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001645 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001646
1647 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1648 // returned on the X87 stack in %st0 as 80-bit x87 number.
1649 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001650 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001651 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001652
1653 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1654 // part of the value is returned in %st0 and the imaginary part in
1655 // %st1.
1656 case ComplexX87:
1657 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001658 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001659 llvm::Type::getX86_FP80Ty(getVMContext()),
1660 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001661 NULL);
1662 break;
1663 }
1664
Chris Lattner3db4dde2010-09-01 00:20:33 +00001665 const llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001666 switch (Hi) {
1667 // Memory was handled previously and X87 should
1668 // never occur as a hi class.
1669 case Memory:
1670 case X87:
1671 assert(0 && "Invalid classification for hi word.");
1672
1673 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001674 case NoClass:
1675 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001676
Chris Lattner3db4dde2010-09-01 00:20:33 +00001677 case Integer:
1678 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1679 8, RetTy, 8);
1680 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1681 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001682 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001683 case SSE:
1684 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1685 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1686 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001687 break;
1688
1689 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1690 // is passed in the upper half of the last used SSE register.
1691 //
1692 // SSEUP should always be preceeded by SSE, just widen.
1693 case SSEUp:
1694 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner0f408f52010-07-29 04:56:46 +00001695 ResType = Get16ByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001696 break;
1697
1698 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1699 // returned together with the previous X87 value in %st0.
1700 case X87Up:
1701 // If X87Up is preceeded by X87, we don't need to do
1702 // anything. However, in some cases with unions it may not be
1703 // preceeded by X87. In such situations we follow gcc and pass the
1704 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001705 if (Lo != X87) {
Chris Lattner3db4dde2010-09-01 00:20:33 +00001706 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1707 8, RetTy, 8);
1708 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1709 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001710 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001711 break;
1712 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001713
Chris Lattner3db4dde2010-09-01 00:20:33 +00001714 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001715 // known to pass in the high eightbyte of the result. We do this by forming a
1716 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001717 if (HighPart)
1718 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001719
Chris Lattnereb518b42010-07-29 21:42:50 +00001720 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001721}
1722
Chris Lattnera3c109b2010-07-29 02:16:43 +00001723ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001724 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001725 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001726 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001727
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001728 // Check some invariants.
1729 // FIXME: Enforce these by construction.
1730 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001731 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1732
1733 neededInt = 0;
1734 neededSSE = 0;
1735 const llvm::Type *ResType = 0;
1736 switch (Lo) {
1737 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001738 if (Hi == NoClass)
1739 return ABIArgInfo::getIgnore();
1740 // If the low part is just padding, it takes no register, leave ResType
1741 // null.
1742 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1743 "Unknown missing lo part");
1744 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001745
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001746 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1747 // on the stack.
1748 case Memory:
1749
1750 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1751 // COMPLEX_X87, it is passed in memory.
1752 case X87:
1753 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001754 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001755
1756 case SSEUp:
1757 case X87Up:
1758 assert(0 && "Invalid classification for lo word.");
1759
1760 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1761 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1762 // and %r9 is used.
1763 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001764 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001765
Chris Lattner49382de2010-07-28 22:44:07 +00001766 // Pick an 8-byte type based on the preferred type.
Chris Lattner0d2656d2010-07-29 17:40:35 +00001767 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001768
1769 // If we have a sign or zero extended integer, make sure to return Extend
1770 // so that the parameter gets the right LLVM IR attributes.
1771 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1772 // Treat an enum type as its underlying type.
1773 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1774 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001775
Chris Lattnereb518b42010-07-29 21:42:50 +00001776 if (Ty->isIntegralOrEnumerationType() &&
1777 Ty->isPromotableIntegerType())
1778 return ABIArgInfo::getExtend();
1779 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001780
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001781 break;
1782
1783 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1784 // available SSE register is used, the registers are taken in the
1785 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001786 case SSE: {
1787 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001788 if (Hi != NoClass || !UseX86_MMXType(IRType))
Bill Wendlingbb465d72010-10-18 03:41:31 +00001789 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001790 else
Bill Wendlingbb465d72010-10-18 03:41:31 +00001791 // This is an MMX type. Treat it as such.
1792 ResType = llvm::Type::getX86_MMXTy(getVMContext());
Bill Wendlingbb465d72010-10-18 03:41:31 +00001793
Bill Wendling99aaae82010-10-18 23:51:38 +00001794 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001795 break;
1796 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001797 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001798
Chris Lattner645406a2010-09-01 00:24:35 +00001799 const llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001800 switch (Hi) {
1801 // Memory was handled previously, ComplexX87 and X87 should
1802 // never occur as hi classes, and X87Up must be preceed by X87,
1803 // which is passed in memory.
1804 case Memory:
1805 case X87:
1806 case ComplexX87:
1807 assert(0 && "Invalid classification for hi word.");
1808 break;
1809
1810 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001811
Chris Lattner645406a2010-09-01 00:24:35 +00001812 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001813 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001814 // Pick an 8-byte type based on the preferred type.
Chris Lattner645406a2010-09-01 00:24:35 +00001815 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001816
Chris Lattner645406a2010-09-01 00:24:35 +00001817 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1818 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001819 break;
1820
1821 // X87Up generally doesn't occur here (long double is passed in
1822 // memory), except in situations involving unions.
1823 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001824 case SSE:
1825 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001826
Chris Lattner645406a2010-09-01 00:24:35 +00001827 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1828 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001829
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001830 ++neededSSE;
1831 break;
1832
1833 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1834 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001835 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001836 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001837 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner0f408f52010-07-29 04:56:46 +00001838 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001839 break;
1840 }
1841
Chris Lattner645406a2010-09-01 00:24:35 +00001842 // If a high part was specified, merge it together with the low part. It is
1843 // known to pass in the high eightbyte of the result. We do this by forming a
1844 // first class struct aggregate with the high and low part: {low, high}
1845 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001846 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001847
Chris Lattnereb518b42010-07-29 21:42:50 +00001848 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001849}
1850
Chris Lattneree5dcd02010-07-29 02:31:05 +00001851void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001852
Chris Lattnera3c109b2010-07-29 02:16:43 +00001853 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001854
1855 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001856 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001857
1858 // If the return value is indirect, then the hidden argument is consuming one
1859 // integer register.
1860 if (FI.getReturnInfo().isIndirect())
1861 --freeIntRegs;
1862
1863 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1864 // get assigned (in left-to-right order) for passing as follows...
1865 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1866 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001867 unsigned neededInt, neededSSE;
1868 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001869
1870 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1871 // eightbyte of an argument, the whole argument is passed on the
1872 // stack. If registers have already been assigned for some
1873 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001874 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001875 freeIntRegs -= neededInt;
1876 freeSSERegs -= neededSSE;
1877 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001878 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001879 }
1880 }
1881}
1882
1883static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1884 QualType Ty,
1885 CodeGenFunction &CGF) {
1886 llvm::Value *overflow_arg_area_p =
1887 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1888 llvm::Value *overflow_arg_area =
1889 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1890
1891 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1892 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1893 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1894 if (Align > 8) {
1895 // Note that we follow the ABI & gcc here, even though the type
1896 // could in theory have an alignment greater than 16. This case
1897 // shouldn't ever matter in practice.
1898
1899 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001900 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001901 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001902 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1903 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001904 CGF.Int64Ty);
1905 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001906 overflow_arg_area =
1907 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1908 overflow_arg_area->getType(),
1909 "overflow_arg_area.align");
1910 }
1911
1912 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1913 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1914 llvm::Value *Res =
1915 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001916 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001917
1918 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1919 // l->overflow_arg_area + sizeof(type).
1920 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1921 // an 8 byte boundary.
1922
1923 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001924 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001925 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001926 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1927 "overflow_arg_area.next");
1928 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1929
1930 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1931 return Res;
1932}
1933
1934llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1935 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001936 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001938 // Assume that va_list type is correct; should be pointer to LLVM type:
1939 // struct {
1940 // i32 gp_offset;
1941 // i32 fp_offset;
1942 // i8* overflow_arg_area;
1943 // i8* reg_save_area;
1944 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00001945 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001946
Chris Lattnera14db752010-03-11 18:19:55 +00001947 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00001948 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001949
1950 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1951 // in the registers. If not go to step 7.
1952 if (!neededInt && !neededSSE)
1953 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1954
1955 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1956 // general purpose registers needed to pass type and num_fp to hold
1957 // the number of floating point registers needed.
1958
1959 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1960 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1961 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1962 //
1963 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1964 // register save space).
1965
1966 llvm::Value *InRegs = 0;
1967 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1968 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1969 if (neededInt) {
1970 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1971 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001972 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1973 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001974 }
1975
1976 if (neededSSE) {
1977 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1978 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1979 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001980 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1981 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001982 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1983 }
1984
1985 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1986 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1987 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1988 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1989
1990 // Emit code to load the value if it was passed in registers.
1991
1992 CGF.EmitBlock(InRegBlock);
1993
1994 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1995 // an offset of l->gp_offset and/or l->fp_offset. This may require
1996 // copying to a temporary location in case the parameter is passed
1997 // in different register classes or requires an alignment greater
1998 // than 8 for general purpose registers and 16 for XMM registers.
1999 //
2000 // FIXME: This really results in shameful code when we end up needing to
2001 // collect arguments from different places; often what should result in a
2002 // simple assembling of a structure from scattered addresses has many more
2003 // loads than necessary. Can we clean this up?
2004 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2005 llvm::Value *RegAddr =
2006 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2007 "reg_save_area");
2008 if (neededInt && neededSSE) {
2009 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002010 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002011 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2012 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2013 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2014 const llvm::Type *TyLo = ST->getElementType(0);
2015 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002016 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002017 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00002018 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2019 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002020 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2021 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002022 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2023 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002024 llvm::Value *V =
2025 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2026 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2027 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2028 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2029
Owen Andersona1cf15f2009-07-14 23:10:40 +00002030 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002031 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002032 } else if (neededInt) {
2033 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2034 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002035 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002036 } else if (neededSSE == 1) {
2037 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2038 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2039 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002040 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002041 assert(neededSSE == 2 && "Invalid number of needed registers!");
2042 // SSE registers are spaced 16 bytes apart in the register save
2043 // area, we need to collect the two eightbytes together.
2044 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002045 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002046 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
2047 const llvm::Type *DblPtrTy =
2048 llvm::PointerType::getUnqual(DoubleTy);
2049 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
2050 DoubleTy, NULL);
2051 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2052 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2053 DblPtrTy));
2054 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2055 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2056 DblPtrTy));
2057 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2058 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2059 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002060 }
2061
2062 // AMD64-ABI 3.5.7p5: Step 5. Set:
2063 // l->gp_offset = l->gp_offset + num_gp * 8
2064 // l->fp_offset = l->fp_offset + num_fp * 16.
2065 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002066 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002067 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2068 gp_offset_p);
2069 }
2070 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002071 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002072 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2073 fp_offset_p);
2074 }
2075 CGF.EmitBranch(ContBlock);
2076
2077 // Emit code to load the value if it was passed in memory.
2078
2079 CGF.EmitBlock(InMemBlock);
2080 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2081
2082 // Return the appropriate result.
2083
2084 CGF.EmitBlock(ContBlock);
2085 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2086 "vaarg.addr");
2087 ResAddr->reserveOperandSpace(2);
2088 ResAddr->addIncoming(RegAddr, InRegBlock);
2089 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002090 return ResAddr;
2091}
2092
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002093ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2094
2095 if (Ty->isVoidType())
2096 return ABIArgInfo::getIgnore();
2097
2098 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2099 Ty = EnumTy->getDecl()->getIntegerType();
2100
2101 uint64_t Size = getContext().getTypeSize(Ty);
2102
2103 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002104 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2105 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002106 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2107
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002108 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2109 if (Size == 128 &&
2110 getContext().Target.getTriple().getOS() == llvm::Triple::MinGW32)
2111 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2112 Size));
2113
2114 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2115 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2116 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002117 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002118 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2119 Size));
2120
2121 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2122 }
2123
2124 if (Ty->isPromotableIntegerType())
2125 return ABIArgInfo::getExtend();
2126
2127 return ABIArgInfo::getDirect();
2128}
2129
2130void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2131
2132 QualType RetTy = FI.getReturnType();
2133 FI.getReturnInfo() = classify(RetTy);
2134
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002135 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2136 it != ie; ++it)
2137 it->info = classify(it->type);
2138}
2139
Chris Lattnerf13721d2010-08-31 16:44:54 +00002140llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2141 CodeGenFunction &CGF) const {
2142 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2143 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002144
Chris Lattnerf13721d2010-08-31 16:44:54 +00002145 CGBuilderTy &Builder = CGF.Builder;
2146 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2147 "ap");
2148 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2149 llvm::Type *PTy =
2150 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2151 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2152
2153 uint64_t Offset =
2154 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2155 llvm::Value *NextAddr =
2156 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2157 "ap.next");
2158 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2159
2160 return AddrTyped;
2161}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002162
John McCallec853ba2010-03-11 00:10:12 +00002163// PowerPC-32
2164
2165namespace {
2166class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2167public:
Chris Lattnerea044322010-07-29 02:01:43 +00002168 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002169
John McCallec853ba2010-03-11 00:10:12 +00002170 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2171 // This is recovered from gcc output.
2172 return 1; // r1 is the dedicated stack pointer
2173 }
2174
2175 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002176 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002177};
2178
2179}
2180
2181bool
2182PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2183 llvm::Value *Address) const {
2184 // This is calculated from the LLVM and GCC tables and verified
2185 // against gcc output. AFAIK all ABIs use the same encoding.
2186
2187 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2188 llvm::LLVMContext &Context = CGF.getLLVMContext();
2189
2190 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2191 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2192 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2193 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2194
2195 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002196 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002197
2198 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002199 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002200
2201 // 64-76 are various 4-byte special-purpose registers:
2202 // 64: mq
2203 // 65: lr
2204 // 66: ctr
2205 // 67: ap
2206 // 68-75 cr0-7
2207 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002208 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002209
2210 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002211 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002212
2213 // 109: vrsave
2214 // 110: vscr
2215 // 111: spe_acc
2216 // 112: spefscr
2217 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002218 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002219
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002220 return false;
John McCallec853ba2010-03-11 00:10:12 +00002221}
2222
2223
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002224//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002225// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002226//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002227
2228namespace {
2229
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002230class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002231public:
2232 enum ABIKind {
2233 APCS = 0,
2234 AAPCS = 1,
2235 AAPCS_VFP
2236 };
2237
2238private:
2239 ABIKind Kind;
2240
2241public:
Chris Lattnerea044322010-07-29 02:01:43 +00002242 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002243
2244private:
2245 ABIKind getABIKind() const { return Kind; }
2246
Chris Lattnera3c109b2010-07-29 02:16:43 +00002247 ABIArgInfo classifyReturnType(QualType RetTy) const;
2248 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002249
Chris Lattneree5dcd02010-07-29 02:31:05 +00002250 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002251
2252 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2253 CodeGenFunction &CGF) const;
2254};
2255
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002256class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2257public:
Chris Lattnerea044322010-07-29 02:01:43 +00002258 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2259 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002260
2261 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2262 return 13;
2263 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002264};
2265
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002266}
2267
Chris Lattneree5dcd02010-07-29 02:31:05 +00002268void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002269 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002270 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002271 it != ie; ++it)
2272 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002273
Chris Lattnera3c109b2010-07-29 02:16:43 +00002274 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindola25117ab2010-06-16 16:13:39 +00002275 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002276 if (Triple.getEnvironmentName() == "gnueabi" ||
2277 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002278 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002279 else
2280 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002281
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002282 switch (getABIKind()) {
2283 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002284 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2285 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002286 break;
2287
2288 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002289 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2290 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002291 break;
2292
2293 case AAPCS_VFP:
2294 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2295 break;
2296 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297}
2298
Chris Lattnera3c109b2010-07-29 02:16:43 +00002299ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002300 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002301 // Treat an enum type as its underlying type.
2302 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2303 Ty = EnumTy->getDecl()->getIntegerType();
2304
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002305 return (Ty->isPromotableIntegerType() ?
2306 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002307 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002308
Daniel Dunbar42025572009-09-14 21:54:03 +00002309 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002310 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002311 return ABIArgInfo::getIgnore();
2312
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002313 // Structures with either a non-trivial destructor or a non-trivial
2314 // copy constructor are always indirect.
2315 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2316 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2317
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002318 // Otherwise, pass by coercing to a structure of the appropriate size.
2319 //
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002320 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2321 // backend doesn't support byval.
2322 // FIXME: This doesn't handle alignment > 64 bits.
2323 const llvm::Type* ElemTy;
2324 unsigned SizeRegs;
Chris Lattnera3c109b2010-07-29 02:16:43 +00002325 if (getContext().getTypeAlign(Ty) > 32) {
2326 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2327 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002328 } else {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002329 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2330 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002331 }
2332 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00002333 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattnera3c109b2010-07-29 02:16:43 +00002334 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2335 true);
Chris Lattner800588f2010-07-29 06:26:06 +00002336 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002337}
2338
Chris Lattnera3c109b2010-07-29 02:16:43 +00002339static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002340 llvm::LLVMContext &VMContext) {
2341 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2342 // is called integer-like if its size is less than or equal to one word, and
2343 // the offset of each of its addressable sub-fields is zero.
2344
2345 uint64_t Size = Context.getTypeSize(Ty);
2346
2347 // Check that the type fits in a word.
2348 if (Size > 32)
2349 return false;
2350
2351 // FIXME: Handle vector types!
2352 if (Ty->isVectorType())
2353 return false;
2354
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002355 // Float types are never treated as "integer like".
2356 if (Ty->isRealFloatingType())
2357 return false;
2358
Daniel Dunbar98303b92009-09-13 08:03:58 +00002359 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002360 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002361 return true;
2362
Daniel Dunbar45815812010-02-01 23:31:26 +00002363 // Small complex integer types are "integer like".
2364 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2365 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002366
2367 // Single element and zero sized arrays should be allowed, by the definition
2368 // above, but they are not.
2369
2370 // Otherwise, it must be a record type.
2371 const RecordType *RT = Ty->getAs<RecordType>();
2372 if (!RT) return false;
2373
2374 // Ignore records with flexible arrays.
2375 const RecordDecl *RD = RT->getDecl();
2376 if (RD->hasFlexibleArrayMember())
2377 return false;
2378
2379 // Check that all sub-fields are at offset 0, and are themselves "integer
2380 // like".
2381 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2382
2383 bool HadField = false;
2384 unsigned idx = 0;
2385 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2386 i != e; ++i, ++idx) {
2387 const FieldDecl *FD = *i;
2388
Daniel Dunbar679855a2010-01-29 03:22:29 +00002389 // Bit-fields are not addressable, we only need to verify they are "integer
2390 // like". We still have to disallow a subsequent non-bitfield, for example:
2391 // struct { int : 0; int x }
2392 // is non-integer like according to gcc.
2393 if (FD->isBitField()) {
2394 if (!RD->isUnion())
2395 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002396
Daniel Dunbar679855a2010-01-29 03:22:29 +00002397 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2398 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002399
Daniel Dunbar679855a2010-01-29 03:22:29 +00002400 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002401 }
2402
Daniel Dunbar679855a2010-01-29 03:22:29 +00002403 // Check if this field is at offset 0.
2404 if (Layout.getFieldOffset(idx) != 0)
2405 return false;
2406
Daniel Dunbar98303b92009-09-13 08:03:58 +00002407 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2408 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002409
Daniel Dunbar679855a2010-01-29 03:22:29 +00002410 // Only allow at most one field in a structure. This doesn't match the
2411 // wording above, but follows gcc in situations with a field following an
2412 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002413 if (!RD->isUnion()) {
2414 if (HadField)
2415 return false;
2416
2417 HadField = true;
2418 }
2419 }
2420
2421 return true;
2422}
2423
Chris Lattnera3c109b2010-07-29 02:16:43 +00002424ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002425 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002426 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002427
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002428 // Large vector types should be returned via memory.
2429 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2430 return ABIArgInfo::getIndirect(0);
2431
John McCalld608cdb2010-08-22 10:59:02 +00002432 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002433 // Treat an enum type as its underlying type.
2434 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2435 RetTy = EnumTy->getDecl()->getIntegerType();
2436
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002437 return (RetTy->isPromotableIntegerType() ?
2438 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002439 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002440
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002441 // Structures with either a non-trivial destructor or a non-trivial
2442 // copy constructor are always indirect.
2443 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2444 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2445
Daniel Dunbar98303b92009-09-13 08:03:58 +00002446 // Are we following APCS?
2447 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002448 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002449 return ABIArgInfo::getIgnore();
2450
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002451 // Complex types are all returned as packed integers.
2452 //
2453 // FIXME: Consider using 2 x vector types if the back end handles them
2454 // correctly.
2455 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002456 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002457 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002458
Daniel Dunbar98303b92009-09-13 08:03:58 +00002459 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002460 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002461 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002462 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002463 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002464 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002465 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002466 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2467 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002468 }
2469
2470 // Otherwise return in memory.
2471 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002472 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002473
2474 // Otherwise this is an AAPCS variant.
2475
Chris Lattnera3c109b2010-07-29 02:16:43 +00002476 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002477 return ABIArgInfo::getIgnore();
2478
Daniel Dunbar98303b92009-09-13 08:03:58 +00002479 // Aggregates <= 4 bytes are returned in r0; other aggregates
2480 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002481 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002482 if (Size <= 32) {
2483 // Return in the smallest viable integer type.
2484 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002485 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002486 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002487 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2488 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002489 }
2490
Daniel Dunbar98303b92009-09-13 08:03:58 +00002491 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002492}
2493
2494llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002495 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002496 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002497 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002498 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002499
2500 CGBuilderTy &Builder = CGF.Builder;
2501 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2502 "ap");
2503 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2504 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002505 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002506 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2507
2508 uint64_t Offset =
2509 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2510 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002511 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002512 "ap.next");
2513 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2514
2515 return AddrTyped;
2516}
2517
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002518//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002519// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002520//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002521
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002522namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002523
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002524class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002525public:
2526 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2527
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002528 bool isPromotableIntegerType(QualType Ty) const;
2529
Chris Lattnera3c109b2010-07-29 02:16:43 +00002530 ABIArgInfo classifyReturnType(QualType RetTy) const;
2531 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002532
Chris Lattneree5dcd02010-07-29 02:31:05 +00002533 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002534 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002535 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2536 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002537 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002538 }
2539
2540 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2541 CodeGenFunction &CGF) const;
2542};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002543
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002544class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2545public:
Chris Lattnerea044322010-07-29 02:01:43 +00002546 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2547 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002548};
2549
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002550}
2551
2552bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2553 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002554 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002555 switch (BT->getKind()) {
2556 case BuiltinType::Bool:
2557 case BuiltinType::Char_S:
2558 case BuiltinType::Char_U:
2559 case BuiltinType::SChar:
2560 case BuiltinType::UChar:
2561 case BuiltinType::Short:
2562 case BuiltinType::UShort:
2563 case BuiltinType::Int:
2564 case BuiltinType::UInt:
2565 return true;
2566 default:
2567 return false;
2568 }
2569 return false;
2570}
2571
2572llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2573 CodeGenFunction &CGF) const {
2574 // FIXME: Implement
2575 return 0;
2576}
2577
2578
Chris Lattnera3c109b2010-07-29 02:16:43 +00002579ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2580 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002581 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002582 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002583 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002584
2585 return (isPromotableIntegerType(RetTy) ?
2586 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002587}
2588
Chris Lattnera3c109b2010-07-29 02:16:43 +00002589ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002590 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002591 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002592
2593 return (isPromotableIntegerType(Ty) ?
2594 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002595}
2596
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002597//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002598// MBlaze ABI Implementation
2599//===----------------------------------------------------------------------===//
2600
2601namespace {
2602
2603class MBlazeABIInfo : public ABIInfo {
2604public:
2605 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2606
2607 bool isPromotableIntegerType(QualType Ty) const;
2608
2609 ABIArgInfo classifyReturnType(QualType RetTy) const;
2610 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2611
2612 virtual void computeInfo(CGFunctionInfo &FI) const {
2613 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2614 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2615 it != ie; ++it)
2616 it->info = classifyArgumentType(it->type);
2617 }
2618
2619 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2620 CodeGenFunction &CGF) const;
2621};
2622
2623class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2624public:
2625 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2626 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2627 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2628 CodeGen::CodeGenModule &M) const;
2629};
2630
2631}
2632
2633bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2634 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2635 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2636 switch (BT->getKind()) {
2637 case BuiltinType::Bool:
2638 case BuiltinType::Char_S:
2639 case BuiltinType::Char_U:
2640 case BuiltinType::SChar:
2641 case BuiltinType::UChar:
2642 case BuiltinType::Short:
2643 case BuiltinType::UShort:
2644 return true;
2645 default:
2646 return false;
2647 }
2648 return false;
2649}
2650
2651llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2652 CodeGenFunction &CGF) const {
2653 // FIXME: Implement
2654 return 0;
2655}
2656
2657
2658ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2659 if (RetTy->isVoidType())
2660 return ABIArgInfo::getIgnore();
2661 if (isAggregateTypeForABI(RetTy))
2662 return ABIArgInfo::getIndirect(0);
2663
2664 return (isPromotableIntegerType(RetTy) ?
2665 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2666}
2667
2668ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2669 if (isAggregateTypeForABI(Ty))
2670 return ABIArgInfo::getIndirect(0);
2671
2672 return (isPromotableIntegerType(Ty) ?
2673 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2674}
2675
2676void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2677 llvm::GlobalValue *GV,
2678 CodeGen::CodeGenModule &M)
2679 const {
2680 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2681 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002682
Wesley Peck276fdf42010-12-19 19:57:51 +00002683 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2684 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2685 CC = llvm::CallingConv::MBLAZE_INTR;
2686 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2687 CC = llvm::CallingConv::MBLAZE_SVOL;
2688
2689 if (CC != llvm::CallingConv::C) {
2690 // Handle 'interrupt_handler' attribute:
2691 llvm::Function *F = cast<llvm::Function>(GV);
2692
2693 // Step 1: Set ISR calling convention.
2694 F->setCallingConv(CC);
2695
2696 // Step 2: Add attributes goodness.
2697 F->addFnAttr(llvm::Attribute::NoInline);
2698 }
2699
2700 // Step 3: Emit _interrupt_handler alias.
2701 if (CC == llvm::CallingConv::MBLAZE_INTR)
2702 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2703 "_interrupt_handler", GV, &M.getModule());
2704}
2705
2706
2707//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002708// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002709//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002710
2711namespace {
2712
2713class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2714public:
Chris Lattnerea044322010-07-29 02:01:43 +00002715 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2716 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002717 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2718 CodeGen::CodeGenModule &M) const;
2719};
2720
2721}
2722
2723void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2724 llvm::GlobalValue *GV,
2725 CodeGen::CodeGenModule &M) const {
2726 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2727 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2728 // Handle 'interrupt' attribute:
2729 llvm::Function *F = cast<llvm::Function>(GV);
2730
2731 // Step 1: Set ISR calling convention.
2732 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2733
2734 // Step 2: Add attributes goodness.
2735 F->addFnAttr(llvm::Attribute::NoInline);
2736
2737 // Step 3: Emit ISR vector alias.
2738 unsigned Num = attr->getNumber() + 0xffe0;
2739 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Benjamin Kramer77d66052010-11-12 15:42:18 +00002740 "vector_" + llvm::Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002741 GV, &M.getModule());
2742 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002743 }
2744}
2745
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002746//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002747// MIPS ABI Implementation. This works for both little-endian and
2748// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002749//===----------------------------------------------------------------------===//
2750
John McCallaeeb7012010-05-27 06:19:26 +00002751namespace {
2752class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2753public:
Chris Lattnerea044322010-07-29 02:01:43 +00002754 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2755 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002756
2757 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2758 return 29;
2759 }
2760
2761 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002762 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002763};
2764}
2765
2766bool
2767MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2768 llvm::Value *Address) const {
2769 // This information comes from gcc's implementation, which seems to
2770 // as canonical as it gets.
2771
2772 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2773 llvm::LLVMContext &Context = CGF.getLLVMContext();
2774
2775 // Everything on MIPS is 4 bytes. Double-precision FP registers
2776 // are aliased to pairs of single-precision FP registers.
2777 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2778 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2779
2780 // 0-31 are the general purpose registers, $0 - $31.
2781 // 32-63 are the floating-point registers, $f0 - $f31.
2782 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2783 // 66 is the (notional, I think) register for signal-handler return.
2784 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2785
2786 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2787 // They are one bit wide and ignored here.
2788
2789 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2790 // (coprocessor 1 is the FP unit)
2791 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2792 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2793 // 176-181 are the DSP accumulator registers.
2794 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2795
2796 return false;
2797}
2798
2799
Chris Lattnerea044322010-07-29 02:01:43 +00002800const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002801 if (TheTargetCodeGenInfo)
2802 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002803
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002804 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2805 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002806
Chris Lattner9c254f02010-06-29 06:01:59 +00002807 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002808 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002809 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002810 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002811
John McCallaeeb7012010-05-27 06:19:26 +00002812 case llvm::Triple::mips:
2813 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002814 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002815
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002816 case llvm::Triple::arm:
2817 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002818 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002819 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002820 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002821 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002822
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002823 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002824 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002825
John McCallec853ba2010-03-11 00:10:12 +00002826 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00002827 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00002828
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002829 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00002830 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002831
Wesley Peck276fdf42010-12-19 19:57:51 +00002832 case llvm::Triple::mblaze:
2833 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
2834
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002835 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00002836 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002837
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002838 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002839 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002840 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002841 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002842 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002843 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002844 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002845 case llvm::Triple::AuroraUX:
2846 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002847 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002848 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00002849 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002850 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002851 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002852
2853 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002854 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002855 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002856 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002857
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002858 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00002859 switch (Triple.getOS()) {
2860 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00002861 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00002862 case llvm::Triple::Cygwin:
2863 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2864 default:
2865 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2866 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002867 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002868}