blob: 3c18b0b10836e8a462efb2ceb8aa6d9f1344d8ea [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel45df3dd2011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar626f1d82009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000091
92/// isEmptyField - Return true iff a the field is "empty", that is it
93/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000100
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000101 // Constant arrays of empty records count as empty, strip them off.
102 if (AllowArrays)
103 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
104 FT = AT->getElementType();
105
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000106 const RecordType *RT = FT->getAs<RecordType>();
107 if (!RT)
108 return false;
109
110 // C++ record fields are never empty, at least in the Itanium ABI.
111 //
112 // FIXME: We should use a predicate for whether this behavior is true in the
113 // current ABI.
114 if (isa<CXXRecordDecl>(RT->getDecl()))
115 return false;
116
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000118}
119
120/// isEmptyRecord - Return true iff a structure contains only empty
121/// fields. Note that a structure with a flexible array member is not
122/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000125 if (!RT)
126 return 0;
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return false;
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000130
Argyrios Kyrtzidisceee5e82011-05-17 00:46:40 +0000131 // If this is a C++ record, check if it is empty.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisceee5e82011-05-17 00:46:40 +0000133 return CXXRD->isEmpty();
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000134
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000135 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
136 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000137 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000138 return false;
139 return true;
140}
141
Anders Carlsson20759ad2009-09-16 15:53:40 +0000142/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
143/// a non-trivial destructor or a non-trivial copy constructor.
144static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
145 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
146 if (!RD)
147 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000148
Anders Carlsson20759ad2009-09-16 15:53:40 +0000149 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
150}
151
152/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
153/// a record type with either a non-trivial destructor or a non-trivial copy
154/// constructor.
155static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
156 const RecordType *RT = T->getAs<RecordType>();
157 if (!RT)
158 return false;
159
160 return hasNonTrivialDestructorOrCopyConstructor(RT);
161}
162
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000163/// isSingleElementStruct - Determine if a structure is a "single
164/// element struct", i.e. it has exactly one non-empty field or
165/// exactly one field which is itself a single element
166/// struct. Structures with flexible array members are never
167/// considered single element structs.
168///
169/// \return The field declaration for the single non-empty field, if
170/// it exists.
171static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
172 const RecordType *RT = T->getAsStructureType();
173 if (!RT)
174 return 0;
175
176 const RecordDecl *RD = RT->getDecl();
177 if (RD->hasFlexibleArrayMember())
178 return 0;
179
180 const Type *Found = 0;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000181
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000182 // If this is a C++ record, check the bases first.
183 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
184 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
185 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000186 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000187 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000188 continue;
189
190 // If we already found an element then this isn't a single-element struct.
191 if (Found)
192 return 0;
193
194 // If this is non-empty and not a single element struct, the composite
195 // cannot be a single element struct.
196 Found = isSingleElementStruct(i->getType(), Context);
197 if (!Found)
198 return 0;
199 }
200 }
201
202 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000203 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
204 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000205 const FieldDecl *FD = *i;
206 QualType FT = FD->getType();
207
208 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000209 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000210 continue;
211
212 // If we already found an element then this isn't a single-element
213 // struct.
214 if (Found)
215 return 0;
216
217 // Treat single element arrays as the element.
218 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
219 if (AT->getSize().getZExtValue() != 1)
220 break;
221 FT = AT->getElementType();
222 }
223
John McCalla1dee5302010-08-22 10:59:02 +0000224 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000225 Found = FT.getTypePtr();
226 } else {
227 Found = isSingleElementStruct(FT, Context);
228 if (!Found)
229 return 0;
230 }
231 }
232
233 return Found;
234}
235
236static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000237 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000238 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
239 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000240 return false;
241
242 uint64_t Size = Context.getTypeSize(Ty);
243 return Size == 32 || Size == 64;
244}
245
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000246/// canExpandIndirectArgument - Test whether an argument type which is to be
247/// passed indirectly (on the stack) would have the equivalent layout if it was
248/// expanded into separate arguments. If so, we prefer to do the latter to avoid
249/// inhibiting optimizations.
250///
251// FIXME: This predicate is missing many cases, currently it just follows
252// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
253// should probably make this smarter, or better yet make the LLVM backend
254// capable of handling it.
255static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
256 // We can only expand structure types.
257 const RecordType *RT = Ty->getAs<RecordType>();
258 if (!RT)
259 return false;
260
261 // We can only expand (C) structures.
262 //
263 // FIXME: This needs to be generalized to handle classes as well.
264 const RecordDecl *RD = RT->getDecl();
265 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
266 return false;
267
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000268 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
269 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000270 const FieldDecl *FD = *i;
271
272 if (!is32Or64BitBasicType(FD->getType(), Context))
273 return false;
274
275 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
276 // how to expand them yet, and the predicate for telling if a bitfield still
277 // counts as "basic" is more complicated than what we were doing previously.
278 if (FD->isBitField())
279 return false;
280 }
281
282 return true;
283}
284
285namespace {
286/// DefaultABIInfo - The default implementation for ABI specific
287/// details. This implementation provides information which results in
288/// self-consistent and sensible LLVM IR generation, but does not
289/// conform to any particular ABI.
290class DefaultABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +0000291public:
292 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000293
Chris Lattner458b2aa2010-07-29 02:16:43 +0000294 ABIArgInfo classifyReturnType(QualType RetTy) const;
295 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000296
Chris Lattner22326a12010-07-29 02:31:05 +0000297 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000298 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
300 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000301 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000302 }
303
304 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
305 CodeGenFunction &CGF) const;
306};
307
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000308class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
309public:
Chris Lattner2b037972010-07-29 02:01:43 +0000310 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
311 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000312};
313
314llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
315 CodeGenFunction &CGF) const {
316 return 0;
317}
318
Chris Lattner458b2aa2010-07-29 02:16:43 +0000319ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000320 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000321 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000322
Chris Lattner9723d6c2010-03-11 18:19:55 +0000323 // Treat an enum type as its underlying type.
324 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
325 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000326
Chris Lattner9723d6c2010-03-11 18:19:55 +0000327 return (Ty->isPromotableIntegerType() ?
328 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000329}
330
Bob Wilsonbd4520b2011-01-10 23:54:17 +0000331ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
332 if (RetTy->isVoidType())
333 return ABIArgInfo::getIgnore();
334
335 if (isAggregateTypeForABI(RetTy))
336 return ABIArgInfo::getIndirect(0);
337
338 // Treat an enum type as its underlying type.
339 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
340 RetTy = EnumTy->getDecl()->getIntegerType();
341
342 return (RetTy->isPromotableIntegerType() ?
343 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
344}
345
Bill Wendling5cd41c42010-10-18 03:41:31 +0000346/// UseX86_MMXType - Return true if this is an MMX type that should use the special
347/// x86_mmx type.
348bool UseX86_MMXType(const llvm::Type *IRType) {
349 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
350 // special x86_mmx type.
351 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
352 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
353 IRType->getScalarSizeInBits() != 64;
354}
355
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000356static const llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
357 llvm::StringRef Constraint,
358 const llvm::Type* Ty) {
Bill Wendlingec9d2632011-03-07 22:47:14 +0000359 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000360 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
361 return Ty;
362}
363
Chris Lattner0cf24192010-06-28 20:05:43 +0000364//===----------------------------------------------------------------------===//
365// X86-32 ABI Implementation
366//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000367
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000368/// X86_32ABIInfo - The X86-32 ABI information.
369class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000370 static const unsigned MinABIStackAlignInBytes = 4;
371
David Chisnallde3a0692009-08-17 23:08:21 +0000372 bool IsDarwinVectorABI;
373 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000374
375 static bool isRegisterSize(unsigned Size) {
376 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
377 }
378
379 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
380
Daniel Dunbar557893d2010-04-21 19:10:51 +0000381 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
382 /// such that the argument will be passed in memory.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000383 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000384
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000385 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000386 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000387
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000388public:
Chris Lattner2b037972010-07-29 02:01:43 +0000389
Chris Lattner458b2aa2010-07-29 02:16:43 +0000390 ABIArgInfo classifyReturnType(QualType RetTy) const;
391 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000392
Chris Lattner22326a12010-07-29 02:31:05 +0000393 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000394 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000395 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
396 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000397 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000398 }
399
400 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
401 CodeGenFunction &CGF) const;
402
Chris Lattner2b037972010-07-29 02:01:43 +0000403 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
404 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000405};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000406
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000407class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
408public:
Chris Lattner2b037972010-07-29 02:01:43 +0000409 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
410 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000411
412 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
413 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000414
415 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
416 // Darwin uses different dwarf register numbers for EH.
417 if (CGM.isTargetDarwin()) return 5;
418
419 return 4;
420 }
421
422 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
423 llvm::Value *Address) const;
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000424
425 const llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
426 llvm::StringRef Constraint,
427 const llvm::Type* Ty) const {
428 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
429 }
430
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000431};
432
433}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000434
435/// shouldReturnTypeInRegister - Determine if the given type should be
436/// passed in a register (for the Darwin ABI).
437bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
438 ASTContext &Context) {
439 uint64_t Size = Context.getTypeSize(Ty);
440
441 // Type must be register sized.
442 if (!isRegisterSize(Size))
443 return false;
444
445 if (Ty->isVectorType()) {
446 // 64- and 128- bit vectors inside structures are not returned in
447 // registers.
448 if (Size == 64 || Size == 128)
449 return false;
450
451 return true;
452 }
453
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000454 // If this is a builtin, pointer, enum, complex type, member pointer, or
455 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000456 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000457 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000458 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000459 return true;
460
461 // Arrays are treated like records.
462 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
463 return shouldReturnTypeInRegister(AT->getElementType(), Context);
464
465 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000466 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000467 if (!RT) return false;
468
Anders Carlsson40446e82010-01-27 03:25:19 +0000469 // FIXME: Traverse bases here too.
470
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000471 // Structure types are passed in register if all fields would be
472 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000473 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
474 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000475 const FieldDecl *FD = *i;
476
477 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000478 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000479 continue;
480
481 // Check fields recursively.
482 if (!shouldReturnTypeInRegister(FD->getType(), Context))
483 return false;
484 }
485
486 return true;
487}
488
Chris Lattner458b2aa2010-07-29 02:16:43 +0000489ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
490 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000491 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000492
Chris Lattner458b2aa2010-07-29 02:16:43 +0000493 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000494 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000495 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000496 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000497
498 // 128-bit vectors are a special case; they are returned in
499 // registers and we need to make sure to pick a type the LLVM
500 // backend will like.
501 if (Size == 128)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000502 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000503 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000504
505 // Always return in register if it fits in a general purpose
506 // register, or if it is 64 bits and has a single element.
507 if ((Size == 8 || Size == 16 || Size == 32) ||
508 (Size == 64 && VT->getNumElements() == 1))
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000509 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000510 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000511
512 return ABIArgInfo::getIndirect(0);
513 }
514
515 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000516 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000517
John McCalla1dee5302010-08-22 10:59:02 +0000518 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000519 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000520 // Structures with either a non-trivial destructor or a non-trivial
521 // copy constructor are always indirect.
522 if (hasNonTrivialDestructorOrCopyConstructor(RT))
523 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000524
Anders Carlsson5789c492009-10-20 22:07:59 +0000525 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000526 if (RT->getDecl()->hasFlexibleArrayMember())
527 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000528 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000529
David Chisnallde3a0692009-08-17 23:08:21 +0000530 // If specified, structs and unions are always indirect.
531 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000532 return ABIArgInfo::getIndirect(0);
533
534 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000535 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000536 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 if (BT->isIntegerType()) {
538 // We need to use the size of the structure, padding
539 // bit-fields can adjust that to be larger than the single
540 // element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000541 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000542 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000543 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
544 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000545
Chris Lattner458b2aa2010-07-29 02:16:43 +0000546 if (BT->getKind() == BuiltinType::Float) {
547 assert(getContext().getTypeSize(RetTy) ==
548 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000549 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000550 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000551 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000552
Chris Lattner458b2aa2010-07-29 02:16:43 +0000553 if (BT->getKind() == BuiltinType::Double) {
554 assert(getContext().getTypeSize(RetTy) ==
555 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000556 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000557 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000558 }
559 } else if (SeltTy->isPointerType()) {
560 // FIXME: It would be really nice if this could come out as the proper
561 // pointer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000562 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000563 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000564 } else if (SeltTy->isVectorType()) {
565 // 64- and 128-bit vectors are never returned in a
566 // register when inside a structure.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000567 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000568 if (Size == 64 || Size == 128)
569 return ABIArgInfo::getIndirect(0);
570
Chris Lattner458b2aa2010-07-29 02:16:43 +0000571 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000572 }
573 }
574
575 // Small structures which are register sized are generally returned
576 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000577 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
578 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000579 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000580 }
581
582 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000583 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000584
Chris Lattner458b2aa2010-07-29 02:16:43 +0000585 // Treat an enum type as its underlying type.
586 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
587 RetTy = EnumTy->getDecl()->getIntegerType();
588
589 return (RetTy->isPromotableIntegerType() ?
590 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000591}
592
Daniel Dunbared23de32010-09-16 20:42:00 +0000593static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
594 const RecordType *RT = Ty->getAs<RecordType>();
595 if (!RT)
596 return 0;
597 const RecordDecl *RD = RT->getDecl();
598
599 // If this is a C++ record, check the bases first.
600 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
601 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
602 e = CXXRD->bases_end(); i != e; ++i)
603 if (!isRecordWithSSEVectorType(Context, i->getType()))
604 return false;
605
606 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
607 i != e; ++i) {
608 QualType FT = i->getType();
609
610 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
611 return true;
612
613 if (isRecordWithSSEVectorType(Context, FT))
614 return true;
615 }
616
617 return false;
618}
619
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000620unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
621 unsigned Align) const {
622 // Otherwise, if the alignment is less than or equal to the minimum ABI
623 // alignment, just use the default; the backend will handle this.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000624 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000625 return 0; // Use default alignment.
626
627 // On non-Darwin, the stack type alignment is always 4.
628 if (!IsDarwinVectorABI) {
629 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000630 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000631 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000632
Daniel Dunbared23de32010-09-16 20:42:00 +0000633 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
634 if (isRecordWithSSEVectorType(getContext(), Ty))
635 return 16;
636
637 return MinABIStackAlignInBytes;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000638}
639
Chris Lattner458b2aa2010-07-29 02:16:43 +0000640ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000641 if (!ByVal)
642 return ABIArgInfo::getIndirect(0, false);
643
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000644 // Compute the byval alignment.
645 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
646 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
647 if (StackAlign == 0)
648 return ABIArgInfo::getIndirect(0);
649
650 // If the stack alignment is less than the type alignment, realign the
651 // argument.
652 if (StackAlign < TypeAlign)
653 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
654 /*Realign=*/true);
655
656 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000657}
658
Chris Lattner458b2aa2010-07-29 02:16:43 +0000659ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000660 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000661 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000662 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000663 if (const RecordType *RT = Ty->getAs<RecordType>()) {
664 // Structures with either a non-trivial destructor or a non-trivial
665 // copy constructor are always indirect.
666 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattner458b2aa2010-07-29 02:16:43 +0000667 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000668
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000669 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000670 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000671 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000672
673 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000674 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000675 return ABIArgInfo::getIgnore();
676
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000677 // Expand small (<= 128-bit) record types when we know that the stack layout
678 // of those arguments will match the struct. This is important because the
679 // LLVM backend isn't smart enough to remove byval, which inhibits many
680 // optimizations.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000681 if (getContext().getTypeSize(Ty) <= 4*32 &&
682 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000683 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000684
Chris Lattner458b2aa2010-07-29 02:16:43 +0000685 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000686 }
687
Chris Lattnerd774ae92010-08-26 20:05:13 +0000688 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-08-26 20:08:43 +0000689 // On Darwin, some vectors are passed in memory, we handle this by passing
690 // it as an i8/i16/i32/i64.
Chris Lattnerd774ae92010-08-26 20:05:13 +0000691 if (IsDarwinVectorABI) {
692 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-08-26 20:05:13 +0000693 if ((Size == 8 || Size == 16 || Size == 32) ||
694 (Size == 64 && VT->getNumElements() == 1))
695 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
696 Size));
Chris Lattnerd774ae92010-08-26 20:05:13 +0000697 }
Bill Wendling5cd41c42010-10-18 03:41:31 +0000698
699 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
700 if (UseX86_MMXType(IRType)) {
701 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
702 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
703 return AAI;
704 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000705
Chris Lattnerd774ae92010-08-26 20:05:13 +0000706 return ABIArgInfo::getDirect();
707 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000708
709
Chris Lattner458b2aa2010-07-29 02:16:43 +0000710 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
711 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000712
Chris Lattner458b2aa2010-07-29 02:16:43 +0000713 return (Ty->isPromotableIntegerType() ?
714 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000715}
716
717llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
718 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000719 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000720 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000721
722 CGBuilderTy &Builder = CGF.Builder;
723 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
724 "ap");
725 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
726 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000727 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000728 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
729
730 uint64_t Offset =
731 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
732 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +0000733 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000734 "ap.next");
735 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
736
737 return AddrTyped;
738}
739
Charles Davis4ea31ab2010-02-13 15:54:06 +0000740void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
741 llvm::GlobalValue *GV,
742 CodeGen::CodeGenModule &CGM) const {
743 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
744 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
745 // Get the LLVM function.
746 llvm::Function *Fn = cast<llvm::Function>(GV);
747
748 // Now add the 'alignstack' attribute with a value of 16.
749 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
750 }
751 }
752}
753
John McCallbeec5a02010-03-06 00:35:14 +0000754bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
755 CodeGen::CodeGenFunction &CGF,
756 llvm::Value *Address) const {
757 CodeGen::CGBuilderTy &Builder = CGF.Builder;
758 llvm::LLVMContext &Context = CGF.getLLVMContext();
759
760 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
761 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000762
John McCallbeec5a02010-03-06 00:35:14 +0000763 // 0-7 are the eight integer registers; the order is different
764 // on Darwin (for EH), but the range is the same.
765 // 8 is %eip.
John McCall943fae92010-05-27 06:19:26 +0000766 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-03-06 00:35:14 +0000767
768 if (CGF.CGM.isTargetDarwin()) {
769 // 12-16 are st(0..4). Not sure why we stop at 4.
770 // These have size 16, which is sizeof(long double) on
771 // platforms with 8-byte alignment for that type.
772 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCall943fae92010-05-27 06:19:26 +0000773 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000774
John McCallbeec5a02010-03-06 00:35:14 +0000775 } else {
776 // 9 is %eflags, which doesn't get a size on Darwin for some
777 // reason.
778 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
779
780 // 11-16 are st(0..5). Not sure why we stop at 5.
781 // These have size 12, which is sizeof(long double) on
782 // platforms with 4-byte alignment for that type.
783 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCall943fae92010-05-27 06:19:26 +0000784 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
785 }
John McCallbeec5a02010-03-06 00:35:14 +0000786
787 return false;
788}
789
Chris Lattner0cf24192010-06-28 20:05:43 +0000790//===----------------------------------------------------------------------===//
791// X86-64 ABI Implementation
792//===----------------------------------------------------------------------===//
793
794
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000795namespace {
796/// X86_64ABIInfo - The X86_64 ABI information.
797class X86_64ABIInfo : public ABIInfo {
798 enum Class {
799 Integer = 0,
800 SSE,
801 SSEUp,
802 X87,
803 X87Up,
804 ComplexX87,
805 NoClass,
806 Memory
807 };
808
809 /// merge - Implement the X86_64 ABI merging algorithm.
810 ///
811 /// Merge an accumulating classification \arg Accum with a field
812 /// classification \arg Field.
813 ///
814 /// \param Accum - The accumulating classification. This should
815 /// always be either NoClass or the result of a previous merge
816 /// call. In addition, this should never be Memory (the caller
817 /// should just return Memory for the aggregate).
Chris Lattnerd776fb12010-06-28 21:43:59 +0000818 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000819
820 /// classify - Determine the x86_64 register classes in which the
821 /// given type T should be passed.
822 ///
823 /// \param Lo - The classification for the parts of the type
824 /// residing in the low word of the containing object.
825 ///
826 /// \param Hi - The classification for the parts of the type
827 /// residing in the high word of the containing object.
828 ///
829 /// \param OffsetBase - The bit offset of this type in the
830 /// containing object. Some parameters are classified different
831 /// depending on whether they straddle an eightbyte boundary.
832 ///
833 /// If a word is unused its result will be NoClass; if a type should
834 /// be passed in Memory then at least the classification of \arg Lo
835 /// will be Memory.
836 ///
837 /// The \arg Lo class will be NoClass iff the argument is ignored.
838 ///
839 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
840 /// also be ComplexX87.
Chris Lattner22a931e2010-06-29 06:01:59 +0000841 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000842
Chris Lattner4200fe42010-07-29 04:56:46 +0000843 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000844 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000845 unsigned IROffset, QualType SourceTy,
846 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-07-29 17:40:35 +0000847 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
848 unsigned IROffset, QualType SourceTy,
849 unsigned SourceOffset) const;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000850
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000851 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000852 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000853 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000854
855 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000856 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000857 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000858
Chris Lattner458b2aa2010-07-29 02:16:43 +0000859 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000860
Bill Wendling5cd41c42010-10-18 03:41:31 +0000861 ABIArgInfo classifyArgumentType(QualType Ty,
862 unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +0000863 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000864
John McCalle0fda732011-04-21 01:20:55 +0000865 /// The 0.98 ABI revision clarified a lot of ambiguities,
866 /// unfortunately in ways that were not always consistent with
867 /// certain previous compilers. In particular, platforms which
868 /// required strict binary compatibility with older versions of GCC
869 /// may need to exempt themselves.
870 bool honorsRevision0_98() const {
871 return !getContext().Target.getTriple().isOSDarwin();
872 }
873
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000874public:
Chris Lattner2b037972010-07-29 02:01:43 +0000875 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000876
Chris Lattner22326a12010-07-29 02:31:05 +0000877 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000878
879 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
880 CodeGenFunction &CGF) const;
881};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000882
Chris Lattner04dc9572010-08-31 16:44:54 +0000883/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000884class WinX86_64ABIInfo : public ABIInfo {
885
886 ABIArgInfo classify(QualType Ty) const;
887
Chris Lattner04dc9572010-08-31 16:44:54 +0000888public:
NAKAMURA Takumibd91f502011-01-17 22:56:31 +0000889 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
890
891 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattner04dc9572010-08-31 16:44:54 +0000892
893 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
894 CodeGenFunction &CGF) const;
895};
896
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000897class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
898public:
Chris Lattner2b037972010-07-29 02:01:43 +0000899 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
900 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-03-06 00:35:14 +0000901
902 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
903 return 7;
904 }
905
906 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
907 llvm::Value *Address) const {
908 CodeGen::CGBuilderTy &Builder = CGF.Builder;
909 llvm::LLVMContext &Context = CGF.getLLVMContext();
910
911 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
912 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000913
John McCall943fae92010-05-27 06:19:26 +0000914 // 0-15 are the 16 integer registers.
915 // 16 is %rip.
916 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCallbeec5a02010-03-06 00:35:14 +0000917
918 return false;
919 }
Peter Collingbourne8f5cf742011-02-19 23:03:58 +0000920
921 const llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
922 llvm::StringRef Constraint,
923 const llvm::Type* Ty) const {
924 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
925 }
926
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000927};
928
Chris Lattner04dc9572010-08-31 16:44:54 +0000929class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
930public:
931 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
932 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
933
934 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
935 return 7;
936 }
937
938 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
939 llvm::Value *Address) const {
940 CodeGen::CGBuilderTy &Builder = CGF.Builder;
941 llvm::LLVMContext &Context = CGF.getLLVMContext();
942
943 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
944 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000945
Chris Lattner04dc9572010-08-31 16:44:54 +0000946 // 0-15 are the 16 integer registers.
947 // 16 is %rip.
948 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
949
950 return false;
951 }
952};
953
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000954}
955
Chris Lattnerd776fb12010-06-28 21:43:59 +0000956X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000957 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
958 // classified recursively so that always two fields are
959 // considered. The resulting class is calculated according to
960 // the classes of the fields in the eightbyte:
961 //
962 // (a) If both classes are equal, this is the resulting class.
963 //
964 // (b) If one of the classes is NO_CLASS, the resulting class is
965 // the other class.
966 //
967 // (c) If one of the classes is MEMORY, the result is the MEMORY
968 // class.
969 //
970 // (d) If one of the classes is INTEGER, the result is the
971 // INTEGER.
972 //
973 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
974 // MEMORY is used as class.
975 //
976 // (f) Otherwise class SSE is used.
977
978 // Accum should never be memory (we should have returned) or
979 // ComplexX87 (because this cannot be passed in a structure).
980 assert((Accum != Memory && Accum != ComplexX87) &&
981 "Invalid accumulated classification during merge.");
982 if (Accum == Field || Field == NoClass)
983 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000984 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000985 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000986 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000987 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000988 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000989 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000990 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
991 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000992 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000993 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000994}
995
Chris Lattner5c740f12010-06-30 19:14:05 +0000996void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000997 Class &Lo, Class &Hi) const {
998 // FIXME: This code can be simplified by introducing a simple value class for
999 // Class pairs with appropriate constructor methods for the various
1000 // situations.
1001
1002 // FIXME: Some of the split computations are wrong; unaligned vectors
1003 // shouldn't be passed in registers for example, so there is no chance they
1004 // can straddle an eightbyte. Verify & simplify.
1005
1006 Lo = Hi = NoClass;
1007
1008 Class &Current = OffsetBase < 64 ? Lo : Hi;
1009 Current = Memory;
1010
John McCall9dd450b2009-09-21 23:43:11 +00001011 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001012 BuiltinType::Kind k = BT->getKind();
1013
1014 if (k == BuiltinType::Void) {
1015 Current = NoClass;
1016 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1017 Lo = Integer;
1018 Hi = Integer;
1019 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1020 Current = Integer;
1021 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1022 Current = SSE;
1023 } else if (k == BuiltinType::LongDouble) {
1024 Lo = X87;
1025 Hi = X87Up;
1026 }
1027 // FIXME: _Decimal32 and _Decimal64 are SSE.
1028 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001029 return;
1030 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001031
Chris Lattnerd776fb12010-06-28 21:43:59 +00001032 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001033 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001034 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001035 return;
1036 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001037
Chris Lattnerd776fb12010-06-28 21:43:59 +00001038 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001039 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001040 return;
1041 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001042
Chris Lattnerd776fb12010-06-28 21:43:59 +00001043 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001044 if (Ty->isMemberFunctionPointerType())
1045 Lo = Hi = Integer;
1046 else
1047 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001048 return;
1049 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001050
Chris Lattnerd776fb12010-06-28 21:43:59 +00001051 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001052 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001053 if (Size == 32) {
1054 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1055 // float> as integer.
1056 Current = Integer;
1057
1058 // If this type crosses an eightbyte boundary, it should be
1059 // split.
1060 uint64_t EB_Real = (OffsetBase) / 64;
1061 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1062 if (EB_Real != EB_Imag)
1063 Hi = Lo;
1064 } else if (Size == 64) {
1065 // gcc passes <1 x double> in memory. :(
1066 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1067 return;
1068
1069 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001070 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001071 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1072 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1073 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001074 Current = Integer;
1075 else
1076 Current = SSE;
1077
1078 // If this type crosses an eightbyte boundary, it should be
1079 // split.
1080 if (OffsetBase && OffsetBase != 64)
1081 Hi = Lo;
1082 } else if (Size == 128) {
1083 Lo = SSE;
1084 Hi = SSEUp;
1085 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001086 return;
1087 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001088
Chris Lattnerd776fb12010-06-28 21:43:59 +00001089 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001090 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001091
Chris Lattner2b037972010-07-29 02:01:43 +00001092 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001093 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001094 if (Size <= 64)
1095 Current = Integer;
1096 else if (Size <= 128)
1097 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001098 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001099 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001100 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001101 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001102 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001103 Current = ComplexX87;
1104
1105 // If this complex type crosses an eightbyte boundary then it
1106 // should be split.
1107 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001108 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001109 if (Hi == NoClass && EB_Real != EB_Imag)
1110 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001111
Chris Lattnerd776fb12010-06-28 21:43:59 +00001112 return;
1113 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001114
Chris Lattner2b037972010-07-29 02:01:43 +00001115 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001116 // Arrays are treated like structures.
1117
Chris Lattner2b037972010-07-29 02:01:43 +00001118 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001119
1120 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1121 // than two eightbytes, ..., it has class MEMORY.
1122 if (Size > 128)
1123 return;
1124
1125 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1126 // fields, it has class MEMORY.
1127 //
1128 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001129 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001130 return;
1131
1132 // Otherwise implement simplified merge. We could be smarter about
1133 // this, but it isn't worth it and would be harder to verify.
1134 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001135 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001136 uint64_t ArraySize = AT->getSize().getZExtValue();
1137 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1138 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001139 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001140 Lo = merge(Lo, FieldLo);
1141 Hi = merge(Hi, FieldHi);
1142 if (Lo == Memory || Hi == Memory)
1143 break;
1144 }
1145
1146 // Do post merger cleanup (see below). Only case we worry about is Memory.
1147 if (Hi == Memory)
1148 Lo = Memory;
1149 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001150 return;
1151 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001152
Chris Lattnerd776fb12010-06-28 21:43:59 +00001153 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001154 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001155
1156 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1157 // than two eightbytes, ..., it has class MEMORY.
1158 if (Size > 128)
1159 return;
1160
Anders Carlsson20759ad2009-09-16 15:53:40 +00001161 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1162 // copy constructor or a non-trivial destructor, it is passed by invisible
1163 // reference.
1164 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1165 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001166
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001167 const RecordDecl *RD = RT->getDecl();
1168
1169 // Assume variable sized types are passed in memory.
1170 if (RD->hasFlexibleArrayMember())
1171 return;
1172
Chris Lattner2b037972010-07-29 02:01:43 +00001173 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001174
1175 // Reset Lo class, this will be recomputed.
1176 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001177
1178 // If this is a C++ record, classify the bases first.
1179 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1180 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1181 e = CXXRD->bases_end(); i != e; ++i) {
1182 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1183 "Unexpected base class!");
1184 const CXXRecordDecl *Base =
1185 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1186
1187 // Classify this field.
1188 //
1189 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1190 // single eightbyte, each is classified separately. Each eightbyte gets
1191 // initialized to class NO_CLASS.
1192 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001193 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001194 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001195 Lo = merge(Lo, FieldLo);
1196 Hi = merge(Hi, FieldHi);
1197 if (Lo == Memory || Hi == Memory)
1198 break;
1199 }
1200 }
1201
1202 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001203 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001204 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1205 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001206 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1207 bool BitField = i->isBitField();
1208
1209 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1210 // fields, it has class MEMORY.
1211 //
1212 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001213 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001214 Lo = Memory;
1215 return;
1216 }
1217
1218 // Classify this field.
1219 //
1220 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1221 // exceeds a single eightbyte, each is classified
1222 // separately. Each eightbyte gets initialized to class
1223 // NO_CLASS.
1224 Class FieldLo, FieldHi;
1225
1226 // Bit-fields require special handling, they do not force the
1227 // structure to be passed in memory even if unaligned, and
1228 // therefore they can straddle an eightbyte.
1229 if (BitField) {
1230 // Ignore padding bit-fields.
1231 if (i->isUnnamedBitfield())
1232 continue;
1233
1234 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattner2b037972010-07-29 02:01:43 +00001235 uint64_t Size =
1236 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001237
1238 uint64_t EB_Lo = Offset / 64;
1239 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1240 FieldLo = FieldHi = NoClass;
1241 if (EB_Lo) {
1242 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1243 FieldLo = NoClass;
1244 FieldHi = Integer;
1245 } else {
1246 FieldLo = Integer;
1247 FieldHi = EB_Hi ? Integer : NoClass;
1248 }
1249 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001250 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001251 Lo = merge(Lo, FieldLo);
1252 Hi = merge(Hi, FieldHi);
1253 if (Lo == Memory || Hi == Memory)
1254 break;
1255 }
1256
1257 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1258 //
1259 // (a) If one of the classes is MEMORY, the whole argument is
1260 // passed in memory.
1261 //
John McCalle0fda732011-04-21 01:20:55 +00001262 // (b) If X87UP is not preceded by X87, the whole argument is
1263 // passed in memory.
1264 //
1265 // (c) If the size of the aggregate exceeds two eightbytes and the first
1266 // eight-byte isn’t SSE or any other eightbyte isn’t SSEUP, the whole
1267 // argument is passed in memory.
1268 //
1269 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001270 //
John McCalle0fda732011-04-21 01:20:55 +00001271 // Some of these are enforced by the merging logic. Others can arise
1272 // only with unions; for example:
1273 // union { _Complex double; unsigned; }
1274 //
1275 // Note that clauses (b) and (c) were added in 0.98.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001276 if (Hi == Memory)
1277 Lo = Memory;
John McCalle0fda732011-04-21 01:20:55 +00001278 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1279 Lo = Memory;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001280 if (Hi == SSEUp && Lo != SSE)
1281 Hi = SSE;
1282 }
1283}
1284
Chris Lattner22a931e2010-06-29 06:01:59 +00001285ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001286 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1287 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001288 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001289 // Treat an enum type as its underlying type.
1290 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1291 Ty = EnumTy->getDecl()->getIntegerType();
1292
1293 return (Ty->isPromotableIntegerType() ?
1294 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1295 }
1296
1297 return ABIArgInfo::getIndirect(0);
1298}
1299
Chris Lattner22a931e2010-06-29 06:01:59 +00001300ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001301 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1302 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001303 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001304 // Treat an enum type as its underlying type.
1305 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1306 Ty = EnumTy->getDecl()->getIntegerType();
1307
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001308 return (Ty->isPromotableIntegerType() ?
1309 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001310 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001311
Daniel Dunbar53fac692010-04-21 19:49:55 +00001312 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1313 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001314
Daniel Dunbar53fac692010-04-21 19:49:55 +00001315 // Compute the byval alignment. We trust the back-end to honor the
1316 // minimum ABI alignment for byval, to make cleaner IR.
1317 const unsigned MinABIAlign = 8;
Chris Lattner2b037972010-07-29 02:01:43 +00001318 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001319 if (Align > MinABIAlign)
1320 return ABIArgInfo::getIndirect(Align);
1321 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001322}
1323
Chris Lattner4200fe42010-07-29 04:56:46 +00001324/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1325/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1326/// vector register.
1327const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner9fa15c32010-07-29 05:02:29 +00001328 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001329
Chris Lattner9fa15c32010-07-29 05:02:29 +00001330 // Wrapper structs that just contain vectors are passed just like vectors,
1331 // strip them off if present.
1332 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1333 while (STy && STy->getNumElements() == 1) {
1334 IRType = STy->getElementType(0);
1335 STy = dyn_cast<llvm::StructType>(IRType);
1336 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001337
Chris Lattner4200fe42010-07-29 04:56:46 +00001338 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001339 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-07-29 04:56:46 +00001340 const llvm::Type *EltTy = VT->getElementType();
1341 if (VT->getBitWidth() == 128 &&
1342 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1343 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1344 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1345 EltTy->isIntegerTy(128)))
1346 return VT;
1347 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001348
Chris Lattner4200fe42010-07-29 04:56:46 +00001349 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1350}
1351
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001352/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1353/// is known to either be off the end of the specified type or being in
1354/// alignment padding. The user type specified is known to be at most 128 bits
1355/// in size, and have passed through X86_64ABIInfo::classify with a successful
1356/// classification that put one of the two halves in the INTEGER class.
1357///
1358/// It is conservatively correct to return false.
1359static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1360 unsigned EndBit, ASTContext &Context) {
1361 // If the bytes being queried are off the end of the type, there is no user
1362 // data hiding here. This handles analysis of builtins, vectors and other
1363 // types that don't contain interesting padding.
1364 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1365 if (TySize <= StartBit)
1366 return true;
1367
Chris Lattner98076a22010-07-29 07:43:55 +00001368 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1369 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1370 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1371
1372 // Check each element to see if the element overlaps with the queried range.
1373 for (unsigned i = 0; i != NumElts; ++i) {
1374 // If the element is after the span we care about, then we're done..
1375 unsigned EltOffset = i*EltSize;
1376 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001377
Chris Lattner98076a22010-07-29 07:43:55 +00001378 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1379 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1380 EndBit-EltOffset, Context))
1381 return false;
1382 }
1383 // If it overlaps no elements, then it is safe to process as padding.
1384 return true;
1385 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001386
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001387 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1388 const RecordDecl *RD = RT->getDecl();
1389 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001390
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001391 // If this is a C++ record, check the bases first.
1392 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1393 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1394 e = CXXRD->bases_end(); i != e; ++i) {
1395 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1396 "Unexpected base class!");
1397 const CXXRecordDecl *Base =
1398 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001399
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001400 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001401 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001402 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001403
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001404 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1405 if (!BitsContainNoUserData(i->getType(), BaseStart,
1406 EndBit-BaseOffset, Context))
1407 return false;
1408 }
1409 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001410
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001411 // Verify that no field has data that overlaps the region of interest. Yes
1412 // this could be sped up a lot by being smarter about queried fields,
1413 // however we're only looking at structs up to 16 bytes, so we don't care
1414 // much.
1415 unsigned idx = 0;
1416 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1417 i != e; ++i, ++idx) {
1418 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001419
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001420 // If we found a field after the region we care about, then we're done.
1421 if (FieldOffset >= EndBit) break;
1422
1423 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1424 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1425 Context))
1426 return false;
1427 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001428
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001429 // If nothing in this record overlapped the area of interest, then we're
1430 // clean.
1431 return true;
1432 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001433
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001434 return false;
1435}
1436
Chris Lattnere556a712010-07-29 18:39:32 +00001437/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1438/// float member at the specified offset. For example, {int,{float}} has a
1439/// float at offset 4. It is conservatively correct for this routine to return
1440/// false.
1441static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1442 const llvm::TargetData &TD) {
1443 // Base case if we find a float.
1444 if (IROffset == 0 && IRType->isFloatTy())
1445 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001446
Chris Lattnere556a712010-07-29 18:39:32 +00001447 // If this is a struct, recurse into the field at the specified offset.
1448 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1449 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1450 unsigned Elt = SL->getElementContainingOffset(IROffset);
1451 IROffset -= SL->getElementOffset(Elt);
1452 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1453 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001454
Chris Lattnere556a712010-07-29 18:39:32 +00001455 // If this is an array, recurse into the field at the specified offset.
1456 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1457 const llvm::Type *EltTy = ATy->getElementType();
1458 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1459 IROffset -= IROffset/EltSize*EltSize;
1460 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1461 }
1462
1463 return false;
1464}
1465
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001466
1467/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1468/// low 8 bytes of an XMM register, corresponding to the SSE class.
1469const llvm::Type *X86_64ABIInfo::
1470GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1471 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001472 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001473 // pass as float if the last 4 bytes is just padding. This happens for
1474 // structs that contain 3 floats.
1475 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1476 SourceOffset*8+64, getContext()))
1477 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001478
Chris Lattnere556a712010-07-29 18:39:32 +00001479 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1480 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1481 // case.
1482 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001483 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1484 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001485
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001486 return llvm::Type::getDoubleTy(getVMContext());
1487}
1488
1489
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001490/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1491/// an 8-byte GPR. This means that we either have a scalar or we are talking
1492/// about the high or low part of an up-to-16-byte struct. This routine picks
1493/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001494/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1495/// etc).
1496///
1497/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1498/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1499/// the 8-byte value references. PrefType may be null.
1500///
1501/// SourceTy is the source level type for the entire argument. SourceOffset is
1502/// an offset into this that we're processing (which is always either 0 or 8).
1503///
Chris Lattnerc11301c2010-07-29 02:20:19 +00001504const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001505GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1506 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001507 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1508 // returning an 8-byte unit starting with it. See if we can safely use it.
1509 if (IROffset == 0) {
1510 // Pointers and int64's always fill the 8-byte unit.
1511 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1512 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001513
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001514 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1515 // goodness in the source type is just tail padding. This is allowed to
1516 // kick in for struct {double,int} on the int, but not on
1517 // struct{double,int,int} because we wouldn't return the second int. We
1518 // have to do this analysis on the source type because we can't depend on
1519 // unions being lowered a specific way etc.
1520 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1521 IRType->isIntegerTy(32)) {
1522 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001523
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001524 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1525 SourceOffset*8+64, getContext()))
1526 return IRType;
1527 }
1528 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001529
Chris Lattnerce1bd752010-07-29 04:51:12 +00001530 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001531 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001532 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001533 if (IROffset < SL->getSizeInBytes()) {
1534 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1535 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001536
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001537 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1538 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001539 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001540 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001541
Chris Lattner98076a22010-07-29 07:43:55 +00001542 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1543 const llvm::Type *EltTy = ATy->getElementType();
1544 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1545 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001546 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1547 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001548 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001549
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001550 // Okay, we don't have any better idea of what to pass, so we pass this in an
1551 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001552 unsigned TySizeInBytes =
1553 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001554
Chris Lattner3f763422010-07-29 17:34:39 +00001555 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001556
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001557 // It is always safe to classify this as an integer type up to i64 that
1558 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001559 return llvm::IntegerType::get(getVMContext(),
1560 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001561}
1562
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001563
1564/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1565/// be used as elements of a two register pair to pass or return, return a
1566/// first class aggregate to represent them. For example, if the low part of
1567/// a by-value argument should be passed as i32* and the high part as float,
1568/// return {i32*, float}.
1569static const llvm::Type *
1570GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1571 const llvm::TargetData &TD) {
1572 // In order to correctly satisfy the ABI, we need to the high part to start
1573 // at offset 8. If the high and low parts we inferred are both 4-byte types
1574 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1575 // the second element at offset 8. Check for this:
1576 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1577 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1578 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1579 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001580
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001581 // To handle this, we have to increase the size of the low part so that the
1582 // second element will start at an 8 byte offset. We can't increase the size
1583 // of the second element because it might make us access off the end of the
1584 // struct.
1585 if (HiStart != 8) {
1586 // There are only two sorts of types the ABI generation code can produce for
1587 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1588 // Promote these to a larger type.
1589 if (Lo->isFloatTy())
1590 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1591 else {
1592 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1593 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1594 }
1595 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001596
1597 const llvm::StructType *Result =
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001598 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001599
1600
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001601 // Verify that the second element is at an 8-byte offset.
1602 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1603 "Invalid x86-64 argument pair!");
1604 return Result;
1605}
1606
Chris Lattner31faff52010-07-28 23:06:14 +00001607ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001608classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001609 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1610 // classification algorithm.
1611 X86_64ABIInfo::Class Lo, Hi;
1612 classify(RetTy, 0, Lo, Hi);
1613
1614 // Check some invariants.
1615 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001616 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1617
1618 const llvm::Type *ResType = 0;
1619 switch (Lo) {
1620 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001621 if (Hi == NoClass)
1622 return ABIArgInfo::getIgnore();
1623 // If the low part is just padding, it takes no register, leave ResType
1624 // null.
1625 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1626 "Unknown missing lo part");
1627 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001628
1629 case SSEUp:
1630 case X87Up:
1631 assert(0 && "Invalid classification for lo word.");
1632
1633 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1634 // hidden argument.
1635 case Memory:
1636 return getIndirectReturnResult(RetTy);
1637
1638 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1639 // available register of the sequence %rax, %rdx is used.
1640 case Integer:
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001641 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1642 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001643
Chris Lattner1f3a0632010-07-29 21:42:50 +00001644 // If we have a sign or zero extended integer, make sure to return Extend
1645 // so that the parameter gets the right LLVM IR attributes.
1646 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1647 // Treat an enum type as its underlying type.
1648 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1649 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001650
Chris Lattner1f3a0632010-07-29 21:42:50 +00001651 if (RetTy->isIntegralOrEnumerationType() &&
1652 RetTy->isPromotableIntegerType())
1653 return ABIArgInfo::getExtend();
1654 }
Chris Lattner31faff52010-07-28 23:06:14 +00001655 break;
1656
1657 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1658 // available SSE register of the sequence %xmm0, %xmm1 is used.
1659 case SSE:
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001660 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001661 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001662
1663 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1664 // returned on the X87 stack in %st0 as 80-bit x87 number.
1665 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001666 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001667 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001668
1669 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1670 // part of the value is returned in %st0 and the imaginary part in
1671 // %st1.
1672 case ComplexX87:
1673 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner458b2aa2010-07-29 02:16:43 +00001674 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001675 llvm::Type::getX86_FP80Ty(getVMContext()),
1676 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001677 NULL);
1678 break;
1679 }
1680
Chris Lattner52b3c132010-09-01 00:20:33 +00001681 const llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001682 switch (Hi) {
1683 // Memory was handled previously and X87 should
1684 // never occur as a hi class.
1685 case Memory:
1686 case X87:
1687 assert(0 && "Invalid classification for hi word.");
1688
1689 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001690 case NoClass:
1691 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001692
Chris Lattner52b3c132010-09-01 00:20:33 +00001693 case Integer:
1694 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1695 8, RetTy, 8);
1696 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1697 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001698 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001699 case SSE:
1700 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1701 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1702 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001703 break;
1704
1705 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1706 // is passed in the upper half of the last used SSE register.
1707 //
Chris Lattner57540c52011-04-15 05:22:18 +00001708 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001709 case SSEUp:
1710 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner4200fe42010-07-29 04:56:46 +00001711 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001712 break;
1713
1714 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1715 // returned together with the previous X87 value in %st0.
1716 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001717 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001718 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001719 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001720 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001721 if (Lo != X87) {
Chris Lattner52b3c132010-09-01 00:20:33 +00001722 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1723 8, RetTy, 8);
1724 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1725 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001726 }
Chris Lattner31faff52010-07-28 23:06:14 +00001727 break;
1728 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001729
Chris Lattner52b3c132010-09-01 00:20:33 +00001730 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001731 // known to pass in the high eightbyte of the result. We do this by forming a
1732 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001733 if (HighPart)
1734 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001735
Chris Lattner1f3a0632010-07-29 21:42:50 +00001736 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001737}
1738
Chris Lattner458b2aa2010-07-29 02:16:43 +00001739ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +00001740 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001741 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001742 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001743
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001744 // Check some invariants.
1745 // FIXME: Enforce these by construction.
1746 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001747 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1748
1749 neededInt = 0;
1750 neededSSE = 0;
1751 const llvm::Type *ResType = 0;
1752 switch (Lo) {
1753 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001754 if (Hi == NoClass)
1755 return ABIArgInfo::getIgnore();
1756 // If the low part is just padding, it takes no register, leave ResType
1757 // null.
1758 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1759 "Unknown missing lo part");
1760 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001761
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001762 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1763 // on the stack.
1764 case Memory:
1765
1766 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1767 // COMPLEX_X87, it is passed in memory.
1768 case X87:
1769 case ComplexX87:
Chris Lattner22a931e2010-06-29 06:01:59 +00001770 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001771
1772 case SSEUp:
1773 case X87Up:
1774 assert(0 && "Invalid classification for lo word.");
1775
1776 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1777 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1778 // and %r9 is used.
1779 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001780 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001781
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001782 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001783 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001784
1785 // If we have a sign or zero extended integer, make sure to return Extend
1786 // so that the parameter gets the right LLVM IR attributes.
1787 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1788 // Treat an enum type as its underlying type.
1789 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1790 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001791
Chris Lattner1f3a0632010-07-29 21:42:50 +00001792 if (Ty->isIntegralOrEnumerationType() &&
1793 Ty->isPromotableIntegerType())
1794 return ABIArgInfo::getExtend();
1795 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001796
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001797 break;
1798
1799 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1800 // available SSE register is used, the registers are taken in the
1801 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001802 case SSE: {
1803 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001804 if (Hi != NoClass || !UseX86_MMXType(IRType))
Bill Wendling5cd41c42010-10-18 03:41:31 +00001805 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001806 else
Bill Wendling5cd41c42010-10-18 03:41:31 +00001807 // This is an MMX type. Treat it as such.
1808 ResType = llvm::Type::getX86_MMXTy(getVMContext());
Bill Wendling5cd41c42010-10-18 03:41:31 +00001809
Bill Wendling9987c0e2010-10-18 23:51:38 +00001810 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001811 break;
1812 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001813 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001814
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001815 const llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001816 switch (Hi) {
1817 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00001818 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001819 // which is passed in memory.
1820 case Memory:
1821 case X87:
1822 case ComplexX87:
1823 assert(0 && "Invalid classification for hi word.");
1824 break;
1825
1826 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001827
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001828 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001829 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001830 // Pick an 8-byte type based on the preferred type.
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001831 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001832
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001833 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1834 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001835 break;
1836
1837 // X87Up generally doesn't occur here (long double is passed in
1838 // memory), except in situations involving unions.
1839 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001840 case SSE:
1841 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001842
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001843 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1844 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001845
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001846 ++neededSSE;
1847 break;
1848
1849 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1850 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001851 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001852 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001853 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001854 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001855 break;
1856 }
1857
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001858 // If a high part was specified, merge it together with the low part. It is
1859 // known to pass in the high eightbyte of the result. We do this by forming a
1860 // first class struct aggregate with the high and low part: {low, high}
1861 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001862 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001863
Chris Lattner1f3a0632010-07-29 21:42:50 +00001864 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001865}
1866
Chris Lattner22326a12010-07-29 02:31:05 +00001867void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001868
Chris Lattner458b2aa2010-07-29 02:16:43 +00001869 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001870
1871 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001872 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001873
1874 // If the return value is indirect, then the hidden argument is consuming one
1875 // integer register.
1876 if (FI.getReturnInfo().isIndirect())
1877 --freeIntRegs;
1878
1879 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1880 // get assigned (in left-to-right order) for passing as follows...
1881 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1882 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00001883 unsigned neededInt, neededSSE;
1884 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001885
1886 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1887 // eightbyte of an argument, the whole argument is passed on the
1888 // stack. If registers have already been assigned for some
1889 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001890 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001891 freeIntRegs -= neededInt;
1892 freeSSERegs -= neededSSE;
1893 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00001894 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001895 }
1896 }
1897}
1898
1899static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1900 QualType Ty,
1901 CodeGenFunction &CGF) {
1902 llvm::Value *overflow_arg_area_p =
1903 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1904 llvm::Value *overflow_arg_area =
1905 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1906
1907 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1908 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1909 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1910 if (Align > 8) {
1911 // Note that we follow the ABI & gcc here, even though the type
1912 // could in theory have an alignment greater than 16. This case
1913 // shouldn't ever matter in practice.
1914
1915 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001916 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001917 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001918 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1919 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00001920 CGF.Int64Ty);
1921 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001922 overflow_arg_area =
1923 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1924 overflow_arg_area->getType(),
1925 "overflow_arg_area.align");
1926 }
1927
1928 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1929 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1930 llvm::Value *Res =
1931 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001932 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001933
1934 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1935 // l->overflow_arg_area + sizeof(type).
1936 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1937 // an 8 byte boundary.
1938
1939 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001940 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001941 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001942 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1943 "overflow_arg_area.next");
1944 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1945
1946 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1947 return Res;
1948}
1949
1950llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1951 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001952 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001953
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001954 // Assume that va_list type is correct; should be pointer to LLVM type:
1955 // struct {
1956 // i32 gp_offset;
1957 // i32 fp_offset;
1958 // i8* overflow_arg_area;
1959 // i8* reg_save_area;
1960 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00001961 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001962
Chris Lattner9723d6c2010-03-11 18:19:55 +00001963 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001964 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001965
1966 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1967 // in the registers. If not go to step 7.
1968 if (!neededInt && !neededSSE)
1969 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1970
1971 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1972 // general purpose registers needed to pass type and num_fp to hold
1973 // the number of floating point registers needed.
1974
1975 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1976 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1977 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1978 //
1979 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1980 // register save space).
1981
1982 llvm::Value *InRegs = 0;
1983 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1984 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1985 if (neededInt) {
1986 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1987 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001988 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1989 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001990 }
1991
1992 if (neededSSE) {
1993 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1994 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1995 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00001996 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1997 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001998 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1999 }
2000
2001 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2002 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2003 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2004 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2005
2006 // Emit code to load the value if it was passed in registers.
2007
2008 CGF.EmitBlock(InRegBlock);
2009
2010 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2011 // an offset of l->gp_offset and/or l->fp_offset. This may require
2012 // copying to a temporary location in case the parameter is passed
2013 // in different register classes or requires an alignment greater
2014 // than 8 for general purpose registers and 16 for XMM registers.
2015 //
2016 // FIXME: This really results in shameful code when we end up needing to
2017 // collect arguments from different places; often what should result in a
2018 // simple assembling of a structure from scattered addresses has many more
2019 // loads than necessary. Can we clean this up?
2020 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2021 llvm::Value *RegAddr =
2022 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2023 "reg_save_area");
2024 if (neededInt && neededSSE) {
2025 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002026 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002027 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2028 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2029 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2030 const llvm::Type *TyLo = ST->getElementType(0);
2031 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002032 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002033 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00002034 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2035 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002036 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2037 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002038 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2039 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002040 llvm::Value *V =
2041 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2042 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2043 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2044 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2045
Owen Anderson170229f2009-07-14 23:10:40 +00002046 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002047 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002048 } else if (neededInt) {
2049 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2050 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002051 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002052 } else if (neededSSE == 1) {
2053 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2054 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2055 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002056 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002057 assert(neededSSE == 2 && "Invalid number of needed registers!");
2058 // SSE registers are spaced 16 bytes apart in the register save
2059 // area, we need to collect the two eightbytes together.
2060 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002061 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-06-28 20:05:43 +00002062 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
2063 const llvm::Type *DblPtrTy =
2064 llvm::PointerType::getUnqual(DoubleTy);
2065 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
2066 DoubleTy, NULL);
2067 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2068 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2069 DblPtrTy));
2070 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2071 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2072 DblPtrTy));
2073 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2074 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2075 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002076 }
2077
2078 // AMD64-ABI 3.5.7p5: Step 5. Set:
2079 // l->gp_offset = l->gp_offset + num_gp * 8
2080 // l->fp_offset = l->fp_offset + num_fp * 16.
2081 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002082 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002083 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2084 gp_offset_p);
2085 }
2086 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002087 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002088 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2089 fp_offset_p);
2090 }
2091 CGF.EmitBranch(ContBlock);
2092
2093 // Emit code to load the value if it was passed in memory.
2094
2095 CGF.EmitBlock(InMemBlock);
2096 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2097
2098 // Return the appropriate result.
2099
2100 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002101 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002102 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002103 ResAddr->addIncoming(RegAddr, InRegBlock);
2104 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002105 return ResAddr;
2106}
2107
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002108ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2109
2110 if (Ty->isVoidType())
2111 return ABIArgInfo::getIgnore();
2112
2113 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2114 Ty = EnumTy->getDecl()->getIntegerType();
2115
2116 uint64_t Size = getContext().getTypeSize(Ty);
2117
2118 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002119 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2120 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002121 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2122
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002123 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2124 if (Size == 128 &&
2125 getContext().Target.getTriple().getOS() == llvm::Triple::MinGW32)
2126 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2127 Size));
2128
2129 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2130 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2131 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002132 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002133 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2134 Size));
2135
2136 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2137 }
2138
2139 if (Ty->isPromotableIntegerType())
2140 return ABIArgInfo::getExtend();
2141
2142 return ABIArgInfo::getDirect();
2143}
2144
2145void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2146
2147 QualType RetTy = FI.getReturnType();
2148 FI.getReturnInfo() = classify(RetTy);
2149
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002150 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2151 it != ie; ++it)
2152 it->info = classify(it->type);
2153}
2154
Chris Lattner04dc9572010-08-31 16:44:54 +00002155llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2156 CodeGenFunction &CGF) const {
2157 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2158 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002159
Chris Lattner04dc9572010-08-31 16:44:54 +00002160 CGBuilderTy &Builder = CGF.Builder;
2161 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2162 "ap");
2163 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2164 llvm::Type *PTy =
2165 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2166 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2167
2168 uint64_t Offset =
2169 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2170 llvm::Value *NextAddr =
2171 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2172 "ap.next");
2173 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2174
2175 return AddrTyped;
2176}
Chris Lattner0cf24192010-06-28 20:05:43 +00002177
John McCallea8d8bb2010-03-11 00:10:12 +00002178// PowerPC-32
2179
2180namespace {
2181class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2182public:
Chris Lattner2b037972010-07-29 02:01:43 +00002183 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002184
John McCallea8d8bb2010-03-11 00:10:12 +00002185 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2186 // This is recovered from gcc output.
2187 return 1; // r1 is the dedicated stack pointer
2188 }
2189
2190 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002191 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002192};
2193
2194}
2195
2196bool
2197PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2198 llvm::Value *Address) const {
2199 // This is calculated from the LLVM and GCC tables and verified
2200 // against gcc output. AFAIK all ABIs use the same encoding.
2201
2202 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2203 llvm::LLVMContext &Context = CGF.getLLVMContext();
2204
2205 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2206 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2207 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2208 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2209
2210 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002211 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002212
2213 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002214 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002215
2216 // 64-76 are various 4-byte special-purpose registers:
2217 // 64: mq
2218 // 65: lr
2219 // 66: ctr
2220 // 67: ap
2221 // 68-75 cr0-7
2222 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002223 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002224
2225 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002226 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002227
2228 // 109: vrsave
2229 // 110: vscr
2230 // 111: spe_acc
2231 // 112: spefscr
2232 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002233 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002234
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002235 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002236}
2237
2238
Chris Lattner0cf24192010-06-28 20:05:43 +00002239//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002240// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002241//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002242
2243namespace {
2244
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002245class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002246public:
2247 enum ABIKind {
2248 APCS = 0,
2249 AAPCS = 1,
2250 AAPCS_VFP
2251 };
2252
2253private:
2254 ABIKind Kind;
2255
2256public:
Chris Lattner2b037972010-07-29 02:01:43 +00002257 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002258
2259private:
2260 ABIKind getABIKind() const { return Kind; }
2261
Chris Lattner458b2aa2010-07-29 02:16:43 +00002262 ABIArgInfo classifyReturnType(QualType RetTy) const;
2263 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002264
Chris Lattner22326a12010-07-29 02:31:05 +00002265 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002266
2267 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2268 CodeGenFunction &CGF) const;
2269};
2270
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002271class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2272public:
Chris Lattner2b037972010-07-29 02:01:43 +00002273 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2274 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002275
2276 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2277 return 13;
2278 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002279};
2280
Daniel Dunbard59655c2009-09-12 00:59:49 +00002281}
2282
Chris Lattner22326a12010-07-29 02:31:05 +00002283void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002284 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002285 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002286 it != ie; ++it)
2287 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002288
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002289 // Always honor user-specified calling convention.
2290 if (FI.getCallingConvention() != llvm::CallingConv::C)
2291 return;
2292
2293 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002294 llvm::CallingConv::ID DefaultCC;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002295 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2296 if (Env == "gnueabi" || Env == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002297 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002298 else
2299 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002300
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002301 // If user did not ask for specific calling convention explicitly (e.g. via
2302 // pcs attribute), set effective calling convention if it's different than ABI
2303 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002304 switch (getABIKind()) {
2305 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002306 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2307 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002308 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002309 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002310 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2311 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002312 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002313 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002314 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2315 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002316 break;
2317 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002318}
2319
Chris Lattner458b2aa2010-07-29 02:16:43 +00002320ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002321 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002322 // Treat an enum type as its underlying type.
2323 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2324 Ty = EnumTy->getDecl()->getIntegerType();
2325
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002326 return (Ty->isPromotableIntegerType() ?
2327 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002328 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002329
Daniel Dunbar09d33622009-09-14 21:54:03 +00002330 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002331 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002332 return ABIArgInfo::getIgnore();
2333
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002334 // Structures with either a non-trivial destructor or a non-trivial
2335 // copy constructor are always indirect.
2336 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2337 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2338
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002339 // Otherwise, pass by coercing to a structure of the appropriate size.
2340 //
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002341 // FIXME: This doesn't handle alignment > 64 bits.
2342 const llvm::Type* ElemTy;
2343 unsigned SizeRegs;
Stuart Hastings9f02fd92011-04-28 21:35:59 +00002344 if (getContext().getTypeSizeInChars(Ty) <= CharUnits::fromQuantity(64)) {
Eric Christopher6f095d62011-04-26 01:02:04 +00002345 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2346 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings4b214952011-04-28 18:16:06 +00002347 } else if (getABIKind() == ARMABIInfo::APCS) {
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002348 // Initial ARM ByVal support is APCS-only.
2349 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
2350 } else {
2351 // FIXME: This is kind of nasty... but there isn't much choice
2352 // because most of the ARM calling conventions don't yet support
2353 // byval.
2354 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2355 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002356 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002357
Stuart Hastingsdd77c8e2011-04-28 19:24:47 +00002358 const llvm::Type *STy =
2359 llvm::StructType::get(getVMContext(),
2360 llvm::ArrayType::get(ElemTy, SizeRegs), NULL, NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002361 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002362}
2363
Chris Lattner458b2aa2010-07-29 02:16:43 +00002364static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002365 llvm::LLVMContext &VMContext) {
2366 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2367 // is called integer-like if its size is less than or equal to one word, and
2368 // the offset of each of its addressable sub-fields is zero.
2369
2370 uint64_t Size = Context.getTypeSize(Ty);
2371
2372 // Check that the type fits in a word.
2373 if (Size > 32)
2374 return false;
2375
2376 // FIXME: Handle vector types!
2377 if (Ty->isVectorType())
2378 return false;
2379
Daniel Dunbard53bac72009-09-14 02:20:34 +00002380 // Float types are never treated as "integer like".
2381 if (Ty->isRealFloatingType())
2382 return false;
2383
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002384 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002385 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002386 return true;
2387
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002388 // Small complex integer types are "integer like".
2389 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2390 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002391
2392 // Single element and zero sized arrays should be allowed, by the definition
2393 // above, but they are not.
2394
2395 // Otherwise, it must be a record type.
2396 const RecordType *RT = Ty->getAs<RecordType>();
2397 if (!RT) return false;
2398
2399 // Ignore records with flexible arrays.
2400 const RecordDecl *RD = RT->getDecl();
2401 if (RD->hasFlexibleArrayMember())
2402 return false;
2403
2404 // Check that all sub-fields are at offset 0, and are themselves "integer
2405 // like".
2406 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2407
2408 bool HadField = false;
2409 unsigned idx = 0;
2410 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2411 i != e; ++i, ++idx) {
2412 const FieldDecl *FD = *i;
2413
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002414 // Bit-fields are not addressable, we only need to verify they are "integer
2415 // like". We still have to disallow a subsequent non-bitfield, for example:
2416 // struct { int : 0; int x }
2417 // is non-integer like according to gcc.
2418 if (FD->isBitField()) {
2419 if (!RD->isUnion())
2420 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002421
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002422 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2423 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002424
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002425 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002426 }
2427
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002428 // Check if this field is at offset 0.
2429 if (Layout.getFieldOffset(idx) != 0)
2430 return false;
2431
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002432 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2433 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002434
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002435 // Only allow at most one field in a structure. This doesn't match the
2436 // wording above, but follows gcc in situations with a field following an
2437 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002438 if (!RD->isUnion()) {
2439 if (HadField)
2440 return false;
2441
2442 HadField = true;
2443 }
2444 }
2445
2446 return true;
2447}
2448
Chris Lattner458b2aa2010-07-29 02:16:43 +00002449ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002450 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002451 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002452
Daniel Dunbar19964db2010-09-23 01:54:32 +00002453 // Large vector types should be returned via memory.
2454 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2455 return ABIArgInfo::getIndirect(0);
2456
John McCalla1dee5302010-08-22 10:59:02 +00002457 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002458 // Treat an enum type as its underlying type.
2459 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2460 RetTy = EnumTy->getDecl()->getIntegerType();
2461
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002462 return (RetTy->isPromotableIntegerType() ?
2463 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002464 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002465
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002466 // Structures with either a non-trivial destructor or a non-trivial
2467 // copy constructor are always indirect.
2468 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2469 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2470
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002471 // Are we following APCS?
2472 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002473 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002474 return ABIArgInfo::getIgnore();
2475
Daniel Dunbareedf1512010-02-01 23:31:19 +00002476 // Complex types are all returned as packed integers.
2477 //
2478 // FIXME: Consider using 2 x vector types if the back end handles them
2479 // correctly.
2480 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002481 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002482 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002483
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002484 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002485 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002486 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002487 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002488 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002489 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002490 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002491 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2492 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002493 }
2494
2495 // Otherwise return in memory.
2496 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002497 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002498
2499 // Otherwise this is an AAPCS variant.
2500
Chris Lattner458b2aa2010-07-29 02:16:43 +00002501 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002502 return ABIArgInfo::getIgnore();
2503
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002504 // Aggregates <= 4 bytes are returned in r0; other aggregates
2505 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002506 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002507 if (Size <= 32) {
2508 // Return in the smallest viable integer type.
2509 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002510 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002511 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002512 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2513 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002514 }
2515
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002516 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002517}
2518
2519llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002520 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002521 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002522 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002523 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002524
2525 CGBuilderTy &Builder = CGF.Builder;
2526 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2527 "ap");
2528 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2529 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002530 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002531 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2532
2533 uint64_t Offset =
2534 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2535 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002536 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002537 "ap.next");
2538 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2539
2540 return AddrTyped;
2541}
2542
Chris Lattner0cf24192010-06-28 20:05:43 +00002543//===----------------------------------------------------------------------===//
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002544// PTX ABI Implementation
2545//===----------------------------------------------------------------------===//
2546
2547namespace {
2548
2549class PTXABIInfo : public ABIInfo {
2550public:
2551 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2552
2553 ABIArgInfo classifyReturnType(QualType RetTy) const;
2554 ABIArgInfo classifyArgumentType(QualType Ty) const;
2555
2556 virtual void computeInfo(CGFunctionInfo &FI) const;
2557 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2558 CodeGenFunction &CFG) const;
2559};
2560
2561class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2562public:
2563 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2564 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
2565};
2566
2567ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2568 if (RetTy->isVoidType())
2569 return ABIArgInfo::getIgnore();
2570 if (isAggregateTypeForABI(RetTy))
2571 return ABIArgInfo::getIndirect(0);
2572 return ABIArgInfo::getDirect();
2573}
2574
2575ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2576 if (isAggregateTypeForABI(Ty))
2577 return ABIArgInfo::getIndirect(0);
2578
2579 return ABIArgInfo::getDirect();
2580}
2581
2582void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2583 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2584 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2585 it != ie; ++it)
2586 it->info = classifyArgumentType(it->type);
2587
2588 // Always honor user-specified calling convention.
2589 if (FI.getCallingConvention() != llvm::CallingConv::C)
2590 return;
2591
2592 // Calling convention as default by an ABI.
2593 llvm::CallingConv::ID DefaultCC;
2594 llvm::StringRef Env = getContext().Target.getTriple().getEnvironmentName();
2595 if (Env == "device")
2596 DefaultCC = llvm::CallingConv::PTX_Device;
2597 else
2598 DefaultCC = llvm::CallingConv::PTX_Kernel;
2599
2600 FI.setEffectiveCallingConvention(DefaultCC);
2601}
2602
2603llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2604 CodeGenFunction &CFG) const {
2605 llvm_unreachable("PTX does not support varargs");
2606 return 0;
2607}
2608
2609}
2610
2611//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002612// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002613//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002614
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002615namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002616
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002617class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002618public:
2619 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2620
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002621 bool isPromotableIntegerType(QualType Ty) const;
2622
Chris Lattner458b2aa2010-07-29 02:16:43 +00002623 ABIArgInfo classifyReturnType(QualType RetTy) const;
2624 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002625
Chris Lattner22326a12010-07-29 02:31:05 +00002626 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002627 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002628 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2629 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002630 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002631 }
2632
2633 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2634 CodeGenFunction &CGF) const;
2635};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002636
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002637class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2638public:
Chris Lattner2b037972010-07-29 02:01:43 +00002639 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2640 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002641};
2642
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002643}
2644
2645bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2646 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002647 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002648 switch (BT->getKind()) {
2649 case BuiltinType::Bool:
2650 case BuiltinType::Char_S:
2651 case BuiltinType::Char_U:
2652 case BuiltinType::SChar:
2653 case BuiltinType::UChar:
2654 case BuiltinType::Short:
2655 case BuiltinType::UShort:
2656 case BuiltinType::Int:
2657 case BuiltinType::UInt:
2658 return true;
2659 default:
2660 return false;
2661 }
2662 return false;
2663}
2664
2665llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2666 CodeGenFunction &CGF) const {
2667 // FIXME: Implement
2668 return 0;
2669}
2670
2671
Chris Lattner458b2aa2010-07-29 02:16:43 +00002672ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2673 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002674 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002675 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002676 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002677
2678 return (isPromotableIntegerType(RetTy) ?
2679 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002680}
2681
Chris Lattner458b2aa2010-07-29 02:16:43 +00002682ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002683 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002684 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002685
2686 return (isPromotableIntegerType(Ty) ?
2687 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002688}
2689
Chris Lattner0cf24192010-06-28 20:05:43 +00002690//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00002691// MBlaze ABI Implementation
2692//===----------------------------------------------------------------------===//
2693
2694namespace {
2695
2696class MBlazeABIInfo : public ABIInfo {
2697public:
2698 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2699
2700 bool isPromotableIntegerType(QualType Ty) const;
2701
2702 ABIArgInfo classifyReturnType(QualType RetTy) const;
2703 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2704
2705 virtual void computeInfo(CGFunctionInfo &FI) const {
2706 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2707 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2708 it != ie; ++it)
2709 it->info = classifyArgumentType(it->type);
2710 }
2711
2712 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2713 CodeGenFunction &CGF) const;
2714};
2715
2716class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2717public:
2718 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2719 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2720 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2721 CodeGen::CodeGenModule &M) const;
2722};
2723
2724}
2725
2726bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2727 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2728 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2729 switch (BT->getKind()) {
2730 case BuiltinType::Bool:
2731 case BuiltinType::Char_S:
2732 case BuiltinType::Char_U:
2733 case BuiltinType::SChar:
2734 case BuiltinType::UChar:
2735 case BuiltinType::Short:
2736 case BuiltinType::UShort:
2737 return true;
2738 default:
2739 return false;
2740 }
2741 return false;
2742}
2743
2744llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2745 CodeGenFunction &CGF) const {
2746 // FIXME: Implement
2747 return 0;
2748}
2749
2750
2751ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2752 if (RetTy->isVoidType())
2753 return ABIArgInfo::getIgnore();
2754 if (isAggregateTypeForABI(RetTy))
2755 return ABIArgInfo::getIndirect(0);
2756
2757 return (isPromotableIntegerType(RetTy) ?
2758 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2759}
2760
2761ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2762 if (isAggregateTypeForABI(Ty))
2763 return ABIArgInfo::getIndirect(0);
2764
2765 return (isPromotableIntegerType(Ty) ?
2766 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2767}
2768
2769void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2770 llvm::GlobalValue *GV,
2771 CodeGen::CodeGenModule &M)
2772 const {
2773 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2774 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002775
Wesley Peck36a1f682010-12-19 19:57:51 +00002776 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2777 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2778 CC = llvm::CallingConv::MBLAZE_INTR;
2779 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2780 CC = llvm::CallingConv::MBLAZE_SVOL;
2781
2782 if (CC != llvm::CallingConv::C) {
2783 // Handle 'interrupt_handler' attribute:
2784 llvm::Function *F = cast<llvm::Function>(GV);
2785
2786 // Step 1: Set ISR calling convention.
2787 F->setCallingConv(CC);
2788
2789 // Step 2: Add attributes goodness.
2790 F->addFnAttr(llvm::Attribute::NoInline);
2791 }
2792
2793 // Step 3: Emit _interrupt_handler alias.
2794 if (CC == llvm::CallingConv::MBLAZE_INTR)
2795 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2796 "_interrupt_handler", GV, &M.getModule());
2797}
2798
2799
2800//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002801// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002802//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002803
2804namespace {
2805
2806class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2807public:
Chris Lattner2b037972010-07-29 02:01:43 +00002808 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2809 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002810 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2811 CodeGen::CodeGenModule &M) const;
2812};
2813
2814}
2815
2816void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2817 llvm::GlobalValue *GV,
2818 CodeGen::CodeGenModule &M) const {
2819 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2820 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2821 // Handle 'interrupt' attribute:
2822 llvm::Function *F = cast<llvm::Function>(GV);
2823
2824 // Step 1: Set ISR calling convention.
2825 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2826
2827 // Step 2: Add attributes goodness.
2828 F->addFnAttr(llvm::Attribute::NoInline);
2829
2830 // Step 3: Emit ISR vector alias.
2831 unsigned Num = attr->getNumber() + 0xffe0;
2832 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Benjamin Kramer8c173cc2010-11-12 15:42:18 +00002833 "vector_" + llvm::Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002834 GV, &M.getModule());
2835 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002836 }
2837}
2838
Chris Lattner0cf24192010-06-28 20:05:43 +00002839//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002840// MIPS ABI Implementation. This works for both little-endian and
2841// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002842//===----------------------------------------------------------------------===//
2843
John McCall943fae92010-05-27 06:19:26 +00002844namespace {
2845class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2846public:
Chris Lattner2b037972010-07-29 02:01:43 +00002847 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2848 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002849
2850 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2851 return 29;
2852 }
2853
2854 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002855 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002856};
2857}
2858
2859bool
2860MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2861 llvm::Value *Address) const {
2862 // This information comes from gcc's implementation, which seems to
2863 // as canonical as it gets.
2864
2865 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2866 llvm::LLVMContext &Context = CGF.getLLVMContext();
2867
2868 // Everything on MIPS is 4 bytes. Double-precision FP registers
2869 // are aliased to pairs of single-precision FP registers.
2870 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2871 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2872
2873 // 0-31 are the general purpose registers, $0 - $31.
2874 // 32-63 are the floating-point registers, $f0 - $f31.
2875 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2876 // 66 is the (notional, I think) register for signal-handler return.
2877 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2878
2879 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2880 // They are one bit wide and ignored here.
2881
2882 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2883 // (coprocessor 1 is the FP unit)
2884 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2885 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2886 // 176-181 are the DSP accumulator registers.
2887 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2888
2889 return false;
2890}
2891
2892
Chris Lattner2b037972010-07-29 02:01:43 +00002893const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002894 if (TheTargetCodeGenInfo)
2895 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002896
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002897 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2898 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002899
Chris Lattner22a931e2010-06-29 06:01:59 +00002900 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002901 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002902 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002903 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002904
John McCall943fae92010-05-27 06:19:26 +00002905 case llvm::Triple::mips:
2906 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002907 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002908
Daniel Dunbard59655c2009-09-12 00:59:49 +00002909 case llvm::Triple::arm:
2910 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00002911 {
2912 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002913
Sandeep Patel45df3dd2011-04-05 00:23:47 +00002914 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
2915 Kind = ARMABIInfo::APCS;
2916 else if (CodeGenOpts.FloatABI == "hard")
2917 Kind = ARMABIInfo::AAPCS_VFP;
2918
2919 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
2920 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00002921
John McCallea8d8bb2010-03-11 00:10:12 +00002922 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002923 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002924
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002925 case llvm::Triple::ptx32:
2926 case llvm::Triple::ptx64:
2927 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
2928
Daniel Dunbard59655c2009-09-12 00:59:49 +00002929 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002930 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002931
Wesley Peck36a1f682010-12-19 19:57:51 +00002932 case llvm::Triple::mblaze:
2933 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
2934
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002935 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002936 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002937
Daniel Dunbar40165182009-08-24 09:10:05 +00002938 case llvm::Triple::x86:
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00002939 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002940 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002941 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00002942
2943 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002944 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002945 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002946 case llvm::Triple::AuroraUX:
2947 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002948 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002949 case llvm::Triple::OpenBSD:
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00002950 case llvm::Triple::NetBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002951 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002952 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002953
2954 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002955 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002956 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002957 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002958
Daniel Dunbare3532f82009-08-24 08:52:16 +00002959 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00002960 switch (Triple.getOS()) {
2961 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00002962 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00002963 case llvm::Triple::Cygwin:
2964 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2965 default:
2966 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2967 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00002968 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002969}