blob: ad59fd2c90860619ff624e4555442351067aa955 [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"
Micah Villmow25a6a842012-10-08 16:25:52 +000021#include "llvm/DataLayout.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
Micah Villmow25a6a842012-10-08 16:25:52 +000054const llvm::DataLayout &ABIInfo::getDataLayout() const {
55 return CGT.getDataLayout();
Chris Lattnerea044322010-07-29 02:01:43 +000056}
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
John McCallde5d3c72012-02-17 03:33:10 +0000101bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102 const FunctionNoProtoType *fnType) 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
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000112/// isEmptyField - Return true iff a the field is "empty", that is it
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000113/// 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
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000144/// isEmptyRecord - Return true iff a structure contains only empty
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000145/// 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)
David Blaikie581deb32012-06-06 20:45:41 +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) {
David Blaikie581deb32012-06-06 20:45:41 +0000232 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000233 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) {
David Blaikie581deb32012-06-06 20:45:41 +0000304 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305
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
Derek Schuff9ed63f82012-09-06 17:37:28 +0000392//===----------------------------------------------------------------------===//
393// le32/PNaCl bitcode ABI Implementation
394//===----------------------------------------------------------------------===//
395
396class PNaClABIInfo : public ABIInfo {
397 public:
398 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
399
400 ABIArgInfo classifyReturnType(QualType RetTy) const;
401 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
402
403 virtual void computeInfo(CGFunctionInfo &FI) const;
404 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
405 CodeGenFunction &CGF) const;
406};
407
408class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
409 public:
410 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
411 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
412};
413
414void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
415 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
416
417 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
418
419 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
420 it != ie; ++it)
421 it->info = classifyArgumentType(it->type, FreeRegs);
422 }
423
424llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
425 CodeGenFunction &CGF) const {
426 return 0;
427}
428
429ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
430 unsigned &FreeRegs) const {
431 if (isAggregateTypeForABI(Ty)) {
432 // Records with non trivial destructors/constructors should not be passed
433 // by value.
434 FreeRegs = 0;
435 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
436 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
437
438 return ABIArgInfo::getIndirect(0);
439 }
440
441 // Treat an enum type as its underlying type.
442 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
443 Ty = EnumTy->getDecl()->getIntegerType();
444
445 ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
446 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
447
448 // Regparm regs hold 32 bits.
449 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
450 if (SizeInRegs == 0) return BaseInfo;
451 if (SizeInRegs > FreeRegs) {
452 FreeRegs = 0;
453 return BaseInfo;
454 }
455 FreeRegs -= SizeInRegs;
456 return BaseInfo.isDirect() ?
457 ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
458 ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
459}
460
461ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
462 if (RetTy->isVoidType())
463 return ABIArgInfo::getIgnore();
464
465 if (isAggregateTypeForABI(RetTy))
466 return ABIArgInfo::getIndirect(0);
467
468 // Treat an enum type as its underlying type.
469 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
470 RetTy = EnumTy->getDecl()->getIntegerType();
471
472 return (RetTy->isPromotableIntegerType() ?
473 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
474}
475
Eli Friedman55fc7e22012-01-25 22:46:34 +0000476/// UseX86_MMXType - Return true if this is an MMX type that should use the
477/// special x86_mmx type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000478bool UseX86_MMXType(llvm::Type *IRType) {
Bill Wendlingbb465d72010-10-18 03:41:31 +0000479 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
480 // special x86_mmx type.
481 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
482 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
483 IRType->getScalarSizeInBits() != 64;
484}
485
Jay Foadef6de3d2011-07-11 09:56:20 +0000486static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000488 llvm::Type* Ty) {
Bill Wendling0507be62011-03-07 22:47:14 +0000489 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000490 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
491 return Ty;
492}
493
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000494//===----------------------------------------------------------------------===//
495// X86-32 ABI Implementation
496//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000497
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000498/// X86_32ABIInfo - The X86-32 ABI information.
499class X86_32ABIInfo : public ABIInfo {
Rafael Espindolab48280b2012-07-31 02:44:24 +0000500 enum Class {
501 Integer,
502 Float
503 };
504
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000505 static const unsigned MinABIStackAlignInBytes = 4;
506
David Chisnall1e4249c2009-08-17 23:08:21 +0000507 bool IsDarwinVectorABI;
508 bool IsSmallStructInRegABI;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000509 bool IsMMXDisabled;
Eli Friedman55fc7e22012-01-25 22:46:34 +0000510 bool IsWin32FloatStructABI;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000511 unsigned DefaultNumRegisterParameters;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000512
513 static bool isRegisterSize(unsigned Size) {
514 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
515 }
516
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000517 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
518 unsigned callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000520 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
521 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000522 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000523
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000524 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000525 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000526
Rafael Espindolab48280b2012-07-31 02:44:24 +0000527 Class classify(QualType Ty) const;
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000528 ABIArgInfo classifyReturnType(QualType RetTy,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000529 unsigned callingConvention) const;
Rafael Espindolab48280b2012-07-31 02:44:24 +0000530 ABIArgInfo classifyArgumentTypeWithReg(QualType RetTy,
531 unsigned &FreeRegs) const;
Chris Lattnera3c109b2010-07-29 02:16:43 +0000532 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000533
Rafael Espindolab33a3c42012-07-23 23:30:29 +0000534public:
535
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000536 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000537 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
538 CodeGenFunction &CGF) const;
539
Rafael Espindolab48280b2012-07-31 02:44:24 +0000540 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
541 unsigned r)
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000542 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
Rafael Espindolab48280b2012-07-31 02:44:24 +0000543 IsMMXDisabled(m), IsWin32FloatStructABI(w),
544 DefaultNumRegisterParameters(r) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000545};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000547class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
548public:
Eli Friedman55fc7e22012-01-25 22:46:34 +0000549 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
Rafael Espindolab48280b2012-07-31 02:44:24 +0000550 bool d, bool p, bool m, bool w, unsigned r)
551 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000552
553 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
554 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000555
556 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
557 // Darwin uses different dwarf register numbers for EH.
558 if (CGM.isTargetDarwin()) return 5;
559
560 return 4;
561 }
562
563 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
564 llvm::Value *Address) const;
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000565
Jay Foadef6de3d2011-07-11 09:56:20 +0000566 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000567 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +0000568 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +0000569 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
570 }
571
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000572};
573
574}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000575
576/// shouldReturnTypeInRegister - Determine if the given type should be
577/// passed in a register (for the Darwin ABI).
578bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000579 ASTContext &Context,
580 unsigned callingConvention) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000581 uint64_t Size = Context.getTypeSize(Ty);
582
583 // Type must be register sized.
584 if (!isRegisterSize(Size))
585 return false;
586
587 if (Ty->isVectorType()) {
588 // 64- and 128- bit vectors inside structures are not returned in
589 // registers.
590 if (Size == 64 || Size == 128)
591 return false;
592
593 return true;
594 }
595
Daniel Dunbar77115232010-05-15 00:00:30 +0000596 // If this is a builtin, pointer, enum, complex type, member pointer, or
597 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000598 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000599 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000600 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000601 return true;
602
603 // Arrays are treated like records.
604 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000605 return shouldReturnTypeInRegister(AT->getElementType(), Context,
606 callingConvention);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000607
608 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000609 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000610 if (!RT) return false;
611
Anders Carlssona8874232010-01-27 03:25:19 +0000612 // FIXME: Traverse bases here too.
613
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000614 // For thiscall conventions, structures will never be returned in
615 // a register. This is for compatibility with the MSVC ABI
616 if (callingConvention == llvm::CallingConv::X86_ThisCall &&
617 RT->isStructureType()) {
618 return false;
619 }
620
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000621 // Structure types are passed in register if all fields would be
622 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000623 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
624 e = RT->getDecl()->field_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000625 const FieldDecl *FD = *i;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000626
627 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000628 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000629 continue;
630
631 // Check fields recursively.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000632 if (!shouldReturnTypeInRegister(FD->getType(), Context,
633 callingConvention))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000634 return false;
635 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000636 return true;
637}
638
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000639ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
640 unsigned callingConvention) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000641 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000642 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000643
Chris Lattnera3c109b2010-07-29 02:16:43 +0000644 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000645 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000646 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000647 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000648
649 // 128-bit vectors are a special case; they are returned in
650 // registers and we need to make sure to pick a type the LLVM
651 // backend will like.
652 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000653 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000654 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000655
656 // Always return in register if it fits in a general purpose
657 // register, or if it is 64 bits and has a single element.
658 if ((Size == 8 || Size == 16 || Size == 32) ||
659 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000660 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000661 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000662
663 return ABIArgInfo::getIndirect(0);
664 }
665
666 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000667 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000668
John McCalld608cdb2010-08-22 10:59:02 +0000669 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000670 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000671 // Structures with either a non-trivial destructor or a non-trivial
672 // copy constructor are always indirect.
673 if (hasNonTrivialDestructorOrCopyConstructor(RT))
674 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000675
Anders Carlsson40092972009-10-20 22:07:59 +0000676 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677 if (RT->getDecl()->hasFlexibleArrayMember())
678 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000679 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000680
David Chisnall1e4249c2009-08-17 23:08:21 +0000681 // If specified, structs and unions are always indirect.
682 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000683 return ABIArgInfo::getIndirect(0);
684
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000685 // Small structures which are register sized are generally returned
686 // in a register.
Aaron Ballman6c60c8d2012-02-22 03:04:13 +0000687 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
688 callingConvention)) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000689 uint64_t Size = getContext().getTypeSize(RetTy);
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000690
691 // As a special-case, if the struct is a "single-element" struct, and
692 // the field is of type "float" or "double", return it in a
Eli Friedman55fc7e22012-01-25 22:46:34 +0000693 // floating-point register. (MSVC does not apply this special case.)
694 // We apply a similar transformation for pointer types to improve the
695 // quality of the generated IR.
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000696 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
Eli Friedman55fc7e22012-01-25 22:46:34 +0000697 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
698 || SeltTy->hasPointerRepresentation())
Eli Friedmanbd4d3bc2011-11-18 01:25:50 +0000699 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
700
701 // FIXME: We should be able to narrow this integer in cases with dead
702 // padding.
Chris Lattner800588f2010-07-29 06:26:06 +0000703 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000704 }
705
706 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000707 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000708
Chris Lattnera3c109b2010-07-29 02:16:43 +0000709 // Treat an enum type as its underlying type.
710 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
711 RetTy = EnumTy->getDecl()->getIntegerType();
712
713 return (RetTy->isPromotableIntegerType() ?
714 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000715}
716
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000717static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
718 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
719}
720
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000721static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
722 const RecordType *RT = Ty->getAs<RecordType>();
723 if (!RT)
724 return 0;
725 const RecordDecl *RD = RT->getDecl();
726
727 // If this is a C++ record, check the bases first.
728 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
729 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
730 e = CXXRD->bases_end(); i != e; ++i)
731 if (!isRecordWithSSEVectorType(Context, i->getType()))
732 return false;
733
734 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
735 i != e; ++i) {
736 QualType FT = i->getType();
737
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000738 if (isSSEVectorType(Context, FT))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000739 return true;
740
741 if (isRecordWithSSEVectorType(Context, FT))
742 return true;
743 }
744
745 return false;
746}
747
Daniel Dunbare59d8582010-09-16 20:42:06 +0000748unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
749 unsigned Align) const {
750 // Otherwise, if the alignment is less than or equal to the minimum ABI
751 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000752 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000753 return 0; // Use default alignment.
754
755 // On non-Darwin, the stack type alignment is always 4.
756 if (!IsDarwinVectorABI) {
757 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000758 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000759 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000760
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000761 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
Eli Friedmanf4bd4d82012-06-05 19:40:46 +0000762 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
763 isRecordWithSSEVectorType(getContext(), Ty)))
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000764 return 16;
765
766 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000767}
768
Chris Lattnera3c109b2010-07-29 02:16:43 +0000769ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000770 if (!ByVal)
771 return ABIArgInfo::getIndirect(0, false);
772
Daniel Dunbare59d8582010-09-16 20:42:06 +0000773 // Compute the byval alignment.
774 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
775 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
776 if (StackAlign == 0)
Chris Lattnerde92d732011-05-22 23:35:00 +0000777 return ABIArgInfo::getIndirect(4);
Daniel Dunbare59d8582010-09-16 20:42:06 +0000778
779 // If the stack alignment is less than the type alignment, realign the
780 // argument.
781 if (StackAlign < TypeAlign)
782 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
783 /*Realign=*/true);
784
785 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000786}
787
Rafael Espindolab48280b2012-07-31 02:44:24 +0000788X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
789 const Type *T = isSingleElementStruct(Ty, getContext());
790 if (!T)
791 T = Ty.getTypePtr();
792
793 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
794 BuiltinType::Kind K = BT->getKind();
795 if (K == BuiltinType::Float || K == BuiltinType::Double)
796 return Float;
797 }
798 return Integer;
799}
800
801ABIArgInfo
802X86_32ABIInfo::classifyArgumentTypeWithReg(QualType Ty,
803 unsigned &FreeRegs) const {
804 // Common case first.
805 if (FreeRegs == 0)
806 return classifyArgumentType(Ty);
807
808 Class C = classify(Ty);
809 if (C == Float)
810 return classifyArgumentType(Ty);
811
812 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
813 if (SizeInRegs == 0)
814 return classifyArgumentType(Ty);
815
816 if (SizeInRegs > FreeRegs) {
817 FreeRegs = 0;
818 return classifyArgumentType(Ty);
819 }
820 assert(SizeInRegs >= 1 && SizeInRegs <= 3);
821 FreeRegs -= SizeInRegs;
822
823 // If it is a simple scalar, keep the type so that we produce a cleaner IR.
824 ABIArgInfo Foo = classifyArgumentType(Ty);
825 if (Foo.isDirect() && !Foo.getDirectOffset() && !Foo.getPaddingType())
826 return ABIArgInfo::getDirectInReg(Foo.getCoerceToType());
827 if (Foo.isExtend())
828 return ABIArgInfo::getExtendInReg(Foo.getCoerceToType());
829
830 llvm::LLVMContext &LLVMContext = getVMContext();
831 llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
832 SmallVector<llvm::Type*, 3> Elements;
833 for (unsigned I = 0; I < SizeInRegs; ++I)
834 Elements.push_back(Int32);
835 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
836 return ABIArgInfo::getDirectInReg(Result);
837}
838
Chris Lattnera3c109b2010-07-29 02:16:43 +0000839ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000840 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000841 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000842 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000843 if (const RecordType *RT = Ty->getAs<RecordType>()) {
844 // Structures with either a non-trivial destructor or a non-trivial
845 // copy constructor are always indirect.
846 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000847 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000848
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000849 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000850 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000851 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000852
Eli Friedman5a4d3522011-11-18 00:28:11 +0000853 // Ignore empty structs/unions.
Eli Friedman5a1ac892011-11-18 04:01:36 +0000854 if (isEmptyRecord(getContext(), Ty, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000855 return ABIArgInfo::getIgnore();
856
Daniel Dunbar53012f42009-11-09 01:33:53 +0000857 // Expand small (<= 128-bit) record types when we know that the stack layout
858 // of those arguments will match the struct. This is important because the
859 // LLVM backend isn't smart enough to remove byval, which inhibits many
860 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000861 if (getContext().getTypeSize(Ty) <= 4*32 &&
862 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000863 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000864
Chris Lattnera3c109b2010-07-29 02:16:43 +0000865 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000866 }
867
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000868 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000869 // On Darwin, some vectors are passed in memory, we handle this by passing
870 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000871 if (IsDarwinVectorABI) {
872 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000873 if ((Size == 8 || Size == 16 || Size == 32) ||
874 (Size == 64 && VT->getNumElements() == 1))
875 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
876 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000877 }
Bill Wendlingbb465d72010-10-18 03:41:31 +0000878
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000879 llvm::Type *IRType = CGT.ConvertType(Ty);
Bill Wendlingbb465d72010-10-18 03:41:31 +0000880 if (UseX86_MMXType(IRType)) {
Eli Friedmanc3e0fb42011-07-08 23:31:17 +0000881 if (IsMMXDisabled)
882 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
883 64));
Bill Wendlingbb465d72010-10-18 03:41:31 +0000884 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
885 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
886 return AAI;
887 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000888
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000889 return ABIArgInfo::getDirect();
890 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000891
892
Chris Lattnera3c109b2010-07-29 02:16:43 +0000893 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
894 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000895
Chris Lattnera3c109b2010-07-29 02:16:43 +0000896 return (Ty->isPromotableIntegerType() ?
897 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000898}
899
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000900void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
901 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
902 FI.getCallingConvention());
Rafael Espindolab48280b2012-07-31 02:44:24 +0000903
904 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() :
905 DefaultNumRegisterParameters;
906
907 // If the return value is indirect, then the hidden argument is consuming one
908 // integer register.
909 if (FI.getReturnInfo().isIndirect() && FreeRegs) {
910 --FreeRegs;
911 ABIArgInfo &Old = FI.getReturnInfo();
912 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
913 Old.getIndirectByVal(),
914 Old.getIndirectRealign());
915 }
916
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000917 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
918 it != ie; ++it)
Rafael Espindolab48280b2012-07-31 02:44:24 +0000919 it->info = classifyArgumentTypeWithReg(it->type, FreeRegs);
Rafael Espindolaaa9cf8d2012-07-24 00:01:07 +0000920}
921
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000922llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
923 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +0000924 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000925
926 CGBuilderTy &Builder = CGF.Builder;
927 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
928 "ap");
929 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Eli Friedman7b1fb812011-11-18 02:12:09 +0000930
931 // Compute if the address needs to be aligned
932 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
933 Align = getTypeStackAlignInBytes(Ty, Align);
934 Align = std::max(Align, 4U);
935 if (Align > 4) {
936 // addr = (addr + align - 1) & -align;
937 llvm::Value *Offset =
938 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
939 Addr = CGF.Builder.CreateGEP(Addr, Offset);
940 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
941 CGF.Int32Ty);
942 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
943 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
944 Addr->getType(),
945 "ap.cur.aligned");
946 }
947
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000948 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000949 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000950 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
951
952 uint64_t Offset =
Eli Friedman7b1fb812011-11-18 02:12:09 +0000953 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000954 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000955 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000956 "ap.next");
957 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
958
959 return AddrTyped;
960}
961
Charles Davis74f72932010-02-13 15:54:06 +0000962void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
963 llvm::GlobalValue *GV,
964 CodeGen::CodeGenModule &CGM) const {
965 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
966 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
967 // Get the LLVM function.
968 llvm::Function *Fn = cast<llvm::Function>(GV);
969
970 // Now add the 'alignstack' attribute with a value of 16.
Bill Wendling0d583392012-10-15 20:36:26 +0000971 llvm::AttrBuilder B;
Bill Wendlinge91e9ec2012-10-14 03:28:14 +0000972 B.addStackAlignmentAttr(16);
Bill Wendling75d37b42012-10-15 07:31:59 +0000973 Fn->addAttribute(llvm::AttrListPtr::FunctionIndex,
974 llvm::Attributes::get(CGM.getLLVMContext(), B));
Charles Davis74f72932010-02-13 15:54:06 +0000975 }
976 }
977}
978
John McCall6374c332010-03-06 00:35:14 +0000979bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
980 CodeGen::CodeGenFunction &CGF,
981 llvm::Value *Address) const {
982 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000983
Chris Lattner8b418682012-02-07 00:39:47 +0000984 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000985
John McCall6374c332010-03-06 00:35:14 +0000986 // 0-7 are the eight integer registers; the order is different
987 // on Darwin (for EH), but the range is the same.
988 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000989 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000990
991 if (CGF.CGM.isTargetDarwin()) {
992 // 12-16 are st(0..4). Not sure why we stop at 4.
993 // These have size 16, which is sizeof(long double) on
994 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000995 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000996 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000997
John McCall6374c332010-03-06 00:35:14 +0000998 } else {
999 // 9 is %eflags, which doesn't get a size on Darwin for some
1000 // reason.
1001 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1002
1003 // 11-16 are st(0..5). Not sure why we stop at 5.
1004 // These have size 12, which is sizeof(long double) on
1005 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001006 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001007 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1008 }
John McCall6374c332010-03-06 00:35:14 +00001009
1010 return false;
1011}
1012
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001013//===----------------------------------------------------------------------===//
1014// X86-64 ABI Implementation
1015//===----------------------------------------------------------------------===//
1016
1017
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001018namespace {
1019/// X86_64ABIInfo - The X86_64 ABI information.
1020class X86_64ABIInfo : public ABIInfo {
1021 enum Class {
1022 Integer = 0,
1023 SSE,
1024 SSEUp,
1025 X87,
1026 X87Up,
1027 ComplexX87,
1028 NoClass,
1029 Memory
1030 };
1031
1032 /// merge - Implement the X86_64 ABI merging algorithm.
1033 ///
1034 /// Merge an accumulating classification \arg Accum with a field
1035 /// classification \arg Field.
1036 ///
1037 /// \param Accum - The accumulating classification. This should
1038 /// always be either NoClass or the result of a previous merge
1039 /// call. In addition, this should never be Memory (the caller
1040 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001041 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001042
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001043 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1044 ///
1045 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1046 /// final MEMORY or SSE classes when necessary.
1047 ///
1048 /// \param AggregateSize - The size of the current aggregate in
1049 /// the classification process.
1050 ///
1051 /// \param Lo - The classification for the parts of the type
1052 /// residing in the low word of the containing object.
1053 ///
1054 /// \param Hi - The classification for the parts of the type
1055 /// residing in the higher words of the containing object.
1056 ///
1057 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1058
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001059 /// classify - Determine the x86_64 register classes in which the
1060 /// given type T should be passed.
1061 ///
1062 /// \param Lo - The classification for the parts of the type
1063 /// residing in the low word of the containing object.
1064 ///
1065 /// \param Hi - The classification for the parts of the type
1066 /// residing in the high word of the containing object.
1067 ///
1068 /// \param OffsetBase - The bit offset of this type in the
1069 /// containing object. Some parameters are classified different
1070 /// depending on whether they straddle an eightbyte boundary.
1071 ///
1072 /// If a word is unused its result will be NoClass; if a type should
1073 /// be passed in Memory then at least the classification of \arg Lo
1074 /// will be Memory.
1075 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001076 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001077 ///
1078 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1079 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001080 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001081
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001082 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001083 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1084 unsigned IROffset, QualType SourceTy,
1085 unsigned SourceOffset) const;
1086 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1087 unsigned IROffset, QualType SourceTy,
1088 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001089
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001090 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001091 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001092 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001093
1094 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001095 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001096 ///
1097 /// \param freeIntRegs - The number of free integer registers remaining
1098 /// available.
1099 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100
Chris Lattnera3c109b2010-07-29 02:16:43 +00001101 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001102
Bill Wendlingbb465d72010-10-18 03:41:31 +00001103 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001104 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001105 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001106 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001107
Eli Friedmanee1ad992011-12-02 00:11:43 +00001108 bool IsIllegalVectorType(QualType Ty) const;
1109
John McCall67a57732011-04-21 01:20:55 +00001110 /// The 0.98 ABI revision clarified a lot of ambiguities,
1111 /// unfortunately in ways that were not always consistent with
1112 /// certain previous compilers. In particular, platforms which
1113 /// required strict binary compatibility with older versions of GCC
1114 /// may need to exempt themselves.
1115 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001116 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001117 }
1118
Eli Friedmanee1ad992011-12-02 00:11:43 +00001119 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001120 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1121 // 64-bit hardware.
1122 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001123
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001124public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001125 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001126 ABIInfo(CGT), HasAVX(hasavx),
Derek Schuff90da80c2012-10-11 18:21:13 +00001127 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001128 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001129
John McCallde5d3c72012-02-17 03:33:10 +00001130 bool isPassedUsingAVXType(QualType type) const {
1131 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001132 // The freeIntRegs argument doesn't matter here.
1133 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001134 if (info.isDirect()) {
1135 llvm::Type *ty = info.getCoerceToType();
1136 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1137 return (vectorTy->getBitWidth() > 128);
1138 }
1139 return false;
1140 }
1141
Chris Lattneree5dcd02010-07-29 02:31:05 +00001142 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001143
1144 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1145 CodeGenFunction &CGF) const;
1146};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001147
Chris Lattnerf13721d2010-08-31 16:44:54 +00001148/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001149class WinX86_64ABIInfo : public ABIInfo {
1150
1151 ABIArgInfo classify(QualType Ty) const;
1152
Chris Lattnerf13721d2010-08-31 16:44:54 +00001153public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001154 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1155
1156 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001157
1158 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1159 CodeGenFunction &CGF) const;
1160};
1161
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001162class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1163public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001164 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001165 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001166
John McCallde5d3c72012-02-17 03:33:10 +00001167 const X86_64ABIInfo &getABIInfo() const {
1168 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1169 }
1170
John McCall6374c332010-03-06 00:35:14 +00001171 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1172 return 7;
1173 }
1174
1175 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1176 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001177 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001178
John McCallaeeb7012010-05-27 06:19:26 +00001179 // 0-15 are the 16 integer registers.
1180 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001181 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001182 return false;
1183 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001184
Jay Foadef6de3d2011-07-11 09:56:20 +00001185 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001186 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001187 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001188 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1189 }
1190
John McCallde5d3c72012-02-17 03:33:10 +00001191 bool isNoProtoCallVariadic(const CallArgList &args,
1192 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001193 // The default CC on x86-64 sets %al to the number of SSA
1194 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001195 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001196 // that when AVX types are involved: the ABI explicitly states it is
1197 // undefined, and it doesn't work in practice because of how the ABI
1198 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001199 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001200 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001201 for (CallArgList::const_iterator
1202 it = args.begin(), ie = args.end(); it != ie; ++it) {
1203 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1204 HasAVXType = true;
1205 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001206 }
1207 }
John McCallde5d3c72012-02-17 03:33:10 +00001208
Eli Friedman3ed79032011-12-01 04:53:19 +00001209 if (!HasAVXType)
1210 return true;
1211 }
John McCall01f151e2011-09-21 08:08:30 +00001212
John McCallde5d3c72012-02-17 03:33:10 +00001213 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001214 }
1215
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001216};
1217
Chris Lattnerf13721d2010-08-31 16:44:54 +00001218class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1219public:
1220 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1221 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1222
1223 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1224 return 7;
1225 }
1226
1227 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1228 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001229 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001230
Chris Lattnerf13721d2010-08-31 16:44:54 +00001231 // 0-15 are the 16 integer registers.
1232 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001233 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001234 return false;
1235 }
1236};
1237
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001238}
1239
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001240void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1241 Class &Hi) const {
1242 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1243 //
1244 // (a) If one of the classes is Memory, the whole argument is passed in
1245 // memory.
1246 //
1247 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1248 // memory.
1249 //
1250 // (c) If the size of the aggregate exceeds two eightbytes and the first
1251 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1252 // argument is passed in memory. NOTE: This is necessary to keep the
1253 // ABI working for processors that don't support the __m256 type.
1254 //
1255 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1256 //
1257 // Some of these are enforced by the merging logic. Others can arise
1258 // only with unions; for example:
1259 // union { _Complex double; unsigned; }
1260 //
1261 // Note that clauses (b) and (c) were added in 0.98.
1262 //
1263 if (Hi == Memory)
1264 Lo = Memory;
1265 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1266 Lo = Memory;
1267 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1268 Lo = Memory;
1269 if (Hi == SSEUp && Lo != SSE)
1270 Hi = SSE;
1271}
1272
Chris Lattner1090a9b2010-06-28 21:43:59 +00001273X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001274 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1275 // classified recursively so that always two fields are
1276 // considered. The resulting class is calculated according to
1277 // the classes of the fields in the eightbyte:
1278 //
1279 // (a) If both classes are equal, this is the resulting class.
1280 //
1281 // (b) If one of the classes is NO_CLASS, the resulting class is
1282 // the other class.
1283 //
1284 // (c) If one of the classes is MEMORY, the result is the MEMORY
1285 // class.
1286 //
1287 // (d) If one of the classes is INTEGER, the result is the
1288 // INTEGER.
1289 //
1290 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1291 // MEMORY is used as class.
1292 //
1293 // (f) Otherwise class SSE is used.
1294
1295 // Accum should never be memory (we should have returned) or
1296 // ComplexX87 (because this cannot be passed in a structure).
1297 assert((Accum != Memory && Accum != ComplexX87) &&
1298 "Invalid accumulated classification during merge.");
1299 if (Accum == Field || Field == NoClass)
1300 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001301 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001302 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001303 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001304 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001305 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001306 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001307 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1308 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001310 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001311}
1312
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001313void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001314 Class &Lo, Class &Hi) const {
1315 // FIXME: This code can be simplified by introducing a simple value class for
1316 // Class pairs with appropriate constructor methods for the various
1317 // situations.
1318
1319 // FIXME: Some of the split computations are wrong; unaligned vectors
1320 // shouldn't be passed in registers for example, so there is no chance they
1321 // can straddle an eightbyte. Verify & simplify.
1322
1323 Lo = Hi = NoClass;
1324
1325 Class &Current = OffsetBase < 64 ? Lo : Hi;
1326 Current = Memory;
1327
John McCall183700f2009-09-21 23:43:11 +00001328 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001329 BuiltinType::Kind k = BT->getKind();
1330
1331 if (k == BuiltinType::Void) {
1332 Current = NoClass;
1333 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1334 Lo = Integer;
1335 Hi = Integer;
1336 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1337 Current = Integer;
Derek Schuff7da46f92012-10-11 16:55:58 +00001338 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1339 (k == BuiltinType::LongDouble &&
1340 getContext().getTargetInfo().getTriple().getOS() ==
1341 llvm::Triple::NativeClient)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001342 Current = SSE;
1343 } else if (k == BuiltinType::LongDouble) {
1344 Lo = X87;
1345 Hi = X87Up;
1346 }
1347 // FIXME: _Decimal32 and _Decimal64 are SSE.
1348 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001349 return;
1350 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001351
Chris Lattner1090a9b2010-06-28 21:43:59 +00001352 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001353 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001354 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001355 return;
1356 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001357
Chris Lattner1090a9b2010-06-28 21:43:59 +00001358 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001359 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001360 return;
1361 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001362
Chris Lattner1090a9b2010-06-28 21:43:59 +00001363 if (Ty->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001364 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001365 Lo = Hi = Integer;
1366 else
1367 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001368 return;
1369 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001370
Chris Lattner1090a9b2010-06-28 21:43:59 +00001371 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001372 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001373 if (Size == 32) {
1374 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1375 // float> as integer.
1376 Current = Integer;
1377
1378 // If this type crosses an eightbyte boundary, it should be
1379 // split.
1380 uint64_t EB_Real = (OffsetBase) / 64;
1381 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1382 if (EB_Real != EB_Imag)
1383 Hi = Lo;
1384 } else if (Size == 64) {
1385 // gcc passes <1 x double> in memory. :(
1386 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1387 return;
1388
1389 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001390 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001391 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1392 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1393 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001394 Current = Integer;
1395 else
1396 Current = SSE;
1397
1398 // If this type crosses an eightbyte boundary, it should be
1399 // split.
1400 if (OffsetBase && OffsetBase != 64)
1401 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001402 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001403 // Arguments of 256-bits are split into four eightbyte chunks. The
1404 // least significant one belongs to class SSE and all the others to class
1405 // SSEUP. The original Lo and Hi design considers that types can't be
1406 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1407 // This design isn't correct for 256-bits, but since there're no cases
1408 // where the upper parts would need to be inspected, avoid adding
1409 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 Lo = SSE;
1411 Hi = SSEUp;
1412 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001413 return;
1414 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001415
Chris Lattner1090a9b2010-06-28 21:43:59 +00001416 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001417 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001418
Chris Lattnerea044322010-07-29 02:01:43 +00001419 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001420 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421 if (Size <= 64)
1422 Current = Integer;
1423 else if (Size <= 128)
1424 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001425 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001426 Current = SSE;
Derek Schuff7da46f92012-10-11 16:55:58 +00001427 else if (ET == getContext().DoubleTy ||
1428 (ET == getContext().LongDoubleTy &&
1429 getContext().getTargetInfo().getTriple().getOS() ==
1430 llvm::Triple::NativeClient))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001431 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001432 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001433 Current = ComplexX87;
1434
1435 // If this complex type crosses an eightbyte boundary then it
1436 // should be split.
1437 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001438 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001439 if (Hi == NoClass && EB_Real != EB_Imag)
1440 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001441
Chris Lattner1090a9b2010-06-28 21:43:59 +00001442 return;
1443 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001444
Chris Lattnerea044322010-07-29 02:01:43 +00001445 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001446 // Arrays are treated like structures.
1447
Chris Lattnerea044322010-07-29 02:01:43 +00001448 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001449
1450 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001451 // than four eightbytes, ..., it has class MEMORY.
1452 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001453 return;
1454
1455 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1456 // fields, it has class MEMORY.
1457 //
1458 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001459 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001460 return;
1461
1462 // Otherwise implement simplified merge. We could be smarter about
1463 // this, but it isn't worth it and would be harder to verify.
1464 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001465 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001466 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001467
1468 // The only case a 256-bit wide vector could be used is when the array
1469 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1470 // to work for sizes wider than 128, early check and fallback to memory.
1471 if (Size > 128 && EltSize != 256)
1472 return;
1473
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001474 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1475 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001476 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001477 Lo = merge(Lo, FieldLo);
1478 Hi = merge(Hi, FieldHi);
1479 if (Lo == Memory || Hi == Memory)
1480 break;
1481 }
1482
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001483 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001484 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001485 return;
1486 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001487
Chris Lattner1090a9b2010-06-28 21:43:59 +00001488 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001489 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001490
1491 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001492 // than four eightbytes, ..., it has class MEMORY.
1493 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001494 return;
1495
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001496 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1497 // copy constructor or a non-trivial destructor, it is passed by invisible
1498 // reference.
1499 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1500 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001501
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001502 const RecordDecl *RD = RT->getDecl();
1503
1504 // Assume variable sized types are passed in memory.
1505 if (RD->hasFlexibleArrayMember())
1506 return;
1507
Chris Lattnerea044322010-07-29 02:01:43 +00001508 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001509
1510 // Reset Lo class, this will be recomputed.
1511 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001512
1513 // If this is a C++ record, classify the bases first.
1514 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1515 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1516 e = CXXRD->bases_end(); i != e; ++i) {
1517 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1518 "Unexpected base class!");
1519 const CXXRecordDecl *Base =
1520 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1521
1522 // Classify this field.
1523 //
1524 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1525 // single eightbyte, each is classified separately. Each eightbyte gets
1526 // initialized to class NO_CLASS.
1527 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001528 uint64_t Offset =
1529 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001530 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001531 Lo = merge(Lo, FieldLo);
1532 Hi = merge(Hi, FieldHi);
1533 if (Lo == Memory || Hi == Memory)
1534 break;
1535 }
1536 }
1537
1538 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001539 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001540 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001541 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001542 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1543 bool BitField = i->isBitField();
1544
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001545 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1546 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001548 // The only case a 256-bit wide vector could be used is when the struct
1549 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1550 // to work for sizes wider than 128, early check and fallback to memory.
1551 //
1552 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1553 Lo = Memory;
1554 return;
1555 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001556 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001557 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001558 Lo = Memory;
1559 return;
1560 }
1561
1562 // Classify this field.
1563 //
1564 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1565 // exceeds a single eightbyte, each is classified
1566 // separately. Each eightbyte gets initialized to class
1567 // NO_CLASS.
1568 Class FieldLo, FieldHi;
1569
1570 // Bit-fields require special handling, they do not force the
1571 // structure to be passed in memory even if unaligned, and
1572 // therefore they can straddle an eightbyte.
1573 if (BitField) {
1574 // Ignore padding bit-fields.
1575 if (i->isUnnamedBitfield())
1576 continue;
1577
1578 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001579 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001580
1581 uint64_t EB_Lo = Offset / 64;
1582 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1583 FieldLo = FieldHi = NoClass;
1584 if (EB_Lo) {
1585 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1586 FieldLo = NoClass;
1587 FieldHi = Integer;
1588 } else {
1589 FieldLo = Integer;
1590 FieldHi = EB_Hi ? Integer : NoClass;
1591 }
1592 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001593 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001594 Lo = merge(Lo, FieldLo);
1595 Hi = merge(Hi, FieldHi);
1596 if (Lo == Memory || Hi == Memory)
1597 break;
1598 }
1599
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001600 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001601 }
1602}
1603
Chris Lattner9c254f02010-06-29 06:01:59 +00001604ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001605 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1606 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001607 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001608 // Treat an enum type as its underlying type.
1609 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1610 Ty = EnumTy->getDecl()->getIntegerType();
1611
1612 return (Ty->isPromotableIntegerType() ?
1613 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1614 }
1615
1616 return ABIArgInfo::getIndirect(0);
1617}
1618
Eli Friedmanee1ad992011-12-02 00:11:43 +00001619bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1620 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1621 uint64_t Size = getContext().getTypeSize(VecTy);
1622 unsigned LargestVector = HasAVX ? 256 : 128;
1623 if (Size <= 64 || Size > LargestVector)
1624 return true;
1625 }
1626
1627 return false;
1628}
1629
Daniel Dunbaredfac032012-03-10 01:03:58 +00001630ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1631 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001632 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1633 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001634 //
1635 // This assumption is optimistic, as there could be free registers available
1636 // when we need to pass this argument in memory, and LLVM could try to pass
1637 // the argument in the free register. This does not seem to happen currently,
1638 // but this code would be much safer if we could mark the argument with
1639 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001640 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001641 // Treat an enum type as its underlying type.
1642 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1643 Ty = EnumTy->getDecl()->getIntegerType();
1644
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001645 return (Ty->isPromotableIntegerType() ?
1646 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001647 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001648
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001649 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1650 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001651
Chris Lattner855d2272011-05-22 23:21:23 +00001652 // Compute the byval alignment. We specify the alignment of the byval in all
1653 // cases so that the mid-level optimizer knows the alignment of the byval.
1654 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001655
1656 // Attempt to avoid passing indirect results using byval when possible. This
1657 // is important for good codegen.
1658 //
1659 // We do this by coercing the value into a scalar type which the backend can
1660 // handle naturally (i.e., without using byval).
1661 //
1662 // For simplicity, we currently only do this when we have exhausted all of the
1663 // free integer registers. Doing this when there are free integer registers
1664 // would require more care, as we would have to ensure that the coerced value
1665 // did not claim the unused register. That would require either reording the
1666 // arguments to the function (so that any subsequent inreg values came first),
1667 // or only doing this optimization when there were no following arguments that
1668 // might be inreg.
1669 //
1670 // We currently expect it to be rare (particularly in well written code) for
1671 // arguments to be passed on the stack when there are still free integer
1672 // registers available (this would typically imply large structs being passed
1673 // by value), so this seems like a fair tradeoff for now.
1674 //
1675 // We can revisit this if the backend grows support for 'onstack' parameter
1676 // attributes. See PR12193.
1677 if (freeIntRegs == 0) {
1678 uint64_t Size = getContext().getTypeSize(Ty);
1679
1680 // If this type fits in an eightbyte, coerce it into the matching integral
1681 // type, which will end up on the stack (with alignment 8).
1682 if (Align == 8 && Size <= 64)
1683 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1684 Size));
1685 }
1686
Chris Lattner855d2272011-05-22 23:21:23 +00001687 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001688}
1689
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001690/// GetByteVectorType - The ABI specifies that a value should be passed in an
1691/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001692/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001693llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001694 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001695
Chris Lattner15842bd2010-07-29 05:02:29 +00001696 // Wrapper structs that just contain vectors are passed just like vectors,
1697 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001698 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001699 while (STy && STy->getNumElements() == 1) {
1700 IRType = STy->getElementType(0);
1701 STy = dyn_cast<llvm::StructType>(IRType);
1702 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001703
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001704 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001705 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1706 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001707 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001708 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001709 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1710 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1711 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1712 EltTy->isIntegerTy(128)))
1713 return VT;
1714 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001715
Chris Lattner0f408f52010-07-29 04:56:46 +00001716 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1717}
1718
Chris Lattnere2962be2010-07-29 07:30:00 +00001719/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1720/// is known to either be off the end of the specified type or being in
1721/// alignment padding. The user type specified is known to be at most 128 bits
1722/// in size, and have passed through X86_64ABIInfo::classify with a successful
1723/// classification that put one of the two halves in the INTEGER class.
1724///
1725/// It is conservatively correct to return false.
1726static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1727 unsigned EndBit, ASTContext &Context) {
1728 // If the bytes being queried are off the end of the type, there is no user
1729 // data hiding here. This handles analysis of builtins, vectors and other
1730 // types that don't contain interesting padding.
1731 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1732 if (TySize <= StartBit)
1733 return true;
1734
Chris Lattner021c3a32010-07-29 07:43:55 +00001735 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1736 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1737 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1738
1739 // Check each element to see if the element overlaps with the queried range.
1740 for (unsigned i = 0; i != NumElts; ++i) {
1741 // If the element is after the span we care about, then we're done..
1742 unsigned EltOffset = i*EltSize;
1743 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001744
Chris Lattner021c3a32010-07-29 07:43:55 +00001745 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1746 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1747 EndBit-EltOffset, Context))
1748 return false;
1749 }
1750 // If it overlaps no elements, then it is safe to process as padding.
1751 return true;
1752 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001753
Chris Lattnere2962be2010-07-29 07:30:00 +00001754 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1755 const RecordDecl *RD = RT->getDecl();
1756 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001757
Chris Lattnere2962be2010-07-29 07:30:00 +00001758 // If this is a C++ record, check the bases first.
1759 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1760 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1761 e = CXXRD->bases_end(); i != e; ++i) {
1762 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1763 "Unexpected base class!");
1764 const CXXRecordDecl *Base =
1765 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001766
Chris Lattnere2962be2010-07-29 07:30:00 +00001767 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001768 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001769 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001770
Chris Lattnere2962be2010-07-29 07:30:00 +00001771 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1772 if (!BitsContainNoUserData(i->getType(), BaseStart,
1773 EndBit-BaseOffset, Context))
1774 return false;
1775 }
1776 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001777
Chris Lattnere2962be2010-07-29 07:30:00 +00001778 // Verify that no field has data that overlaps the region of interest. Yes
1779 // this could be sped up a lot by being smarter about queried fields,
1780 // however we're only looking at structs up to 16 bytes, so we don't care
1781 // much.
1782 unsigned idx = 0;
1783 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1784 i != e; ++i, ++idx) {
1785 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001786
Chris Lattnere2962be2010-07-29 07:30:00 +00001787 // If we found a field after the region we care about, then we're done.
1788 if (FieldOffset >= EndBit) break;
1789
1790 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1791 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1792 Context))
1793 return false;
1794 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001795
Chris Lattnere2962be2010-07-29 07:30:00 +00001796 // If nothing in this record overlapped the area of interest, then we're
1797 // clean.
1798 return true;
1799 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001800
Chris Lattnere2962be2010-07-29 07:30:00 +00001801 return false;
1802}
1803
Chris Lattner0b362002010-07-29 18:39:32 +00001804/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1805/// float member at the specified offset. For example, {int,{float}} has a
1806/// float at offset 4. It is conservatively correct for this routine to return
1807/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001808static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001809 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001810 // Base case if we find a float.
1811 if (IROffset == 0 && IRType->isFloatTy())
1812 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001813
Chris Lattner0b362002010-07-29 18:39:32 +00001814 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001815 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001816 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1817 unsigned Elt = SL->getElementContainingOffset(IROffset);
1818 IROffset -= SL->getElementOffset(Elt);
1819 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1820 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001821
Chris Lattner0b362002010-07-29 18:39:32 +00001822 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001823 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1824 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001825 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1826 IROffset -= IROffset/EltSize*EltSize;
1827 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1828 }
1829
1830 return false;
1831}
1832
Chris Lattnerf47c9442010-07-29 18:13:09 +00001833
1834/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1835/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001836llvm::Type *X86_64ABIInfo::
1837GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001838 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001839 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001840 // pass as float if the last 4 bytes is just padding. This happens for
1841 // structs that contain 3 floats.
1842 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1843 SourceOffset*8+64, getContext()))
1844 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001845
Chris Lattner0b362002010-07-29 18:39:32 +00001846 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1847 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1848 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001849 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1850 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001851 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001852
Chris Lattnerf47c9442010-07-29 18:13:09 +00001853 return llvm::Type::getDoubleTy(getVMContext());
1854}
1855
1856
Chris Lattner0d2656d2010-07-29 17:40:35 +00001857/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1858/// an 8-byte GPR. This means that we either have a scalar or we are talking
1859/// about the high or low part of an up-to-16-byte struct. This routine picks
1860/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001861/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1862/// etc).
1863///
1864/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1865/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1866/// the 8-byte value references. PrefType may be null.
1867///
1868/// SourceTy is the source level type for the entire argument. SourceOffset is
1869/// an offset into this that we're processing (which is always either 0 or 8).
1870///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001871llvm::Type *X86_64ABIInfo::
1872GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001873 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001874 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1875 // returning an 8-byte unit starting with it. See if we can safely use it.
1876 if (IROffset == 0) {
1877 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001878 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1879 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001880 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001881
Chris Lattnere2962be2010-07-29 07:30:00 +00001882 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1883 // goodness in the source type is just tail padding. This is allowed to
1884 // kick in for struct {double,int} on the int, but not on
1885 // struct{double,int,int} because we wouldn't return the second int. We
1886 // have to do this analysis on the source type because we can't depend on
1887 // unions being lowered a specific way etc.
1888 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001889 IRType->isIntegerTy(32) ||
1890 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1891 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1892 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001893
Chris Lattnere2962be2010-07-29 07:30:00 +00001894 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1895 SourceOffset*8+64, getContext()))
1896 return IRType;
1897 }
1898 }
Chris Lattner49382de2010-07-28 22:44:07 +00001899
Chris Lattner2acc6e32011-07-18 04:24:23 +00001900 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001901 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001902 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001903 if (IROffset < SL->getSizeInBytes()) {
1904 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1905 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001906
Chris Lattner0d2656d2010-07-29 17:40:35 +00001907 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1908 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001909 }
Chris Lattner49382de2010-07-28 22:44:07 +00001910 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001911
Chris Lattner2acc6e32011-07-18 04:24:23 +00001912 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001913 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001914 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001915 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001916 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1917 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001918 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001919
Chris Lattner49382de2010-07-28 22:44:07 +00001920 // Okay, we don't have any better idea of what to pass, so we pass this in an
1921 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001922 unsigned TySizeInBytes =
1923 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001924
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001925 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001926
Chris Lattner49382de2010-07-28 22:44:07 +00001927 // It is always safe to classify this as an integer type up to i64 that
1928 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001929 return llvm::IntegerType::get(getVMContext(),
1930 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001931}
1932
Chris Lattner66e7b682010-09-01 00:50:20 +00001933
1934/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1935/// be used as elements of a two register pair to pass or return, return a
1936/// first class aggregate to represent them. For example, if the low part of
1937/// a by-value argument should be passed as i32* and the high part as float,
1938/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001939static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001940GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001941 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001942 // In order to correctly satisfy the ABI, we need to the high part to start
1943 // at offset 8. If the high and low parts we inferred are both 4-byte types
1944 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1945 // the second element at offset 8. Check for this:
1946 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1947 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001948 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001949 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001950
Chris Lattner66e7b682010-09-01 00:50:20 +00001951 // To handle this, we have to increase the size of the low part so that the
1952 // second element will start at an 8 byte offset. We can't increase the size
1953 // of the second element because it might make us access off the end of the
1954 // struct.
1955 if (HiStart != 8) {
1956 // There are only two sorts of types the ABI generation code can produce for
1957 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1958 // Promote these to a larger type.
1959 if (Lo->isFloatTy())
1960 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1961 else {
1962 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1963 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1964 }
1965 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001966
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001967 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001968
1969
Chris Lattner66e7b682010-09-01 00:50:20 +00001970 // Verify that the second element is at an 8-byte offset.
1971 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1972 "Invalid x86-64 argument pair!");
1973 return Result;
1974}
1975
Chris Lattner519f68c2010-07-28 23:06:14 +00001976ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001977classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001978 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1979 // classification algorithm.
1980 X86_64ABIInfo::Class Lo, Hi;
1981 classify(RetTy, 0, Lo, Hi);
1982
1983 // Check some invariants.
1984 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001985 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1986
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001987 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001988 switch (Lo) {
1989 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001990 if (Hi == NoClass)
1991 return ABIArgInfo::getIgnore();
1992 // If the low part is just padding, it takes no register, leave ResType
1993 // null.
1994 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1995 "Unknown missing lo part");
1996 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001997
1998 case SSEUp:
1999 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002000 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002001
2002 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2003 // hidden argument.
2004 case Memory:
2005 return getIndirectReturnResult(RetTy);
2006
2007 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2008 // available register of the sequence %rax, %rdx is used.
2009 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002010 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002011
Chris Lattnereb518b42010-07-29 21:42:50 +00002012 // If we have a sign or zero extended integer, make sure to return Extend
2013 // so that the parameter gets the right LLVM IR attributes.
2014 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2015 // Treat an enum type as its underlying type.
2016 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2017 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002018
Chris Lattnereb518b42010-07-29 21:42:50 +00002019 if (RetTy->isIntegralOrEnumerationType() &&
2020 RetTy->isPromotableIntegerType())
2021 return ABIArgInfo::getExtend();
2022 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002023 break;
2024
2025 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2026 // available SSE register of the sequence %xmm0, %xmm1 is used.
2027 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002028 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002029 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002030
2031 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2032 // returned on the X87 stack in %st0 as 80-bit x87 number.
2033 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002034 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002035 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002036
2037 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2038 // part of the value is returned in %st0 and the imaginary part in
2039 // %st1.
2040 case ComplexX87:
2041 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002042 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002043 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002044 NULL);
2045 break;
2046 }
2047
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002048 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002049 switch (Hi) {
2050 // Memory was handled previously and X87 should
2051 // never occur as a hi class.
2052 case Memory:
2053 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002054 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002055
2056 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002057 case NoClass:
2058 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002059
Chris Lattner3db4dde2010-09-01 00:20:33 +00002060 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002061 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002062 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2063 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002064 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002065 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002066 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002067 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2068 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002069 break;
2070
2071 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002072 // is passed in the next available eightbyte chunk if the last used
2073 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002074 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002075 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002076 case SSEUp:
2077 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002078 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002079 break;
2080
2081 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2082 // returned together with the previous X87 value in %st0.
2083 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002084 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002085 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002086 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002087 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002088 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002089 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002090 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2091 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002092 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002093 break;
2094 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002095
Chris Lattner3db4dde2010-09-01 00:20:33 +00002096 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002097 // known to pass in the high eightbyte of the result. We do this by forming a
2098 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002099 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002100 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002101
Chris Lattnereb518b42010-07-29 21:42:50 +00002102 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002103}
2104
Daniel Dunbaredfac032012-03-10 01:03:58 +00002105ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2106 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2107 const
2108{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002109 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002110 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002111
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112 // Check some invariants.
2113 // FIXME: Enforce these by construction.
2114 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002115 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2116
2117 neededInt = 0;
2118 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002119 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002120 switch (Lo) {
2121 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002122 if (Hi == NoClass)
2123 return ABIArgInfo::getIgnore();
2124 // If the low part is just padding, it takes no register, leave ResType
2125 // null.
2126 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2127 "Unknown missing lo part");
2128 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002129
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002130 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2131 // on the stack.
2132 case Memory:
2133
2134 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2135 // COMPLEX_X87, it is passed in memory.
2136 case X87:
2137 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002138 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2139 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002140 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002141
2142 case SSEUp:
2143 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002144 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002145
2146 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2147 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2148 // and %r9 is used.
2149 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002150 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002151
Chris Lattner49382de2010-07-28 22:44:07 +00002152 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002153 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002154
2155 // If we have a sign or zero extended integer, make sure to return Extend
2156 // so that the parameter gets the right LLVM IR attributes.
2157 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2158 // Treat an enum type as its underlying type.
2159 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2160 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002161
Chris Lattnereb518b42010-07-29 21:42:50 +00002162 if (Ty->isIntegralOrEnumerationType() &&
2163 Ty->isPromotableIntegerType())
2164 return ABIArgInfo::getExtend();
2165 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002166
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002167 break;
2168
2169 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2170 // available SSE register is used, the registers are taken in the
2171 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002172 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002173 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002174 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002175 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002176 break;
2177 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002178 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002179
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002180 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181 switch (Hi) {
2182 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002183 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002184 // which is passed in memory.
2185 case Memory:
2186 case X87:
2187 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002188 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002189
2190 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002191
Chris Lattner645406a2010-09-01 00:24:35 +00002192 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002193 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002194 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002195 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002196
Chris Lattner645406a2010-09-01 00:24:35 +00002197 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2198 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002199 break;
2200
2201 // X87Up generally doesn't occur here (long double is passed in
2202 // memory), except in situations involving unions.
2203 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002204 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002205 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002206
Chris Lattner645406a2010-09-01 00:24:35 +00002207 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2208 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002209
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002210 ++neededSSE;
2211 break;
2212
2213 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2214 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002215 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002216 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002217 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002218 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002219 break;
2220 }
2221
Chris Lattner645406a2010-09-01 00:24:35 +00002222 // If a high part was specified, merge it together with the low part. It is
2223 // known to pass in the high eightbyte of the result. We do this by forming a
2224 // first class struct aggregate with the high and low part: {low, high}
2225 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002226 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002227
Chris Lattnereb518b42010-07-29 21:42:50 +00002228 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002229}
2230
Chris Lattneree5dcd02010-07-29 02:31:05 +00002231void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002232
Chris Lattnera3c109b2010-07-29 02:16:43 +00002233 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002234
2235 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002236 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002237
2238 // If the return value is indirect, then the hidden argument is consuming one
2239 // integer register.
2240 if (FI.getReturnInfo().isIndirect())
2241 --freeIntRegs;
2242
2243 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2244 // get assigned (in left-to-right order) for passing as follows...
2245 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2246 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002247 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002248 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2249 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002250
2251 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2252 // eightbyte of an argument, the whole argument is passed on the
2253 // stack. If registers have already been assigned for some
2254 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002255 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002256 freeIntRegs -= neededInt;
2257 freeSSERegs -= neededSSE;
2258 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002259 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002260 }
2261 }
2262}
2263
2264static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2265 QualType Ty,
2266 CodeGenFunction &CGF) {
2267 llvm::Value *overflow_arg_area_p =
2268 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2269 llvm::Value *overflow_arg_area =
2270 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2271
2272 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2273 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002274 // It isn't stated explicitly in the standard, but in practice we use
2275 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002276 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2277 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002278 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002279 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002280 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002281 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2282 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002283 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002284 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002285 overflow_arg_area =
2286 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2287 overflow_arg_area->getType(),
2288 "overflow_arg_area.align");
2289 }
2290
2291 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002292 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002293 llvm::Value *Res =
2294 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002295 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002296
2297 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2298 // l->overflow_arg_area + sizeof(type).
2299 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2300 // an 8 byte boundary.
2301
2302 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002303 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002304 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002305 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2306 "overflow_arg_area.next");
2307 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2308
2309 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2310 return Res;
2311}
2312
2313llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2314 CodeGenFunction &CGF) const {
2315 // Assume that va_list type is correct; should be pointer to LLVM type:
2316 // struct {
2317 // i32 gp_offset;
2318 // i32 fp_offset;
2319 // i8* overflow_arg_area;
2320 // i8* reg_save_area;
2321 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002322 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002323
Chris Lattnera14db752010-03-11 18:19:55 +00002324 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002325 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002326
2327 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2328 // in the registers. If not go to step 7.
2329 if (!neededInt && !neededSSE)
2330 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2331
2332 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2333 // general purpose registers needed to pass type and num_fp to hold
2334 // the number of floating point registers needed.
2335
2336 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2337 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2338 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2339 //
2340 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2341 // register save space).
2342
2343 llvm::Value *InRegs = 0;
2344 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2345 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2346 if (neededInt) {
2347 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2348 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002349 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2350 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002351 }
2352
2353 if (neededSSE) {
2354 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2355 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2356 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002357 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2358 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002359 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2360 }
2361
2362 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2363 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2364 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2365 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2366
2367 // Emit code to load the value if it was passed in registers.
2368
2369 CGF.EmitBlock(InRegBlock);
2370
2371 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2372 // an offset of l->gp_offset and/or l->fp_offset. This may require
2373 // copying to a temporary location in case the parameter is passed
2374 // in different register classes or requires an alignment greater
2375 // than 8 for general purpose registers and 16 for XMM registers.
2376 //
2377 // FIXME: This really results in shameful code when we end up needing to
2378 // collect arguments from different places; often what should result in a
2379 // simple assembling of a structure from scattered addresses has many more
2380 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002381 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002382 llvm::Value *RegAddr =
2383 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2384 "reg_save_area");
2385 if (neededInt && neededSSE) {
2386 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002387 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002388 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002389 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2390 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002391 llvm::Type *TyLo = ST->getElementType(0);
2392 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002393 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002394 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002395 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2396 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002397 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2398 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002399 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2400 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002401 llvm::Value *V =
2402 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2403 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2404 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2405 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2406
Owen Andersona1cf15f2009-07-14 23:10:40 +00002407 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002408 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002409 } else if (neededInt) {
2410 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2411 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002412 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002413 } else if (neededSSE == 1) {
2414 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2415 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2416 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002417 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002418 assert(neededSSE == 2 && "Invalid number of needed registers!");
2419 // SSE registers are spaced 16 bytes apart in the register save
2420 // area, we need to collect the two eightbytes together.
2421 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002422 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002423 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002424 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002425 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002426 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002427 DoubleTy, NULL);
2428 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2429 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2430 DblPtrTy));
2431 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2432 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2433 DblPtrTy));
2434 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2435 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2436 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002437 }
2438
2439 // AMD64-ABI 3.5.7p5: Step 5. Set:
2440 // l->gp_offset = l->gp_offset + num_gp * 8
2441 // l->fp_offset = l->fp_offset + num_fp * 16.
2442 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002443 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002444 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2445 gp_offset_p);
2446 }
2447 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002448 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002449 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2450 fp_offset_p);
2451 }
2452 CGF.EmitBranch(ContBlock);
2453
2454 // Emit code to load the value if it was passed in memory.
2455
2456 CGF.EmitBlock(InMemBlock);
2457 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2458
2459 // Return the appropriate result.
2460
2461 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002462 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002463 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002464 ResAddr->addIncoming(RegAddr, InRegBlock);
2465 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002466 return ResAddr;
2467}
2468
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002469ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2470
2471 if (Ty->isVoidType())
2472 return ABIArgInfo::getIgnore();
2473
2474 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2475 Ty = EnumTy->getDecl()->getIntegerType();
2476
2477 uint64_t Size = getContext().getTypeSize(Ty);
2478
2479 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002480 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2481 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002482 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2483
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002484 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2485 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002486 getContext().getTargetInfo().getTriple().getOS()
2487 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002488 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2489 Size));
2490
2491 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2492 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2493 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002494 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002495 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2496 Size));
2497
2498 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2499 }
2500
2501 if (Ty->isPromotableIntegerType())
2502 return ABIArgInfo::getExtend();
2503
2504 return ABIArgInfo::getDirect();
2505}
2506
2507void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2508
2509 QualType RetTy = FI.getReturnType();
2510 FI.getReturnInfo() = classify(RetTy);
2511
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002512 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2513 it != ie; ++it)
2514 it->info = classify(it->type);
2515}
2516
Chris Lattnerf13721d2010-08-31 16:44:54 +00002517llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2518 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002519 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002520
Chris Lattnerf13721d2010-08-31 16:44:54 +00002521 CGBuilderTy &Builder = CGF.Builder;
2522 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2523 "ap");
2524 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2525 llvm::Type *PTy =
2526 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2527 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2528
2529 uint64_t Offset =
2530 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2531 llvm::Value *NextAddr =
2532 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2533 "ap.next");
2534 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2535
2536 return AddrTyped;
2537}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002538
Derek Schuff263366f2012-10-16 22:30:41 +00002539class NaClX86_64ABIInfo : public ABIInfo {
2540 public:
2541 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2542 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2543 virtual void computeInfo(CGFunctionInfo &FI) const;
2544 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2545 CodeGenFunction &CGF) const;
2546 private:
2547 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
2548 X86_64ABIInfo NInfo; // Used for everything else.
2549};
2550
2551class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2552 public:
2553 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2554 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2555};
2556
2557void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2558 if (FI.getASTCallingConvention() == CC_PnaclCall)
2559 PInfo.computeInfo(FI);
2560 else
2561 NInfo.computeInfo(FI);
2562}
2563
2564llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2565 CodeGenFunction &CGF) const {
2566 // Always use the native convention; calling pnacl-style varargs functions
2567 // is unuspported.
2568 return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2569}
2570
2571
John McCallec853ba2010-03-11 00:10:12 +00002572// PowerPC-32
2573
2574namespace {
2575class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2576public:
Chris Lattnerea044322010-07-29 02:01:43 +00002577 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002578
John McCallec853ba2010-03-11 00:10:12 +00002579 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2580 // This is recovered from gcc output.
2581 return 1; // r1 is the dedicated stack pointer
2582 }
2583
2584 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002585 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002586};
2587
2588}
2589
2590bool
2591PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2592 llvm::Value *Address) const {
2593 // This is calculated from the LLVM and GCC tables and verified
2594 // against gcc output. AFAIK all ABIs use the same encoding.
2595
2596 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002597
Chris Lattner8b418682012-02-07 00:39:47 +00002598 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002599 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2600 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2601 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2602
2603 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002604 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002605
2606 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002607 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002608
2609 // 64-76 are various 4-byte special-purpose registers:
2610 // 64: mq
2611 // 65: lr
2612 // 66: ctr
2613 // 67: ap
2614 // 68-75 cr0-7
2615 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002616 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002617
2618 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002619 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002620
2621 // 109: vrsave
2622 // 110: vscr
2623 // 111: spe_acc
2624 // 112: spefscr
2625 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002626 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002627
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002628 return false;
John McCallec853ba2010-03-11 00:10:12 +00002629}
2630
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002631// PowerPC-64
2632
2633namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002634/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2635class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2636
2637public:
2638 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2639
Bill Schmidtb1f5fe02012-10-12 19:26:17 +00002640 // TODO: We can add more logic to computeInfo to improve performance.
2641 // Example: For aggregate arguments that fit in a register, we could
2642 // use getDirectInReg (as is done below for structs containing a single
2643 // floating-point value) to avoid pushing them to memory on function
2644 // entry. This would require changing the logic in PPCISelLowering
2645 // when lowering the parameters in the caller and args in the callee.
2646 virtual void computeInfo(CGFunctionInfo &FI) const {
2647 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2648 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2649 it != ie; ++it) {
2650 // We rely on the default argument classification for the most part.
2651 // One exception: An aggregate containing a single floating-point
2652 // item must be passed in a register if one is available.
2653 const Type *T = isSingleElementStruct(it->type, getContext());
2654 if (T) {
2655 const BuiltinType *BT = T->getAs<BuiltinType>();
2656 if (BT && BT->isFloatingPoint()) {
2657 QualType QT(T, 0);
2658 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2659 continue;
2660 }
2661 }
2662 it->info = classifyArgumentType(it->type);
2663 }
2664 }
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002665
2666 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2667 QualType Ty,
2668 CodeGenFunction &CGF) const;
2669};
2670
2671class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2672public:
2673 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2674 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2675
2676 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2677 // This is recovered from gcc output.
2678 return 1; // r1 is the dedicated stack pointer
2679 }
2680
2681 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2682 llvm::Value *Address) const;
2683};
2684
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002685class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2686public:
2687 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2688
2689 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2690 // This is recovered from gcc output.
2691 return 1; // r1 is the dedicated stack pointer
2692 }
2693
2694 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2695 llvm::Value *Address) const;
2696};
2697
2698}
2699
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002700// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2701llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2702 QualType Ty,
2703 CodeGenFunction &CGF) const {
2704 llvm::Type *BP = CGF.Int8PtrTy;
2705 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2706
2707 CGBuilderTy &Builder = CGF.Builder;
2708 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2709 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2710
2711 // Handle address alignment for type alignment > 64 bits. Although
2712 // long double normally requires 16-byte alignment, this is not the
2713 // case when it is passed as an argument; so handle that special case.
2714 const BuiltinType *BT = Ty->getAs<BuiltinType>();
2715 unsigned TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2716
2717 if (TyAlign > 8 && (!BT || !BT->isFloatingPoint())) {
2718 assert((TyAlign & (TyAlign - 1)) == 0 &&
2719 "Alignment is not power of 2!");
2720 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2721 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(TyAlign - 1));
2722 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(~(TyAlign - 1)));
2723 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2724 }
2725
2726 // Update the va_list pointer.
2727 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2728 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2729 llvm::Value *NextAddr =
2730 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2731 "ap.next");
2732 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2733
2734 // If the argument is smaller than 8 bytes, it is right-adjusted in
2735 // its doubleword slot. Adjust the pointer to pick it up from the
2736 // correct offset.
2737 if (SizeInBytes < 8) {
2738 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2739 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2740 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2741 }
2742
2743 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2744 return Builder.CreateBitCast(Addr, PTy);
2745}
2746
2747static bool
2748PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2749 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002750 // This is calculated from the LLVM and GCC tables and verified
2751 // against gcc output. AFAIK all ABIs use the same encoding.
2752
2753 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2754
2755 llvm::IntegerType *i8 = CGF.Int8Ty;
2756 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2757 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2758 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2759
2760 // 0-31: r0-31, the 8-byte general-purpose registers
2761 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2762
2763 // 32-63: fp0-31, the 8-byte floating-point registers
2764 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2765
2766 // 64-76 are various 4-byte special-purpose registers:
2767 // 64: mq
2768 // 65: lr
2769 // 66: ctr
2770 // 67: ap
2771 // 68-75 cr0-7
2772 // 76: xer
2773 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2774
2775 // 77-108: v0-31, the 16-byte vector registers
2776 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2777
2778 // 109: vrsave
2779 // 110: vscr
2780 // 111: spe_acc
2781 // 112: spefscr
2782 // 113: sfp
2783 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2784
2785 return false;
2786}
John McCallec853ba2010-03-11 00:10:12 +00002787
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002788bool
2789PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2790 CodeGen::CodeGenFunction &CGF,
2791 llvm::Value *Address) const {
2792
2793 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2794}
2795
2796bool
2797PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2798 llvm::Value *Address) const {
2799
2800 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2801}
2802
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002803//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002804// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002805//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002806
2807namespace {
2808
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002809class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002810public:
2811 enum ABIKind {
2812 APCS = 0,
2813 AAPCS = 1,
2814 AAPCS_VFP
2815 };
2816
2817private:
2818 ABIKind Kind;
2819
2820public:
Chris Lattnerea044322010-07-29 02:01:43 +00002821 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002822
John McCall49e34be2011-08-30 01:42:09 +00002823 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002824 StringRef Env =
2825 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002826 return (Env == "gnueabi" || Env == "eabi" ||
2827 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002828 }
2829
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002830private:
2831 ABIKind getABIKind() const { return Kind; }
2832
Chris Lattnera3c109b2010-07-29 02:16:43 +00002833 ABIArgInfo classifyReturnType(QualType RetTy) const;
2834 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Manman Ren97f81572012-10-16 19:18:39 +00002835 bool isIllegalVectorType(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002836
Chris Lattneree5dcd02010-07-29 02:31:05 +00002837 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002838
2839 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2840 CodeGenFunction &CGF) const;
2841};
2842
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002843class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2844public:
Chris Lattnerea044322010-07-29 02:01:43 +00002845 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2846 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002847
John McCall49e34be2011-08-30 01:42:09 +00002848 const ARMABIInfo &getABIInfo() const {
2849 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2850 }
2851
John McCall6374c332010-03-06 00:35:14 +00002852 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2853 return 13;
2854 }
Roman Divacky09345d12011-05-18 19:36:54 +00002855
Chris Lattner5f9e2722011-07-23 10:55:15 +00002856 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002857 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2858 }
2859
Roman Divacky09345d12011-05-18 19:36:54 +00002860 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2861 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002862 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002863
2864 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002865 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002866 return false;
2867 }
John McCall49e34be2011-08-30 01:42:09 +00002868
2869 unsigned getSizeOfUnwindException() const {
2870 if (getABIInfo().isEABI()) return 88;
2871 return TargetCodeGenInfo::getSizeOfUnwindException();
2872 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002873};
2874
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002875}
2876
Chris Lattneree5dcd02010-07-29 02:31:05 +00002877void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002878 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002879 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002880 it != ie; ++it)
2881 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002882
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002883 // Always honor user-specified calling convention.
2884 if (FI.getCallingConvention() != llvm::CallingConv::C)
2885 return;
2886
2887 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002888 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002889 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002890 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002891 else
2892 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002893
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002894 // If user did not ask for specific calling convention explicitly (e.g. via
2895 // pcs attribute), set effective calling convention if it's different than ABI
2896 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002897 switch (getABIKind()) {
2898 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002899 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2900 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002901 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002902 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002903 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2904 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002905 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002906 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002907 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2908 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002909 break;
2910 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002911}
2912
Bob Wilson194f06a2011-08-03 05:58:22 +00002913/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2914/// aggregate. If HAMembers is non-null, the number of base elements
2915/// contained in the type is returned through it; this is used for the
2916/// recursive calls that check aggregate component types.
2917static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2918 ASTContext &Context,
2919 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002920 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002921 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2922 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2923 return false;
2924 Members *= AT->getSize().getZExtValue();
2925 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2926 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002927 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002928 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002929
Bob Wilson194f06a2011-08-03 05:58:22 +00002930 Members = 0;
2931 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2932 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002933 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002934 uint64_t FldMembers;
2935 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2936 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002937
2938 Members = (RD->isUnion() ?
2939 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002940 }
2941 } else {
2942 Members = 1;
2943 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2944 Members = 2;
2945 Ty = CT->getElementType();
2946 }
2947
2948 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2949 // double, or 64-bit or 128-bit vectors.
2950 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2951 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00002952 BT->getKind() != BuiltinType::Double &&
2953 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00002954 return false;
2955 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2956 unsigned VecSize = Context.getTypeSize(VT);
2957 if (VecSize != 64 && VecSize != 128)
2958 return false;
2959 } else {
2960 return false;
2961 }
2962
2963 // The base type must be the same for all members. Vector types of the
2964 // same total size are treated as being equivalent here.
2965 const Type *TyPtr = Ty.getTypePtr();
2966 if (!Base)
2967 Base = TyPtr;
2968 if (Base != TyPtr &&
2969 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2970 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2971 return false;
2972 }
2973
2974 // Homogeneous Aggregates can have at most 4 members of the base type.
2975 if (HAMembers)
2976 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002977
2978 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00002979}
2980
Chris Lattnera3c109b2010-07-29 02:16:43 +00002981ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
Manman Ren97f81572012-10-16 19:18:39 +00002982 // Handle illegal vector types here.
2983 if (isIllegalVectorType(Ty)) {
2984 uint64_t Size = getContext().getTypeSize(Ty);
2985 if (Size <= 32) {
2986 llvm::Type *ResType =
2987 llvm::Type::getInt32Ty(getVMContext());
2988 return ABIArgInfo::getDirect(ResType);
2989 }
2990 if (Size == 64) {
2991 llvm::Type *ResType = llvm::VectorType::get(
2992 llvm::Type::getInt32Ty(getVMContext()), 2);
2993 return ABIArgInfo::getDirect(ResType);
2994 }
2995 if (Size == 128) {
2996 llvm::Type *ResType = llvm::VectorType::get(
2997 llvm::Type::getInt32Ty(getVMContext()), 4);
2998 return ABIArgInfo::getDirect(ResType);
2999 }
3000 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3001 }
3002
John McCalld608cdb2010-08-22 10:59:02 +00003003 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003004 // Treat an enum type as its underlying type.
3005 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3006 Ty = EnumTy->getDecl()->getIntegerType();
3007
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003008 return (Ty->isPromotableIntegerType() ?
3009 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003010 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003011
Daniel Dunbar42025572009-09-14 21:54:03 +00003012 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003013 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00003014 return ABIArgInfo::getIgnore();
3015
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003016 // Structures with either a non-trivial destructor or a non-trivial
3017 // copy constructor are always indirect.
3018 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3019 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3020
Bob Wilson194f06a2011-08-03 05:58:22 +00003021 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
3022 // Homogeneous Aggregates need to be expanded.
3023 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003024 if (isHomogeneousAggregate(Ty, Base, getContext())) {
3025 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson194f06a2011-08-03 05:58:22 +00003026 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003027 }
Bob Wilson194f06a2011-08-03 05:58:22 +00003028 }
3029
Manman Ren634b3d22012-08-13 21:23:55 +00003030 // Support byval for ARM.
3031 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
3032 getContext().getTypeAlign(Ty) > 64) {
3033 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
Eli Friedman79f30982012-08-09 00:31:40 +00003034 }
3035
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00003036 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00003037 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003038 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00003039 // FIXME: Try to match the types of the arguments more accurately where
3040 // we can.
3041 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00003042 ElemTy = llvm::Type::getInt32Ty(getVMContext());
3043 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00003044 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00003045 ElemTy = llvm::Type::getInt64Ty(getVMContext());
3046 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00003047 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003048
Chris Lattner9cbe4f02011-07-09 17:41:47 +00003049 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00003050 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00003051 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003052}
3053
Chris Lattnera3c109b2010-07-29 02:16:43 +00003054static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00003055 llvm::LLVMContext &VMContext) {
3056 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3057 // is called integer-like if its size is less than or equal to one word, and
3058 // the offset of each of its addressable sub-fields is zero.
3059
3060 uint64_t Size = Context.getTypeSize(Ty);
3061
3062 // Check that the type fits in a word.
3063 if (Size > 32)
3064 return false;
3065
3066 // FIXME: Handle vector types!
3067 if (Ty->isVectorType())
3068 return false;
3069
Daniel Dunbarb0d58192009-09-14 02:20:34 +00003070 // Float types are never treated as "integer like".
3071 if (Ty->isRealFloatingType())
3072 return false;
3073
Daniel Dunbar98303b92009-09-13 08:03:58 +00003074 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00003075 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00003076 return true;
3077
Daniel Dunbar45815812010-02-01 23:31:26 +00003078 // Small complex integer types are "integer like".
3079 if (const ComplexType *CT = Ty->getAs<ComplexType>())
3080 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003081
3082 // Single element and zero sized arrays should be allowed, by the definition
3083 // above, but they are not.
3084
3085 // Otherwise, it must be a record type.
3086 const RecordType *RT = Ty->getAs<RecordType>();
3087 if (!RT) return false;
3088
3089 // Ignore records with flexible arrays.
3090 const RecordDecl *RD = RT->getDecl();
3091 if (RD->hasFlexibleArrayMember())
3092 return false;
3093
3094 // Check that all sub-fields are at offset 0, and are themselves "integer
3095 // like".
3096 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3097
3098 bool HadField = false;
3099 unsigned idx = 0;
3100 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3101 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003102 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003103
Daniel Dunbar679855a2010-01-29 03:22:29 +00003104 // Bit-fields are not addressable, we only need to verify they are "integer
3105 // like". We still have to disallow a subsequent non-bitfield, for example:
3106 // struct { int : 0; int x }
3107 // is non-integer like according to gcc.
3108 if (FD->isBitField()) {
3109 if (!RD->isUnion())
3110 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003111
Daniel Dunbar679855a2010-01-29 03:22:29 +00003112 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3113 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003114
Daniel Dunbar679855a2010-01-29 03:22:29 +00003115 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003116 }
3117
Daniel Dunbar679855a2010-01-29 03:22:29 +00003118 // Check if this field is at offset 0.
3119 if (Layout.getFieldOffset(idx) != 0)
3120 return false;
3121
Daniel Dunbar98303b92009-09-13 08:03:58 +00003122 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3123 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003124
Daniel Dunbar679855a2010-01-29 03:22:29 +00003125 // Only allow at most one field in a structure. This doesn't match the
3126 // wording above, but follows gcc in situations with a field following an
3127 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003128 if (!RD->isUnion()) {
3129 if (HadField)
3130 return false;
3131
3132 HadField = true;
3133 }
3134 }
3135
3136 return true;
3137}
3138
Chris Lattnera3c109b2010-07-29 02:16:43 +00003139ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003140 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003141 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003142
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003143 // Large vector types should be returned via memory.
3144 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3145 return ABIArgInfo::getIndirect(0);
3146
John McCalld608cdb2010-08-22 10:59:02 +00003147 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003148 // Treat an enum type as its underlying type.
3149 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3150 RetTy = EnumTy->getDecl()->getIntegerType();
3151
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003152 return (RetTy->isPromotableIntegerType() ?
3153 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003154 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003155
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003156 // Structures with either a non-trivial destructor or a non-trivial
3157 // copy constructor are always indirect.
3158 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3159 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3160
Daniel Dunbar98303b92009-09-13 08:03:58 +00003161 // Are we following APCS?
3162 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003163 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003164 return ABIArgInfo::getIgnore();
3165
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003166 // Complex types are all returned as packed integers.
3167 //
3168 // FIXME: Consider using 2 x vector types if the back end handles them
3169 // correctly.
3170 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003171 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003172 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003173
Daniel Dunbar98303b92009-09-13 08:03:58 +00003174 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003175 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003176 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003177 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003178 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003179 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003180 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003181 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3182 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003183 }
3184
3185 // Otherwise return in memory.
3186 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003187 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003188
3189 // Otherwise this is an AAPCS variant.
3190
Chris Lattnera3c109b2010-07-29 02:16:43 +00003191 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003192 return ABIArgInfo::getIgnore();
3193
Bob Wilson3b694fa2011-11-02 04:51:36 +00003194 // Check for homogeneous aggregates with AAPCS-VFP.
3195 if (getABIKind() == AAPCS_VFP) {
3196 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003197 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3198 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003199 // Homogeneous Aggregates are returned directly.
3200 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003201 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003202 }
3203
Daniel Dunbar98303b92009-09-13 08:03:58 +00003204 // Aggregates <= 4 bytes are returned in r0; other aggregates
3205 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003206 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003207 if (Size <= 32) {
3208 // Return in the smallest viable integer type.
3209 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003210 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003211 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003212 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3213 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003214 }
3215
Daniel Dunbar98303b92009-09-13 08:03:58 +00003216 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003217}
3218
Manman Ren97f81572012-10-16 19:18:39 +00003219/// isIllegalVector - check whether Ty is an illegal vector type.
3220bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3221 if (const VectorType *VT = Ty->getAs<VectorType>()) {
3222 // Check whether VT is legal.
3223 unsigned NumElements = VT->getNumElements();
3224 uint64_t Size = getContext().getTypeSize(VT);
3225 // NumElements should be power of 2.
3226 if ((NumElements & (NumElements - 1)) != 0)
3227 return true;
3228 // Size should be greater than 32 bits.
3229 return Size <= 32;
3230 }
3231 return false;
3232}
3233
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003234llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003235 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003236 llvm::Type *BP = CGF.Int8PtrTy;
3237 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003238
3239 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003240 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003241 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Manman Rend105e732012-10-16 19:01:37 +00003242
3243 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
Rafael Espindolae164c182011-08-02 22:33:37 +00003244 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
Manman Ren97f81572012-10-16 19:18:39 +00003245 bool IsIndirect = false;
Manman Rend105e732012-10-16 19:01:37 +00003246
3247 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3248 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
Manman Ren93371022012-10-16 19:51:48 +00003249 if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3250 getABIKind() == ARMABIInfo::AAPCS)
3251 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3252 else
3253 TyAlign = 4;
Manman Ren97f81572012-10-16 19:18:39 +00003254 // Use indirect if size of the illegal vector is bigger than 16 bytes.
3255 if (isIllegalVectorType(Ty) && Size > 16) {
3256 IsIndirect = true;
3257 Size = 4;
3258 TyAlign = 4;
3259 }
Manman Rend105e732012-10-16 19:01:37 +00003260
3261 // Handle address alignment for ABI alignment > 4 bytes.
Rafael Espindolae164c182011-08-02 22:33:37 +00003262 if (TyAlign > 4) {
3263 assert((TyAlign & (TyAlign - 1)) == 0 &&
3264 "Alignment is not power of 2!");
3265 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3266 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3267 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
Manman Rend105e732012-10-16 19:01:37 +00003268 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
Rafael Espindolae164c182011-08-02 22:33:37 +00003269 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003270
3271 uint64_t Offset =
Manman Rend105e732012-10-16 19:01:37 +00003272 llvm::RoundUpToAlignment(Size, 4);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003273 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003274 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003275 "ap.next");
3276 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3277
Manman Ren97f81572012-10-16 19:18:39 +00003278 if (IsIndirect)
3279 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
Manman Ren93371022012-10-16 19:51:48 +00003280 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
Manman Rend105e732012-10-16 19:01:37 +00003281 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3282 // may not be correctly aligned for the vector type. We create an aligned
3283 // temporary space and copy the content over from ap.cur to the temporary
3284 // space. This is necessary if the natural alignment of the type is greater
3285 // than the ABI alignment.
3286 llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3287 CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3288 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3289 "var.align");
3290 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3291 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3292 Builder.CreateMemCpy(Dst, Src,
3293 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3294 TyAlign, false);
3295 Addr = AlignedTemp; //The content is in aligned location.
3296 }
3297 llvm::Type *PTy =
3298 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3299 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3300
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003301 return AddrTyped;
3302}
3303
Derek Schuff263366f2012-10-16 22:30:41 +00003304class NaClARMABIInfo : public ABIInfo {
3305 public:
3306 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3307 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3308 virtual void computeInfo(CGFunctionInfo &FI) const;
3309 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3310 CodeGenFunction &CGF) const;
3311 private:
3312 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3313 ARMABIInfo NInfo; // Used for everything else.
3314};
3315
3316class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo {
3317 public:
3318 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3319 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3320};
3321
3322void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3323 if (FI.getASTCallingConvention() == CC_PnaclCall)
3324 PInfo.computeInfo(FI);
3325 else
3326 static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3327}
3328
3329llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3330 CodeGenFunction &CGF) const {
3331 // Always use the native convention; calling pnacl-style varargs functions
3332 // is unsupported.
3333 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3334}
3335
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003336//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003337// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003338//===----------------------------------------------------------------------===//
3339
3340namespace {
3341
Justin Holewinski2c585b92012-05-24 17:43:12 +00003342class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003343public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003344 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003345
3346 ABIArgInfo classifyReturnType(QualType RetTy) const;
3347 ABIArgInfo classifyArgumentType(QualType Ty) const;
3348
3349 virtual void computeInfo(CGFunctionInfo &FI) const;
3350 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3351 CodeGenFunction &CFG) const;
3352};
3353
Justin Holewinski2c585b92012-05-24 17:43:12 +00003354class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003355public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003356 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3357 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003358
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003359 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3360 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003361};
3362
Justin Holewinski2c585b92012-05-24 17:43:12 +00003363ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003364 if (RetTy->isVoidType())
3365 return ABIArgInfo::getIgnore();
3366 if (isAggregateTypeForABI(RetTy))
3367 return ABIArgInfo::getIndirect(0);
3368 return ABIArgInfo::getDirect();
3369}
3370
Justin Holewinski2c585b92012-05-24 17:43:12 +00003371ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003372 if (isAggregateTypeForABI(Ty))
3373 return ABIArgInfo::getIndirect(0);
3374
3375 return ABIArgInfo::getDirect();
3376}
3377
Justin Holewinski2c585b92012-05-24 17:43:12 +00003378void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003379 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3380 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3381 it != ie; ++it)
3382 it->info = classifyArgumentType(it->type);
3383
3384 // Always honor user-specified calling convention.
3385 if (FI.getCallingConvention() != llvm::CallingConv::C)
3386 return;
3387
3388 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003389 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3390 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003391 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003392 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003393 if (LangOpts.OpenCL || LangOpts.CUDA) {
3394 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003395 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003396 } else {
3397 // If we are in standard C/C++ mode, use the triple to decide on the default
3398 StringRef Env =
3399 getContext().getTargetInfo().getTriple().getEnvironmentName();
3400 if (Env == "device")
3401 DefaultCC = llvm::CallingConv::PTX_Device;
3402 else
3403 DefaultCC = llvm::CallingConv::PTX_Kernel;
3404 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003405 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003406
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003407}
3408
Justin Holewinski2c585b92012-05-24 17:43:12 +00003409llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3410 CodeGenFunction &CFG) const {
3411 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003412}
3413
Justin Holewinski2c585b92012-05-24 17:43:12 +00003414void NVPTXTargetCodeGenInfo::
3415SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3416 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003417 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3418 if (!FD) return;
3419
3420 llvm::Function *F = cast<llvm::Function>(GV);
3421
3422 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003423 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003424 // Use OpenCL function attributes to set proper calling conventions
3425 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003426 if (FD->hasAttr<OpenCLKernelAttr>()) {
3427 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003428 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003429 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003430 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003431 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003432 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003433
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003434 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003435 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003436 // CUDA __global__ functions get a kernel calling convention. Since
3437 // __global__ functions cannot be called from the device, we do not
3438 // need to set the noinline attribute.
3439 if (FD->getAttr<CUDAGlobalAttr>())
3440 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003441 }
3442}
3443
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003444}
3445
3446//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003447// MBlaze ABI Implementation
3448//===----------------------------------------------------------------------===//
3449
3450namespace {
3451
3452class MBlazeABIInfo : public ABIInfo {
3453public:
3454 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3455
3456 bool isPromotableIntegerType(QualType Ty) const;
3457
3458 ABIArgInfo classifyReturnType(QualType RetTy) const;
3459 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3460
3461 virtual void computeInfo(CGFunctionInfo &FI) const {
3462 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3463 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3464 it != ie; ++it)
3465 it->info = classifyArgumentType(it->type);
3466 }
3467
3468 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3469 CodeGenFunction &CGF) const;
3470};
3471
3472class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3473public:
3474 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3475 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3476 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3477 CodeGen::CodeGenModule &M) const;
3478};
3479
3480}
3481
3482bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3483 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3484 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3485 switch (BT->getKind()) {
3486 case BuiltinType::Bool:
3487 case BuiltinType::Char_S:
3488 case BuiltinType::Char_U:
3489 case BuiltinType::SChar:
3490 case BuiltinType::UChar:
3491 case BuiltinType::Short:
3492 case BuiltinType::UShort:
3493 return true;
3494 default:
3495 return false;
3496 }
3497 return false;
3498}
3499
3500llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3501 CodeGenFunction &CGF) const {
3502 // FIXME: Implement
3503 return 0;
3504}
3505
3506
3507ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3508 if (RetTy->isVoidType())
3509 return ABIArgInfo::getIgnore();
3510 if (isAggregateTypeForABI(RetTy))
3511 return ABIArgInfo::getIndirect(0);
3512
3513 return (isPromotableIntegerType(RetTy) ?
3514 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3515}
3516
3517ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3518 if (isAggregateTypeForABI(Ty))
3519 return ABIArgInfo::getIndirect(0);
3520
3521 return (isPromotableIntegerType(Ty) ?
3522 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3523}
3524
3525void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3526 llvm::GlobalValue *GV,
3527 CodeGen::CodeGenModule &M)
3528 const {
3529 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3530 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003531
Wesley Peck276fdf42010-12-19 19:57:51 +00003532 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3533 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3534 CC = llvm::CallingConv::MBLAZE_INTR;
3535 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3536 CC = llvm::CallingConv::MBLAZE_SVOL;
3537
3538 if (CC != llvm::CallingConv::C) {
3539 // Handle 'interrupt_handler' attribute:
3540 llvm::Function *F = cast<llvm::Function>(GV);
3541
3542 // Step 1: Set ISR calling convention.
3543 F->setCallingConv(CC);
3544
3545 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003546 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003547 }
3548
3549 // Step 3: Emit _interrupt_handler alias.
3550 if (CC == llvm::CallingConv::MBLAZE_INTR)
3551 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3552 "_interrupt_handler", GV, &M.getModule());
3553}
3554
3555
3556//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003557// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003558//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003559
3560namespace {
3561
3562class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3563public:
Chris Lattnerea044322010-07-29 02:01:43 +00003564 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3565 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003566 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3567 CodeGen::CodeGenModule &M) const;
3568};
3569
3570}
3571
3572void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3573 llvm::GlobalValue *GV,
3574 CodeGen::CodeGenModule &M) const {
3575 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3576 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3577 // Handle 'interrupt' attribute:
3578 llvm::Function *F = cast<llvm::Function>(GV);
3579
3580 // Step 1: Set ISR calling convention.
3581 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3582
3583 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003584 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003585
3586 // Step 3: Emit ISR vector alias.
3587 unsigned Num = attr->getNumber() + 0xffe0;
3588 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003589 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003590 GV, &M.getModule());
3591 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003592 }
3593}
3594
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003595//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003596// MIPS ABI Implementation. This works for both little-endian and
3597// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003598//===----------------------------------------------------------------------===//
3599
John McCallaeeb7012010-05-27 06:19:26 +00003600namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003601class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003602 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003603 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3604 void CoerceToIntArgs(uint64_t TySize,
3605 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003606 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003607 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003608 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003609public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003610 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003611 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3612 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003613
3614 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003615 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003616 virtual void computeInfo(CGFunctionInfo &FI) const;
3617 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3618 CodeGenFunction &CGF) const;
3619};
3620
John McCallaeeb7012010-05-27 06:19:26 +00003621class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003622 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003623public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003624 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3625 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3626 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003627
3628 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3629 return 29;
3630 }
3631
3632 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003633 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003634
3635 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003636 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003637 }
John McCallaeeb7012010-05-27 06:19:26 +00003638};
3639}
3640
Akira Hatanakac359f202012-07-03 19:24:06 +00003641void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3642 SmallVector<llvm::Type*, 8> &ArgList) const {
3643 llvm::IntegerType *IntTy =
3644 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003645
3646 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3647 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3648 ArgList.push_back(IntTy);
3649
3650 // If necessary, add one more integer type to ArgList.
3651 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3652
3653 if (R)
3654 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003655}
3656
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003657// In N32/64, an aligned double precision floating point field is passed in
3658// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003659llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003660 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3661
3662 if (IsO32) {
3663 CoerceToIntArgs(TySize, ArgList);
3664 return llvm::StructType::get(getVMContext(), ArgList);
3665 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003666
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003667 if (Ty->isComplexType())
3668 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003669
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003670 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003671
Akira Hatanakac359f202012-07-03 19:24:06 +00003672 // Unions/vectors are passed in integer registers.
3673 if (!RT || !RT->isStructureOrClassType()) {
3674 CoerceToIntArgs(TySize, ArgList);
3675 return llvm::StructType::get(getVMContext(), ArgList);
3676 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003677
3678 const RecordDecl *RD = RT->getDecl();
3679 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003680 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003681
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003682 uint64_t LastOffset = 0;
3683 unsigned idx = 0;
3684 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3685
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003686 // Iterate over fields in the struct/class and check if there are any aligned
3687 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003688 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3689 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003690 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003691 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3692
3693 if (!BT || BT->getKind() != BuiltinType::Double)
3694 continue;
3695
3696 uint64_t Offset = Layout.getFieldOffset(idx);
3697 if (Offset % 64) // Ignore doubles that are not aligned.
3698 continue;
3699
3700 // Add ((Offset - LastOffset) / 64) args of type i64.
3701 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3702 ArgList.push_back(I64);
3703
3704 // Add double type.
3705 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3706 LastOffset = Offset + 64;
3707 }
3708
Akira Hatanakac359f202012-07-03 19:24:06 +00003709 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3710 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003711
3712 return llvm::StructType::get(getVMContext(), ArgList);
3713}
3714
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003715llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003716 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003717
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003718 if ((Align - 1) & Offset)
3719 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3720
3721 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003722}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003723
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003724ABIArgInfo
3725MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003726 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003727 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003728 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003729
Akira Hatanakac359f202012-07-03 19:24:06 +00003730 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3731 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003732 Offset = llvm::RoundUpToAlignment(Offset, Align);
3733 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003734
Akira Hatanakac359f202012-07-03 19:24:06 +00003735 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003736 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003737 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003738 return ABIArgInfo::getIgnore();
3739
Akira Hatanaka511949b2011-08-01 18:09:58 +00003740 // Records with non trivial destructors/constructors should not be passed
3741 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003742 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003743 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003744 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003745 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003746
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003747 // If we have reached here, aggregates are passed directly by coercing to
3748 // another structure type. Padding is inserted if the offset of the
3749 // aggregate is unaligned.
3750 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3751 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003752 }
3753
3754 // Treat an enum type as its underlying type.
3755 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3756 Ty = EnumTy->getDecl()->getIntegerType();
3757
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003758 if (Ty->isPromotableIntegerType())
3759 return ABIArgInfo::getExtend();
3760
3761 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003762}
3763
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003764llvm::Type*
3765MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003766 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003767 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003768
Akira Hatanakada54ff32012-02-09 18:49:26 +00003769 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003770 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003771 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3772 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003773
Akira Hatanakada54ff32012-02-09 18:49:26 +00003774 // N32/64 returns struct/classes in floating point registers if the
3775 // following conditions are met:
3776 // 1. The size of the struct/class is no larger than 128-bit.
3777 // 2. The struct/class has one or two fields all of which are floating
3778 // point types.
3779 // 3. The offset of the first field is zero (this follows what gcc does).
3780 //
3781 // Any other composite results are returned in integer registers.
3782 //
3783 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3784 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3785 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003786 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003787
Akira Hatanakada54ff32012-02-09 18:49:26 +00003788 if (!BT || !BT->isFloatingPoint())
3789 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003790
David Blaikie262bc182012-04-30 02:36:29 +00003791 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003792 }
3793
3794 if (b == e)
3795 return llvm::StructType::get(getVMContext(), RTList,
3796 RD->hasAttr<PackedAttr>());
3797
3798 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003799 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003800 }
3801
Akira Hatanakac359f202012-07-03 19:24:06 +00003802 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003803 return llvm::StructType::get(getVMContext(), RTList);
3804}
3805
Akira Hatanaka619e8872011-06-02 00:09:17 +00003806ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003807 uint64_t Size = getContext().getTypeSize(RetTy);
3808
3809 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003810 return ABIArgInfo::getIgnore();
3811
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003812 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003813 if (Size <= 128) {
3814 if (RetTy->isAnyComplexType())
3815 return ABIArgInfo::getDirect();
3816
Akira Hatanakac359f202012-07-03 19:24:06 +00003817 // O32 returns integer vectors in registers.
3818 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3819 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3820
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003821 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003822 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3823 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003824
3825 return ABIArgInfo::getIndirect(0);
3826 }
3827
3828 // Treat an enum type as its underlying type.
3829 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3830 RetTy = EnumTy->getDecl()->getIntegerType();
3831
3832 return (RetTy->isPromotableIntegerType() ?
3833 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3834}
3835
3836void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003837 ABIArgInfo &RetInfo = FI.getReturnInfo();
3838 RetInfo = classifyReturnType(FI.getReturnType());
3839
3840 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003841 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003842
Akira Hatanaka619e8872011-06-02 00:09:17 +00003843 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3844 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003845 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003846}
3847
3848llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3849 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003850 llvm::Type *BP = CGF.Int8PtrTy;
3851 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003852
3853 CGBuilderTy &Builder = CGF.Builder;
3854 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3855 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003856 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003857 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3858 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003859 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3860 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003861
3862 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003863 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3864 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3865 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3866 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003867 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3868 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3869 }
3870 else
3871 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3872
3873 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003874 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003875 uint64_t Offset =
3876 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3877 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003878 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003879 "ap.next");
3880 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3881
3882 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003883}
3884
John McCallaeeb7012010-05-27 06:19:26 +00003885bool
3886MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3887 llvm::Value *Address) const {
3888 // This information comes from gcc's implementation, which seems to
3889 // as canonical as it gets.
3890
John McCallaeeb7012010-05-27 06:19:26 +00003891 // Everything on MIPS is 4 bytes. Double-precision FP registers
3892 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003893 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003894
3895 // 0-31 are the general purpose registers, $0 - $31.
3896 // 32-63 are the floating-point registers, $f0 - $f31.
3897 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3898 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003899 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003900
3901 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3902 // They are one bit wide and ignored here.
3903
3904 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3905 // (coprocessor 1 is the FP unit)
3906 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3907 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3908 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003909 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003910 return false;
3911}
3912
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003913//===----------------------------------------------------------------------===//
3914// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3915// Currently subclassed only to implement custom OpenCL C function attribute
3916// handling.
3917//===----------------------------------------------------------------------===//
3918
3919namespace {
3920
3921class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3922public:
3923 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3924 : DefaultTargetCodeGenInfo(CGT) {}
3925
3926 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3927 CodeGen::CodeGenModule &M) const;
3928};
3929
3930void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3931 llvm::GlobalValue *GV,
3932 CodeGen::CodeGenModule &M) const {
3933 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3934 if (!FD) return;
3935
3936 llvm::Function *F = cast<llvm::Function>(GV);
3937
David Blaikie4e4d0842012-03-11 07:00:24 +00003938 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003939 if (FD->hasAttr<OpenCLKernelAttr>()) {
3940 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003941 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003942
3943 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3944
3945 // Convert the reqd_work_group_size() attributes to metadata.
3946 llvm::LLVMContext &Context = F->getContext();
3947 llvm::NamedMDNode *OpenCLMetadata =
3948 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3949
3950 SmallVector<llvm::Value*, 5> Operands;
3951 Operands.push_back(F);
3952
Chris Lattner8b418682012-02-07 00:39:47 +00003953 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3954 llvm::APInt(32,
3955 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3956 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3957 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003958 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003959 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3960 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003961 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3962
3963 // Add a boolean constant operand for "required" (true) or "hint" (false)
3964 // for implementing the work_group_size_hint attr later. Currently
3965 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003966 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003967 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3968 }
3969 }
3970 }
3971}
3972
3973}
John McCallaeeb7012010-05-27 06:19:26 +00003974
Tony Linthicum96319392011-12-12 21:14:55 +00003975//===----------------------------------------------------------------------===//
3976// Hexagon ABI Implementation
3977//===----------------------------------------------------------------------===//
3978
3979namespace {
3980
3981class HexagonABIInfo : public ABIInfo {
3982
3983
3984public:
3985 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3986
3987private:
3988
3989 ABIArgInfo classifyReturnType(QualType RetTy) const;
3990 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3991
3992 virtual void computeInfo(CGFunctionInfo &FI) const;
3993
3994 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3995 CodeGenFunction &CGF) const;
3996};
3997
3998class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3999public:
4000 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4001 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4002
4003 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
4004 return 29;
4005 }
4006};
4007
4008}
4009
4010void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
4011 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4012 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4013 it != ie; ++it)
4014 it->info = classifyArgumentType(it->type);
4015}
4016
4017ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
4018 if (!isAggregateTypeForABI(Ty)) {
4019 // Treat an enum type as its underlying type.
4020 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4021 Ty = EnumTy->getDecl()->getIntegerType();
4022
4023 return (Ty->isPromotableIntegerType() ?
4024 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4025 }
4026
4027 // Ignore empty records.
4028 if (isEmptyRecord(getContext(), Ty, true))
4029 return ABIArgInfo::getIgnore();
4030
4031 // Structures with either a non-trivial destructor or a non-trivial
4032 // copy constructor are always indirect.
4033 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4034 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4035
4036 uint64_t Size = getContext().getTypeSize(Ty);
4037 if (Size > 64)
4038 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4039 // Pass in the smallest viable integer type.
4040 else if (Size > 32)
4041 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4042 else if (Size > 16)
4043 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4044 else if (Size > 8)
4045 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4046 else
4047 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4048}
4049
4050ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
4051 if (RetTy->isVoidType())
4052 return ABIArgInfo::getIgnore();
4053
4054 // Large vector types should be returned via memory.
4055 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
4056 return ABIArgInfo::getIndirect(0);
4057
4058 if (!isAggregateTypeForABI(RetTy)) {
4059 // Treat an enum type as its underlying type.
4060 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4061 RetTy = EnumTy->getDecl()->getIntegerType();
4062
4063 return (RetTy->isPromotableIntegerType() ?
4064 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4065 }
4066
4067 // Structures with either a non-trivial destructor or a non-trivial
4068 // copy constructor are always indirect.
4069 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
4070 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4071
4072 if (isEmptyRecord(getContext(), RetTy, true))
4073 return ABIArgInfo::getIgnore();
4074
4075 // Aggregates <= 8 bytes are returned in r0; other aggregates
4076 // are returned indirectly.
4077 uint64_t Size = getContext().getTypeSize(RetTy);
4078 if (Size <= 64) {
4079 // Return in the smallest viable integer type.
4080 if (Size <= 8)
4081 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4082 if (Size <= 16)
4083 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
4084 if (Size <= 32)
4085 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
4086 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
4087 }
4088
4089 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
4090}
4091
4092llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00004093 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00004094 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00004095 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00004096
4097 CGBuilderTy &Builder = CGF.Builder;
4098 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
4099 "ap");
4100 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4101 llvm::Type *PTy =
4102 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4103 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
4104
4105 uint64_t Offset =
4106 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
4107 llvm::Value *NextAddr =
4108 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
4109 "ap.next");
4110 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4111
4112 return AddrTyped;
4113}
4114
4115
Chris Lattnerea044322010-07-29 02:01:43 +00004116const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004117 if (TheTargetCodeGenInfo)
4118 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004119
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004120 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00004121 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004122 default:
Chris Lattnerea044322010-07-29 02:01:43 +00004123 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004124
Derek Schuff9ed63f82012-09-06 17:37:28 +00004125 case llvm::Triple::le32:
4126 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00004127 case llvm::Triple::mips:
4128 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004129 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00004130
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004131 case llvm::Triple::mips64:
4132 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00004133 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00004134
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004135 case llvm::Triple::arm:
4136 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00004137 {
4138 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00004139
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004140 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00004141 Kind = ARMABIInfo::APCS;
4142 else if (CodeGenOpts.FloatABI == "hard")
4143 Kind = ARMABIInfo::AAPCS_VFP;
4144
Derek Schuff263366f2012-10-16 22:30:41 +00004145 switch (Triple.getOS()) {
4146 case llvm::Triple::NativeClient:
4147 return *(TheTargetCodeGenInfo =
4148 new NaClARMTargetCodeGenInfo(Types, Kind));
4149 default:
4150 return *(TheTargetCodeGenInfo =
4151 new ARMTargetCodeGenInfo(Types, Kind));
4152 }
Sandeep Patel34c1af82011-04-05 00:23:47 +00004153 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004154
John McCallec853ba2010-03-11 00:10:12 +00004155 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00004156 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00004157 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00004158 if (Triple.isOSBinFormatELF())
4159 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
4160 else
4161 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00004162
Peter Collingbourneedb66f32012-05-20 23:28:41 +00004163 case llvm::Triple::nvptx:
4164 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00004165 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00004166
Wesley Peck276fdf42010-12-19 19:57:51 +00004167 case llvm::Triple::mblaze:
4168 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
4169
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004170 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00004171 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00004172
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004173 case llvm::Triple::tce:
4174 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4175
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004176 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004177 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004178
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004179 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004180 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004181 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4182 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004183
4184 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004185 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004186 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004187 case llvm::Triple::AuroraUX:
4188 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004189 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004190 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004191 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004192 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004193 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4194 false,
4195 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004196
4197 case llvm::Triple::Win32:
4198 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004199 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4200 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004201
4202 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004203 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004204 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4205 false,
4206 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004207 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004208 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004209
Eli Friedmanee1ad992011-12-02 00:11:43 +00004210 case llvm::Triple::x86_64: {
4211 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4212
Chris Lattnerf13721d2010-08-31 16:44:54 +00004213 switch (Triple.getOS()) {
4214 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004215 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004216 case llvm::Triple::Cygwin:
4217 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
Derek Schuff263366f2012-10-16 22:30:41 +00004218 case llvm::Triple::NativeClient:
4219 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004220 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004221 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4222 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004223 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004224 }
Tony Linthicum96319392011-12-12 21:14:55 +00004225 case llvm::Triple::hexagon:
4226 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004227 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004228}