blob: 3136245a9a1133f08a9bf25d406501e0fa0b508b [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 Wendlingfac63102012-10-10 03:13:20 +0000971 Fn->addAttribute(~0U,
972 llvm::Attributes::constructStackAlignmentFromInt(16));
Charles Davis74f72932010-02-13 15:54:06 +0000973 }
974 }
975}
976
John McCall6374c332010-03-06 00:35:14 +0000977bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
978 CodeGen::CodeGenFunction &CGF,
979 llvm::Value *Address) const {
980 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCall6374c332010-03-06 00:35:14 +0000981
Chris Lattner8b418682012-02-07 00:39:47 +0000982 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000983
John McCall6374c332010-03-06 00:35:14 +0000984 // 0-7 are the eight integer registers; the order is different
985 // on Darwin (for EH), but the range is the same.
986 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000987 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000988
989 if (CGF.CGM.isTargetDarwin()) {
990 // 12-16 are st(0..4). Not sure why we stop at 4.
991 // These have size 16, which is sizeof(long double) on
992 // platforms with 8-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +0000993 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000994 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000995
John McCall6374c332010-03-06 00:35:14 +0000996 } else {
997 // 9 is %eflags, which doesn't get a size on Darwin for some
998 // reason.
999 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1000
1001 // 11-16 are st(0..5). Not sure why we stop at 5.
1002 // These have size 12, which is sizeof(long double) on
1003 // platforms with 4-byte alignment for that type.
Chris Lattner8b418682012-02-07 00:39:47 +00001004 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
John McCallaeeb7012010-05-27 06:19:26 +00001005 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1006 }
John McCall6374c332010-03-06 00:35:14 +00001007
1008 return false;
1009}
1010
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001011//===----------------------------------------------------------------------===//
1012// X86-64 ABI Implementation
1013//===----------------------------------------------------------------------===//
1014
1015
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001016namespace {
1017/// X86_64ABIInfo - The X86_64 ABI information.
1018class X86_64ABIInfo : public ABIInfo {
1019 enum Class {
1020 Integer = 0,
1021 SSE,
1022 SSEUp,
1023 X87,
1024 X87Up,
1025 ComplexX87,
1026 NoClass,
1027 Memory
1028 };
1029
1030 /// merge - Implement the X86_64 ABI merging algorithm.
1031 ///
1032 /// Merge an accumulating classification \arg Accum with a field
1033 /// classification \arg Field.
1034 ///
1035 /// \param Accum - The accumulating classification. This should
1036 /// always be either NoClass or the result of a previous merge
1037 /// call. In addition, this should never be Memory (the caller
1038 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001039 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001040
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001041 /// postMerge - Implement the X86_64 ABI post merging algorithm.
1042 ///
1043 /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1044 /// final MEMORY or SSE classes when necessary.
1045 ///
1046 /// \param AggregateSize - The size of the current aggregate in
1047 /// the classification process.
1048 ///
1049 /// \param Lo - The classification for the parts of the type
1050 /// residing in the low word of the containing object.
1051 ///
1052 /// \param Hi - The classification for the parts of the type
1053 /// residing in the higher words of the containing object.
1054 ///
1055 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1056
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001057 /// classify - Determine the x86_64 register classes in which the
1058 /// given type T should be passed.
1059 ///
1060 /// \param Lo - The classification for the parts of the type
1061 /// residing in the low word of the containing object.
1062 ///
1063 /// \param Hi - The classification for the parts of the type
1064 /// residing in the high word of the containing object.
1065 ///
1066 /// \param OffsetBase - The bit offset of this type in the
1067 /// containing object. Some parameters are classified different
1068 /// depending on whether they straddle an eightbyte boundary.
1069 ///
1070 /// If a word is unused its result will be NoClass; if a type should
1071 /// be passed in Memory then at least the classification of \arg Lo
1072 /// will be Memory.
1073 ///
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001074 /// The \arg Lo class will be NoClass iff the argument is ignored.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001075 ///
1076 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1077 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +00001078 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001079
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001080 llvm::Type *GetByteVectorType(QualType Ty) const;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001081 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1082 unsigned IROffset, QualType SourceTy,
1083 unsigned SourceOffset) const;
1084 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1085 unsigned IROffset, QualType SourceTy,
1086 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001087
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001088 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001089 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +00001090 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001091
1092 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001093 /// such that the argument will be passed in memory.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001094 ///
1095 /// \param freeIntRegs - The number of free integer registers remaining
1096 /// available.
1097 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001098
Chris Lattnera3c109b2010-07-29 02:16:43 +00001099 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001100
Bill Wendlingbb465d72010-10-18 03:41:31 +00001101 ABIArgInfo classifyArgumentType(QualType Ty,
Daniel Dunbaredfac032012-03-10 01:03:58 +00001102 unsigned freeIntRegs,
Bill Wendlingbb465d72010-10-18 03:41:31 +00001103 unsigned &neededInt,
Bill Wendling99aaae82010-10-18 23:51:38 +00001104 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001105
Eli Friedmanee1ad992011-12-02 00:11:43 +00001106 bool IsIllegalVectorType(QualType Ty) const;
1107
John McCall67a57732011-04-21 01:20:55 +00001108 /// The 0.98 ABI revision clarified a lot of ambiguities,
1109 /// unfortunately in ways that were not always consistent with
1110 /// certain previous compilers. In particular, platforms which
1111 /// required strict binary compatibility with older versions of GCC
1112 /// may need to exempt themselves.
1113 bool honorsRevision0_98() const {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001114 return !getContext().getTargetInfo().getTriple().isOSDarwin();
John McCall67a57732011-04-21 01:20:55 +00001115 }
1116
Eli Friedmanee1ad992011-12-02 00:11:43 +00001117 bool HasAVX;
Derek Schuffbabaf312012-10-11 15:52:22 +00001118 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1119 // 64-bit hardware.
1120 bool Has64BitPointers;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001121
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001122public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001123 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
Derek Schuffbabaf312012-10-11 15:52:22 +00001124 ABIInfo(CGT), HasAVX(hasavx),
1125 Has64BitPointers(CGT.getDataLayout().getPointerSize() == 8) {
1126 }
Chris Lattner9c254f02010-06-29 06:01:59 +00001127
John McCallde5d3c72012-02-17 03:33:10 +00001128 bool isPassedUsingAVXType(QualType type) const {
1129 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00001130 // The freeIntRegs argument doesn't matter here.
1131 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
John McCallde5d3c72012-02-17 03:33:10 +00001132 if (info.isDirect()) {
1133 llvm::Type *ty = info.getCoerceToType();
1134 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1135 return (vectorTy->getBitWidth() > 128);
1136 }
1137 return false;
1138 }
1139
Chris Lattneree5dcd02010-07-29 02:31:05 +00001140 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001141
1142 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1143 CodeGenFunction &CGF) const;
1144};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001145
Chris Lattnerf13721d2010-08-31 16:44:54 +00001146/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001147class WinX86_64ABIInfo : public ABIInfo {
1148
1149 ABIArgInfo classify(QualType Ty) const;
1150
Chris Lattnerf13721d2010-08-31 16:44:54 +00001151public:
NAKAMURA Takumia7573222011-01-17 22:56:31 +00001152 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1153
1154 virtual void computeInfo(CGFunctionInfo &FI) const;
Chris Lattnerf13721d2010-08-31 16:44:54 +00001155
1156 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1157 CodeGenFunction &CGF) const;
1158};
1159
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001160class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1161public:
Eli Friedmanee1ad992011-12-02 00:11:43 +00001162 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
Derek Schuffbabaf312012-10-11 15:52:22 +00001163 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
John McCall6374c332010-03-06 00:35:14 +00001164
John McCallde5d3c72012-02-17 03:33:10 +00001165 const X86_64ABIInfo &getABIInfo() const {
1166 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1167 }
1168
John McCall6374c332010-03-06 00:35:14 +00001169 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1170 return 7;
1171 }
1172
1173 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1174 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001175 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001176
John McCallaeeb7012010-05-27 06:19:26 +00001177 // 0-15 are the 16 integer registers.
1178 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001179 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +00001180 return false;
1181 }
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001182
Jay Foadef6de3d2011-07-11 09:56:20 +00001183 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001184 StringRef Constraint,
Jay Foadef6de3d2011-07-11 09:56:20 +00001185 llvm::Type* Ty) const {
Peter Collingbourne4b93d662011-02-19 23:03:58 +00001186 return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1187 }
1188
John McCallde5d3c72012-02-17 03:33:10 +00001189 bool isNoProtoCallVariadic(const CallArgList &args,
1190 const FunctionNoProtoType *fnType) const {
John McCall01f151e2011-09-21 08:08:30 +00001191 // The default CC on x86-64 sets %al to the number of SSA
1192 // registers used, and GCC sets this when calling an unprototyped
Eli Friedman3ed79032011-12-01 04:53:19 +00001193 // function, so we override the default behavior. However, don't do
Eli Friedman68805fe2011-12-06 03:08:26 +00001194 // that when AVX types are involved: the ABI explicitly states it is
1195 // undefined, and it doesn't work in practice because of how the ABI
1196 // defines varargs anyway.
John McCallde5d3c72012-02-17 03:33:10 +00001197 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
Eli Friedman3ed79032011-12-01 04:53:19 +00001198 bool HasAVXType = false;
John McCallde5d3c72012-02-17 03:33:10 +00001199 for (CallArgList::const_iterator
1200 it = args.begin(), ie = args.end(); it != ie; ++it) {
1201 if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1202 HasAVXType = true;
1203 break;
Eli Friedman3ed79032011-12-01 04:53:19 +00001204 }
1205 }
John McCallde5d3c72012-02-17 03:33:10 +00001206
Eli Friedman3ed79032011-12-01 04:53:19 +00001207 if (!HasAVXType)
1208 return true;
1209 }
John McCall01f151e2011-09-21 08:08:30 +00001210
John McCallde5d3c72012-02-17 03:33:10 +00001211 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
John McCall01f151e2011-09-21 08:08:30 +00001212 }
1213
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001214};
1215
Chris Lattnerf13721d2010-08-31 16:44:54 +00001216class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1217public:
1218 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1219 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1220
1221 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1222 return 7;
1223 }
1224
1225 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1226 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00001227 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001228
Chris Lattnerf13721d2010-08-31 16:44:54 +00001229 // 0-15 are the 16 integer registers.
1230 // 16 is %rip.
Chris Lattner8b418682012-02-07 00:39:47 +00001231 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
Chris Lattnerf13721d2010-08-31 16:44:54 +00001232 return false;
1233 }
1234};
1235
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236}
1237
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001238void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1239 Class &Hi) const {
1240 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1241 //
1242 // (a) If one of the classes is Memory, the whole argument is passed in
1243 // memory.
1244 //
1245 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1246 // memory.
1247 //
1248 // (c) If the size of the aggregate exceeds two eightbytes and the first
1249 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1250 // argument is passed in memory. NOTE: This is necessary to keep the
1251 // ABI working for processors that don't support the __m256 type.
1252 //
1253 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1254 //
1255 // Some of these are enforced by the merging logic. Others can arise
1256 // only with unions; for example:
1257 // union { _Complex double; unsigned; }
1258 //
1259 // Note that clauses (b) and (c) were added in 0.98.
1260 //
1261 if (Hi == Memory)
1262 Lo = Memory;
1263 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1264 Lo = Memory;
1265 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1266 Lo = Memory;
1267 if (Hi == SSEUp && Lo != SSE)
1268 Hi = SSE;
1269}
1270
Chris Lattner1090a9b2010-06-28 21:43:59 +00001271X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001272 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1273 // classified recursively so that always two fields are
1274 // considered. The resulting class is calculated according to
1275 // the classes of the fields in the eightbyte:
1276 //
1277 // (a) If both classes are equal, this is the resulting class.
1278 //
1279 // (b) If one of the classes is NO_CLASS, the resulting class is
1280 // the other class.
1281 //
1282 // (c) If one of the classes is MEMORY, the result is the MEMORY
1283 // class.
1284 //
1285 // (d) If one of the classes is INTEGER, the result is the
1286 // INTEGER.
1287 //
1288 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1289 // MEMORY is used as class.
1290 //
1291 // (f) Otherwise class SSE is used.
1292
1293 // Accum should never be memory (we should have returned) or
1294 // ComplexX87 (because this cannot be passed in a structure).
1295 assert((Accum != Memory && Accum != ComplexX87) &&
1296 "Invalid accumulated classification during merge.");
1297 if (Accum == Field || Field == NoClass)
1298 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001299 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001300 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001301 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001302 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001303 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001304 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001305 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1306 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001307 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001308 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001309}
1310
Chris Lattnerbcaedae2010-06-30 19:14:05 +00001311void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001312 Class &Lo, Class &Hi) const {
1313 // FIXME: This code can be simplified by introducing a simple value class for
1314 // Class pairs with appropriate constructor methods for the various
1315 // situations.
1316
1317 // FIXME: Some of the split computations are wrong; unaligned vectors
1318 // shouldn't be passed in registers for example, so there is no chance they
1319 // can straddle an eightbyte. Verify & simplify.
1320
1321 Lo = Hi = NoClass;
1322
1323 Class &Current = OffsetBase < 64 ? Lo : Hi;
1324 Current = Memory;
1325
John McCall183700f2009-09-21 23:43:11 +00001326 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001327 BuiltinType::Kind k = BT->getKind();
1328
1329 if (k == BuiltinType::Void) {
1330 Current = NoClass;
1331 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1332 Lo = Integer;
1333 Hi = Integer;
1334 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1335 Current = Integer;
1336 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1337 Current = SSE;
1338 } else if (k == BuiltinType::LongDouble) {
1339 Lo = X87;
1340 Hi = X87Up;
1341 }
1342 // FIXME: _Decimal32 and _Decimal64 are SSE.
1343 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +00001344 return;
1345 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001346
Chris Lattner1090a9b2010-06-28 21:43:59 +00001347 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001348 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +00001349 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001350 return;
1351 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001352
Chris Lattner1090a9b2010-06-28 21:43:59 +00001353 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001354 Current = Integer;
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->isMemberPointerType()) {
Derek Schuffbabaf312012-10-11 15:52:22 +00001359 if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
Daniel Dunbar67d438d2010-05-15 00:00:37 +00001360 Lo = Hi = Integer;
1361 else
1362 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +00001363 return;
1364 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001365
Chris Lattner1090a9b2010-06-28 21:43:59 +00001366 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001367 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001368 if (Size == 32) {
1369 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1370 // float> as integer.
1371 Current = Integer;
1372
1373 // If this type crosses an eightbyte boundary, it should be
1374 // split.
1375 uint64_t EB_Real = (OffsetBase) / 64;
1376 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1377 if (EB_Real != EB_Imag)
1378 Hi = Lo;
1379 } else if (Size == 64) {
1380 // gcc passes <1 x double> in memory. :(
1381 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1382 return;
1383
1384 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001385 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001386 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1387 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1388 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001389 Current = Integer;
1390 else
1391 Current = SSE;
1392
1393 // If this type crosses an eightbyte boundary, it should be
1394 // split.
1395 if (OffsetBase && OffsetBase != 64)
1396 Hi = Lo;
Eli Friedmanee1ad992011-12-02 00:11:43 +00001397 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001398 // Arguments of 256-bits are split into four eightbyte chunks. The
1399 // least significant one belongs to class SSE and all the others to class
1400 // SSEUP. The original Lo and Hi design considers that types can't be
1401 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1402 // This design isn't correct for 256-bits, but since there're no cases
1403 // where the upper parts would need to be inspected, avoid adding
1404 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001405 Lo = SSE;
1406 Hi = SSEUp;
1407 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001408 return;
1409 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001410
Chris Lattner1090a9b2010-06-28 21:43:59 +00001411 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001412 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001413
Chris Lattnerea044322010-07-29 02:01:43 +00001414 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001415 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001416 if (Size <= 64)
1417 Current = Integer;
1418 else if (Size <= 128)
1419 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001420 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001421 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001422 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001423 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001424 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001425 Current = ComplexX87;
1426
1427 // If this complex type crosses an eightbyte boundary then it
1428 // should be split.
1429 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001430 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001431 if (Hi == NoClass && EB_Real != EB_Imag)
1432 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001433
Chris Lattner1090a9b2010-06-28 21:43:59 +00001434 return;
1435 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001436
Chris Lattnerea044322010-07-29 02:01:43 +00001437 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001438 // Arrays are treated like structures.
1439
Chris Lattnerea044322010-07-29 02:01:43 +00001440 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001441
1442 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001443 // than four eightbytes, ..., it has class MEMORY.
1444 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001445 return;
1446
1447 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1448 // fields, it has class MEMORY.
1449 //
1450 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001451 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001452 return;
1453
1454 // Otherwise implement simplified merge. We could be smarter about
1455 // this, but it isn't worth it and would be harder to verify.
1456 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001457 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001458 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes089d8922011-07-12 01:27:38 +00001459
1460 // The only case a 256-bit wide vector could be used is when the array
1461 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1462 // to work for sizes wider than 128, early check and fallback to memory.
1463 if (Size > 128 && EltSize != 256)
1464 return;
1465
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001466 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1467 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001468 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001469 Lo = merge(Lo, FieldLo);
1470 Hi = merge(Hi, FieldHi);
1471 if (Lo == Memory || Hi == Memory)
1472 break;
1473 }
1474
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001475 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001476 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001477 return;
1478 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001479
Chris Lattner1090a9b2010-06-28 21:43:59 +00001480 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001481 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001482
1483 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001484 // than four eightbytes, ..., it has class MEMORY.
1485 if (Size > 256)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001486 return;
1487
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001488 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1489 // copy constructor or a non-trivial destructor, it is passed by invisible
1490 // reference.
1491 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1492 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001493
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001494 const RecordDecl *RD = RT->getDecl();
1495
1496 // Assume variable sized types are passed in memory.
1497 if (RD->hasFlexibleArrayMember())
1498 return;
1499
Chris Lattnerea044322010-07-29 02:01:43 +00001500 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001501
1502 // Reset Lo class, this will be recomputed.
1503 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001504
1505 // If this is a C++ record, classify the bases first.
1506 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1507 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1508 e = CXXRD->bases_end(); i != e; ++i) {
1509 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1510 "Unexpected base class!");
1511 const CXXRecordDecl *Base =
1512 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1513
1514 // Classify this field.
1515 //
1516 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1517 // single eightbyte, each is classified separately. Each eightbyte gets
1518 // initialized to class NO_CLASS.
1519 Class FieldLo, FieldHi;
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001520 uint64_t Offset =
1521 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
Chris Lattner9c254f02010-06-29 06:01:59 +00001522 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001523 Lo = merge(Lo, FieldLo);
1524 Hi = merge(Hi, FieldHi);
1525 if (Lo == Memory || Hi == Memory)
1526 break;
1527 }
1528 }
1529
1530 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001531 unsigned idx = 0;
Bruno Cardoso Lopes548e4782011-07-12 22:30:58 +00001532 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001533 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001534 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1535 bool BitField = i->isBitField();
1536
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001537 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1538 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001539 //
Bruno Cardoso Lopesb8981df2011-07-13 21:58:55 +00001540 // The only case a 256-bit wide vector could be used is when the struct
1541 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1542 // to work for sizes wider than 128, early check and fallback to memory.
1543 //
1544 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1545 Lo = Memory;
1546 return;
1547 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001548 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001549 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001550 Lo = Memory;
1551 return;
1552 }
1553
1554 // Classify this field.
1555 //
1556 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1557 // exceeds a single eightbyte, each is classified
1558 // separately. Each eightbyte gets initialized to class
1559 // NO_CLASS.
1560 Class FieldLo, FieldHi;
1561
1562 // Bit-fields require special handling, they do not force the
1563 // structure to be passed in memory even if unaligned, and
1564 // therefore they can straddle an eightbyte.
1565 if (BitField) {
1566 // Ignore padding bit-fields.
1567 if (i->isUnnamedBitfield())
1568 continue;
1569
1570 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001571 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001572
1573 uint64_t EB_Lo = Offset / 64;
1574 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1575 FieldLo = FieldHi = NoClass;
1576 if (EB_Lo) {
1577 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1578 FieldLo = NoClass;
1579 FieldHi = Integer;
1580 } else {
1581 FieldLo = Integer;
1582 FieldHi = EB_Hi ? Integer : NoClass;
1583 }
1584 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001585 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001586 Lo = merge(Lo, FieldLo);
1587 Hi = merge(Hi, FieldHi);
1588 if (Lo == Memory || Hi == Memory)
1589 break;
1590 }
1591
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001592 postMerge(Size, Lo, Hi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001593 }
1594}
1595
Chris Lattner9c254f02010-06-29 06:01:59 +00001596ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001597 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1598 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001599 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001600 // Treat an enum type as its underlying type.
1601 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1602 Ty = EnumTy->getDecl()->getIntegerType();
1603
1604 return (Ty->isPromotableIntegerType() ?
1605 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1606 }
1607
1608 return ABIArgInfo::getIndirect(0);
1609}
1610
Eli Friedmanee1ad992011-12-02 00:11:43 +00001611bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1612 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1613 uint64_t Size = getContext().getTypeSize(VecTy);
1614 unsigned LargestVector = HasAVX ? 256 : 128;
1615 if (Size <= 64 || Size > LargestVector)
1616 return true;
1617 }
1618
1619 return false;
1620}
1621
Daniel Dunbaredfac032012-03-10 01:03:58 +00001622ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1623 unsigned freeIntRegs) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001624 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1625 // place naturally.
Daniel Dunbaredfac032012-03-10 01:03:58 +00001626 //
1627 // This assumption is optimistic, as there could be free registers available
1628 // when we need to pass this argument in memory, and LLVM could try to pass
1629 // the argument in the free register. This does not seem to happen currently,
1630 // but this code would be much safer if we could mark the argument with
1631 // 'onstack'. See PR12193.
Eli Friedmanee1ad992011-12-02 00:11:43 +00001632 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001633 // Treat an enum type as its underlying type.
1634 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1635 Ty = EnumTy->getDecl()->getIntegerType();
1636
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001637 return (Ty->isPromotableIntegerType() ?
1638 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001639 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001640
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001641 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1642 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001643
Chris Lattner855d2272011-05-22 23:21:23 +00001644 // Compute the byval alignment. We specify the alignment of the byval in all
1645 // cases so that the mid-level optimizer knows the alignment of the byval.
1646 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
Daniel Dunbaredfac032012-03-10 01:03:58 +00001647
1648 // Attempt to avoid passing indirect results using byval when possible. This
1649 // is important for good codegen.
1650 //
1651 // We do this by coercing the value into a scalar type which the backend can
1652 // handle naturally (i.e., without using byval).
1653 //
1654 // For simplicity, we currently only do this when we have exhausted all of the
1655 // free integer registers. Doing this when there are free integer registers
1656 // would require more care, as we would have to ensure that the coerced value
1657 // did not claim the unused register. That would require either reording the
1658 // arguments to the function (so that any subsequent inreg values came first),
1659 // or only doing this optimization when there were no following arguments that
1660 // might be inreg.
1661 //
1662 // We currently expect it to be rare (particularly in well written code) for
1663 // arguments to be passed on the stack when there are still free integer
1664 // registers available (this would typically imply large structs being passed
1665 // by value), so this seems like a fair tradeoff for now.
1666 //
1667 // We can revisit this if the backend grows support for 'onstack' parameter
1668 // attributes. See PR12193.
1669 if (freeIntRegs == 0) {
1670 uint64_t Size = getContext().getTypeSize(Ty);
1671
1672 // If this type fits in an eightbyte, coerce it into the matching integral
1673 // type, which will end up on the stack (with alignment 8).
1674 if (Align == 8 && Size <= 64)
1675 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1676 Size));
1677 }
1678
Chris Lattner855d2272011-05-22 23:21:23 +00001679 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001680}
1681
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001682/// GetByteVectorType - The ABI specifies that a value should be passed in an
1683/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner0f408f52010-07-29 04:56:46 +00001684/// vector register.
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001685llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001686 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001687
Chris Lattner15842bd2010-07-29 05:02:29 +00001688 // Wrapper structs that just contain vectors are passed just like vectors,
1689 // strip them off if present.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001690 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner15842bd2010-07-29 05:02:29 +00001691 while (STy && STy->getNumElements() == 1) {
1692 IRType = STy->getElementType(0);
1693 STy = dyn_cast<llvm::StructType>(IRType);
1694 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001695
Bruno Cardoso Lopes528a8c72011-07-08 22:57:35 +00001696 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001697 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1698 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00001699 unsigned BitWidth = VT->getBitWidth();
Tanya Lattnerce275672011-11-28 23:18:11 +00001700 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner0f408f52010-07-29 04:56:46 +00001701 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1702 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1703 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1704 EltTy->isIntegerTy(128)))
1705 return VT;
1706 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001707
Chris Lattner0f408f52010-07-29 04:56:46 +00001708 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1709}
1710
Chris Lattnere2962be2010-07-29 07:30:00 +00001711/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1712/// is known to either be off the end of the specified type or being in
1713/// alignment padding. The user type specified is known to be at most 128 bits
1714/// in size, and have passed through X86_64ABIInfo::classify with a successful
1715/// classification that put one of the two halves in the INTEGER class.
1716///
1717/// It is conservatively correct to return false.
1718static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1719 unsigned EndBit, ASTContext &Context) {
1720 // If the bytes being queried are off the end of the type, there is no user
1721 // data hiding here. This handles analysis of builtins, vectors and other
1722 // types that don't contain interesting padding.
1723 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1724 if (TySize <= StartBit)
1725 return true;
1726
Chris Lattner021c3a32010-07-29 07:43:55 +00001727 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1728 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1729 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1730
1731 // Check each element to see if the element overlaps with the queried range.
1732 for (unsigned i = 0; i != NumElts; ++i) {
1733 // If the element is after the span we care about, then we're done..
1734 unsigned EltOffset = i*EltSize;
1735 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001736
Chris Lattner021c3a32010-07-29 07:43:55 +00001737 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1738 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1739 EndBit-EltOffset, Context))
1740 return false;
1741 }
1742 // If it overlaps no elements, then it is safe to process as padding.
1743 return true;
1744 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001745
Chris Lattnere2962be2010-07-29 07:30:00 +00001746 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1747 const RecordDecl *RD = RT->getDecl();
1748 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001749
Chris Lattnere2962be2010-07-29 07:30:00 +00001750 // If this is a C++ record, check the bases first.
1751 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1752 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1753 e = CXXRD->bases_end(); i != e; ++i) {
1754 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1755 "Unexpected base class!");
1756 const CXXRecordDecl *Base =
1757 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001758
Chris Lattnere2962be2010-07-29 07:30:00 +00001759 // If the base is after the span we care about, ignore it.
Benjamin Kramerd4f51982012-07-04 18:45:14 +00001760 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
Chris Lattnere2962be2010-07-29 07:30:00 +00001761 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001762
Chris Lattnere2962be2010-07-29 07:30:00 +00001763 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1764 if (!BitsContainNoUserData(i->getType(), BaseStart,
1765 EndBit-BaseOffset, Context))
1766 return false;
1767 }
1768 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001769
Chris Lattnere2962be2010-07-29 07:30:00 +00001770 // Verify that no field has data that overlaps the region of interest. Yes
1771 // this could be sped up a lot by being smarter about queried fields,
1772 // however we're only looking at structs up to 16 bytes, so we don't care
1773 // much.
1774 unsigned idx = 0;
1775 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1776 i != e; ++i, ++idx) {
1777 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001778
Chris Lattnere2962be2010-07-29 07:30:00 +00001779 // If we found a field after the region we care about, then we're done.
1780 if (FieldOffset >= EndBit) break;
1781
1782 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1783 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1784 Context))
1785 return false;
1786 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001787
Chris Lattnere2962be2010-07-29 07:30:00 +00001788 // If nothing in this record overlapped the area of interest, then we're
1789 // clean.
1790 return true;
1791 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001792
Chris Lattnere2962be2010-07-29 07:30:00 +00001793 return false;
1794}
1795
Chris Lattner0b362002010-07-29 18:39:32 +00001796/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1797/// float member at the specified offset. For example, {int,{float}} has a
1798/// float at offset 4. It is conservatively correct for this routine to return
1799/// false.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001800static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Micah Villmow25a6a842012-10-08 16:25:52 +00001801 const llvm::DataLayout &TD) {
Chris Lattner0b362002010-07-29 18:39:32 +00001802 // Base case if we find a float.
1803 if (IROffset == 0 && IRType->isFloatTy())
1804 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001805
Chris Lattner0b362002010-07-29 18:39:32 +00001806 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001807 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner0b362002010-07-29 18:39:32 +00001808 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1809 unsigned Elt = SL->getElementContainingOffset(IROffset);
1810 IROffset -= SL->getElementOffset(Elt);
1811 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1812 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001813
Chris Lattner0b362002010-07-29 18:39:32 +00001814 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001815 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1816 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner0b362002010-07-29 18:39:32 +00001817 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1818 IROffset -= IROffset/EltSize*EltSize;
1819 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1820 }
1821
1822 return false;
1823}
1824
Chris Lattnerf47c9442010-07-29 18:13:09 +00001825
1826/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1827/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001828llvm::Type *X86_64ABIInfo::
1829GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnerf47c9442010-07-29 18:13:09 +00001830 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001831 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001832 // pass as float if the last 4 bytes is just padding. This happens for
1833 // structs that contain 3 floats.
1834 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1835 SourceOffset*8+64, getContext()))
1836 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001837
Chris Lattner0b362002010-07-29 18:39:32 +00001838 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1839 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1840 // case.
Micah Villmow25a6a842012-10-08 16:25:52 +00001841 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1842 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001843 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001844
Chris Lattnerf47c9442010-07-29 18:13:09 +00001845 return llvm::Type::getDoubleTy(getVMContext());
1846}
1847
1848
Chris Lattner0d2656d2010-07-29 17:40:35 +00001849/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1850/// an 8-byte GPR. This means that we either have a scalar or we are talking
1851/// about the high or low part of an up-to-16-byte struct. This routine picks
1852/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001853/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1854/// etc).
1855///
1856/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1857/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1858/// the 8-byte value references. PrefType may be null.
1859///
1860/// SourceTy is the source level type for the entire argument. SourceOffset is
1861/// an offset into this that we're processing (which is always either 0 or 8).
1862///
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001863llvm::Type *X86_64ABIInfo::
1864GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner0d2656d2010-07-29 17:40:35 +00001865 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001866 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1867 // returning an 8-byte unit starting with it. See if we can safely use it.
1868 if (IROffset == 0) {
1869 // Pointers and int64's always fill the 8-byte unit.
Derek Schuffbabaf312012-10-11 15:52:22 +00001870 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1871 IRType->isIntegerTy(64))
Chris Lattnere2962be2010-07-29 07:30:00 +00001872 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001873
Chris Lattnere2962be2010-07-29 07:30:00 +00001874 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1875 // goodness in the source type is just tail padding. This is allowed to
1876 // kick in for struct {double,int} on the int, but not on
1877 // struct{double,int,int} because we wouldn't return the second int. We
1878 // have to do this analysis on the source type because we can't depend on
1879 // unions being lowered a specific way etc.
1880 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
Derek Schuffbabaf312012-10-11 15:52:22 +00001881 IRType->isIntegerTy(32) ||
1882 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
1883 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
1884 cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001885
Chris Lattnere2962be2010-07-29 07:30:00 +00001886 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1887 SourceOffset*8+64, getContext()))
1888 return IRType;
1889 }
1890 }
Chris Lattner49382de2010-07-28 22:44:07 +00001891
Chris Lattner2acc6e32011-07-18 04:24:23 +00001892 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001893 // If this is a struct, recurse into the field at the specified offset.
Micah Villmow25a6a842012-10-08 16:25:52 +00001894 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001895 if (IROffset < SL->getSizeInBytes()) {
1896 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1897 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001898
Chris Lattner0d2656d2010-07-29 17:40:35 +00001899 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1900 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001901 }
Chris Lattner49382de2010-07-28 22:44:07 +00001902 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001903
Chris Lattner2acc6e32011-07-18 04:24:23 +00001904 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001905 llvm::Type *EltTy = ATy->getElementType();
Micah Villmow25a6a842012-10-08 16:25:52 +00001906 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
Chris Lattner021c3a32010-07-29 07:43:55 +00001907 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001908 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1909 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001910 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001911
Chris Lattner49382de2010-07-28 22:44:07 +00001912 // Okay, we don't have any better idea of what to pass, so we pass this in an
1913 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001914 unsigned TySizeInBytes =
1915 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001916
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001917 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001918
Chris Lattner49382de2010-07-28 22:44:07 +00001919 // It is always safe to classify this as an integer type up to i64 that
1920 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001921 return llvm::IntegerType::get(getVMContext(),
1922 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001923}
1924
Chris Lattner66e7b682010-09-01 00:50:20 +00001925
1926/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1927/// be used as elements of a two register pair to pass or return, return a
1928/// first class aggregate to represent them. For example, if the low part of
1929/// a by-value argument should be passed as i32* and the high part as float,
1930/// return {i32*, float}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001931static llvm::Type *
Jay Foadef6de3d2011-07-11 09:56:20 +00001932GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Micah Villmow25a6a842012-10-08 16:25:52 +00001933 const llvm::DataLayout &TD) {
Chris Lattner66e7b682010-09-01 00:50:20 +00001934 // In order to correctly satisfy the ABI, we need to the high part to start
1935 // at offset 8. If the high and low parts we inferred are both 4-byte types
1936 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1937 // the second element at offset 8. Check for this:
1938 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1939 unsigned HiAlign = TD.getABITypeAlignment(Hi);
Micah Villmow25a6a842012-10-08 16:25:52 +00001940 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
Chris Lattner66e7b682010-09-01 00:50:20 +00001941 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001942
Chris Lattner66e7b682010-09-01 00:50:20 +00001943 // To handle this, we have to increase the size of the low part so that the
1944 // second element will start at an 8 byte offset. We can't increase the size
1945 // of the second element because it might make us access off the end of the
1946 // struct.
1947 if (HiStart != 8) {
1948 // There are only two sorts of types the ABI generation code can produce for
1949 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1950 // Promote these to a larger type.
1951 if (Lo->isFloatTy())
1952 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1953 else {
1954 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1955 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1956 }
1957 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001958
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001959 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001960
1961
Chris Lattner66e7b682010-09-01 00:50:20 +00001962 // Verify that the second element is at an 8-byte offset.
1963 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1964 "Invalid x86-64 argument pair!");
1965 return Result;
1966}
1967
Chris Lattner519f68c2010-07-28 23:06:14 +00001968ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001969classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001970 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1971 // classification algorithm.
1972 X86_64ABIInfo::Class Lo, Hi;
1973 classify(RetTy, 0, Lo, Hi);
1974
1975 // Check some invariants.
1976 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001977 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1978
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001979 llvm::Type *ResType = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001980 switch (Lo) {
1981 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001982 if (Hi == NoClass)
1983 return ABIArgInfo::getIgnore();
1984 // If the low part is just padding, it takes no register, leave ResType
1985 // null.
1986 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1987 "Unknown missing lo part");
1988 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001989
1990 case SSEUp:
1991 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00001992 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001993
1994 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1995 // hidden argument.
1996 case Memory:
1997 return getIndirectReturnResult(RetTy);
1998
1999 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2000 // available register of the sequence %rax, %rdx is used.
2001 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002002 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002003
Chris Lattnereb518b42010-07-29 21:42:50 +00002004 // If we have a sign or zero extended integer, make sure to return Extend
2005 // so that the parameter gets the right LLVM IR attributes.
2006 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2007 // Treat an enum type as its underlying type.
2008 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2009 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002010
Chris Lattnereb518b42010-07-29 21:42:50 +00002011 if (RetTy->isIntegralOrEnumerationType() &&
2012 RetTy->isPromotableIntegerType())
2013 return ABIArgInfo::getExtend();
2014 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002015 break;
2016
2017 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2018 // available SSE register of the sequence %xmm0, %xmm1 is used.
2019 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002020 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00002021 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002022
2023 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2024 // returned on the X87 stack in %st0 as 80-bit x87 number.
2025 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00002026 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00002027 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002028
2029 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2030 // part of the value is returned in %st0 and the imaginary part in
2031 // %st1.
2032 case ComplexX87:
2033 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner7650d952011-06-18 22:49:11 +00002034 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattnerea044322010-07-29 02:01:43 +00002035 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00002036 NULL);
2037 break;
2038 }
2039
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002040 llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00002041 switch (Hi) {
2042 // Memory was handled previously and X87 should
2043 // never occur as a hi class.
2044 case Memory:
2045 case X87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002046 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner519f68c2010-07-28 23:06:14 +00002047
2048 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00002049 case NoClass:
2050 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00002051
Chris Lattner3db4dde2010-09-01 00:20:33 +00002052 case Integer:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002053 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002054 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2055 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002056 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00002057 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002058 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002059 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2060 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00002061 break;
2062
2063 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002064 // is passed in the next available eightbyte chunk if the last used
2065 // vector register.
Chris Lattner519f68c2010-07-28 23:06:14 +00002066 //
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002067 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner519f68c2010-07-28 23:06:14 +00002068 case SSEUp:
2069 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002070 ResType = GetByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00002071 break;
2072
2073 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2074 // returned together with the previous X87 value in %st0.
2075 case X87Up:
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002076 // If X87Up is preceded by X87, we don't need to do
Chris Lattner519f68c2010-07-28 23:06:14 +00002077 // anything. However, in some cases with unions it may not be
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002078 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner519f68c2010-07-28 23:06:14 +00002079 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00002080 if (Lo != X87) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002081 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner3db4dde2010-09-01 00:20:33 +00002082 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
2083 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00002084 }
Chris Lattner519f68c2010-07-28 23:06:14 +00002085 break;
2086 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002087
Chris Lattner3db4dde2010-09-01 00:20:33 +00002088 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00002089 // known to pass in the high eightbyte of the result. We do this by forming a
2090 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00002091 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002092 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Chris Lattner519f68c2010-07-28 23:06:14 +00002093
Chris Lattnereb518b42010-07-29 21:42:50 +00002094 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00002095}
2096
Daniel Dunbaredfac032012-03-10 01:03:58 +00002097ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2098 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2099 const
2100{
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002101 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00002102 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002103
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002104 // Check some invariants.
2105 // FIXME: Enforce these by construction.
2106 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002107 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2108
2109 neededInt = 0;
2110 neededSSE = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002111 llvm::Type *ResType = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002112 switch (Lo) {
2113 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00002114 if (Hi == NoClass)
2115 return ABIArgInfo::getIgnore();
2116 // If the low part is just padding, it takes no register, leave ResType
2117 // null.
2118 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2119 "Unknown missing lo part");
2120 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002121
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002122 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2123 // on the stack.
2124 case Memory:
2125
2126 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2127 // COMPLEX_X87, it is passed in memory.
2128 case X87:
2129 case ComplexX87:
Eli Friedmanded137f2011-06-29 07:04:55 +00002130 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2131 ++neededInt;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002132 return getIndirectResult(Ty, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002133
2134 case SSEUp:
2135 case X87Up:
David Blaikieb219cfc2011-09-23 05:06:16 +00002136 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002137
2138 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2139 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2140 // and %r9 is used.
2141 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00002142 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002143
Chris Lattner49382de2010-07-28 22:44:07 +00002144 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002145 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00002146
2147 // If we have a sign or zero extended integer, make sure to return Extend
2148 // so that the parameter gets the right LLVM IR attributes.
2149 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2150 // Treat an enum type as its underlying type.
2151 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2152 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002153
Chris Lattnereb518b42010-07-29 21:42:50 +00002154 if (Ty->isIntegralOrEnumerationType() &&
2155 Ty->isPromotableIntegerType())
2156 return ABIArgInfo::getExtend();
2157 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002158
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002159 break;
2160
2161 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2162 // available SSE register is used, the registers are taken in the
2163 // order from %xmm0 to %xmm7.
Bill Wendlingbb465d72010-10-18 03:41:31 +00002164 case SSE: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002165 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman14508ff2011-07-02 00:57:27 +00002166 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling99aaae82010-10-18 23:51:38 +00002167 ++neededSSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002168 break;
2169 }
Bill Wendlingbb465d72010-10-18 03:41:31 +00002170 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002171
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002172 llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002173 switch (Hi) {
2174 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002175 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002176 // which is passed in memory.
2177 case Memory:
2178 case X87:
2179 case ComplexX87:
David Blaikieb219cfc2011-09-23 05:06:16 +00002180 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002181
2182 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002183
Chris Lattner645406a2010-09-01 00:24:35 +00002184 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002185 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00002186 // Pick an 8-byte type based on the preferred type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002187 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002188
Chris Lattner645406a2010-09-01 00:24:35 +00002189 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2190 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002191 break;
2192
2193 // X87Up generally doesn't occur here (long double is passed in
2194 // memory), except in situations involving unions.
2195 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00002196 case SSE:
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002197 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002198
Chris Lattner645406a2010-09-01 00:24:35 +00002199 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
2200 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00002201
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002202 ++neededSSE;
2203 break;
2204
2205 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2206 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002207 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00002209 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes4943c152011-07-11 22:41:29 +00002210 ResType = GetByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002211 break;
2212 }
2213
Chris Lattner645406a2010-09-01 00:24:35 +00002214 // If a high part was specified, merge it together with the low part. It is
2215 // known to pass in the high eightbyte of the result. We do this by forming a
2216 // first class struct aggregate with the high and low part: {low, high}
2217 if (HighPart)
Micah Villmow25a6a842012-10-08 16:25:52 +00002218 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002219
Chris Lattnereb518b42010-07-29 21:42:50 +00002220 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002221}
2222
Chris Lattneree5dcd02010-07-29 02:31:05 +00002223void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002224
Chris Lattnera3c109b2010-07-29 02:16:43 +00002225 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002226
2227 // Keep track of the number of assigned registers.
Bill Wendling99aaae82010-10-18 23:51:38 +00002228 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002229
2230 // If the return value is indirect, then the hidden argument is consuming one
2231 // integer register.
2232 if (FI.getReturnInfo().isIndirect())
2233 --freeIntRegs;
2234
2235 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2236 // get assigned (in left-to-right order) for passing as follows...
2237 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2238 it != ie; ++it) {
Bill Wendling99aaae82010-10-18 23:51:38 +00002239 unsigned neededInt, neededSSE;
Daniel Dunbaredfac032012-03-10 01:03:58 +00002240 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2241 neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002242
2243 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2244 // eightbyte of an argument, the whole argument is passed on the
2245 // stack. If registers have already been assigned for some
2246 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling99aaae82010-10-18 23:51:38 +00002247 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002248 freeIntRegs -= neededInt;
2249 freeSSERegs -= neededSSE;
2250 } else {
Daniel Dunbaredfac032012-03-10 01:03:58 +00002251 it->info = getIndirectResult(it->type, freeIntRegs);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002252 }
2253 }
2254}
2255
2256static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2257 QualType Ty,
2258 CodeGenFunction &CGF) {
2259 llvm::Value *overflow_arg_area_p =
2260 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2261 llvm::Value *overflow_arg_area =
2262 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2263
2264 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2265 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedman8d2fe422011-11-18 02:44:19 +00002266 // It isn't stated explicitly in the standard, but in practice we use
2267 // alignment greater than 16 where necessary.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002268 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2269 if (Align > 8) {
Eli Friedman8d2fe422011-11-18 02:44:19 +00002270 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson0032b272009-08-13 21:57:51 +00002271 llvm::Value *Offset =
Eli Friedman8d2fe422011-11-18 02:44:19 +00002272 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002273 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2274 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00002275 CGF.Int64Ty);
Eli Friedman8d2fe422011-11-18 02:44:19 +00002276 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002277 overflow_arg_area =
2278 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2279 overflow_arg_area->getType(),
2280 "overflow_arg_area.align");
2281 }
2282
2283 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002284 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002285 llvm::Value *Res =
2286 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002287 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002288
2289 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2290 // l->overflow_arg_area + sizeof(type).
2291 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2292 // an 8 byte boundary.
2293
2294 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00002295 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00002296 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002297 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2298 "overflow_arg_area.next");
2299 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2300
2301 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2302 return Res;
2303}
2304
2305llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2306 CodeGenFunction &CGF) const {
2307 // Assume that va_list type is correct; should be pointer to LLVM type:
2308 // struct {
2309 // i32 gp_offset;
2310 // i32 fp_offset;
2311 // i8* overflow_arg_area;
2312 // i8* reg_save_area;
2313 // };
Bill Wendling99aaae82010-10-18 23:51:38 +00002314 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002315
Chris Lattnera14db752010-03-11 18:19:55 +00002316 Ty = CGF.getContext().getCanonicalType(Ty);
Daniel Dunbaredfac032012-03-10 01:03:58 +00002317 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002318
2319 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2320 // in the registers. If not go to step 7.
2321 if (!neededInt && !neededSSE)
2322 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2323
2324 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2325 // general purpose registers needed to pass type and num_fp to hold
2326 // the number of floating point registers needed.
2327
2328 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2329 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2330 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2331 //
2332 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2333 // register save space).
2334
2335 llvm::Value *InRegs = 0;
2336 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2337 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2338 if (neededInt) {
2339 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2340 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00002341 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2342 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002343 }
2344
2345 if (neededSSE) {
2346 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2347 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2348 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00002349 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2350 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002351 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2352 }
2353
2354 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2355 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2356 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2357 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2358
2359 // Emit code to load the value if it was passed in registers.
2360
2361 CGF.EmitBlock(InRegBlock);
2362
2363 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2364 // an offset of l->gp_offset and/or l->fp_offset. This may require
2365 // copying to a temporary location in case the parameter is passed
2366 // in different register classes or requires an alignment greater
2367 // than 8 for general purpose registers and 16 for XMM registers.
2368 //
2369 // FIXME: This really results in shameful code when we end up needing to
2370 // collect arguments from different places; often what should result in a
2371 // simple assembling of a structure from scattered addresses has many more
2372 // loads than necessary. Can we clean this up?
Chris Lattner2acc6e32011-07-18 04:24:23 +00002373 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002374 llvm::Value *RegAddr =
2375 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2376 "reg_save_area");
2377 if (neededInt && neededSSE) {
2378 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00002379 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002380 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002381 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2382 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002383 llvm::Type *TyLo = ST->getElementType(0);
2384 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00002385 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002386 "Unexpected ABI info for mixed regs");
Chris Lattner2acc6e32011-07-18 04:24:23 +00002387 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2388 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002389 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2390 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00002391 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2392 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002393 llvm::Value *V =
2394 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2395 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2396 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2397 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2398
Owen Andersona1cf15f2009-07-14 23:10:40 +00002399 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002400 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002401 } else if (neededInt) {
2402 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2403 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00002404 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002405 } else if (neededSSE == 1) {
2406 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2407 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2408 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002409 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002410 assert(neededSSE == 2 && "Invalid number of needed registers!");
2411 // SSE registers are spaced 16 bytes apart in the register save
2412 // area, we need to collect the two eightbytes together.
2413 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00002414 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner8b418682012-02-07 00:39:47 +00002415 llvm::Type *DoubleTy = CGF.DoubleTy;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002416 llvm::Type *DblPtrTy =
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002417 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002418 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002419 DoubleTy, NULL);
2420 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2421 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2422 DblPtrTy));
2423 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2424 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2425 DblPtrTy));
2426 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2427 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2428 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002429 }
2430
2431 // AMD64-ABI 3.5.7p5: Step 5. Set:
2432 // l->gp_offset = l->gp_offset + num_gp * 8
2433 // l->fp_offset = l->fp_offset + num_fp * 16.
2434 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002435 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002436 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2437 gp_offset_p);
2438 }
2439 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002440 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002441 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2442 fp_offset_p);
2443 }
2444 CGF.EmitBranch(ContBlock);
2445
2446 // Emit code to load the value if it was passed in memory.
2447
2448 CGF.EmitBlock(InMemBlock);
2449 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2450
2451 // Return the appropriate result.
2452
2453 CGF.EmitBlock(ContBlock);
Jay Foadbbf3bac2011-03-30 11:28:58 +00002454 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002455 "vaarg.addr");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002456 ResAddr->addIncoming(RegAddr, InRegBlock);
2457 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002458 return ResAddr;
2459}
2460
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002461ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2462
2463 if (Ty->isVoidType())
2464 return ABIArgInfo::getIgnore();
2465
2466 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2467 Ty = EnumTy->getDecl()->getIntegerType();
2468
2469 uint64_t Size = getContext().getTypeSize(Ty);
2470
2471 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002472 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2473 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002474 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2475
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002476 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2477 if (Size == 128 &&
Eli Friedman55fc7e22012-01-25 22:46:34 +00002478 getContext().getTargetInfo().getTriple().getOS()
2479 == llvm::Triple::MinGW32)
NAKAMURA Takumi6f174332011-02-22 03:56:57 +00002480 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2481 Size));
2482
2483 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2484 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2485 if (Size <= 64 &&
NAKAMURA Takumiff8be0e2011-01-19 00:11:33 +00002486 (Size & (Size - 1)) == 0)
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002487 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2488 Size));
2489
2490 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2491 }
2492
2493 if (Ty->isPromotableIntegerType())
2494 return ABIArgInfo::getExtend();
2495
2496 return ABIArgInfo::getDirect();
2497}
2498
2499void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2500
2501 QualType RetTy = FI.getReturnType();
2502 FI.getReturnInfo() = classify(RetTy);
2503
NAKAMURA Takumia7573222011-01-17 22:56:31 +00002504 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2505 it != ie; ++it)
2506 it->info = classify(it->type);
2507}
2508
Chris Lattnerf13721d2010-08-31 16:44:54 +00002509llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2510 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002511 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002512
Chris Lattnerf13721d2010-08-31 16:44:54 +00002513 CGBuilderTy &Builder = CGF.Builder;
2514 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2515 "ap");
2516 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2517 llvm::Type *PTy =
2518 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2519 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2520
2521 uint64_t Offset =
2522 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2523 llvm::Value *NextAddr =
2524 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2525 "ap.next");
2526 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2527
2528 return AddrTyped;
2529}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002530
John McCallec853ba2010-03-11 00:10:12 +00002531// PowerPC-32
2532
2533namespace {
2534class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2535public:
Chris Lattnerea044322010-07-29 02:01:43 +00002536 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002537
John McCallec853ba2010-03-11 00:10:12 +00002538 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2539 // This is recovered from gcc output.
2540 return 1; // r1 is the dedicated stack pointer
2541 }
2542
2543 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002544 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002545};
2546
2547}
2548
2549bool
2550PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2551 llvm::Value *Address) const {
2552 // This is calculated from the LLVM and GCC tables and verified
2553 // against gcc output. AFAIK all ABIs use the same encoding.
2554
2555 CodeGen::CGBuilderTy &Builder = CGF.Builder;
John McCallec853ba2010-03-11 00:10:12 +00002556
Chris Lattner8b418682012-02-07 00:39:47 +00002557 llvm::IntegerType *i8 = CGF.Int8Ty;
John McCallec853ba2010-03-11 00:10:12 +00002558 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2559 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2560 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2561
2562 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002563 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002564
2565 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002566 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002567
2568 // 64-76 are various 4-byte special-purpose registers:
2569 // 64: mq
2570 // 65: lr
2571 // 66: ctr
2572 // 67: ap
2573 // 68-75 cr0-7
2574 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002575 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002576
2577 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002578 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002579
2580 // 109: vrsave
2581 // 110: vscr
2582 // 111: spe_acc
2583 // 112: spefscr
2584 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002585 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002586
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002587 return false;
John McCallec853ba2010-03-11 00:10:12 +00002588}
2589
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002590// PowerPC-64
2591
2592namespace {
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002593/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2594class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2595
2596public:
2597 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2598
2599 // TODO: Could override computeInfo to model the ABI more completely if
2600 // it would be helpful. Example: We might remove the byVal flag from
2601 // aggregate arguments that fit in a register to avoid pushing them to
2602 // memory on function entry. Note that this is a performance optimization,
2603 // not a compliance issue. In general we prefer to keep ABI details in
2604 // the back end where possible, but modifying an argument flag seems like
2605 // a good thing to do before invoking the back end.
2606
2607 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2608 QualType Ty,
2609 CodeGenFunction &CGF) const;
2610};
2611
2612class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2613public:
2614 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2615 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2616
2617 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2618 // This is recovered from gcc output.
2619 return 1; // r1 is the dedicated stack pointer
2620 }
2621
2622 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2623 llvm::Value *Address) const;
2624};
2625
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002626class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2627public:
2628 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2629
2630 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2631 // This is recovered from gcc output.
2632 return 1; // r1 is the dedicated stack pointer
2633 }
2634
2635 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2636 llvm::Value *Address) const;
2637};
2638
2639}
2640
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002641// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2642llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2643 QualType Ty,
2644 CodeGenFunction &CGF) const {
2645 llvm::Type *BP = CGF.Int8PtrTy;
2646 llvm::Type *BPP = CGF.Int8PtrPtrTy;
2647
2648 CGBuilderTy &Builder = CGF.Builder;
2649 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2650 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2651
2652 // Handle address alignment for type alignment > 64 bits. Although
2653 // long double normally requires 16-byte alignment, this is not the
2654 // case when it is passed as an argument; so handle that special case.
2655 const BuiltinType *BT = Ty->getAs<BuiltinType>();
2656 unsigned TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2657
2658 if (TyAlign > 8 && (!BT || !BT->isFloatingPoint())) {
2659 assert((TyAlign & (TyAlign - 1)) == 0 &&
2660 "Alignment is not power of 2!");
2661 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2662 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(TyAlign - 1));
2663 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(~(TyAlign - 1)));
2664 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2665 }
2666
2667 // Update the va_list pointer.
2668 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2669 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2670 llvm::Value *NextAddr =
2671 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2672 "ap.next");
2673 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2674
2675 // If the argument is smaller than 8 bytes, it is right-adjusted in
2676 // its doubleword slot. Adjust the pointer to pick it up from the
2677 // correct offset.
2678 if (SizeInBytes < 8) {
2679 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2680 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2681 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2682 }
2683
2684 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2685 return Builder.CreateBitCast(Addr, PTy);
2686}
2687
2688static bool
2689PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2690 llvm::Value *Address) {
Roman Divacky0fbc4b92012-05-09 18:22:46 +00002691 // This is calculated from the LLVM and GCC tables and verified
2692 // against gcc output. AFAIK all ABIs use the same encoding.
2693
2694 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2695
2696 llvm::IntegerType *i8 = CGF.Int8Ty;
2697 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2698 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2699 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2700
2701 // 0-31: r0-31, the 8-byte general-purpose registers
2702 AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2703
2704 // 32-63: fp0-31, the 8-byte floating-point registers
2705 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2706
2707 // 64-76 are various 4-byte special-purpose registers:
2708 // 64: mq
2709 // 65: lr
2710 // 66: ctr
2711 // 67: ap
2712 // 68-75 cr0-7
2713 // 76: xer
2714 AssignToArrayRange(Builder, Address, Four8, 64, 76);
2715
2716 // 77-108: v0-31, the 16-byte vector registers
2717 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2718
2719 // 109: vrsave
2720 // 110: vscr
2721 // 111: spe_acc
2722 // 112: spefscr
2723 // 113: sfp
2724 AssignToArrayRange(Builder, Address, Four8, 109, 113);
2725
2726 return false;
2727}
John McCallec853ba2010-03-11 00:10:12 +00002728
Bill Schmidt2fc107f2012-10-03 19:18:57 +00002729bool
2730PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
2731 CodeGen::CodeGenFunction &CGF,
2732 llvm::Value *Address) const {
2733
2734 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2735}
2736
2737bool
2738PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2739 llvm::Value *Address) const {
2740
2741 return PPC64_initDwarfEHRegSizeTable(CGF, Address);
2742}
2743
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002744//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002745// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002746//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002747
2748namespace {
2749
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002750class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002751public:
2752 enum ABIKind {
2753 APCS = 0,
2754 AAPCS = 1,
2755 AAPCS_VFP
2756 };
2757
2758private:
2759 ABIKind Kind;
2760
2761public:
Chris Lattnerea044322010-07-29 02:01:43 +00002762 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002763
John McCall49e34be2011-08-30 01:42:09 +00002764 bool isEABI() const {
Eli Friedman55fc7e22012-01-25 22:46:34 +00002765 StringRef Env =
2766 getContext().getTargetInfo().getTriple().getEnvironmentName();
Logan Chien94a71422012-09-02 09:30:11 +00002767 return (Env == "gnueabi" || Env == "eabi" ||
2768 Env == "android" || Env == "androideabi");
John McCall49e34be2011-08-30 01:42:09 +00002769 }
2770
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002771private:
2772 ABIKind getABIKind() const { return Kind; }
2773
Chris Lattnera3c109b2010-07-29 02:16:43 +00002774 ABIArgInfo classifyReturnType(QualType RetTy) const;
2775 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002776
Chris Lattneree5dcd02010-07-29 02:31:05 +00002777 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002778
2779 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2780 CodeGenFunction &CGF) const;
2781};
2782
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002783class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2784public:
Chris Lattnerea044322010-07-29 02:01:43 +00002785 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2786 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002787
John McCall49e34be2011-08-30 01:42:09 +00002788 const ARMABIInfo &getABIInfo() const {
2789 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2790 }
2791
John McCall6374c332010-03-06 00:35:14 +00002792 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2793 return 13;
2794 }
Roman Divacky09345d12011-05-18 19:36:54 +00002795
Chris Lattner5f9e2722011-07-23 10:55:15 +00002796 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCallf85e1932011-06-15 23:02:42 +00002797 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2798 }
2799
Roman Divacky09345d12011-05-18 19:36:54 +00002800 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2801 llvm::Value *Address) const {
Chris Lattner8b418682012-02-07 00:39:47 +00002802 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
Roman Divacky09345d12011-05-18 19:36:54 +00002803
2804 // 0-15 are the 16 integer registers.
Chris Lattner8b418682012-02-07 00:39:47 +00002805 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
Roman Divacky09345d12011-05-18 19:36:54 +00002806 return false;
2807 }
John McCall49e34be2011-08-30 01:42:09 +00002808
2809 unsigned getSizeOfUnwindException() const {
2810 if (getABIInfo().isEABI()) return 88;
2811 return TargetCodeGenInfo::getSizeOfUnwindException();
2812 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002813};
2814
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002815}
2816
Chris Lattneree5dcd02010-07-29 02:31:05 +00002817void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002818 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002819 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002820 it != ie; ++it)
2821 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002822
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002823 // Always honor user-specified calling convention.
2824 if (FI.getCallingConvention() != llvm::CallingConv::C)
2825 return;
2826
2827 // Calling convention as default by an ABI.
Rafael Espindola25117ab2010-06-16 16:13:39 +00002828 llvm::CallingConv::ID DefaultCC;
John McCall49e34be2011-08-30 01:42:09 +00002829 if (isEABI())
Rafael Espindola25117ab2010-06-16 16:13:39 +00002830 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002831 else
2832 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002833
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002834 // If user did not ask for specific calling convention explicitly (e.g. via
2835 // pcs attribute), set effective calling convention if it's different than ABI
2836 // default.
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002837 switch (getABIKind()) {
2838 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002839 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2840 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002841 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002842 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002843 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2844 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002845 break;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002846 case AAPCS_VFP:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002847 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2848 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002849 break;
2850 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002851}
2852
Bob Wilson194f06a2011-08-03 05:58:22 +00002853/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2854/// aggregate. If HAMembers is non-null, the number of base elements
2855/// contained in the type is returned through it; this is used for the
2856/// recursive calls that check aggregate component types.
2857static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2858 ASTContext &Context,
2859 uint64_t *HAMembers = 0) {
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002860 uint64_t Members = 0;
Bob Wilson194f06a2011-08-03 05:58:22 +00002861 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2862 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2863 return false;
2864 Members *= AT->getSize().getZExtValue();
2865 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2866 const RecordDecl *RD = RT->getDecl();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002867 if (RD->hasFlexibleArrayMember())
Bob Wilson194f06a2011-08-03 05:58:22 +00002868 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002869
Bob Wilson194f06a2011-08-03 05:58:22 +00002870 Members = 0;
2871 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2872 i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002873 const FieldDecl *FD = *i;
Bob Wilson194f06a2011-08-03 05:58:22 +00002874 uint64_t FldMembers;
2875 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2876 return false;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002877
2878 Members = (RD->isUnion() ?
2879 std::max(Members, FldMembers) : Members + FldMembers);
Bob Wilson194f06a2011-08-03 05:58:22 +00002880 }
2881 } else {
2882 Members = 1;
2883 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2884 Members = 2;
2885 Ty = CT->getElementType();
2886 }
2887
2888 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2889 // double, or 64-bit or 128-bit vectors.
2890 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2891 if (BT->getKind() != BuiltinType::Float &&
Tim Northoveradfa45f2012-07-20 22:29:29 +00002892 BT->getKind() != BuiltinType::Double &&
2893 BT->getKind() != BuiltinType::LongDouble)
Bob Wilson194f06a2011-08-03 05:58:22 +00002894 return false;
2895 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2896 unsigned VecSize = Context.getTypeSize(VT);
2897 if (VecSize != 64 && VecSize != 128)
2898 return false;
2899 } else {
2900 return false;
2901 }
2902
2903 // The base type must be the same for all members. Vector types of the
2904 // same total size are treated as being equivalent here.
2905 const Type *TyPtr = Ty.getTypePtr();
2906 if (!Base)
2907 Base = TyPtr;
2908 if (Base != TyPtr &&
2909 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2910 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2911 return false;
2912 }
2913
2914 // Homogeneous Aggregates can have at most 4 members of the base type.
2915 if (HAMembers)
2916 *HAMembers = Members;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002917
2918 return (Members > 0 && Members <= 4);
Bob Wilson194f06a2011-08-03 05:58:22 +00002919}
2920
Chris Lattnera3c109b2010-07-29 02:16:43 +00002921ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002922 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002923 // Treat an enum type as its underlying type.
2924 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2925 Ty = EnumTy->getDecl()->getIntegerType();
2926
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002927 return (Ty->isPromotableIntegerType() ?
2928 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002929 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002930
Daniel Dunbar42025572009-09-14 21:54:03 +00002931 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002932 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002933 return ABIArgInfo::getIgnore();
2934
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002935 // Structures with either a non-trivial destructor or a non-trivial
2936 // copy constructor are always indirect.
2937 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2938 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2939
Bob Wilson194f06a2011-08-03 05:58:22 +00002940 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2941 // Homogeneous Aggregates need to be expanded.
2942 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002943 if (isHomogeneousAggregate(Ty, Base, getContext())) {
2944 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson194f06a2011-08-03 05:58:22 +00002945 return ABIArgInfo::getExpand();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00002946 }
Bob Wilson194f06a2011-08-03 05:58:22 +00002947 }
2948
Manman Ren634b3d22012-08-13 21:23:55 +00002949 // Support byval for ARM.
2950 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
2951 getContext().getTypeAlign(Ty) > 64) {
2952 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
Eli Friedman79f30982012-08-09 00:31:40 +00002953 }
2954
Daniel Dunbar8aa87c72010-09-23 01:54:28 +00002955 // Otherwise, pass by coercing to a structure of the appropriate size.
Chris Lattner2acc6e32011-07-18 04:24:23 +00002956 llvm::Type* ElemTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002957 unsigned SizeRegs;
Eli Friedman79f30982012-08-09 00:31:40 +00002958 // FIXME: Try to match the types of the arguments more accurately where
2959 // we can.
2960 if (getContext().getTypeAlign(Ty) <= 32) {
Bob Wilson53fc1a62011-08-01 23:39:04 +00002961 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2962 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Manman Ren78eb76e2012-06-25 22:04:00 +00002963 } else {
Manman Ren78eb76e2012-06-25 22:04:00 +00002964 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2965 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Stuart Hastings67d097e2011-04-27 17:24:02 +00002966 }
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002967
Chris Lattner9cbe4f02011-07-09 17:41:47 +00002968 llvm::Type *STy =
Chris Lattner7650d952011-06-18 22:49:11 +00002969 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastingsb7f62d02011-04-28 18:16:06 +00002970 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002971}
2972
Chris Lattnera3c109b2010-07-29 02:16:43 +00002973static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002974 llvm::LLVMContext &VMContext) {
2975 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2976 // is called integer-like if its size is less than or equal to one word, and
2977 // the offset of each of its addressable sub-fields is zero.
2978
2979 uint64_t Size = Context.getTypeSize(Ty);
2980
2981 // Check that the type fits in a word.
2982 if (Size > 32)
2983 return false;
2984
2985 // FIXME: Handle vector types!
2986 if (Ty->isVectorType())
2987 return false;
2988
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002989 // Float types are never treated as "integer like".
2990 if (Ty->isRealFloatingType())
2991 return false;
2992
Daniel Dunbar98303b92009-09-13 08:03:58 +00002993 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002994 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002995 return true;
2996
Daniel Dunbar45815812010-02-01 23:31:26 +00002997 // Small complex integer types are "integer like".
2998 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2999 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003000
3001 // Single element and zero sized arrays should be allowed, by the definition
3002 // above, but they are not.
3003
3004 // Otherwise, it must be a record type.
3005 const RecordType *RT = Ty->getAs<RecordType>();
3006 if (!RT) return false;
3007
3008 // Ignore records with flexible arrays.
3009 const RecordDecl *RD = RT->getDecl();
3010 if (RD->hasFlexibleArrayMember())
3011 return false;
3012
3013 // Check that all sub-fields are at offset 0, and are themselves "integer
3014 // like".
3015 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3016
3017 bool HadField = false;
3018 unsigned idx = 0;
3019 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3020 i != e; ++i, ++idx) {
David Blaikie581deb32012-06-06 20:45:41 +00003021 const FieldDecl *FD = *i;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003022
Daniel Dunbar679855a2010-01-29 03:22:29 +00003023 // Bit-fields are not addressable, we only need to verify they are "integer
3024 // like". We still have to disallow a subsequent non-bitfield, for example:
3025 // struct { int : 0; int x }
3026 // is non-integer like according to gcc.
3027 if (FD->isBitField()) {
3028 if (!RD->isUnion())
3029 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003030
Daniel Dunbar679855a2010-01-29 03:22:29 +00003031 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3032 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003033
Daniel Dunbar679855a2010-01-29 03:22:29 +00003034 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00003035 }
3036
Daniel Dunbar679855a2010-01-29 03:22:29 +00003037 // Check if this field is at offset 0.
3038 if (Layout.getFieldOffset(idx) != 0)
3039 return false;
3040
Daniel Dunbar98303b92009-09-13 08:03:58 +00003041 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3042 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003043
Daniel Dunbar679855a2010-01-29 03:22:29 +00003044 // Only allow at most one field in a structure. This doesn't match the
3045 // wording above, but follows gcc in situations with a field following an
3046 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00003047 if (!RD->isUnion()) {
3048 if (HadField)
3049 return false;
3050
3051 HadField = true;
3052 }
3053 }
3054
3055 return true;
3056}
3057
Chris Lattnera3c109b2010-07-29 02:16:43 +00003058ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003059 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003060 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00003061
Daniel Dunbarf554b1c2010-09-23 01:54:32 +00003062 // Large vector types should be returned via memory.
3063 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3064 return ABIArgInfo::getIndirect(0);
3065
John McCalld608cdb2010-08-22 10:59:02 +00003066 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003067 // Treat an enum type as its underlying type.
3068 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3069 RetTy = EnumTy->getDecl()->getIntegerType();
3070
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00003071 return (RetTy->isPromotableIntegerType() ?
3072 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00003073 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003074
Rafael Espindola0eb1d972010-06-08 02:42:08 +00003075 // Structures with either a non-trivial destructor or a non-trivial
3076 // copy constructor are always indirect.
3077 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3078 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3079
Daniel Dunbar98303b92009-09-13 08:03:58 +00003080 // Are we following APCS?
3081 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00003082 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00003083 return ABIArgInfo::getIgnore();
3084
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003085 // Complex types are all returned as packed integers.
3086 //
3087 // FIXME: Consider using 2 x vector types if the back end handles them
3088 // correctly.
3089 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00003090 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00003091 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00003092
Daniel Dunbar98303b92009-09-13 08:03:58 +00003093 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003094 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00003095 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003096 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00003097 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003098 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003099 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003100 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3101 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00003102 }
3103
3104 // Otherwise return in memory.
3105 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003106 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00003107
3108 // Otherwise this is an AAPCS variant.
3109
Chris Lattnera3c109b2010-07-29 02:16:43 +00003110 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00003111 return ABIArgInfo::getIgnore();
3112
Bob Wilson3b694fa2011-11-02 04:51:36 +00003113 // Check for homogeneous aggregates with AAPCS-VFP.
3114 if (getABIKind() == AAPCS_VFP) {
3115 const Type *Base = 0;
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003116 if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3117 assert(Base && "Base class should be set for homogeneous aggregate");
Bob Wilson3b694fa2011-11-02 04:51:36 +00003118 // Homogeneous Aggregates are returned directly.
3119 return ABIArgInfo::getDirect();
Anton Korobeynikoveaf856d2012-04-13 11:22:00 +00003120 }
Bob Wilson3b694fa2011-11-02 04:51:36 +00003121 }
3122
Daniel Dunbar98303b92009-09-13 08:03:58 +00003123 // Aggregates <= 4 bytes are returned in r0; other aggregates
3124 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00003125 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00003126 if (Size <= 32) {
3127 // Return in the smallest viable integer type.
3128 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00003129 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003130 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00003131 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3132 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00003133 }
3134
Daniel Dunbar98303b92009-09-13 08:03:58 +00003135 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003136}
3137
3138llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00003139 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003140 llvm::Type *BP = CGF.Int8PtrTy;
3141 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003142
3143 CGBuilderTy &Builder = CGF.Builder;
Chris Lattner8b418682012-02-07 00:39:47 +00003144 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003145 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindolae164c182011-08-02 22:33:37 +00003146 // Handle address alignment for type alignment > 32 bits
3147 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
3148 if (TyAlign > 4) {
3149 assert((TyAlign & (TyAlign - 1)) == 0 &&
3150 "Alignment is not power of 2!");
3151 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3152 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3153 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
3154 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
3155 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003156 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003157 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003158 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3159
3160 uint64_t Offset =
3161 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3162 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00003163 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003164 "ap.next");
3165 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3166
3167 return AddrTyped;
3168}
3169
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003170//===----------------------------------------------------------------------===//
Justin Holewinski2c585b92012-05-24 17:43:12 +00003171// NVPTX ABI Implementation
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003172//===----------------------------------------------------------------------===//
3173
3174namespace {
3175
Justin Holewinski2c585b92012-05-24 17:43:12 +00003176class NVPTXABIInfo : public ABIInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003177public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003178 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003179
3180 ABIArgInfo classifyReturnType(QualType RetTy) const;
3181 ABIArgInfo classifyArgumentType(QualType Ty) const;
3182
3183 virtual void computeInfo(CGFunctionInfo &FI) const;
3184 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3185 CodeGenFunction &CFG) const;
3186};
3187
Justin Holewinski2c585b92012-05-24 17:43:12 +00003188class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003189public:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003190 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
3191 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
Justin Holewinski818eafb2011-10-05 17:58:44 +00003192
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003193 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3194 CodeGen::CodeGenModule &M) const;
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003195};
3196
Justin Holewinski2c585b92012-05-24 17:43:12 +00003197ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003198 if (RetTy->isVoidType())
3199 return ABIArgInfo::getIgnore();
3200 if (isAggregateTypeForABI(RetTy))
3201 return ABIArgInfo::getIndirect(0);
3202 return ABIArgInfo::getDirect();
3203}
3204
Justin Holewinski2c585b92012-05-24 17:43:12 +00003205ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003206 if (isAggregateTypeForABI(Ty))
3207 return ABIArgInfo::getIndirect(0);
3208
3209 return ABIArgInfo::getDirect();
3210}
3211
Justin Holewinski2c585b92012-05-24 17:43:12 +00003212void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003213 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3214 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3215 it != ie; ++it)
3216 it->info = classifyArgumentType(it->type);
3217
3218 // Always honor user-specified calling convention.
3219 if (FI.getCallingConvention() != llvm::CallingConv::C)
3220 return;
3221
3222 // Calling convention as default by an ABI.
Justin Holewinski2c585b92012-05-24 17:43:12 +00003223 // We're still using the PTX_Kernel/PTX_Device calling conventions here,
3224 // but we should switch to NVVM metadata later on.
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003225 llvm::CallingConv::ID DefaultCC;
David Blaikie4e4d0842012-03-11 07:00:24 +00003226 const LangOptions &LangOpts = getContext().getLangOpts();
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003227 if (LangOpts.OpenCL || LangOpts.CUDA) {
3228 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003229 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski818eafb2011-10-05 17:58:44 +00003230 } else {
3231 // If we are in standard C/C++ mode, use the triple to decide on the default
3232 StringRef Env =
3233 getContext().getTargetInfo().getTriple().getEnvironmentName();
3234 if (Env == "device")
3235 DefaultCC = llvm::CallingConv::PTX_Device;
3236 else
3237 DefaultCC = llvm::CallingConv::PTX_Kernel;
3238 }
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003239 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003240
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003241}
3242
Justin Holewinski2c585b92012-05-24 17:43:12 +00003243llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3244 CodeGenFunction &CFG) const {
3245 llvm_unreachable("NVPTX does not support varargs");
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003246}
3247
Justin Holewinski2c585b92012-05-24 17:43:12 +00003248void NVPTXTargetCodeGenInfo::
3249SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3250 CodeGen::CodeGenModule &M) const{
Justin Holewinski818eafb2011-10-05 17:58:44 +00003251 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3252 if (!FD) return;
3253
3254 llvm::Function *F = cast<llvm::Function>(GV);
3255
3256 // Perform special handling in OpenCL mode
David Blaikie4e4d0842012-03-11 07:00:24 +00003257 if (M.getLangOpts().OpenCL) {
Justin Holewinski818eafb2011-10-05 17:58:44 +00003258 // Use OpenCL function attributes to set proper calling conventions
3259 // By default, all functions are device functions
Justin Holewinski818eafb2011-10-05 17:58:44 +00003260 if (FD->hasAttr<OpenCLKernelAttr>()) {
3261 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003262 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003263 // And kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003264 F->addFnAttr(llvm::Attributes::NoInline);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003265 }
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003266 }
Justin Holewinski818eafb2011-10-05 17:58:44 +00003267
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003268 // Perform special handling in CUDA mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003269 if (M.getLangOpts().CUDA) {
Peter Collingbourne744d90b2011-10-06 16:49:54 +00003270 // CUDA __global__ functions get a kernel calling convention. Since
3271 // __global__ functions cannot be called from the device, we do not
3272 // need to set the noinline attribute.
3273 if (FD->getAttr<CUDAGlobalAttr>())
3274 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski818eafb2011-10-05 17:58:44 +00003275 }
3276}
3277
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003278}
3279
3280//===----------------------------------------------------------------------===//
Wesley Peck276fdf42010-12-19 19:57:51 +00003281// MBlaze ABI Implementation
3282//===----------------------------------------------------------------------===//
3283
3284namespace {
3285
3286class MBlazeABIInfo : public ABIInfo {
3287public:
3288 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3289
3290 bool isPromotableIntegerType(QualType Ty) const;
3291
3292 ABIArgInfo classifyReturnType(QualType RetTy) const;
3293 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3294
3295 virtual void computeInfo(CGFunctionInfo &FI) const {
3296 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3297 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3298 it != ie; ++it)
3299 it->info = classifyArgumentType(it->type);
3300 }
3301
3302 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3303 CodeGenFunction &CGF) const;
3304};
3305
3306class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3307public:
3308 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3309 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3310 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3311 CodeGen::CodeGenModule &M) const;
3312};
3313
3314}
3315
3316bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3317 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3318 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3319 switch (BT->getKind()) {
3320 case BuiltinType::Bool:
3321 case BuiltinType::Char_S:
3322 case BuiltinType::Char_U:
3323 case BuiltinType::SChar:
3324 case BuiltinType::UChar:
3325 case BuiltinType::Short:
3326 case BuiltinType::UShort:
3327 return true;
3328 default:
3329 return false;
3330 }
3331 return false;
3332}
3333
3334llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3335 CodeGenFunction &CGF) const {
3336 // FIXME: Implement
3337 return 0;
3338}
3339
3340
3341ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3342 if (RetTy->isVoidType())
3343 return ABIArgInfo::getIgnore();
3344 if (isAggregateTypeForABI(RetTy))
3345 return ABIArgInfo::getIndirect(0);
3346
3347 return (isPromotableIntegerType(RetTy) ?
3348 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3349}
3350
3351ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3352 if (isAggregateTypeForABI(Ty))
3353 return ABIArgInfo::getIndirect(0);
3354
3355 return (isPromotableIntegerType(Ty) ?
3356 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3357}
3358
3359void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3360 llvm::GlobalValue *GV,
3361 CodeGen::CodeGenModule &M)
3362 const {
3363 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3364 if (!FD) return;
NAKAMURA Takumi125b4cb2011-02-17 08:50:50 +00003365
Wesley Peck276fdf42010-12-19 19:57:51 +00003366 llvm::CallingConv::ID CC = llvm::CallingConv::C;
3367 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3368 CC = llvm::CallingConv::MBLAZE_INTR;
3369 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3370 CC = llvm::CallingConv::MBLAZE_SVOL;
3371
3372 if (CC != llvm::CallingConv::C) {
3373 // Handle 'interrupt_handler' attribute:
3374 llvm::Function *F = cast<llvm::Function>(GV);
3375
3376 // Step 1: Set ISR calling convention.
3377 F->setCallingConv(CC);
3378
3379 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003380 F->addFnAttr(llvm::Attributes::NoInline);
Wesley Peck276fdf42010-12-19 19:57:51 +00003381 }
3382
3383 // Step 3: Emit _interrupt_handler alias.
3384 if (CC == llvm::CallingConv::MBLAZE_INTR)
3385 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3386 "_interrupt_handler", GV, &M.getModule());
3387}
3388
3389
3390//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003391// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003392//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003393
3394namespace {
3395
3396class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3397public:
Chris Lattnerea044322010-07-29 02:01:43 +00003398 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3399 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003400 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3401 CodeGen::CodeGenModule &M) const;
3402};
3403
3404}
3405
3406void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3407 llvm::GlobalValue *GV,
3408 CodeGen::CodeGenModule &M) const {
3409 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3410 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3411 // Handle 'interrupt' attribute:
3412 llvm::Function *F = cast<llvm::Function>(GV);
3413
3414 // Step 1: Set ISR calling convention.
3415 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3416
3417 // Step 2: Add attributes goodness.
Bill Wendlingfac63102012-10-10 03:13:20 +00003418 F->addFnAttr(llvm::Attributes::NoInline);
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003419
3420 // Step 3: Emit ISR vector alias.
3421 unsigned Num = attr->getNumber() + 0xffe0;
3422 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003423 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003424 GV, &M.getModule());
3425 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003426 }
3427}
3428
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003429//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00003430// MIPS ABI Implementation. This works for both little-endian and
3431// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00003432//===----------------------------------------------------------------------===//
3433
John McCallaeeb7012010-05-27 06:19:26 +00003434namespace {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003435class MipsABIInfo : public ABIInfo {
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003436 bool IsO32;
Akira Hatanakac359f202012-07-03 19:24:06 +00003437 unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3438 void CoerceToIntArgs(uint64_t TySize,
3439 SmallVector<llvm::Type*, 8> &ArgList) const;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003440 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003441 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003442 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003443public:
Akira Hatanakab551dd32011-11-03 00:05:50 +00003444 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
Akira Hatanakac359f202012-07-03 19:24:06 +00003445 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3446 StackAlignInBytes(IsO32 ? 8 : 16) {}
Akira Hatanaka619e8872011-06-02 00:09:17 +00003447
3448 ABIArgInfo classifyReturnType(QualType RetTy) const;
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003449 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003450 virtual void computeInfo(CGFunctionInfo &FI) const;
3451 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3452 CodeGenFunction &CGF) const;
3453};
3454
John McCallaeeb7012010-05-27 06:19:26 +00003455class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003456 unsigned SizeOfUnwindException;
John McCallaeeb7012010-05-27 06:19:26 +00003457public:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003458 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3459 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3460 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCallaeeb7012010-05-27 06:19:26 +00003461
3462 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3463 return 29;
3464 }
3465
3466 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00003467 llvm::Value *Address) const;
John McCall49e34be2011-08-30 01:42:09 +00003468
3469 unsigned getSizeOfUnwindException() const {
Akira Hatanakae624fa02011-09-20 18:23:28 +00003470 return SizeOfUnwindException;
John McCall49e34be2011-08-30 01:42:09 +00003471 }
John McCallaeeb7012010-05-27 06:19:26 +00003472};
3473}
3474
Akira Hatanakac359f202012-07-03 19:24:06 +00003475void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3476 SmallVector<llvm::Type*, 8> &ArgList) const {
3477 llvm::IntegerType *IntTy =
3478 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003479
3480 // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
3481 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
3482 ArgList.push_back(IntTy);
3483
3484 // If necessary, add one more integer type to ArgList.
3485 unsigned R = TySize % (MinABIStackAlignInBytes * 8);
3486
3487 if (R)
3488 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003489}
3490
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003491// In N32/64, an aligned double precision floating point field is passed in
3492// a register.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003493llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
Akira Hatanakac359f202012-07-03 19:24:06 +00003494 SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3495
3496 if (IsO32) {
3497 CoerceToIntArgs(TySize, ArgList);
3498 return llvm::StructType::get(getVMContext(), ArgList);
3499 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003500
Akira Hatanaka2afd23d2012-01-12 00:52:17 +00003501 if (Ty->isComplexType())
3502 return CGT.ConvertType(Ty);
Akira Hatanaka6d1080f2012-01-10 23:12:19 +00003503
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003504 const RecordType *RT = Ty->getAs<RecordType>();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003505
Akira Hatanakac359f202012-07-03 19:24:06 +00003506 // Unions/vectors are passed in integer registers.
3507 if (!RT || !RT->isStructureOrClassType()) {
3508 CoerceToIntArgs(TySize, ArgList);
3509 return llvm::StructType::get(getVMContext(), ArgList);
3510 }
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003511
3512 const RecordDecl *RD = RT->getDecl();
3513 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003514 assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003515
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003516 uint64_t LastOffset = 0;
3517 unsigned idx = 0;
3518 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3519
Akira Hatanakaa34e9212012-02-09 19:54:16 +00003520 // Iterate over fields in the struct/class and check if there are any aligned
3521 // double fields.
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003522 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3523 i != e; ++i, ++idx) {
David Blaikie262bc182012-04-30 02:36:29 +00003524 const QualType Ty = i->getType();
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003525 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3526
3527 if (!BT || BT->getKind() != BuiltinType::Double)
3528 continue;
3529
3530 uint64_t Offset = Layout.getFieldOffset(idx);
3531 if (Offset % 64) // Ignore doubles that are not aligned.
3532 continue;
3533
3534 // Add ((Offset - LastOffset) / 64) args of type i64.
3535 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3536 ArgList.push_back(I64);
3537
3538 // Add double type.
3539 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3540 LastOffset = Offset + 64;
3541 }
3542
Akira Hatanakac359f202012-07-03 19:24:06 +00003543 CoerceToIntArgs(TySize - LastOffset, IntArgList);
3544 ArgList.append(IntArgList.begin(), IntArgList.end());
Akira Hatanakad5a257f2011-11-02 23:54:49 +00003545
3546 return llvm::StructType::get(getVMContext(), ArgList);
3547}
3548
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003549llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003550 assert((Offset % MinABIStackAlignInBytes) == 0);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003551
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003552 if ((Align - 1) & Offset)
3553 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3554
3555 return 0;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003556}
Akira Hatanaka9659d592012-01-10 22:44:52 +00003557
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003558ABIArgInfo
3559MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003560 uint64_t OrigOffset = Offset;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003561 uint64_t TySize = getContext().getTypeSize(Ty);
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003562 uint64_t Align = getContext().getTypeAlign(Ty) / 8;
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003563
Akira Hatanakac359f202012-07-03 19:24:06 +00003564 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3565 (uint64_t)StackAlignInBytes);
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003566 Offset = llvm::RoundUpToAlignment(Offset, Align);
3567 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003568
Akira Hatanakac359f202012-07-03 19:24:06 +00003569 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
Akira Hatanaka619e8872011-06-02 00:09:17 +00003570 // Ignore empty aggregates.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003571 if (TySize == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003572 return ABIArgInfo::getIgnore();
3573
Akira Hatanaka511949b2011-08-01 18:09:58 +00003574 // Records with non trivial destructors/constructors should not be passed
3575 // by value.
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003576 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003577 Offset = OrigOffset + MinABIStackAlignInBytes;
Akira Hatanaka511949b2011-08-01 18:09:58 +00003578 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003579 }
Akira Hatanaka511949b2011-08-01 18:09:58 +00003580
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003581 // If we have reached here, aggregates are passed directly by coercing to
3582 // another structure type. Padding is inserted if the offset of the
3583 // aggregate is unaligned.
3584 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
3585 getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003586 }
3587
3588 // Treat an enum type as its underlying type.
3589 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3590 Ty = EnumTy->getDecl()->getIntegerType();
3591
Akira Hatanakaa33fd392012-01-09 19:31:25 +00003592 if (Ty->isPromotableIntegerType())
3593 return ABIArgInfo::getExtend();
3594
3595 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
Akira Hatanaka619e8872011-06-02 00:09:17 +00003596}
3597
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003598llvm::Type*
3599MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
Akira Hatanakada54ff32012-02-09 18:49:26 +00003600 const RecordType *RT = RetTy->getAs<RecordType>();
Akira Hatanakac359f202012-07-03 19:24:06 +00003601 SmallVector<llvm::Type*, 8> RTList;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003602
Akira Hatanakada54ff32012-02-09 18:49:26 +00003603 if (RT && RT->isStructureOrClassType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003604 const RecordDecl *RD = RT->getDecl();
Akira Hatanakada54ff32012-02-09 18:49:26 +00003605 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3606 unsigned FieldCnt = Layout.getFieldCount();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003607
Akira Hatanakada54ff32012-02-09 18:49:26 +00003608 // N32/64 returns struct/classes in floating point registers if the
3609 // following conditions are met:
3610 // 1. The size of the struct/class is no larger than 128-bit.
3611 // 2. The struct/class has one or two fields all of which are floating
3612 // point types.
3613 // 3. The offset of the first field is zero (this follows what gcc does).
3614 //
3615 // Any other composite results are returned in integer registers.
3616 //
3617 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3618 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3619 for (; b != e; ++b) {
David Blaikie262bc182012-04-30 02:36:29 +00003620 const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003621
Akira Hatanakada54ff32012-02-09 18:49:26 +00003622 if (!BT || !BT->isFloatingPoint())
3623 break;
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003624
David Blaikie262bc182012-04-30 02:36:29 +00003625 RTList.push_back(CGT.ConvertType(b->getType()));
Akira Hatanakada54ff32012-02-09 18:49:26 +00003626 }
3627
3628 if (b == e)
3629 return llvm::StructType::get(getVMContext(), RTList,
3630 RD->hasAttr<PackedAttr>());
3631
3632 RTList.clear();
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003633 }
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003634 }
3635
Akira Hatanakac359f202012-07-03 19:24:06 +00003636 CoerceToIntArgs(Size, RTList);
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003637 return llvm::StructType::get(getVMContext(), RTList);
3638}
3639
Akira Hatanaka619e8872011-06-02 00:09:17 +00003640ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
Akira Hatanakaa8536c02012-01-23 23:18:57 +00003641 uint64_t Size = getContext().getTypeSize(RetTy);
3642
3643 if (RetTy->isVoidType() || Size == 0)
Akira Hatanaka619e8872011-06-02 00:09:17 +00003644 return ABIArgInfo::getIgnore();
3645
Akira Hatanaka8aeb1472012-05-11 21:01:17 +00003646 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003647 if (Size <= 128) {
3648 if (RetTy->isAnyComplexType())
3649 return ABIArgInfo::getDirect();
3650
Akira Hatanakac359f202012-07-03 19:24:06 +00003651 // O32 returns integer vectors in registers.
3652 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3653 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3654
Akira Hatanaka526cdfb2012-02-08 01:31:22 +00003655 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
Akira Hatanakac7ecc2e2012-01-04 03:34:42 +00003656 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3657 }
Akira Hatanaka619e8872011-06-02 00:09:17 +00003658
3659 return ABIArgInfo::getIndirect(0);
3660 }
3661
3662 // Treat an enum type as its underlying type.
3663 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3664 RetTy = EnumTy->getDecl()->getIntegerType();
3665
3666 return (RetTy->isPromotableIntegerType() ?
3667 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3668}
3669
3670void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
Akira Hatanakacc662542012-01-12 01:10:09 +00003671 ABIArgInfo &RetInfo = FI.getReturnInfo();
3672 RetInfo = classifyReturnType(FI.getReturnType());
3673
3674 // Check if a pointer to an aggregate is passed as a hidden argument.
Akira Hatanaka91338cf2012-05-11 21:56:58 +00003675 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
Akira Hatanakacc662542012-01-12 01:10:09 +00003676
Akira Hatanaka619e8872011-06-02 00:09:17 +00003677 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3678 it != ie; ++it)
Akira Hatanakaf0cc2082012-01-07 00:25:33 +00003679 it->info = classifyArgumentType(it->type, Offset);
Akira Hatanaka619e8872011-06-02 00:09:17 +00003680}
3681
3682llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3683 CodeGenFunction &CGF) const {
Chris Lattner8b418682012-02-07 00:39:47 +00003684 llvm::Type *BP = CGF.Int8PtrTy;
3685 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003686
3687 CGBuilderTy &Builder = CGF.Builder;
3688 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3689 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003690 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003691 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3692 llvm::Value *AddrTyped;
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003693 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
3694 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003695
3696 if (TypeAlign > MinABIStackAlignInBytes) {
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003697 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
3698 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
3699 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
3700 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003701 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3702 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3703 }
3704 else
3705 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3706
3707 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003708 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003709 uint64_t Offset =
3710 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3711 llvm::Value *NextAddr =
Akira Hatanaka8f675e42012-01-23 23:59:52 +00003712 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
Akira Hatanakac35e69d2011-08-01 20:48:01 +00003713 "ap.next");
3714 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3715
3716 return AddrTyped;
Akira Hatanaka619e8872011-06-02 00:09:17 +00003717}
3718
John McCallaeeb7012010-05-27 06:19:26 +00003719bool
3720MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3721 llvm::Value *Address) const {
3722 // This information comes from gcc's implementation, which seems to
3723 // as canonical as it gets.
3724
John McCallaeeb7012010-05-27 06:19:26 +00003725 // Everything on MIPS is 4 bytes. Double-precision FP registers
3726 // are aliased to pairs of single-precision FP registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003727 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
John McCallaeeb7012010-05-27 06:19:26 +00003728
3729 // 0-31 are the general purpose registers, $0 - $31.
3730 // 32-63 are the floating-point registers, $f0 - $f31.
3731 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3732 // 66 is the (notional, I think) register for signal-handler return.
Chris Lattner8b418682012-02-07 00:39:47 +00003733 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
John McCallaeeb7012010-05-27 06:19:26 +00003734
3735 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3736 // They are one bit wide and ignored here.
3737
3738 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3739 // (coprocessor 1 is the FP unit)
3740 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3741 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3742 // 176-181 are the DSP accumulator registers.
Chris Lattner8b418682012-02-07 00:39:47 +00003743 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
John McCallaeeb7012010-05-27 06:19:26 +00003744 return false;
3745}
3746
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003747//===----------------------------------------------------------------------===//
3748// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3749// Currently subclassed only to implement custom OpenCL C function attribute
3750// handling.
3751//===----------------------------------------------------------------------===//
3752
3753namespace {
3754
3755class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3756public:
3757 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3758 : DefaultTargetCodeGenInfo(CGT) {}
3759
3760 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3761 CodeGen::CodeGenModule &M) const;
3762};
3763
3764void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3765 llvm::GlobalValue *GV,
3766 CodeGen::CodeGenModule &M) const {
3767 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3768 if (!FD) return;
3769
3770 llvm::Function *F = cast<llvm::Function>(GV);
3771
David Blaikie4e4d0842012-03-11 07:00:24 +00003772 if (M.getLangOpts().OpenCL) {
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003773 if (FD->hasAttr<OpenCLKernelAttr>()) {
3774 // OpenCL C Kernel functions are not subject to inlining
Bill Wendlingfac63102012-10-10 03:13:20 +00003775 F->addFnAttr(llvm::Attributes::NoInline);
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003776
3777 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3778
3779 // Convert the reqd_work_group_size() attributes to metadata.
3780 llvm::LLVMContext &Context = F->getContext();
3781 llvm::NamedMDNode *OpenCLMetadata =
3782 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3783
3784 SmallVector<llvm::Value*, 5> Operands;
3785 Operands.push_back(F);
3786
Chris Lattner8b418682012-02-07 00:39:47 +00003787 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3788 llvm::APInt(32,
3789 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3790 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3791 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003792 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
Chris Lattner8b418682012-02-07 00:39:47 +00003793 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
3794 llvm::APInt(32,
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003795 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3796
3797 // Add a boolean constant operand for "required" (true) or "hint" (false)
3798 // for implementing the work_group_size_hint attr later. Currently
3799 // always true as the hint is not yet implemented.
Chris Lattner8b418682012-02-07 00:39:47 +00003800 Operands.push_back(llvm::ConstantInt::getTrue(Context));
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00003801 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3802 }
3803 }
3804 }
3805}
3806
3807}
John McCallaeeb7012010-05-27 06:19:26 +00003808
Tony Linthicum96319392011-12-12 21:14:55 +00003809//===----------------------------------------------------------------------===//
3810// Hexagon ABI Implementation
3811//===----------------------------------------------------------------------===//
3812
3813namespace {
3814
3815class HexagonABIInfo : public ABIInfo {
3816
3817
3818public:
3819 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3820
3821private:
3822
3823 ABIArgInfo classifyReturnType(QualType RetTy) const;
3824 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3825
3826 virtual void computeInfo(CGFunctionInfo &FI) const;
3827
3828 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3829 CodeGenFunction &CGF) const;
3830};
3831
3832class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
3833public:
3834 HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
3835 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
3836
3837 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3838 return 29;
3839 }
3840};
3841
3842}
3843
3844void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
3845 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3846 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3847 it != ie; ++it)
3848 it->info = classifyArgumentType(it->type);
3849}
3850
3851ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
3852 if (!isAggregateTypeForABI(Ty)) {
3853 // Treat an enum type as its underlying type.
3854 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3855 Ty = EnumTy->getDecl()->getIntegerType();
3856
3857 return (Ty->isPromotableIntegerType() ?
3858 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3859 }
3860
3861 // Ignore empty records.
3862 if (isEmptyRecord(getContext(), Ty, true))
3863 return ABIArgInfo::getIgnore();
3864
3865 // Structures with either a non-trivial destructor or a non-trivial
3866 // copy constructor are always indirect.
3867 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3868 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3869
3870 uint64_t Size = getContext().getTypeSize(Ty);
3871 if (Size > 64)
3872 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3873 // Pass in the smallest viable integer type.
3874 else if (Size > 32)
3875 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3876 else if (Size > 16)
3877 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3878 else if (Size > 8)
3879 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3880 else
3881 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3882}
3883
3884ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
3885 if (RetTy->isVoidType())
3886 return ABIArgInfo::getIgnore();
3887
3888 // Large vector types should be returned via memory.
3889 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
3890 return ABIArgInfo::getIndirect(0);
3891
3892 if (!isAggregateTypeForABI(RetTy)) {
3893 // Treat an enum type as its underlying type.
3894 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3895 RetTy = EnumTy->getDecl()->getIntegerType();
3896
3897 return (RetTy->isPromotableIntegerType() ?
3898 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3899 }
3900
3901 // Structures with either a non-trivial destructor or a non-trivial
3902 // copy constructor are always indirect.
3903 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3904 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3905
3906 if (isEmptyRecord(getContext(), RetTy, true))
3907 return ABIArgInfo::getIgnore();
3908
3909 // Aggregates <= 8 bytes are returned in r0; other aggregates
3910 // are returned indirectly.
3911 uint64_t Size = getContext().getTypeSize(RetTy);
3912 if (Size <= 64) {
3913 // Return in the smallest viable integer type.
3914 if (Size <= 8)
3915 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3916 if (Size <= 16)
3917 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3918 if (Size <= 32)
3919 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3920 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
3921 }
3922
3923 return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
3924}
3925
3926llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner8b418682012-02-07 00:39:47 +00003927 CodeGenFunction &CGF) const {
Tony Linthicum96319392011-12-12 21:14:55 +00003928 // FIXME: Need to handle alignment
Chris Lattner8b418682012-02-07 00:39:47 +00003929 llvm::Type *BPP = CGF.Int8PtrPtrTy;
Tony Linthicum96319392011-12-12 21:14:55 +00003930
3931 CGBuilderTy &Builder = CGF.Builder;
3932 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
3933 "ap");
3934 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3935 llvm::Type *PTy =
3936 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3937 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3938
3939 uint64_t Offset =
3940 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3941 llvm::Value *NextAddr =
3942 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3943 "ap.next");
3944 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3945
3946 return AddrTyped;
3947}
3948
3949
Chris Lattnerea044322010-07-29 02:01:43 +00003950const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003951 if (TheTargetCodeGenInfo)
3952 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00003953
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003954 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00003955 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003956 default:
Chris Lattnerea044322010-07-29 02:01:43 +00003957 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00003958
Derek Schuff9ed63f82012-09-06 17:37:28 +00003959 case llvm::Triple::le32:
3960 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00003961 case llvm::Triple::mips:
3962 case llvm::Triple::mipsel:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003963 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCallaeeb7012010-05-27 06:19:26 +00003964
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003965 case llvm::Triple::mips64:
3966 case llvm::Triple::mips64el:
Akira Hatanakac0e3b662011-11-02 23:14:57 +00003967 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanaka8c6dfbe2011-09-20 18:30:57 +00003968
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003969 case llvm::Triple::arm:
3970 case llvm::Triple::thumb:
Sandeep Patel34c1af82011-04-05 00:23:47 +00003971 {
3972 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00003973
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003974 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel34c1af82011-04-05 00:23:47 +00003975 Kind = ARMABIInfo::APCS;
3976 else if (CodeGenOpts.FloatABI == "hard")
3977 Kind = ARMABIInfo::AAPCS_VFP;
3978
3979 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3980 }
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003981
John McCallec853ba2010-03-11 00:10:12 +00003982 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00003983 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
Roman Divacky0fbc4b92012-05-09 18:22:46 +00003984 case llvm::Triple::ppc64:
Bill Schmidt2fc107f2012-10-03 19:18:57 +00003985 if (Triple.isOSBinFormatELF())
3986 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
3987 else
3988 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00003989
Peter Collingbourneedb66f32012-05-20 23:28:41 +00003990 case llvm::Triple::nvptx:
3991 case llvm::Triple::nvptx64:
Justin Holewinski2c585b92012-05-24 17:43:12 +00003992 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
Justin Holewinski0259c3a2011-04-22 11:10:38 +00003993
Wesley Peck276fdf42010-12-19 19:57:51 +00003994 case llvm::Triple::mblaze:
3995 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3996
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003997 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00003998 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00003999
Peter Collingbourne2f7aa992011-10-13 16:24:41 +00004000 case llvm::Triple::tce:
4001 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
4002
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004003 case llvm::Triple::x86: {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004004 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004005
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004006 if (Triple.isOSDarwin())
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004007 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004008 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
4009 CodeGenOpts.NumRegisterParameters));
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00004010
4011 switch (Triple.getOS()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004012 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004013 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00004014 case llvm::Triple::AuroraUX:
4015 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00004016 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004017 case llvm::Triple::OpenBSD:
Eli Friedman42f74f22012-08-08 23:57:20 +00004018 case llvm::Triple::Bitrig:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004019 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004020 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
4021 false,
4022 CodeGenOpts.NumRegisterParameters));
Eli Friedman55fc7e22012-01-25 22:46:34 +00004023
4024 case llvm::Triple::Win32:
4025 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004026 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
4027 CodeGenOpts.NumRegisterParameters));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004028
4029 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00004030 return *(TheTargetCodeGenInfo =
Rafael Espindolab48280b2012-07-31 02:44:24 +00004031 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
4032 false,
4033 CodeGenOpts.NumRegisterParameters));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004034 }
Eli Friedmanc3e0fb42011-07-08 23:31:17 +00004035 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004036
Eli Friedmanee1ad992011-12-02 00:11:43 +00004037 case llvm::Triple::x86_64: {
4038 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
4039
Chris Lattnerf13721d2010-08-31 16:44:54 +00004040 switch (Triple.getOS()) {
4041 case llvm::Triple::Win32:
NAKAMURA Takumi0aa20572011-02-17 08:51:38 +00004042 case llvm::Triple::MinGW32:
Chris Lattnerf13721d2010-08-31 16:44:54 +00004043 case llvm::Triple::Cygwin:
4044 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
4045 default:
Eli Friedmanee1ad992011-12-02 00:11:43 +00004046 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
4047 HasAVX));
Chris Lattnerf13721d2010-08-31 16:44:54 +00004048 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00004049 }
Tony Linthicum96319392011-12-12 21:14:55 +00004050 case llvm::Triple::hexagon:
4051 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
Eli Friedmanee1ad992011-12-02 00:11:43 +00004052 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00004053}