blob: 16d22dd53cb04e389dd2e45bbe82c4b366f7993b [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
991 // that when AVX types are involved.
992 if (FI.getCallingConvention() == llvm::CallingConv::C) {
993 bool HasAVXType = false;
994 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
995 ie = FI.arg_end();
996 it != ie; ++it) {
997 if (it->info.isDirect()) {
998 llvm::Type *Ty = it->info.getCoerceToType();
999 if (llvm::VectorType *VTy = dyn_cast_or_null<llvm::VectorType>(Ty)) {
1000 if (VTy->getBitWidth() > 128) {
1001 HasAVXType = true;
1002 break;
1003 }
1004 }
1005 }
1006 }
1007 if (!HasAVXType)
1008 return true;
1009 }
John McCallcbc038a2011-09-21 08:08:30 +00001010
Eli Friedmanf37bd2f2011-12-01 04:53:19 +00001011 return TargetCodeGenInfo::isNoProtoCallVariadic(FI);
John McCallcbc038a2011-09-21 08:08:30 +00001012 }
1013
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001014};
1015
Chris Lattner04dc9572010-08-31 16:44:54 +00001016class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1017public:
1018 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1019 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1020
1021 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1022 return 7;
1023 }
1024
1025 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1026 llvm::Value *Address) const {
1027 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1028 llvm::LLVMContext &Context = CGF.getLLVMContext();
1029
Chris Lattner2192fe52011-07-18 04:24:23 +00001030 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Chris Lattner04dc9572010-08-31 16:44:54 +00001031 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001032
Chris Lattner04dc9572010-08-31 16:44:54 +00001033 // 0-15 are the 16 integer registers.
1034 // 16 is %rip.
1035 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
1036
1037 return false;
1038 }
1039};
1040
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001041}
1042
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001043void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1044 Class &Hi) const {
1045 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1046 //
1047 // (a) If one of the classes is Memory, the whole argument is passed in
1048 // memory.
1049 //
1050 // (b) If X87UP is not preceded by X87, the whole argument is passed in
1051 // memory.
1052 //
1053 // (c) If the size of the aggregate exceeds two eightbytes and the first
1054 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1055 // argument is passed in memory. NOTE: This is necessary to keep the
1056 // ABI working for processors that don't support the __m256 type.
1057 //
1058 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1059 //
1060 // Some of these are enforced by the merging logic. Others can arise
1061 // only with unions; for example:
1062 // union { _Complex double; unsigned; }
1063 //
1064 // Note that clauses (b) and (c) were added in 0.98.
1065 //
1066 if (Hi == Memory)
1067 Lo = Memory;
1068 if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1069 Lo = Memory;
1070 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1071 Lo = Memory;
1072 if (Hi == SSEUp && Lo != SSE)
1073 Hi = SSE;
1074}
1075
Chris Lattnerd776fb12010-06-28 21:43:59 +00001076X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001077 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1078 // classified recursively so that always two fields are
1079 // considered. The resulting class is calculated according to
1080 // the classes of the fields in the eightbyte:
1081 //
1082 // (a) If both classes are equal, this is the resulting class.
1083 //
1084 // (b) If one of the classes is NO_CLASS, the resulting class is
1085 // the other class.
1086 //
1087 // (c) If one of the classes is MEMORY, the result is the MEMORY
1088 // class.
1089 //
1090 // (d) If one of the classes is INTEGER, the result is the
1091 // INTEGER.
1092 //
1093 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1094 // MEMORY is used as class.
1095 //
1096 // (f) Otherwise class SSE is used.
1097
1098 // Accum should never be memory (we should have returned) or
1099 // ComplexX87 (because this cannot be passed in a structure).
1100 assert((Accum != Memory && Accum != ComplexX87) &&
1101 "Invalid accumulated classification during merge.");
1102 if (Accum == Field || Field == NoClass)
1103 return Accum;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001104 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001105 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001106 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001107 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001108 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001109 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001110 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1111 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001112 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001113 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001114}
1115
Chris Lattner5c740f12010-06-30 19:14:05 +00001116void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001117 Class &Lo, Class &Hi) const {
1118 // FIXME: This code can be simplified by introducing a simple value class for
1119 // Class pairs with appropriate constructor methods for the various
1120 // situations.
1121
1122 // FIXME: Some of the split computations are wrong; unaligned vectors
1123 // shouldn't be passed in registers for example, so there is no chance they
1124 // can straddle an eightbyte. Verify & simplify.
1125
1126 Lo = Hi = NoClass;
1127
1128 Class &Current = OffsetBase < 64 ? Lo : Hi;
1129 Current = Memory;
1130
John McCall9dd450b2009-09-21 23:43:11 +00001131 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001132 BuiltinType::Kind k = BT->getKind();
1133
1134 if (k == BuiltinType::Void) {
1135 Current = NoClass;
1136 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1137 Lo = Integer;
1138 Hi = Integer;
1139 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1140 Current = Integer;
1141 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1142 Current = SSE;
1143 } else if (k == BuiltinType::LongDouble) {
1144 Lo = X87;
1145 Hi = X87Up;
1146 }
1147 // FIXME: _Decimal32 and _Decimal64 are SSE.
1148 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattnerd776fb12010-06-28 21:43:59 +00001149 return;
1150 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001151
Chris Lattnerd776fb12010-06-28 21:43:59 +00001152 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001153 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +00001154 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +00001155 return;
1156 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001157
Chris Lattnerd776fb12010-06-28 21:43:59 +00001158 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001159 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001160 return;
1161 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001162
Chris Lattnerd776fb12010-06-28 21:43:59 +00001163 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +00001164 if (Ty->isMemberFunctionPointerType())
1165 Lo = Hi = Integer;
1166 else
1167 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +00001168 return;
1169 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001170
Chris Lattnerd776fb12010-06-28 21:43:59 +00001171 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001172 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001173 if (Size == 32) {
1174 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1175 // float> as integer.
1176 Current = Integer;
1177
1178 // If this type crosses an eightbyte boundary, it should be
1179 // split.
1180 uint64_t EB_Real = (OffsetBase) / 64;
1181 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1182 if (EB_Real != EB_Imag)
1183 Hi = Lo;
1184 } else if (Size == 64) {
1185 // gcc passes <1 x double> in memory. :(
1186 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1187 return;
1188
1189 // gcc passes <1 x long long> as INTEGER.
Chris Lattner46830f22010-08-26 18:03:20 +00001190 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001191 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1192 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1193 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001194 Current = Integer;
1195 else
1196 Current = SSE;
1197
1198 // If this type crosses an eightbyte boundary, it should be
1199 // split.
1200 if (OffsetBase && OffsetBase != 64)
1201 Hi = Lo;
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001202 } else if (Size == 128 || (HasAVX && Size == 256)) {
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001203 // Arguments of 256-bits are split into four eightbyte chunks. The
1204 // least significant one belongs to class SSE and all the others to class
1205 // SSEUP. The original Lo and Hi design considers that types can't be
1206 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1207 // This design isn't correct for 256-bits, but since there're no cases
1208 // where the upper parts would need to be inspected, avoid adding
1209 // complexity and just consider Hi to match the 64-256 part.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001210 Lo = SSE;
1211 Hi = SSEUp;
1212 }
Chris Lattnerd776fb12010-06-28 21:43:59 +00001213 return;
1214 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001215
Chris Lattnerd776fb12010-06-28 21:43:59 +00001216 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001217 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001218
Chris Lattner2b037972010-07-29 02:01:43 +00001219 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001220 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001221 if (Size <= 64)
1222 Current = Integer;
1223 else if (Size <= 128)
1224 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001225 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001226 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001227 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001228 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001229 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001230 Current = ComplexX87;
1231
1232 // If this complex type crosses an eightbyte boundary then it
1233 // should be split.
1234 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattner2b037972010-07-29 02:01:43 +00001235 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001236 if (Hi == NoClass && EB_Real != EB_Imag)
1237 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001238
Chris Lattnerd776fb12010-06-28 21:43:59 +00001239 return;
1240 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001241
Chris Lattner2b037972010-07-29 02:01:43 +00001242 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001243 // Arrays are treated like structures.
1244
Chris Lattner2b037972010-07-29 02:01:43 +00001245 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001246
1247 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001248 // than four eightbytes, ..., it has class MEMORY.
1249 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001250 return;
1251
1252 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1253 // fields, it has class MEMORY.
1254 //
1255 // Only need to check alignment of array base.
Chris Lattner2b037972010-07-29 02:01:43 +00001256 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001257 return;
1258
1259 // Otherwise implement simplified merge. We could be smarter about
1260 // this, but it isn't worth it and would be harder to verify.
1261 Current = NoClass;
Chris Lattner2b037972010-07-29 02:01:43 +00001262 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001263 uint64_t ArraySize = AT->getSize().getZExtValue();
Bruno Cardoso Lopes75541d02011-07-12 01:27:38 +00001264
1265 // The only case a 256-bit wide vector could be used is when the array
1266 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1267 // to work for sizes wider than 128, early check and fallback to memory.
1268 if (Size > 128 && EltSize != 256)
1269 return;
1270
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001271 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1272 Class FieldLo, FieldHi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001273 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001274 Lo = merge(Lo, FieldLo);
1275 Hi = merge(Hi, FieldHi);
1276 if (Lo == Memory || Hi == Memory)
1277 break;
1278 }
1279
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001280 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001281 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattnerd776fb12010-06-28 21:43:59 +00001282 return;
1283 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001284
Chris Lattnerd776fb12010-06-28 21:43:59 +00001285 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001286 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001287
1288 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001289 // than four eightbytes, ..., it has class MEMORY.
1290 if (Size > 256)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001291 return;
1292
Anders Carlsson20759ad2009-09-16 15:53:40 +00001293 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1294 // copy constructor or a non-trivial destructor, it is passed by invisible
1295 // reference.
1296 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1297 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001298
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001299 const RecordDecl *RD = RT->getDecl();
1300
1301 // Assume variable sized types are passed in memory.
1302 if (RD->hasFlexibleArrayMember())
1303 return;
1304
Chris Lattner2b037972010-07-29 02:01:43 +00001305 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001306
1307 // Reset Lo class, this will be recomputed.
1308 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001309
1310 // If this is a C++ record, classify the bases first.
1311 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1312 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1313 e = CXXRD->bases_end(); i != e; ++i) {
1314 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1315 "Unexpected base class!");
1316 const CXXRecordDecl *Base =
1317 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1318
1319 // Classify this field.
1320 //
1321 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1322 // single eightbyte, each is classified separately. Each eightbyte gets
1323 // initialized to class NO_CLASS.
1324 Class FieldLo, FieldHi;
Anders Carlssonfd88a612010-10-31 23:22:37 +00001325 uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
Chris Lattner22a931e2010-06-29 06:01:59 +00001326 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001327 Lo = merge(Lo, FieldLo);
1328 Hi = merge(Hi, FieldHi);
1329 if (Lo == Memory || Hi == Memory)
1330 break;
1331 }
1332 }
1333
1334 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001335 unsigned idx = 0;
Bruno Cardoso Lopes0aadf832011-07-12 22:30:58 +00001336 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001337 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001338 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1339 bool BitField = i->isBitField();
1340
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001341 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1342 // four eightbytes, or it contains unaligned fields, it has class MEMORY.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001343 //
Bruno Cardoso Lopes98154a72011-07-13 21:58:55 +00001344 // The only case a 256-bit wide vector could be used is when the struct
1345 // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1346 // to work for sizes wider than 128, early check and fallback to memory.
1347 //
1348 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1349 Lo = Memory;
1350 return;
1351 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001352 // Note, skip this test for bit-fields, see below.
Chris Lattner2b037972010-07-29 02:01:43 +00001353 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001354 Lo = Memory;
1355 return;
1356 }
1357
1358 // Classify this field.
1359 //
1360 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1361 // exceeds a single eightbyte, each is classified
1362 // separately. Each eightbyte gets initialized to class
1363 // NO_CLASS.
1364 Class FieldLo, FieldHi;
1365
1366 // Bit-fields require special handling, they do not force the
1367 // structure to be passed in memory even if unaligned, and
1368 // therefore they can straddle an eightbyte.
1369 if (BitField) {
1370 // Ignore padding bit-fields.
1371 if (i->isUnnamedBitfield())
1372 continue;
1373
1374 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Richard Smithcaf33902011-10-10 18:28:20 +00001375 uint64_t Size = i->getBitWidthValue(getContext());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001376
1377 uint64_t EB_Lo = Offset / 64;
1378 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1379 FieldLo = FieldHi = NoClass;
1380 if (EB_Lo) {
1381 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1382 FieldLo = NoClass;
1383 FieldHi = Integer;
1384 } else {
1385 FieldLo = Integer;
1386 FieldHi = EB_Hi ? Integer : NoClass;
1387 }
1388 } else
Chris Lattner22a931e2010-06-29 06:01:59 +00001389 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001390 Lo = merge(Lo, FieldLo);
1391 Hi = merge(Hi, FieldHi);
1392 if (Lo == Memory || Hi == Memory)
1393 break;
1394 }
1395
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001396 postMerge(Size, Lo, Hi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001397 }
1398}
1399
Chris Lattner22a931e2010-06-29 06:01:59 +00001400ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001401 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1402 // place naturally.
John McCalla1dee5302010-08-22 10:59:02 +00001403 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-04-21 19:49:55 +00001404 // Treat an enum type as its underlying type.
1405 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1406 Ty = EnumTy->getDecl()->getIntegerType();
1407
1408 return (Ty->isPromotableIntegerType() ?
1409 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1410 }
1411
1412 return ABIArgInfo::getIndirect(0);
1413}
1414
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001415bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1416 if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1417 uint64_t Size = getContext().getTypeSize(VecTy);
1418 unsigned LargestVector = HasAVX ? 256 : 128;
1419 if (Size <= 64 || Size > LargestVector)
1420 return true;
1421 }
1422
1423 return false;
1424}
1425
Chris Lattner22a931e2010-06-29 06:01:59 +00001426ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001427 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1428 // place naturally.
Eli Friedmanbfd5add2011-12-02 00:11:43 +00001429 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00001430 // Treat an enum type as its underlying type.
1431 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1432 Ty = EnumTy->getDecl()->getIntegerType();
1433
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001434 return (Ty->isPromotableIntegerType() ?
1435 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001436 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001437
Daniel Dunbar53fac692010-04-21 19:49:55 +00001438 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1439 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001440
Chris Lattner44c2b902011-05-22 23:21:23 +00001441 // Compute the byval alignment. We specify the alignment of the byval in all
1442 // cases so that the mid-level optimizer knows the alignment of the byval.
1443 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1444 return ABIArgInfo::getIndirect(Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001445}
1446
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001447/// GetByteVectorType - The ABI specifies that a value should be passed in an
1448/// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
Chris Lattner4200fe42010-07-29 04:56:46 +00001449/// vector register.
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001450llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001451 llvm::Type *IRType = CGT.ConvertType(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001452
Chris Lattner9fa15c32010-07-29 05:02:29 +00001453 // Wrapper structs that just contain vectors are passed just like vectors,
1454 // strip them off if present.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001455 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
Chris Lattner9fa15c32010-07-29 05:02:29 +00001456 while (STy && STy->getNumElements() == 1) {
1457 IRType = STy->getElementType(0);
1458 STy = dyn_cast<llvm::StructType>(IRType);
1459 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001460
Bruno Cardoso Lopes129b4cc2011-07-08 22:57:35 +00001461 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001462 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1463 llvm::Type *EltTy = VT->getElementType();
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001464 unsigned BitWidth = VT->getBitWidth();
Tanya Lattner71f1b2d2011-11-28 23:18:11 +00001465 if ((BitWidth >= 128 && BitWidth <= 256) &&
Chris Lattner4200fe42010-07-29 04:56:46 +00001466 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1467 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1468 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1469 EltTy->isIntegerTy(128)))
1470 return VT;
1471 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001472
Chris Lattner4200fe42010-07-29 04:56:46 +00001473 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1474}
1475
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001476/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1477/// is known to either be off the end of the specified type or being in
1478/// alignment padding. The user type specified is known to be at most 128 bits
1479/// in size, and have passed through X86_64ABIInfo::classify with a successful
1480/// classification that put one of the two halves in the INTEGER class.
1481///
1482/// It is conservatively correct to return false.
1483static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1484 unsigned EndBit, ASTContext &Context) {
1485 // If the bytes being queried are off the end of the type, there is no user
1486 // data hiding here. This handles analysis of builtins, vectors and other
1487 // types that don't contain interesting padding.
1488 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1489 if (TySize <= StartBit)
1490 return true;
1491
Chris Lattner98076a22010-07-29 07:43:55 +00001492 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1493 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1494 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1495
1496 // Check each element to see if the element overlaps with the queried range.
1497 for (unsigned i = 0; i != NumElts; ++i) {
1498 // If the element is after the span we care about, then we're done..
1499 unsigned EltOffset = i*EltSize;
1500 if (EltOffset >= EndBit) break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001501
Chris Lattner98076a22010-07-29 07:43:55 +00001502 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1503 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1504 EndBit-EltOffset, Context))
1505 return false;
1506 }
1507 // If it overlaps no elements, then it is safe to process as padding.
1508 return true;
1509 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001510
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001511 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1512 const RecordDecl *RD = RT->getDecl();
1513 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001514
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001515 // If this is a C++ record, check the bases first.
1516 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1517 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1518 e = CXXRD->bases_end(); i != e; ++i) {
1519 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1520 "Unexpected base class!");
1521 const CXXRecordDecl *Base =
1522 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001523
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001524 // If the base is after the span we care about, ignore it.
Anders Carlssonfd88a612010-10-31 23:22:37 +00001525 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001526 if (BaseOffset >= EndBit) continue;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001527
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001528 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1529 if (!BitsContainNoUserData(i->getType(), BaseStart,
1530 EndBit-BaseOffset, Context))
1531 return false;
1532 }
1533 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001534
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001535 // Verify that no field has data that overlaps the region of interest. Yes
1536 // this could be sped up a lot by being smarter about queried fields,
1537 // however we're only looking at structs up to 16 bytes, so we don't care
1538 // much.
1539 unsigned idx = 0;
1540 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1541 i != e; ++i, ++idx) {
1542 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001543
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001544 // If we found a field after the region we care about, then we're done.
1545 if (FieldOffset >= EndBit) break;
1546
1547 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1548 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1549 Context))
1550 return false;
1551 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001552
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001553 // If nothing in this record overlapped the area of interest, then we're
1554 // clean.
1555 return true;
1556 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001557
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001558 return false;
1559}
1560
Chris Lattnere556a712010-07-29 18:39:32 +00001561/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1562/// float member at the specified offset. For example, {int,{float}} has a
1563/// float at offset 4. It is conservatively correct for this routine to return
1564/// false.
Chris Lattner2192fe52011-07-18 04:24:23 +00001565static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattnere556a712010-07-29 18:39:32 +00001566 const llvm::TargetData &TD) {
1567 // Base case if we find a float.
1568 if (IROffset == 0 && IRType->isFloatTy())
1569 return true;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001570
Chris Lattnere556a712010-07-29 18:39:32 +00001571 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001572 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnere556a712010-07-29 18:39:32 +00001573 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1574 unsigned Elt = SL->getElementContainingOffset(IROffset);
1575 IROffset -= SL->getElementOffset(Elt);
1576 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1577 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001578
Chris Lattnere556a712010-07-29 18:39:32 +00001579 // If this is an array, recurse into the field at the specified offset.
Chris Lattner2192fe52011-07-18 04:24:23 +00001580 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1581 llvm::Type *EltTy = ATy->getElementType();
Chris Lattnere556a712010-07-29 18:39:32 +00001582 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1583 IROffset -= IROffset/EltSize*EltSize;
1584 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1585 }
1586
1587 return false;
1588}
1589
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001590
1591/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1592/// low 8 bytes of an XMM register, corresponding to the SSE class.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001593llvm::Type *X86_64ABIInfo::
1594GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001595 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattner50a357e2010-07-29 18:19:50 +00001596 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001597 // pass as float if the last 4 bytes is just padding. This happens for
1598 // structs that contain 3 floats.
1599 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1600 SourceOffset*8+64, getContext()))
1601 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001602
Chris Lattnere556a712010-07-29 18:39:32 +00001603 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1604 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1605 // case.
1606 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner9f8b4512010-08-25 23:39:14 +00001607 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1608 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001609
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001610 return llvm::Type::getDoubleTy(getVMContext());
1611}
1612
1613
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001614/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1615/// an 8-byte GPR. This means that we either have a scalar or we are talking
1616/// about the high or low part of an up-to-16-byte struct. This routine picks
1617/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001618/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1619/// etc).
1620///
1621/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1622/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1623/// the 8-byte value references. PrefType may be null.
1624///
1625/// SourceTy is the source level type for the entire argument. SourceOffset is
1626/// an offset into this that we're processing (which is always either 0 or 8).
1627///
Chris Lattnera5f58b02011-07-09 17:41:47 +00001628llvm::Type *X86_64ABIInfo::
1629GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001630 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001631 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1632 // returning an 8-byte unit starting with it. See if we can safely use it.
1633 if (IROffset == 0) {
1634 // Pointers and int64's always fill the 8-byte unit.
1635 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1636 return IRType;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001637
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001638 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1639 // goodness in the source type is just tail padding. This is allowed to
1640 // kick in for struct {double,int} on the int, but not on
1641 // struct{double,int,int} because we wouldn't return the second int. We
1642 // have to do this analysis on the source type because we can't depend on
1643 // unions being lowered a specific way etc.
1644 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1645 IRType->isIntegerTy(32)) {
1646 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001647
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001648 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1649 SourceOffset*8+64, getContext()))
1650 return IRType;
1651 }
1652 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001653
Chris Lattner2192fe52011-07-18 04:24:23 +00001654 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001655 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001656 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001657 if (IROffset < SL->getSizeInBytes()) {
1658 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1659 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001660
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001661 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1662 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001663 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001664 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001665
Chris Lattner2192fe52011-07-18 04:24:23 +00001666 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001667 llvm::Type *EltTy = ATy->getElementType();
Chris Lattner98076a22010-07-29 07:43:55 +00001668 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1669 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001670 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1671 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001672 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001673
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001674 // Okay, we don't have any better idea of what to pass, so we pass this in an
1675 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner3f763422010-07-29 17:34:39 +00001676 unsigned TySizeInBytes =
1677 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001678
Chris Lattner3f763422010-07-29 17:34:39 +00001679 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001680
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001681 // It is always safe to classify this as an integer type up to i64 that
1682 // isn't larger than the structure.
Chris Lattner3f763422010-07-29 17:34:39 +00001683 return llvm::IntegerType::get(getVMContext(),
1684 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001685}
1686
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001687
1688/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1689/// be used as elements of a two register pair to pass or return, return a
1690/// first class aggregate to represent them. For example, if the low part of
1691/// a by-value argument should be passed as i32* and the high part as float,
1692/// return {i32*, float}.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001693static llvm::Type *
Jay Foad7c57be32011-07-11 09:56:20 +00001694GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001695 const llvm::TargetData &TD) {
1696 // In order to correctly satisfy the ABI, we need to the high part to start
1697 // at offset 8. If the high and low parts we inferred are both 4-byte types
1698 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1699 // the second element at offset 8. Check for this:
1700 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1701 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1702 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1703 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001704
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001705 // To handle this, we have to increase the size of the low part so that the
1706 // second element will start at an 8 byte offset. We can't increase the size
1707 // of the second element because it might make us access off the end of the
1708 // struct.
1709 if (HiStart != 8) {
1710 // There are only two sorts of types the ABI generation code can produce for
1711 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1712 // Promote these to a larger type.
1713 if (Lo->isFloatTy())
1714 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1715 else {
1716 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1717 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1718 }
1719 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001720
Chris Lattnera5f58b02011-07-09 17:41:47 +00001721 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001722
1723
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001724 // Verify that the second element is at an 8-byte offset.
1725 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1726 "Invalid x86-64 argument pair!");
1727 return Result;
1728}
1729
Chris Lattner31faff52010-07-28 23:06:14 +00001730ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001731classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-07-28 23:06:14 +00001732 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1733 // classification algorithm.
1734 X86_64ABIInfo::Class Lo, Hi;
1735 classify(RetTy, 0, Lo, Hi);
1736
1737 // Check some invariants.
1738 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner31faff52010-07-28 23:06:14 +00001739 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1740
Chris Lattnera5f58b02011-07-09 17:41:47 +00001741 llvm::Type *ResType = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001742 switch (Lo) {
1743 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001744 if (Hi == NoClass)
1745 return ABIArgInfo::getIgnore();
1746 // If the low part is just padding, it takes no register, leave ResType
1747 // null.
1748 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1749 "Unknown missing lo part");
1750 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001751
1752 case SSEUp:
1753 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001754 llvm_unreachable("Invalid classification for lo word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001755
1756 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1757 // hidden argument.
1758 case Memory:
1759 return getIndirectReturnResult(RetTy);
1760
1761 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1762 // available register of the sequence %rax, %rdx is used.
1763 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001764 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001765
Chris Lattner1f3a0632010-07-29 21:42:50 +00001766 // If we have a sign or zero extended integer, make sure to return Extend
1767 // so that the parameter gets the right LLVM IR attributes.
1768 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1769 // Treat an enum type as its underlying type.
1770 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1771 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001772
Chris Lattner1f3a0632010-07-29 21:42:50 +00001773 if (RetTy->isIntegralOrEnumerationType() &&
1774 RetTy->isPromotableIntegerType())
1775 return ABIArgInfo::getExtend();
1776 }
Chris Lattner31faff52010-07-28 23:06:14 +00001777 break;
1778
1779 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1780 // available SSE register of the sequence %xmm0, %xmm1 is used.
1781 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001782 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001783 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001784
1785 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1786 // returned on the X87 stack in %st0 as 80-bit x87 number.
1787 case X87:
Chris Lattner2b037972010-07-29 02:01:43 +00001788 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001789 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001790
1791 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1792 // part of the value is returned in %st0 and the imaginary part in
1793 // %st1.
1794 case ComplexX87:
1795 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner845511f2011-06-18 22:49:11 +00001796 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner2b037972010-07-29 02:01:43 +00001797 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001798 NULL);
1799 break;
1800 }
1801
Chris Lattnera5f58b02011-07-09 17:41:47 +00001802 llvm::Type *HighPart = 0;
Chris Lattner31faff52010-07-28 23:06:14 +00001803 switch (Hi) {
1804 // Memory was handled previously and X87 should
1805 // never occur as a hi class.
1806 case Memory:
1807 case X87:
David Blaikie83d382b2011-09-23 05:06:16 +00001808 llvm_unreachable("Invalid classification for hi word.");
Chris Lattner31faff52010-07-28 23:06:14 +00001809
1810 case ComplexX87: // Previously handled.
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001811 case NoClass:
1812 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001813
Chris Lattner52b3c132010-09-01 00:20:33 +00001814 case Integer:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001815 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001816 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1817 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001818 break;
Chris Lattner52b3c132010-09-01 00:20:33 +00001819 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001820 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001821 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1822 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner31faff52010-07-28 23:06:14 +00001823 break;
1824
1825 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001826 // is passed in the next available eightbyte chunk if the last used
1827 // vector register.
Chris Lattner31faff52010-07-28 23:06:14 +00001828 //
Chris Lattner57540c52011-04-15 05:22:18 +00001829 // SSEUP should always be preceded by SSE, just widen.
Chris Lattner31faff52010-07-28 23:06:14 +00001830 case SSEUp:
1831 assert(Lo == SSE && "Unexpected SSEUp classification.");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001832 ResType = GetByteVectorType(RetTy);
Chris Lattner31faff52010-07-28 23:06:14 +00001833 break;
1834
1835 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1836 // returned together with the previous X87 value in %st0.
1837 case X87Up:
Chris Lattner57540c52011-04-15 05:22:18 +00001838 // If X87Up is preceded by X87, we don't need to do
Chris Lattner31faff52010-07-28 23:06:14 +00001839 // anything. However, in some cases with unions it may not be
Chris Lattner57540c52011-04-15 05:22:18 +00001840 // preceded by X87. In such situations we follow gcc and pass the
Chris Lattner31faff52010-07-28 23:06:14 +00001841 // extra bits in an SSE reg.
Chris Lattnerc95a3982010-07-29 17:49:08 +00001842 if (Lo != X87) {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001843 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
Chris Lattner52b3c132010-09-01 00:20:33 +00001844 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1845 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattnerc95a3982010-07-29 17:49:08 +00001846 }
Chris Lattner31faff52010-07-28 23:06:14 +00001847 break;
1848 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001849
Chris Lattner52b3c132010-09-01 00:20:33 +00001850 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001851 // known to pass in the high eightbyte of the result. We do this by forming a
1852 // first class struct aggregate with the high and low part: {low, high}
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001853 if (HighPart)
1854 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001855
Chris Lattner1f3a0632010-07-29 21:42:50 +00001856 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001857}
1858
Chris Lattner458b2aa2010-07-29 02:16:43 +00001859ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Bill Wendling9987c0e2010-10-18 23:51:38 +00001860 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001861 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001862 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001863
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001864 // Check some invariants.
1865 // FIXME: Enforce these by construction.
1866 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001867 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1868
1869 neededInt = 0;
1870 neededSSE = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +00001871 llvm::Type *ResType = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001872 switch (Lo) {
1873 case NoClass:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001874 if (Hi == NoClass)
1875 return ABIArgInfo::getIgnore();
1876 // If the low part is just padding, it takes no register, leave ResType
1877 // null.
1878 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1879 "Unknown missing lo part");
1880 break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001881
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001882 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1883 // on the stack.
1884 case Memory:
1885
1886 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1887 // COMPLEX_X87, it is passed in memory.
1888 case X87:
1889 case ComplexX87:
Eli Friedman4774b7e2011-06-29 07:04:55 +00001890 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1891 ++neededInt;
Chris Lattner22a931e2010-06-29 06:01:59 +00001892 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001893
1894 case SSEUp:
1895 case X87Up:
David Blaikie83d382b2011-09-23 05:06:16 +00001896 llvm_unreachable("Invalid classification for lo word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001897
1898 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1899 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1900 // and %r9 is used.
1901 case Integer:
Chris Lattner22a931e2010-06-29 06:01:59 +00001902 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001903
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001904 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001905 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-07-29 21:42:50 +00001906
1907 // If we have a sign or zero extended integer, make sure to return Extend
1908 // so that the parameter gets the right LLVM IR attributes.
1909 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1910 // Treat an enum type as its underlying type.
1911 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1912 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001913
Chris Lattner1f3a0632010-07-29 21:42:50 +00001914 if (Ty->isIntegralOrEnumerationType() &&
1915 Ty->isPromotableIntegerType())
1916 return ABIArgInfo::getExtend();
1917 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001918
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001919 break;
1920
1921 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1922 // available SSE register is used, the registers are taken in the
1923 // order from %xmm0 to %xmm7.
Bill Wendling5cd41c42010-10-18 03:41:31 +00001924 case SSE: {
Chris Lattnera5f58b02011-07-09 17:41:47 +00001925 llvm::Type *IRType = CGT.ConvertType(Ty);
Eli Friedman1310c682011-07-02 00:57:27 +00001926 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
Bill Wendling9987c0e2010-10-18 23:51:38 +00001927 ++neededSSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001928 break;
1929 }
Bill Wendling5cd41c42010-10-18 03:41:31 +00001930 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001931
Chris Lattnera5f58b02011-07-09 17:41:47 +00001932 llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001933 switch (Hi) {
1934 // Memory was handled previously, ComplexX87 and X87 should
Chris Lattner57540c52011-04-15 05:22:18 +00001935 // never occur as hi classes, and X87Up must be preceded by X87,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001936 // which is passed in memory.
1937 case Memory:
1938 case X87:
1939 case ComplexX87:
David Blaikie83d382b2011-09-23 05:06:16 +00001940 llvm_unreachable("Invalid classification for hi word.");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001941
1942 case NoClass: break;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001943
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001944 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001945 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001946 // Pick an 8-byte type based on the preferred type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001947 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001948
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001949 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1950 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 break;
1952
1953 // X87Up generally doesn't occur here (long double is passed in
1954 // memory), except in situations involving unions.
1955 case X87Up:
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001956 case SSE:
Chris Lattnera5f58b02011-07-09 17:41:47 +00001957 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001958
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001959 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1960 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001961
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001962 ++neededSSE;
1963 break;
1964
1965 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1966 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001967 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001968 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001969 assert(Lo == SSE && "Unexpected SSEUp classification");
Bruno Cardoso Lopes21a41bb2011-07-11 22:41:29 +00001970 ResType = GetByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001971 break;
1972 }
1973
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001974 // If a high part was specified, merge it together with the low part. It is
1975 // known to pass in the high eightbyte of the result. We do this by forming a
1976 // first class struct aggregate with the high and low part: {low, high}
1977 if (HighPart)
Chris Lattnerd426c8e2010-09-01 00:50:20 +00001978 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001979
Chris Lattner1f3a0632010-07-29 21:42:50 +00001980 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001981}
1982
Chris Lattner22326a12010-07-29 02:31:05 +00001983void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001984
Chris Lattner458b2aa2010-07-29 02:16:43 +00001985 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001986
1987 // Keep track of the number of assigned registers.
Bill Wendling9987c0e2010-10-18 23:51:38 +00001988 unsigned freeIntRegs = 6, freeSSERegs = 8;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001989
1990 // If the return value is indirect, then the hidden argument is consuming one
1991 // integer register.
1992 if (FI.getReturnInfo().isIndirect())
1993 --freeIntRegs;
1994
1995 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1996 // get assigned (in left-to-right order) for passing as follows...
1997 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1998 it != ie; ++it) {
Bill Wendling9987c0e2010-10-18 23:51:38 +00001999 unsigned neededInt, neededSSE;
2000 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002001
2002 // AMD64-ABI 3.2.3p3: If there are no registers available for any
2003 // eightbyte of an argument, the whole argument is passed on the
2004 // stack. If registers have already been assigned for some
2005 // eightbytes of such an argument, the assignments get reverted.
Bill Wendling9987c0e2010-10-18 23:51:38 +00002006 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002007 freeIntRegs -= neededInt;
2008 freeSSERegs -= neededSSE;
2009 } else {
Chris Lattner22a931e2010-06-29 06:01:59 +00002010 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002011 }
2012 }
2013}
2014
2015static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2016 QualType Ty,
2017 CodeGenFunction &CGF) {
2018 llvm::Value *overflow_arg_area_p =
2019 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2020 llvm::Value *overflow_arg_area =
2021 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2022
2023 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2024 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Eli Friedmana1748562011-11-18 02:44:19 +00002025 // It isn't stated explicitly in the standard, but in practice we use
2026 // alignment greater than 16 where necessary.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002027 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2028 if (Align > 8) {
Eli Friedmana1748562011-11-18 02:44:19 +00002029 // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
Owen Anderson41a75022009-08-13 21:57:51 +00002030 llvm::Value *Offset =
Eli Friedmana1748562011-11-18 02:44:19 +00002031 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002032 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2033 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002034 CGF.Int64Ty);
Eli Friedmana1748562011-11-18 02:44:19 +00002035 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002036 overflow_arg_area =
2037 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2038 overflow_arg_area->getType(),
2039 "overflow_arg_area.align");
2040 }
2041
2042 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
Chris Lattner2192fe52011-07-18 04:24:23 +00002043 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002044 llvm::Value *Res =
2045 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002046 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002047
2048 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2049 // l->overflow_arg_area + sizeof(type).
2050 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2051 // an 8 byte boundary.
2052
2053 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00002054 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002055 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002056 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2057 "overflow_arg_area.next");
2058 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2059
2060 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2061 return Res;
2062}
2063
2064llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2065 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00002066 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00002067
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002068 // Assume that va_list type is correct; should be pointer to LLVM type:
2069 // struct {
2070 // i32 gp_offset;
2071 // i32 fp_offset;
2072 // i8* overflow_arg_area;
2073 // i8* reg_save_area;
2074 // };
Bill Wendling9987c0e2010-10-18 23:51:38 +00002075 unsigned neededInt, neededSSE;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002076
Chris Lattner9723d6c2010-03-11 18:19:55 +00002077 Ty = CGF.getContext().getCanonicalType(Ty);
Bill Wendling9987c0e2010-10-18 23:51:38 +00002078 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002079
2080 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2081 // in the registers. If not go to step 7.
2082 if (!neededInt && !neededSSE)
2083 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2084
2085 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2086 // general purpose registers needed to pass type and num_fp to hold
2087 // the number of floating point registers needed.
2088
2089 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2090 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2091 // l->fp_offset > 304 - num_fp * 16 go to step 7.
2092 //
2093 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2094 // register save space).
2095
2096 llvm::Value *InRegs = 0;
2097 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2098 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2099 if (neededInt) {
2100 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2101 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattnerd776fb12010-06-28 21:43:59 +00002102 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2103 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002104 }
2105
2106 if (neededSSE) {
2107 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2108 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2109 llvm::Value *FitsInFP =
Chris Lattnerd776fb12010-06-28 21:43:59 +00002110 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2111 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002112 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2113 }
2114
2115 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2116 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2117 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2118 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2119
2120 // Emit code to load the value if it was passed in registers.
2121
2122 CGF.EmitBlock(InRegBlock);
2123
2124 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2125 // an offset of l->gp_offset and/or l->fp_offset. This may require
2126 // copying to a temporary location in case the parameter is passed
2127 // in different register classes or requires an alignment greater
2128 // than 8 for general purpose registers and 16 for XMM registers.
2129 //
2130 // FIXME: This really results in shameful code when we end up needing to
2131 // collect arguments from different places; often what should result in a
2132 // simple assembling of a structure from scattered addresses has many more
2133 // loads than necessary. Can we clean this up?
Chris Lattner2192fe52011-07-18 04:24:23 +00002134 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002135 llvm::Value *RegAddr =
2136 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2137 "reg_save_area");
2138 if (neededInt && neededSSE) {
2139 // FIXME: Cleanup.
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002140 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002141 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002142 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2143 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002144 llvm::Type *TyLo = ST->getElementType(0);
2145 llvm::Type *TyHi = ST->getElementType(1);
Chris Lattner51e1cc22010-08-26 06:28:35 +00002146 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002147 "Unexpected ABI info for mixed regs");
Chris Lattner2192fe52011-07-18 04:24:23 +00002148 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2149 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002150 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2151 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00002152 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2153 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002154 llvm::Value *V =
2155 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2156 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2157 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2158 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2159
Owen Anderson170229f2009-07-14 23:10:40 +00002160 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002161 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002162 } else if (neededInt) {
2163 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2164 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00002165 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-06-28 20:05:43 +00002166 } else if (neededSSE == 1) {
2167 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2168 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2169 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002170 } else {
Chris Lattner0cf24192010-06-28 20:05:43 +00002171 assert(neededSSE == 2 && "Invalid number of needed registers!");
2172 // SSE registers are spaced 16 bytes apart in the register save
2173 // area, we need to collect the two eightbytes together.
2174 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattnerd776fb12010-06-28 21:43:59 +00002175 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Jay Foad7c57be32011-07-11 09:56:20 +00002176 llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Chris Lattner2192fe52011-07-18 04:24:23 +00002177 llvm::Type *DblPtrTy =
Chris Lattner0cf24192010-06-28 20:05:43 +00002178 llvm::PointerType::getUnqual(DoubleTy);
Chris Lattner2192fe52011-07-18 04:24:23 +00002179 llvm::StructType *ST = llvm::StructType::get(DoubleTy,
Chris Lattner0cf24192010-06-28 20:05:43 +00002180 DoubleTy, NULL);
2181 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2182 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2183 DblPtrTy));
2184 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2185 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2186 DblPtrTy));
2187 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2188 RegAddr = CGF.Builder.CreateBitCast(Tmp,
2189 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002190 }
2191
2192 // AMD64-ABI 3.5.7p5: Step 5. Set:
2193 // l->gp_offset = l->gp_offset + num_gp * 8
2194 // l->fp_offset = l->fp_offset + num_fp * 16.
2195 if (neededInt) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002196 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002197 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2198 gp_offset_p);
2199 }
2200 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002201 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002202 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2203 fp_offset_p);
2204 }
2205 CGF.EmitBranch(ContBlock);
2206
2207 // Emit code to load the value if it was passed in memory.
2208
2209 CGF.EmitBlock(InMemBlock);
2210 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2211
2212 // Return the appropriate result.
2213
2214 CGF.EmitBlock(ContBlock);
Jay Foad20c0f022011-03-30 11:28:58 +00002215 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002216 "vaarg.addr");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002217 ResAddr->addIncoming(RegAddr, InRegBlock);
2218 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002219 return ResAddr;
2220}
2221
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002222ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2223
2224 if (Ty->isVoidType())
2225 return ABIArgInfo::getIgnore();
2226
2227 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2228 Ty = EnumTy->getDecl()->getIntegerType();
2229
2230 uint64_t Size = getContext().getTypeSize(Ty);
2231
2232 if (const RecordType *RT = Ty->getAs<RecordType>()) {
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002233 if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2234 RT->getDecl()->hasFlexibleArrayMember())
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002235 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2236
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002237 // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2238 if (Size == 128 &&
Douglas Gregore8bbc122011-09-02 00:18:52 +00002239 getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32)
NAKAMURA Takumif8a6e802011-02-22 03:56:57 +00002240 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2241 Size));
2242
2243 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2244 // not 1, 2, 4, or 8 bytes, must be passed by reference."
2245 if (Size <= 64 &&
NAKAMURA Takumie03c6032011-01-19 00:11:33 +00002246 (Size & (Size - 1)) == 0)
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002247 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2248 Size));
2249
2250 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2251 }
2252
2253 if (Ty->isPromotableIntegerType())
2254 return ABIArgInfo::getExtend();
2255
2256 return ABIArgInfo::getDirect();
2257}
2258
2259void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2260
2261 QualType RetTy = FI.getReturnType();
2262 FI.getReturnInfo() = classify(RetTy);
2263
NAKAMURA Takumibd91f502011-01-17 22:56:31 +00002264 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2265 it != ie; ++it)
2266 it->info = classify(it->type);
2267}
2268
Chris Lattner04dc9572010-08-31 16:44:54 +00002269llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2270 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002271 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2272 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattner0cf24192010-06-28 20:05:43 +00002273
Chris Lattner04dc9572010-08-31 16:44:54 +00002274 CGBuilderTy &Builder = CGF.Builder;
2275 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2276 "ap");
2277 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2278 llvm::Type *PTy =
2279 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2280 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2281
2282 uint64_t Offset =
2283 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2284 llvm::Value *NextAddr =
2285 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2286 "ap.next");
2287 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2288
2289 return AddrTyped;
2290}
Chris Lattner0cf24192010-06-28 20:05:43 +00002291
John McCallea8d8bb2010-03-11 00:10:12 +00002292// PowerPC-32
2293
2294namespace {
2295class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2296public:
Chris Lattner2b037972010-07-29 02:01:43 +00002297 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002298
John McCallea8d8bb2010-03-11 00:10:12 +00002299 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2300 // This is recovered from gcc output.
2301 return 1; // r1 is the dedicated stack pointer
2302 }
2303
2304 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002305 llvm::Value *Address) const;
John McCallea8d8bb2010-03-11 00:10:12 +00002306};
2307
2308}
2309
2310bool
2311PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2312 llvm::Value *Address) const {
2313 // This is calculated from the LLVM and GCC tables and verified
2314 // against gcc output. AFAIK all ABIs use the same encoding.
2315
2316 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2317 llvm::LLVMContext &Context = CGF.getLLVMContext();
2318
Chris Lattner2192fe52011-07-18 04:24:23 +00002319 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCallea8d8bb2010-03-11 00:10:12 +00002320 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2321 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2322 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2323
2324 // 0-31: r0-31, the 4-byte general-purpose registers
John McCall943fae92010-05-27 06:19:26 +00002325 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002326
2327 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002328 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-03-11 00:10:12 +00002329
2330 // 64-76 are various 4-byte special-purpose registers:
2331 // 64: mq
2332 // 65: lr
2333 // 66: ctr
2334 // 67: ap
2335 // 68-75 cr0-7
2336 // 76: xer
John McCall943fae92010-05-27 06:19:26 +00002337 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002338
2339 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002340 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002341
2342 // 109: vrsave
2343 // 110: vscr
2344 // 111: spe_acc
2345 // 112: spefscr
2346 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002347 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002348
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002349 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002350}
2351
2352
Chris Lattner0cf24192010-06-28 20:05:43 +00002353//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002354// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002355//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002356
2357namespace {
2358
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002359class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00002360public:
2361 enum ABIKind {
2362 APCS = 0,
2363 AAPCS = 1,
2364 AAPCS_VFP
2365 };
2366
2367private:
2368 ABIKind Kind;
2369
2370public:
Chris Lattner2b037972010-07-29 02:01:43 +00002371 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002372
John McCall3480ef22011-08-30 01:42:09 +00002373 bool isEABI() const {
Douglas Gregore8bbc122011-09-02 00:18:52 +00002374 StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName();
John McCall3480ef22011-08-30 01:42:09 +00002375 return (Env == "gnueabi" || Env == "eabi");
2376 }
2377
Daniel Dunbar020daa92009-09-12 01:00:39 +00002378private:
2379 ABIKind getABIKind() const { return Kind; }
2380
Chris Lattner458b2aa2010-07-29 02:16:43 +00002381 ABIArgInfo classifyReturnType(QualType RetTy) const;
2382 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002383
Chris Lattner22326a12010-07-29 02:31:05 +00002384 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002385
2386 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2387 CodeGenFunction &CGF) const;
2388};
2389
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002390class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2391public:
Chris Lattner2b037972010-07-29 02:01:43 +00002392 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2393 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002394
John McCall3480ef22011-08-30 01:42:09 +00002395 const ARMABIInfo &getABIInfo() const {
2396 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
2397 }
2398
John McCallbeec5a02010-03-06 00:35:14 +00002399 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2400 return 13;
2401 }
Roman Divackyc1617352011-05-18 19:36:54 +00002402
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002403 StringRef getARCRetainAutoreleasedReturnValueMarker() const {
John McCall31168b02011-06-15 23:02:42 +00002404 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2405 }
2406
Roman Divackyc1617352011-05-18 19:36:54 +00002407 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2408 llvm::Value *Address) const {
2409 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2410 llvm::LLVMContext &Context = CGF.getLLVMContext();
2411
Chris Lattner2192fe52011-07-18 04:24:23 +00002412 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
Roman Divackyc1617352011-05-18 19:36:54 +00002413 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2414
2415 // 0-15 are the 16 integer registers.
2416 AssignToArrayRange(Builder, Address, Four8, 0, 15);
2417
2418 return false;
2419 }
John McCall3480ef22011-08-30 01:42:09 +00002420
2421 unsigned getSizeOfUnwindException() const {
2422 if (getABIInfo().isEABI()) return 88;
2423 return TargetCodeGenInfo::getSizeOfUnwindException();
2424 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002425};
2426
Daniel Dunbard59655c2009-09-12 00:59:49 +00002427}
2428
Chris Lattner22326a12010-07-29 02:31:05 +00002429void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002430 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002431 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002432 it != ie; ++it)
2433 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002434
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002435 // Always honor user-specified calling convention.
2436 if (FI.getCallingConvention() != llvm::CallingConv::C)
2437 return;
2438
2439 // Calling convention as default by an ABI.
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002440 llvm::CallingConv::ID DefaultCC;
John McCall3480ef22011-08-30 01:42:09 +00002441 if (isEABI())
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002442 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002443 else
2444 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002445
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002446 // If user did not ask for specific calling convention explicitly (e.g. via
2447 // pcs attribute), set effective calling convention if it's different than ABI
2448 // default.
Daniel Dunbar020daa92009-09-12 01:00:39 +00002449 switch (getABIKind()) {
2450 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002451 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2452 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002453 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002454 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002455 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2456 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002457 break;
Daniel Dunbar020daa92009-09-12 01:00:39 +00002458 case AAPCS_VFP:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002459 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2460 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002461 break;
2462 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002463}
2464
Bob Wilsone826a2a2011-08-03 05:58:22 +00002465/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2466/// aggregate. If HAMembers is non-null, the number of base elements
2467/// contained in the type is returned through it; this is used for the
2468/// recursive calls that check aggregate component types.
2469static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2470 ASTContext &Context,
2471 uint64_t *HAMembers = 0) {
2472 uint64_t Members;
2473 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2474 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2475 return false;
2476 Members *= AT->getSize().getZExtValue();
2477 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2478 const RecordDecl *RD = RT->getDecl();
2479 if (RD->isUnion() || RD->hasFlexibleArrayMember())
2480 return false;
2481 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2482 if (!CXXRD->isAggregate())
2483 return false;
2484 }
2485 Members = 0;
2486 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2487 i != e; ++i) {
2488 const FieldDecl *FD = *i;
2489 uint64_t FldMembers;
2490 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2491 return false;
2492 Members += FldMembers;
2493 }
2494 } else {
2495 Members = 1;
2496 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2497 Members = 2;
2498 Ty = CT->getElementType();
2499 }
2500
2501 // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2502 // double, or 64-bit or 128-bit vectors.
2503 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2504 if (BT->getKind() != BuiltinType::Float &&
2505 BT->getKind() != BuiltinType::Double)
2506 return false;
2507 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2508 unsigned VecSize = Context.getTypeSize(VT);
2509 if (VecSize != 64 && VecSize != 128)
2510 return false;
2511 } else {
2512 return false;
2513 }
2514
2515 // The base type must be the same for all members. Vector types of the
2516 // same total size are treated as being equivalent here.
2517 const Type *TyPtr = Ty.getTypePtr();
2518 if (!Base)
2519 Base = TyPtr;
2520 if (Base != TyPtr &&
2521 (!Base->isVectorType() || !TyPtr->isVectorType() ||
2522 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2523 return false;
2524 }
2525
2526 // Homogeneous Aggregates can have at most 4 members of the base type.
2527 if (HAMembers)
2528 *HAMembers = Members;
2529 return (Members <= 4);
2530}
2531
Chris Lattner458b2aa2010-07-29 02:16:43 +00002532ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002533 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002534 // Treat an enum type as its underlying type.
2535 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2536 Ty = EnumTy->getDecl()->getIntegerType();
2537
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002538 return (Ty->isPromotableIntegerType() ?
2539 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002540 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002541
Daniel Dunbar09d33622009-09-14 21:54:03 +00002542 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002543 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002544 return ABIArgInfo::getIgnore();
2545
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002546 // Structures with either a non-trivial destructor or a non-trivial
2547 // copy constructor are always indirect.
2548 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2549 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2550
Bob Wilsone826a2a2011-08-03 05:58:22 +00002551 if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2552 // Homogeneous Aggregates need to be expanded.
2553 const Type *Base = 0;
2554 if (isHomogeneousAggregate(Ty, Base, getContext()))
2555 return ABIArgInfo::getExpand();
2556 }
2557
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002558 // Otherwise, pass by coercing to a structure of the appropriate size.
2559 //
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002560 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2561 // backend doesn't support byval.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002562 // FIXME: This doesn't handle alignment > 64 bits.
Chris Lattner2192fe52011-07-18 04:24:23 +00002563 llvm::Type* ElemTy;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002564 unsigned SizeRegs;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002565 if (getContext().getTypeAlign(Ty) > 32) {
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002566 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2567 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Bob Wilson8e2b75d2011-08-01 23:39:04 +00002568 } else {
2569 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2570 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Stuart Hastingsf2752a32011-04-27 17:24:02 +00002571 }
Stuart Hastings4b214952011-04-28 18:16:06 +00002572
Chris Lattnera5f58b02011-07-09 17:41:47 +00002573 llvm::Type *STy =
Chris Lattner845511f2011-06-18 22:49:11 +00002574 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
Stuart Hastings4b214952011-04-28 18:16:06 +00002575 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002576}
2577
Chris Lattner458b2aa2010-07-29 02:16:43 +00002578static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002579 llvm::LLVMContext &VMContext) {
2580 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2581 // is called integer-like if its size is less than or equal to one word, and
2582 // the offset of each of its addressable sub-fields is zero.
2583
2584 uint64_t Size = Context.getTypeSize(Ty);
2585
2586 // Check that the type fits in a word.
2587 if (Size > 32)
2588 return false;
2589
2590 // FIXME: Handle vector types!
2591 if (Ty->isVectorType())
2592 return false;
2593
Daniel Dunbard53bac72009-09-14 02:20:34 +00002594 // Float types are never treated as "integer like".
2595 if (Ty->isRealFloatingType())
2596 return false;
2597
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002598 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002599 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002600 return true;
2601
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002602 // Small complex integer types are "integer like".
2603 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2604 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002605
2606 // Single element and zero sized arrays should be allowed, by the definition
2607 // above, but they are not.
2608
2609 // Otherwise, it must be a record type.
2610 const RecordType *RT = Ty->getAs<RecordType>();
2611 if (!RT) return false;
2612
2613 // Ignore records with flexible arrays.
2614 const RecordDecl *RD = RT->getDecl();
2615 if (RD->hasFlexibleArrayMember())
2616 return false;
2617
2618 // Check that all sub-fields are at offset 0, and are themselves "integer
2619 // like".
2620 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2621
2622 bool HadField = false;
2623 unsigned idx = 0;
2624 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2625 i != e; ++i, ++idx) {
2626 const FieldDecl *FD = *i;
2627
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002628 // Bit-fields are not addressable, we only need to verify they are "integer
2629 // like". We still have to disallow a subsequent non-bitfield, for example:
2630 // struct { int : 0; int x }
2631 // is non-integer like according to gcc.
2632 if (FD->isBitField()) {
2633 if (!RD->isUnion())
2634 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002635
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002636 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2637 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002638
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002639 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002640 }
2641
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002642 // Check if this field is at offset 0.
2643 if (Layout.getFieldOffset(idx) != 0)
2644 return false;
2645
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002646 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2647 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002648
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002649 // Only allow at most one field in a structure. This doesn't match the
2650 // wording above, but follows gcc in situations with a field following an
2651 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002652 if (!RD->isUnion()) {
2653 if (HadField)
2654 return false;
2655
2656 HadField = true;
2657 }
2658 }
2659
2660 return true;
2661}
2662
Chris Lattner458b2aa2010-07-29 02:16:43 +00002663ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002664 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002665 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002666
Daniel Dunbar19964db2010-09-23 01:54:32 +00002667 // Large vector types should be returned via memory.
2668 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2669 return ABIArgInfo::getIndirect(0);
2670
John McCalla1dee5302010-08-22 10:59:02 +00002671 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002672 // Treat an enum type as its underlying type.
2673 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2674 RetTy = EnumTy->getDecl()->getIntegerType();
2675
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002676 return (RetTy->isPromotableIntegerType() ?
2677 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002678 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002679
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002680 // Structures with either a non-trivial destructor or a non-trivial
2681 // copy constructor are always indirect.
2682 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2683 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2684
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002685 // Are we following APCS?
2686 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002687 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002688 return ABIArgInfo::getIgnore();
2689
Daniel Dunbareedf1512010-02-01 23:31:19 +00002690 // Complex types are all returned as packed integers.
2691 //
2692 // FIXME: Consider using 2 x vector types if the back end handles them
2693 // correctly.
2694 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002695 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002696 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002697
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002698 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002699 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002700 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002701 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002702 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002703 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002704 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002705 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2706 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002707 }
2708
2709 // Otherwise return in memory.
2710 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002711 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002712
2713 // Otherwise this is an AAPCS variant.
2714
Chris Lattner458b2aa2010-07-29 02:16:43 +00002715 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002716 return ABIArgInfo::getIgnore();
2717
Bob Wilson1d9269a2011-11-02 04:51:36 +00002718 // Check for homogeneous aggregates with AAPCS-VFP.
2719 if (getABIKind() == AAPCS_VFP) {
2720 const Type *Base = 0;
2721 if (isHomogeneousAggregate(RetTy, Base, getContext()))
2722 // Homogeneous Aggregates are returned directly.
2723 return ABIArgInfo::getDirect();
2724 }
2725
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002726 // Aggregates <= 4 bytes are returned in r0; other aggregates
2727 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002728 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002729 if (Size <= 32) {
2730 // Return in the smallest viable integer type.
2731 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002732 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002733 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002734 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2735 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002736 }
2737
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002738 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002739}
2740
2741llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002742 CodeGenFunction &CGF) const {
Chris Lattner2192fe52011-07-18 04:24:23 +00002743 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2744 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002745
2746 CGBuilderTy &Builder = CGF.Builder;
2747 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2748 "ap");
2749 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
Rafael Espindola11d994b2011-08-02 22:33:37 +00002750 // Handle address alignment for type alignment > 32 bits
2751 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2752 if (TyAlign > 4) {
2753 assert((TyAlign & (TyAlign - 1)) == 0 &&
2754 "Alignment is not power of 2!");
2755 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2756 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2757 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2758 Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2759 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002760 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002761 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002762 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2763
2764 uint64_t Offset =
2765 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2766 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002767 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002768 "ap.next");
2769 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2770
2771 return AddrTyped;
2772}
2773
Chris Lattner0cf24192010-06-28 20:05:43 +00002774//===----------------------------------------------------------------------===//
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002775// PTX ABI Implementation
2776//===----------------------------------------------------------------------===//
2777
2778namespace {
2779
2780class PTXABIInfo : public ABIInfo {
2781public:
2782 PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2783
2784 ABIArgInfo classifyReturnType(QualType RetTy) const;
2785 ABIArgInfo classifyArgumentType(QualType Ty) const;
2786
2787 virtual void computeInfo(CGFunctionInfo &FI) const;
2788 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2789 CodeGenFunction &CFG) const;
2790};
2791
2792class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
2793public:
2794 PTXTargetCodeGenInfo(CodeGenTypes &CGT)
2795 : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
Justin Holewinski38031972011-10-05 17:58:44 +00002796
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00002797 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2798 CodeGen::CodeGenModule &M) const;
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002799};
2800
2801ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
2802 if (RetTy->isVoidType())
2803 return ABIArgInfo::getIgnore();
2804 if (isAggregateTypeForABI(RetTy))
2805 return ABIArgInfo::getIndirect(0);
2806 return ABIArgInfo::getDirect();
2807}
2808
2809ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
2810 if (isAggregateTypeForABI(Ty))
2811 return ABIArgInfo::getIndirect(0);
2812
2813 return ABIArgInfo::getDirect();
2814}
2815
2816void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
2817 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2818 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2819 it != ie; ++it)
2820 it->info = classifyArgumentType(it->type);
2821
2822 // Always honor user-specified calling convention.
2823 if (FI.getCallingConvention() != llvm::CallingConv::C)
2824 return;
2825
2826 // Calling convention as default by an ABI.
2827 llvm::CallingConv::ID DefaultCC;
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002828 const LangOptions &LangOpts = getContext().getLangOptions();
2829 if (LangOpts.OpenCL || LangOpts.CUDA) {
2830 // If we are in OpenCL or CUDA mode, then default to device functions
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002831 DefaultCC = llvm::CallingConv::PTX_Device;
Justin Holewinski38031972011-10-05 17:58:44 +00002832 } else {
2833 // If we are in standard C/C++ mode, use the triple to decide on the default
2834 StringRef Env =
2835 getContext().getTargetInfo().getTriple().getEnvironmentName();
2836 if (Env == "device")
2837 DefaultCC = llvm::CallingConv::PTX_Device;
2838 else
2839 DefaultCC = llvm::CallingConv::PTX_Kernel;
2840 }
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002841 FI.setEffectiveCallingConvention(DefaultCC);
Justin Holewinski38031972011-10-05 17:58:44 +00002842
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002843}
2844
2845llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2846 CodeGenFunction &CFG) const {
2847 llvm_unreachable("PTX does not support varargs");
2848 return 0;
2849}
2850
Justin Holewinski38031972011-10-05 17:58:44 +00002851void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2852 llvm::GlobalValue *GV,
2853 CodeGen::CodeGenModule &M) const{
2854 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2855 if (!FD) return;
2856
2857 llvm::Function *F = cast<llvm::Function>(GV);
2858
2859 // Perform special handling in OpenCL mode
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002860 if (M.getLangOptions().OpenCL) {
Justin Holewinski38031972011-10-05 17:58:44 +00002861 // Use OpenCL function attributes to set proper calling conventions
2862 // By default, all functions are device functions
Justin Holewinski38031972011-10-05 17:58:44 +00002863 if (FD->hasAttr<OpenCLKernelAttr>()) {
2864 // OpenCL __kernel functions get a kernel calling convention
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002865 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002866 // And kernel functions are not subject to inlining
2867 F->addFnAttr(llvm::Attribute::NoInline);
2868 }
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002869 }
Justin Holewinski38031972011-10-05 17:58:44 +00002870
Peter Collingbourne5bad4af2011-10-06 16:49:54 +00002871 // Perform special handling in CUDA mode.
2872 if (M.getLangOptions().CUDA) {
2873 // CUDA __global__ functions get a kernel calling convention. Since
2874 // __global__ functions cannot be called from the device, we do not
2875 // need to set the noinline attribute.
2876 if (FD->getAttr<CUDAGlobalAttr>())
2877 F->setCallingConv(llvm::CallingConv::PTX_Kernel);
Justin Holewinski38031972011-10-05 17:58:44 +00002878 }
2879}
2880
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00002881}
2882
2883//===----------------------------------------------------------------------===//
Wesley Peck36a1f682010-12-19 19:57:51 +00002884// MBlaze ABI Implementation
2885//===----------------------------------------------------------------------===//
2886
2887namespace {
2888
2889class MBlazeABIInfo : public ABIInfo {
2890public:
2891 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2892
2893 bool isPromotableIntegerType(QualType Ty) const;
2894
2895 ABIArgInfo classifyReturnType(QualType RetTy) const;
2896 ABIArgInfo classifyArgumentType(QualType RetTy) const;
2897
2898 virtual void computeInfo(CGFunctionInfo &FI) const {
2899 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2900 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2901 it != ie; ++it)
2902 it->info = classifyArgumentType(it->type);
2903 }
2904
2905 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2906 CodeGenFunction &CGF) const;
2907};
2908
2909class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2910public:
2911 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2912 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2913 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2914 CodeGen::CodeGenModule &M) const;
2915};
2916
2917}
2918
2919bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2920 // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2921 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2922 switch (BT->getKind()) {
2923 case BuiltinType::Bool:
2924 case BuiltinType::Char_S:
2925 case BuiltinType::Char_U:
2926 case BuiltinType::SChar:
2927 case BuiltinType::UChar:
2928 case BuiltinType::Short:
2929 case BuiltinType::UShort:
2930 return true;
2931 default:
2932 return false;
2933 }
2934 return false;
2935}
2936
2937llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2938 CodeGenFunction &CGF) const {
2939 // FIXME: Implement
2940 return 0;
2941}
2942
2943
2944ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
2945 if (RetTy->isVoidType())
2946 return ABIArgInfo::getIgnore();
2947 if (isAggregateTypeForABI(RetTy))
2948 return ABIArgInfo::getIndirect(0);
2949
2950 return (isPromotableIntegerType(RetTy) ?
2951 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2952}
2953
2954ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
2955 if (isAggregateTypeForABI(Ty))
2956 return ABIArgInfo::getIndirect(0);
2957
2958 return (isPromotableIntegerType(Ty) ?
2959 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2960}
2961
2962void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2963 llvm::GlobalValue *GV,
2964 CodeGen::CodeGenModule &M)
2965 const {
2966 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2967 if (!FD) return;
NAKAMURA Takumi029d74b2011-02-17 08:50:50 +00002968
Wesley Peck36a1f682010-12-19 19:57:51 +00002969 llvm::CallingConv::ID CC = llvm::CallingConv::C;
2970 if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
2971 CC = llvm::CallingConv::MBLAZE_INTR;
2972 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
2973 CC = llvm::CallingConv::MBLAZE_SVOL;
2974
2975 if (CC != llvm::CallingConv::C) {
2976 // Handle 'interrupt_handler' attribute:
2977 llvm::Function *F = cast<llvm::Function>(GV);
2978
2979 // Step 1: Set ISR calling convention.
2980 F->setCallingConv(CC);
2981
2982 // Step 2: Add attributes goodness.
2983 F->addFnAttr(llvm::Attribute::NoInline);
2984 }
2985
2986 // Step 3: Emit _interrupt_handler alias.
2987 if (CC == llvm::CallingConv::MBLAZE_INTR)
2988 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2989 "_interrupt_handler", GV, &M.getModule());
2990}
2991
2992
2993//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002994// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002995//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002996
2997namespace {
2998
2999class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
3000public:
Chris Lattner2b037972010-07-29 02:01:43 +00003001 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3002 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003003 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3004 CodeGen::CodeGenModule &M) const;
3005};
3006
3007}
3008
3009void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3010 llvm::GlobalValue *GV,
3011 CodeGen::CodeGenModule &M) const {
3012 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
3013 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
3014 // Handle 'interrupt' attribute:
3015 llvm::Function *F = cast<llvm::Function>(GV);
3016
3017 // Step 1: Set ISR calling convention.
3018 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
3019
3020 // Step 2: Add attributes goodness.
3021 F->addFnAttr(llvm::Attribute::NoInline);
3022
3023 // Step 3: Emit ISR vector alias.
3024 unsigned Num = attr->getNumber() + 0xffe0;
3025 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003026 "vector_" + Twine::utohexstr(Num),
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003027 GV, &M.getModule());
3028 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003029 }
3030}
3031
Chris Lattner0cf24192010-06-28 20:05:43 +00003032//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00003033// MIPS ABI Implementation. This works for both little-endian and
3034// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00003035//===----------------------------------------------------------------------===//
3036
John McCall943fae92010-05-27 06:19:26 +00003037namespace {
Akira Hatanakab579fe52011-06-02 00:09:17 +00003038class MipsABIInfo : public ABIInfo {
Akira Hatanaka14378522011-11-02 23:14:57 +00003039 bool IsO32;
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00003040 unsigned MinABIStackAlignInBytes;
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003041 llvm::Type* HandleStructTy(QualType Ty) const;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003042public:
Akira Hatanaka756ce7f2011-11-03 00:05:50 +00003043 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3044 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
Akira Hatanakab579fe52011-06-02 00:09:17 +00003045
3046 ABIArgInfo classifyReturnType(QualType RetTy) const;
3047 ABIArgInfo classifyArgumentType(QualType RetTy) const;
3048 virtual void computeInfo(CGFunctionInfo &FI) const;
3049 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3050 CodeGenFunction &CGF) const;
3051};
3052
John McCall943fae92010-05-27 06:19:26 +00003053class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003054 unsigned SizeOfUnwindException;
John McCall943fae92010-05-27 06:19:26 +00003055public:
Akira Hatanaka14378522011-11-02 23:14:57 +00003056 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3057 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3058 SizeOfUnwindException(IsO32 ? 24 : 32) {}
John McCall943fae92010-05-27 06:19:26 +00003059
3060 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3061 return 29;
3062 }
3063
3064 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00003065 llvm::Value *Address) const;
John McCall3480ef22011-08-30 01:42:09 +00003066
3067 unsigned getSizeOfUnwindException() const {
Akira Hatanaka0486db02011-09-20 18:23:28 +00003068 return SizeOfUnwindException;
John McCall3480ef22011-08-30 01:42:09 +00003069 }
John McCall943fae92010-05-27 06:19:26 +00003070};
3071}
3072
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003073// In N32/64, an aligned double precision floating point field is passed in
3074// a register.
3075llvm::Type* MipsABIInfo::HandleStructTy(QualType Ty) const {
3076 if (IsO32)
3077 return 0;
3078
3079 const RecordType *RT = Ty->getAsStructureType();
3080
3081 if (!RT)
3082 return 0;
3083
3084 const RecordDecl *RD = RT->getDecl();
3085 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3086 uint64_t StructSize = getContext().getTypeSize(Ty);
3087 assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3088
3089 SmallVector<llvm::Type*, 8> ArgList;
3090 uint64_t LastOffset = 0;
3091 unsigned idx = 0;
3092 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3093
3094 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3095 i != e; ++i, ++idx) {
3096 const QualType Ty = (*i)->getType();
3097 const BuiltinType *BT = Ty->getAs<BuiltinType>();
3098
3099 if (!BT || BT->getKind() != BuiltinType::Double)
3100 continue;
3101
3102 uint64_t Offset = Layout.getFieldOffset(idx);
3103 if (Offset % 64) // Ignore doubles that are not aligned.
3104 continue;
3105
3106 // Add ((Offset - LastOffset) / 64) args of type i64.
3107 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3108 ArgList.push_back(I64);
3109
3110 // Add double type.
3111 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3112 LastOffset = Offset + 64;
3113 }
3114
3115 // This structure doesn't have an aligned double field.
3116 if (!LastOffset)
3117 return 0;
3118
3119 // Add ((StructSize - LastOffset) / 64) args of type i64.
3120 for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3121 ArgList.push_back(I64);
3122
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003123 // If the size of the remainder is not zero, add one more integer type to
3124 // ArgList.
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003125 unsigned R = (StructSize - LastOffset) % 64;
Akira Hatanakaf3879ee2011-11-03 23:31:00 +00003126 if (R)
3127 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003128
3129 return llvm::StructType::get(getVMContext(), ArgList);
3130}
3131
Akira Hatanakab579fe52011-06-02 00:09:17 +00003132ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const {
3133 if (isAggregateTypeForABI(Ty)) {
3134 // Ignore empty aggregates.
3135 if (getContext().getTypeSize(Ty) == 0)
3136 return ABIArgInfo::getIgnore();
3137
Akira Hatanakadf425db2011-08-01 18:09:58 +00003138 // Records with non trivial destructors/constructors should not be passed
3139 // by value.
3140 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
3141 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3142
Akira Hatanaka101f70d2011-11-02 23:54:49 +00003143 llvm::Type *ResType;
3144 if ((ResType = HandleStructTy(Ty)))
3145 return ABIArgInfo::getDirect(ResType);
3146
Akira Hatanakab579fe52011-06-02 00:09:17 +00003147 return ABIArgInfo::getIndirect(0);
3148 }
3149
3150 // Treat an enum type as its underlying type.
3151 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3152 Ty = EnumTy->getDecl()->getIntegerType();
3153
3154 return (Ty->isPromotableIntegerType() ?
3155 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3156}
3157
3158ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3159 if (RetTy->isVoidType())
3160 return ABIArgInfo::getIgnore();
3161
3162 if (isAggregateTypeForABI(RetTy)) {
Akira Hatanaka14378522011-11-02 23:14:57 +00003163 if ((IsO32 && RetTy->isAnyComplexType()) ||
3164 (!IsO32 && (getContext().getTypeSize(RetTy) <= 128)))
Akira Hatanakab579fe52011-06-02 00:09:17 +00003165 return ABIArgInfo::getDirect();
3166
3167 return ABIArgInfo::getIndirect(0);
3168 }
3169
3170 // Treat an enum type as its underlying type.
3171 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3172 RetTy = EnumTy->getDecl()->getIntegerType();
3173
3174 return (RetTy->isPromotableIntegerType() ?
3175 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3176}
3177
3178void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3179 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3180 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3181 it != ie; ++it)
3182 it->info = classifyArgumentType(it->type);
3183}
3184
3185llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3186 CodeGenFunction &CGF) const {
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003187 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3188 llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
3189
3190 CGBuilderTy &Builder = CGF.Builder;
3191 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3192 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3193 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
3194 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3195 llvm::Value *AddrTyped;
3196
3197 if (TypeAlign > MinABIStackAlignInBytes) {
3198 llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3199 llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1);
3200 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign);
3201 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc);
3202 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3203 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3204 }
3205 else
3206 AddrTyped = Builder.CreateBitCast(Addr, PTy);
3207
3208 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
Akira Hatanakaae31c7a2011-08-12 02:30:14 +00003209 TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes);
Akira Hatanakafb1d9f32011-08-01 20:48:01 +00003210 uint64_t Offset =
3211 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3212 llvm::Value *NextAddr =
3213 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3214 "ap.next");
3215 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3216
3217 return AddrTyped;
Akira Hatanakab579fe52011-06-02 00:09:17 +00003218}
3219
John McCall943fae92010-05-27 06:19:26 +00003220bool
3221MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3222 llvm::Value *Address) const {
3223 // This information comes from gcc's implementation, which seems to
3224 // as canonical as it gets.
3225
3226 CodeGen::CGBuilderTy &Builder = CGF.Builder;
3227 llvm::LLVMContext &Context = CGF.getLLVMContext();
3228
3229 // Everything on MIPS is 4 bytes. Double-precision FP registers
3230 // are aliased to pairs of single-precision FP registers.
Chris Lattner2192fe52011-07-18 04:24:23 +00003231 llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
John McCall943fae92010-05-27 06:19:26 +00003232 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3233
3234 // 0-31 are the general purpose registers, $0 - $31.
3235 // 32-63 are the floating-point registers, $f0 - $f31.
3236 // 64 and 65 are the multiply/divide registers, $hi and $lo.
3237 // 66 is the (notional, I think) register for signal-handler return.
3238 AssignToArrayRange(Builder, Address, Four8, 0, 65);
3239
3240 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3241 // They are one bit wide and ignored here.
3242
3243 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3244 // (coprocessor 1 is the FP unit)
3245 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3246 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3247 // 176-181 are the DSP accumulator registers.
3248 AssignToArrayRange(Builder, Address, Four8, 80, 181);
3249
3250 return false;
3251}
3252
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003253//===----------------------------------------------------------------------===//
3254// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
3255// Currently subclassed only to implement custom OpenCL C function attribute
3256// handling.
3257//===----------------------------------------------------------------------===//
3258
3259namespace {
3260
3261class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3262public:
3263 TCETargetCodeGenInfo(CodeGenTypes &CGT)
3264 : DefaultTargetCodeGenInfo(CGT) {}
3265
3266 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3267 CodeGen::CodeGenModule &M) const;
3268};
3269
3270void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3271 llvm::GlobalValue *GV,
3272 CodeGen::CodeGenModule &M) const {
3273 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3274 if (!FD) return;
3275
3276 llvm::Function *F = cast<llvm::Function>(GV);
3277
3278 if (M.getLangOptions().OpenCL) {
3279 if (FD->hasAttr<OpenCLKernelAttr>()) {
3280 // OpenCL C Kernel functions are not subject to inlining
3281 F->addFnAttr(llvm::Attribute::NoInline);
3282
3283 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
3284
3285 // Convert the reqd_work_group_size() attributes to metadata.
3286 llvm::LLVMContext &Context = F->getContext();
3287 llvm::NamedMDNode *OpenCLMetadata =
3288 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
3289
3290 SmallVector<llvm::Value*, 5> Operands;
3291 Operands.push_back(F);
3292
3293 Operands.push_back(llvm::Constant::getIntegerValue(
3294 llvm::Type::getInt32Ty(Context),
3295 llvm::APInt(
3296 32,
3297 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
3298 Operands.push_back(llvm::Constant::getIntegerValue(
3299 llvm::Type::getInt32Ty(Context),
3300 llvm::APInt(
3301 32,
3302 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
3303 Operands.push_back(llvm::Constant::getIntegerValue(
3304 llvm::Type::getInt32Ty(Context),
3305 llvm::APInt(
3306 32,
3307 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
3308
3309 // Add a boolean constant operand for "required" (true) or "hint" (false)
3310 // for implementing the work_group_size_hint attr later. Currently
3311 // always true as the hint is not yet implemented.
3312 Operands.push_back(llvm::ConstantInt::getTrue(llvm::Type::getInt1Ty(Context)));
3313
3314 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
3315 }
3316 }
3317 }
3318}
3319
3320}
John McCall943fae92010-05-27 06:19:26 +00003321
Chris Lattner2b037972010-07-29 02:01:43 +00003322const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003323 if (TheTargetCodeGenInfo)
3324 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003325
Douglas Gregore8bbc122011-09-02 00:18:52 +00003326 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00003327 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003328 default:
Chris Lattner2b037972010-07-29 02:01:43 +00003329 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003330
John McCall943fae92010-05-27 06:19:26 +00003331 case llvm::Triple::mips:
3332 case llvm::Triple::mipsel:
Akira Hatanaka14378522011-11-02 23:14:57 +00003333 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
John McCall943fae92010-05-27 06:19:26 +00003334
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003335 case llvm::Triple::mips64:
3336 case llvm::Triple::mips64el:
Akira Hatanaka14378522011-11-02 23:14:57 +00003337 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
Akira Hatanakaec11b4f2011-09-20 18:30:57 +00003338
Daniel Dunbard59655c2009-09-12 00:59:49 +00003339 case llvm::Triple::arm:
3340 case llvm::Triple::thumb:
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003341 {
3342 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
Daniel Dunbar020daa92009-09-12 01:00:39 +00003343
Douglas Gregore8bbc122011-09-02 00:18:52 +00003344 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
Sandeep Patel45df3dd2011-04-05 00:23:47 +00003345 Kind = ARMABIInfo::APCS;
3346 else if (CodeGenOpts.FloatABI == "hard")
3347 Kind = ARMABIInfo::AAPCS_VFP;
3348
3349 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
3350 }
Daniel Dunbard59655c2009-09-12 00:59:49 +00003351
John McCallea8d8bb2010-03-11 00:10:12 +00003352 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00003353 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00003354
Justin Holewinskibd4a3c02011-04-22 11:10:38 +00003355 case llvm::Triple::ptx32:
3356 case llvm::Triple::ptx64:
3357 return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
3358
Wesley Peck36a1f682010-12-19 19:57:51 +00003359 case llvm::Triple::mblaze:
3360 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3361
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003362 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00003363 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00003364
Peter Collingbourneadcf7c92011-10-13 16:24:41 +00003365 case llvm::Triple::tce:
3366 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
3367
Eli Friedman33465822011-07-08 23:31:17 +00003368 case llvm::Triple::x86: {
Douglas Gregore8bbc122011-09-02 00:18:52 +00003369 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
Eli Friedman33465822011-07-08 23:31:17 +00003370
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003371 if (Triple.isOSDarwin())
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003372 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003373 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX));
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00003374
3375 switch (Triple.getOS()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00003376 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003377 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00003378 case llvm::Triple::AuroraUX:
3379 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00003380 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00003381 case llvm::Triple::OpenBSD:
Benjamin Kramer24f1d3e2011-02-02 18:59:27 +00003382 case llvm::Triple::NetBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003383 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003384 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX));
Daniel Dunbare3532f82009-08-24 08:52:16 +00003385
3386 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003387 return *(TheTargetCodeGenInfo =
Eli Friedman33465822011-07-08 23:31:17 +00003388 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003389 }
Eli Friedman33465822011-07-08 23:31:17 +00003390 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003391
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003392 case llvm::Triple::x86_64: {
3393 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3394
Chris Lattner04dc9572010-08-31 16:44:54 +00003395 switch (Triple.getOS()) {
3396 case llvm::Triple::Win32:
NAKAMURA Takumi31ea2f12011-02-17 08:51:38 +00003397 case llvm::Triple::MinGW32:
Chris Lattner04dc9572010-08-31 16:44:54 +00003398 case llvm::Triple::Cygwin:
3399 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3400 default:
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003401 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3402 HasAVX));
Chris Lattner04dc9572010-08-31 16:44:54 +00003403 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00003404 }
Eli Friedmanbfd5add2011-12-02 00:11:43 +00003405 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00003406}