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