blob: 34a6d37726b5f5065f543037523ae1e216e58a08 [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner9c254f02010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCallaeeb7012010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalld608cdb2010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattnerea044322010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattner800588f2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000069 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000072 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000075 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbardc6d5742010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar98303b92009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000091
92/// isEmptyField - Return true iff a the field is "empty", that is it
93/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000100
Daniel Dunbar98303b92009-09-13 08:03:58 +0000101 // Constant arrays of empty records count as empty, strip them off.
102 if (AllowArrays)
103 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
104 FT = AT->getElementType();
105
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000106 const RecordType *RT = FT->getAs<RecordType>();
107 if (!RT)
108 return false;
109
110 // C++ record fields are never empty, at least in the Itanium ABI.
111 //
112 // FIXME: We should use a predicate for whether this behavior is true in the
113 // current ABI.
114 if (isa<CXXRecordDecl>(RT->getDecl()))
115 return false;
116
Daniel Dunbar98303b92009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000118}
119
120/// isEmptyRecord - Return true iff a structure contains only empty
121/// fields. Note that a structure with a flexible array member is not
122/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000125 if (!RT)
126 return 0;
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return false;
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000130
131 // If this is a C++ record, check the bases first.
132 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
133 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
134 e = CXXRD->bases_end(); i != e; ++i)
135 if (!isEmptyRecord(Context, i->getType(), true))
136 return false;
137
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000145/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
146/// a non-trivial destructor or a non-trivial copy constructor.
147static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
148 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
149 if (!RD)
150 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000151
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000152 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
153}
154
155/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
156/// a record type with either a non-trivial destructor or a non-trivial copy
157/// constructor.
158static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
159 const RecordType *RT = T->getAs<RecordType>();
160 if (!RT)
161 return false;
162
163 return hasNonTrivialDestructorOrCopyConstructor(RT);
164}
165
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000166/// isSingleElementStruct - Determine if a structure is a "single
167/// element struct", i.e. it has exactly one non-empty field or
168/// exactly one field which is itself a single element
169/// struct. Structures with flexible array members are never
170/// considered single element structs.
171///
172/// \return The field declaration for the single non-empty field, if
173/// it exists.
174static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
175 const RecordType *RT = T->getAsStructureType();
176 if (!RT)
177 return 0;
178
179 const RecordDecl *RD = RT->getDecl();
180 if (RD->hasFlexibleArrayMember())
181 return 0;
182
183 const Type *Found = 0;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000184
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000185 // If this is a C++ record, check the bases first.
186 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
187 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
188 e = CXXRD->bases_end(); i != e; ++i) {
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbar5ea68612010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar9430d5a2010-05-11 21:15:36 +0000191 continue;
192
193 // If we already found an element then this isn't a single-element struct.
194 if (Found)
195 return 0;
196
197 // If this is non-empty and not a single element struct, the composite
198 // cannot be a single element struct.
199 Found = isSingleElementStruct(i->getType(), Context);
200 if (!Found)
201 return 0;
202 }
203 }
204
205 // Check for single element.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000213 continue;
214
215 // If we already found an element then this isn't a single-element
216 // struct.
217 if (Found)
218 return 0;
219
220 // Treat single element arrays as the element.
221 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
222 if (AT->getSize().getZExtValue() != 1)
223 break;
224 FT = AT->getElementType();
225 }
226
John McCalld608cdb2010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000228 Found = FT.getTypePtr();
229 } else {
230 Found = isSingleElementStruct(FT, Context);
231 if (!Found)
232 return 0;
233 }
234 }
235
236 return Found;
237}
238
239static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbara1842d32010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000243 return false;
244
245 uint64_t Size = Context.getTypeSize(Ty);
246 return Size == 32 || Size == 64;
247}
248
Daniel Dunbar53012f42009-11-09 01:33:53 +0000249/// canExpandIndirectArgument - Test whether an argument type which is to be
250/// passed indirectly (on the stack) would have the equivalent layout if it was
251/// expanded into separate arguments. If so, we prefer to do the latter to avoid
252/// inhibiting optimizations.
253///
254// FIXME: This predicate is missing many cases, currently it just follows
255// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
256// should probably make this smarter, or better yet make the LLVM backend
257// capable of handling it.
258static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
259 // We can only expand structure types.
260 const RecordType *RT = Ty->getAs<RecordType>();
261 if (!RT)
262 return false;
263
264 // We can only expand (C) structures.
265 //
266 // FIXME: This needs to be generalized to handle classes as well.
267 const RecordDecl *RD = RT->getDecl();
268 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
269 return false;
270
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000273 const FieldDecl *FD = *i;
274
275 if (!is32Or64BitBasicType(FD->getType(), Context))
276 return false;
277
278 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
279 // how to expand them yet, and the predicate for telling if a bitfield still
280 // counts as "basic" is more complicated than what we were doing previously.
281 if (FD->isBitField())
282 return false;
283 }
284
285 return true;
286}
287
288namespace {
289/// DefaultABIInfo - The default implementation for ABI specific
290/// details. This implementation provides information which results in
291/// self-consistent and sensible LLVM IR generation, but does not
292/// conform to any particular ABI.
293class DefaultABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000296
Chris Lattnera3c109b2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000299
Chris Lattneree5dcd02010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattnerea044322010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000315};
316
317llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
318 CodeGenFunction &CGF) const {
319 return 0;
320}
321
Chris Lattnera3c109b2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000325
Chris Lattnera14db752010-03-11 18:19:55 +0000326 // Treat an enum type as its underlying type.
327 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
328 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000329
Chris Lattnera14db752010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000332}
333
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000334//===----------------------------------------------------------------------===//
335// X86-32 ABI Implementation
336//===----------------------------------------------------------------------===//
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000337
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000338/// X86_32ABIInfo - The X86-32 ABI information.
339class X86_32ABIInfo : public ABIInfo {
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000340 static const unsigned MinABIStackAlignInBytes = 4;
341
David Chisnall1e4249c2009-08-17 23:08:21 +0000342 bool IsDarwinVectorABI;
343 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000344
345 static bool isRegisterSize(unsigned Size) {
346 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
347 }
348
349 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
350
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000351 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
352 /// such that the argument will be passed in memory.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000353 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000354
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000355 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbare59d8582010-09-16 20:42:06 +0000356 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000357
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000358public:
Chris Lattnerea044322010-07-29 02:01:43 +0000359
Chris Lattnera3c109b2010-07-29 02:16:43 +0000360 ABIArgInfo classifyReturnType(QualType RetTy) const;
361 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000362
Chris Lattneree5dcd02010-07-29 02:31:05 +0000363 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000364 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000365 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
366 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +0000367 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000368 }
369
370 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
371 CodeGenFunction &CGF) const;
372
Chris Lattnerea044322010-07-29 02:01:43 +0000373 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
374 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000375};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000376
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000377class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
378public:
Chris Lattnerea044322010-07-29 02:01:43 +0000379 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
380 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000381
382 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
383 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000384
385 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
386 // Darwin uses different dwarf register numbers for EH.
387 if (CGM.isTargetDarwin()) return 5;
388
389 return 4;
390 }
391
392 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
393 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000394};
395
396}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000397
398/// shouldReturnTypeInRegister - Determine if the given type should be
399/// passed in a register (for the Darwin ABI).
400bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
401 ASTContext &Context) {
402 uint64_t Size = Context.getTypeSize(Ty);
403
404 // Type must be register sized.
405 if (!isRegisterSize(Size))
406 return false;
407
408 if (Ty->isVectorType()) {
409 // 64- and 128- bit vectors inside structures are not returned in
410 // registers.
411 if (Size == 64 || Size == 128)
412 return false;
413
414 return true;
415 }
416
Daniel Dunbar77115232010-05-15 00:00:30 +0000417 // If this is a builtin, pointer, enum, complex type, member pointer, or
418 // member function pointer it is ok.
Daniel Dunbara1842d32010-05-14 03:40:53 +0000419 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000420 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar77115232010-05-15 00:00:30 +0000421 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000422 return true;
423
424 // Arrays are treated like records.
425 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
426 return shouldReturnTypeInRegister(AT->getElementType(), Context);
427
428 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000429 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000430 if (!RT) return false;
431
Anders Carlssona8874232010-01-27 03:25:19 +0000432 // FIXME: Traverse bases here too.
433
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000434 // Structure types are passed in register if all fields would be
435 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000436 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
437 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000438 const FieldDecl *FD = *i;
439
440 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000441 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000442 continue;
443
444 // Check fields recursively.
445 if (!shouldReturnTypeInRegister(FD->getType(), Context))
446 return false;
447 }
448
449 return true;
450}
451
Chris Lattnera3c109b2010-07-29 02:16:43 +0000452ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
453 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454 return ABIArgInfo::getIgnore();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000455
Chris Lattnera3c109b2010-07-29 02:16:43 +0000456 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000457 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000458 if (IsDarwinVectorABI) {
Chris Lattnera3c109b2010-07-29 02:16:43 +0000459 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000460
461 // 128-bit vectors are a special case; they are returned in
462 // registers and we need to make sure to pick a type the LLVM
463 // backend will like.
464 if (Size == 128)
Chris Lattner800588f2010-07-29 06:26:06 +0000465 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000466 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000467
468 // Always return in register if it fits in a general purpose
469 // register, or if it is 64 bits and has a single element.
470 if ((Size == 8 || Size == 16 || Size == 32) ||
471 (Size == 64 && VT->getNumElements() == 1))
Chris Lattner800588f2010-07-29 06:26:06 +0000472 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +0000473 Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000474
475 return ABIArgInfo::getIndirect(0);
476 }
477
478 return ABIArgInfo::getDirect();
Chris Lattnera3c109b2010-07-29 02:16:43 +0000479 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000480
John McCalld608cdb2010-08-22 10:59:02 +0000481 if (isAggregateTypeForABI(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000482 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000483 // Structures with either a non-trivial destructor or a non-trivial
484 // copy constructor are always indirect.
485 if (hasNonTrivialDestructorOrCopyConstructor(RT))
486 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000487
Anders Carlsson40092972009-10-20 22:07:59 +0000488 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000489 if (RT->getDecl()->hasFlexibleArrayMember())
490 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000491 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000492
David Chisnall1e4249c2009-08-17 23:08:21 +0000493 // If specified, structs and unions are always indirect.
494 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000495 return ABIArgInfo::getIndirect(0);
496
497 // Classify "single element" structs as their element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000498 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall183700f2009-09-21 23:43:11 +0000499 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000500 if (BT->isIntegerType()) {
501 // We need to use the size of the structure, padding
502 // bit-fields can adjust that to be larger than the single
503 // element type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000504 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000505 return ABIArgInfo::getDirect(
Chris Lattnera3c109b2010-07-29 02:16:43 +0000506 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
507 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000508
Chris Lattnera3c109b2010-07-29 02:16:43 +0000509 if (BT->getKind() == BuiltinType::Float) {
510 assert(getContext().getTypeSize(RetTy) ==
511 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000512 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000513 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattnera3c109b2010-07-29 02:16:43 +0000514 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000515
Chris Lattnera3c109b2010-07-29 02:16:43 +0000516 if (BT->getKind() == BuiltinType::Double) {
517 assert(getContext().getTypeSize(RetTy) ==
518 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519 "Unexpect single element structure size!");
Chris Lattner800588f2010-07-29 06:26:06 +0000520 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000521 }
522 } else if (SeltTy->isPointerType()) {
523 // FIXME: It would be really nice if this could come out as the proper
524 // pointer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000525 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattner800588f2010-07-29 06:26:06 +0000526 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000527 } else if (SeltTy->isVectorType()) {
528 // 64- and 128-bit vectors are never returned in a
529 // register when inside a structure.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000530 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000531 if (Size == 64 || Size == 128)
532 return ABIArgInfo::getIndirect(0);
533
Chris Lattnera3c109b2010-07-29 02:16:43 +0000534 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000535 }
536 }
537
538 // Small structures which are register sized are generally returned
539 // in a register.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000540 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
541 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +0000542 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000543 }
544
545 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000547
Chris Lattnera3c109b2010-07-29 02:16:43 +0000548 // Treat an enum type as its underlying type.
549 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
550 RetTy = EnumTy->getDecl()->getIntegerType();
551
552 return (RetTy->isPromotableIntegerType() ?
553 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000554}
555
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000556static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
557 const RecordType *RT = Ty->getAs<RecordType>();
558 if (!RT)
559 return 0;
560 const RecordDecl *RD = RT->getDecl();
561
562 // If this is a C++ record, check the bases first.
563 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
564 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
565 e = CXXRD->bases_end(); i != e; ++i)
566 if (!isRecordWithSSEVectorType(Context, i->getType()))
567 return false;
568
569 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
570 i != e; ++i) {
571 QualType FT = i->getType();
572
573 if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128)
574 return true;
575
576 if (isRecordWithSSEVectorType(Context, FT))
577 return true;
578 }
579
580 return false;
581}
582
Daniel Dunbare59d8582010-09-16 20:42:06 +0000583unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
584 unsigned Align) const {
585 // Otherwise, if the alignment is less than or equal to the minimum ABI
586 // alignment, just use the default; the backend will handle this.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000587 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbare59d8582010-09-16 20:42:06 +0000588 return 0; // Use default alignment.
589
590 // On non-Darwin, the stack type alignment is always 4.
591 if (!IsDarwinVectorABI) {
592 // Set explicit alignment, since we may need to realign the top.
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000593 return MinABIStackAlignInBytes;
Daniel Dunbare59d8582010-09-16 20:42:06 +0000594 }
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000595
Daniel Dunbar93ae9472010-09-16 20:42:00 +0000596 // Otherwise, if the type contains an SSE vector type, the alignment is 16.
597 if (isRecordWithSSEVectorType(getContext(), Ty))
598 return 16;
599
600 return MinABIStackAlignInBytes;
Daniel Dunbarfb67d6c2010-09-16 20:41:56 +0000601}
602
Chris Lattnera3c109b2010-07-29 02:16:43 +0000603ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000604 if (!ByVal)
605 return ABIArgInfo::getIndirect(0, false);
606
Daniel Dunbare59d8582010-09-16 20:42:06 +0000607 // Compute the byval alignment.
608 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
609 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
610 if (StackAlign == 0)
611 return ABIArgInfo::getIndirect(0);
612
613 // If the stack alignment is less than the type alignment, realign the
614 // argument.
615 if (StackAlign < TypeAlign)
616 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
617 /*Realign=*/true);
618
619 return ABIArgInfo::getIndirect(StackAlign);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000620}
621
Chris Lattnera3c109b2010-07-29 02:16:43 +0000622ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000623 // FIXME: Set alignment on indirect arguments.
John McCalld608cdb2010-08-22 10:59:02 +0000624 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000626 if (const RecordType *RT = Ty->getAs<RecordType>()) {
627 // Structures with either a non-trivial destructor or a non-trivial
628 // copy constructor are always indirect.
629 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Chris Lattnera3c109b2010-07-29 02:16:43 +0000630 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbardc6d5742010-04-21 19:10:51 +0000631
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000632 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattnera3c109b2010-07-29 02:16:43 +0000633 return getIndirectResult(Ty);
Anders Carlssona8874232010-01-27 03:25:19 +0000634 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000635
636 // Ignore empty structs.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000637 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000638 return ABIArgInfo::getIgnore();
639
Daniel Dunbar53012f42009-11-09 01:33:53 +0000640 // Expand small (<= 128-bit) record types when we know that the stack layout
641 // of those arguments will match the struct. This is important because the
642 // LLVM backend isn't smart enough to remove byval, which inhibits many
643 // optimizations.
Chris Lattnera3c109b2010-07-29 02:16:43 +0000644 if (getContext().getTypeSize(Ty) <= 4*32 &&
645 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar53012f42009-11-09 01:33:53 +0000646 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000647
Chris Lattnera3c109b2010-07-29 02:16:43 +0000648 return getIndirectResult(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000649 }
650
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000651 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner7b733502010-08-26 20:08:43 +0000652 // On Darwin, some vectors are passed in memory, we handle this by passing
653 // it as an i8/i16/i32/i64.
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000654 if (IsDarwinVectorABI) {
655 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000656 if ((Size == 8 || Size == 16 || Size == 32) ||
657 (Size == 64 && VT->getNumElements() == 1))
658 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
659 Size));
Chris Lattnerbbae8b42010-08-26 20:05:13 +0000660 }
661
662 return ABIArgInfo::getDirect();
663 }
664
665
Chris Lattnera3c109b2010-07-29 02:16:43 +0000666 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
667 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000668
Chris Lattnera3c109b2010-07-29 02:16:43 +0000669 return (Ty->isPromotableIntegerType() ?
670 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000671}
672
673llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
674 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000675 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000676 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000677
678 CGBuilderTy &Builder = CGF.Builder;
679 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
680 "ap");
681 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
682 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000683 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000684 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
685
686 uint64_t Offset =
687 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
688 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +0000689 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000690 "ap.next");
691 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
692
693 return AddrTyped;
694}
695
Charles Davis74f72932010-02-13 15:54:06 +0000696void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
697 llvm::GlobalValue *GV,
698 CodeGen::CodeGenModule &CGM) const {
699 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
700 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
701 // Get the LLVM function.
702 llvm::Function *Fn = cast<llvm::Function>(GV);
703
704 // Now add the 'alignstack' attribute with a value of 16.
705 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
706 }
707 }
708}
709
John McCall6374c332010-03-06 00:35:14 +0000710bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
711 CodeGen::CodeGenFunction &CGF,
712 llvm::Value *Address) const {
713 CodeGen::CGBuilderTy &Builder = CGF.Builder;
714 llvm::LLVMContext &Context = CGF.getLLVMContext();
715
716 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
717 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000718
John McCall6374c332010-03-06 00:35:14 +0000719 // 0-7 are the eight integer registers; the order is different
720 // on Darwin (for EH), but the range is the same.
721 // 8 is %eip.
John McCallaeeb7012010-05-27 06:19:26 +0000722 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCall6374c332010-03-06 00:35:14 +0000723
724 if (CGF.CGM.isTargetDarwin()) {
725 // 12-16 are st(0..4). Not sure why we stop at 4.
726 // These have size 16, which is sizeof(long double) on
727 // platforms with 8-byte alignment for that type.
728 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
John McCallaeeb7012010-05-27 06:19:26 +0000729 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000730
John McCall6374c332010-03-06 00:35:14 +0000731 } else {
732 // 9 is %eflags, which doesn't get a size on Darwin for some
733 // reason.
734 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
735
736 // 11-16 are st(0..5). Not sure why we stop at 5.
737 // These have size 12, which is sizeof(long double) on
738 // platforms with 4-byte alignment for that type.
739 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
John McCallaeeb7012010-05-27 06:19:26 +0000740 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
741 }
John McCall6374c332010-03-06 00:35:14 +0000742
743 return false;
744}
745
Chris Lattnerdce5ad02010-06-28 20:05:43 +0000746//===----------------------------------------------------------------------===//
747// X86-64 ABI Implementation
748//===----------------------------------------------------------------------===//
749
750
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000751namespace {
752/// X86_64ABIInfo - The X86_64 ABI information.
753class X86_64ABIInfo : public ABIInfo {
754 enum Class {
755 Integer = 0,
756 SSE,
757 SSEUp,
758 X87,
759 X87Up,
760 ComplexX87,
761 NoClass,
762 Memory
763 };
764
765 /// merge - Implement the X86_64 ABI merging algorithm.
766 ///
767 /// Merge an accumulating classification \arg Accum with a field
768 /// classification \arg Field.
769 ///
770 /// \param Accum - The accumulating classification. This should
771 /// always be either NoClass or the result of a previous merge
772 /// call. In addition, this should never be Memory (the caller
773 /// should just return Memory for the aggregate).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000774 static Class merge(Class Accum, Class Field);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000775
776 /// classify - Determine the x86_64 register classes in which the
777 /// given type T should be passed.
778 ///
779 /// \param Lo - The classification for the parts of the type
780 /// residing in the low word of the containing object.
781 ///
782 /// \param Hi - The classification for the parts of the type
783 /// residing in the high word of the containing object.
784 ///
785 /// \param OffsetBase - The bit offset of this type in the
786 /// containing object. Some parameters are classified different
787 /// depending on whether they straddle an eightbyte boundary.
788 ///
789 /// If a word is unused its result will be NoClass; if a type should
790 /// be passed in Memory then at least the classification of \arg Lo
791 /// will be Memory.
792 ///
793 /// The \arg Lo class will be NoClass iff the argument is ignored.
794 ///
795 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
796 /// also be ComplexX87.
Chris Lattner9c254f02010-06-29 06:01:59 +0000797 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000798
Chris Lattner0f408f52010-07-29 04:56:46 +0000799 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattner603519d2010-07-29 17:49:08 +0000800 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattnerf47c9442010-07-29 18:13:09 +0000801 unsigned IROffset, QualType SourceTy,
802 unsigned SourceOffset) const;
Chris Lattner0d2656d2010-07-29 17:40:35 +0000803 const llvm::Type *GetINTEGERTypeAtOffset(const llvm::Type *IRType,
804 unsigned IROffset, QualType SourceTy,
805 unsigned SourceOffset) const;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000806
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000807 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000808 /// such that the argument will be returned in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000809 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +0000810
811 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000812 /// such that the argument will be passed in memory.
Chris Lattner9c254f02010-06-29 06:01:59 +0000813 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000814
Chris Lattnera3c109b2010-07-29 02:16:43 +0000815 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000816
Chris Lattner5868ca22010-07-29 04:41:05 +0000817 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
818 unsigned &neededSSE) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000819
820public:
Chris Lattnerea044322010-07-29 02:01:43 +0000821 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner9c254f02010-06-29 06:01:59 +0000822
Chris Lattneree5dcd02010-07-29 02:31:05 +0000823 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000824
825 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
826 CodeGenFunction &CGF) const;
827};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000828
Chris Lattnerf13721d2010-08-31 16:44:54 +0000829/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
830class WinX86_64ABIInfo : public X86_64ABIInfo {
831public:
832 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : X86_64ABIInfo(CGT) {}
833
834 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
835 CodeGenFunction &CGF) const;
836};
837
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000838class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
839public:
Chris Lattnerea044322010-07-29 02:01:43 +0000840 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
841 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCall6374c332010-03-06 00:35:14 +0000842
843 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
844 return 7;
845 }
846
847 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
848 llvm::Value *Address) const {
849 CodeGen::CGBuilderTy &Builder = CGF.Builder;
850 llvm::LLVMContext &Context = CGF.getLLVMContext();
851
852 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
853 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000854
John McCallaeeb7012010-05-27 06:19:26 +0000855 // 0-15 are the 16 integer registers.
856 // 16 is %rip.
857 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
John McCall6374c332010-03-06 00:35:14 +0000858
859 return false;
860 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000861};
862
Chris Lattnerf13721d2010-08-31 16:44:54 +0000863class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
864public:
865 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
866 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
867
868 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
869 return 7;
870 }
871
872 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
873 llvm::Value *Address) const {
874 CodeGen::CGBuilderTy &Builder = CGF.Builder;
875 llvm::LLVMContext &Context = CGF.getLLVMContext();
876
877 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
878 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
879
880 // 0-15 are the 16 integer registers.
881 // 16 is %rip.
882 AssignToArrayRange(Builder, Address, Eight8, 0, 16);
883
884 return false;
885 }
886};
887
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000888}
889
Chris Lattner1090a9b2010-06-28 21:43:59 +0000890X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000891 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
892 // classified recursively so that always two fields are
893 // considered. The resulting class is calculated according to
894 // the classes of the fields in the eightbyte:
895 //
896 // (a) If both classes are equal, this is the resulting class.
897 //
898 // (b) If one of the classes is NO_CLASS, the resulting class is
899 // the other class.
900 //
901 // (c) If one of the classes is MEMORY, the result is the MEMORY
902 // class.
903 //
904 // (d) If one of the classes is INTEGER, the result is the
905 // INTEGER.
906 //
907 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
908 // MEMORY is used as class.
909 //
910 // (f) Otherwise class SSE is used.
911
912 // Accum should never be memory (we should have returned) or
913 // ComplexX87 (because this cannot be passed in a structure).
914 assert((Accum != Memory && Accum != ComplexX87) &&
915 "Invalid accumulated classification during merge.");
916 if (Accum == Field || Field == NoClass)
917 return Accum;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000918 if (Field == Memory)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000919 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000920 if (Accum == NoClass)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000921 return Field;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000922 if (Accum == Integer || Field == Integer)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000923 return Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000924 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
925 Accum == X87 || Accum == X87Up)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000926 return Memory;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000927 return SSE;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000928}
929
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000930void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000931 Class &Lo, Class &Hi) const {
932 // FIXME: This code can be simplified by introducing a simple value class for
933 // Class pairs with appropriate constructor methods for the various
934 // situations.
935
936 // FIXME: Some of the split computations are wrong; unaligned vectors
937 // shouldn't be passed in registers for example, so there is no chance they
938 // can straddle an eightbyte. Verify & simplify.
939
940 Lo = Hi = NoClass;
941
942 Class &Current = OffsetBase < 64 ? Lo : Hi;
943 Current = Memory;
944
John McCall183700f2009-09-21 23:43:11 +0000945 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000946 BuiltinType::Kind k = BT->getKind();
947
948 if (k == BuiltinType::Void) {
949 Current = NoClass;
950 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
951 Lo = Integer;
952 Hi = Integer;
953 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
954 Current = Integer;
955 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
956 Current = SSE;
957 } else if (k == BuiltinType::LongDouble) {
958 Lo = X87;
959 Hi = X87Up;
960 }
961 // FIXME: _Decimal32 and _Decimal64 are SSE.
962 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Chris Lattner1090a9b2010-06-28 21:43:59 +0000963 return;
964 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000965
Chris Lattner1090a9b2010-06-28 21:43:59 +0000966 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000967 // Classify the underlying integer type.
Chris Lattner9c254f02010-06-29 06:01:59 +0000968 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattner1090a9b2010-06-28 21:43:59 +0000969 return;
970 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000971
Chris Lattner1090a9b2010-06-28 21:43:59 +0000972 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000973 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000974 return;
975 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000976
Chris Lattner1090a9b2010-06-28 21:43:59 +0000977 if (Ty->isMemberPointerType()) {
Daniel Dunbar67d438d2010-05-15 00:00:37 +0000978 if (Ty->isMemberFunctionPointerType())
979 Lo = Hi = Integer;
980 else
981 Current = Integer;
Chris Lattner1090a9b2010-06-28 21:43:59 +0000982 return;
983 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +0000984
Chris Lattner1090a9b2010-06-28 21:43:59 +0000985 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +0000986 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000987 if (Size == 32) {
988 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
989 // float> as integer.
990 Current = Integer;
991
992 // If this type crosses an eightbyte boundary, it should be
993 // split.
994 uint64_t EB_Real = (OffsetBase) / 64;
995 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
996 if (EB_Real != EB_Imag)
997 Hi = Lo;
998 } else if (Size == 64) {
999 // gcc passes <1 x double> in memory. :(
1000 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1001 return;
1002
1003 // gcc passes <1 x long long> as INTEGER.
Chris Lattner473f8e72010-08-26 18:03:20 +00001004 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner0fefa412010-08-26 18:13:50 +00001005 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1006 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1007 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001008 Current = Integer;
1009 else
1010 Current = SSE;
1011
1012 // If this type crosses an eightbyte boundary, it should be
1013 // split.
1014 if (OffsetBase && OffsetBase != 64)
1015 Hi = Lo;
1016 } else if (Size == 128) {
1017 Lo = SSE;
1018 Hi = SSEUp;
1019 }
Chris Lattner1090a9b2010-06-28 21:43:59 +00001020 return;
1021 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001022
Chris Lattner1090a9b2010-06-28 21:43:59 +00001023 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001024 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001025
Chris Lattnerea044322010-07-29 02:01:43 +00001026 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001027 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001028 if (Size <= 64)
1029 Current = Integer;
1030 else if (Size <= 128)
1031 Lo = Hi = Integer;
Chris Lattnerea044322010-07-29 02:01:43 +00001032 } else if (ET == getContext().FloatTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001033 Current = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001034 else if (ET == getContext().DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001035 Lo = Hi = SSE;
Chris Lattnerea044322010-07-29 02:01:43 +00001036 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001037 Current = ComplexX87;
1038
1039 // If this complex type crosses an eightbyte boundary then it
1040 // should be split.
1041 uint64_t EB_Real = (OffsetBase) / 64;
Chris Lattnerea044322010-07-29 02:01:43 +00001042 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001043 if (Hi == NoClass && EB_Real != EB_Imag)
1044 Hi = Lo;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001045
Chris Lattner1090a9b2010-06-28 21:43:59 +00001046 return;
1047 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001048
Chris Lattnerea044322010-07-29 02:01:43 +00001049 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001050 // Arrays are treated like structures.
1051
Chris Lattnerea044322010-07-29 02:01:43 +00001052 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001053
1054 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1055 // than two eightbytes, ..., it has class MEMORY.
1056 if (Size > 128)
1057 return;
1058
1059 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1060 // fields, it has class MEMORY.
1061 //
1062 // Only need to check alignment of array base.
Chris Lattnerea044322010-07-29 02:01:43 +00001063 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001064 return;
1065
1066 // Otherwise implement simplified merge. We could be smarter about
1067 // this, but it isn't worth it and would be harder to verify.
1068 Current = NoClass;
Chris Lattnerea044322010-07-29 02:01:43 +00001069 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001070 uint64_t ArraySize = AT->getSize().getZExtValue();
1071 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1072 Class FieldLo, FieldHi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001073 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001074 Lo = merge(Lo, FieldLo);
1075 Hi = merge(Hi, FieldHi);
1076 if (Lo == Memory || Hi == Memory)
1077 break;
1078 }
1079
1080 // Do post merger cleanup (see below). Only case we worry about is Memory.
1081 if (Hi == Memory)
1082 Lo = Memory;
1083 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001084 return;
1085 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001086
Chris Lattner1090a9b2010-06-28 21:43:59 +00001087 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattnerea044322010-07-29 02:01:43 +00001088 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001089
1090 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1091 // than two eightbytes, ..., it has class MEMORY.
1092 if (Size > 128)
1093 return;
1094
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001095 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1096 // copy constructor or a non-trivial destructor, it is passed by invisible
1097 // reference.
1098 if (hasNonTrivialDestructorOrCopyConstructor(RT))
1099 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001100
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001101 const RecordDecl *RD = RT->getDecl();
1102
1103 // Assume variable sized types are passed in memory.
1104 if (RD->hasFlexibleArrayMember())
1105 return;
1106
Chris Lattnerea044322010-07-29 02:01:43 +00001107 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001108
1109 // Reset Lo class, this will be recomputed.
1110 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001111
1112 // If this is a C++ record, classify the bases first.
1113 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1114 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1115 e = CXXRD->bases_end(); i != e; ++i) {
1116 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1117 "Unexpected base class!");
1118 const CXXRecordDecl *Base =
1119 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1120
1121 // Classify this field.
1122 //
1123 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1124 // single eightbyte, each is classified separately. Each eightbyte gets
1125 // initialized to class NO_CLASS.
1126 Class FieldLo, FieldHi;
1127 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
Chris Lattner9c254f02010-06-29 06:01:59 +00001128 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbarce9f4232009-11-22 23:01:23 +00001129 Lo = merge(Lo, FieldLo);
1130 Hi = merge(Hi, FieldHi);
1131 if (Lo == Memory || Hi == Memory)
1132 break;
1133 }
1134 }
1135
1136 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001137 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1139 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001140 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1141 bool BitField = i->isBitField();
1142
1143 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1144 // fields, it has class MEMORY.
1145 //
1146 // Note, skip this test for bit-fields, see below.
Chris Lattnerea044322010-07-29 02:01:43 +00001147 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001148 Lo = Memory;
1149 return;
1150 }
1151
1152 // Classify this field.
1153 //
1154 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1155 // exceeds a single eightbyte, each is classified
1156 // separately. Each eightbyte gets initialized to class
1157 // NO_CLASS.
1158 Class FieldLo, FieldHi;
1159
1160 // Bit-fields require special handling, they do not force the
1161 // structure to be passed in memory even if unaligned, and
1162 // therefore they can straddle an eightbyte.
1163 if (BitField) {
1164 // Ignore padding bit-fields.
1165 if (i->isUnnamedBitfield())
1166 continue;
1167
1168 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Chris Lattnerea044322010-07-29 02:01:43 +00001169 uint64_t Size =
1170 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001171
1172 uint64_t EB_Lo = Offset / 64;
1173 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1174 FieldLo = FieldHi = NoClass;
1175 if (EB_Lo) {
1176 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1177 FieldLo = NoClass;
1178 FieldHi = Integer;
1179 } else {
1180 FieldLo = Integer;
1181 FieldHi = EB_Hi ? Integer : NoClass;
1182 }
1183 } else
Chris Lattner9c254f02010-06-29 06:01:59 +00001184 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001185 Lo = merge(Lo, FieldLo);
1186 Hi = merge(Hi, FieldHi);
1187 if (Lo == Memory || Hi == Memory)
1188 break;
1189 }
1190
1191 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1192 //
1193 // (a) If one of the classes is MEMORY, the whole argument is
1194 // passed in memory.
1195 //
1196 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1197
1198 // The first of these conditions is guaranteed by how we implement
1199 // the merge (just bail).
1200 //
1201 // The second condition occurs in the case of unions; for example
1202 // union { _Complex double; unsigned; }.
1203 if (Hi == Memory)
1204 Lo = Memory;
1205 if (Hi == SSEUp && Lo != SSE)
1206 Hi = SSE;
1207 }
1208}
1209
Chris Lattner9c254f02010-06-29 06:01:59 +00001210ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001211 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1212 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001213 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001214 // Treat an enum type as its underlying type.
1215 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1216 Ty = EnumTy->getDecl()->getIntegerType();
1217
1218 return (Ty->isPromotableIntegerType() ?
1219 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1220 }
1221
1222 return ABIArgInfo::getIndirect(0);
1223}
1224
Chris Lattner9c254f02010-06-29 06:01:59 +00001225ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001226 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1227 // place naturally.
John McCalld608cdb2010-08-22 10:59:02 +00001228 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001229 // Treat an enum type as its underlying type.
1230 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1231 Ty = EnumTy->getDecl()->getIntegerType();
1232
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001233 return (Ty->isPromotableIntegerType() ?
1234 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001235 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001236
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001237 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1238 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001239
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001240 // Compute the byval alignment. We trust the back-end to honor the
1241 // minimum ABI alignment for byval, to make cleaner IR.
1242 const unsigned MinABIAlign = 8;
Chris Lattnerea044322010-07-29 02:01:43 +00001243 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar46c54fb2010-04-21 19:49:55 +00001244 if (Align > MinABIAlign)
1245 return ABIArgInfo::getIndirect(Align);
1246 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001247}
1248
Chris Lattner0f408f52010-07-29 04:56:46 +00001249/// Get16ByteVectorType - The ABI specifies that a value should be passed in an
1250/// full vector XMM register. Pick an LLVM IR type that will be passed as a
1251/// vector register.
1252const llvm::Type *X86_64ABIInfo::Get16ByteVectorType(QualType Ty) const {
Chris Lattner15842bd2010-07-29 05:02:29 +00001253 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001254
Chris Lattner15842bd2010-07-29 05:02:29 +00001255 // Wrapper structs that just contain vectors are passed just like vectors,
1256 // strip them off if present.
1257 const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1258 while (STy && STy->getNumElements() == 1) {
1259 IRType = STy->getElementType(0);
1260 STy = dyn_cast<llvm::StructType>(IRType);
1261 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001262
Chris Lattner0f408f52010-07-29 04:56:46 +00001263 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner15842bd2010-07-29 05:02:29 +00001264 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner0f408f52010-07-29 04:56:46 +00001265 const llvm::Type *EltTy = VT->getElementType();
1266 if (VT->getBitWidth() == 128 &&
1267 (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1268 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1269 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1270 EltTy->isIntegerTy(128)))
1271 return VT;
1272 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001273
Chris Lattner0f408f52010-07-29 04:56:46 +00001274 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1275}
1276
Chris Lattnere2962be2010-07-29 07:30:00 +00001277/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1278/// is known to either be off the end of the specified type or being in
1279/// alignment padding. The user type specified is known to be at most 128 bits
1280/// in size, and have passed through X86_64ABIInfo::classify with a successful
1281/// classification that put one of the two halves in the INTEGER class.
1282///
1283/// It is conservatively correct to return false.
1284static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1285 unsigned EndBit, ASTContext &Context) {
1286 // If the bytes being queried are off the end of the type, there is no user
1287 // data hiding here. This handles analysis of builtins, vectors and other
1288 // types that don't contain interesting padding.
1289 unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1290 if (TySize <= StartBit)
1291 return true;
1292
Chris Lattner021c3a32010-07-29 07:43:55 +00001293 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1294 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1295 unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1296
1297 // Check each element to see if the element overlaps with the queried range.
1298 for (unsigned i = 0; i != NumElts; ++i) {
1299 // If the element is after the span we care about, then we're done..
1300 unsigned EltOffset = i*EltSize;
1301 if (EltOffset >= EndBit) break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001302
Chris Lattner021c3a32010-07-29 07:43:55 +00001303 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1304 if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1305 EndBit-EltOffset, Context))
1306 return false;
1307 }
1308 // If it overlaps no elements, then it is safe to process as padding.
1309 return true;
1310 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001311
Chris Lattnere2962be2010-07-29 07:30:00 +00001312 if (const RecordType *RT = Ty->getAs<RecordType>()) {
1313 const RecordDecl *RD = RT->getDecl();
1314 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001315
Chris Lattnere2962be2010-07-29 07:30:00 +00001316 // If this is a C++ record, check the bases first.
1317 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1318 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1319 e = CXXRD->bases_end(); i != e; ++i) {
1320 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1321 "Unexpected base class!");
1322 const CXXRecordDecl *Base =
1323 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001324
Chris Lattnere2962be2010-07-29 07:30:00 +00001325 // If the base is after the span we care about, ignore it.
1326 unsigned BaseOffset = (unsigned)Layout.getBaseClassOffset(Base);
1327 if (BaseOffset >= EndBit) continue;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001328
Chris Lattnere2962be2010-07-29 07:30:00 +00001329 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1330 if (!BitsContainNoUserData(i->getType(), BaseStart,
1331 EndBit-BaseOffset, Context))
1332 return false;
1333 }
1334 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001335
Chris Lattnere2962be2010-07-29 07:30:00 +00001336 // Verify that no field has data that overlaps the region of interest. Yes
1337 // this could be sped up a lot by being smarter about queried fields,
1338 // however we're only looking at structs up to 16 bytes, so we don't care
1339 // much.
1340 unsigned idx = 0;
1341 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1342 i != e; ++i, ++idx) {
1343 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001344
Chris Lattnere2962be2010-07-29 07:30:00 +00001345 // If we found a field after the region we care about, then we're done.
1346 if (FieldOffset >= EndBit) break;
1347
1348 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1349 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1350 Context))
1351 return false;
1352 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001353
Chris Lattnere2962be2010-07-29 07:30:00 +00001354 // If nothing in this record overlapped the area of interest, then we're
1355 // clean.
1356 return true;
1357 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001358
Chris Lattnere2962be2010-07-29 07:30:00 +00001359 return false;
1360}
1361
Chris Lattner0b362002010-07-29 18:39:32 +00001362/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1363/// float member at the specified offset. For example, {int,{float}} has a
1364/// float at offset 4. It is conservatively correct for this routine to return
1365/// false.
1366static bool ContainsFloatAtOffset(const llvm::Type *IRType, unsigned IROffset,
1367 const llvm::TargetData &TD) {
1368 // Base case if we find a float.
1369 if (IROffset == 0 && IRType->isFloatTy())
1370 return true;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001371
Chris Lattner0b362002010-07-29 18:39:32 +00001372 // If this is a struct, recurse into the field at the specified offset.
1373 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1374 const llvm::StructLayout *SL = TD.getStructLayout(STy);
1375 unsigned Elt = SL->getElementContainingOffset(IROffset);
1376 IROffset -= SL->getElementOffset(Elt);
1377 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1378 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001379
Chris Lattner0b362002010-07-29 18:39:32 +00001380 // If this is an array, recurse into the field at the specified offset.
1381 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1382 const llvm::Type *EltTy = ATy->getElementType();
1383 unsigned EltSize = TD.getTypeAllocSize(EltTy);
1384 IROffset -= IROffset/EltSize*EltSize;
1385 return ContainsFloatAtOffset(EltTy, IROffset, TD);
1386 }
1387
1388 return false;
1389}
1390
Chris Lattnerf47c9442010-07-29 18:13:09 +00001391
1392/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1393/// low 8 bytes of an XMM register, corresponding to the SSE class.
1394const llvm::Type *X86_64ABIInfo::
1395GetSSETypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1396 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnercba8d312010-07-29 18:19:50 +00001397 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattnerf47c9442010-07-29 18:13:09 +00001398 // pass as float if the last 4 bytes is just padding. This happens for
1399 // structs that contain 3 floats.
1400 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1401 SourceOffset*8+64, getContext()))
1402 return llvm::Type::getFloatTy(getVMContext());
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001403
Chris Lattner0b362002010-07-29 18:39:32 +00001404 // We want to pass as <2 x float> if the LLVM IR type contains a float at
1405 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
1406 // case.
1407 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
Chris Lattner22fd4ba2010-08-25 23:39:14 +00001408 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1409 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001410
Chris Lattnerf47c9442010-07-29 18:13:09 +00001411 return llvm::Type::getDoubleTy(getVMContext());
1412}
1413
1414
Chris Lattner0d2656d2010-07-29 17:40:35 +00001415/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1416/// an 8-byte GPR. This means that we either have a scalar or we are talking
1417/// about the high or low part of an up-to-16-byte struct. This routine picks
1418/// the best LLVM IR type to represent this, which may be i64 or may be anything
Chris Lattner49382de2010-07-28 22:44:07 +00001419/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1420/// etc).
1421///
1422/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1423/// the source type. IROffset is an offset in bytes into the LLVM IR type that
1424/// the 8-byte value references. PrefType may be null.
1425///
1426/// SourceTy is the source level type for the entire argument. SourceOffset is
1427/// an offset into this that we're processing (which is always either 0 or 8).
1428///
Chris Lattner44f0fd22010-07-29 02:20:19 +00001429const llvm::Type *X86_64ABIInfo::
Chris Lattner0d2656d2010-07-29 17:40:35 +00001430GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1431 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnere2962be2010-07-29 07:30:00 +00001432 // If we're dealing with an un-offset LLVM IR type, then it means that we're
1433 // returning an 8-byte unit starting with it. See if we can safely use it.
1434 if (IROffset == 0) {
1435 // Pointers and int64's always fill the 8-byte unit.
1436 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1437 return IRType;
Chris Lattner49382de2010-07-28 22:44:07 +00001438
Chris Lattnere2962be2010-07-29 07:30:00 +00001439 // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1440 // goodness in the source type is just tail padding. This is allowed to
1441 // kick in for struct {double,int} on the int, but not on
1442 // struct{double,int,int} because we wouldn't return the second int. We
1443 // have to do this analysis on the source type because we can't depend on
1444 // unions being lowered a specific way etc.
1445 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1446 IRType->isIntegerTy(32)) {
1447 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001448
Chris Lattnere2962be2010-07-29 07:30:00 +00001449 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1450 SourceOffset*8+64, getContext()))
1451 return IRType;
1452 }
1453 }
Chris Lattner49382de2010-07-28 22:44:07 +00001454
Chris Lattnerfe12d1e2010-07-29 04:51:12 +00001455 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattner49382de2010-07-28 22:44:07 +00001456 // If this is a struct, recurse into the field at the specified offset.
Chris Lattner44f0fd22010-07-29 02:20:19 +00001457 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattner49382de2010-07-28 22:44:07 +00001458 if (IROffset < SL->getSizeInBytes()) {
1459 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1460 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001461
Chris Lattner0d2656d2010-07-29 17:40:35 +00001462 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1463 SourceTy, SourceOffset);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001464 }
Chris Lattner49382de2010-07-28 22:44:07 +00001465 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001466
Chris Lattner021c3a32010-07-29 07:43:55 +00001467 if (const llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1468 const llvm::Type *EltTy = ATy->getElementType();
1469 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1470 unsigned EltOffset = IROffset/EltSize*EltSize;
Chris Lattner0d2656d2010-07-29 17:40:35 +00001471 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1472 SourceOffset);
Chris Lattner021c3a32010-07-29 07:43:55 +00001473 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001474
Chris Lattner49382de2010-07-28 22:44:07 +00001475 // Okay, we don't have any better idea of what to pass, so we pass this in an
1476 // integer register that isn't too big to fit the rest of the struct.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001477 unsigned TySizeInBytes =
1478 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattner49382de2010-07-28 22:44:07 +00001479
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001480 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001481
Chris Lattner49382de2010-07-28 22:44:07 +00001482 // It is always safe to classify this as an integer type up to i64 that
1483 // isn't larger than the structure.
Chris Lattner9e45a3d2010-07-29 17:34:39 +00001484 return llvm::IntegerType::get(getVMContext(),
1485 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner9c254f02010-06-29 06:01:59 +00001486}
1487
Chris Lattner66e7b682010-09-01 00:50:20 +00001488
1489/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
1490/// be used as elements of a two register pair to pass or return, return a
1491/// first class aggregate to represent them. For example, if the low part of
1492/// a by-value argument should be passed as i32* and the high part as float,
1493/// return {i32*, float}.
1494static const llvm::Type *
1495GetX86_64ByValArgumentPair(const llvm::Type *Lo, const llvm::Type *Hi,
1496 const llvm::TargetData &TD) {
1497 // In order to correctly satisfy the ABI, we need to the high part to start
1498 // at offset 8. If the high and low parts we inferred are both 4-byte types
1499 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
1500 // the second element at offset 8. Check for this:
1501 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
1502 unsigned HiAlign = TD.getABITypeAlignment(Hi);
1503 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
1504 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
1505
1506 // To handle this, we have to increase the size of the low part so that the
1507 // second element will start at an 8 byte offset. We can't increase the size
1508 // of the second element because it might make us access off the end of the
1509 // struct.
1510 if (HiStart != 8) {
1511 // There are only two sorts of types the ABI generation code can produce for
1512 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
1513 // Promote these to a larger type.
1514 if (Lo->isFloatTy())
1515 Lo = llvm::Type::getDoubleTy(Lo->getContext());
1516 else {
1517 assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
1518 Lo = llvm::Type::getInt64Ty(Lo->getContext());
1519 }
1520 }
1521
1522 const llvm::StructType *Result =
1523 llvm::StructType::get(Lo->getContext(), Lo, Hi, NULL);
1524
1525
1526 // Verify that the second element is at an 8-byte offset.
1527 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
1528 "Invalid x86-64 argument pair!");
1529 return Result;
1530}
1531
Chris Lattner519f68c2010-07-28 23:06:14 +00001532ABIArgInfo X86_64ABIInfo::
Chris Lattnera3c109b2010-07-29 02:16:43 +00001533classifyReturnType(QualType RetTy) const {
Chris Lattner519f68c2010-07-28 23:06:14 +00001534 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1535 // classification algorithm.
1536 X86_64ABIInfo::Class Lo, Hi;
1537 classify(RetTy, 0, Lo, Hi);
1538
1539 // Check some invariants.
1540 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Chris Lattner519f68c2010-07-28 23:06:14 +00001541 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1542
1543 const llvm::Type *ResType = 0;
1544 switch (Lo) {
1545 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001546 if (Hi == NoClass)
1547 return ABIArgInfo::getIgnore();
1548 // If the low part is just padding, it takes no register, leave ResType
1549 // null.
1550 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1551 "Unknown missing lo part");
1552 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001553
1554 case SSEUp:
1555 case X87Up:
1556 assert(0 && "Invalid classification for lo word.");
1557
1558 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1559 // hidden argument.
1560 case Memory:
1561 return getIndirectReturnResult(RetTy);
1562
1563 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1564 // available register of the sequence %rax, %rdx is used.
1565 case Integer:
Chris Lattner0d2656d2010-07-29 17:40:35 +00001566 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1567 RetTy, 0);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001568
Chris Lattnereb518b42010-07-29 21:42:50 +00001569 // If we have a sign or zero extended integer, make sure to return Extend
1570 // so that the parameter gets the right LLVM IR attributes.
1571 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1572 // Treat an enum type as its underlying type.
1573 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1574 RetTy = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001575
Chris Lattnereb518b42010-07-29 21:42:50 +00001576 if (RetTy->isIntegralOrEnumerationType() &&
1577 RetTy->isPromotableIntegerType())
1578 return ABIArgInfo::getExtend();
1579 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001580 break;
1581
1582 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1583 // available SSE register of the sequence %xmm0, %xmm1 is used.
1584 case SSE:
Chris Lattnerf47c9442010-07-29 18:13:09 +00001585 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattner0b30c672010-07-28 23:12:33 +00001586 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001587
1588 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1589 // returned on the X87 stack in %st0 as 80-bit x87 number.
1590 case X87:
Chris Lattnerea044322010-07-29 02:01:43 +00001591 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattner0b30c672010-07-28 23:12:33 +00001592 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001593
1594 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1595 // part of the value is returned in %st0 and the imaginary part in
1596 // %st1.
1597 case ComplexX87:
1598 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnera3c109b2010-07-29 02:16:43 +00001599 ResType = llvm::StructType::get(getVMContext(),
Chris Lattnerea044322010-07-29 02:01:43 +00001600 llvm::Type::getX86_FP80Ty(getVMContext()),
1601 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner519f68c2010-07-28 23:06:14 +00001602 NULL);
1603 break;
1604 }
1605
Chris Lattner3db4dde2010-09-01 00:20:33 +00001606 const llvm::Type *HighPart = 0;
Chris Lattner519f68c2010-07-28 23:06:14 +00001607 switch (Hi) {
1608 // Memory was handled previously and X87 should
1609 // never occur as a hi class.
1610 case Memory:
1611 case X87:
1612 assert(0 && "Invalid classification for hi word.");
1613
1614 case ComplexX87: // Previously handled.
Chris Lattner0b30c672010-07-28 23:12:33 +00001615 case NoClass:
1616 break;
Chris Lattner519f68c2010-07-28 23:06:14 +00001617
Chris Lattner3db4dde2010-09-01 00:20:33 +00001618 case Integer:
1619 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1620 8, RetTy, 8);
1621 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1622 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001623 break;
Chris Lattner3db4dde2010-09-01 00:20:33 +00001624 case SSE:
1625 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 8, RetTy, 8);
1626 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1627 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner519f68c2010-07-28 23:06:14 +00001628 break;
1629
1630 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1631 // is passed in the upper half of the last used SSE register.
1632 //
1633 // SSEUP should always be preceeded by SSE, just widen.
1634 case SSEUp:
1635 assert(Lo == SSE && "Unexpected SSEUp classification.");
Chris Lattner0f408f52010-07-29 04:56:46 +00001636 ResType = Get16ByteVectorType(RetTy);
Chris Lattner519f68c2010-07-28 23:06:14 +00001637 break;
1638
1639 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1640 // returned together with the previous X87 value in %st0.
1641 case X87Up:
1642 // If X87Up is preceeded by X87, we don't need to do
1643 // anything. However, in some cases with unions it may not be
1644 // preceeded by X87. In such situations we follow gcc and pass the
1645 // extra bits in an SSE reg.
Chris Lattner603519d2010-07-29 17:49:08 +00001646 if (Lo != X87) {
Chris Lattner3db4dde2010-09-01 00:20:33 +00001647 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy),
1648 8, RetTy, 8);
1649 if (Lo == NoClass) // Return HighPart at offset 8 in memory.
1650 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner603519d2010-07-29 17:49:08 +00001651 }
Chris Lattner519f68c2010-07-28 23:06:14 +00001652 break;
1653 }
Chris Lattner3db4dde2010-09-01 00:20:33 +00001654
1655 // If a high part was specified, merge it together with the low part. It is
Chris Lattner645406a2010-09-01 00:24:35 +00001656 // known to pass in the high eightbyte of the result. We do this by forming a
1657 // first class struct aggregate with the high and low part: {low, high}
Chris Lattner66e7b682010-09-01 00:50:20 +00001658 if (HighPart)
1659 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner519f68c2010-07-28 23:06:14 +00001660
Chris Lattnereb518b42010-07-29 21:42:50 +00001661 return ABIArgInfo::getDirect(ResType);
Chris Lattner519f68c2010-07-28 23:06:14 +00001662}
1663
Chris Lattnera3c109b2010-07-29 02:16:43 +00001664ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner5868ca22010-07-29 04:41:05 +00001665 unsigned &neededSSE) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001666 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner9c254f02010-06-29 06:01:59 +00001667 classify(Ty, 0, Lo, Hi);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001668
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001669 // Check some invariants.
1670 // FIXME: Enforce these by construction.
1671 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001672 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1673
1674 neededInt = 0;
1675 neededSSE = 0;
1676 const llvm::Type *ResType = 0;
1677 switch (Lo) {
1678 case NoClass:
Chris Lattner117e3f42010-07-30 04:02:24 +00001679 if (Hi == NoClass)
1680 return ABIArgInfo::getIgnore();
1681 // If the low part is just padding, it takes no register, leave ResType
1682 // null.
1683 assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1684 "Unknown missing lo part");
1685 break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001686
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001687 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1688 // on the stack.
1689 case Memory:
1690
1691 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1692 // COMPLEX_X87, it is passed in memory.
1693 case X87:
1694 case ComplexX87:
Chris Lattner9c254f02010-06-29 06:01:59 +00001695 return getIndirectResult(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001696
1697 case SSEUp:
1698 case X87Up:
1699 assert(0 && "Invalid classification for lo word.");
1700
1701 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1702 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1703 // and %r9 is used.
1704 case Integer:
Chris Lattner9c254f02010-06-29 06:01:59 +00001705 ++neededInt;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001706
Chris Lattner49382de2010-07-28 22:44:07 +00001707 // Pick an 8-byte type based on the preferred type.
Chris Lattner0d2656d2010-07-29 17:40:35 +00001708 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattnereb518b42010-07-29 21:42:50 +00001709
1710 // If we have a sign or zero extended integer, make sure to return Extend
1711 // so that the parameter gets the right LLVM IR attributes.
1712 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1713 // Treat an enum type as its underlying type.
1714 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1715 Ty = EnumTy->getDecl()->getIntegerType();
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001716
Chris Lattnereb518b42010-07-29 21:42:50 +00001717 if (Ty->isIntegralOrEnumerationType() &&
1718 Ty->isPromotableIntegerType())
1719 return ABIArgInfo::getExtend();
1720 }
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001721
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001722 break;
1723
1724 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1725 // available SSE register is used, the registers are taken in the
1726 // order from %xmm0 to %xmm7.
1727 case SSE:
1728 ++neededSSE;
Chris Lattnerf47c9442010-07-29 18:13:09 +00001729 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001730 break;
1731 }
1732
Chris Lattner645406a2010-09-01 00:24:35 +00001733 const llvm::Type *HighPart = 0;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001734 switch (Hi) {
1735 // Memory was handled previously, ComplexX87 and X87 should
1736 // never occur as hi classes, and X87Up must be preceed by X87,
1737 // which is passed in memory.
1738 case Memory:
1739 case X87:
1740 case ComplexX87:
1741 assert(0 && "Invalid classification for hi word.");
1742 break;
1743
1744 case NoClass: break;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001745
Chris Lattner645406a2010-09-01 00:24:35 +00001746 case Integer:
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001747 ++neededInt;
Chris Lattner49382de2010-07-28 22:44:07 +00001748 // Pick an 8-byte type based on the preferred type.
Chris Lattner645406a2010-09-01 00:24:35 +00001749 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001750
Chris Lattner645406a2010-09-01 00:24:35 +00001751 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1752 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001753 break;
1754
1755 // X87Up generally doesn't occur here (long double is passed in
1756 // memory), except in situations involving unions.
1757 case X87Up:
Chris Lattner645406a2010-09-01 00:24:35 +00001758 case SSE:
1759 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001760
Chris Lattner645406a2010-09-01 00:24:35 +00001761 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1762 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner117e3f42010-07-30 04:02:24 +00001763
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001764 ++neededSSE;
1765 break;
1766
1767 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1768 // eightbyte is passed in the upper half of the last used SSE
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001769 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001770 case SSEUp:
Chris Lattnerab5722e2010-07-28 23:47:21 +00001771 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner0f408f52010-07-29 04:56:46 +00001772 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001773 break;
1774 }
1775
Chris Lattner645406a2010-09-01 00:24:35 +00001776 // If a high part was specified, merge it together with the low part. It is
1777 // known to pass in the high eightbyte of the result. We do this by forming a
1778 // first class struct aggregate with the high and low part: {low, high}
1779 if (HighPart)
Chris Lattner66e7b682010-09-01 00:50:20 +00001780 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner645406a2010-09-01 00:24:35 +00001781
Chris Lattnereb518b42010-07-29 21:42:50 +00001782 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001783}
1784
Chris Lattneree5dcd02010-07-29 02:31:05 +00001785void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001786
Chris Lattnera3c109b2010-07-29 02:16:43 +00001787 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001788
1789 // Keep track of the number of assigned registers.
1790 unsigned freeIntRegs = 6, freeSSERegs = 8;
1791
1792 // If the return value is indirect, then the hidden argument is consuming one
1793 // integer register.
1794 if (FI.getReturnInfo().isIndirect())
1795 --freeIntRegs;
1796
1797 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1798 // get assigned (in left-to-right order) for passing as follows...
1799 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1800 it != ie; ++it) {
1801 unsigned neededInt, neededSSE;
Chris Lattner5868ca22010-07-29 04:41:05 +00001802 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001803
1804 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1805 // eightbyte of an argument, the whole argument is passed on the
1806 // stack. If registers have already been assigned for some
1807 // eightbytes of such an argument, the assignments get reverted.
1808 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1809 freeIntRegs -= neededInt;
1810 freeSSERegs -= neededSSE;
1811 } else {
Chris Lattner9c254f02010-06-29 06:01:59 +00001812 it->info = getIndirectResult(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001813 }
1814 }
1815}
1816
1817static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1818 QualType Ty,
1819 CodeGenFunction &CGF) {
1820 llvm::Value *overflow_arg_area_p =
1821 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1822 llvm::Value *overflow_arg_area =
1823 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1824
1825 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1826 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1827 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1828 if (Align > 8) {
1829 // Note that we follow the ABI & gcc here, even though the type
1830 // could in theory have an alignment greater than 16. This case
1831 // shouldn't ever matter in practice.
1832
1833 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001834 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001835 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001836 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1837 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Chris Lattner77b89b82010-06-27 07:15:29 +00001838 CGF.Int64Ty);
1839 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001840 overflow_arg_area =
1841 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1842 overflow_arg_area->getType(),
1843 "overflow_arg_area.align");
1844 }
1845
1846 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1847 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1848 llvm::Value *Res =
1849 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001850 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001851
1852 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1853 // l->overflow_arg_area + sizeof(type).
1854 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1855 // an 8 byte boundary.
1856
1857 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001858 llvm::Value *Offset =
Chris Lattner77b89b82010-06-27 07:15:29 +00001859 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001860 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1861 "overflow_arg_area.next");
1862 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1863
1864 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1865 return Res;
1866}
1867
1868llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1869 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001870 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001872 // Assume that va_list type is correct; should be pointer to LLVM type:
1873 // struct {
1874 // i32 gp_offset;
1875 // i32 fp_offset;
1876 // i8* overflow_arg_area;
1877 // i8* reg_save_area;
1878 // };
1879 unsigned neededInt, neededSSE;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00001880
Chris Lattnera14db752010-03-11 18:19:55 +00001881 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner5868ca22010-07-29 04:41:05 +00001882 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001883
1884 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1885 // in the registers. If not go to step 7.
1886 if (!neededInt && !neededSSE)
1887 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1888
1889 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1890 // general purpose registers needed to pass type and num_fp to hold
1891 // the number of floating point registers needed.
1892
1893 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1894 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1895 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1896 //
1897 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1898 // register save space).
1899
1900 llvm::Value *InRegs = 0;
1901 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1902 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1903 if (neededInt) {
1904 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1905 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
Chris Lattner1090a9b2010-06-28 21:43:59 +00001906 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
1907 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001908 }
1909
1910 if (neededSSE) {
1911 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1912 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1913 llvm::Value *FitsInFP =
Chris Lattner1090a9b2010-06-28 21:43:59 +00001914 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
1915 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001916 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1917 }
1918
1919 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1920 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1921 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1922 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1923
1924 // Emit code to load the value if it was passed in registers.
1925
1926 CGF.EmitBlock(InRegBlock);
1927
1928 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1929 // an offset of l->gp_offset and/or l->fp_offset. This may require
1930 // copying to a temporary location in case the parameter is passed
1931 // in different register classes or requires an alignment greater
1932 // than 8 for general purpose registers and 16 for XMM registers.
1933 //
1934 // FIXME: This really results in shameful code when we end up needing to
1935 // collect arguments from different places; often what should result in a
1936 // simple assembling of a structure from scattered addresses has many more
1937 // loads than necessary. Can we clean this up?
1938 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1939 llvm::Value *RegAddr =
1940 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1941 "reg_save_area");
1942 if (neededInt && neededSSE) {
1943 // FIXME: Cleanup.
Chris Lattner800588f2010-07-29 06:26:06 +00001944 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001945 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1946 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1947 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1948 const llvm::Type *TyLo = ST->getElementType(0);
1949 const llvm::Type *TyHi = ST->getElementType(1);
Chris Lattnera8b7a7d2010-08-26 06:28:35 +00001950 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001951 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001952 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1953 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001954 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1955 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001956 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1957 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001958 llvm::Value *V =
1959 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1960 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1961 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1962 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1963
Owen Andersona1cf15f2009-07-14 23:10:40 +00001964 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001965 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001966 } else if (neededInt) {
1967 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1968 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001969 llvm::PointerType::getUnqual(LTy));
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001970 } else if (neededSSE == 1) {
1971 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1972 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1973 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001974 } else {
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001975 assert(neededSSE == 2 && "Invalid number of needed registers!");
1976 // SSE registers are spaced 16 bytes apart in the register save
1977 // area, we need to collect the two eightbytes together.
1978 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Chris Lattner1090a9b2010-06-28 21:43:59 +00001979 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00001980 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
1981 const llvm::Type *DblPtrTy =
1982 llvm::PointerType::getUnqual(DoubleTy);
1983 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1984 DoubleTy, NULL);
1985 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1986 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1987 DblPtrTy));
1988 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1989 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1990 DblPtrTy));
1991 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1992 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1993 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001994 }
1995
1996 // AMD64-ABI 3.5.7p5: Step 5. Set:
1997 // l->gp_offset = l->gp_offset + num_gp * 8
1998 // l->fp_offset = l->fp_offset + num_fp * 16.
1999 if (neededInt) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002000 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002001 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2002 gp_offset_p);
2003 }
2004 if (neededSSE) {
Chris Lattner77b89b82010-06-27 07:15:29 +00002005 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002006 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2007 fp_offset_p);
2008 }
2009 CGF.EmitBranch(ContBlock);
2010
2011 // Emit code to load the value if it was passed in memory.
2012
2013 CGF.EmitBlock(InMemBlock);
2014 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2015
2016 // Return the appropriate result.
2017
2018 CGF.EmitBlock(ContBlock);
2019 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
2020 "vaarg.addr");
2021 ResAddr->reserveOperandSpace(2);
2022 ResAddr->addIncoming(RegAddr, InRegBlock);
2023 ResAddr->addIncoming(MemAddr, InMemBlock);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002024 return ResAddr;
2025}
2026
Chris Lattnerf13721d2010-08-31 16:44:54 +00002027llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2028 CodeGenFunction &CGF) const {
2029 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2030 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002031
Chris Lattnerf13721d2010-08-31 16:44:54 +00002032 CGBuilderTy &Builder = CGF.Builder;
2033 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2034 "ap");
2035 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2036 llvm::Type *PTy =
2037 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2038 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2039
2040 uint64_t Offset =
2041 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2042 llvm::Value *NextAddr =
2043 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2044 "ap.next");
2045 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2046
2047 return AddrTyped;
2048}
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002049
2050//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002051// PIC16 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002052//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002053
2054namespace {
2055
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002056class PIC16ABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002057public:
2058 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002059
Chris Lattnera3c109b2010-07-29 02:16:43 +00002060 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002061
Chris Lattnera3c109b2010-07-29 02:16:43 +00002062 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002063
Chris Lattneree5dcd02010-07-29 02:31:05 +00002064 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002065 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002066 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2067 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002068 it->info = classifyArgumentType(it->type);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002069 }
2070
2071 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2072 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002073};
2074
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002075class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
2076public:
Chris Lattnerea044322010-07-29 02:01:43 +00002077 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
2078 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002079};
2080
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002081}
2082
Chris Lattnera3c109b2010-07-29 02:16:43 +00002083ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002084 if (RetTy->isVoidType()) {
2085 return ABIArgInfo::getIgnore();
2086 } else {
2087 return ABIArgInfo::getDirect();
2088 }
2089}
2090
Chris Lattnera3c109b2010-07-29 02:16:43 +00002091ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002092 return ABIArgInfo::getDirect();
2093}
2094
2095llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002096 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00002097 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00002098 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
2099
2100 CGBuilderTy &Builder = CGF.Builder;
2101 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2102 "ap");
2103 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2104 llvm::Type *PTy =
2105 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2106 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2107
2108 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
2109
2110 llvm::Value *NextAddr =
2111 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
2112 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
2113 "ap.next");
2114 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2115
2116 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002117}
2118
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00002119
John McCallec853ba2010-03-11 00:10:12 +00002120// PowerPC-32
2121
2122namespace {
2123class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2124public:
Chris Lattnerea044322010-07-29 02:01:43 +00002125 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002126
John McCallec853ba2010-03-11 00:10:12 +00002127 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2128 // This is recovered from gcc output.
2129 return 1; // r1 is the dedicated stack pointer
2130 }
2131
2132 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002133 llvm::Value *Address) const;
John McCallec853ba2010-03-11 00:10:12 +00002134};
2135
2136}
2137
2138bool
2139PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2140 llvm::Value *Address) const {
2141 // This is calculated from the LLVM and GCC tables and verified
2142 // against gcc output. AFAIK all ABIs use the same encoding.
2143
2144 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2145 llvm::LLVMContext &Context = CGF.getLLVMContext();
2146
2147 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2148 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2149 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2150 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2151
2152 // 0-31: r0-31, the 4-byte general-purpose registers
John McCallaeeb7012010-05-27 06:19:26 +00002153 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallec853ba2010-03-11 00:10:12 +00002154
2155 // 32-63: fp0-31, the 8-byte floating-point registers
John McCallaeeb7012010-05-27 06:19:26 +00002156 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallec853ba2010-03-11 00:10:12 +00002157
2158 // 64-76 are various 4-byte special-purpose registers:
2159 // 64: mq
2160 // 65: lr
2161 // 66: ctr
2162 // 67: ap
2163 // 68-75 cr0-7
2164 // 76: xer
John McCallaeeb7012010-05-27 06:19:26 +00002165 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallec853ba2010-03-11 00:10:12 +00002166
2167 // 77-108: v0-31, the 16-byte vector registers
John McCallaeeb7012010-05-27 06:19:26 +00002168 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallec853ba2010-03-11 00:10:12 +00002169
2170 // 109: vrsave
2171 // 110: vscr
2172 // 111: spe_acc
2173 // 112: spefscr
2174 // 113: sfp
John McCallaeeb7012010-05-27 06:19:26 +00002175 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallec853ba2010-03-11 00:10:12 +00002176
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002177 return false;
John McCallec853ba2010-03-11 00:10:12 +00002178}
2179
2180
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002181//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002182// ARM ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002183//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002184
2185namespace {
2186
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002187class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002188public:
2189 enum ABIKind {
2190 APCS = 0,
2191 AAPCS = 1,
2192 AAPCS_VFP
2193 };
2194
2195private:
2196 ABIKind Kind;
2197
2198public:
Chris Lattnerea044322010-07-29 02:01:43 +00002199 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002200
2201private:
2202 ABIKind getABIKind() const { return Kind; }
2203
Chris Lattnera3c109b2010-07-29 02:16:43 +00002204 ABIArgInfo classifyReturnType(QualType RetTy) const;
2205 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002206
Chris Lattneree5dcd02010-07-29 02:31:05 +00002207 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002208
2209 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2210 CodeGenFunction &CGF) const;
2211};
2212
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002213class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2214public:
Chris Lattnerea044322010-07-29 02:01:43 +00002215 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2216 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCall6374c332010-03-06 00:35:14 +00002217
2218 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2219 return 13;
2220 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002221};
2222
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002223}
2224
Chris Lattneree5dcd02010-07-29 02:31:05 +00002225void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002226 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002227 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattnera3c109b2010-07-29 02:16:43 +00002228 it != ie; ++it)
2229 it->info = classifyArgumentType(it->type);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002230
Chris Lattnera3c109b2010-07-29 02:16:43 +00002231 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindola25117ab2010-06-16 16:13:39 +00002232 llvm::CallingConv::ID DefaultCC;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002233 if (Triple.getEnvironmentName() == "gnueabi" ||
2234 Triple.getEnvironmentName() == "eabi")
Rafael Espindola25117ab2010-06-16 16:13:39 +00002235 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola1ed1a592010-06-16 19:01:17 +00002236 else
2237 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindola25117ab2010-06-16 16:13:39 +00002238
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002239 switch (getABIKind()) {
2240 case APCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002241 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2242 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002243 break;
2244
2245 case AAPCS:
Rafael Espindola25117ab2010-06-16 16:13:39 +00002246 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2247 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002248 break;
2249
2250 case AAPCS_VFP:
2251 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2252 break;
2253 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002254}
2255
Chris Lattnera3c109b2010-07-29 02:16:43 +00002256ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002257 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002258 // Treat an enum type as its underlying type.
2259 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2260 Ty = EnumTy->getDecl()->getIntegerType();
2261
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002262 return (Ty->isPromotableIntegerType() ?
2263 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002264 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002265
Daniel Dunbar42025572009-09-14 21:54:03 +00002266 // Ignore empty records.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002267 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar42025572009-09-14 21:54:03 +00002268 return ABIArgInfo::getIgnore();
2269
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002270 // Structures with either a non-trivial destructor or a non-trivial
2271 // copy constructor are always indirect.
2272 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2273 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2274
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002275 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2276 // backend doesn't support byval.
2277 // FIXME: This doesn't handle alignment > 64 bits.
2278 const llvm::Type* ElemTy;
2279 unsigned SizeRegs;
Chris Lattnera3c109b2010-07-29 02:16:43 +00002280 if (getContext().getTypeAlign(Ty) > 32) {
2281 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2282 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002283 } else {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002284 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2285 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002286 }
2287 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00002288 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattnera3c109b2010-07-29 02:16:43 +00002289 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2290 true);
Chris Lattner800588f2010-07-29 06:26:06 +00002291 return ABIArgInfo::getDirect(STy);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002292}
2293
Chris Lattnera3c109b2010-07-29 02:16:43 +00002294static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar98303b92009-09-13 08:03:58 +00002295 llvm::LLVMContext &VMContext) {
2296 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2297 // is called integer-like if its size is less than or equal to one word, and
2298 // the offset of each of its addressable sub-fields is zero.
2299
2300 uint64_t Size = Context.getTypeSize(Ty);
2301
2302 // Check that the type fits in a word.
2303 if (Size > 32)
2304 return false;
2305
2306 // FIXME: Handle vector types!
2307 if (Ty->isVectorType())
2308 return false;
2309
Daniel Dunbarb0d58192009-09-14 02:20:34 +00002310 // Float types are never treated as "integer like".
2311 if (Ty->isRealFloatingType())
2312 return false;
2313
Daniel Dunbar98303b92009-09-13 08:03:58 +00002314 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00002315 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00002316 return true;
2317
Daniel Dunbar45815812010-02-01 23:31:26 +00002318 // Small complex integer types are "integer like".
2319 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2320 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002321
2322 // Single element and zero sized arrays should be allowed, by the definition
2323 // above, but they are not.
2324
2325 // Otherwise, it must be a record type.
2326 const RecordType *RT = Ty->getAs<RecordType>();
2327 if (!RT) return false;
2328
2329 // Ignore records with flexible arrays.
2330 const RecordDecl *RD = RT->getDecl();
2331 if (RD->hasFlexibleArrayMember())
2332 return false;
2333
2334 // Check that all sub-fields are at offset 0, and are themselves "integer
2335 // like".
2336 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2337
2338 bool HadField = false;
2339 unsigned idx = 0;
2340 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2341 i != e; ++i, ++idx) {
2342 const FieldDecl *FD = *i;
2343
Daniel Dunbar679855a2010-01-29 03:22:29 +00002344 // Bit-fields are not addressable, we only need to verify they are "integer
2345 // like". We still have to disallow a subsequent non-bitfield, for example:
2346 // struct { int : 0; int x }
2347 // is non-integer like according to gcc.
2348 if (FD->isBitField()) {
2349 if (!RD->isUnion())
2350 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002351
Daniel Dunbar679855a2010-01-29 03:22:29 +00002352 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2353 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002354
Daniel Dunbar679855a2010-01-29 03:22:29 +00002355 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00002356 }
2357
Daniel Dunbar679855a2010-01-29 03:22:29 +00002358 // Check if this field is at offset 0.
2359 if (Layout.getFieldOffset(idx) != 0)
2360 return false;
2361
Daniel Dunbar98303b92009-09-13 08:03:58 +00002362 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2363 return false;
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002364
Daniel Dunbar679855a2010-01-29 03:22:29 +00002365 // Only allow at most one field in a structure. This doesn't match the
2366 // wording above, but follows gcc in situations with a field following an
2367 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00002368 if (!RD->isUnion()) {
2369 if (HadField)
2370 return false;
2371
2372 HadField = true;
2373 }
2374 }
2375
2376 return true;
2377}
2378
Chris Lattnera3c109b2010-07-29 02:16:43 +00002379ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002380 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002381 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00002382
John McCalld608cdb2010-08-22 10:59:02 +00002383 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002384 // Treat an enum type as its underlying type.
2385 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2386 RetTy = EnumTy->getDecl()->getIntegerType();
2387
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00002388 return (RetTy->isPromotableIntegerType() ?
2389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002390 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002391
Rafael Espindola0eb1d972010-06-08 02:42:08 +00002392 // Structures with either a non-trivial destructor or a non-trivial
2393 // copy constructor are always indirect.
2394 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2395 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2396
Daniel Dunbar98303b92009-09-13 08:03:58 +00002397 // Are we following APCS?
2398 if (getABIKind() == APCS) {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002399 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar98303b92009-09-13 08:03:58 +00002400 return ABIArgInfo::getIgnore();
2401
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002402 // Complex types are all returned as packed integers.
2403 //
2404 // FIXME: Consider using 2 x vector types if the back end handles them
2405 // correctly.
2406 if (RetTy->isAnyComplexType())
Chris Lattner800588f2010-07-29 06:26:06 +00002407 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattnera3c109b2010-07-29 02:16:43 +00002408 getContext().getTypeSize(RetTy)));
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00002409
Daniel Dunbar98303b92009-09-13 08:03:58 +00002410 // Integer like structures are returned in r0.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002411 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar98303b92009-09-13 08:03:58 +00002412 // Return in the smallest viable integer type.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002413 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar98303b92009-09-13 08:03:58 +00002414 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002415 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002416 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002417 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2418 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar98303b92009-09-13 08:03:58 +00002419 }
2420
2421 // Otherwise return in memory.
2422 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002423 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00002424
2425 // Otherwise this is an AAPCS variant.
2426
Chris Lattnera3c109b2010-07-29 02:16:43 +00002427 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar16a08082009-09-14 00:56:55 +00002428 return ABIArgInfo::getIgnore();
2429
Daniel Dunbar98303b92009-09-13 08:03:58 +00002430 // Aggregates <= 4 bytes are returned in r0; other aggregates
2431 // are returned indirectly.
Chris Lattnera3c109b2010-07-29 02:16:43 +00002432 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00002433 if (Size <= 32) {
2434 // Return in the smallest viable integer type.
2435 if (Size <= 8)
Chris Lattner800588f2010-07-29 06:26:06 +00002436 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002437 if (Size <= 16)
Chris Lattner800588f2010-07-29 06:26:06 +00002438 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2439 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar16a08082009-09-14 00:56:55 +00002440 }
2441
Daniel Dunbar98303b92009-09-13 08:03:58 +00002442 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002443}
2444
2445llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner77b89b82010-06-27 07:15:29 +00002446 CodeGenFunction &CGF) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002447 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002448 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002449 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002450
2451 CGBuilderTy &Builder = CGF.Builder;
2452 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2453 "ap");
2454 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2455 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002456 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002457 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2458
2459 uint64_t Offset =
2460 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2461 llvm::Value *NextAddr =
Chris Lattner77b89b82010-06-27 07:15:29 +00002462 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002463 "ap.next");
2464 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2465
2466 return AddrTyped;
2467}
2468
Chris Lattnera3c109b2010-07-29 02:16:43 +00002469ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2470 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002471 return ABIArgInfo::getIgnore();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00002472
John McCalld608cdb2010-08-22 10:59:02 +00002473 if (isAggregateTypeForABI(RetTy))
Chris Lattnera3c109b2010-07-29 02:16:43 +00002474 return ABIArgInfo::getIndirect(0);
2475
2476 // Treat an enum type as its underlying type.
2477 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2478 RetTy = EnumTy->getDecl()->getIntegerType();
2479
2480 return (RetTy->isPromotableIntegerType() ?
2481 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002482}
2483
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002484//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002485// SystemZ ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002486//===----------------------------------------------------------------------===//
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002487
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002488namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002489
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002490class SystemZABIInfo : public ABIInfo {
Chris Lattnerea044322010-07-29 02:01:43 +00002491public:
2492 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2493
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002494 bool isPromotableIntegerType(QualType Ty) const;
2495
Chris Lattnera3c109b2010-07-29 02:16:43 +00002496 ABIArgInfo classifyReturnType(QualType RetTy) const;
2497 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002498
Chris Lattneree5dcd02010-07-29 02:31:05 +00002499 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattnera3c109b2010-07-29 02:16:43 +00002500 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002501 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2502 it != ie; ++it)
Chris Lattnera3c109b2010-07-29 02:16:43 +00002503 it->info = classifyArgumentType(it->type);
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002504 }
2505
2506 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2507 CodeGenFunction &CGF) const;
2508};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002509
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002510class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2511public:
Chris Lattnerea044322010-07-29 02:01:43 +00002512 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2513 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002514};
2515
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002516}
2517
2518bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2519 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002520 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002521 switch (BT->getKind()) {
2522 case BuiltinType::Bool:
2523 case BuiltinType::Char_S:
2524 case BuiltinType::Char_U:
2525 case BuiltinType::SChar:
2526 case BuiltinType::UChar:
2527 case BuiltinType::Short:
2528 case BuiltinType::UShort:
2529 case BuiltinType::Int:
2530 case BuiltinType::UInt:
2531 return true;
2532 default:
2533 return false;
2534 }
2535 return false;
2536}
2537
2538llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2539 CodeGenFunction &CGF) const {
2540 // FIXME: Implement
2541 return 0;
2542}
2543
2544
Chris Lattnera3c109b2010-07-29 02:16:43 +00002545ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2546 if (RetTy->isVoidType())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002547 return ABIArgInfo::getIgnore();
John McCalld608cdb2010-08-22 10:59:02 +00002548 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002549 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002550
2551 return (isPromotableIntegerType(RetTy) ?
2552 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002553}
2554
Chris Lattnera3c109b2010-07-29 02:16:43 +00002555ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalld608cdb2010-08-22 10:59:02 +00002556 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002557 return ABIArgInfo::getIndirect(0);
Chris Lattnera3c109b2010-07-29 02:16:43 +00002558
2559 return (isPromotableIntegerType(Ty) ?
2560 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002561}
2562
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002563//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002564// MSP430 ABI Implementation
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002565//===----------------------------------------------------------------------===//
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002566
2567namespace {
2568
2569class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2570public:
Chris Lattnerea044322010-07-29 02:01:43 +00002571 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2572 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002573 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2574 CodeGen::CodeGenModule &M) const;
2575};
2576
2577}
2578
2579void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2580 llvm::GlobalValue *GV,
2581 CodeGen::CodeGenModule &M) const {
2582 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2583 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2584 // Handle 'interrupt' attribute:
2585 llvm::Function *F = cast<llvm::Function>(GV);
2586
2587 // Step 1: Set ISR calling convention.
2588 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2589
2590 // Step 2: Add attributes goodness.
2591 F->addFnAttr(llvm::Attribute::NoInline);
2592
2593 // Step 3: Emit ISR vector alias.
2594 unsigned Num = attr->getNumber() + 0xffe0;
2595 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2596 "vector_" +
2597 llvm::LowercaseString(llvm::utohexstr(Num)),
2598 GV, &M.getModule());
2599 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002600 }
2601}
2602
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002603//===----------------------------------------------------------------------===//
John McCallaeeb7012010-05-27 06:19:26 +00002604// MIPS ABI Implementation. This works for both little-endian and
2605// big-endian variants.
Chris Lattnerdce5ad02010-06-28 20:05:43 +00002606//===----------------------------------------------------------------------===//
2607
John McCallaeeb7012010-05-27 06:19:26 +00002608namespace {
2609class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2610public:
Chris Lattnerea044322010-07-29 02:01:43 +00002611 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2612 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCallaeeb7012010-05-27 06:19:26 +00002613
2614 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2615 return 29;
2616 }
2617
2618 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencer8bea82f2010-08-25 18:17:27 +00002619 llvm::Value *Address) const;
John McCallaeeb7012010-05-27 06:19:26 +00002620};
2621}
2622
2623bool
2624MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2625 llvm::Value *Address) const {
2626 // This information comes from gcc's implementation, which seems to
2627 // as canonical as it gets.
2628
2629 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2630 llvm::LLVMContext &Context = CGF.getLLVMContext();
2631
2632 // Everything on MIPS is 4 bytes. Double-precision FP registers
2633 // are aliased to pairs of single-precision FP registers.
2634 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2635 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2636
2637 // 0-31 are the general purpose registers, $0 - $31.
2638 // 32-63 are the floating-point registers, $f0 - $f31.
2639 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2640 // 66 is the (notional, I think) register for signal-handler return.
2641 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2642
2643 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2644 // They are one bit wide and ignored here.
2645
2646 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2647 // (coprocessor 1 is the FP unit)
2648 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2649 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2650 // 176-181 are the DSP accumulator registers.
2651 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2652
2653 return false;
2654}
2655
2656
Chris Lattnerea044322010-07-29 02:01:43 +00002657const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002658 if (TheTargetCodeGenInfo)
2659 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002660
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002661 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2662 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002663
Chris Lattner9c254f02010-06-29 06:01:59 +00002664 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002665 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002666 default:
Chris Lattnerea044322010-07-29 02:01:43 +00002667 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002668
John McCallaeeb7012010-05-27 06:19:26 +00002669 case llvm::Triple::mips:
2670 case llvm::Triple::mipsel:
Chris Lattnerea044322010-07-29 02:01:43 +00002671 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCallaeeb7012010-05-27 06:19:26 +00002672
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002673 case llvm::Triple::arm:
2674 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002675 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002676 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002677 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002678 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002679
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002680 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002681 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002682
2683 case llvm::Triple::pic16:
Chris Lattnerea044322010-07-29 02:01:43 +00002684 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002685
John McCallec853ba2010-03-11 00:10:12 +00002686 case llvm::Triple::ppc:
Chris Lattnerea044322010-07-29 02:01:43 +00002687 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallec853ba2010-03-11 00:10:12 +00002688
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002689 case llvm::Triple::systemz:
Chris Lattnerea044322010-07-29 02:01:43 +00002690 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002691
2692 case llvm::Triple::msp430:
Chris Lattnerea044322010-07-29 02:01:43 +00002693 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002694
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002695 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002696 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002697 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002698 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002699 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002700 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002701 case llvm::Triple::MinGW32:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002702 case llvm::Triple::AuroraUX:
2703 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002704 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002705 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002706 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002707 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002708
2709 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002710 return *(TheTargetCodeGenInfo =
Chris Lattnerea044322010-07-29 02:01:43 +00002711 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002712 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002713
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002714 case llvm::Triple::x86_64:
Chris Lattnerf13721d2010-08-31 16:44:54 +00002715 switch (Triple.getOS()) {
2716 case llvm::Triple::Win32:
2717 case llvm::Triple::MinGW64:
2718 case llvm::Triple::Cygwin:
2719 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2720 default:
2721 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2722 }
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002723 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002724}