blob: e5865f35bf056db677174c6d0b5518fa09da8293 [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
Eli Friedman55fc7e22012-01-25 22:46:34 +0000392/// UseX86_MMXType - Return true if this is an MMX type that should use the
393/// special 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;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000421 bool IsWin32FloatStructABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000422
423 static bool isRegisterSize(unsigned Size) {
424 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
425 }
426
427 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
428
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000429 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
430 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000431 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000432
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000433 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000434 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000435
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000436public:
Chris Lattnerea044322010-07-29 02:01:43 +0000437
Chris Lattnera3c109b2010-07-29 02:16:43 +0000438 ABIArgInfo classifyReturnType(QualType RetTy) const;
439 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000440
Chris Lattneree5dcd02010-07-29 02:31:05 +0000441 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000442 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000443 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
444 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000445 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000446 }
447
448 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
449 CodeGenFunction &CGF) const;
450
Eli Friedman55fc7e22012-01-25 22:46:34 +0000451 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000452 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Eli Friedman55fc7e22012-01-25 22:46:34 +0000453 IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000455
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000456class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
457public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000458 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
459 bool d, bool p, bool m, bool w)
460 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000461
462 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
463 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000464
465 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
466 // Darwin uses different dwarf register numbers for EH.
467 if (CGM.isTargetDarwin()) return 5;
468
469 return 4;
470 }
471
472 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
473 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000474
Jay Foadef6de3d2011-07-11 09:56:20 +0000475 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000476 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000477 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000478 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
479 }
480
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000481};
482
483}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000484
485/// shouldReturnTypeInRegister - Determine if the given type should be
486/// passed in a register (for the Darwin ABI).
487bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
488 ASTContext &Context) {
489 uint64_t Size = Context.getTypeSize(Ty);
490
491 // Type must be register sized.
492 if (!isRegisterSize(Size))
493 return false;
494
495 if (Ty->isVectorType()) {
496 // 64- and 128- bit vectors inside structures are not returned in
497 // registers.
498 if (Size == 64 || Size == 128)
499 return false;
500
501 return true;
502 }
503
Daniel Dunbar77115232010-05-15 00:00:30 +0000504 // If this is a builtin, pointer, enum, complex type, member pointer, or
505 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000506 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000507 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000508 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000509 return true;
510
511 // Arrays are treated like records.
512 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
513 return shouldReturnTypeInRegister(AT->getElementType(), Context);
514
515 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000516 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000517 if (!RT) return false;
518
Anders Carlssona8874232010-01-27 03:25:19 +0000519 // FIXME: Traverse bases here too.
520
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000521 // Structure types are passed in register if all fields would be
522 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000523 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
524 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000525 const FieldDecl *FD = *i;
526
527 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000528 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000529 continue;
530
531 // Check fields recursively.
532 if (!shouldReturnTypeInRegister(FD->getType(), Context))
533 return false;
534 }
535
536 return true;
537}
538
Chris Lattnera3c109b2010-07-29 02:16:43 +0000539ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
540 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000541 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000542
Chris Lattnera3c109b2010-07-29 02:16:43 +0000543 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000544 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000545 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000546 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000547
548 // 128-bit vectors are a special case; they are returned in
549 // registers and we need to make sure to pick a type the LLVM
550 // backend will like.
551 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000552 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000553 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000554
555 // Always return in register if it fits in a general purpose
556 // register, or if it is 64 bits and has a single element.
557 if ((Size == 8 || Size == 16 || Size == 32) ||
558 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000559 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000560 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000561
562 return ABIArgInfo::getIndirect(0);
563 }
564
565 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000566 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000567
John McCalld608cdb2010-08-22 10:59:02 +0000568 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000569 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000570 // Structures with either a non-trivial destructor or a non-trivial
571 // copy constructor are always indirect.
572 if (hasNonTrivialDestructorOrCopyConstructor(RT))
573 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000574
Anders Carlsson40092972009-10-20 22:07:59 +0000575 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000576 if (RT->getDecl()->hasFlexibleArrayMember())
577 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000578 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000579
David Chisnall1e4249c2009-08-17 23:08:21 +0000580 // If specified, structs and unions are always indirect.
581 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000582 return ABIArgInfo::getIndirect(0);
583
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000584 // Small structures which are register sized are generally returned
585 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000586 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
587 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000588
589 // As a special-case, if the struct is a "single-element" struct, and
590 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000591 // floating-point register. (MSVC does not apply this special case.)
592 // We apply a similar transformation for pointer types to improve the
593 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000594 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000595 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
596 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000597 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
598
599 // FIXME: We should be able to narrow this integer in cases with dead
600 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000601 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000602 }
603
604 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000605 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000606
Chris Lattnera3c109b2010-07-29 02:16:43 +0000607 // Treat an enum type as its underlying type.
608 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
609 RetTy = EnumTy->getDecl()->getIntegerType();
610
611 return (RetTy->isPromotableIntegerType() ?
612 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000613}
614
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000615static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
616 const RecordType *RT = Ty->getAs<RecordType>();
617 if (!RT)
618 return 0;
619 const RecordDecl *RD = RT->getDecl();
620
621 // If this is a C++ record, check the bases first.
622 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
623 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
624 e = CXXRD->bases_end(); i != e; ++i)
625 if (!isRecordWithSSEVectorType(Context, i->getType()))
626 return false;
627
628 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
629 i != e; ++i) {
630 QualType FT = i->getType();
631
Eli Friedman7b1fb812011-11-18 02:12:09 +0000632 if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000633 return true;
634
635 if (isRecordWithSSEVectorType(Context, FT))
636 return true;
637 }
638
639 return false;
640}
641
Daniel Dunbare59d8582010-09-16 20:42:06 +0000642unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
643 unsigned Align) const {
644 // Otherwise, if the alignment is less than or equal to the minimum ABI
645 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000646 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000647 return 0; // Use default alignment.
648
649 // On non-Darwin, the stack type alignment is always 4.
650 if (!IsDarwinVectorABI) {
651 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000652 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000653 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000654
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000655 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedman7b1fb812011-11-18 02:12:09 +0000656 if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000657 return 16;
658
659 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000660}
661
Chris Lattnera3c109b2010-07-29 02:16:43 +0000662ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000663 if (!ByVal)
664 return ABIArgInfo::getIndirect(0, false);
665
Daniel Dunbare59d8582010-09-16 20:42:06 +0000666 // Compute the byval alignment.
667 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
668 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
669 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000670 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000671
672 // If the stack alignment is less than the type alignment, realign the
673 // argument.
674 if (StackAlign < TypeAlign)
675 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
676 /*Realign=*/true);
677
678 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000679}
680
Chris Lattnera3c109b2010-07-29 02:16:43 +0000681ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000682 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000683 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000684 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000685 if (const RecordType *RT = Ty->getAs<RecordType>()) {
686 // Structures with either a non-trivial destructor or a non-trivial
687 // copy constructor are always indirect.
688 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000689 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000690
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000691 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000692 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000693 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000694
Eli Friedman5a4d3522011-11-18 00:28:11 +0000695 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000696 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000697 return ABIArgInfo::getIgnore();
698
Daniel Dunbar53012f42009-11-09 01:33:53 +0000699 // Expand small (<= 128-bit) record types when we know that the stack layout
700 // of those arguments will match the struct. This is important because the
701 // LLVM backend isn't smart enough to remove byval, which inhibits many
702 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000703 if (getContext().getTypeSize(Ty) <= 4*32 &&
704 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000705 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000706
Chris Lattnera3c109b2010-07-29 02:16:43 +0000707 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000708 }
709
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000710 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000711 // On Darwin, some vectors are passed in memory, we handle this by passing
712 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000713 if (IsDarwinVectorABI) {
714 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000715 if ((Size == 8 || Size == 16 || Size == 32) ||
716 (Size == 64 && VT->getNumElements() == 1))
717 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
718 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000719 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000720
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000721 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000722 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000723 if (IsMMXDisabled)
724 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
725 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000726 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
727 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
728 return AAI;
729 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000730
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000731 return ABIArgInfo::getDirect();
732 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000733
734
Chris Lattnera3c109b2010-07-29 02:16:43 +0000735 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
736 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000737
Chris Lattnera3c109b2010-07-29 02:16:43 +0000738 return (Ty->isPromotableIntegerType() ?
739 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000740}
741
742llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
743 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000744 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
745 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000746
747 CGBuilderTy &Builder = CGF.Builder;
748 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
749 "ap");
750 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000751
752 // Compute if the address needs to be aligned
753 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
754 Align = getTypeStackAlignInBytes(Ty, Align);
755 Align = std::max(Align, 4U);
756 if (Align > 4) {
757 // addr = (addr + align - 1) & -align;
758 llvm::Value *Offset =
759 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
760 Addr = CGF.Builder.CreateGEP(Addr, Offset);
761 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
762 CGF.Int32Ty);
763 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
764 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
765 Addr->getType(),
766 "ap.cur.aligned");
767 }
768
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000769 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000770 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000771 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
772
773 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000774 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000775 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000776 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000777 "ap.next");
778 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
779
780 return AddrTyped;
781}
782
Charles Davis74f72932010-02-13 15:54:06 +0000783void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
784 llvm::GlobalValue *GV,
785 CodeGen::CodeGenModule &CGM) const {
786 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
787 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
788 // Get the LLVM function.
789 llvm::Function *Fn = cast<llvm::Function>(GV);
790
791 // Now add the 'alignstack' attribute with a value of 16.
792 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
793 }
794 }
795}
796
John McCall6374c332010-03-06 00:35:14 +0000797bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
798 CodeGen::CodeGenFunction &CGF,
799 llvm::Value *Address) const {
800 CodeGen::CGBuilderTy &Builder = CGF.Builder;
801 llvm::LLVMContext &Context = CGF.getLLVMContext();
802
Chris Lattner2acc6e32011-07-18 04:24:23 +0000803 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000804 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000805
John McCall6374c332010-03-06 00:35:14 +0000806 // 0-7 are the eight integer registers; the order is different
807 // on Darwin (for EH), but the range is the same.
808 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000809 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000810
811 if (CGF.CGM.isTargetDarwin()) {
812 // 12-16 are st(0..4). Not sure why we stop at 4.
813 // These have size 16, which is sizeof(long double) on
814 // platforms with 8-byte alignment for that type.
815 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000816 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000817
John McCall6374c332010-03-06 00:35:14 +0000818 } else {
819 // 9 is %eflags, which doesn't get a size on Darwin for some
820 // reason.
821 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
822
823 // 11-16 are st(0..5). Not sure why we stop at 5.
824 // These have size 12, which is sizeof(long double) on
825 // platforms with 4-byte alignment for that type.
826 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000827 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
828 }
John McCall6374c332010-03-06 00:35:14 +0000829
830 return false;
831}
832
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000833//===----------------------------------------------------------------------===//
834// X86-64 ABI Implementation
835//===----------------------------------------------------------------------===//
836
837
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000838namespace {
839/// X86_64ABIInfo - The X86_64 ABI information.
840class X86_64ABIInfo : public ABIInfo {
841 enum Class {
842 Integer = 0,
843 SSE,
844 SSEUp,
845 X87,
846 X87Up,
847 ComplexX87,
848 NoClass,
849 Memory
850 };
851
852 /// merge - Implement the X86_64 ABI merging algorithm.
853 ///
854 /// Merge an accumulating classification \arg Accum with a field
855 /// classification \arg Field.
856 ///
857 /// \param Accum - The accumulating classification. This should
858 /// always be either NoClass or the result of a previous merge
859 /// call. In addition, this should never be Memory (the caller
860 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000861 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000862
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000863 /// postMerge - Implement the X86_64 ABI post merging algorithm.
864 ///
865 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
866 /// final MEMORY or SSE classes when necessary.
867 ///
868 /// \param AggregateSize - The size of the current aggregate in
869 /// the classification process.
870 ///
871 /// \param Lo - The classification for the parts of the type
872 /// residing in the low word of the containing object.
873 ///
874 /// \param Hi - The classification for the parts of the type
875 /// residing in the higher words of the containing object.
876 ///
877 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
878
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000879 /// classify - Determine the x86_64 register classes in which the
880 /// given type T should be passed.
881 ///
882 /// \param Lo - The classification for the parts of the type
883 /// residing in the low word of the containing object.
884 ///
885 /// \param Hi - The classification for the parts of the type
886 /// residing in the high word of the containing object.
887 ///
888 /// \param OffsetBase - The bit offset of this type in the
889 /// containing object. Some parameters are classified different
890 /// depending on whether they straddle an eightbyte boundary.
891 ///
892 /// If a word is unused its result will be NoClass; if a type should
893 /// be passed in Memory then at least the classification of \arg Lo
894 /// will be Memory.
895 ///
896 /// The \arg Lo class will be NoClass iff the argument is ignored.
897 ///
898 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
899 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000900 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000901
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000902 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000903 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
904 unsigned IROffset, QualType SourceTy,
905 unsigned SourceOffset) const;
906 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
907 unsigned IROffset, QualType SourceTy,
908 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000909
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000910 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000911 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000912 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000913
914 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000915 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000916 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000917
Chris Lattnera3c109b2010-07-29 02:16:43 +0000918 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000919
Bill Wendlingbb465d72010-10-18 03:41:31 +0000920 ABIArgInfo classifyArgumentType(QualType Ty,
921 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000922 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000923
Eli Friedmanee1ad992011-12-02 00:11:43 +0000924 bool IsIllegalVectorType(QualType Ty) const;
925
John McCall67a57732011-04-21 01:20:55 +0000926 /// The 0.98 ABI revision clarified a lot of ambiguities,
927 /// unfortunately in ways that were not always consistent with
928 /// certain previous compilers. In particular, platforms which
929 /// required strict binary compatibility with older versions of GCC
930 /// may need to exempt themselves.
931 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000932 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000933 }
934
Eli Friedmanee1ad992011-12-02 00:11:43 +0000935 bool HasAVX;
936
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000937public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000938 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
939 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000940
Chris Lattneree5dcd02010-07-29 02:31:05 +0000941 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000942
943 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
944 CodeGenFunction &CGF) const;
945};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000946
Chris Lattnerf13721d2010-08-31 16:44:54 +0000947/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000948class WinX86_64ABIInfo : public ABIInfo {
949
950 ABIArgInfo classify(QualType Ty) const;
951
Chris Lattnerf13721d2010-08-31 16:44:54 +0000952public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000953 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
954
955 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000956
957 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
958 CodeGenFunction &CGF) const;
959};
960
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000961class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
962public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000963 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
964 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +0000965
966 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
967 return 7;
968 }
969
970 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
971 llvm::Value *Address) const {
972 CodeGen::CGBuilderTy &Builder = CGF.Builder;
973 llvm::LLVMContext &Context = CGF.getLLVMContext();
974
Chris Lattner2acc6e32011-07-18 04:24:23 +0000975 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall6374c332010-03-06 00:35:14 +0000976 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000977
John McCallaeeb7012010-05-27 06:19:26 +0000978 // 0-15 are the 16 integer registers.
979 // 16 is %rip.
980 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000981
982 return false;
983 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000984
Jay Foadef6de3d2011-07-11 09:56:20 +0000985 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000986 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000987 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000988 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
989 }
990
Eli Friedman3ed79032011-12-01 04:53:19 +0000991 bool isNoProtoCallVariadic(const CodeGen::CGFunctionInfo &FI) const {
John McCall01f151e2011-09-21 08:08:30 +0000992 // The default CC on x86-64 sets %al to the number of SSA
993 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +0000994 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +0000995 // that when AVX types are involved: the ABI explicitly states it is
996 // undefined, and it doesn't work in practice because of how the ABI
997 // defines varargs anyway.
Eli Friedman3ed79032011-12-01 04:53:19 +0000998 if (FI.getCallingConvention() == llvm::CallingConv::C) {
999 bool HasAVXType = false;
1000 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1001 ie = FI.arg_end();
1002 it != ie; ++it) {
1003 if (it->info.isDirect()) {
1004 llvm::Type *Ty = it->info.getCoerceToType();
1005 if (llvm::VectorType *VTy = dyn_cast_or_null<llvm::VectorType>(Ty)) {
1006 if (VTy->getBitWidth() > 128) {
1007 HasAVXType = true;
1008 break;
1009 }
1010 }
1011 }
1012 }
1013 if (!HasAVXType)
1014 return true;
1015 }
John McCall01f151e2011-09-21 08:08:30 +00001016
Eli Friedman3ed79032011-12-01 04:53:19 +00001017 return TargetCodeGenInfo::isNoProtoCallVariadic(FI);
John McCall01f151e2011-09-21 08:08:30 +00001018 }
1019
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001020};
1021
Chris Lattnerf13721d2010-08-31 16:44:54 +00001022class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1023public:
1024 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1025 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1026
1027 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1028 return 7;
1029 }
1030
1031 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1032 llvm::Value *Address) const {
1033 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1034 llvm::LLVMContext &Context = CGF.getLLVMContext();
1035
Chris Lattner2acc6e32011-07-18 04:24:23 +00001036 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001037 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001038
Chris Lattnerf13721d2010-08-31 16:44:54 +00001039 // 0-15 are the 16 integer registers.
1040 // 16 is %rip.
1041 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1042
1043 return false;
1044 }
1045};
1046
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001047}
1048
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001049void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1050 Class &Hi) const {
1051 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1052 //
1053 // (a) If one of the classes is Memory, the whole argument is passed in
1054 // memory.
1055 //
1056 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1057 // memory.
1058 //
1059 // (c) If the size of the aggregate exceeds two eightbytes and the first
1060 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1061 // argument is passed in memory. NOTE: This is necessary to keep the
1062 // ABI working for processors that don't support the __m256 type.
1063 //
1064 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1065 //
1066 // Some of these are enforced by the merging logic. Others can arise
1067 // only with unions; for example:
1068 // union { _Complex double; unsigned; }
1069 //
1070 // Note that clauses (b) and (c) were added in 0.98.
1071 //
1072 if (Hi == Memory)
1073 Lo = Memory;
1074 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1075 Lo = Memory;
1076 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1077 Lo = Memory;
1078 if (Hi == SSEUp && Lo != SSE)
1079 Hi = SSE;
1080}
1081
Chris Lattner1090a9b2010-06-28 21:43:59 +00001082X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001083 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1084 // classified recursively so that always two fields are
1085 // considered. The resulting class is calculated according to
1086 // the classes of the fields in the eightbyte:
1087 //
1088 // (a) If both classes are equal, this is the resulting class.
1089 //
1090 // (b) If one of the classes is NO_CLASS, the resulting class is
1091 // the other class.
1092 //
1093 // (c) If one of the classes is MEMORY, the result is the MEMORY
1094 // class.
1095 //
1096 // (d) If one of the classes is INTEGER, the result is the
1097 // INTEGER.
1098 //
1099 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1100 // MEMORY is used as class.
1101 //
1102 // (f) Otherwise class SSE is used.
1103
1104 // Accum should never be memory (we should have returned) or
1105 // ComplexX87 (because this cannot be passed in a structure).
1106 assert((Accum != Memory && Accum != ComplexX87) &&
1107 "Invalid accumulated classification during merge.");
1108 if (Accum == Field || Field == NoClass)
1109 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001110 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001111 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001112 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001113 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001114 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001115 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001116 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1117 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001119 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001120}
1121
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001122void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001123 Class &Lo, Class &Hi) const {
1124 // FIXME: This code can be simplified by introducing a simple value class for
1125 // Class pairs with appropriate constructor methods for the various
1126 // situations.
1127
1128 // FIXME: Some of the split computations are wrong; unaligned vectors
1129 // shouldn't be passed in registers for example, so there is no chance they
1130 // can straddle an eightbyte. Verify & simplify.
1131
1132 Lo = Hi = NoClass;
1133
1134 Class &Current = OffsetBase < 64 ? Lo : Hi;
1135 Current = Memory;
1136
John McCall183700f2009-09-21 23:43:11 +00001137 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001138 BuiltinType::Kind k = BT->getKind();
1139
1140 if (k == BuiltinType::Void) {
1141 Current = NoClass;
1142 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1143 Lo = Integer;
1144 Hi = Integer;
1145 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1146 Current = Integer;
1147 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1148 Current = SSE;
1149 } else if (k == BuiltinType::LongDouble) {
1150 Lo = X87;
1151 Hi = X87Up;
1152 }
1153 // FIXME: _Decimal32 and _Decimal64 are SSE.
1154 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001155 return;
1156 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001157
Chris Lattner1090a9b2010-06-28 21:43:59 +00001158 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001159 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001160 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001161 return;
1162 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001163
Chris Lattner1090a9b2010-06-28 21:43:59 +00001164 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001165 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001166 return;
1167 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001168
Chris Lattner1090a9b2010-06-28 21:43:59 +00001169 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001170 if (Ty->isMemberFunctionPointerType())
1171 Lo = Hi = Integer;
1172 else
1173 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001174 return;
1175 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001176
Chris Lattner1090a9b2010-06-28 21:43:59 +00001177 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001178 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001179 if (Size == 32) {
1180 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1181 // float> as integer.
1182 Current = Integer;
1183
1184 // If this type crosses an eightbyte boundary, it should be
1185 // split.
1186 uint64_t EB_Real = (OffsetBase) / 64;
1187 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1188 if (EB_Real != EB_Imag)
1189 Hi = Lo;
1190 } else if (Size == 64) {
1191 // gcc passes <1 x double> in memory. :(
1192 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1193 return;
1194
1195 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001196 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001197 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1198 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1199 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001200 Current = Integer;
1201 else
1202 Current = SSE;
1203
1204 // If this type crosses an eightbyte boundary, it should be
1205 // split.
1206 if (OffsetBase && OffsetBase != 64)
1207 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001208 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001209 // Arguments of 256-bits are split into four eightbyte chunks. The
1210 // least significant one belongs to class SSE and all the others to class
1211 // SSEUP. The original Lo and Hi design considers that types can't be
1212 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1213 // This design isn't correct for 256-bits, but since there're no cases
1214 // where the upper parts would need to be inspected, avoid adding
1215 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001216 Lo = SSE;
1217 Hi = SSEUp;
1218 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001219 return;
1220 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001221
Chris Lattner1090a9b2010-06-28 21:43:59 +00001222 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001223 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001224
Chris Lattnerea044322010-07-29 02:01:43 +00001225 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001226 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001227 if (Size <= 64)
1228 Current = Integer;
1229 else if (Size <= 128)
1230 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001231 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001232 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001233 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001234 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001235 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236 Current = ComplexX87;
1237
1238 // If this complex type crosses an eightbyte boundary then it
1239 // should be split.
1240 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001241 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001242 if (Hi == NoClass && EB_Real != EB_Imag)
1243 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001244
Chris Lattner1090a9b2010-06-28 21:43:59 +00001245 return;
1246 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001247
Chris Lattnerea044322010-07-29 02:01:43 +00001248 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001249 // Arrays are treated like structures.
1250
Chris Lattnerea044322010-07-29 02:01:43 +00001251 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001252
1253 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001254 // than four eightbytes, ..., it has class MEMORY.
1255 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001256 return;
1257
1258 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1259 // fields, it has class MEMORY.
1260 //
1261 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001262 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001263 return;
1264
1265 // Otherwise implement simplified merge. We could be smarter about
1266 // this, but it isn't worth it and would be harder to verify.
1267 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001268 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001269 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001270
1271 // The only case a 256-bit wide vector could be used is when the array
1272 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1273 // to work for sizes wider than 128, early check and fallback to memory.
1274 if (Size > 128 && EltSize != 256)
1275 return;
1276
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001277 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1278 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001279 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280 Lo = merge(Lo, FieldLo);
1281 Hi = merge(Hi, FieldHi);
1282 if (Lo == Memory || Hi == Memory)
1283 break;
1284 }
1285
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001286 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001287 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001288 return;
1289 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001290
Chris Lattner1090a9b2010-06-28 21:43:59 +00001291 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001292 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001293
1294 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001295 // than four eightbytes, ..., it has class MEMORY.
1296 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001297 return;
1298
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001299 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1300 // copy constructor or a non-trivial destructor, it is passed by invisible
1301 // reference.
1302 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1303 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001304
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001305 const RecordDecl *RD = RT->getDecl();
1306
1307 // Assume variable sized types are passed in memory.
1308 if (RD->hasFlexibleArrayMember())
1309 return;
1310
Chris Lattnerea044322010-07-29 02:01:43 +00001311 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001312
1313 // Reset Lo class, this will be recomputed.
1314 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001315
1316 // If this is a C++ record, classify the bases first.
1317 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1318 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1319 e = CXXRD->bases_end(); i != e; ++i) {
1320 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1321 "Unexpected base class!");
1322 const CXXRecordDecl *Base =
1323 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1324
1325 // Classify this field.
1326 //
1327 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1328 // single eightbyte, each is classified separately. Each eightbyte gets
1329 // initialized to class NO_CLASS.
1330 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001331 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001332 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001333 Lo = merge(Lo, FieldLo);
1334 Hi = merge(Hi, FieldHi);
1335 if (Lo == Memory || Hi == Memory)
1336 break;
1337 }
1338 }
1339
1340 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001341 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001342 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001343 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001344 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1345 bool BitField = i->isBitField();
1346
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001347 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1348 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001349 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001350 // The only case a 256-bit wide vector could be used is when the struct
1351 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1352 // to work for sizes wider than 128, early check and fallback to memory.
1353 //
1354 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1355 Lo = Memory;
1356 return;
1357 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001358 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001359 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001360 Lo = Memory;
1361 return;
1362 }
1363
1364 // Classify this field.
1365 //
1366 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1367 // exceeds a single eightbyte, each is classified
1368 // separately. Each eightbyte gets initialized to class
1369 // NO_CLASS.
1370 Class FieldLo, FieldHi;
1371
1372 // Bit-fields require special handling, they do not force the
1373 // structure to be passed in memory even if unaligned, and
1374 // therefore they can straddle an eightbyte.
1375 if (BitField) {
1376 // Ignore padding bit-fields.
1377 if (i->isUnnamedBitfield())
1378 continue;
1379
1380 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001381 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001382
1383 uint64_t EB_Lo = Offset / 64;
1384 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1385 FieldLo = FieldHi = NoClass;
1386 if (EB_Lo) {
1387 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1388 FieldLo = NoClass;
1389 FieldHi = Integer;
1390 } else {
1391 FieldLo = Integer;
1392 FieldHi = EB_Hi ? Integer : NoClass;
1393 }
1394 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001395 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001396 Lo = merge(Lo, FieldLo);
1397 Hi = merge(Hi, FieldHi);
1398 if (Lo == Memory || Hi == Memory)
1399 break;
1400 }
1401
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001402 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001403 }
1404}
1405
Chris Lattner9c254f02010-06-29 06:01:59 +00001406ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001407 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1408 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001409 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001410 // Treat an enum type as its underlying type.
1411 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1412 Ty = EnumTy->getDecl()->getIntegerType();
1413
1414 return (Ty->isPromotableIntegerType() ?
1415 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1416 }
1417
1418 return ABIArgInfo::getIndirect(0);
1419}
1420
Eli Friedmanee1ad992011-12-02 00:11:43 +00001421bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1422 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1423 uint64_t Size = getContext().getTypeSize(VecTy);
1424 unsigned LargestVector = HasAVX ? 256 : 128;
1425 if (Size <= 64 || Size > LargestVector)
1426 return true;
1427 }
1428
1429 return false;
1430}
1431
Chris Lattner9c254f02010-06-29 06:01:59 +00001432ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001433 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1434 // place naturally.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001435 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001436 // Treat an enum type as its underlying type.
1437 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1438 Ty = EnumTy->getDecl()->getIntegerType();
1439
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001440 return (Ty->isPromotableIntegerType() ?
1441 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001442 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001443
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001444 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1445 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001446
Chris Lattner855d2272011-05-22 23:21:23 +00001447 // Compute the byval alignment. We specify the alignment of the byval in all
1448 // cases so that the mid-level optimizer knows the alignment of the byval.
1449 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1450 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001451}
1452
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001453/// GetByteVectorType - The ABI specifies that a value should be passed in an
1454/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001455/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001456llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001457 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001458
Chris Lattner15842bd2010-07-29 05:02:29 +00001459 // Wrapper structs that just contain vectors are passed just like vectors,
1460 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001461 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001462 while (STy && STy->getNumElements() == 1) {
1463 IRType = STy->getElementType(0);
1464 STy = dyn_cast<llvm::StructType>(IRType);
1465 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001466
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001467 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001468 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1469 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001470 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001471 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001472 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1473 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1474 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1475 EltTy->isIntegerTy(128)))
1476 return VT;
1477 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001478
Chris Lattner0f408f52010-07-29 04:56:46 +00001479 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1480}
1481
Chris Lattnere2962be2010-07-29 07:30:00 +00001482/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1483/// is known to either be off the end of the specified type or being in
1484/// alignment padding. The user type specified is known to be at most 128 bits
1485/// in size, and have passed through X86_64ABIInfo::classify with a successful
1486/// classification that put one of the two halves in the INTEGER class.
1487///
1488/// It is conservatively correct to return false.
1489static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1490 unsigned EndBit, ASTContext &Context) {
1491 // If the bytes being queried are off the end of the type, there is no user
1492 // data hiding here. This handles analysis of builtins, vectors and other
1493 // types that don't contain interesting padding.
1494 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1495 if (TySize <= StartBit)
1496 return true;
1497
Chris Lattner021c3a32010-07-29 07:43:55 +00001498 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1499 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1500 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1501
1502 // Check each element to see if the element overlaps with the queried range.
1503 for (unsigned i = 0; i != NumElts; ++i) {
1504 // If the element is after the span we care about, then we're done..
1505 unsigned EltOffset = i*EltSize;
1506 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001507
Chris Lattner021c3a32010-07-29 07:43:55 +00001508 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1509 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1510 EndBit-EltOffset, Context))
1511 return false;
1512 }
1513 // If it overlaps no elements, then it is safe to process as padding.
1514 return true;
1515 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001516
Chris Lattnere2962be2010-07-29 07:30:00 +00001517 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1518 const RecordDecl *RD = RT->getDecl();
1519 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001520
Chris Lattnere2962be2010-07-29 07:30:00 +00001521 // If this is a C++ record, check the bases first.
1522 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1523 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1524 e = CXXRD->bases_end(); i != e; ++i) {
1525 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1526 "Unexpected base class!");
1527 const CXXRecordDecl *Base =
1528 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001529
Chris Lattnere2962be2010-07-29 07:30:00 +00001530 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001531 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001532 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001533
Chris Lattnere2962be2010-07-29 07:30:00 +00001534 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1535 if (!BitsContainNoUserData(i->getType(), BaseStart,
1536 EndBit-BaseOffset, Context))
1537 return false;
1538 }
1539 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001540
Chris Lattnere2962be2010-07-29 07:30:00 +00001541 // Verify that no field has data that overlaps the region of interest. Yes
1542 // this could be sped up a lot by being smarter about queried fields,
1543 // however we're only looking at structs up to 16 bytes, so we don't care
1544 // much.
1545 unsigned idx = 0;
1546 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1547 i != e; ++i, ++idx) {
1548 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001549
Chris Lattnere2962be2010-07-29 07:30:00 +00001550 // If we found a field after the region we care about, then we're done.
1551 if (FieldOffset >= EndBit) break;
1552
1553 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1554 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1555 Context))
1556 return false;
1557 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001558
Chris Lattnere2962be2010-07-29 07:30:00 +00001559 // If nothing in this record overlapped the area of interest, then we're
1560 // clean.
1561 return true;
1562 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001563
Chris Lattnere2962be2010-07-29 07:30:00 +00001564 return false;
1565}
1566
Chris Lattner0b362002010-07-29 18:39:32 +00001567/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1568/// float member at the specified offset. For example, {int,{float}} has a
1569/// float at offset 4. It is conservatively correct for this routine to return
1570/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001571static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001572 const llvm::TargetData &TD) {
1573 // Base case if we find a float.
1574 if (IROffset == 0 && IRType->isFloatTy())
1575 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001576
Chris Lattner0b362002010-07-29 18:39:32 +00001577 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001578 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001579 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1580 unsigned Elt = SL->getElementContainingOffset(IROffset);
1581 IROffset -= SL->getElementOffset(Elt);
1582 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1583 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001584
Chris Lattner0b362002010-07-29 18:39:32 +00001585 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001586 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1587 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001588 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1589 IROffset -= IROffset/EltSize*EltSize;
1590 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1591 }
1592
1593 return false;
1594}
1595
Chris Lattnerf47c9442010-07-29 18:13:09 +00001596
1597/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1598/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001599llvm::Type *X86_64ABIInfo::
1600GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001601 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001602 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001603 // pass as float if the last 4 bytes is just padding. This happens for
1604 // structs that contain 3 floats.
1605 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1606 SourceOffset*8+64, getContext()))
1607 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001608
Chris Lattner0b362002010-07-29 18:39:32 +00001609 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1610 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1611 // case.
1612 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001613 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1614 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001615
Chris Lattnerf47c9442010-07-29 18:13:09 +00001616 return llvm::Type::getDoubleTy(getVMContext());
1617}
1618
1619
Chris Lattner0d2656d2010-07-29 17:40:35 +00001620/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1621/// an 8-byte GPR. This means that we either have a scalar or we are talking
1622/// about the high or low part of an up-to-16-byte struct. This routine picks
1623/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001624/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1625/// etc).
1626///
1627/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1628/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1629/// the 8-byte value references. PrefType may be null.
1630///
1631/// SourceTy is the source level type for the entire argument. SourceOffset is
1632/// an offset into this that we're processing (which is always either 0 or 8).
1633///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001634llvm::Type *X86_64ABIInfo::
1635GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001636 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001637 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1638 // returning an 8-byte unit starting with it. See if we can safely use it.
1639 if (IROffset == 0) {
1640 // Pointers and int64's always fill the 8-byte unit.
1641 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1642 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001643
Chris Lattnere2962be2010-07-29 07:30:00 +00001644 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1645 // goodness in the source type is just tail padding. This is allowed to
1646 // kick in for struct {double,int} on the int, but not on
1647 // struct{double,int,int} because we wouldn't return the second int. We
1648 // have to do this analysis on the source type because we can't depend on
1649 // unions being lowered a specific way etc.
1650 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1651 IRType->isIntegerTy(32)) {
1652 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001653
Chris Lattnere2962be2010-07-29 07:30:00 +00001654 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1655 SourceOffset*8+64, getContext()))
1656 return IRType;
1657 }
1658 }
Chris Lattner49382de2010-07-28 22:44:07 +00001659
Chris Lattner2acc6e32011-07-18 04:24:23 +00001660 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001661 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001662 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001663 if (IROffset < SL->getSizeInBytes()) {
1664 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1665 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001666
Chris Lattner0d2656d2010-07-29 17:40:35 +00001667 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1668 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001669 }
Chris Lattner49382de2010-07-28 22:44:07 +00001670 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001671
Chris Lattner2acc6e32011-07-18 04:24:23 +00001672 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001673 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001674 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1675 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001676 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1677 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001678 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001679
Chris Lattner49382de2010-07-28 22:44:07 +00001680 // Okay, we don't have any better idea of what to pass, so we pass this in an
1681 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001682 unsigned TySizeInBytes =
1683 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001684
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001685 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001686
Chris Lattner49382de2010-07-28 22:44:07 +00001687 // It is always safe to classify this as an integer type up to i64 that
1688 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001689 return llvm::IntegerType::get(getVMContext(),
1690 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001691}
1692
Chris Lattner66e7b682010-09-01 00:50:20 +00001693
1694/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1695/// be used as elements of a two register pair to pass or return, return a
1696/// first class aggregate to represent them. For example, if the low part of
1697/// a by-value argument should be passed as i32* and the high part as float,
1698/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001699static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001700GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001701 const llvm::TargetData &TD) {
1702 // In order to correctly satisfy the ABI, we need to the high part to start
1703 // at offset 8. If the high and low parts we inferred are both 4-byte types
1704 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1705 // the second element at offset 8. Check for this:
1706 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1707 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1708 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1709 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001710
Chris Lattner66e7b682010-09-01 00:50:20 +00001711 // To handle this, we have to increase the size of the low part so that the
1712 // second element will start at an 8 byte offset. We can't increase the size
1713 // of the second element because it might make us access off the end of the
1714 // struct.
1715 if (HiStart != 8) {
1716 // There are only two sorts of types the ABI generation code can produce for
1717 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1718 // Promote these to a larger type.
1719 if (Lo->isFloatTy())
1720 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1721 else {
1722 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1723 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1724 }
1725 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001726
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001727 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001728
1729
Chris Lattner66e7b682010-09-01 00:50:20 +00001730 // Verify that the second element is at an 8-byte offset.
1731 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1732 "Invalid x86-64 argument pair!");
1733 return Result;
1734}
1735
Chris Lattner519f68c2010-07-28 23:06:14 +00001736ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001737classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001738 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1739 // classification algorithm.
1740 X86_64ABIInfo::Class Lo, Hi;
1741 classify(RetTy, 0, Lo, Hi);
1742
1743 // Check some invariants.
1744 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001745 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1746
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001747 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001748 switch (Lo) {
1749 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001750 if (Hi == NoClass)
1751 return ABIArgInfo::getIgnore();
1752 // If the low part is just padding, it takes no register, leave ResType
1753 // null.
1754 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1755 "Unknown missing lo part");
1756 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001757
1758 case SSEUp:
1759 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001760 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001761
1762 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1763 // hidden argument.
1764 case Memory:
1765 return getIndirectReturnResult(RetTy);
1766
1767 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1768 // available register of the sequence %rax, %rdx is used.
1769 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001770 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001771
Chris Lattnereb518b42010-07-29 21:42:50 +00001772 // If we have a sign or zero extended integer, make sure to return Extend
1773 // so that the parameter gets the right LLVM IR attributes.
1774 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1775 // Treat an enum type as its underlying type.
1776 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1777 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001778
Chris Lattnereb518b42010-07-29 21:42:50 +00001779 if (RetTy->isIntegralOrEnumerationType() &&
1780 RetTy->isPromotableIntegerType())
1781 return ABIArgInfo::getExtend();
1782 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001783 break;
1784
1785 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1786 // available SSE register of the sequence %xmm0, %xmm1 is used.
1787 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001788 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001789 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001790
1791 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1792 // returned on the X87 stack in %st0 as 80-bit x87 number.
1793 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001794 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001795 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001796
1797 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1798 // part of the value is returned in %st0 and the imaginary part in
1799 // %st1.
1800 case ComplexX87:
1801 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001802 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001803 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001804 NULL);
1805 break;
1806 }
1807
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001808 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001809 switch (Hi) {
1810 // Memory was handled previously and X87 should
1811 // never occur as a hi class.
1812 case Memory:
1813 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001814 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001815
1816 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001817 case NoClass:
1818 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001819
Chris Lattner3db4dde2010-09-01 00:20:33 +00001820 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001821 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001822 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1823 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001824 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001825 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001826 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001827 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1828 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001829 break;
1830
1831 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001832 // is passed in the next available eightbyte chunk if the last used
1833 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001834 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001835 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001836 case SSEUp:
1837 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001838 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001839 break;
1840
1841 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1842 // returned together with the previous X87 value in %st0.
1843 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001844 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001845 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001846 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001847 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001848 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001849 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001850 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1851 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001852 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001853 break;
1854 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001855
Chris Lattner3db4dde2010-09-01 00:20:33 +00001856 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001857 // known to pass in the high eightbyte of the result. We do this by forming a
1858 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001859 if (HighPart)
1860 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001861
Chris Lattnereb518b42010-07-29 21:42:50 +00001862 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001863}
1864
Chris Lattnera3c109b2010-07-29 02:16:43 +00001865ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001866 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001867 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001868 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001869
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001870 // Check some invariants.
1871 // FIXME: Enforce these by construction.
1872 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001873 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1874
1875 neededInt = 0;
1876 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001877 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001878 switch (Lo) {
1879 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001880 if (Hi == NoClass)
1881 return ABIArgInfo::getIgnore();
1882 // If the low part is just padding, it takes no register, leave ResType
1883 // null.
1884 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1885 "Unknown missing lo part");
1886 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001887
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001888 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1889 // on the stack.
1890 case Memory:
1891
1892 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1893 // COMPLEX_X87, it is passed in memory.
1894 case X87:
1895 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001896 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1897 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001898 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001899
1900 case SSEUp:
1901 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001902 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001903
1904 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1905 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1906 // and %r9 is used.
1907 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001908 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001909
Chris Lattner49382de2010-07-28 22:44:07 +00001910 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001911 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001912
1913 // If we have a sign or zero extended integer, make sure to return Extend
1914 // so that the parameter gets the right LLVM IR attributes.
1915 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1916 // Treat an enum type as its underlying type.
1917 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1918 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001919
Chris Lattnereb518b42010-07-29 21:42:50 +00001920 if (Ty->isIntegralOrEnumerationType() &&
1921 Ty->isPromotableIntegerType())
1922 return ABIArgInfo::getExtend();
1923 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001924
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001925 break;
1926
1927 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1928 // available SSE register is used, the registers are taken in the
1929 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001930 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001931 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001932 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001933 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001934 break;
1935 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001936 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001937
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001938 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001939 switch (Hi) {
1940 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001941 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001942 // which is passed in memory.
1943 case Memory:
1944 case X87:
1945 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001946 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001947
1948 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001949
Chris Lattner645406a2010-09-01 00:24:35 +00001950 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001951 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001952 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001953 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001954
Chris Lattner645406a2010-09-01 00:24:35 +00001955 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1956 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001957 break;
1958
1959 // X87Up generally doesn't occur here (long double is passed in
1960 // memory), except in situations involving unions.
1961 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001962 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001963 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001964
Chris Lattner645406a2010-09-01 00:24:35 +00001965 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1966 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001967
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001968 ++neededSSE;
1969 break;
1970
1971 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1972 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001973 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001974 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001975 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001976 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001977 break;
1978 }
1979
Chris Lattner645406a2010-09-01 00:24:35 +00001980 // If a high part was specified, merge it together with the low part. It is
1981 // known to pass in the high eightbyte of the result. We do this by forming a
1982 // first class struct aggregate with the high and low part: {low, high}
1983 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001984 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001985
Chris Lattnereb518b42010-07-29 21:42:50 +00001986 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001987}
1988
Chris Lattneree5dcd02010-07-29 02:31:05 +00001989void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001990
Chris Lattnera3c109b2010-07-29 02:16:43 +00001991 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001992
1993 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001994 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001995
1996 // If the return value is indirect, then the hidden argument is consuming one
1997 // integer register.
1998 if (FI.getReturnInfo().isIndirect())
1999 --freeIntRegs;
2000
2001 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2002 // get assigned (in left-to-right order) for passing as follows...
2003 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2004 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002005 unsigned neededInt, neededSSE;
2006 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002007
2008 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2009 // eightbyte of an argument, the whole argument is passed on the
2010 // stack. If registers have already been assigned for some
2011 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002012 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002013 freeIntRegs -= neededInt;
2014 freeSSERegs -= neededSSE;
2015 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00002016 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002017 }
2018 }
2019}
2020
2021static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2022 QualType Ty,
2023 CodeGenFunction &CGF) {
2024 llvm::Value *overflow_arg_area_p =
2025 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2026 llvm::Value *overflow_arg_area =
2027 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2028
2029 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2030 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002031 // It isn't stated explicitly in the standard, but in practice we use
2032 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002033 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2034 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002035 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002036 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002037 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002038 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2039 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002040 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002041 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002042 overflow_arg_area =
2043 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2044 overflow_arg_area->getType(),
2045 "overflow_arg_area.align");
2046 }
2047
2048 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002049 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002050 llvm::Value *Res =
2051 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002052 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002053
2054 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2055 // l->overflow_arg_area + sizeof(type).
2056 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2057 // an 8 byte boundary.
2058
2059 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002060 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002061 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002062 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2063 "overflow_arg_area.next");
2064 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2065
2066 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2067 return Res;
2068}
2069
2070llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2071 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00002072 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002074 // Assume that va_list type is correct; should be pointer to LLVM type:
2075 // struct {
2076 // i32 gp_offset;
2077 // i32 fp_offset;
2078 // i8* overflow_arg_area;
2079 // i8* reg_save_area;
2080 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002081 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002082
Chris Lattnera14db752010-03-11 18:19:55 +00002083 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002084 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002085
2086 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2087 // in the registers. If not go to step 7.
2088 if (!neededInt && !neededSSE)
2089 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2090
2091 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2092 // general purpose registers needed to pass type and num_fp to hold
2093 // the number of floating point registers needed.
2094
2095 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2096 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2097 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2098 //
2099 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2100 // register save space).
2101
2102 llvm::Value *InRegs = 0;
2103 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2104 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2105 if (neededInt) {
2106 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2107 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002108 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2109 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002110 }
2111
2112 if (neededSSE) {
2113 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2114 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2115 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002116 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2117 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002118 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2119 }
2120
2121 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2122 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2123 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2124 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2125
2126 // Emit code to load the value if it was passed in registers.
2127
2128 CGF.EmitBlock(InRegBlock);
2129
2130 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2131 // an offset of l->gp_offset and/or l->fp_offset. This may require
2132 // copying to a temporary location in case the parameter is passed
2133 // in different register classes or requires an alignment greater
2134 // than 8 for general purpose registers and 16 for XMM registers.
2135 //
2136 // FIXME: This really results in shameful code when we end up needing to
2137 // collect arguments from different places; often what should result in a
2138 // simple assembling of a structure from scattered addresses has many more
2139 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002140 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002141 llvm::Value *RegAddr =
2142 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2143 "reg_save_area");
2144 if (neededInt && neededSSE) {
2145 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002146 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002147 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002148 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2149 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002150 llvm::Type *TyLo = ST->getElementType(0);
2151 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002152 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002153 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002154 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2155 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002156 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2157 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002158 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2159 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002160 llvm::Value *V =
2161 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2162 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2163 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2164 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2165
Owen Andersona1cf15f2009-07-14 23:10:40 +00002166 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002167 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002168 } else if (neededInt) {
2169 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2170 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002171 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002172 } else if (neededSSE == 1) {
2173 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2174 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2175 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002176 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002177 assert(neededSSE == 2 && "Invalid number of needed registers!");
2178 // SSE registers are spaced 16 bytes apart in the register save
2179 // area, we need to collect the two eightbytes together.
2180 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002181 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foadef6de3d2011-07-11 09:56:20 +00002182 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002183 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002184 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002185 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002186 DoubleTy, NULL);
2187 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2188 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2189 DblPtrTy));
2190 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2191 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2192 DblPtrTy));
2193 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2194 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2195 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002196 }
2197
2198 // AMD64-ABI 3.5.7p5: Step 5. Set:
2199 // l->gp_offset = l->gp_offset + num_gp * 8
2200 // l->fp_offset = l->fp_offset + num_fp * 16.
2201 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002202 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002203 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2204 gp_offset_p);
2205 }
2206 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002207 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2209 fp_offset_p);
2210 }
2211 CGF.EmitBranch(ContBlock);
2212
2213 // Emit code to load the value if it was passed in memory.
2214
2215 CGF.EmitBlock(InMemBlock);
2216 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2217
2218 // Return the appropriate result.
2219
2220 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002221 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002222 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002223 ResAddr->addIncoming(RegAddr, InRegBlock);
2224 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002225 return ResAddr;
2226}
2227
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002228ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2229
2230 if (Ty->isVoidType())
2231 return ABIArgInfo::getIgnore();
2232
2233 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2234 Ty = EnumTy->getDecl()->getIntegerType();
2235
2236 uint64_t Size = getContext().getTypeSize(Ty);
2237
2238 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002239 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2240 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002241 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2242
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002243 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2244 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002245 getContext().getTargetInfo().getTriple().getOS()
2246 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002247 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2248 Size));
2249
2250 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2251 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2252 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002253 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002254 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2255 Size));
2256
2257 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2258 }
2259
2260 if (Ty->isPromotableIntegerType())
2261 return ABIArgInfo::getExtend();
2262
2263 return ABIArgInfo::getDirect();
2264}
2265
2266void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2267
2268 QualType RetTy = FI.getReturnType();
2269 FI.getReturnInfo() = classify(RetTy);
2270
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002271 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2272 it != ie; ++it)
2273 it->info = classify(it->type);
2274}
2275
Chris Lattnerf13721d2010-08-31 16:44:54 +00002276llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2277 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002278 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2279 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002280
Chris Lattnerf13721d2010-08-31 16:44:54 +00002281 CGBuilderTy &Builder = CGF.Builder;
2282 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2283 "ap");
2284 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2285 llvm::Type *PTy =
2286 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2287 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2288
2289 uint64_t Offset =
2290 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2291 llvm::Value *NextAddr =
2292 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2293 "ap.next");
2294 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2295
2296 return AddrTyped;
2297}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002298
John McCallec853ba2010-03-11 00:10:12 +00002299// PowerPC-32
2300
2301namespace {
2302class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2303public:
Chris Lattnerea044322010-07-29 02:01:43 +00002304 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002305
John McCallec853ba2010-03-11 00:10:12 +00002306 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2307 // This is recovered from gcc output.
2308 return 1; // r1 is the dedicated stack pointer
2309 }
2310
2311 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002312 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002313};
2314
2315}
2316
2317bool
2318PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2319 llvm::Value *Address) const {
2320 // This is calculated from the LLVM and GCC tables and verified
2321 // against gcc output. AFAIK all ABIs use the same encoding.
2322
2323 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2324 llvm::LLVMContext &Context = CGF.getLLVMContext();
2325
Chris Lattner2acc6e32011-07-18 04:24:23 +00002326 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallec853ba2010-03-11 00:10:12 +00002327 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2328 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2329 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2330
2331 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002332 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002333
2334 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002335 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002336
2337 // 64-76 are various 4-byte special-purpose registers:
2338 // 64: mq
2339 // 65: lr
2340 // 66: ctr
2341 // 67: ap
2342 // 68-75 cr0-7
2343 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002344 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002345
2346 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002347 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002348
2349 // 109: vrsave
2350 // 110: vscr
2351 // 111: spe_acc
2352 // 112: spefscr
2353 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002354 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002355
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002356 return false;
John McCallec853ba2010-03-11 00:10:12 +00002357}
2358
2359
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002360//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002361// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002362//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002363
2364namespace {
2365
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002366class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002367public:
2368 enum ABIKind {
2369 APCS = 0,
2370 AAPCS = 1,
2371 AAPCS_VFP
2372 };
2373
2374private:
2375 ABIKind Kind;
2376
2377public:
Chris Lattnerea044322010-07-29 02:01:43 +00002378 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002379
John McCall49e34be2011-08-30 01:42:09 +00002380 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002381 StringRef Env =
2382 getContext().getTargetInfo().getTriple().getEnvironmentName();
Chandler Carruthb43550b2012-01-10 19:47:42 +00002383 return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002384 }
2385
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002386private:
2387 ABIKind getABIKind() const { return Kind; }
2388
Chris Lattnera3c109b2010-07-29 02:16:43 +00002389 ABIArgInfo classifyReturnType(QualType RetTy) const;
2390 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002391
Chris Lattneree5dcd02010-07-29 02:31:05 +00002392 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002393
2394 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2395 CodeGenFunction &CGF) const;
2396};
2397
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002398class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2399public:
Chris Lattnerea044322010-07-29 02:01:43 +00002400 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2401 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002402
John McCall49e34be2011-08-30 01:42:09 +00002403 const ARMABIInfo &getABIInfo() const {
2404 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2405 }
2406
John McCall6374c332010-03-06 00:35:14 +00002407 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2408 return 13;
2409 }
Roman Divacky09345d12011-05-18 19:36:54 +00002410
Chris Lattner5f9e2722011-07-23 10:55:15 +00002411 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002412 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2413 }
2414
Roman Divacky09345d12011-05-18 19:36:54 +00002415 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2416 llvm::Value *Address) const {
2417 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2418 llvm::LLVMContext &Context = CGF.getLLVMContext();
2419
Chris Lattner2acc6e32011-07-18 04:24:23 +00002420 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divacky09345d12011-05-18 19:36:54 +00002421 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2422
2423 // 0-15 are the 16 integer registers.
2424 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2425
2426 return false;
2427 }
John McCall49e34be2011-08-30 01:42:09 +00002428
2429 unsigned getSizeOfUnwindException() const {
2430 if (getABIInfo().isEABI()) return 88;
2431 return TargetCodeGenInfo::getSizeOfUnwindException();
2432 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002433};
2434
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002435}
2436
Chris Lattneree5dcd02010-07-29 02:31:05 +00002437void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002438 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002439 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002440 it != ie; ++it)
2441 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002442
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002443 // Always honor user-specified calling convention.
2444 if (FI.getCallingConvention() != llvm::CallingConv::C)
2445 return;
2446
2447 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002448 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002449 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002450 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002451 else
2452 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002453
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002454 // If user did not ask for specific calling convention explicitly (e.g. via
2455 // pcs attribute), set effective calling convention if it's different than ABI
2456 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002457 switch (getABIKind()) {
2458 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002459 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2460 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002461 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002462 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002463 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2464 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002465 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002466 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002467 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2468 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002469 break;
2470 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002471}
2472
Bob Wilson194f06a2011-08-03 05:58:22 +00002473/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2474/// aggregate. If HAMembers is non-null, the number of base elements
2475/// contained in the type is returned through it; this is used for the
2476/// recursive calls that check aggregate component types.
2477static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2478 ASTContext &Context,
2479 uint64_t *HAMembers = 0) {
2480 uint64_t Members;
2481 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2482 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2483 return false;
2484 Members *= AT->getSize().getZExtValue();
2485 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2486 const RecordDecl *RD = RT->getDecl();
2487 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2488 return false;
2489 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2490 if (!CXXRD->isAggregate())
2491 return false;
2492 }
2493 Members = 0;
2494 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2495 i != e; ++i) {
2496 const FieldDecl *FD = *i;
2497 uint64_t FldMembers;
2498 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2499 return false;
2500 Members += FldMembers;
2501 }
2502 } else {
2503 Members = 1;
2504 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2505 Members = 2;
2506 Ty = CT->getElementType();
2507 }
2508
2509 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2510 // double, or 64-bit or 128-bit vectors.
2511 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2512 if (BT->getKind() != BuiltinType::Float &&
2513 BT->getKind() != BuiltinType::Double)
2514 return false;
2515 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2516 unsigned VecSize = Context.getTypeSize(VT);
2517 if (VecSize != 64 && VecSize != 128)
2518 return false;
2519 } else {
2520 return false;
2521 }
2522
2523 // The base type must be the same for all members. Vector types of the
2524 // same total size are treated as being equivalent here.
2525 const Type *TyPtr = Ty.getTypePtr();
2526 if (!Base)
2527 Base = TyPtr;
2528 if (Base != TyPtr &&
2529 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2530 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2531 return false;
2532 }
2533
2534 // Homogeneous Aggregates can have at most 4 members of the base type.
2535 if (HAMembers)
2536 *HAMembers = Members;
2537 return (Members <= 4);
2538}
2539
Chris Lattnera3c109b2010-07-29 02:16:43 +00002540ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002541 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002542 // Treat an enum type as its underlying type.
2543 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2544 Ty = EnumTy->getDecl()->getIntegerType();
2545
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002546 return (Ty->isPromotableIntegerType() ?
2547 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002548 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002549
Daniel Dunbar42025572009-09-14 21:54:03 +00002550 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002551 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002552 return ABIArgInfo::getIgnore();
2553
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002554 // Structures with either a non-trivial destructor or a non-trivial
2555 // copy constructor are always indirect.
2556 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2557 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2558
Bob Wilson194f06a2011-08-03 05:58:22 +00002559 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2560 // Homogeneous Aggregates need to be expanded.
2561 const Type *Base = 0;
2562 if (isHomogeneousAggregate(Ty, Base, getContext()))
2563 return ABIArgInfo::getExpand();
2564 }
2565
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002566 // Otherwise, pass by coercing to a structure of the appropriate size.
2567 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002568 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2569 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002570 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002571 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002572 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002573 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002574 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2575 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002576 } else {
2577 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2578 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002579 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002580
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002581 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002582 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002583 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002584}
2585
Chris Lattnera3c109b2010-07-29 02:16:43 +00002586static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002587 llvm::LLVMContext &VMContext) {
2588 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2589 // is called integer-like if its size is less than or equal to one word, and
2590 // the offset of each of its addressable sub-fields is zero.
2591
2592 uint64_t Size = Context.getTypeSize(Ty);
2593
2594 // Check that the type fits in a word.
2595 if (Size > 32)
2596 return false;
2597
2598 // FIXME: Handle vector types!
2599 if (Ty->isVectorType())
2600 return false;
2601
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002602 // Float types are never treated as "integer like".
2603 if (Ty->isRealFloatingType())
2604 return false;
2605
Daniel Dunbar98303b92009-09-13 08:03:58 +00002606 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002607 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002608 return true;
2609
Daniel Dunbar45815812010-02-01 23:31:26 +00002610 // Small complex integer types are "integer like".
2611 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2612 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002613
2614 // Single element and zero sized arrays should be allowed, by the definition
2615 // above, but they are not.
2616
2617 // Otherwise, it must be a record type.
2618 const RecordType *RT = Ty->getAs<RecordType>();
2619 if (!RT) return false;
2620
2621 // Ignore records with flexible arrays.
2622 const RecordDecl *RD = RT->getDecl();
2623 if (RD->hasFlexibleArrayMember())
2624 return false;
2625
2626 // Check that all sub-fields are at offset 0, and are themselves "integer
2627 // like".
2628 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2629
2630 bool HadField = false;
2631 unsigned idx = 0;
2632 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2633 i != e; ++i, ++idx) {
2634 const FieldDecl *FD = *i;
2635
Daniel Dunbar679855a2010-01-29 03:22:29 +00002636 // Bit-fields are not addressable, we only need to verify they are "integer
2637 // like". We still have to disallow a subsequent non-bitfield, for example:
2638 // struct { int : 0; int x }
2639 // is non-integer like according to gcc.
2640 if (FD->isBitField()) {
2641 if (!RD->isUnion())
2642 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002643
Daniel Dunbar679855a2010-01-29 03:22:29 +00002644 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2645 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002646
Daniel Dunbar679855a2010-01-29 03:22:29 +00002647 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002648 }
2649
Daniel Dunbar679855a2010-01-29 03:22:29 +00002650 // Check if this field is at offset 0.
2651 if (Layout.getFieldOffset(idx) != 0)
2652 return false;
2653
Daniel Dunbar98303b92009-09-13 08:03:58 +00002654 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2655 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002656
Daniel Dunbar679855a2010-01-29 03:22:29 +00002657 // Only allow at most one field in a structure. This doesn't match the
2658 // wording above, but follows gcc in situations with a field following an
2659 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002660 if (!RD->isUnion()) {
2661 if (HadField)
2662 return false;
2663
2664 HadField = true;
2665 }
2666 }
2667
2668 return true;
2669}
2670
Chris Lattnera3c109b2010-07-29 02:16:43 +00002671ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002672 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002673 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002674
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002675 // Large vector types should be returned via memory.
2676 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2677 return ABIArgInfo::getIndirect(0);
2678
John McCalld608cdb2010-08-22 10:59:02 +00002679 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002680 // Treat an enum type as its underlying type.
2681 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2682 RetTy = EnumTy->getDecl()->getIntegerType();
2683
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002684 return (RetTy->isPromotableIntegerType() ?
2685 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002686 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002687
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002688 // Structures with either a non-trivial destructor or a non-trivial
2689 // copy constructor are always indirect.
2690 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2691 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2692
Daniel Dunbar98303b92009-09-13 08:03:58 +00002693 // Are we following APCS?
2694 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002695 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002696 return ABIArgInfo::getIgnore();
2697
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002698 // Complex types are all returned as packed integers.
2699 //
2700 // FIXME: Consider using 2 x vector types if the back end handles them
2701 // correctly.
2702 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002703 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002704 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002705
Daniel Dunbar98303b92009-09-13 08:03:58 +00002706 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002707 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002708 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002709 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002710 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002711 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002712 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002713 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2714 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002715 }
2716
2717 // Otherwise return in memory.
2718 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002719 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002720
2721 // Otherwise this is an AAPCS variant.
2722
Chris Lattnera3c109b2010-07-29 02:16:43 +00002723 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002724 return ABIArgInfo::getIgnore();
2725
Bob Wilson3b694fa2011-11-02 04:51:36 +00002726 // Check for homogeneous aggregates with AAPCS-VFP.
2727 if (getABIKind() == AAPCS_VFP) {
2728 const Type *Base = 0;
2729 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2730 // Homogeneous Aggregates are returned directly.
2731 return ABIArgInfo::getDirect();
2732 }
2733
Daniel Dunbar98303b92009-09-13 08:03:58 +00002734 // Aggregates <= 4 bytes are returned in r0; other aggregates
2735 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002736 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002737 if (Size <= 32) {
2738 // Return in the smallest viable integer type.
2739 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002740 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002741 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002742 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2743 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002744 }
2745
Daniel Dunbar98303b92009-09-13 08:03:58 +00002746 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002747}
2748
2749llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002750 CodeGenFunction &CGF) const {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002751 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2752 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002753
2754 CGBuilderTy &Builder = CGF.Builder;
2755 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2756 "ap");
2757 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002758 // Handle address alignment for type alignment > 32 bits
2759 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2760 if (TyAlign > 4) {
2761 assert((TyAlign & (TyAlign - 1)) == 0 &&
2762 "Alignment is not power of 2!");
2763 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2764 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2765 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2766 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2767 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002768 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002769 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002770 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2771
2772 uint64_t Offset =
2773 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2774 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002775 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002776 "ap.next");
2777 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2778
2779 return AddrTyped;
2780}
2781
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002782//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002783// PTX ABI Implementation
2784//===----------------------------------------------------------------------===//
2785
2786namespace {
2787
2788class PTXABIInfo : public ABIInfo {
2789public:
2790 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2791
2792 ABIArgInfo classifyReturnType(QualType RetTy) const;
2793 ABIArgInfo classifyArgumentType(QualType Ty) const;
2794
2795 virtual void computeInfo(CGFunctionInfo &FI) const;
2796 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2797 CodeGenFunction &CFG) const;
2798};
2799
2800class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2801public:
2802 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2803 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002804
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00002805 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2806 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002807};
2808
2809ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2810 if (RetTy->isVoidType())
2811 return ABIArgInfo::getIgnore();
2812 if (isAggregateTypeForABI(RetTy))
2813 return ABIArgInfo::getIndirect(0);
2814 return ABIArgInfo::getDirect();
2815}
2816
2817ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2818 if (isAggregateTypeForABI(Ty))
2819 return ABIArgInfo::getIndirect(0);
2820
2821 return ABIArgInfo::getDirect();
2822}
2823
2824void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2825 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2826 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2827 it != ie; ++it)
2828 it->info = classifyArgumentType(it->type);
2829
2830 // Always honor user-specified calling convention.
2831 if (FI.getCallingConvention() != llvm::CallingConv::C)
2832 return;
2833
2834 // Calling convention as default by an ABI.
2835 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002836 const LangOptions &LangOpts = getContext().getLangOptions();
2837 if (LangOpts.OpenCL || LangOpts.CUDA) {
2838 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002839 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002840 } else {
2841 // If we are in standard C/C++ mode, use the triple to decide on the default
2842 StringRef Env =
2843 getContext().getTargetInfo().getTriple().getEnvironmentName();
2844 if (Env == "device")
2845 DefaultCC = llvm::CallingConv::PTX_Device;
2846 else
2847 DefaultCC = llvm::CallingConv::PTX_Kernel;
2848 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002849 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002850
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002851}
2852
2853llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2854 CodeGenFunction &CFG) const {
2855 llvm_unreachable("PTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002856}
2857
Justin Holewinski818eafb2011-10-05 17:58:44 +00002858void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2859 llvm::GlobalValue *GV,
2860 CodeGen::CodeGenModule &M) const{
2861 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2862 if (!FD) return;
2863
2864 llvm::Function *F = cast<llvm::Function>(GV);
2865
2866 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002867 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002868 // Use OpenCL function attributes to set proper calling conventions
2869 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002870 if (FD->hasAttr<OpenCLKernelAttr>()) {
2871 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002872 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002873 // And kernel functions are not subject to inlining
2874 F->addFnAttr(llvm::Attribute::NoInline);
2875 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002876 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002877
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002878 // Perform special handling in CUDA mode.
2879 if (M.getLangOptions().CUDA) {
2880 // CUDA __global__ functions get a kernel calling convention. Since
2881 // __global__ functions cannot be called from the device, we do not
2882 // need to set the noinline attribute.
2883 if (FD->getAttr<CUDAGlobalAttr>())
2884 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002885 }
2886}
2887
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002888}
2889
2890//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002891// MBlaze ABI Implementation
2892//===----------------------------------------------------------------------===//
2893
2894namespace {
2895
2896class MBlazeABIInfo : public ABIInfo {
2897public:
2898 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2899
2900 bool isPromotableIntegerType(QualType Ty) const;
2901
2902 ABIArgInfo classifyReturnType(QualType RetTy) const;
2903 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2904
2905 virtual void computeInfo(CGFunctionInfo &FI) const {
2906 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2907 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2908 it != ie; ++it)
2909 it->info = classifyArgumentType(it->type);
2910 }
2911
2912 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2913 CodeGenFunction &CGF) const;
2914};
2915
2916class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2917public:
2918 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2919 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2920 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2921 CodeGen::CodeGenModule &M) const;
2922};
2923
2924}
2925
2926bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2927 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2928 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2929 switch (BT->getKind()) {
2930 case BuiltinType::Bool:
2931 case BuiltinType::Char_S:
2932 case BuiltinType::Char_U:
2933 case BuiltinType::SChar:
2934 case BuiltinType::UChar:
2935 case BuiltinType::Short:
2936 case BuiltinType::UShort:
2937 return true;
2938 default:
2939 return false;
2940 }
2941 return false;
2942}
2943
2944llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2945 CodeGenFunction &CGF) const {
2946 // FIXME: Implement
2947 return 0;
2948}
2949
2950
2951ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2952 if (RetTy->isVoidType())
2953 return ABIArgInfo::getIgnore();
2954 if (isAggregateTypeForABI(RetTy))
2955 return ABIArgInfo::getIndirect(0);
2956
2957 return (isPromotableIntegerType(RetTy) ?
2958 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2959}
2960
2961ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2962 if (isAggregateTypeForABI(Ty))
2963 return ABIArgInfo::getIndirect(0);
2964
2965 return (isPromotableIntegerType(Ty) ?
2966 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2967}
2968
2969void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2970 llvm::GlobalValue *GV,
2971 CodeGen::CodeGenModule &M)
2972 const {
2973 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2974 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002975
Wesley Peck276fdf42010-12-19 19:57:51 +00002976 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2977 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2978 CC = llvm::CallingConv::MBLAZE_INTR;
2979 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2980 CC = llvm::CallingConv::MBLAZE_SVOL;
2981
2982 if (CC != llvm::CallingConv::C) {
2983 // Handle 'interrupt_handler' attribute:
2984 llvm::Function *F = cast<llvm::Function>(GV);
2985
2986 // Step 1: Set ISR calling convention.
2987 F->setCallingConv(CC);
2988
2989 // Step 2: Add attributes goodness.
2990 F->addFnAttr(llvm::Attribute::NoInline);
2991 }
2992
2993 // Step 3: Emit _interrupt_handler alias.
2994 if (CC == llvm::CallingConv::MBLAZE_INTR)
2995 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2996 "_interrupt_handler", GV, &M.getModule());
2997}
2998
2999
3000//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003001// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003002//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003003
3004namespace {
3005
3006class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3007public:
Chris Lattnerea044322010-07-29 02:01:43 +00003008 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3009 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003010 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3011 CodeGen::CodeGenModule &M) const;
3012};
3013
3014}
3015
3016void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3017 llvm::GlobalValue *GV,
3018 CodeGen::CodeGenModule &M) const {
3019 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3020 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3021 // Handle 'interrupt' attribute:
3022 llvm::Function *F = cast<llvm::Function>(GV);
3023
3024 // Step 1: Set ISR calling convention.
3025 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3026
3027 // Step 2: Add attributes goodness.
3028 F->addFnAttr(llvm::Attribute::NoInline);
3029
3030 // Step 3: Emit ISR vector alias.
3031 unsigned Num = attr->getNumber() + 0xffe0;
3032 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003033 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003034 GV, &M.getModule());
3035 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003036 }
3037}
3038
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003039//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003040// MIPS ABI Implementation. This works for both little-endian and
3041// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003042//===----------------------------------------------------------------------===//
3043
John McCallaeeb7012010-05-27 06:19:26 +00003044namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003045class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003046 bool IsO32;
Akira Hatanakab551dd32011-11-03 00:05:50 +00003047 unsigned MinABIStackAlignInBytes;
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003048 llvm::Type* HandleAggregates(QualType Ty) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003049 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003050 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003051public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003052 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3053 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003054
3055 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003056 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003057 virtual void computeInfo(CGFunctionInfo &FI) const;
3058 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3059 CodeGenFunction &CGF) const;
3060};
3061
John McCallaeeb7012010-05-27 06:19:26 +00003062class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003063 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003064public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003065 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3066 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3067 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003068
3069 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3070 return 29;
3071 }
3072
3073 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003074 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003075
3076 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003077 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003078 }
John McCallaeeb7012010-05-27 06:19:26 +00003079};
3080}
3081
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003082// In N32/64, an aligned double precision floating point field is passed in
3083// a register.
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003084llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty) const {
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003085 if (IsO32)
3086 return 0;
3087
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003088 if (Ty->isComplexType())
3089 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003090
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003091 const RecordType *RT = Ty->getAsStructureType();
3092
3093 if (!RT)
3094 return 0;
3095
3096 const RecordDecl *RD = RT->getDecl();
3097 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3098 uint64_t StructSize = getContext().getTypeSize(Ty);
3099 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3100
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003101 uint64_t LastOffset = 0;
3102 unsigned idx = 0;
3103 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003104 SmallVector<llvm::Type*, 8> ArgList;
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003105
3106 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3107 i != e; ++i, ++idx) {
3108 const QualType Ty = (*i)->getType();
3109 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3110
3111 if (!BT || BT->getKind() != BuiltinType::Double)
3112 continue;
3113
3114 uint64_t Offset = Layout.getFieldOffset(idx);
3115 if (Offset % 64) // Ignore doubles that are not aligned.
3116 continue;
3117
3118 // Add ((Offset - LastOffset) / 64) args of type i64.
3119 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3120 ArgList.push_back(I64);
3121
3122 // Add double type.
3123 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3124 LastOffset = Offset + 64;
3125 }
3126
3127 // This structure doesn't have an aligned double field.
3128 if (!LastOffset)
3129 return 0;
3130
3131 // Add ((StructSize - LastOffset) / 64) args of type i64.
3132 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3133 ArgList.push_back(I64);
3134
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003135 // If the size of the remainder is not zero, add one more integer type to
3136 // ArgList.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003137 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003138 if (R)
3139 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003140
3141 return llvm::StructType::get(getVMContext(), ArgList);
3142}
3143
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003144llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
3145 // Padding is inserted only for N32/64.
3146 if (IsO32)
3147 return 0;
3148
3149 assert(Align <= 16 && "Alignment larger than 16 not handled.");
3150 return (Align == 16 && Offset & 0xf) ?
3151 llvm::IntegerType::get(getVMContext(), 64) : 0;
3152}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003153
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003154ABIArgInfo
3155MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003156 uint64_t OrigOffset = Offset;
3157 uint64_t TySize =
3158 llvm::RoundUpToAlignment(getContext().getTypeSize(Ty), 64) / 8;
3159 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
3160 Offset = llvm::RoundUpToAlignment(Offset, std::max(Align, (uint64_t)8));
3161 Offset += TySize;
3162
Akira Hatanaka619e8872011-06-02 00:09:17 +00003163 if (isAggregateTypeForABI(Ty)) {
3164 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003165 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003166 return ABIArgInfo::getIgnore();
3167
Akira Hatanaka511949b2011-08-01 18:09:58 +00003168 // Records with non trivial destructors/constructors should not be passed
3169 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003170 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003171 Offset = OrigOffset + 8;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003172 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003173 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003174
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003175 // If we have reached here, aggregates are passed either indirectly via a
3176 // byval pointer or directly by coercing to another structure type. In the
3177 // latter case, padding is inserted if the offset of the aggregate is
3178 // unaligned.
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003179 llvm::Type *ResType = HandleAggregates(Ty);
Akira Hatanaka9659d592012-01-10 22:44:52 +00003180
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003181 if (!ResType)
3182 return ABIArgInfo::getIndirect(0);
3183
3184 return ABIArgInfo::getDirect(ResType, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003185 }
3186
3187 // Treat an enum type as its underlying type.
3188 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3189 Ty = EnumTy->getDecl()->getIntegerType();
3190
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003191 if (Ty->isPromotableIntegerType())
3192 return ABIArgInfo::getExtend();
3193
3194 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003195}
3196
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003197llvm::Type*
3198MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
3199 const RecordType *RT = RetTy->getAsStructureType();
3200 SmallVector<llvm::Type*, 2> RTList;
3201
3202 if (RT) {
3203 const RecordDecl *RD = RT->getDecl();
3204 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(), i;
3205
3206 for (i = b; (i != e) && (std::distance(b, i) < 2); ++i) {
3207 const BuiltinType *BT = (*i)->getType()->getAs<BuiltinType>();
3208
3209 if (!BT || !BT->isFloatingPoint())
3210 break;
3211
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003212 RTList.push_back(CGT.ConvertType((*i)->getType()));
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003213 }
3214
3215 if (i == e)
3216 return llvm::StructType::get(getVMContext(), RTList,
3217 RD->hasAttr<PackedAttr>());
3218
3219 RTList.clear();
3220 }
3221
3222 RTList.push_back(llvm::IntegerType::get(getVMContext(),
3223 std::min(Size, (uint64_t)64)));
3224 if (Size > 64)
3225 RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
3226
3227 return llvm::StructType::get(getVMContext(), RTList);
3228}
3229
Akira Hatanaka619e8872011-06-02 00:09:17 +00003230ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003231 uint64_t Size = getContext().getTypeSize(RetTy);
3232
3233 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003234 return ABIArgInfo::getIgnore();
3235
3236 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003237 if (Size <= 128) {
3238 if (RetTy->isAnyComplexType())
3239 return ABIArgInfo::getDirect();
3240
3241 if (!IsO32)
3242 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3243 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003244
3245 return ABIArgInfo::getIndirect(0);
3246 }
3247
3248 // Treat an enum type as its underlying type.
3249 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3250 RetTy = EnumTy->getDecl()->getIntegerType();
3251
3252 return (RetTy->isPromotableIntegerType() ?
3253 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3254}
3255
3256void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003257 ABIArgInfo &RetInfo = FI.getReturnInfo();
3258 RetInfo = classifyReturnType(FI.getReturnType());
3259
3260 // Check if a pointer to an aggregate is passed as a hidden argument.
3261 uint64_t Offset = RetInfo.isIndirect() ? 8 : 0;
3262
Akira Hatanaka619e8872011-06-02 00:09:17 +00003263 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3264 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003265 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003266}
3267
3268llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3269 CodeGenFunction &CGF) const {
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003270 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3271 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3272
3273 CGBuilderTy &Builder = CGF.Builder;
3274 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3275 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003276 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003277 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3278 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003279 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3280 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003281
3282 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003283 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3284 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3285 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3286 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003287 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3288 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3289 }
3290 else
3291 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3292
3293 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003294 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003295 uint64_t Offset =
3296 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3297 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003298 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003299 "ap.next");
3300 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3301
3302 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003303}
3304
John McCallaeeb7012010-05-27 06:19:26 +00003305bool
3306MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3307 llvm::Value *Address) const {
3308 // This information comes from gcc's implementation, which seems to
3309 // as canonical as it gets.
3310
3311 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3312 llvm::LLVMContext &Context = CGF.getLLVMContext();
3313
3314 // Everything on MIPS is 4 bytes. Double-precision FP registers
3315 // are aliased to pairs of single-precision FP registers.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003316 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallaeeb7012010-05-27 06:19:26 +00003317 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3318
3319 // 0-31 are the general purpose registers, $0 - $31.
3320 // 32-63 are the floating-point registers, $f0 - $f31.
3321 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3322 // 66 is the (notional, I think) register for signal-handler return.
3323 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3324
3325 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3326 // They are one bit wide and ignored here.
3327
3328 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3329 // (coprocessor 1 is the FP unit)
3330 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3331 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3332 // 176-181 are the DSP accumulator registers.
3333 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3334
3335 return false;
3336}
3337
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003338//===----------------------------------------------------------------------===//
3339// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3340// Currently subclassed only to implement custom OpenCL C function attribute
3341// handling.
3342//===----------------------------------------------------------------------===//
3343
3344namespace {
3345
3346class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3347public:
3348 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3349 : DefaultTargetCodeGenInfo(CGT) {}
3350
3351 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3352 CodeGen::CodeGenModule &M) const;
3353};
3354
3355void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3356 llvm::GlobalValue *GV,
3357 CodeGen::CodeGenModule &M) const {
3358 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3359 if (!FD) return;
3360
3361 llvm::Function *F = cast<llvm::Function>(GV);
3362
3363 if (M.getLangOptions().OpenCL) {
3364 if (FD->hasAttr<OpenCLKernelAttr>()) {
3365 // OpenCL C Kernel functions are not subject to inlining
3366 F->addFnAttr(llvm::Attribute::NoInline);
3367
3368 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3369
3370 // Convert the reqd_work_group_size() attributes to metadata.
3371 llvm::LLVMContext &Context = F->getContext();
3372 llvm::NamedMDNode *OpenCLMetadata =
3373 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3374
3375 SmallVector<llvm::Value*, 5> Operands;
3376 Operands.push_back(F);
3377
3378 Operands.push_back(llvm::Constant::getIntegerValue(
3379 llvm::Type::getInt32Ty(Context),
3380 llvm::APInt(
3381 32,
3382 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3383 Operands.push_back(llvm::Constant::getIntegerValue(
3384 llvm::Type::getInt32Ty(Context),
3385 llvm::APInt(
3386 32,
3387 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3388 Operands.push_back(llvm::Constant::getIntegerValue(
3389 llvm::Type::getInt32Ty(Context),
3390 llvm::APInt(
3391 32,
3392 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3393
3394 // Add a boolean constant operand for "required" (true) or "hint" (false)
3395 // for implementing the work_group_size_hint attr later. Currently
3396 // always true as the hint is not yet implemented.
3397 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3398
3399 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3400 }
3401 }
3402 }
3403}
3404
3405}
John McCallaeeb7012010-05-27 06:19:26 +00003406
Tony Linthicum96319392011-12-12 21:14:55 +00003407//===----------------------------------------------------------------------===//
3408// Hexagon ABI Implementation
3409//===----------------------------------------------------------------------===//
3410
3411namespace {
3412
3413class HexagonABIInfo : public ABIInfo {
3414
3415
3416public:
3417 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3418
3419private:
3420
3421 ABIArgInfo classifyReturnType(QualType RetTy) const;
3422 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3423
3424 virtual void computeInfo(CGFunctionInfo &FI) const;
3425
3426 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3427 CodeGenFunction &CGF) const;
3428};
3429
3430class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3431public:
3432 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3433 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3434
3435 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3436 return 29;
3437 }
3438};
3439
3440}
3441
3442void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3443 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3444 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3445 it != ie; ++it)
3446 it->info = classifyArgumentType(it->type);
3447}
3448
3449ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3450 if (!isAggregateTypeForABI(Ty)) {
3451 // Treat an enum type as its underlying type.
3452 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3453 Ty = EnumTy->getDecl()->getIntegerType();
3454
3455 return (Ty->isPromotableIntegerType() ?
3456 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3457 }
3458
3459 // Ignore empty records.
3460 if (isEmptyRecord(getContext(), Ty, true))
3461 return ABIArgInfo::getIgnore();
3462
3463 // Structures with either a non-trivial destructor or a non-trivial
3464 // copy constructor are always indirect.
3465 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3466 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3467
3468 uint64_t Size = getContext().getTypeSize(Ty);
3469 if (Size > 64)
3470 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3471 // Pass in the smallest viable integer type.
3472 else if (Size > 32)
3473 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3474 else if (Size > 16)
3475 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3476 else if (Size > 8)
3477 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3478 else
3479 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3480}
3481
3482ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3483 if (RetTy->isVoidType())
3484 return ABIArgInfo::getIgnore();
3485
3486 // Large vector types should be returned via memory.
3487 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3488 return ABIArgInfo::getIndirect(0);
3489
3490 if (!isAggregateTypeForABI(RetTy)) {
3491 // Treat an enum type as its underlying type.
3492 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3493 RetTy = EnumTy->getDecl()->getIntegerType();
3494
3495 return (RetTy->isPromotableIntegerType() ?
3496 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3497 }
3498
3499 // Structures with either a non-trivial destructor or a non-trivial
3500 // copy constructor are always indirect.
3501 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3502 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3503
3504 if (isEmptyRecord(getContext(), RetTy, true))
3505 return ABIArgInfo::getIgnore();
3506
3507 // Aggregates <= 8 bytes are returned in r0; other aggregates
3508 // are returned indirectly.
3509 uint64_t Size = getContext().getTypeSize(RetTy);
3510 if (Size <= 64) {
3511 // Return in the smallest viable integer type.
3512 if (Size <= 8)
3513 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3514 if (Size <= 16)
3515 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3516 if (Size <= 32)
3517 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3518 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3519 }
3520
3521 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3522}
3523
3524llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3525 CodeGenFunction &CGF) const {
3526 // FIXME: Need to handle alignment
3527 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3528 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3529
3530 CGBuilderTy &Builder = CGF.Builder;
3531 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3532 "ap");
3533 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3534 llvm::Type *PTy =
3535 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3536 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3537
3538 uint64_t Offset =
3539 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3540 llvm::Value *NextAddr =
3541 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3542 "ap.next");
3543 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3544
3545 return AddrTyped;
3546}
3547
3548
Chris Lattnerea044322010-07-29 02:01:43 +00003549const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003550 if (TheTargetCodeGenInfo)
3551 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003552
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003553 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003554 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003555 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003556 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003557
John McCallaeeb7012010-05-27 06:19:26 +00003558 case llvm::Triple::mips:
3559 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003560 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003561
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003562 case llvm::Triple::mips64:
3563 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003564 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003565
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003566 case llvm::Triple::arm:
3567 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003568 {
3569 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003570
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003571 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003572 Kind = ARMABIInfo::APCS;
3573 else if (CodeGenOpts.FloatABI == "hard")
3574 Kind = ARMABIInfo::AAPCS_VFP;
3575
3576 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3577 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003578
John McCallec853ba2010-03-11 00:10:12 +00003579 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003580 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003581
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003582 case llvm::Triple::ptx32:
3583 case llvm::Triple::ptx64:
3584 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3585
Wesley Peck276fdf42010-12-19 19:57:51 +00003586 case llvm::Triple::mblaze:
3587 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3588
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003589 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003590 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003591
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003592 case llvm::Triple::tce:
3593 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3594
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003595 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003596 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003597
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003598 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003599 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003600 new X86_32TargetCodeGenInfo(
3601 Types, true, true, DisableMMX, false));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003602
3603 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003604 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003605 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003606 case llvm::Triple::AuroraUX:
3607 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003608 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003609 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003610 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003611 new X86_32TargetCodeGenInfo(
3612 Types, false, true, DisableMMX, false));
3613
3614 case llvm::Triple::Win32:
3615 return *(TheTargetCodeGenInfo =
3616 new X86_32TargetCodeGenInfo(
3617 Types, false, true, DisableMMX, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003618
3619 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003620 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003621 new X86_32TargetCodeGenInfo(
3622 Types, false, false, DisableMMX, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003623 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003624 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003625
Eli Friedmanee1ad992011-12-02 00:11:43 +00003626 case llvm::Triple::x86_64: {
3627 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3628
Chris Lattnerf13721d2010-08-31 16:44:54 +00003629 switch (Triple.getOS()) {
3630 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003631 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003632 case llvm::Triple::Cygwin:
3633 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3634 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00003635 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3636 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00003637 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003638 }
Tony Linthicum96319392011-12-12 21:14:55 +00003639 case llvm::Triple::hexagon:
3640 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00003641 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003642}