blob: cae8f5fb1a0f3e5c0f9b2d3de1d4aa7dc686dad2 [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 Lattner8b418682012-02-07 00:39:47 +0000744 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000745
746 CGBuilderTy &Builder = CGF.Builder;
747 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
748 "ap");
749 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000750
751 // Compute if the address needs to be aligned
752 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
753 Align = getTypeStackAlignInBytes(Ty, Align);
754 Align = std::max(Align, 4U);
755 if (Align > 4) {
756 // addr = (addr + align - 1) & -align;
757 llvm::Value *Offset =
758 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
759 Addr = CGF.Builder.CreateGEP(Addr, Offset);
760 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
761 CGF.Int32Ty);
762 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
763 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
764 Addr->getType(),
765 "ap.cur.aligned");
766 }
767
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000768 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000769 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000770 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
771
772 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000773 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000774 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000775 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000776 "ap.next");
777 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
778
779 return AddrTyped;
780}
781
Charles Davis74f72932010-02-13 15:54:06 +0000782void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
783 llvm::GlobalValue *GV,
784 CodeGen::CodeGenModule &CGM) const {
785 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
786 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
787 // Get the LLVM function.
788 llvm::Function *Fn = cast<llvm::Function>(GV);
789
790 // Now add the 'alignstack' attribute with a value of 16.
791 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
792 }
793 }
794}
795
John McCall6374c332010-03-06 00:35:14 +0000796bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
797 CodeGen::CodeGenFunction &CGF,
798 llvm::Value *Address) const {
799 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000800
Chris Lattner8b418682012-02-07 00:39:47 +0000801 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000802
John McCall6374c332010-03-06 00:35:14 +0000803 // 0-7 are the eight integer registers; the order is different
804 // on Darwin (for EH), but the range is the same.
805 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000806 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000807
808 if (CGF.CGM.isTargetDarwin()) {
809 // 12-16 are st(0..4). Not sure why we stop at 4.
810 // These have size 16, which is sizeof(long double) on
811 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000812 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000813 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000814
John McCall6374c332010-03-06 00:35:14 +0000815 } else {
816 // 9 is %eflags, which doesn't get a size on Darwin for some
817 // reason.
818 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
819
820 // 11-16 are st(0..5). Not sure why we stop at 5.
821 // These have size 12, which is sizeof(long double) on
822 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000823 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000824 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
825 }
John McCall6374c332010-03-06 00:35:14 +0000826
827 return false;
828}
829
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000830//===----------------------------------------------------------------------===//
831// X86-64 ABI Implementation
832//===----------------------------------------------------------------------===//
833
834
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000835namespace {
836/// X86_64ABIInfo - The X86_64 ABI information.
837class X86_64ABIInfo : public ABIInfo {
838 enum Class {
839 Integer = 0,
840 SSE,
841 SSEUp,
842 X87,
843 X87Up,
844 ComplexX87,
845 NoClass,
846 Memory
847 };
848
849 /// merge - Implement the X86_64 ABI merging algorithm.
850 ///
851 /// Merge an accumulating classification \arg Accum with a field
852 /// classification \arg Field.
853 ///
854 /// \param Accum - The accumulating classification. This should
855 /// always be either NoClass or the result of a previous merge
856 /// call. In addition, this should never be Memory (the caller
857 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000858 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000859
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000860 /// postMerge - Implement the X86_64 ABI post merging algorithm.
861 ///
862 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
863 /// final MEMORY or SSE classes when necessary.
864 ///
865 /// \param AggregateSize - The size of the current aggregate in
866 /// the classification process.
867 ///
868 /// \param Lo - The classification for the parts of the type
869 /// residing in the low word of the containing object.
870 ///
871 /// \param Hi - The classification for the parts of the type
872 /// residing in the higher words of the containing object.
873 ///
874 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
875
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000876 /// classify - Determine the x86_64 register classes in which the
877 /// given type T should be passed.
878 ///
879 /// \param Lo - The classification for the parts of the type
880 /// residing in the low word of the containing object.
881 ///
882 /// \param Hi - The classification for the parts of the type
883 /// residing in the high word of the containing object.
884 ///
885 /// \param OffsetBase - The bit offset of this type in the
886 /// containing object. Some parameters are classified different
887 /// depending on whether they straddle an eightbyte boundary.
888 ///
889 /// If a word is unused its result will be NoClass; if a type should
890 /// be passed in Memory then at least the classification of \arg Lo
891 /// will be Memory.
892 ///
893 /// The \arg Lo class will be NoClass iff the argument is ignored.
894 ///
895 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
896 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000897 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000898
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +0000899 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000900 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
901 unsigned IROffset, QualType SourceTy,
902 unsigned SourceOffset) const;
903 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
904 unsigned IROffset, QualType SourceTy,
905 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000906
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000907 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000908 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000909 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000910
911 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000912 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000913 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000914
Chris Lattnera3c109b2010-07-29 02:16:43 +0000915 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000916
Bill Wendlingbb465d72010-10-18 03:41:31 +0000917 ABIArgInfo classifyArgumentType(QualType Ty,
918 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +0000919 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000920
Eli Friedmanee1ad992011-12-02 00:11:43 +0000921 bool IsIllegalVectorType(QualType Ty) const;
922
John McCall67a57732011-04-21 01:20:55 +0000923 /// The 0.98 ABI revision clarified a lot of ambiguities,
924 /// unfortunately in ways that were not always consistent with
925 /// certain previous compilers. In particular, platforms which
926 /// required strict binary compatibility with older versions of GCC
927 /// may need to exempt themselves.
928 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000929 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +0000930 }
931
Eli Friedmanee1ad992011-12-02 00:11:43 +0000932 bool HasAVX;
933
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000934public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000935 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
936 ABIInfo(CGT), HasAVX(hasavx) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000937
Chris Lattneree5dcd02010-07-29 02:31:05 +0000938 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000939
940 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
941 CodeGenFunction &CGF) const;
942};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000943
Chris Lattnerf13721d2010-08-31 16:44:54 +0000944/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000945class WinX86_64ABIInfo : public ABIInfo {
946
947 ABIArgInfo classify(QualType Ty) const;
948
Chris Lattnerf13721d2010-08-31 16:44:54 +0000949public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +0000950 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
951
952 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +0000953
954 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
955 CodeGenFunction &CGF) const;
956};
957
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000958class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
959public:
Eli Friedmanee1ad992011-12-02 00:11:43 +0000960 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
961 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +0000962
963 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
964 return 7;
965 }
966
967 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
968 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000969 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000970
John McCallaeeb7012010-05-27 06:19:26 +0000971 // 0-15 are the 16 integer registers.
972 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +0000973 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000974 return false;
975 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000976
Jay Foadef6de3d2011-07-11 09:56:20 +0000977 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000978 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000979 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000980 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
981 }
982
Eli Friedman3ed79032011-12-01 04:53:19 +0000983 bool isNoProtoCallVariadic(const CodeGen::CGFunctionInfo &FI) const {
John McCall01f151e2011-09-21 08:08:30 +0000984 // The default CC on x86-64 sets %al to the number of SSA
985 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +0000986 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +0000987 // that when AVX types are involved: the ABI explicitly states it is
988 // undefined, and it doesn't work in practice because of how the ABI
989 // defines varargs anyway.
Eli Friedman3ed79032011-12-01 04:53:19 +0000990 if (FI.getCallingConvention() == llvm::CallingConv::C) {
991 bool HasAVXType = false;
992 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
993 ie = FI.arg_end();
994 it != ie; ++it) {
995 if (it->info.isDirect()) {
996 llvm::Type *Ty = it->info.getCoerceToType();
997 if (llvm::VectorType *VTy = dyn_cast_or_null<llvm::VectorType>(Ty)) {
998 if (VTy->getBitWidth() > 128) {
999 HasAVXType = true;
1000 break;
1001 }
1002 }
1003 }
1004 }
1005 if (!HasAVXType)
1006 return true;
1007 }
John McCall01f151e2011-09-21 08:08:30 +00001008
Eli Friedman3ed79032011-12-01 04:53:19 +00001009 return TargetCodeGenInfo::isNoProtoCallVariadic(FI);
John McCall01f151e2011-09-21 08:08:30 +00001010 }
1011
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001012};
1013
Chris Lattnerf13721d2010-08-31 16:44:54 +00001014class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1015public:
1016 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1017 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1018
1019 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1020 return 7;
1021 }
1022
1023 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1024 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001025 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001026
Chris Lattnerf13721d2010-08-31 16:44:54 +00001027 // 0-15 are the 16 integer registers.
1028 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001029 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001030 return false;
1031 }
1032};
1033
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001034}
1035
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001036void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1037 Class &Hi) const {
1038 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1039 //
1040 // (a) If one of the classes is Memory, the whole argument is passed in
1041 // memory.
1042 //
1043 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1044 // memory.
1045 //
1046 // (c) If the size of the aggregate exceeds two eightbytes and the first
1047 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1048 // argument is passed in memory. NOTE: This is necessary to keep the
1049 // ABI working for processors that don't support the __m256 type.
1050 //
1051 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1052 //
1053 // Some of these are enforced by the merging logic. Others can arise
1054 // only with unions; for example:
1055 // union { _Complex double; unsigned; }
1056 //
1057 // Note that clauses (b) and (c) were added in 0.98.
1058 //
1059 if (Hi == Memory)
1060 Lo = Memory;
1061 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1062 Lo = Memory;
1063 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1064 Lo = Memory;
1065 if (Hi == SSEUp && Lo != SSE)
1066 Hi = SSE;
1067}
1068
Chris Lattner1090a9b2010-06-28 21:43:59 +00001069X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001070 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1071 // classified recursively so that always two fields are
1072 // considered. The resulting class is calculated according to
1073 // the classes of the fields in the eightbyte:
1074 //
1075 // (a) If both classes are equal, this is the resulting class.
1076 //
1077 // (b) If one of the classes is NO_CLASS, the resulting class is
1078 // the other class.
1079 //
1080 // (c) If one of the classes is MEMORY, the result is the MEMORY
1081 // class.
1082 //
1083 // (d) If one of the classes is INTEGER, the result is the
1084 // INTEGER.
1085 //
1086 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1087 // MEMORY is used as class.
1088 //
1089 // (f) Otherwise class SSE is used.
1090
1091 // Accum should never be memory (we should have returned) or
1092 // ComplexX87 (because this cannot be passed in a structure).
1093 assert((Accum != Memory && Accum != ComplexX87) &&
1094 "Invalid accumulated classification during merge.");
1095 if (Accum == Field || Field == NoClass)
1096 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001097 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001098 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001099 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001101 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001102 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001103 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1104 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001105 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001106 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001107}
1108
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001109void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001110 Class &Lo, Class &Hi) const {
1111 // FIXME: This code can be simplified by introducing a simple value class for
1112 // Class pairs with appropriate constructor methods for the various
1113 // situations.
1114
1115 // FIXME: Some of the split computations are wrong; unaligned vectors
1116 // shouldn't be passed in registers for example, so there is no chance they
1117 // can straddle an eightbyte. Verify & simplify.
1118
1119 Lo = Hi = NoClass;
1120
1121 Class &Current = OffsetBase < 64 ? Lo : Hi;
1122 Current = Memory;
1123
John McCall183700f2009-09-21 23:43:11 +00001124 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001125 BuiltinType::Kind k = BT->getKind();
1126
1127 if (k == BuiltinType::Void) {
1128 Current = NoClass;
1129 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1130 Lo = Integer;
1131 Hi = Integer;
1132 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1133 Current = Integer;
1134 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1135 Current = SSE;
1136 } else if (k == BuiltinType::LongDouble) {
1137 Lo = X87;
1138 Hi = X87Up;
1139 }
1140 // FIXME: _Decimal32 and _Decimal64 are SSE.
1141 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001142 return;
1143 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001144
Chris Lattner1090a9b2010-06-28 21:43:59 +00001145 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001146 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001147 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001148 return;
1149 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001150
Chris Lattner1090a9b2010-06-28 21:43:59 +00001151 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001152 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001153 return;
1154 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001155
Chris Lattner1090a9b2010-06-28 21:43:59 +00001156 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001157 if (Ty->isMemberFunctionPointerType())
1158 Lo = Hi = Integer;
1159 else
1160 Current = Integer;
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 (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001165 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001166 if (Size == 32) {
1167 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1168 // float> as integer.
1169 Current = Integer;
1170
1171 // If this type crosses an eightbyte boundary, it should be
1172 // split.
1173 uint64_t EB_Real = (OffsetBase) / 64;
1174 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1175 if (EB_Real != EB_Imag)
1176 Hi = Lo;
1177 } else if (Size == 64) {
1178 // gcc passes <1 x double> in memory. :(
1179 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1180 return;
1181
1182 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001183 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001184 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1185 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1186 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001187 Current = Integer;
1188 else
1189 Current = SSE;
1190
1191 // If this type crosses an eightbyte boundary, it should be
1192 // split.
1193 if (OffsetBase && OffsetBase != 64)
1194 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001195 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001196 // Arguments of 256-bits are split into four eightbyte chunks. The
1197 // least significant one belongs to class SSE and all the others to class
1198 // SSEUP. The original Lo and Hi design considers that types can't be
1199 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1200 // This design isn't correct for 256-bits, but since there're no cases
1201 // where the upper parts would need to be inspected, avoid adding
1202 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001203 Lo = SSE;
1204 Hi = SSEUp;
1205 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001206 return;
1207 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001208
Chris Lattner1090a9b2010-06-28 21:43:59 +00001209 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001210 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001211
Chris Lattnerea044322010-07-29 02:01:43 +00001212 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001213 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001214 if (Size <= 64)
1215 Current = Integer;
1216 else if (Size <= 128)
1217 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001218 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001219 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001220 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001221 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001222 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001223 Current = ComplexX87;
1224
1225 // If this complex type crosses an eightbyte boundary then it
1226 // should be split.
1227 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001228 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001229 if (Hi == NoClass && EB_Real != EB_Imag)
1230 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001231
Chris Lattner1090a9b2010-06-28 21:43:59 +00001232 return;
1233 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001234
Chris Lattnerea044322010-07-29 02:01:43 +00001235 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236 // Arrays are treated like structures.
1237
Chris Lattnerea044322010-07-29 02:01:43 +00001238 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001239
1240 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001241 // than four eightbytes, ..., it has class MEMORY.
1242 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001243 return;
1244
1245 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1246 // fields, it has class MEMORY.
1247 //
1248 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001249 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001250 return;
1251
1252 // Otherwise implement simplified merge. We could be smarter about
1253 // this, but it isn't worth it and would be harder to verify.
1254 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001255 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001256 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001257
1258 // The only case a 256-bit wide vector could be used is when the array
1259 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1260 // to work for sizes wider than 128, early check and fallback to memory.
1261 if (Size > 128 && EltSize != 256)
1262 return;
1263
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001264 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1265 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001266 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001267 Lo = merge(Lo, FieldLo);
1268 Hi = merge(Hi, FieldHi);
1269 if (Lo == Memory || Hi == Memory)
1270 break;
1271 }
1272
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001273 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001274 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001275 return;
1276 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001277
Chris Lattner1090a9b2010-06-28 21:43:59 +00001278 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001279 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001280
1281 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001282 // than four eightbytes, ..., it has class MEMORY.
1283 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001284 return;
1285
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001286 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1287 // copy constructor or a non-trivial destructor, it is passed by invisible
1288 // reference.
1289 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1290 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001291
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001292 const RecordDecl *RD = RT->getDecl();
1293
1294 // Assume variable sized types are passed in memory.
1295 if (RD->hasFlexibleArrayMember())
1296 return;
1297
Chris Lattnerea044322010-07-29 02:01:43 +00001298 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001299
1300 // Reset Lo class, this will be recomputed.
1301 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001302
1303 // If this is a C++ record, classify the bases first.
1304 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1305 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1306 e = CXXRD->bases_end(); i != e; ++i) {
1307 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1308 "Unexpected base class!");
1309 const CXXRecordDecl *Base =
1310 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1311
1312 // Classify this field.
1313 //
1314 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1315 // single eightbyte, each is classified separately. Each eightbyte gets
1316 // initialized to class NO_CLASS.
1317 Class FieldLo, FieldHi;
Anders Carlssona14f5972010-10-31 23:22:37 +00001318 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001319 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001320 Lo = merge(Lo, FieldLo);
1321 Hi = merge(Hi, FieldHi);
1322 if (Lo == Memory || Hi == Memory)
1323 break;
1324 }
1325 }
1326
1327 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001328 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001329 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001330 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001331 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1332 bool BitField = i->isBitField();
1333
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001334 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1335 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001336 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001337 // The only case a 256-bit wide vector could be used is when the struct
1338 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1339 // to work for sizes wider than 128, early check and fallback to memory.
1340 //
1341 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1342 Lo = Memory;
1343 return;
1344 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001345 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001346 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347 Lo = Memory;
1348 return;
1349 }
1350
1351 // Classify this field.
1352 //
1353 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1354 // exceeds a single eightbyte, each is classified
1355 // separately. Each eightbyte gets initialized to class
1356 // NO_CLASS.
1357 Class FieldLo, FieldHi;
1358
1359 // Bit-fields require special handling, they do not force the
1360 // structure to be passed in memory even if unaligned, and
1361 // therefore they can straddle an eightbyte.
1362 if (BitField) {
1363 // Ignore padding bit-fields.
1364 if (i->isUnnamedBitfield())
1365 continue;
1366
1367 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001368 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001369
1370 uint64_t EB_Lo = Offset / 64;
1371 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1372 FieldLo = FieldHi = NoClass;
1373 if (EB_Lo) {
1374 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1375 FieldLo = NoClass;
1376 FieldHi = Integer;
1377 } else {
1378 FieldLo = Integer;
1379 FieldHi = EB_Hi ? Integer : NoClass;
1380 }
1381 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001382 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001383 Lo = merge(Lo, FieldLo);
1384 Hi = merge(Hi, FieldHi);
1385 if (Lo == Memory || Hi == Memory)
1386 break;
1387 }
1388
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001389 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001390 }
1391}
1392
Chris Lattner9c254f02010-06-29 06:01:59 +00001393ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001394 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1395 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001396 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001397 // Treat an enum type as its underlying type.
1398 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1399 Ty = EnumTy->getDecl()->getIntegerType();
1400
1401 return (Ty->isPromotableIntegerType() ?
1402 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1403 }
1404
1405 return ABIArgInfo::getIndirect(0);
1406}
1407
Eli Friedmanee1ad992011-12-02 00:11:43 +00001408bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1409 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1410 uint64_t Size = getContext().getTypeSize(VecTy);
1411 unsigned LargestVector = HasAVX ? 256 : 128;
1412 if (Size <= 64 || Size > LargestVector)
1413 return true;
1414 }
1415
1416 return false;
1417}
1418
Chris Lattner9c254f02010-06-29 06:01:59 +00001419ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001420 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1421 // place naturally.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001422 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001423 // Treat an enum type as its underlying type.
1424 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1425 Ty = EnumTy->getDecl()->getIntegerType();
1426
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001427 return (Ty->isPromotableIntegerType() ?
1428 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001429 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001430
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001431 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1432 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001433
Chris Lattner855d2272011-05-22 23:21:23 +00001434 // Compute the byval alignment. We specify the alignment of the byval in all
1435 // cases so that the mid-level optimizer knows the alignment of the byval.
1436 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1437 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001438}
1439
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001440/// GetByteVectorType - The ABI specifies that a value should be passed in an
1441/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001442/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001443llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001444 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001445
Chris Lattner15842bd2010-07-29 05:02:29 +00001446 // Wrapper structs that just contain vectors are passed just like vectors,
1447 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001448 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001449 while (STy && STy->getNumElements() == 1) {
1450 IRType = STy->getElementType(0);
1451 STy = dyn_cast<llvm::StructType>(IRType);
1452 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001453
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001454 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001455 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1456 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001457 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001458 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001459 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1460 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1461 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1462 EltTy->isIntegerTy(128)))
1463 return VT;
1464 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001465
Chris Lattner0f408f52010-07-29 04:56:46 +00001466 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1467}
1468
Chris Lattnere2962be2010-07-29 07:30:00 +00001469/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1470/// is known to either be off the end of the specified type or being in
1471/// alignment padding. The user type specified is known to be at most 128 bits
1472/// in size, and have passed through X86_64ABIInfo::classify with a successful
1473/// classification that put one of the two halves in the INTEGER class.
1474///
1475/// It is conservatively correct to return false.
1476static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1477 unsigned EndBit, ASTContext &Context) {
1478 // If the bytes being queried are off the end of the type, there is no user
1479 // data hiding here. This handles analysis of builtins, vectors and other
1480 // types that don't contain interesting padding.
1481 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1482 if (TySize <= StartBit)
1483 return true;
1484
Chris Lattner021c3a32010-07-29 07:43:55 +00001485 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1486 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1487 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1488
1489 // Check each element to see if the element overlaps with the queried range.
1490 for (unsigned i = 0; i != NumElts; ++i) {
1491 // If the element is after the span we care about, then we're done..
1492 unsigned EltOffset = i*EltSize;
1493 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001494
Chris Lattner021c3a32010-07-29 07:43:55 +00001495 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1496 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1497 EndBit-EltOffset, Context))
1498 return false;
1499 }
1500 // If it overlaps no elements, then it is safe to process as padding.
1501 return true;
1502 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001503
Chris Lattnere2962be2010-07-29 07:30:00 +00001504 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1505 const RecordDecl *RD = RT->getDecl();
1506 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001507
Chris Lattnere2962be2010-07-29 07:30:00 +00001508 // If this is a C++ record, check the bases first.
1509 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1510 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1511 e = CXXRD->bases_end(); i != e; ++i) {
1512 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1513 "Unexpected base class!");
1514 const CXXRecordDecl *Base =
1515 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001516
Chris Lattnere2962be2010-07-29 07:30:00 +00001517 // If the base is after the span we care about, ignore it.
Anders Carlssona14f5972010-10-31 23:22:37 +00001518 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnere2962be2010-07-29 07:30:00 +00001519 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001520
Chris Lattnere2962be2010-07-29 07:30:00 +00001521 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1522 if (!BitsContainNoUserData(i->getType(), BaseStart,
1523 EndBit-BaseOffset, Context))
1524 return false;
1525 }
1526 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001527
Chris Lattnere2962be2010-07-29 07:30:00 +00001528 // Verify that no field has data that overlaps the region of interest. Yes
1529 // this could be sped up a lot by being smarter about queried fields,
1530 // however we're only looking at structs up to 16 bytes, so we don't care
1531 // much.
1532 unsigned idx = 0;
1533 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1534 i != e; ++i, ++idx) {
1535 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001536
Chris Lattnere2962be2010-07-29 07:30:00 +00001537 // If we found a field after the region we care about, then we're done.
1538 if (FieldOffset >= EndBit) break;
1539
1540 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1541 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1542 Context))
1543 return false;
1544 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001545
Chris Lattnere2962be2010-07-29 07:30:00 +00001546 // If nothing in this record overlapped the area of interest, then we're
1547 // clean.
1548 return true;
1549 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001550
Chris Lattnere2962be2010-07-29 07:30:00 +00001551 return false;
1552}
1553
Chris Lattner0b362002010-07-29 18:39:32 +00001554/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1555/// float member at the specified offset. For example, {int,{float}} has a
1556/// float at offset 4. It is conservatively correct for this routine to return
1557/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001558static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0b362002010-07-29 18:39:32 +00001559 const llvm::TargetData &TD) {
1560 // Base case if we find a float.
1561 if (IROffset == 0 && IRType->isFloatTy())
1562 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001563
Chris Lattner0b362002010-07-29 18:39:32 +00001564 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001565 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001566 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1567 unsigned Elt = SL->getElementContainingOffset(IROffset);
1568 IROffset -= SL->getElementOffset(Elt);
1569 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1570 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001571
Chris Lattner0b362002010-07-29 18:39:32 +00001572 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001573 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1574 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001575 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1576 IROffset -= IROffset/EltSize*EltSize;
1577 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1578 }
1579
1580 return false;
1581}
1582
Chris Lattnerf47c9442010-07-29 18:13:09 +00001583
1584/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1585/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001586llvm::Type *X86_64ABIInfo::
1587GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001588 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001589 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001590 // pass as float if the last 4 bytes is just padding. This happens for
1591 // structs that contain 3 floats.
1592 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1593 SourceOffset*8+64, getContext()))
1594 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001595
Chris Lattner0b362002010-07-29 18:39:32 +00001596 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1597 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1598 // case.
1599 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001600 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1601 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001602
Chris Lattnerf47c9442010-07-29 18:13:09 +00001603 return llvm::Type::getDoubleTy(getVMContext());
1604}
1605
1606
Chris Lattner0d2656d2010-07-29 17:40:35 +00001607/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1608/// an 8-byte GPR. This means that we either have a scalar or we are talking
1609/// about the high or low part of an up-to-16-byte struct. This routine picks
1610/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001611/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1612/// etc).
1613///
1614/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1615/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1616/// the 8-byte value references. PrefType may be null.
1617///
1618/// SourceTy is the source level type for the entire argument. SourceOffset is
1619/// an offset into this that we're processing (which is always either 0 or 8).
1620///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001621llvm::Type *X86_64ABIInfo::
1622GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001623 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001624 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1625 // returning an 8-byte unit starting with it. See if we can safely use it.
1626 if (IROffset == 0) {
1627 // Pointers and int64's always fill the 8-byte unit.
1628 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1629 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001630
Chris Lattnere2962be2010-07-29 07:30:00 +00001631 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1632 // goodness in the source type is just tail padding. This is allowed to
1633 // kick in for struct {double,int} on the int, but not on
1634 // struct{double,int,int} because we wouldn't return the second int. We
1635 // have to do this analysis on the source type because we can't depend on
1636 // unions being lowered a specific way etc.
1637 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1638 IRType->isIntegerTy(32)) {
1639 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001640
Chris Lattnere2962be2010-07-29 07:30:00 +00001641 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1642 SourceOffset*8+64, getContext()))
1643 return IRType;
1644 }
1645 }
Chris Lattner49382de2010-07-28 22:44:07 +00001646
Chris Lattner2acc6e32011-07-18 04:24:23 +00001647 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001648 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001649 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001650 if (IROffset < SL->getSizeInBytes()) {
1651 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1652 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001653
Chris Lattner0d2656d2010-07-29 17:40:35 +00001654 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1655 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001656 }
Chris Lattner49382de2010-07-28 22:44:07 +00001657 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001658
Chris Lattner2acc6e32011-07-18 04:24:23 +00001659 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001660 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner021c3a32010-07-29 07:43:55 +00001661 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1662 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001663 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1664 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001665 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001666
Chris Lattner49382de2010-07-28 22:44:07 +00001667 // Okay, we don't have any better idea of what to pass, so we pass this in an
1668 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001669 unsigned TySizeInBytes =
1670 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001671
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001672 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001673
Chris Lattner49382de2010-07-28 22:44:07 +00001674 // It is always safe to classify this as an integer type up to i64 that
1675 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001676 return llvm::IntegerType::get(getVMContext(),
1677 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001678}
1679
Chris Lattner66e7b682010-09-01 00:50:20 +00001680
1681/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1682/// be used as elements of a two register pair to pass or return, return a
1683/// first class aggregate to represent them. For example, if the low part of
1684/// a by-value argument should be passed as i32* and the high part as float,
1685/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001686static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001687GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattner66e7b682010-09-01 00:50:20 +00001688 const llvm::TargetData &TD) {
1689 // In order to correctly satisfy the ABI, we need to the high part to start
1690 // at offset 8. If the high and low parts we inferred are both 4-byte types
1691 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1692 // the second element at offset 8. Check for this:
1693 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1694 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1695 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1696 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001697
Chris Lattner66e7b682010-09-01 00:50:20 +00001698 // To handle this, we have to increase the size of the low part so that the
1699 // second element will start at an 8 byte offset. We can't increase the size
1700 // of the second element because it might make us access off the end of the
1701 // struct.
1702 if (HiStart != 8) {
1703 // There are only two sorts of types the ABI generation code can produce for
1704 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1705 // Promote these to a larger type.
1706 if (Lo->isFloatTy())
1707 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1708 else {
1709 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1710 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1711 }
1712 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001713
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001714 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001715
1716
Chris Lattner66e7b682010-09-01 00:50:20 +00001717 // Verify that the second element is at an 8-byte offset.
1718 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1719 "Invalid x86-64 argument pair!");
1720 return Result;
1721}
1722
Chris Lattner519f68c2010-07-28 23:06:14 +00001723ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001724classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001725 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1726 // classification algorithm.
1727 X86_64ABIInfo::Class Lo, Hi;
1728 classify(RetTy, 0, Lo, Hi);
1729
1730 // Check some invariants.
1731 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001732 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1733
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001734 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001735 switch (Lo) {
1736 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001737 if (Hi == NoClass)
1738 return ABIArgInfo::getIgnore();
1739 // If the low part is just padding, it takes no register, leave ResType
1740 // null.
1741 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1742 "Unknown missing lo part");
1743 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001744
1745 case SSEUp:
1746 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001747 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001748
1749 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1750 // hidden argument.
1751 case Memory:
1752 return getIndirectReturnResult(RetTy);
1753
1754 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1755 // available register of the sequence %rax, %rdx is used.
1756 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001757 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001758
Chris Lattnereb518b42010-07-29 21:42:50 +00001759 // If we have a sign or zero extended integer, make sure to return Extend
1760 // so that the parameter gets the right LLVM IR attributes.
1761 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1762 // Treat an enum type as its underlying type.
1763 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1764 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001765
Chris Lattnereb518b42010-07-29 21:42:50 +00001766 if (RetTy->isIntegralOrEnumerationType() &&
1767 RetTy->isPromotableIntegerType())
1768 return ABIArgInfo::getExtend();
1769 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001770 break;
1771
1772 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1773 // available SSE register of the sequence %xmm0, %xmm1 is used.
1774 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001775 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001776 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001777
1778 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1779 // returned on the X87 stack in %st0 as 80-bit x87 number.
1780 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001781 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001782 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001783
1784 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1785 // part of the value is returned in %st0 and the imaginary part in
1786 // %st1.
1787 case ComplexX87:
1788 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00001789 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00001790 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001791 NULL);
1792 break;
1793 }
1794
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001795 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001796 switch (Hi) {
1797 // Memory was handled previously and X87 should
1798 // never occur as a hi class.
1799 case Memory:
1800 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001801 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001802
1803 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001804 case NoClass:
1805 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001806
Chris Lattner3db4dde2010-09-01 00:20:33 +00001807 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001808 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001809 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1810 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001811 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001812 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001813 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001814 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1815 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001816 break;
1817
1818 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001819 // is passed in the next available eightbyte chunk if the last used
1820 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00001821 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001822 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00001823 case SSEUp:
1824 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001825 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001826 break;
1827
1828 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1829 // returned together with the previous X87 value in %st0.
1830 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001831 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00001832 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001833 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00001834 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001835 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001836 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00001837 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1838 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001839 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001840 break;
1841 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001842
Chris Lattner3db4dde2010-09-01 00:20:33 +00001843 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001844 // known to pass in the high eightbyte of the result. We do this by forming a
1845 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001846 if (HighPart)
1847 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001848
Chris Lattnereb518b42010-07-29 21:42:50 +00001849 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001850}
1851
Chris Lattnera3c109b2010-07-29 02:16:43 +00001852ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001853 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001854 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001855 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001856
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001857 // Check some invariants.
1858 // FIXME: Enforce these by construction.
1859 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001860 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1861
1862 neededInt = 0;
1863 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001864 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001865 switch (Lo) {
1866 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001867 if (Hi == NoClass)
1868 return ABIArgInfo::getIgnore();
1869 // If the low part is just padding, it takes no register, leave ResType
1870 // null.
1871 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1872 "Unknown missing lo part");
1873 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001874
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001875 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1876 // on the stack.
1877 case Memory:
1878
1879 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1880 // COMPLEX_X87, it is passed in memory.
1881 case X87:
1882 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00001883 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1884 ++neededInt;
Chris Lattner9c254f02010-06-29 06:01:59 +00001885 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001886
1887 case SSEUp:
1888 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001889 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001890
1891 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1892 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1893 // and %r9 is used.
1894 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001895 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001896
Chris Lattner49382de2010-07-28 22:44:07 +00001897 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001898 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001899
1900 // If we have a sign or zero extended integer, make sure to return Extend
1901 // so that the parameter gets the right LLVM IR attributes.
1902 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1903 // Treat an enum type as its underlying type.
1904 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1905 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001906
Chris Lattnereb518b42010-07-29 21:42:50 +00001907 if (Ty->isIntegralOrEnumerationType() &&
1908 Ty->isPromotableIntegerType())
1909 return ABIArgInfo::getExtend();
1910 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001911
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001912 break;
1913
1914 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1915 // available SSE register is used, the registers are taken in the
1916 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00001917 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001918 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00001919 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00001920 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001921 break;
1922 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00001923 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001924
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001925 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001926 switch (Hi) {
1927 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001928 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001929 // which is passed in memory.
1930 case Memory:
1931 case X87:
1932 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00001933 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001934
1935 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001936
Chris Lattner645406a2010-09-01 00:24:35 +00001937 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001938 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001939 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001940 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001941
Chris Lattner645406a2010-09-01 00:24:35 +00001942 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1943 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001944 break;
1945
1946 // X87Up generally doesn't occur here (long double is passed in
1947 // memory), except in situations involving unions.
1948 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001949 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001950 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001951
Chris Lattner645406a2010-09-01 00:24:35 +00001952 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1953 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001954
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001955 ++neededSSE;
1956 break;
1957
1958 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1959 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001960 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001961 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001962 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001963 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001964 break;
1965 }
1966
Chris Lattner645406a2010-09-01 00:24:35 +00001967 // If a high part was specified, merge it together with the low part. It is
1968 // known to pass in the high eightbyte of the result. We do this by forming a
1969 // first class struct aggregate with the high and low part: {low, high}
1970 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001971 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001972
Chris Lattnereb518b42010-07-29 21:42:50 +00001973 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001974}
1975
Chris Lattneree5dcd02010-07-29 02:31:05 +00001976void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001977
Chris Lattnera3c109b2010-07-29 02:16:43 +00001978 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001979
1980 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00001981 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001982
1983 // If the return value is indirect, then the hidden argument is consuming one
1984 // integer register.
1985 if (FI.getReturnInfo().isIndirect())
1986 --freeIntRegs;
1987
1988 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1989 // get assigned (in left-to-right order) for passing as follows...
1990 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1991 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00001992 unsigned neededInt, neededSSE;
1993 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001994
1995 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1996 // eightbyte of an argument, the whole argument is passed on the
1997 // stack. If registers have already been assigned for some
1998 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00001999 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002000 freeIntRegs -= neededInt;
2001 freeSSERegs -= neededSSE;
2002 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00002003 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002004 }
2005 }
2006}
2007
2008static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2009 QualType Ty,
2010 CodeGenFunction &CGF) {
2011 llvm::Value *overflow_arg_area_p =
2012 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2013 llvm::Value *overflow_arg_area =
2014 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2015
2016 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2017 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002018 // It isn't stated explicitly in the standard, but in practice we use
2019 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002020 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2021 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002022 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002023 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002024 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002025 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2026 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002027 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002028 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002029 overflow_arg_area =
2030 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2031 overflow_arg_area->getType(),
2032 "overflow_arg_area.align");
2033 }
2034
2035 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002036 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002037 llvm::Value *Res =
2038 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002039 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002040
2041 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2042 // l->overflow_arg_area + sizeof(type).
2043 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2044 // an 8 byte boundary.
2045
2046 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002047 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002048 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002049 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2050 "overflow_arg_area.next");
2051 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2052
2053 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2054 return Res;
2055}
2056
2057llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2058 CodeGenFunction &CGF) const {
2059 // Assume that va_list type is correct; should be pointer to LLVM type:
2060 // struct {
2061 // i32 gp_offset;
2062 // i32 fp_offset;
2063 // i8* overflow_arg_area;
2064 // i8* reg_save_area;
2065 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002066 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002067
Chris Lattnera14db752010-03-11 18:19:55 +00002068 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling99aaae82010-10-18 23:51:38 +00002069 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002070
2071 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2072 // in the registers. If not go to step 7.
2073 if (!neededInt && !neededSSE)
2074 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2075
2076 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2077 // general purpose registers needed to pass type and num_fp to hold
2078 // the number of floating point registers needed.
2079
2080 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2081 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2082 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2083 //
2084 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2085 // register save space).
2086
2087 llvm::Value *InRegs = 0;
2088 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2089 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2090 if (neededInt) {
2091 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2092 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002093 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2094 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002095 }
2096
2097 if (neededSSE) {
2098 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2099 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2100 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002101 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2102 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002103 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2104 }
2105
2106 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2107 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2108 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2109 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2110
2111 // Emit code to load the value if it was passed in registers.
2112
2113 CGF.EmitBlock(InRegBlock);
2114
2115 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2116 // an offset of l->gp_offset and/or l->fp_offset. This may require
2117 // copying to a temporary location in case the parameter is passed
2118 // in different register classes or requires an alignment greater
2119 // than 8 for general purpose registers and 16 for XMM registers.
2120 //
2121 // FIXME: This really results in shameful code when we end up needing to
2122 // collect arguments from different places; often what should result in a
2123 // simple assembling of a structure from scattered addresses has many more
2124 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002125 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002126 llvm::Value *RegAddr =
2127 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2128 "reg_save_area");
2129 if (neededInt && neededSSE) {
2130 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002131 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002132 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002133 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2134 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002135 llvm::Type *TyLo = ST->getElementType(0);
2136 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002137 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002138 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002139 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2140 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002141 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2142 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002143 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2144 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002145 llvm::Value *V =
2146 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2147 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2148 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2149 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2150
Owen Andersona1cf15f2009-07-14 23:10:40 +00002151 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002152 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002153 } else if (neededInt) {
2154 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2155 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002156 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002157 } else if (neededSSE == 1) {
2158 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2159 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2160 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002161 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002162 assert(neededSSE == 2 && "Invalid number of needed registers!");
2163 // SSE registers are spaced 16 bytes apart in the register save
2164 // area, we need to collect the two eightbytes together.
2165 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002166 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002167 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002168 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002169 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002170 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002171 DoubleTy, NULL);
2172 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2173 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2174 DblPtrTy));
2175 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2176 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2177 DblPtrTy));
2178 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2179 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2180 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181 }
2182
2183 // AMD64-ABI 3.5.7p5: Step 5. Set:
2184 // l->gp_offset = l->gp_offset + num_gp * 8
2185 // l->fp_offset = l->fp_offset + num_fp * 16.
2186 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002187 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002188 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2189 gp_offset_p);
2190 }
2191 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002192 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002193 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2194 fp_offset_p);
2195 }
2196 CGF.EmitBranch(ContBlock);
2197
2198 // Emit code to load the value if it was passed in memory.
2199
2200 CGF.EmitBlock(InMemBlock);
2201 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2202
2203 // Return the appropriate result.
2204
2205 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002206 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002207 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 ResAddr->addIncoming(RegAddr, InRegBlock);
2209 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002210 return ResAddr;
2211}
2212
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002213ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2214
2215 if (Ty->isVoidType())
2216 return ABIArgInfo::getIgnore();
2217
2218 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2219 Ty = EnumTy->getDecl()->getIntegerType();
2220
2221 uint64_t Size = getContext().getTypeSize(Ty);
2222
2223 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002224 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2225 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002226 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2227
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002228 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2229 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002230 getContext().getTargetInfo().getTriple().getOS()
2231 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002232 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2233 Size));
2234
2235 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2236 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2237 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002238 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002239 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2240 Size));
2241
2242 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2243 }
2244
2245 if (Ty->isPromotableIntegerType())
2246 return ABIArgInfo::getExtend();
2247
2248 return ABIArgInfo::getDirect();
2249}
2250
2251void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2252
2253 QualType RetTy = FI.getReturnType();
2254 FI.getReturnInfo() = classify(RetTy);
2255
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002256 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2257 it != ie; ++it)
2258 it->info = classify(it->type);
2259}
2260
Chris Lattnerf13721d2010-08-31 16:44:54 +00002261llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2262 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002263 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002264
Chris Lattnerf13721d2010-08-31 16:44:54 +00002265 CGBuilderTy &Builder = CGF.Builder;
2266 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2267 "ap");
2268 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2269 llvm::Type *PTy =
2270 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2271 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2272
2273 uint64_t Offset =
2274 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2275 llvm::Value *NextAddr =
2276 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2277 "ap.next");
2278 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2279
2280 return AddrTyped;
2281}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002282
John McCallec853ba2010-03-11 00:10:12 +00002283// PowerPC-32
2284
2285namespace {
2286class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2287public:
Chris Lattnerea044322010-07-29 02:01:43 +00002288 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002289
John McCallec853ba2010-03-11 00:10:12 +00002290 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2291 // This is recovered from gcc output.
2292 return 1; // r1 is the dedicated stack pointer
2293 }
2294
2295 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002296 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002297};
2298
2299}
2300
2301bool
2302PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2303 llvm::Value *Address) const {
2304 // This is calculated from the LLVM and GCC tables and verified
2305 // against gcc output. AFAIK all ABIs use the same encoding.
2306
2307 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002308
Chris Lattner8b418682012-02-07 00:39:47 +00002309 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002310 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2311 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2312 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2313
2314 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002315 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002316
2317 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002318 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002319
2320 // 64-76 are various 4-byte special-purpose registers:
2321 // 64: mq
2322 // 65: lr
2323 // 66: ctr
2324 // 67: ap
2325 // 68-75 cr0-7
2326 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002327 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002328
2329 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002330 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002331
2332 // 109: vrsave
2333 // 110: vscr
2334 // 111: spe_acc
2335 // 112: spefscr
2336 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002337 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002338
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002339 return false;
John McCallec853ba2010-03-11 00:10:12 +00002340}
2341
2342
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002343//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002344// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002345//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002346
2347namespace {
2348
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002349class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002350public:
2351 enum ABIKind {
2352 APCS = 0,
2353 AAPCS = 1,
2354 AAPCS_VFP
2355 };
2356
2357private:
2358 ABIKind Kind;
2359
2360public:
Chris Lattnerea044322010-07-29 02:01:43 +00002361 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002362
John McCall49e34be2011-08-30 01:42:09 +00002363 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002364 StringRef Env =
2365 getContext().getTargetInfo().getTriple().getEnvironmentName();
Chandler Carruthb43550b2012-01-10 19:47:42 +00002366 return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002367 }
2368
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002369private:
2370 ABIKind getABIKind() const { return Kind; }
2371
Chris Lattnera3c109b2010-07-29 02:16:43 +00002372 ABIArgInfo classifyReturnType(QualType RetTy) const;
2373 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002374
Chris Lattneree5dcd02010-07-29 02:31:05 +00002375 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002376
2377 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2378 CodeGenFunction &CGF) const;
2379};
2380
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002381class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2382public:
Chris Lattnerea044322010-07-29 02:01:43 +00002383 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2384 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002385
John McCall49e34be2011-08-30 01:42:09 +00002386 const ARMABIInfo &getABIInfo() const {
2387 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2388 }
2389
John McCall6374c332010-03-06 00:35:14 +00002390 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2391 return 13;
2392 }
Roman Divacky09345d12011-05-18 19:36:54 +00002393
Chris Lattner5f9e2722011-07-23 10:55:15 +00002394 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002395 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2396 }
2397
Roman Divacky09345d12011-05-18 19:36:54 +00002398 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2399 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002400 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002401
2402 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002403 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002404 return false;
2405 }
John McCall49e34be2011-08-30 01:42:09 +00002406
2407 unsigned getSizeOfUnwindException() const {
2408 if (getABIInfo().isEABI()) return 88;
2409 return TargetCodeGenInfo::getSizeOfUnwindException();
2410 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002411};
2412
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002413}
2414
Chris Lattneree5dcd02010-07-29 02:31:05 +00002415void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002416 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002417 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002418 it != ie; ++it)
2419 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002420
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002421 // Always honor user-specified calling convention.
2422 if (FI.getCallingConvention() != llvm::CallingConv::C)
2423 return;
2424
2425 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002426 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002427 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002428 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002429 else
2430 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002431
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002432 // If user did not ask for specific calling convention explicitly (e.g. via
2433 // pcs attribute), set effective calling convention if it's different than ABI
2434 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002435 switch (getABIKind()) {
2436 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002437 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2438 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002439 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002440 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002441 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2442 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002443 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002444 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002445 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2446 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002447 break;
2448 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002449}
2450
Bob Wilson194f06a2011-08-03 05:58:22 +00002451/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2452/// aggregate. If HAMembers is non-null, the number of base elements
2453/// contained in the type is returned through it; this is used for the
2454/// recursive calls that check aggregate component types.
2455static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2456 ASTContext &Context,
2457 uint64_t *HAMembers = 0) {
2458 uint64_t Members;
2459 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2460 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2461 return false;
2462 Members *= AT->getSize().getZExtValue();
2463 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2464 const RecordDecl *RD = RT->getDecl();
2465 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2466 return false;
2467 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2468 if (!CXXRD->isAggregate())
2469 return false;
2470 }
2471 Members = 0;
2472 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2473 i != e; ++i) {
2474 const FieldDecl *FD = *i;
2475 uint64_t FldMembers;
2476 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2477 return false;
2478 Members += FldMembers;
2479 }
2480 } else {
2481 Members = 1;
2482 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2483 Members = 2;
2484 Ty = CT->getElementType();
2485 }
2486
2487 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2488 // double, or 64-bit or 128-bit vectors.
2489 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2490 if (BT->getKind() != BuiltinType::Float &&
2491 BT->getKind() != BuiltinType::Double)
2492 return false;
2493 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2494 unsigned VecSize = Context.getTypeSize(VT);
2495 if (VecSize != 64 && VecSize != 128)
2496 return false;
2497 } else {
2498 return false;
2499 }
2500
2501 // The base type must be the same for all members. Vector types of the
2502 // same total size are treated as being equivalent here.
2503 const Type *TyPtr = Ty.getTypePtr();
2504 if (!Base)
2505 Base = TyPtr;
2506 if (Base != TyPtr &&
2507 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2508 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2509 return false;
2510 }
2511
2512 // Homogeneous Aggregates can have at most 4 members of the base type.
2513 if (HAMembers)
2514 *HAMembers = Members;
2515 return (Members <= 4);
2516}
2517
Chris Lattnera3c109b2010-07-29 02:16:43 +00002518ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002519 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002520 // Treat an enum type as its underlying type.
2521 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2522 Ty = EnumTy->getDecl()->getIntegerType();
2523
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002524 return (Ty->isPromotableIntegerType() ?
2525 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002526 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002527
Daniel Dunbar42025572009-09-14 21:54:03 +00002528 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002529 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002530 return ABIArgInfo::getIgnore();
2531
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002532 // Structures with either a non-trivial destructor or a non-trivial
2533 // copy constructor are always indirect.
2534 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2535 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2536
Bob Wilson194f06a2011-08-03 05:58:22 +00002537 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2538 // Homogeneous Aggregates need to be expanded.
2539 const Type *Base = 0;
2540 if (isHomogeneousAggregate(Ty, Base, getContext()))
2541 return ABIArgInfo::getExpand();
2542 }
2543
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002544 // Otherwise, pass by coercing to a structure of the appropriate size.
2545 //
Bob Wilson53fc1a62011-08-01 23:39:04 +00002546 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2547 // backend doesn't support byval.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002548 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002549 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002550 unsigned SizeRegs;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002551 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastings67d097e2011-04-27 17:24:02 +00002552 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2553 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson53fc1a62011-08-01 23:39:04 +00002554 } else {
2555 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2556 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002557 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002558
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002559 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002560 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002561 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002562}
2563
Chris Lattnera3c109b2010-07-29 02:16:43 +00002564static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002565 llvm::LLVMContext &VMContext) {
2566 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2567 // is called integer-like if its size is less than or equal to one word, and
2568 // the offset of each of its addressable sub-fields is zero.
2569
2570 uint64_t Size = Context.getTypeSize(Ty);
2571
2572 // Check that the type fits in a word.
2573 if (Size > 32)
2574 return false;
2575
2576 // FIXME: Handle vector types!
2577 if (Ty->isVectorType())
2578 return false;
2579
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002580 // Float types are never treated as "integer like".
2581 if (Ty->isRealFloatingType())
2582 return false;
2583
Daniel Dunbar98303b92009-09-13 08:03:58 +00002584 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002585 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002586 return true;
2587
Daniel Dunbar45815812010-02-01 23:31:26 +00002588 // Small complex integer types are "integer like".
2589 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2590 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002591
2592 // Single element and zero sized arrays should be allowed, by the definition
2593 // above, but they are not.
2594
2595 // Otherwise, it must be a record type.
2596 const RecordType *RT = Ty->getAs<RecordType>();
2597 if (!RT) return false;
2598
2599 // Ignore records with flexible arrays.
2600 const RecordDecl *RD = RT->getDecl();
2601 if (RD->hasFlexibleArrayMember())
2602 return false;
2603
2604 // Check that all sub-fields are at offset 0, and are themselves "integer
2605 // like".
2606 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2607
2608 bool HadField = false;
2609 unsigned idx = 0;
2610 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2611 i != e; ++i, ++idx) {
2612 const FieldDecl *FD = *i;
2613
Daniel Dunbar679855a2010-01-29 03:22:29 +00002614 // Bit-fields are not addressable, we only need to verify they are "integer
2615 // like". We still have to disallow a subsequent non-bitfield, for example:
2616 // struct { int : 0; int x }
2617 // is non-integer like according to gcc.
2618 if (FD->isBitField()) {
2619 if (!RD->isUnion())
2620 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002621
Daniel Dunbar679855a2010-01-29 03:22:29 +00002622 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2623 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002624
Daniel Dunbar679855a2010-01-29 03:22:29 +00002625 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002626 }
2627
Daniel Dunbar679855a2010-01-29 03:22:29 +00002628 // Check if this field is at offset 0.
2629 if (Layout.getFieldOffset(idx) != 0)
2630 return false;
2631
Daniel Dunbar98303b92009-09-13 08:03:58 +00002632 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2633 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002634
Daniel Dunbar679855a2010-01-29 03:22:29 +00002635 // Only allow at most one field in a structure. This doesn't match the
2636 // wording above, but follows gcc in situations with a field following an
2637 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002638 if (!RD->isUnion()) {
2639 if (HadField)
2640 return false;
2641
2642 HadField = true;
2643 }
2644 }
2645
2646 return true;
2647}
2648
Chris Lattnera3c109b2010-07-29 02:16:43 +00002649ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002650 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002651 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002652
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00002653 // Large vector types should be returned via memory.
2654 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2655 return ABIArgInfo::getIndirect(0);
2656
John McCalld608cdb2010-08-22 10:59:02 +00002657 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002658 // Treat an enum type as its underlying type.
2659 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2660 RetTy = EnumTy->getDecl()->getIntegerType();
2661
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002662 return (RetTy->isPromotableIntegerType() ?
2663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002664 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002665
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002666 // Structures with either a non-trivial destructor or a non-trivial
2667 // copy constructor are always indirect.
2668 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2669 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2670
Daniel Dunbar98303b92009-09-13 08:03:58 +00002671 // Are we following APCS?
2672 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002673 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002674 return ABIArgInfo::getIgnore();
2675
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002676 // Complex types are all returned as packed integers.
2677 //
2678 // FIXME: Consider using 2 x vector types if the back end handles them
2679 // correctly.
2680 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002681 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002682 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002683
Daniel Dunbar98303b92009-09-13 08:03:58 +00002684 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002685 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002686 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002687 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002688 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002689 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002690 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002691 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2692 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002693 }
2694
2695 // Otherwise return in memory.
2696 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002697 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002698
2699 // Otherwise this is an AAPCS variant.
2700
Chris Lattnera3c109b2010-07-29 02:16:43 +00002701 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002702 return ABIArgInfo::getIgnore();
2703
Bob Wilson3b694fa2011-11-02 04:51:36 +00002704 // Check for homogeneous aggregates with AAPCS-VFP.
2705 if (getABIKind() == AAPCS_VFP) {
2706 const Type *Base = 0;
2707 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2708 // Homogeneous Aggregates are returned directly.
2709 return ABIArgInfo::getDirect();
2710 }
2711
Daniel Dunbar98303b92009-09-13 08:03:58 +00002712 // Aggregates <= 4 bytes are returned in r0; other aggregates
2713 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002714 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002715 if (Size <= 32) {
2716 // Return in the smallest viable integer type.
2717 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002718 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002719 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002720 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2721 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002722 }
2723
Daniel Dunbar98303b92009-09-13 08:03:58 +00002724 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002725}
2726
2727llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002728 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002729 llvm::Type *BP = CGF.Int8PtrTy;
2730 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002731
2732 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00002733 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002734 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00002735 // Handle address alignment for type alignment > 32 bits
2736 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2737 if (TyAlign > 4) {
2738 assert((TyAlign & (TyAlign - 1)) == 0 &&
2739 "Alignment is not power of 2!");
2740 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2741 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2742 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2743 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2744 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002745 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002746 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002747 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2748
2749 uint64_t Offset =
2750 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2751 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002752 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002753 "ap.next");
2754 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2755
2756 return AddrTyped;
2757}
2758
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002759//===----------------------------------------------------------------------===//
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002760// PTX ABI Implementation
2761//===----------------------------------------------------------------------===//
2762
2763namespace {
2764
2765class PTXABIInfo : public ABIInfo {
2766public:
2767 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2768
2769 ABIArgInfo classifyReturnType(QualType RetTy) const;
2770 ABIArgInfo classifyArgumentType(QualType Ty) const;
2771
2772 virtual void computeInfo(CGFunctionInfo &FI) const;
2773 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2774 CodeGenFunction &CFG) const;
2775};
2776
2777class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2778public:
2779 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2780 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00002781
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00002782 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2783 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002784};
2785
2786ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2787 if (RetTy->isVoidType())
2788 return ABIArgInfo::getIgnore();
2789 if (isAggregateTypeForABI(RetTy))
2790 return ABIArgInfo::getIndirect(0);
2791 return ABIArgInfo::getDirect();
2792}
2793
2794ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2795 if (isAggregateTypeForABI(Ty))
2796 return ABIArgInfo::getIndirect(0);
2797
2798 return ABIArgInfo::getDirect();
2799}
2800
2801void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2802 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2803 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2804 it != ie; ++it)
2805 it->info = classifyArgumentType(it->type);
2806
2807 // Always honor user-specified calling convention.
2808 if (FI.getCallingConvention() != llvm::CallingConv::C)
2809 return;
2810
2811 // Calling convention as default by an ABI.
2812 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002813 const LangOptions &LangOpts = getContext().getLangOptions();
2814 if (LangOpts.OpenCL || LangOpts.CUDA) {
2815 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002816 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00002817 } else {
2818 // If we are in standard C/C++ mode, use the triple to decide on the default
2819 StringRef Env =
2820 getContext().getTargetInfo().getTriple().getEnvironmentName();
2821 if (Env == "device")
2822 DefaultCC = llvm::CallingConv::PTX_Device;
2823 else
2824 DefaultCC = llvm::CallingConv::PTX_Kernel;
2825 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002826 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002827
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002828}
2829
2830llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2831 CodeGenFunction &CFG) const {
2832 llvm_unreachable("PTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002833}
2834
Justin Holewinski818eafb2011-10-05 17:58:44 +00002835void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2836 llvm::GlobalValue *GV,
2837 CodeGen::CodeGenModule &M) const{
2838 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2839 if (!FD) return;
2840
2841 llvm::Function *F = cast<llvm::Function>(GV);
2842
2843 // Perform special handling in OpenCL mode
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002844 if (M.getLangOptions().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00002845 // Use OpenCL function attributes to set proper calling conventions
2846 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00002847 if (FD->hasAttr<OpenCLKernelAttr>()) {
2848 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002849 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002850 // And kernel functions are not subject to inlining
2851 F->addFnAttr(llvm::Attribute::NoInline);
2852 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002853 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00002854
Peter Collingbourne744d90b2011-10-06 16:49:54 +00002855 // Perform special handling in CUDA mode.
2856 if (M.getLangOptions().CUDA) {
2857 // CUDA __global__ functions get a kernel calling convention. Since
2858 // __global__ functions cannot be called from the device, we do not
2859 // need to set the noinline attribute.
2860 if (FD->getAttr<CUDAGlobalAttr>())
2861 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00002862 }
2863}
2864
Justin Holewinski0259c3a2011-04-22 11:10:38 +00002865}
2866
2867//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00002868// MBlaze ABI Implementation
2869//===----------------------------------------------------------------------===//
2870
2871namespace {
2872
2873class MBlazeABIInfo : public ABIInfo {
2874public:
2875 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2876
2877 bool isPromotableIntegerType(QualType Ty) const;
2878
2879 ABIArgInfo classifyReturnType(QualType RetTy) const;
2880 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2881
2882 virtual void computeInfo(CGFunctionInfo &FI) const {
2883 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2884 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2885 it != ie; ++it)
2886 it->info = classifyArgumentType(it->type);
2887 }
2888
2889 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2890 CodeGenFunction &CGF) const;
2891};
2892
2893class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2894public:
2895 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2896 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2897 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2898 CodeGen::CodeGenModule &M) const;
2899};
2900
2901}
2902
2903bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2904 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2905 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2906 switch (BT->getKind()) {
2907 case BuiltinType::Bool:
2908 case BuiltinType::Char_S:
2909 case BuiltinType::Char_U:
2910 case BuiltinType::SChar:
2911 case BuiltinType::UChar:
2912 case BuiltinType::Short:
2913 case BuiltinType::UShort:
2914 return true;
2915 default:
2916 return false;
2917 }
2918 return false;
2919}
2920
2921llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2922 CodeGenFunction &CGF) const {
2923 // FIXME: Implement
2924 return 0;
2925}
2926
2927
2928ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2929 if (RetTy->isVoidType())
2930 return ABIArgInfo::getIgnore();
2931 if (isAggregateTypeForABI(RetTy))
2932 return ABIArgInfo::getIndirect(0);
2933
2934 return (isPromotableIntegerType(RetTy) ?
2935 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2936}
2937
2938ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2939 if (isAggregateTypeForABI(Ty))
2940 return ABIArgInfo::getIndirect(0);
2941
2942 return (isPromotableIntegerType(Ty) ?
2943 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2944}
2945
2946void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2947 llvm::GlobalValue *GV,
2948 CodeGen::CodeGenModule &M)
2949 const {
2950 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2951 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00002952
Wesley Peck276fdf42010-12-19 19:57:51 +00002953 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2954 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2955 CC = llvm::CallingConv::MBLAZE_INTR;
2956 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2957 CC = llvm::CallingConv::MBLAZE_SVOL;
2958
2959 if (CC != llvm::CallingConv::C) {
2960 // Handle 'interrupt_handler' attribute:
2961 llvm::Function *F = cast<llvm::Function>(GV);
2962
2963 // Step 1: Set ISR calling convention.
2964 F->setCallingConv(CC);
2965
2966 // Step 2: Add attributes goodness.
2967 F->addFnAttr(llvm::Attribute::NoInline);
2968 }
2969
2970 // Step 3: Emit _interrupt_handler alias.
2971 if (CC == llvm::CallingConv::MBLAZE_INTR)
2972 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2973 "_interrupt_handler", GV, &M.getModule());
2974}
2975
2976
2977//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002978// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002979//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002980
2981namespace {
2982
2983class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2984public:
Chris Lattnerea044322010-07-29 02:01:43 +00002985 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2986 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002987 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2988 CodeGen::CodeGenModule &M) const;
2989};
2990
2991}
2992
2993void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2994 llvm::GlobalValue *GV,
2995 CodeGen::CodeGenModule &M) const {
2996 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2997 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2998 // Handle 'interrupt' attribute:
2999 llvm::Function *F = cast<llvm::Function>(GV);
3000
3001 // Step 1: Set ISR calling convention.
3002 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3003
3004 // Step 2: Add attributes goodness.
3005 F->addFnAttr(llvm::Attribute::NoInline);
3006
3007 // Step 3: Emit ISR vector alias.
3008 unsigned Num = attr->getNumber() + 0xffe0;
3009 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003010 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003011 GV, &M.getModule());
3012 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003013 }
3014}
3015
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003016//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003017// MIPS ABI Implementation. This works for both little-endian and
3018// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003019//===----------------------------------------------------------------------===//
3020
John McCallaeeb7012010-05-27 06:19:26 +00003021namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003022class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003023 bool IsO32;
Akira Hatanakab551dd32011-11-03 00:05:50 +00003024 unsigned MinABIStackAlignInBytes;
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003025 llvm::Type* HandleAggregates(QualType Ty) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003026 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003027 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003028public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003029 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3030 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003031
3032 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003033 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003034 virtual void computeInfo(CGFunctionInfo &FI) const;
3035 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3036 CodeGenFunction &CGF) const;
3037};
3038
John McCallaeeb7012010-05-27 06:19:26 +00003039class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003040 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003041public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003042 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3043 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3044 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003045
3046 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3047 return 29;
3048 }
3049
3050 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003051 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003052
3053 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003054 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003055 }
John McCallaeeb7012010-05-27 06:19:26 +00003056};
3057}
3058
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003059// In N32/64, an aligned double precision floating point field is passed in
3060// a register.
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003061llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty) const {
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003062 if (IsO32)
3063 return 0;
3064
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003065 if (Ty->isComplexType())
3066 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003067
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003068 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003069
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003070 // Unions are passed in integer registers.
3071 if (!RT || !RT->isStructureOrClassType())
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003072 return 0;
3073
3074 const RecordDecl *RD = RT->getDecl();
3075 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3076 uint64_t StructSize = getContext().getTypeSize(Ty);
3077 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3078
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003079 uint64_t LastOffset = 0;
3080 unsigned idx = 0;
3081 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003082 SmallVector<llvm::Type*, 8> ArgList;
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003083
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003084 // Iterate over fields in the struct/class and check if there are any aligned
3085 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003086 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3087 i != e; ++i, ++idx) {
3088 const QualType Ty = (*i)->getType();
3089 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3090
3091 if (!BT || BT->getKind() != BuiltinType::Double)
3092 continue;
3093
3094 uint64_t Offset = Layout.getFieldOffset(idx);
3095 if (Offset % 64) // Ignore doubles that are not aligned.
3096 continue;
3097
3098 // Add ((Offset - LastOffset) / 64) args of type i64.
3099 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3100 ArgList.push_back(I64);
3101
3102 // Add double type.
3103 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3104 LastOffset = Offset + 64;
3105 }
3106
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003107 // This struct/class doesn't have an aligned double field.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003108 if (!LastOffset)
3109 return 0;
3110
3111 // Add ((StructSize - LastOffset) / 64) args of type i64.
3112 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3113 ArgList.push_back(I64);
3114
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003115 // If the size of the remainder is not zero, add one more integer type to
3116 // ArgList.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003117 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakab49d5a62011-11-03 23:31:00 +00003118 if (R)
3119 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003120
3121 return llvm::StructType::get(getVMContext(), ArgList);
3122}
3123
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003124llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
3125 // Padding is inserted only for N32/64.
3126 if (IsO32)
3127 return 0;
3128
3129 assert(Align <= 16 && "Alignment larger than 16 not handled.");
3130 return (Align == 16 && Offset & 0xf) ?
3131 llvm::IntegerType::get(getVMContext(), 64) : 0;
3132}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003133
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003134ABIArgInfo
3135MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003136 uint64_t OrigOffset = Offset;
3137 uint64_t TySize =
3138 llvm::RoundUpToAlignment(getContext().getTypeSize(Ty), 64) / 8;
3139 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
3140 Offset = llvm::RoundUpToAlignment(Offset, std::max(Align, (uint64_t)8));
3141 Offset += TySize;
3142
Akira Hatanaka619e8872011-06-02 00:09:17 +00003143 if (isAggregateTypeForABI(Ty)) {
3144 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003145 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003146 return ABIArgInfo::getIgnore();
3147
Akira Hatanaka511949b2011-08-01 18:09:58 +00003148 // Records with non trivial destructors/constructors should not be passed
3149 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003150 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003151 Offset = OrigOffset + 8;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003152 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003153 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003154
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003155 // If we have reached here, aggregates are passed either indirectly via a
3156 // byval pointer or directly by coercing to another structure type. In the
3157 // latter case, padding is inserted if the offset of the aggregate is
3158 // unaligned.
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003159 llvm::Type *ResType = HandleAggregates(Ty);
Akira Hatanaka9659d592012-01-10 22:44:52 +00003160
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003161 if (!ResType)
3162 return ABIArgInfo::getIndirect(0);
3163
3164 return ABIArgInfo::getDirect(ResType, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003165 }
3166
3167 // Treat an enum type as its underlying type.
3168 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3169 Ty = EnumTy->getDecl()->getIntegerType();
3170
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003171 if (Ty->isPromotableIntegerType())
3172 return ABIArgInfo::getExtend();
3173
3174 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003175}
3176
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003177llvm::Type*
3178MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003179 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003180 SmallVector<llvm::Type*, 2> RTList;
3181
Akira Hatanakada54ff32012-02-09 18:49:26 +00003182 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003183 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003184 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3185 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003186
Akira Hatanakada54ff32012-02-09 18:49:26 +00003187 // N32/64 returns struct/classes in floating point registers if the
3188 // following conditions are met:
3189 // 1. The size of the struct/class is no larger than 128-bit.
3190 // 2. The struct/class has one or two fields all of which are floating
3191 // point types.
3192 // 3. The offset of the first field is zero (this follows what gcc does).
3193 //
3194 // Any other composite results are returned in integer registers.
3195 //
3196 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3197 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3198 for (; b != e; ++b) {
3199 const BuiltinType *BT = (*b)->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003200
Akira Hatanakada54ff32012-02-09 18:49:26 +00003201 if (!BT || !BT->isFloatingPoint())
3202 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003203
Akira Hatanakada54ff32012-02-09 18:49:26 +00003204 RTList.push_back(CGT.ConvertType((*b)->getType()));
3205 }
3206
3207 if (b == e)
3208 return llvm::StructType::get(getVMContext(), RTList,
3209 RD->hasAttr<PackedAttr>());
3210
3211 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003212 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003213 }
3214
3215 RTList.push_back(llvm::IntegerType::get(getVMContext(),
3216 std::min(Size, (uint64_t)64)));
3217 if (Size > 64)
3218 RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
3219
3220 return llvm::StructType::get(getVMContext(), RTList);
3221}
3222
Akira Hatanaka619e8872011-06-02 00:09:17 +00003223ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003224 uint64_t Size = getContext().getTypeSize(RetTy);
3225
3226 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003227 return ABIArgInfo::getIgnore();
3228
3229 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003230 if (Size <= 128) {
3231 if (RetTy->isAnyComplexType())
3232 return ABIArgInfo::getDirect();
3233
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003234 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003235 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3236 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003237
3238 return ABIArgInfo::getIndirect(0);
3239 }
3240
3241 // Treat an enum type as its underlying type.
3242 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3243 RetTy = EnumTy->getDecl()->getIntegerType();
3244
3245 return (RetTy->isPromotableIntegerType() ?
3246 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3247}
3248
3249void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003250 ABIArgInfo &RetInfo = FI.getReturnInfo();
3251 RetInfo = classifyReturnType(FI.getReturnType());
3252
3253 // Check if a pointer to an aggregate is passed as a hidden argument.
3254 uint64_t Offset = RetInfo.isIndirect() ? 8 : 0;
3255
Akira Hatanaka619e8872011-06-02 00:09:17 +00003256 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3257 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003258 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003259}
3260
3261llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3262 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003263 llvm::Type *BP = CGF.Int8PtrTy;
3264 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003265
3266 CGBuilderTy &Builder = CGF.Builder;
3267 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3268 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003269 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003270 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3271 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003272 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3273 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003274
3275 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003276 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3277 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3278 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3279 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003280 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3281 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3282 }
3283 else
3284 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3285
3286 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003287 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003288 uint64_t Offset =
3289 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3290 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003291 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003292 "ap.next");
3293 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3294
3295 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003296}
3297
John McCallaeeb7012010-05-27 06:19:26 +00003298bool
3299MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3300 llvm::Value *Address) const {
3301 // This information comes from gcc's implementation, which seems to
3302 // as canonical as it gets.
3303
John McCallaeeb7012010-05-27 06:19:26 +00003304 // Everything on MIPS is 4 bytes. Double-precision FP registers
3305 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003306 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003307
3308 // 0-31 are the general purpose registers, $0 - $31.
3309 // 32-63 are the floating-point registers, $f0 - $f31.
3310 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3311 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003312 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003313
3314 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3315 // They are one bit wide and ignored here.
3316
3317 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3318 // (coprocessor 1 is the FP unit)
3319 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3320 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3321 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003322 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003323 return false;
3324}
3325
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003326//===----------------------------------------------------------------------===//
3327// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3328// Currently subclassed only to implement custom OpenCL C function attribute
3329// handling.
3330//===----------------------------------------------------------------------===//
3331
3332namespace {
3333
3334class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3335public:
3336 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3337 : DefaultTargetCodeGenInfo(CGT) {}
3338
3339 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3340 CodeGen::CodeGenModule &M) const;
3341};
3342
3343void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3344 llvm::GlobalValue *GV,
3345 CodeGen::CodeGenModule &M) const {
3346 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3347 if (!FD) return;
3348
3349 llvm::Function *F = cast<llvm::Function>(GV);
3350
3351 if (M.getLangOptions().OpenCL) {
3352 if (FD->hasAttr<OpenCLKernelAttr>()) {
3353 // OpenCL C Kernel functions are not subject to inlining
3354 F->addFnAttr(llvm::Attribute::NoInline);
3355
3356 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3357
3358 // Convert the reqd_work_group_size() attributes to metadata.
3359 llvm::LLVMContext &Context = F->getContext();
3360 llvm::NamedMDNode *OpenCLMetadata =
3361 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3362
3363 SmallVector<llvm::Value*, 5> Operands;
3364 Operands.push_back(F);
3365
Chris Lattner8b418682012-02-07 00:39:47 +00003366 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3367 llvm::APInt(32,
3368 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3369 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3370 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003371 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003372 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3373 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003374 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3375
3376 // Add a boolean constant operand for "required" (true) or "hint" (false)
3377 // for implementing the work_group_size_hint attr later. Currently
3378 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003379 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003380 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3381 }
3382 }
3383 }
3384}
3385
3386}
John McCallaeeb7012010-05-27 06:19:26 +00003387
Tony Linthicum96319392011-12-12 21:14:55 +00003388//===----------------------------------------------------------------------===//
3389// Hexagon ABI Implementation
3390//===----------------------------------------------------------------------===//
3391
3392namespace {
3393
3394class HexagonABIInfo : public ABIInfo {
3395
3396
3397public:
3398 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3399
3400private:
3401
3402 ABIArgInfo classifyReturnType(QualType RetTy) const;
3403 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3404
3405 virtual void computeInfo(CGFunctionInfo &FI) const;
3406
3407 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3408 CodeGenFunction &CGF) const;
3409};
3410
3411class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3412public:
3413 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3414 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3415
3416 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3417 return 29;
3418 }
3419};
3420
3421}
3422
3423void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3424 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3425 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3426 it != ie; ++it)
3427 it->info = classifyArgumentType(it->type);
3428}
3429
3430ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3431 if (!isAggregateTypeForABI(Ty)) {
3432 // Treat an enum type as its underlying type.
3433 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3434 Ty = EnumTy->getDecl()->getIntegerType();
3435
3436 return (Ty->isPromotableIntegerType() ?
3437 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3438 }
3439
3440 // Ignore empty records.
3441 if (isEmptyRecord(getContext(), Ty, true))
3442 return ABIArgInfo::getIgnore();
3443
3444 // Structures with either a non-trivial destructor or a non-trivial
3445 // copy constructor are always indirect.
3446 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3447 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3448
3449 uint64_t Size = getContext().getTypeSize(Ty);
3450 if (Size > 64)
3451 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3452 // Pass in the smallest viable integer type.
3453 else if (Size > 32)
3454 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3455 else if (Size > 16)
3456 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3457 else if (Size > 8)
3458 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3459 else
3460 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3461}
3462
3463ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3464 if (RetTy->isVoidType())
3465 return ABIArgInfo::getIgnore();
3466
3467 // Large vector types should be returned via memory.
3468 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3469 return ABIArgInfo::getIndirect(0);
3470
3471 if (!isAggregateTypeForABI(RetTy)) {
3472 // Treat an enum type as its underlying type.
3473 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3474 RetTy = EnumTy->getDecl()->getIntegerType();
3475
3476 return (RetTy->isPromotableIntegerType() ?
3477 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3478 }
3479
3480 // Structures with either a non-trivial destructor or a non-trivial
3481 // copy constructor are always indirect.
3482 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3483 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3484
3485 if (isEmptyRecord(getContext(), RetTy, true))
3486 return ABIArgInfo::getIgnore();
3487
3488 // Aggregates <= 8 bytes are returned in r0; other aggregates
3489 // are returned indirectly.
3490 uint64_t Size = getContext().getTypeSize(RetTy);
3491 if (Size <= 64) {
3492 // Return in the smallest viable integer type.
3493 if (Size <= 8)
3494 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3495 if (Size <= 16)
3496 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3497 if (Size <= 32)
3498 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3499 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3500 }
3501
3502 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3503}
3504
3505llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00003506 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00003507 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00003508 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00003509
3510 CGBuilderTy &Builder = CGF.Builder;
3511 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3512 "ap");
3513 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3514 llvm::Type *PTy =
3515 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3516 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3517
3518 uint64_t Offset =
3519 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3520 llvm::Value *NextAddr =
3521 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3522 "ap.next");
3523 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3524
3525 return AddrTyped;
3526}
3527
3528
Chris Lattnerea044322010-07-29 02:01:43 +00003529const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003530 if (TheTargetCodeGenInfo)
3531 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003532
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003533 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003534 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003535 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003536 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003537
John McCallaeeb7012010-05-27 06:19:26 +00003538 case llvm::Triple::mips:
3539 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003540 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003541
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003542 case llvm::Triple::mips64:
3543 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003544 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003545
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003546 case llvm::Triple::arm:
3547 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003548 {
3549 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003550
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003551 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003552 Kind = ARMABIInfo::APCS;
3553 else if (CodeGenOpts.FloatABI == "hard")
3554 Kind = ARMABIInfo::AAPCS_VFP;
3555
3556 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3557 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003558
John McCallec853ba2010-03-11 00:10:12 +00003559 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003560 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003561
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003562 case llvm::Triple::ptx32:
3563 case llvm::Triple::ptx64:
3564 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3565
Wesley Peck276fdf42010-12-19 19:57:51 +00003566 case llvm::Triple::mblaze:
3567 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3568
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003569 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003570 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003571
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003572 case llvm::Triple::tce:
3573 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3574
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003575 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003576 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003577
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003578 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003579 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003580 new X86_32TargetCodeGenInfo(
3581 Types, true, true, DisableMMX, false));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00003582
3583 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003584 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003585 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00003586 case llvm::Triple::AuroraUX:
3587 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00003588 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003589 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003590 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003591 new X86_32TargetCodeGenInfo(
3592 Types, false, true, DisableMMX, false));
3593
3594 case llvm::Triple::Win32:
3595 return *(TheTargetCodeGenInfo =
3596 new X86_32TargetCodeGenInfo(
3597 Types, false, true, DisableMMX, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003598
3599 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003600 return *(TheTargetCodeGenInfo =
Eli Friedman55fc7e22012-01-25 22:46:34 +00003601 new X86_32TargetCodeGenInfo(
3602 Types, false, false, DisableMMX, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003603 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00003604 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003605
Eli Friedmanee1ad992011-12-02 00:11:43 +00003606 case llvm::Triple::x86_64: {
3607 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3608
Chris Lattnerf13721d2010-08-31 16:44:54 +00003609 switch (Triple.getOS()) {
3610 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00003611 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00003612 case llvm::Triple::Cygwin:
3613 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3614 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00003615 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3616 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00003617 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003618 }
Tony Linthicum96319392011-12-12 21:14:55 +00003619 case llvm::Triple::hexagon:
3620 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00003621 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003622}