blob: 0ce5957246d1f9219bea1a949640eace276b0a9a [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Sandeep Patel34c1af82011-04-05 00:23:47 +000019#include "clang/Frontend/CodeGenOptions.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000020#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000021#include "llvm/Target/TargetData.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-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 McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-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 Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 raw_ostream &OS = llvm::errs();
Daniel Dunbar28df7a52009-12-03 09:13:49 +000061 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
Chris Lattner2acc6e32011-07-18 04:24:23 +000065 if (llvm::Type *Ty = getCoerceToType())
Chris Lattner800588f2010-07-29 06:26:06 +000066 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Joerg Sonnenbergere9b5d772011-07-15 18:23:44 +000078 << " ByVal=" << getIndirectByVal()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000079 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
John McCall49e34be2011-08-30 01:42:09 +000090// If someone can figure out a general rule for this, that would be great.
91// It's probably just doomed to be platform-dependent, though.
92unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
93 // Verified for:
94 // x86-64 FreeBSD, Linux, Darwin
95 // x86-32 FreeBSD, Linux, Darwin
96 // PowerPC Linux, Darwin
97 // ARM Darwin (*not* EABI)
98 return 32;
99}
100
Eli Friedman3ed79032011-12-01 04:53:19 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(
102 const CodeGen::CGFunctionInfo &) const {
John McCall01f151e2011-09-21 08:08:30 +0000103 // The following conventions are known to require this to be false:
104 // x86_stdcall
105 // MIPS
106 // For everything else, we just prefer false unless we opt out.
107 return false;
108}
109
Daniel Dunbar98303b92009-09-13 08:03:58 +0000110static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000111
112/// isEmptyField - Return true iff a the field is "empty", that is it
113/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +0000114static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
115 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000116 if (FD->isUnnamedBitfield())
117 return true;
118
119 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000120
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000121 // Constant arrays of empty records count as empty, strip them off.
122 // Constant arrays of zero length always count as empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123 if (AllowArrays)
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
125 if (AT->getSize() == 0)
126 return true;
Daniel Dunbar98303b92009-09-13 08:03:58 +0000127 FT = AT->getElementType();
Eli Friedman7e7ad3f2011-11-18 03:47:20 +0000128 }
Daniel Dunbar98303b92009-09-13 08:03:58 +0000129
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130 const RecordType *RT = FT->getAs<RecordType>();
131 if (!RT)
132 return false;
133
134 // C++ record fields are never empty, at least in the Itanium ABI.
135 //
136 // FIXME: We should use a predicate for whether this behavior is true in the
137 // current ABI.
138 if (isa<CXXRecordDecl>(RT->getDecl()))
139 return false;
140
Daniel Dunbar98303b92009-09-13 08:03:58 +0000141 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000142}
143
144/// isEmptyRecord - Return true iff a structure contains only empty
145/// fields. Note that a structure with a flexible array member is not
146/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000147static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000148 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000149 if (!RT)
150 return 0;
151 const RecordDecl *RD = RT->getDecl();
152 if (RD->hasFlexibleArrayMember())
153 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000154
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000155 // If this is a C++ record, check the bases first.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Argyrios Kyrtzidisc5f18f32011-05-17 02:17:52 +0000157 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158 e = CXXRD->bases_end(); i != e; ++i)
159 if (!isEmptyRecord(Context, i->getType(), true))
160 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000161
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
163 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000164 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000165 return false;
166 return true;
167}
168
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000169/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
170/// a non-trivial destructor or a non-trivial copy constructor.
171static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
172 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
173 if (!RD)
174 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000175
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000176 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
177}
178
179/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
180/// a record type with either a non-trivial destructor or a non-trivial copy
181/// constructor.
182static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
183 const RecordType *RT = T->getAs<RecordType>();
184 if (!RT)
185 return false;
186
187 return hasNonTrivialDestructorOrCopyConstructor(RT);
188}
189
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000190/// isSingleElementStruct - Determine if a structure is a "single
191/// element struct", i.e. it has exactly one non-empty field or
192/// exactly one field which is itself a single element
193/// struct. Structures with flexible array members are never
194/// considered single element structs.
195///
196/// \return The field declaration for the single non-empty field, if
197/// it exists.
198static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
199 const RecordType *RT = T->getAsStructureType();
200 if (!RT)
201 return 0;
202
203 const RecordDecl *RD = RT->getDecl();
204 if (RD->hasFlexibleArrayMember())
205 return 0;
206
207 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000208
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000209 // If this is a C++ record, check the bases first.
210 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
211 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
212 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000213 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000214 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000215 continue;
216
217 // If we already found an element then this isn't a single-element struct.
218 if (Found)
219 return 0;
220
221 // If this is non-empty and not a single element struct, the composite
222 // cannot be a single element struct.
223 Found = isSingleElementStruct(i->getType(), Context);
224 if (!Found)
225 return 0;
226 }
227 }
228
229 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000232 const FieldDecl *FD = *i;
233 QualType FT = FD->getType();
234
235 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000236 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000237 continue;
238
239 // If we already found an element then this isn't a single-element
240 // struct.
241 if (Found)
242 return 0;
243
244 // Treat single element arrays as the element.
245 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
246 if (AT->getSize().getZExtValue() != 1)
247 break;
248 FT = AT->getElementType();
249 }
250
John McCalld608cdb2010-08-22 10:59:02 +0000251 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000252 Found = FT.getTypePtr();
253 } else {
254 Found = isSingleElementStruct(FT, Context);
255 if (!Found)
256 return 0;
257 }
258 }
259
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000260 // We don't consider a struct a single-element struct if it has
261 // padding beyond the element type.
262 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
263 return 0;
264
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000265 return Found;
266}
267
268static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000269 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000270 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
271 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000272 return false;
273
274 uint64_t Size = Context.getTypeSize(Ty);
275 return Size == 32 || Size == 64;
276}
277
Daniel Dunbar53012f42009-11-09 01:33:53 +0000278/// canExpandIndirectArgument - Test whether an argument type which is to be
279/// passed indirectly (on the stack) would have the equivalent layout if it was
280/// expanded into separate arguments. If so, we prefer to do the latter to avoid
281/// inhibiting optimizations.
282///
283// FIXME: This predicate is missing many cases, currently it just follows
284// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
285// should probably make this smarter, or better yet make the LLVM backend
286// capable of handling it.
287static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
288 // We can only expand structure types.
289 const RecordType *RT = Ty->getAs<RecordType>();
290 if (!RT)
291 return false;
292
293 // We can only expand (C) structures.
294 //
295 // FIXME: This needs to be generalized to handle classes as well.
296 const RecordDecl *RD = RT->getDecl();
297 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
298 return false;
299
Eli Friedman506d4e32011-11-18 01:32:26 +0000300 uint64_t Size = 0;
301
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
303 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000304 const FieldDecl *FD = *i;
305
306 if (!is32Or64BitBasicType(FD->getType(), Context))
307 return false;
308
309 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
310 // how to expand them yet, and the predicate for telling if a bitfield still
311 // counts as "basic" is more complicated than what we were doing previously.
312 if (FD->isBitField())
313 return false;
Eli Friedman506d4e32011-11-18 01:32:26 +0000314
315 Size += Context.getTypeSize(FD->getType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000316 }
317
Eli Friedman506d4e32011-11-18 01:32:26 +0000318 // Make sure there are not any holes in the struct.
319 if (Size != Context.getTypeSize(Ty))
320 return false;
321
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322 return true;
323}
324
325namespace {
326/// DefaultABIInfo - The default implementation for ABI specific
327/// details. This implementation provides information which results in
328/// self-consistent and sensible LLVM IR generation, but does not
329/// conform to any particular ABI.
330class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000331public:
332 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000333
Chris Lattnera3c109b2010-07-29 02:16:43 +0000334 ABIArgInfo classifyReturnType(QualType RetTy) const;
335 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000336
Chris Lattneree5dcd02010-07-29 02:31:05 +0000337 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000341 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000342 }
343
344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345 CodeGenFunction &CGF) const;
346};
347
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000348class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
349public:
Chris Lattnerea044322010-07-29 02:01:43 +0000350 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000352};
353
354llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
355 CodeGenFunction &CGF) const {
356 return 0;
357}
358
Chris Lattnera3c109b2010-07-29 02:16:43 +0000359ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
Jan Wen Voung90306932011-11-03 00:59:44 +0000360 if (isAggregateTypeForABI(Ty)) {
361 // Records with non trivial destructors/constructors should not be passed
362 // by value.
363 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
364 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
365
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000366 return ABIArgInfo::getIndirect(0);
Jan Wen Voung90306932011-11-03 00:59:44 +0000367 }
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000368
Chris Lattnera14db752010-03-11 18:19:55 +0000369 // Treat an enum type as its underlying type.
370 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
371 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000372
Chris Lattnera14db752010-03-11 18:19:55 +0000373 return (Ty->isPromotableIntegerType() ?
374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000375}
376
Bob Wilson0024f942011-01-10 23:54:17 +0000377ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
378 if (RetTy->isVoidType())
379 return ABIArgInfo::getIgnore();
380
381 if (isAggregateTypeForABI(RetTy))
382 return ABIArgInfo::getIndirect(0);
383
384 // Treat an enum type as its underlying type.
385 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
386 RetTy = EnumTy->getDecl()->getIntegerType();
387
388 return (RetTy->isPromotableIntegerType() ?
389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
390}
391
Bill Wendlingbb465d72010-10-18 03:41:31 +0000392/// UseX86_MMXType - Return true if this is an MMX type that should use the special
393/// x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000394bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000395 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
396 // special x86_mmx type.
397 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
398 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
399 IRType->getScalarSizeInBits() != 64;
400}
401
Jay Foadef6de3d2011-07-11 09:56:20 +0000402static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000403 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000404 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000405 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000406 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
407 return Ty;
408}
409
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000410//===----------------------------------------------------------------------===//
411// X86-32 ABI Implementation
412//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000413
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000414/// X86_32ABIInfo - The X86-32 ABI information.
415class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000416 static const unsigned MinABIStackAlignInBytes = 4;
417
David Chisnall1e4249c2009-08-17 23:08:21 +0000418 bool IsDarwinVectorABI;
419 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000420 bool IsMMXDisabled;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000421
422 static bool isRegisterSize(unsigned Size) {
423 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
424 }
425
426 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
427
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000428 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
429 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000430 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000431
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000432 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000433 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000434
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000435public:
Chris Lattnerea044322010-07-29 02:01:43 +0000436
Chris Lattnera3c109b2010-07-29 02:16:43 +0000437 ABIArgInfo classifyReturnType(QualType RetTy) const;
438 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000439
Chris Lattneree5dcd02010-07-29 02:31:05 +0000440 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000441 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000442 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
443 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000444 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000445 }
446
447 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
448 CodeGenFunction &CGF) const;
449
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000450 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
451 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
452 IsMMXDisabled(m) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000453};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000455class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
456public:
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000457 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m)
458 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000459
460 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
461 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000462
463 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
464 // Darwin uses different dwarf register numbers for EH.
465 if (CGM.isTargetDarwin()) return 5;
466
467 return 4;
468 }
469
470 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
471 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000472
Jay Foadef6de3d2011-07-11 09:56:20 +0000473 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000474 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000475 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000476 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
477 }
478
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000479};
480
481}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000482
483/// shouldReturnTypeInRegister - Determine if the given type should be
484/// passed in a register (for the Darwin ABI).
485bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
486 ASTContext &Context) {
487 uint64_t Size = Context.getTypeSize(Ty);
488
489 // Type must be register sized.
490 if (!isRegisterSize(Size))
491 return false;
492
493 if (Ty->isVectorType()) {
494 // 64- and 128- bit vectors inside structures are not returned in
495 // registers.
496 if (Size == 64 || Size == 128)
497 return false;
498
499 return true;
500 }
501
Daniel Dunbar77115232010-05-15 00:00:30 +0000502 // If this is a builtin, pointer, enum, complex type, member pointer, or
503 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000504 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000505 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000506 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000507 return true;
508
509 // Arrays are treated like records.
510 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
511 return shouldReturnTypeInRegister(AT->getElementType(), Context);
512
513 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000514 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515 if (!RT) return false;
516
Anders Carlssona8874232010-01-27 03:25:19 +0000517 // FIXME: Traverse bases here too.
518
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519 // Structure types are passed in register if all fields would be
520 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000521 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
522 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000523 const FieldDecl *FD = *i;
524
525 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000526 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000527 continue;
528
529 // Check fields recursively.
530 if (!shouldReturnTypeInRegister(FD->getType(), Context))
531 return false;
532 }
533
534 return true;
535}
536
Chris Lattnera3c109b2010-07-29 02:16:43 +0000537ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
538 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000539 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000540
Chris Lattnera3c109b2010-07-29 02:16:43 +0000541 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000542 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000543 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000544 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545
546 // 128-bit vectors are a special case; they are returned in
547 // registers and we need to make sure to pick a type the LLVM
548 // backend will like.
549 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000550 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000551 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000552
553 // Always return in register if it fits in a general purpose
554 // register, or if it is 64 bits and has a single element.
555 if ((Size == 8 || Size == 16 || Size == 32) ||
556 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000557 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000558 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000559
560 return ABIArgInfo::getIndirect(0);
561 }
562
563 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000564 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000565
John McCalld608cdb2010-08-22 10:59:02 +0000566 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000567 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000568 // Structures with either a non-trivial destructor or a non-trivial
569 // copy constructor are always indirect.
570 if (hasNonTrivialDestructorOrCopyConstructor(RT))
571 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000572
Anders Carlsson40092972009-10-20 22:07:59 +0000573 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000574 if (RT->getDecl()->hasFlexibleArrayMember())
575 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000576 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000577
David Chisnall1e4249c2009-08-17 23:08:21 +0000578 // If specified, structs and unions are always indirect.
579 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000580 return ABIArgInfo::getIndirect(0);
581
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000582 // Small structures which are register sized are generally returned
583 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000584 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
585 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000586
587 // As a special-case, if the struct is a "single-element" struct, and
588 // the field is of type "float" or "double", return it in a
589 // floating-point register. We apply a similar transformation for
590 // pointer types to improve the quality of the generated IR.
591 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
592 if (SeltTy->isRealFloatingType() || SeltTy->hasPointerRepresentation())
593 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
594
595 // FIXME: We should be able to narrow this integer in cases with dead
596 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000597 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000598 }
599
600 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000602
Chris Lattnera3c109b2010-07-29 02:16:43 +0000603 // Treat an enum type as its underlying type.
604 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
605 RetTy = EnumTy->getDecl()->getIntegerType();
606
607 return (RetTy->isPromotableIntegerType() ?
608 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000609}
610
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000611static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
612 const RecordType *RT = Ty->getAs<RecordType>();
613 if (!RT)
614 return 0;
615 const RecordDecl *RD = RT->getDecl();
616
617 // If this is a C++ record, check the bases first.
618 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
619 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
620 e = CXXRD->bases_end(); i != e; ++i)
621 if (!isRecordWithSSEVectorType(Context, i->getType()))
622 return false;
623
624 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
625 i != e; ++i) {
626 QualType FT = i->getType();
627
Eli Friedman7b1fb812011-11-18 02:12:09 +0000628 if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000629 return true;
630
631 if (isRecordWithSSEVectorType(Context, FT))
632 return true;
633 }
634
635 return false;
636}
637
Daniel Dunbare59d8582010-09-16 20:42:06 +0000638unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
639 unsigned Align) const {
640 // Otherwise, if the alignment is less than or equal to the minimum ABI
641 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000642 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000643 return 0; // Use default alignment.
644
645 // On non-Darwin, the stack type alignment is always 4.
646 if (!IsDarwinVectorABI) {
647 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000648 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000649 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000650
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000651 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7b1fb812011-11-18 02:12:09 +0000652 if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000653 return 16;
654
655 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000656}
657
Chris Lattnera3c109b2010-07-29 02:16:43 +0000658ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000659 if (!ByVal)
660 return ABIArgInfo::getIndirect(0, false);
661
Daniel Dunbare59d8582010-09-16 20:42:06 +0000662 // Compute the byval alignment.
663 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
664 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
665 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000666 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000667
668 // If the stack alignment is less than the type alignment, realign the
669 // argument.
670 if (StackAlign < TypeAlign)
671 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
672 /*Realign=*/true);
673
674 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000675}
676
Chris Lattnera3c109b2010-07-29 02:16:43 +0000677ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000678 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000679 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000680 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000681 if (const RecordType *RT = Ty->getAs<RecordType>()) {
682 // Structures with either a non-trivial destructor or a non-trivial
683 // copy constructor are always indirect.
684 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000685 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000686
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000687 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000688 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000689 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000690
Eli Friedman5a4d3522011-11-18 00:28:11 +0000691 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000692 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000693 return ABIArgInfo::getIgnore();
694
Daniel Dunbar53012f42009-11-09 01:33:53 +0000695 // Expand small (<= 128-bit) record types when we know that the stack layout
696 // of those arguments will match the struct. This is important because the
697 // LLVM backend isn't smart enough to remove byval, which inhibits many
698 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000699 if (getContext().getTypeSize(Ty) <= 4*32 &&
700 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000701 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000702
Chris Lattnera3c109b2010-07-29 02:16:43 +0000703 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000704 }
705
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000706 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000707 // On Darwin, some vectors are passed in memory, we handle this by passing
708 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000709 if (IsDarwinVectorABI) {
710 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000711 if ((Size == 8 || Size == 16 || Size == 32) ||
712 (Size == 64 && VT->getNumElements() == 1))
713 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
714 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000715 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000716
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000717 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000718 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000719 if (IsMMXDisabled)
720 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
721 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000722 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
723 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
724 return AAI;
725 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000726
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000727 return ABIArgInfo::getDirect();
728 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000729
730
Chris Lattnera3c109b2010-07-29 02:16:43 +0000731 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
732 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000733
Chris Lattnera3c109b2010-07-29 02:16:43 +0000734 return (Ty->isPromotableIntegerType() ?
735 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000736}
737
738llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
739 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000740 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
741 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000742
743 CGBuilderTy &Builder = CGF.Builder;
744 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
745 "ap");
746 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000747
748 // Compute if the address needs to be aligned
749 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
750 Align = getTypeStackAlignInBytes(Ty, Align);
751 Align = std::max(Align, 4U);
752 if (Align > 4) {
753 // addr = (addr + align - 1) & -align;
754 llvm::Value *Offset =
755 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
756 Addr = CGF.Builder.CreateGEP(Addr, Offset);
757 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
758 CGF.Int32Ty);
759 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
760 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
761 Addr->getType(),
762 "ap.cur.aligned");
763 }
764
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000765 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000766 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000767 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
768
769 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000770 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000771 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000772 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000773 "ap.next");
774 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
775
776 return AddrTyped;
777}
778
Charles Davis74f72932010-02-13 15:54:06 +0000779void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
780 llvm::GlobalValue *GV,
781 CodeGen::CodeGenModule &CGM) const {
782 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
783 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
784 // Get the LLVM function.
785 llvm::Function *Fn = cast<llvm::Function>(GV);
786
787 // Now add the 'alignstack' attribute with a value of 16.
788 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
789 }
790 }
791}
792
John McCall6374c332010-03-06 00:35:14 +0000793bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
794 CodeGen::CodeGenFunction &CGF,
795 llvm::Value *Address) const {
796 CodeGen::CGBuilderTy &Builder = CGF.Builder;
797 llvm::LLVMContext &Context = CGF.getLLVMContext();
798
Chris Lattner2acc6e32011-07-18 04:24:23 +0000799 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000800 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000801
John McCall6374c332010-03-06 00:35:14 +0000802 // 0-7 are the eight integer registers; the order is different
803 // on Darwin (for EH), but the range is the same.
804 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000805 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000806
807 if (CGF.CGM.isTargetDarwin()) {
808 // 12-16 are st(0..4). Not sure why we stop at 4.
809 // These have size 16, which is sizeof(long double) on
810 // platforms with 8-byte alignment for that type.
811 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000812 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000813
John McCall6374c332010-03-06 00:35:14 +0000814 } else {
815 // 9 is %eflags, which doesn't get a size on Darwin for some
816 // reason.
817 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
818
819 // 11-16 are st(0..5). Not sure why we stop at 5.
820 // These have size 12, which is sizeof(long double) on
821 // platforms with 4-byte alignment for that type.
822 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000823 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
824 }
John McCall6374c332010-03-06 00:35:14 +0000825
826 return false;
827}
828
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000829//===----------------------------------------------------------------------===//
830// X86-64 ABI Implementation
831//===----------------------------------------------------------------------===//
832
833
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000834namespace {
835/// X86_64ABIInfo - The X86_64 ABI information.
836class X86_64ABIInfo : public ABIInfo {
837 enum Class {
838 Integer = 0,
839 SSE,
840 SSEUp,
841 X87,
842 X87Up,
843 ComplexX87,
844 NoClass,
845 Memory
846 };
847
848 /// merge - Implement the X86_64 ABI merging algorithm.
849 ///
850 /// Merge an accumulating classification \arg Accum with a field
851 /// classification \arg Field.
852 ///
853 /// \param Accum - The accumulating classification. This should
854 /// always be either NoClass or the result of a previous merge
855 /// call. In addition, this should never be Memory (the caller
856 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000857 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000858
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000859 /// postMerge - Implement the X86_64 ABI post merging algorithm.
860 ///
861 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
862 /// final MEMORY or SSE classes when necessary.
863 ///
864 /// \param AggregateSize - The size of the current aggregate in
865 /// the classification process.
866 ///
867 /// \param Lo - The classification for the parts of the type
868 /// residing in the low word of the containing object.
869 ///
870 /// \param Hi - The classification for the parts of the type
871 /// residing in the higher words of the containing object.
872 ///
873 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
874
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000875 /// classify - Determine the x86_64 register classes in which the
876 /// given type T should be passed.
877 ///
878 /// \param Lo - The classification for the parts of the type
879 /// residing in the low word of the containing object.
880 ///
881 /// \param Hi - The classification for the parts of the type
882 /// residing in the high word of the containing object.
883 ///
884 /// \param OffsetBase - The bit offset of this type in the
885 /// containing object. Some parameters are classified different
886 /// depending on whether they straddle an eightbyte boundary.
887 ///
888 /// If a word is unused its result will be NoClass; if a type should
889 /// be passed in Memory then at least the classification of \arg Lo
890 /// will be Memory.
891 ///
892 /// The \arg Lo class will be NoClass iff the argument is ignored.
893 ///
894 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
895 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000896 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000897
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000898 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000899 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
900 unsigned IROffset, QualType SourceTy,
901 unsigned SourceOffset) const;
902 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
903 unsigned IROffset, QualType SourceTy,
904 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000905
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000906 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000907 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000908 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000909
910 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000911 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000912 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000913
Chris Lattnera3c109b2010-07-29 02:16:43 +0000914 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000915
Bill Wendlingbb465d72010-10-18 03:41:31 +0000916 ABIArgInfo classifyArgumentType(QualType Ty,
917 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000918 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000919
Eli Friedmanee1ad992011-12-02 00:11:43 +0000920 bool IsIllegalVectorType(QualType Ty) const;
921
John McCall67a57732011-04-21 01:20:55 +0000922 /// The 0.98 ABI revision clarified a lot of ambiguities,
923 /// unfortunately in ways that were not always consistent with
924 /// certain previous compilers. In particular, platforms which
925 /// required strict binary compatibility with older versions of GCC
926 /// may need to exempt themselves.
927 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000928 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000929 }
930
Eli Friedmanee1ad992011-12-02 00:11:43 +0000931 bool HasAVX;
932
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000933public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000934 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
935 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000936
Chris Lattneree5dcd02010-07-29 02:31:05 +0000937 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000938
939 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
940 CodeGenFunction &CGF) const;
941};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000942
Chris Lattnerf13721d2010-08-31 16:44:54 +0000943/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000944class WinX86_64ABIInfo : public ABIInfo {
945
946 ABIArgInfo classify(QualType Ty) const;
947
Chris Lattnerf13721d2010-08-31 16:44:54 +0000948public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000949 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
950
951 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000952
953 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
954 CodeGenFunction &CGF) const;
955};
956
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000957class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
958public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000959 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
960 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +0000961
962 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
963 return 7;
964 }
965
966 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
967 llvm::Value *Address) const {
968 CodeGen::CGBuilderTy &Builder = CGF.Builder;
969 llvm::LLVMContext &Context = CGF.getLLVMContext();
970
Chris Lattner2acc6e32011-07-18 04:24:23 +0000971 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000972 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000973
John McCallaeeb7012010-05-27 06:19:26 +0000974 // 0-15 are the 16 integer registers.
975 // 16 is %rip.
976 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000977
978 return false;
979 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000980
Jay Foadef6de3d2011-07-11 09:56:20 +0000981 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000982 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000983 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000984 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
985 }
986
Eli Friedman3ed79032011-12-01 04:53:19 +0000987 bool isNoProtoCallVariadic(const CodeGen::CGFunctionInfo &FI) const {
John McCall01f151e2011-09-21 08:08:30 +0000988 // The default CC on x86-64 sets %al to the number of SSA
989 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +0000990 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +0000991 // that when AVX types are involved: the ABI explicitly states it is
992 // undefined, and it doesn't work in practice because of how the ABI
993 // defines varargs anyway.
Eli Friedman3ed79032011-12-01 04:53:19 +0000994 if (FI.getCallingConvention() == llvm::CallingConv::C) {
995 bool HasAVXType = false;
996 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
997 ie = FI.arg_end();
998 it != ie; ++it) {
999 if (it->info.isDirect()) {
1000 llvm::Type *Ty = it->info.getCoerceToType();
1001 if (llvm::VectorType *VTy = dyn_cast_or_null<llvm::VectorType>(Ty)) {
1002 if (VTy->getBitWidth() > 128) {
1003 HasAVXType = true;
1004 break;
1005 }
1006 }
1007 }
1008 }
1009 if (!HasAVXType)
1010 return true;
1011 }
John McCall01f151e2011-09-21 08:08:30 +00001012
Eli Friedman3ed79032011-12-01 04:53:19 +00001013 return TargetCodeGenInfo::isNoProtoCallVariadic(FI);
John McCall01f151e2011-09-21 08:08:30 +00001014 }
1015
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001016};
1017
Chris Lattnerf13721d2010-08-31 16:44:54 +00001018class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1019public:
1020 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1021 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1022
1023 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1024 return 7;
1025 }
1026
1027 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1028 llvm::Value *Address) const {
1029 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1030 llvm::LLVMContext &Context = CGF.getLLVMContext();
1031
Chris Lattner2acc6e32011-07-18 04:24:23 +00001032 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001033 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001034
Chris Lattnerf13721d2010-08-31 16:44:54 +00001035 // 0-15 are the 16 integer registers.
1036 // 16 is %rip.
1037 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1038
1039 return false;
1040 }
1041};
1042
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001043}
1044
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001045void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1046 Class &Hi) const {
1047 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1048 //
1049 // (a) If one of the classes is Memory, the whole argument is passed in
1050 // memory.
1051 //
1052 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1053 // memory.
1054 //
1055 // (c) If the size of the aggregate exceeds two eightbytes and the first
1056 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1057 // argument is passed in memory. NOTE: This is necessary to keep the
1058 // ABI working for processors that don't support the __m256 type.
1059 //
1060 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1061 //
1062 // Some of these are enforced by the merging logic. Others can arise
1063 // only with unions; for example:
1064 // union { _Complex double; unsigned; }
1065 //
1066 // Note that clauses (b) and (c) were added in 0.98.
1067 //
1068 if (Hi == Memory)
1069 Lo = Memory;
1070 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1071 Lo = Memory;
1072 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1073 Lo = Memory;
1074 if (Hi == SSEUp && Lo != SSE)
1075 Hi = SSE;
1076}
1077
Chris Lattner1090a9b2010-06-28 21:43:59 +00001078X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001079 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1080 // classified recursively so that always two fields are
1081 // considered. The resulting class is calculated according to
1082 // the classes of the fields in the eightbyte:
1083 //
1084 // (a) If both classes are equal, this is the resulting class.
1085 //
1086 // (b) If one of the classes is NO_CLASS, the resulting class is
1087 // the other class.
1088 //
1089 // (c) If one of the classes is MEMORY, the result is the MEMORY
1090 // class.
1091 //
1092 // (d) If one of the classes is INTEGER, the result is the
1093 // INTEGER.
1094 //
1095 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1096 // MEMORY is used as class.
1097 //
1098 // (f) Otherwise class SSE is used.
1099
1100 // Accum should never be memory (we should have returned) or
1101 // ComplexX87 (because this cannot be passed in a structure).
1102 assert((Accum != Memory && Accum != ComplexX87) &&
1103 "Invalid accumulated classification during merge.");
1104 if (Accum == Field || Field == NoClass)
1105 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001106 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001107 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001108 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001109 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001110 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001111 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001112 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1113 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001114 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001115 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001116}
1117
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001118void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001119 Class &Lo, Class &Hi) const {
1120 // FIXME: This code can be simplified by introducing a simple value class for
1121 // Class pairs with appropriate constructor methods for the various
1122 // situations.
1123
1124 // FIXME: Some of the split computations are wrong; unaligned vectors
1125 // shouldn't be passed in registers for example, so there is no chance they
1126 // can straddle an eightbyte. Verify & simplify.
1127
1128 Lo = Hi = NoClass;
1129
1130 Class &Current = OffsetBase < 64 ? Lo : Hi;
1131 Current = Memory;
1132
John McCall183700f2009-09-21 23:43:11 +00001133 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001134 BuiltinType::Kind k = BT->getKind();
1135
1136 if (k == BuiltinType::Void) {
1137 Current = NoClass;
1138 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1139 Lo = Integer;
1140 Hi = Integer;
1141 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1142 Current = Integer;
1143 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1144 Current = SSE;
1145 } else if (k == BuiltinType::LongDouble) {
1146 Lo = X87;
1147 Hi = X87Up;
1148 }
1149 // FIXME: _Decimal32 and _Decimal64 are SSE.
1150 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001151 return;
1152 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001153
Chris Lattner1090a9b2010-06-28 21:43:59 +00001154 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001155 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001156 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001157 return;
1158 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001159
Chris Lattner1090a9b2010-06-28 21:43:59 +00001160 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001161 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001162 return;
1163 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001164
Chris Lattner1090a9b2010-06-28 21:43:59 +00001165 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001166 if (Ty->isMemberFunctionPointerType())
1167 Lo = Hi = Integer;
1168 else
1169 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001170 return;
1171 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001172
Chris Lattner1090a9b2010-06-28 21:43:59 +00001173 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001174 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001175 if (Size == 32) {
1176 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1177 // float> as integer.
1178 Current = Integer;
1179
1180 // If this type crosses an eightbyte boundary, it should be
1181 // split.
1182 uint64_t EB_Real = (OffsetBase) / 64;
1183 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1184 if (EB_Real != EB_Imag)
1185 Hi = Lo;
1186 } else if (Size == 64) {
1187 // gcc passes <1 x double> in memory. :(
1188 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1189 return;
1190
1191 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001192 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001193 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1194 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1195 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001196 Current = Integer;
1197 else
1198 Current = SSE;
1199
1200 // If this type crosses an eightbyte boundary, it should be
1201 // split.
1202 if (OffsetBase && OffsetBase != 64)
1203 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001204 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001205 // Arguments of 256-bits are split into four eightbyte chunks. The
1206 // least significant one belongs to class SSE and all the others to class
1207 // SSEUP. The original Lo and Hi design considers that types can't be
1208 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1209 // This design isn't correct for 256-bits, but since there're no cases
1210 // where the upper parts would need to be inspected, avoid adding
1211 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001212 Lo = SSE;
1213 Hi = SSEUp;
1214 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001215 return;
1216 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001217
Chris Lattner1090a9b2010-06-28 21:43:59 +00001218 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001219 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001220
Chris Lattnerea044322010-07-29 02:01:43 +00001221 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001222 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001223 if (Size <= 64)
1224 Current = Integer;
1225 else if (Size <= 128)
1226 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001227 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001228 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001229 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001230 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001231 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001232 Current = ComplexX87;
1233
1234 // If this complex type crosses an eightbyte boundary then it
1235 // should be split.
1236 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001237 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001238 if (Hi == NoClass && EB_Real != EB_Imag)
1239 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001240
Chris Lattner1090a9b2010-06-28 21:43:59 +00001241 return;
1242 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001243
Chris Lattnerea044322010-07-29 02:01:43 +00001244 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001245 // Arrays are treated like structures.
1246
Chris Lattnerea044322010-07-29 02:01:43 +00001247 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001248
1249 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001250 // than four eightbytes, ..., it has class MEMORY.
1251 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001252 return;
1253
1254 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1255 // fields, it has class MEMORY.
1256 //
1257 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001258 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001259 return;
1260
1261 // Otherwise implement simplified merge. We could be smarter about
1262 // this, but it isn't worth it and would be harder to verify.
1263 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001264 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001265 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001266
1267 // The only case a 256-bit wide vector could be used is when the array
1268 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1269 // to work for sizes wider than 128, early check and fallback to memory.
1270 if (Size > 128 && EltSize != 256)
1271 return;
1272
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001273 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1274 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001275 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001276 Lo = merge(Lo, FieldLo);
1277 Hi = merge(Hi, FieldHi);
1278 if (Lo == Memory || Hi == Memory)
1279 break;
1280 }
1281
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001282 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001283 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001284 return;
1285 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001286
Chris Lattner1090a9b2010-06-28 21:43:59 +00001287 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001288 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001289
1290 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001291 // than four eightbytes, ..., it has class MEMORY.
1292 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001293 return;
1294
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001295 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1296 // copy constructor or a non-trivial destructor, it is passed by invisible
1297 // reference.
1298 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1299 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001300
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001301 const RecordDecl *RD = RT->getDecl();
1302
1303 // Assume variable sized types are passed in memory.
1304 if (RD->hasFlexibleArrayMember())
1305 return;
1306
Chris Lattnerea044322010-07-29 02:01:43 +00001307 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001308
1309 // Reset Lo class, this will be recomputed.
1310 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001311
1312 // If this is a C++ record, classify the bases first.
1313 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1314 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1315 e = CXXRD->bases_end(); i != e; ++i) {
1316 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1317 "Unexpected base class!");
1318 const CXXRecordDecl *Base =
1319 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1320
1321 // Classify this field.
1322 //
1323 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1324 // single eightbyte, each is classified separately. Each eightbyte gets
1325 // initialized to class NO_CLASS.
1326 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001327 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001328 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001329 Lo = merge(Lo, FieldLo);
1330 Hi = merge(Hi, FieldHi);
1331 if (Lo == Memory || Hi == Memory)
1332 break;
1333 }
1334 }
1335
1336 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001337 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001338 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001339 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001340 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1341 bool BitField = i->isBitField();
1342
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001343 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1344 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001346 // The only case a 256-bit wide vector could be used is when the struct
1347 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1348 // to work for sizes wider than 128, early check and fallback to memory.
1349 //
1350 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1351 Lo = Memory;
1352 return;
1353 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001354 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001355 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001356 Lo = Memory;
1357 return;
1358 }
1359
1360 // Classify this field.
1361 //
1362 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1363 // exceeds a single eightbyte, each is classified
1364 // separately. Each eightbyte gets initialized to class
1365 // NO_CLASS.
1366 Class FieldLo, FieldHi;
1367
1368 // Bit-fields require special handling, they do not force the
1369 // structure to be passed in memory even if unaligned, and
1370 // therefore they can straddle an eightbyte.
1371 if (BitField) {
1372 // Ignore padding bit-fields.
1373 if (i->isUnnamedBitfield())
1374 continue;
1375
1376 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001377 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001378
1379 uint64_t EB_Lo = Offset / 64;
1380 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1381 FieldLo = FieldHi = NoClass;
1382 if (EB_Lo) {
1383 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1384 FieldLo = NoClass;
1385 FieldHi = Integer;
1386 } else {
1387 FieldLo = Integer;
1388 FieldHi = EB_Hi ? Integer : NoClass;
1389 }
1390 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001391 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001392 Lo = merge(Lo, FieldLo);
1393 Hi = merge(Hi, FieldHi);
1394 if (Lo == Memory || Hi == Memory)
1395 break;
1396 }
1397
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001398 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001399 }
1400}
1401
Chris Lattner9c254f02010-06-29 06:01:59 +00001402ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001403 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1404 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001405 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001406 // Treat an enum type as its underlying type.
1407 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1408 Ty = EnumTy->getDecl()->getIntegerType();
1409
1410 return (Ty->isPromotableIntegerType() ?
1411 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1412 }
1413
1414 return ABIArgInfo::getIndirect(0);
1415}
1416
Eli Friedmanee1ad992011-12-02 00:11:43 +00001417bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1418 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1419 uint64_t Size = getContext().getTypeSize(VecTy);
1420 unsigned LargestVector = HasAVX ? 256 : 128;
1421 if (Size <= 64 || Size > LargestVector)
1422 return true;
1423 }
1424
1425 return false;
1426}
1427
Chris Lattner9c254f02010-06-29 06:01:59 +00001428ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001429 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1430 // place naturally.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001431 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001432 // Treat an enum type as its underlying type.
1433 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1434 Ty = EnumTy->getDecl()->getIntegerType();
1435
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001436 return (Ty->isPromotableIntegerType() ?
1437 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001438 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001439
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001440 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1441 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001442
Chris Lattner855d2272011-05-22 23:21:23 +00001443 // Compute the byval alignment. We specify the alignment of the byval in all
1444 // cases so that the mid-level optimizer knows the alignment of the byval.
1445 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1446 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001447}
1448
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001449/// GetByteVectorType - The ABI specifies that a value should be passed in an
1450/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001451/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001452llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001453 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001454
Chris Lattner15842bd2010-07-29 05:02:29 +00001455 // Wrapper structs that just contain vectors are passed just like vectors,
1456 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001457 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001458 while (STy && STy->getNumElements() == 1) {
1459 IRType = STy->getElementType(0);
1460 STy = dyn_cast<llvm::StructType>(IRType);
1461 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001462
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001463 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001464 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1465 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001466 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001467 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001468 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1469 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1470 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1471 EltTy->isIntegerTy(128)))
1472 return VT;
1473 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001474
Chris Lattner0f408f52010-07-29 04:56:46 +00001475 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1476}
1477
Chris Lattnere2962be2010-07-29 07:30:00 +00001478/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1479/// is known to either be off the end of the specified type or being in
1480/// alignment padding. The user type specified is known to be at most 128 bits
1481/// in size, and have passed through X86_64ABIInfo::classify with a successful
1482/// classification that put one of the two halves in the INTEGER class.
1483///
1484/// It is conservatively correct to return false.
1485static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1486 unsigned EndBit, ASTContext &Context) {
1487 // If the bytes being queried are off the end of the type, there is no user
1488 // data hiding here. This handles analysis of builtins, vectors and other
1489 // types that don't contain interesting padding.
1490 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1491 if (TySize <= StartBit)
1492 return true;
1493
Chris Lattner021c3a32010-07-29 07:43:55 +00001494 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1495 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1496 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1497
1498 // Check each element to see if the element overlaps with the queried range.
1499 for (unsigned i = 0; i != NumElts; ++i) {
1500 // If the element is after the span we care about, then we're done..
1501 unsigned EltOffset = i*EltSize;
1502 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001503
Chris Lattner021c3a32010-07-29 07:43:55 +00001504 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1505 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1506 EndBit-EltOffset, Context))
1507 return false;
1508 }
1509 // If it overlaps no elements, then it is safe to process as padding.
1510 return true;
1511 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001512
Chris Lattnere2962be2010-07-29 07:30:00 +00001513 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1514 const RecordDecl *RD = RT->getDecl();
1515 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001516
Chris Lattnere2962be2010-07-29 07:30:00 +00001517 // If this is a C++ record, check the bases first.
1518 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1519 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1520 e = CXXRD->bases_end(); i != e; ++i) {
1521 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1522 "Unexpected base class!");
1523 const CXXRecordDecl *Base =
1524 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001525
Chris Lattnere2962be2010-07-29 07:30:00 +00001526 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001527 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001528 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001529
Chris Lattnere2962be2010-07-29 07:30:00 +00001530 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1531 if (!BitsContainNoUserData(i->getType(), BaseStart,
1532 EndBit-BaseOffset, Context))
1533 return false;
1534 }
1535 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001536
Chris Lattnere2962be2010-07-29 07:30:00 +00001537 // Verify that no field has data that overlaps the region of interest. Yes
1538 // this could be sped up a lot by being smarter about queried fields,
1539 // however we're only looking at structs up to 16 bytes, so we don't care
1540 // much.
1541 unsigned idx = 0;
1542 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1543 i != e; ++i, ++idx) {
1544 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001545
Chris Lattnere2962be2010-07-29 07:30:00 +00001546 // If we found a field after the region we care about, then we're done.
1547 if (FieldOffset >= EndBit) break;
1548
1549 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1550 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1551 Context))
1552 return false;
1553 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001554
Chris Lattnere2962be2010-07-29 07:30:00 +00001555 // If nothing in this record overlapped the area of interest, then we're
1556 // clean.
1557 return true;
1558 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001559
Chris Lattnere2962be2010-07-29 07:30:00 +00001560 return false;
1561}
1562
Chris Lattner0b362002010-07-29 18:39:32 +00001563/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1564/// float member at the specified offset. For example, {int,{float}} has a
1565/// float at offset 4. It is conservatively correct for this routine to return
1566/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001567static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001568 const llvm::TargetData &TD) {
1569 // Base case if we find a float.
1570 if (IROffset == 0 && IRType->isFloatTy())
1571 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001572
Chris Lattner0b362002010-07-29 18:39:32 +00001573 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001574 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001575 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1576 unsigned Elt = SL->getElementContainingOffset(IROffset);
1577 IROffset -= SL->getElementOffset(Elt);
1578 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1579 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001580
Chris Lattner0b362002010-07-29 18:39:32 +00001581 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001582 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1583 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001584 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1585 IROffset -= IROffset/EltSize*EltSize;
1586 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1587 }
1588
1589 return false;
1590}
1591
Chris Lattnerf47c9442010-07-29 18:13:09 +00001592
1593/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1594/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001595llvm::Type *X86_64ABIInfo::
1596GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001597 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001598 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001599 // pass as float if the last 4 bytes is just padding. This happens for
1600 // structs that contain 3 floats.
1601 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1602 SourceOffset*8+64, getContext()))
1603 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001604
Chris Lattner0b362002010-07-29 18:39:32 +00001605 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1606 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1607 // case.
1608 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001609 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1610 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001611
Chris Lattnerf47c9442010-07-29 18:13:09 +00001612 return llvm::Type::getDoubleTy(getVMContext());
1613}
1614
1615
Chris Lattner0d2656d2010-07-29 17:40:35 +00001616/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1617/// an 8-byte GPR. This means that we either have a scalar or we are talking
1618/// about the high or low part of an up-to-16-byte struct. This routine picks
1619/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001620/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1621/// etc).
1622///
1623/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1624/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1625/// the 8-byte value references. PrefType may be null.
1626///
1627/// SourceTy is the source level type for the entire argument. SourceOffset is
1628/// an offset into this that we're processing (which is always either 0 or 8).
1629///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001630llvm::Type *X86_64ABIInfo::
1631GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001632 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001633 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1634 // returning an 8-byte unit starting with it. See if we can safely use it.
1635 if (IROffset == 0) {
1636 // Pointers and int64's always fill the 8-byte unit.
1637 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1638 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001639
Chris Lattnere2962be2010-07-29 07:30:00 +00001640 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1641 // goodness in the source type is just tail padding. This is allowed to
1642 // kick in for struct {double,int} on the int, but not on
1643 // struct{double,int,int} because we wouldn't return the second int. We
1644 // have to do this analysis on the source type because we can't depend on
1645 // unions being lowered a specific way etc.
1646 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1647 IRType->isIntegerTy(32)) {
1648 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001649
Chris Lattnere2962be2010-07-29 07:30:00 +00001650 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1651 SourceOffset*8+64, getContext()))
1652 return IRType;
1653 }
1654 }
Chris Lattner49382de2010-07-28 22:44:07 +00001655
Chris Lattner2acc6e32011-07-18 04:24:23 +00001656 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001657 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001658 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001659 if (IROffset < SL->getSizeInBytes()) {
1660 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1661 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001662
Chris Lattner0d2656d2010-07-29 17:40:35 +00001663 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1664 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001665 }
Chris Lattner49382de2010-07-28 22:44:07 +00001666 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001667
Chris Lattner2acc6e32011-07-18 04:24:23 +00001668 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001669 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001670 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1671 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001672 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1673 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001674 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001675
Chris Lattner49382de2010-07-28 22:44:07 +00001676 // Okay, we don't have any better idea of what to pass, so we pass this in an
1677 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001678 unsigned TySizeInBytes =
1679 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001680
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001681 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001682
Chris Lattner49382de2010-07-28 22:44:07 +00001683 // It is always safe to classify this as an integer type up to i64 that
1684 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001685 return llvm::IntegerType::get(getVMContext(),
1686 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001687}
1688
Chris Lattner66e7b682010-09-01 00:50:20 +00001689
1690/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1691/// be used as elements of a two register pair to pass or return, return a
1692/// first class aggregate to represent them. For example, if the low part of
1693/// a by-value argument should be passed as i32* and the high part as float,
1694/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001695static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001696GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001697 const llvm::TargetData &TD) {
1698 // In order to correctly satisfy the ABI, we need to the high part to start
1699 // at offset 8. If the high and low parts we inferred are both 4-byte types
1700 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1701 // the second element at offset 8. Check for this:
1702 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1703 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1704 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1705 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001706
Chris Lattner66e7b682010-09-01 00:50:20 +00001707 // To handle this, we have to increase the size of the low part so that the
1708 // second element will start at an 8 byte offset. We can't increase the size
1709 // of the second element because it might make us access off the end of the
1710 // struct.
1711 if (HiStart != 8) {
1712 // There are only two sorts of types the ABI generation code can produce for
1713 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1714 // Promote these to a larger type.
1715 if (Lo->isFloatTy())
1716 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1717 else {
1718 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1719 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1720 }
1721 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001722
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001723 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001724
1725
Chris Lattner66e7b682010-09-01 00:50:20 +00001726 // Verify that the second element is at an 8-byte offset.
1727 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1728 "Invalid x86-64 argument pair!");
1729 return Result;
1730}
1731
Chris Lattner519f68c2010-07-28 23:06:14 +00001732ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001733classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001734 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1735 // classification algorithm.
1736 X86_64ABIInfo::Class Lo, Hi;
1737 classify(RetTy, 0, Lo, Hi);
1738
1739 // Check some invariants.
1740 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001741 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1742
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001743 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001744 switch (Lo) {
1745 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001746 if (Hi == NoClass)
1747 return ABIArgInfo::getIgnore();
1748 // If the low part is just padding, it takes no register, leave ResType
1749 // null.
1750 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1751 "Unknown missing lo part");
1752 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001753
1754 case SSEUp:
1755 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001756 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001757
1758 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1759 // hidden argument.
1760 case Memory:
1761 return getIndirectReturnResult(RetTy);
1762
1763 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1764 // available register of the sequence %rax, %rdx is used.
1765 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001766 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001767
Chris Lattnereb518b42010-07-29 21:42:50 +00001768 // If we have a sign or zero extended integer, make sure to return Extend
1769 // so that the parameter gets the right LLVM IR attributes.
1770 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1771 // Treat an enum type as its underlying type.
1772 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1773 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001774
Chris Lattnereb518b42010-07-29 21:42:50 +00001775 if (RetTy->isIntegralOrEnumerationType() &&
1776 RetTy->isPromotableIntegerType())
1777 return ABIArgInfo::getExtend();
1778 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001779 break;
1780
1781 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1782 // available SSE register of the sequence %xmm0, %xmm1 is used.
1783 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001784 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001785 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001786
1787 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1788 // returned on the X87 stack in %st0 as 80-bit x87 number.
1789 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001790 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001791 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001792
1793 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1794 // part of the value is returned in %st0 and the imaginary part in
1795 // %st1.
1796 case ComplexX87:
1797 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001798 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001799 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001800 NULL);
1801 break;
1802 }
1803
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001804 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001805 switch (Hi) {
1806 // Memory was handled previously and X87 should
1807 // never occur as a hi class.
1808 case Memory:
1809 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001810 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001811
1812 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001813 case NoClass:
1814 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001815
Chris Lattner3db4dde2010-09-01 00:20:33 +00001816 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001817 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001818 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1819 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001820 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001821 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001822 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001823 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1824 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001825 break;
1826
1827 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001828 // is passed in the next available eightbyte chunk if the last used
1829 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001830 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001831 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001832 case SSEUp:
1833 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001834 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001835 break;
1836
1837 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1838 // returned together with the previous X87 value in %st0.
1839 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001840 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001841 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001842 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001843 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001844 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001845 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001846 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1847 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001848 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001849 break;
1850 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001851
Chris Lattner3db4dde2010-09-01 00:20:33 +00001852 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001853 // known to pass in the high eightbyte of the result. We do this by forming a
1854 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001855 if (HighPart)
1856 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001857
Chris Lattnereb518b42010-07-29 21:42:50 +00001858 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001859}
1860
Chris Lattnera3c109b2010-07-29 02:16:43 +00001861ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001862 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001863 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001864 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001865
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001866 // Check some invariants.
1867 // FIXME: Enforce these by construction.
1868 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001869 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1870
1871 neededInt = 0;
1872 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001873 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001874 switch (Lo) {
1875 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001876 if (Hi == NoClass)
1877 return ABIArgInfo::getIgnore();
1878 // If the low part is just padding, it takes no register, leave ResType
1879 // null.
1880 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1881 "Unknown missing lo part");
1882 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001883
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001884 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1885 // on the stack.
1886 case Memory:
1887
1888 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1889 // COMPLEX_X87, it is passed in memory.
1890 case X87:
1891 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001892 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1893 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001894 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001895
1896 case SSEUp:
1897 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001898 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001899
1900 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1901 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1902 // and %r9 is used.
1903 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001904 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001905
Chris Lattner49382de2010-07-28 22:44:07 +00001906 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001907 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001908
1909 // If we have a sign or zero extended integer, make sure to return Extend
1910 // so that the parameter gets the right LLVM IR attributes.
1911 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1912 // Treat an enum type as its underlying type.
1913 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1914 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001915
Chris Lattnereb518b42010-07-29 21:42:50 +00001916 if (Ty->isIntegralOrEnumerationType() &&
1917 Ty->isPromotableIntegerType())
1918 return ABIArgInfo::getExtend();
1919 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001920
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001921 break;
1922
1923 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1924 // available SSE register is used, the registers are taken in the
1925 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001926 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001927 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001928 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001929 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001930 break;
1931 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001932 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001933
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001934 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001935 switch (Hi) {
1936 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001937 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001938 // which is passed in memory.
1939 case Memory:
1940 case X87:
1941 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001942 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001943
1944 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001945
Chris Lattner645406a2010-09-01 00:24:35 +00001946 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001947 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001948 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001949 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001950
Chris Lattner645406a2010-09-01 00:24:35 +00001951 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1952 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001953 break;
1954
1955 // X87Up generally doesn't occur here (long double is passed in
1956 // memory), except in situations involving unions.
1957 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001958 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001959 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001960
Chris Lattner645406a2010-09-01 00:24:35 +00001961 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1962 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001963
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001964 ++neededSSE;
1965 break;
1966
1967 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1968 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001969 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001970 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001971 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001972 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001973 break;
1974 }
1975
Chris Lattner645406a2010-09-01 00:24:35 +00001976 // If a high part was specified, merge it together with the low part. It is
1977 // known to pass in the high eightbyte of the result. We do this by forming a
1978 // first class struct aggregate with the high and low part: {low, high}
1979 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001980 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001981
Chris Lattnereb518b42010-07-29 21:42:50 +00001982 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001983}
1984
Chris Lattneree5dcd02010-07-29 02:31:05 +00001985void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001986
Chris Lattnera3c109b2010-07-29 02:16:43 +00001987 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001988
1989 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001990 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001991
1992 // If the return value is indirect, then the hidden argument is consuming one
1993 // integer register.
1994 if (FI.getReturnInfo().isIndirect())
1995 --freeIntRegs;
1996
1997 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1998 // get assigned (in left-to-right order) for passing as follows...
1999 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2000 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002001 unsigned neededInt, neededSSE;
2002 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002003
2004 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2005 // eightbyte of an argument, the whole argument is passed on the
2006 // stack. If registers have already been assigned for some
2007 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002008 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002009 freeIntRegs -= neededInt;
2010 freeSSERegs -= neededSSE;
2011 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00002012 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002013 }
2014 }
2015}
2016
2017static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2018 QualType Ty,
2019 CodeGenFunction &CGF) {
2020 llvm::Value *overflow_arg_area_p =
2021 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2022 llvm::Value *overflow_arg_area =
2023 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2024
2025 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2026 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002027 // It isn't stated explicitly in the standard, but in practice we use
2028 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002029 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2030 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002031 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002032 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002033 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002034 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2035 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002036 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002037 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002038 overflow_arg_area =
2039 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2040 overflow_arg_area->getType(),
2041 "overflow_arg_area.align");
2042 }
2043
2044 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002045 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002046 llvm::Value *Res =
2047 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002048 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002049
2050 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2051 // l->overflow_arg_area + sizeof(type).
2052 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2053 // an 8 byte boundary.
2054
2055 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002056 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002057 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002058 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2059 "overflow_arg_area.next");
2060 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2061
2062 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2063 return Res;
2064}
2065
2066llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2067 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002068 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002070 // Assume that va_list type is correct; should be pointer to LLVM type:
2071 // struct {
2072 // i32 gp_offset;
2073 // i32 fp_offset;
2074 // i8* overflow_arg_area;
2075 // i8* reg_save_area;
2076 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002077 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002078
Chris Lattnera14db752010-03-11 18:19:55 +00002079 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002080 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002081
2082 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2083 // in the registers. If not go to step 7.
2084 if (!neededInt && !neededSSE)
2085 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2086
2087 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2088 // general purpose registers needed to pass type and num_fp to hold
2089 // the number of floating point registers needed.
2090
2091 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2092 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2093 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2094 //
2095 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2096 // register save space).
2097
2098 llvm::Value *InRegs = 0;
2099 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2100 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2101 if (neededInt) {
2102 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2103 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002104 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2105 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002106 }
2107
2108 if (neededSSE) {
2109 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2110 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2111 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002112 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2113 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002114 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2115 }
2116
2117 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2118 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2119 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2120 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2121
2122 // Emit code to load the value if it was passed in registers.
2123
2124 CGF.EmitBlock(InRegBlock);
2125
2126 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2127 // an offset of l->gp_offset and/or l->fp_offset. This may require
2128 // copying to a temporary location in case the parameter is passed
2129 // in different register classes or requires an alignment greater
2130 // than 8 for general purpose registers and 16 for XMM registers.
2131 //
2132 // FIXME: This really results in shameful code when we end up needing to
2133 // collect arguments from different places; often what should result in a
2134 // simple assembling of a structure from scattered addresses has many more
2135 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002136 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002137 llvm::Value *RegAddr =
2138 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2139 "reg_save_area");
2140 if (neededInt && neededSSE) {
2141 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002142 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002143 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002144 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2145 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002146 llvm::Type *TyLo = ST->getElementType(0);
2147 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002148 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002149 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002150 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2151 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002152 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2153 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002154 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2155 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002156 llvm::Value *V =
2157 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2158 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2159 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2160 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2161
Owen Andersona1cf15f2009-07-14 23:10:40 +00002162 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002163 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002164 } else if (neededInt) {
2165 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2166 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002167 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002168 } else if (neededSSE == 1) {
2169 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2170 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2171 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002172 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002173 assert(neededSSE == 2 && "Invalid number of needed registers!");
2174 // SSE registers are spaced 16 bytes apart in the register save
2175 // area, we need to collect the two eightbytes together.
2176 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002177 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002178 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002179 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002180 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002181 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002182 DoubleTy, NULL);
2183 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2184 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2185 DblPtrTy));
2186 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2187 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2188 DblPtrTy));
2189 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2190 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2191 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002192 }
2193
2194 // AMD64-ABI 3.5.7p5: Step 5. Set:
2195 // l->gp_offset = l->gp_offset + num_gp * 8
2196 // l->fp_offset = l->fp_offset + num_fp * 16.
2197 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002198 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002199 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2200 gp_offset_p);
2201 }
2202 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002203 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002204 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2205 fp_offset_p);
2206 }
2207 CGF.EmitBranch(ContBlock);
2208
2209 // Emit code to load the value if it was passed in memory.
2210
2211 CGF.EmitBlock(InMemBlock);
2212 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2213
2214 // Return the appropriate result.
2215
2216 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002217 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002218 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002219 ResAddr->addIncoming(RegAddr, InRegBlock);
2220 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002221 return ResAddr;
2222}
2223
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002224ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2225
2226 if (Ty->isVoidType())
2227 return ABIArgInfo::getIgnore();
2228
2229 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2230 Ty = EnumTy->getDecl()->getIntegerType();
2231
2232 uint64_t Size = getContext().getTypeSize(Ty);
2233
2234 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002235 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2236 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002237 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2238
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002239 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2240 if (Size == 128 &&
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002241 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002242 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2243 Size));
2244
2245 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2246 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2247 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002248 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002249 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2250 Size));
2251
2252 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2253 }
2254
2255 if (Ty->isPromotableIntegerType())
2256 return ABIArgInfo::getExtend();
2257
2258 return ABIArgInfo::getDirect();
2259}
2260
2261void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2262
2263 QualType RetTy = FI.getReturnType();
2264 FI.getReturnInfo() = classify(RetTy);
2265
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002266 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2267 it != ie; ++it)
2268 it->info = classify(it->type);
2269}
2270
Chris Lattnerf13721d2010-08-31 16:44:54 +00002271llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2272 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002273 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2274 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002275
Chris Lattnerf13721d2010-08-31 16:44:54 +00002276 CGBuilderTy &Builder = CGF.Builder;
2277 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2278 "ap");
2279 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2280 llvm::Type *PTy =
2281 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2282 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2283
2284 uint64_t Offset =
2285 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2286 llvm::Value *NextAddr =
2287 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2288 "ap.next");
2289 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2290
2291 return AddrTyped;
2292}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002293
John McCallec853ba2010-03-11 00:10:12 +00002294// PowerPC-32
2295
2296namespace {
2297class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2298public:
Chris Lattnerea044322010-07-29 02:01:43 +00002299 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002300
John McCallec853ba2010-03-11 00:10:12 +00002301 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2302 // This is recovered from gcc output.
2303 return 1; // r1 is the dedicated stack pointer
2304 }
2305
2306 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002307 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002308};
2309
2310}
2311
2312bool
2313PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2314 llvm::Value *Address) const {
2315 // This is calculated from the LLVM and GCC tables and verified
2316 // against gcc output. AFAIK all ABIs use the same encoding.
2317
2318 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2319 llvm::LLVMContext &Context = CGF.getLLVMContext();
2320
Chris Lattner2acc6e32011-07-18 04:24:23 +00002321 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002322 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2323 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2324 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2325
2326 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002327 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002328
2329 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002330 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002331
2332 // 64-76 are various 4-byte special-purpose registers:
2333 // 64: mq
2334 // 65: lr
2335 // 66: ctr
2336 // 67: ap
2337 // 68-75 cr0-7
2338 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002339 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002340
2341 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002342 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002343
2344 // 109: vrsave
2345 // 110: vscr
2346 // 111: spe_acc
2347 // 112: spefscr
2348 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002349 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002350
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002351 return false;
John McCallec853ba2010-03-11 00:10:12 +00002352}
2353
2354
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002355//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002356// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002357//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002358
2359namespace {
2360
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002361class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002362public:
2363 enum ABIKind {
2364 APCS = 0,
2365 AAPCS = 1,
2366 AAPCS_VFP
2367 };
2368
2369private:
2370 ABIKind Kind;
2371
2372public:
Chris Lattnerea044322010-07-29 02:01:43 +00002373 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002374
John McCall49e34be2011-08-30 01:42:09 +00002375 bool isEABI() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002376 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall49e34be2011-08-30 01:42:09 +00002377 return (Env == "gnueabi" || Env == "eabi");
2378 }
2379
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002380private:
2381 ABIKind getABIKind() const { return Kind; }
2382
Chris Lattnera3c109b2010-07-29 02:16:43 +00002383 ABIArgInfo classifyReturnType(QualType RetTy) const;
2384 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002385
Chris Lattneree5dcd02010-07-29 02:31:05 +00002386 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002387
2388 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2389 CodeGenFunction &CGF) const;
2390};
2391
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002392class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2393public:
Chris Lattnerea044322010-07-29 02:01:43 +00002394 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2395 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002396
John McCall49e34be2011-08-30 01:42:09 +00002397 const ARMABIInfo &getABIInfo() const {
2398 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2399 }
2400
John McCall6374c332010-03-06 00:35:14 +00002401 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2402 return 13;
2403 }
Roman Divacky09345d12011-05-18 19:36:54 +00002404
Chris Lattner5f9e2722011-07-23 10:55:15 +00002405 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002406 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2407 }
2408
Roman Divacky09345d12011-05-18 19:36:54 +00002409 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2410 llvm::Value *Address) const {
2411 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2412 llvm::LLVMContext &Context = CGF.getLLVMContext();
2413
Chris Lattner2acc6e32011-07-18 04:24:23 +00002414 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002415 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2416
2417 // 0-15 are the 16 integer registers.
2418 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2419
2420 return false;
2421 }
John McCall49e34be2011-08-30 01:42:09 +00002422
2423 unsigned getSizeOfUnwindException() const {
2424 if (getABIInfo().isEABI()) return 88;
2425 return TargetCodeGenInfo::getSizeOfUnwindException();
2426 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002427};
2428
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002429}
2430
Chris Lattneree5dcd02010-07-29 02:31:05 +00002431void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002432 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002433 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002434 it != ie; ++it)
2435 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002436
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002437 // Always honor user-specified calling convention.
2438 if (FI.getCallingConvention() != llvm::CallingConv::C)
2439 return;
2440
2441 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002442 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002443 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002444 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002445 else
2446 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002447
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002448 // If user did not ask for specific calling convention explicitly (e.g. via
2449 // pcs attribute), set effective calling convention if it's different than ABI
2450 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002451 switch (getABIKind()) {
2452 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002453 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2454 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002455 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002456 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002457 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2458 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002459 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002460 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002461 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2462 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002463 break;
2464 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002465}
2466
Bob Wilson194f06a2011-08-03 05:58:22 +00002467/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2468/// aggregate. If HAMembers is non-null, the number of base elements
2469/// contained in the type is returned through it; this is used for the
2470/// recursive calls that check aggregate component types.
2471static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2472 ASTContext &Context,
2473 uint64_t *HAMembers = 0) {
2474 uint64_t Members;
2475 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2476 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2477 return false;
2478 Members *= AT->getSize().getZExtValue();
2479 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2480 const RecordDecl *RD = RT->getDecl();
2481 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2482 return false;
2483 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2484 if (!CXXRD->isAggregate())
2485 return false;
2486 }
2487 Members = 0;
2488 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2489 i != e; ++i) {
2490 const FieldDecl *FD = *i;
2491 uint64_t FldMembers;
2492 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2493 return false;
2494 Members += FldMembers;
2495 }
2496 } else {
2497 Members = 1;
2498 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2499 Members = 2;
2500 Ty = CT->getElementType();
2501 }
2502
2503 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2504 // double, or 64-bit or 128-bit vectors.
2505 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2506 if (BT->getKind() != BuiltinType::Float &&
2507 BT->getKind() != BuiltinType::Double)
2508 return false;
2509 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2510 unsigned VecSize = Context.getTypeSize(VT);
2511 if (VecSize != 64 && VecSize != 128)
2512 return false;
2513 } else {
2514 return false;
2515 }
2516
2517 // The base type must be the same for all members. Vector types of the
2518 // same total size are treated as being equivalent here.
2519 const Type *TyPtr = Ty.getTypePtr();
2520 if (!Base)
2521 Base = TyPtr;
2522 if (Base != TyPtr &&
2523 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2524 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2525 return false;
2526 }
2527
2528 // Homogeneous Aggregates can have at most 4 members of the base type.
2529 if (HAMembers)
2530 *HAMembers = Members;
2531 return (Members <= 4);
2532}
2533
Chris Lattnera3c109b2010-07-29 02:16:43 +00002534ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002535 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002536 // Treat an enum type as its underlying type.
2537 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2538 Ty = EnumTy->getDecl()->getIntegerType();
2539
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002540 return (Ty->isPromotableIntegerType() ?
2541 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002542 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002543
Daniel Dunbar42025572009-09-14 21:54:03 +00002544 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002545 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002546 return ABIArgInfo::getIgnore();
2547
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002548 // Structures with either a non-trivial destructor or a non-trivial
2549 // copy constructor are always indirect.
2550 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2551 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2552
Bob Wilson194f06a2011-08-03 05:58:22 +00002553 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2554 // Homogeneous Aggregates need to be expanded.
2555 const Type *Base = 0;
2556 if (isHomogeneousAggregate(Ty, Base, getContext()))
2557 return ABIArgInfo::getExpand();
2558 }
2559
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002560 // Otherwise, pass by coercing to a structure of the appropriate size.
2561 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002562 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2563 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002564 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002565 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002566 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002567 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002568 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2569 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002570 } else {
2571 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2572 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002573 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002574
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002575 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002576 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002577 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002578}
2579
Chris Lattnera3c109b2010-07-29 02:16:43 +00002580static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002581 llvm::LLVMContext &VMContext) {
2582 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2583 // is called integer-like if its size is less than or equal to one word, and
2584 // the offset of each of its addressable sub-fields is zero.
2585
2586 uint64_t Size = Context.getTypeSize(Ty);
2587
2588 // Check that the type fits in a word.
2589 if (Size > 32)
2590 return false;
2591
2592 // FIXME: Handle vector types!
2593 if (Ty->isVectorType())
2594 return false;
2595
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002596 // Float types are never treated as "integer like".
2597 if (Ty->isRealFloatingType())
2598 return false;
2599
Daniel Dunbar98303b92009-09-13 08:03:58 +00002600 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002601 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002602 return true;
2603
Daniel Dunbar45815812010-02-01 23:31:26 +00002604 // Small complex integer types are "integer like".
2605 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2606 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002607
2608 // Single element and zero sized arrays should be allowed, by the definition
2609 // above, but they are not.
2610
2611 // Otherwise, it must be a record type.
2612 const RecordType *RT = Ty->getAs<RecordType>();
2613 if (!RT) return false;
2614
2615 // Ignore records with flexible arrays.
2616 const RecordDecl *RD = RT->getDecl();
2617 if (RD->hasFlexibleArrayMember())
2618 return false;
2619
2620 // Check that all sub-fields are at offset 0, and are themselves "integer
2621 // like".
2622 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2623
2624 bool HadField = false;
2625 unsigned idx = 0;
2626 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2627 i != e; ++i, ++idx) {
2628 const FieldDecl *FD = *i;
2629
Daniel Dunbar679855a2010-01-29 03:22:29 +00002630 // Bit-fields are not addressable, we only need to verify they are "integer
2631 // like". We still have to disallow a subsequent non-bitfield, for example:
2632 // struct { int : 0; int x }
2633 // is non-integer like according to gcc.
2634 if (FD->isBitField()) {
2635 if (!RD->isUnion())
2636 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002637
Daniel Dunbar679855a2010-01-29 03:22:29 +00002638 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2639 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002640
Daniel Dunbar679855a2010-01-29 03:22:29 +00002641 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002642 }
2643
Daniel Dunbar679855a2010-01-29 03:22:29 +00002644 // Check if this field is at offset 0.
2645 if (Layout.getFieldOffset(idx) != 0)
2646 return false;
2647
Daniel Dunbar98303b92009-09-13 08:03:58 +00002648 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2649 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002650
Daniel Dunbar679855a2010-01-29 03:22:29 +00002651 // Only allow at most one field in a structure. This doesn't match the
2652 // wording above, but follows gcc in situations with a field following an
2653 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002654 if (!RD->isUnion()) {
2655 if (HadField)
2656 return false;
2657
2658 HadField = true;
2659 }
2660 }
2661
2662 return true;
2663}
2664
Chris Lattnera3c109b2010-07-29 02:16:43 +00002665ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002666 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002667 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002668
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002669 // Large vector types should be returned via memory.
2670 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2671 return ABIArgInfo::getIndirect(0);
2672
John McCalld608cdb2010-08-22 10:59:02 +00002673 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002674 // Treat an enum type as its underlying type.
2675 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2676 RetTy = EnumTy->getDecl()->getIntegerType();
2677
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002678 return (RetTy->isPromotableIntegerType() ?
2679 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002680 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002681
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002682 // Structures with either a non-trivial destructor or a non-trivial
2683 // copy constructor are always indirect.
2684 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2685 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2686
Daniel Dunbar98303b92009-09-13 08:03:58 +00002687 // Are we following APCS?
2688 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002689 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002690 return ABIArgInfo::getIgnore();
2691
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002692 // Complex types are all returned as packed integers.
2693 //
2694 // FIXME: Consider using 2 x vector types if the back end handles them
2695 // correctly.
2696 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002697 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002698 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002699
Daniel Dunbar98303b92009-09-13 08:03:58 +00002700 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002701 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002702 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002703 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002704 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002705 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002706 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002707 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2708 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002709 }
2710
2711 // Otherwise return in memory.
2712 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002713 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002714
2715 // Otherwise this is an AAPCS variant.
2716
Chris Lattnera3c109b2010-07-29 02:16:43 +00002717 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002718 return ABIArgInfo::getIgnore();
2719
Bob Wilson3b694fa2011-11-02 04:51:36 +00002720 // Check for homogeneous aggregates with AAPCS-VFP.
2721 if (getABIKind() == AAPCS_VFP) {
2722 const Type *Base = 0;
2723 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2724 // Homogeneous Aggregates are returned directly.
2725 return ABIArgInfo::getDirect();
2726 }
2727
Daniel Dunbar98303b92009-09-13 08:03:58 +00002728 // Aggregates <= 4 bytes are returned in r0; other aggregates
2729 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002730 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002731 if (Size <= 32) {
2732 // Return in the smallest viable integer type.
2733 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002734 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002735 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002736 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2737 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002738 }
2739
Daniel Dunbar98303b92009-09-13 08:03:58 +00002740 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002741}
2742
2743llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002744 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002745 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2746 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002747
2748 CGBuilderTy &Builder = CGF.Builder;
2749 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2750 "ap");
2751 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002752 // Handle address alignment for type alignment > 32 bits
2753 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2754 if (TyAlign > 4) {
2755 assert((TyAlign & (TyAlign - 1)) == 0 &&
2756 "Alignment is not power of 2!");
2757 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2758 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2759 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2760 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2761 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002762 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002763 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002764 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2765
2766 uint64_t Offset =
2767 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2768 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002769 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002770 "ap.next");
2771 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2772
2773 return AddrTyped;
2774}
2775
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002776//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002777// PTX ABI Implementation
2778//===----------------------------------------------------------------------===//
2779
2780namespace {
2781
2782class PTXABIInfo : public ABIInfo {
2783public:
2784 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2785
2786 ABIArgInfo classifyReturnType(QualType RetTy) const;
2787 ABIArgInfo classifyArgumentType(QualType Ty) const;
2788
2789 virtual void computeInfo(CGFunctionInfo &FI) const;
2790 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2791 CodeGenFunction &CFG) const;
2792};
2793
2794class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2795public:
2796 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2797 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002798
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00002799 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2800 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002801};
2802
2803ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2804 if (RetTy->isVoidType())
2805 return ABIArgInfo::getIgnore();
2806 if (isAggregateTypeForABI(RetTy))
2807 return ABIArgInfo::getIndirect(0);
2808 return ABIArgInfo::getDirect();
2809}
2810
2811ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2812 if (isAggregateTypeForABI(Ty))
2813 return ABIArgInfo::getIndirect(0);
2814
2815 return ABIArgInfo::getDirect();
2816}
2817
2818void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2819 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2820 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2821 it != ie; ++it)
2822 it->info = classifyArgumentType(it->type);
2823
2824 // Always honor user-specified calling convention.
2825 if (FI.getCallingConvention() != llvm::CallingConv::C)
2826 return;
2827
2828 // Calling convention as default by an ABI.
2829 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002830 const LangOptions &LangOpts = getContext().getLangOptions();
2831 if (LangOpts.OpenCL || LangOpts.CUDA) {
2832 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002833 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002834 } else {
2835 // If we are in standard C/C++ mode, use the triple to decide on the default
2836 StringRef Env =
2837 getContext().getTargetInfo().getTriple().getEnvironmentName();
2838 if (Env == "device")
2839 DefaultCC = llvm::CallingConv::PTX_Device;
2840 else
2841 DefaultCC = llvm::CallingConv::PTX_Kernel;
2842 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002843 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002844
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002845}
2846
2847llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2848 CodeGenFunction &CFG) const {
2849 llvm_unreachable("PTX does not support varargs");
2850 return 0;
2851}
2852
Justin Holewinski818eafb2011-10-05 17:58:44 +00002853void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2854 llvm::GlobalValue *GV,
2855 CodeGen::CodeGenModule &M) const{
2856 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2857 if (!FD) return;
2858
2859 llvm::Function *F = cast<llvm::Function>(GV);
2860
2861 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002862 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002863 // Use OpenCL function attributes to set proper calling conventions
2864 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002865 if (FD->hasAttr<OpenCLKernelAttr>()) {
2866 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002867 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002868 // And kernel functions are not subject to inlining
2869 F->addFnAttr(llvm::Attribute::NoInline);
2870 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002871 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002872
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002873 // Perform special handling in CUDA mode.
2874 if (M.getLangOptions().CUDA) {
2875 // CUDA __global__ functions get a kernel calling convention. Since
2876 // __global__ functions cannot be called from the device, we do not
2877 // need to set the noinline attribute.
2878 if (FD->getAttr<CUDAGlobalAttr>())
2879 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002880 }
2881}
2882
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002883}
2884
2885//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002886// MBlaze ABI Implementation
2887//===----------------------------------------------------------------------===//
2888
2889namespace {
2890
2891class MBlazeABIInfo : public ABIInfo {
2892public:
2893 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2894
2895 bool isPromotableIntegerType(QualType Ty) const;
2896
2897 ABIArgInfo classifyReturnType(QualType RetTy) const;
2898 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2899
2900 virtual void computeInfo(CGFunctionInfo &FI) const {
2901 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2902 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2903 it != ie; ++it)
2904 it->info = classifyArgumentType(it->type);
2905 }
2906
2907 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2908 CodeGenFunction &CGF) const;
2909};
2910
2911class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2912public:
2913 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2914 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2915 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2916 CodeGen::CodeGenModule &M) const;
2917};
2918
2919}
2920
2921bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2922 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2923 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2924 switch (BT->getKind()) {
2925 case BuiltinType::Bool:
2926 case BuiltinType::Char_S:
2927 case BuiltinType::Char_U:
2928 case BuiltinType::SChar:
2929 case BuiltinType::UChar:
2930 case BuiltinType::Short:
2931 case BuiltinType::UShort:
2932 return true;
2933 default:
2934 return false;
2935 }
2936 return false;
2937}
2938
2939llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2940 CodeGenFunction &CGF) const {
2941 // FIXME: Implement
2942 return 0;
2943}
2944
2945
2946ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2947 if (RetTy->isVoidType())
2948 return ABIArgInfo::getIgnore();
2949 if (isAggregateTypeForABI(RetTy))
2950 return ABIArgInfo::getIndirect(0);
2951
2952 return (isPromotableIntegerType(RetTy) ?
2953 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2954}
2955
2956ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2957 if (isAggregateTypeForABI(Ty))
2958 return ABIArgInfo::getIndirect(0);
2959
2960 return (isPromotableIntegerType(Ty) ?
2961 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2962}
2963
2964void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2965 llvm::GlobalValue *GV,
2966 CodeGen::CodeGenModule &M)
2967 const {
2968 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2969 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002970
Wesley Peck276fdf42010-12-19 19:57:51 +00002971 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2972 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2973 CC = llvm::CallingConv::MBLAZE_INTR;
2974 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2975 CC = llvm::CallingConv::MBLAZE_SVOL;
2976
2977 if (CC != llvm::CallingConv::C) {
2978 // Handle 'interrupt_handler' attribute:
2979 llvm::Function *F = cast<llvm::Function>(GV);
2980
2981 // Step 1: Set ISR calling convention.
2982 F->setCallingConv(CC);
2983
2984 // Step 2: Add attributes goodness.
2985 F->addFnAttr(llvm::Attribute::NoInline);
2986 }
2987
2988 // Step 3: Emit _interrupt_handler alias.
2989 if (CC == llvm::CallingConv::MBLAZE_INTR)
2990 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2991 "_interrupt_handler", GV, &M.getModule());
2992}
2993
2994
2995//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002996// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002997//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002998
2999namespace {
3000
3001class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3002public:
Chris Lattnerea044322010-07-29 02:01:43 +00003003 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3004 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003005 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3006 CodeGen::CodeGenModule &M) const;
3007};
3008
3009}
3010
3011void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3012 llvm::GlobalValue *GV,
3013 CodeGen::CodeGenModule &M) const {
3014 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3015 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3016 // Handle 'interrupt' attribute:
3017 llvm::Function *F = cast<llvm::Function>(GV);
3018
3019 // Step 1: Set ISR calling convention.
3020 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3021
3022 // Step 2: Add attributes goodness.
3023 F->addFnAttr(llvm::Attribute::NoInline);
3024
3025 // Step 3: Emit ISR vector alias.
3026 unsigned Num = attr->getNumber() + 0xffe0;
3027 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003028 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003029 GV, &M.getModule());
3030 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003031 }
3032}
3033
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003034//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003035// MIPS ABI Implementation. This works for both little-endian and
3036// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003037//===----------------------------------------------------------------------===//
3038
John McCallaeeb7012010-05-27 06:19:26 +00003039namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003040class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003041 bool IsO32;
Akira Hatanakab551dd32011-11-03 00:05:50 +00003042 unsigned MinABIStackAlignInBytes;
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003043 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003044 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003045public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003046 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3047 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003048
3049 ABIArgInfo classifyReturnType(QualType RetTy) const;
3050 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3051 virtual void computeInfo(CGFunctionInfo &FI) const;
3052 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3053 CodeGenFunction &CGF) const;
3054};
3055
John McCallaeeb7012010-05-27 06:19:26 +00003056class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003057 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003058public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003059 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3060 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3061 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003062
3063 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3064 return 29;
3065 }
3066
3067 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003068 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003069
3070 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003071 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003072 }
John McCallaeeb7012010-05-27 06:19:26 +00003073};
3074}
3075
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003076// In N32/64, an aligned double precision floating point field is passed in
3077// a register.
3078llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3079 if (IsO32)
3080 return 0;
3081
3082 const RecordType *RT = Ty->getAsStructureType();
3083
3084 if (!RT)
3085 return 0;
3086
3087 const RecordDecl *RD = RT->getDecl();
3088 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3089 uint64_t StructSize = getContext().getTypeSize(Ty);
3090 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3091
3092 SmallVector<llvm::Type*, 8> ArgList;
3093 uint64_t LastOffset = 0;
3094 unsigned idx = 0;
3095 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3096
3097 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3098 i != e; ++i, ++idx) {
3099 const QualType Ty = (*i)->getType();
3100 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3101
3102 if (!BT || BT->getKind() != BuiltinType::Double)
3103 continue;
3104
3105 uint64_t Offset = Layout.getFieldOffset(idx);
3106 if (Offset % 64) // Ignore doubles that are not aligned.
3107 continue;
3108
3109 // Add ((Offset - LastOffset) / 64) args of type i64.
3110 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3111 ArgList.push_back(I64);
3112
3113 // Add double type.
3114 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3115 LastOffset = Offset + 64;
3116 }
3117
3118 // This structure doesn't have an aligned double field.
3119 if (!LastOffset)
3120 return 0;
3121
3122 // Add ((StructSize - LastOffset) / 64) args of type i64.
3123 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3124 ArgList.push_back(I64);
3125
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003126 // If the size of the remainder is not zero, add one more integer type to
3127 // ArgList.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003128 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003129 if (R)
3130 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003131
3132 return llvm::StructType::get(getVMContext(), ArgList);
3133}
3134
Akira Hatanaka619e8872011-06-02 00:09:17 +00003135ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3136 if (isAggregateTypeForABI(Ty)) {
3137 // Ignore empty aggregates.
3138 if (getContext().getTypeSize(Ty) == 0)
3139 return ABIArgInfo::getIgnore();
3140
Akira Hatanaka511949b2011-08-01 18:09:58 +00003141 // Records with non trivial destructors/constructors should not be passed
3142 // by value.
3143 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3144 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3145
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003146 llvm::Type *ResType;
3147 if ((ResType = HandleStructTy(Ty)))
3148 return ABIArgInfo::getDirect(ResType);
3149
Akira Hatanaka619e8872011-06-02 00:09:17 +00003150 return ABIArgInfo::getIndirect(0);
3151 }
3152
3153 // Treat an enum type as its underlying type.
3154 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3155 Ty = EnumTy->getDecl()->getIntegerType();
3156
3157 return (Ty->isPromotableIntegerType() ?
3158 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3159}
3160
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003161llvm::Type*
3162MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
3163 const RecordType *RT = RetTy->getAsStructureType();
3164 SmallVector<llvm::Type*, 2> RTList;
3165
3166 if (RT) {
3167 const RecordDecl *RD = RT->getDecl();
3168 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(), i;
3169
3170 for (i = b; (i != e) && (std::distance(b, i) < 2); ++i) {
3171 const BuiltinType *BT = (*i)->getType()->getAs<BuiltinType>();
3172
3173 if (!BT || !BT->isFloatingPoint())
3174 break;
3175
3176 switch (BT->getKind()) {
3177 case BuiltinType::Float:
3178 RTList.push_back(llvm::Type::getFloatTy(getVMContext()));
3179 break;
3180 case BuiltinType::Double:
3181 RTList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3182 break;
3183 case BuiltinType::LongDouble:
3184 RTList.push_back(llvm::Type::getFP128Ty(getVMContext()));
3185 break;
3186 default:
3187 assert(false && "Unexpexted floating point type.");
3188 }
3189 }
3190
3191 if (i == e)
3192 return llvm::StructType::get(getVMContext(), RTList,
3193 RD->hasAttr<PackedAttr>());
3194
3195 RTList.clear();
3196 }
3197
3198 RTList.push_back(llvm::IntegerType::get(getVMContext(),
3199 std::min(Size, (uint64_t)64)));
3200 if (Size > 64)
3201 RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
3202
3203 return llvm::StructType::get(getVMContext(), RTList);
3204}
3205
Akira Hatanaka619e8872011-06-02 00:09:17 +00003206ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3207 if (RetTy->isVoidType())
3208 return ABIArgInfo::getIgnore();
3209
3210 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003211 uint64_t Size = getContext().getTypeSize(RetTy);
3212 if (Size <= 128) {
3213 if (RetTy->isAnyComplexType())
3214 return ABIArgInfo::getDirect();
3215
3216 if (!IsO32)
3217 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3218 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003219
3220 return ABIArgInfo::getIndirect(0);
3221 }
3222
3223 // Treat an enum type as its underlying type.
3224 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3225 RetTy = EnumTy->getDecl()->getIntegerType();
3226
3227 return (RetTy->isPromotableIntegerType() ?
3228 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3229}
3230
3231void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3232 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3233 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3234 it != ie; ++it)
3235 it->info = classifyArgumentType(it->type);
3236}
3237
3238llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3239 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003240 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3241 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3242
3243 CGBuilderTy &Builder = CGF.Builder;
3244 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3245 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3246 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3247 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3248 llvm::Value *AddrTyped;
3249
3250 if (TypeAlign > MinABIStackAlignInBytes) {
3251 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3252 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3253 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3254 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3255 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3256 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3257 }
3258 else
3259 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3260
3261 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka7b0a0382011-08-12 02:30:14 +00003262 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003263 uint64_t Offset =
3264 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3265 llvm::Value *NextAddr =
3266 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3267 "ap.next");
3268 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3269
3270 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003271}
3272
John McCallaeeb7012010-05-27 06:19:26 +00003273bool
3274MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3275 llvm::Value *Address) const {
3276 // This information comes from gcc's implementation, which seems to
3277 // as canonical as it gets.
3278
3279 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3280 llvm::LLVMContext &Context = CGF.getLLVMContext();
3281
3282 // Everything on MIPS is 4 bytes. Double-precision FP registers
3283 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003284 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003285 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3286
3287 // 0-31 are the general purpose registers, $0 - $31.
3288 // 32-63 are the floating-point registers, $f0 - $f31.
3289 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3290 // 66 is the (notional, I think) register for signal-handler return.
3291 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3292
3293 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3294 // They are one bit wide and ignored here.
3295
3296 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3297 // (coprocessor 1 is the FP unit)
3298 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3299 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3300 // 176-181 are the DSP accumulator registers.
3301 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3302
3303 return false;
3304}
3305
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003306//===----------------------------------------------------------------------===//
3307// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3308// Currently subclassed only to implement custom OpenCL C function attribute
3309// handling.
3310//===----------------------------------------------------------------------===//
3311
3312namespace {
3313
3314class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3315public:
3316 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3317 : DefaultTargetCodeGenInfo(CGT) {}
3318
3319 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3320 CodeGen::CodeGenModule &M) const;
3321};
3322
3323void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3324 llvm::GlobalValue *GV,
3325 CodeGen::CodeGenModule &M) const {
3326 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3327 if (!FD) return;
3328
3329 llvm::Function *F = cast<llvm::Function>(GV);
3330
3331 if (M.getLangOptions().OpenCL) {
3332 if (FD->hasAttr<OpenCLKernelAttr>()) {
3333 // OpenCL C Kernel functions are not subject to inlining
3334 F->addFnAttr(llvm::Attribute::NoInline);
3335
3336 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3337
3338 // Convert the reqd_work_group_size() attributes to metadata.
3339 llvm::LLVMContext &Context = F->getContext();
3340 llvm::NamedMDNode *OpenCLMetadata =
3341 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3342
3343 SmallVector<llvm::Value*, 5> Operands;
3344 Operands.push_back(F);
3345
3346 Operands.push_back(llvm::Constant::getIntegerValue(
3347 llvm::Type::getInt32Ty(Context),
3348 llvm::APInt(
3349 32,
3350 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3351 Operands.push_back(llvm::Constant::getIntegerValue(
3352 llvm::Type::getInt32Ty(Context),
3353 llvm::APInt(
3354 32,
3355 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3356 Operands.push_back(llvm::Constant::getIntegerValue(
3357 llvm::Type::getInt32Ty(Context),
3358 llvm::APInt(
3359 32,
3360 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3361
3362 // Add a boolean constant operand for "required" (true) or "hint" (false)
3363 // for implementing the work_group_size_hint attr later. Currently
3364 // always true as the hint is not yet implemented.
3365 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3366
3367 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3368 }
3369 }
3370 }
3371}
3372
3373}
John McCallaeeb7012010-05-27 06:19:26 +00003374
Tony Linthicum96319392011-12-12 21:14:55 +00003375//===----------------------------------------------------------------------===//
3376// Hexagon ABI Implementation
3377//===----------------------------------------------------------------------===//
3378
3379namespace {
3380
3381class HexagonABIInfo : public ABIInfo {
3382
3383
3384public:
3385 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3386
3387private:
3388
3389 ABIArgInfo classifyReturnType(QualType RetTy) const;
3390 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3391
3392 virtual void computeInfo(CGFunctionInfo &FI) const;
3393
3394 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3395 CodeGenFunction &CGF) const;
3396};
3397
3398class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3399public:
3400 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3401 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3402
3403 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3404 return 29;
3405 }
3406};
3407
3408}
3409
3410void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3411 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3412 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3413 it != ie; ++it)
3414 it->info = classifyArgumentType(it->type);
3415}
3416
3417ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3418 if (!isAggregateTypeForABI(Ty)) {
3419 // Treat an enum type as its underlying type.
3420 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3421 Ty = EnumTy->getDecl()->getIntegerType();
3422
3423 return (Ty->isPromotableIntegerType() ?
3424 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3425 }
3426
3427 // Ignore empty records.
3428 if (isEmptyRecord(getContext(), Ty, true))
3429 return ABIArgInfo::getIgnore();
3430
3431 // Structures with either a non-trivial destructor or a non-trivial
3432 // copy constructor are always indirect.
3433 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3434 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3435
3436 uint64_t Size = getContext().getTypeSize(Ty);
3437 if (Size > 64)
3438 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3439 // Pass in the smallest viable integer type.
3440 else if (Size > 32)
3441 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3442 else if (Size > 16)
3443 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3444 else if (Size > 8)
3445 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3446 else
3447 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3448}
3449
3450ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3451 if (RetTy->isVoidType())
3452 return ABIArgInfo::getIgnore();
3453
3454 // Large vector types should be returned via memory.
3455 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3456 return ABIArgInfo::getIndirect(0);
3457
3458 if (!isAggregateTypeForABI(RetTy)) {
3459 // Treat an enum type as its underlying type.
3460 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3461 RetTy = EnumTy->getDecl()->getIntegerType();
3462
3463 return (RetTy->isPromotableIntegerType() ?
3464 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3465 }
3466
3467 // Structures with either a non-trivial destructor or a non-trivial
3468 // copy constructor are always indirect.
3469 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3470 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3471
3472 if (isEmptyRecord(getContext(), RetTy, true))
3473 return ABIArgInfo::getIgnore();
3474
3475 // Aggregates <= 8 bytes are returned in r0; other aggregates
3476 // are returned indirectly.
3477 uint64_t Size = getContext().getTypeSize(RetTy);
3478 if (Size <= 64) {
3479 // Return in the smallest viable integer type.
3480 if (Size <= 8)
3481 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3482 if (Size <= 16)
3483 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3484 if (Size <= 32)
3485 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3486 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3487 }
3488
3489 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3490}
3491
3492llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3493 CodeGenFunction &CGF) const {
3494 // FIXME: Need to handle alignment
3495 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3496 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3497
3498 CGBuilderTy &Builder = CGF.Builder;
3499 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3500 "ap");
3501 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3502 llvm::Type *PTy =
3503 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3504 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3505
3506 uint64_t Offset =
3507 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3508 llvm::Value *NextAddr =
3509 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3510 "ap.next");
3511 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3512
3513 return AddrTyped;
3514}
3515
3516
Chris Lattnerea044322010-07-29 02:01:43 +00003517const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003518 if (TheTargetCodeGenInfo)
3519 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003520
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003521 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003522 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003523 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003524 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003525
John McCallaeeb7012010-05-27 06:19:26 +00003526 case llvm::Triple::mips:
3527 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003528 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003529
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003530 case llvm::Triple::mips64:
3531 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003532 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003533
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003534 case llvm::Triple::arm:
3535 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003536 {
3537 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003538
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003539 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003540 Kind = ARMABIInfo::APCS;
3541 else if (CodeGenOpts.FloatABI == "hard")
3542 Kind = ARMABIInfo::AAPCS_VFP;
3543
3544 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3545 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003546
John McCallec853ba2010-03-11 00:10:12 +00003547 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003548 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003549
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003550 case llvm::Triple::ptx32:
3551 case llvm::Triple::ptx64:
3552 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3553
Wesley Peck276fdf42010-12-19 19:57:51 +00003554 case llvm::Triple::mblaze:
3555 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3556
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003557 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003558 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003559
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003560 case llvm::Triple::tce:
3561 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3562
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003563 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003564 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003565
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003566 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003567 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003568 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003569
3570 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003571 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003572 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003573 case llvm::Triple::AuroraUX:
3574 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003575 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003576 case llvm::Triple::OpenBSD:
Benjamin Kramer8e50a962011-02-02 18:59:27 +00003577 case llvm::Triple::NetBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003578 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003579 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003580
3581 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003582 return *(TheTargetCodeGenInfo =
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003583 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003584 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003585 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003586
Eli Friedmanee1ad992011-12-02 00:11:43 +00003587 case llvm::Triple::x86_64: {
3588 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3589
Chris Lattnerf13721d2010-08-31 16:44:54 +00003590 switch (Triple.getOS()) {
3591 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003592 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003593 case llvm::Triple::Cygwin:
3594 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3595 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00003596 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3597 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00003598 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003599 }
Tony Linthicum96319392011-12-12 21:14:55 +00003600 case llvm::Triple::hexagon:
3601 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00003602 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003603}