blob: e8e25ca6d488dd8743f432418f8fd1ee48ba89a2 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Chris Lattner22a931e2010-06-29 06:01:59 +000020#include "llvm/Target/TargetData.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000021#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000022#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000024using namespace clang;
25using namespace CodeGen;
26
John McCall943fae92010-05-27 06:19:26 +000027static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28 llvm::Value *Array,
29 llvm::Value *Value,
30 unsigned FirstIndex,
31 unsigned LastIndex) {
32 // Alternatively, we could emit this as a loop in the source.
33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35 Builder.CreateStore(Value, Cell);
36 }
37}
38
John McCalla1dee5302010-08-22 10:59:02 +000039static bool isAggregateTypeForABI(QualType T) {
40 return CodeGenFunction::hasAggregateLLVMType(T) ||
41 T->isMemberFunctionPointerType();
42}
43
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044ABIInfo::~ABIInfo() {}
45
Chris Lattner2b037972010-07-29 02:01:43 +000046ASTContext &ABIInfo::getContext() const {
47 return CGT.getContext();
48}
49
50llvm::LLVMContext &ABIInfo::getVMContext() const {
51 return CGT.getLLVMContext();
52}
53
54const llvm::TargetData &ABIInfo::getTargetData() const {
55 return CGT.getTargetData();
56}
57
58
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000060 llvm::raw_ostream &OS = llvm::errs();
61 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000062 switch (TheKind) {
63 case Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +000064 OS << "Direct Type=";
65 if (const llvm::Type *Ty = getCoerceToType())
66 Ty->print(OS);
67 else
68 OS << "null";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000069 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000070 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000071 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000072 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000073 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000074 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000076 case Indirect:
Daniel Dunbar557893d2010-04-21 19:10:51 +000077 OS << "Indirect Align=" << getIndirectAlign()
Daniel Dunbar7b7c2932010-09-16 20:42:02 +000078 << " Byal=" << getIndirectByVal()
79 << " Realign=" << getIndirectRealign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000080 break;
81 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000082 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000083 break;
84 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000085 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000086}
87
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000088TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
89
Daniel Dunbar626f1d82009-09-13 08:03:58 +000090static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +000094static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
95 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000096 if (FD->isUnnamedBitfield())
97 return true;
98
99 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000100
Daniel Dunbar626f1d82009-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 Dunbarcd20ce12010-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 Dunbar626f1d82009-09-13 08:03:58 +0000117 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-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 Dunbar626f1d82009-09-13 08:03:58 +0000123static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000124 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-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 Dunbarcd20ce12010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
139 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000140 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000141 return false;
142 return true;
143}
144
Anders Carlsson20759ad2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000151
Anders Carlsson20759ad2009-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 Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +0000184
Daniel Dunbar12ebb472010-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 Dunbar12ebb472010-05-11 21:15:36 +0000189 // Ignore empty records.
Daniel Dunbarcd20ce12010-05-17 16:46:00 +0000190 if (isEmptyRecord(Context, i->getType(), true))
Daniel Dunbar12ebb472010-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000206 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
207 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000208 const FieldDecl *FD = *i;
209 QualType FT = FD->getType();
210
211 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000212 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 McCalla1dee5302010-08-22 10:59:02 +0000227 if (!isAggregateTypeForABI(FT)) {
Anton Korobeynikov244360d2009-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 Dunbar6b45b672010-05-14 03:40:53 +0000240 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000241 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
242 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-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 Dunbar11c08c82009-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 Kyrtzidiscfbfe782009-06-30 02:36:12 +0000271 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
272 i != e; ++i) {
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +0000294public:
295 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000296
Chris Lattner458b2aa2010-07-29 02:16:43 +0000297 ABIArgInfo classifyReturnType(QualType RetTy) const;
298 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000299
Chris Lattner22326a12010-07-29 02:31:05 +0000300 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000301 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000302 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
303 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000304 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000305 }
306
307 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
308 CodeGenFunction &CGF) const;
309};
310
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000311class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
312public:
Chris Lattner2b037972010-07-29 02:01:43 +0000313 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
314 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-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 Lattner458b2aa2010-07-29 02:16:43 +0000322ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +0000323 if (isAggregateTypeForABI(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000324 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000325
Chris Lattner9723d6c2010-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 Gregora71cc152010-02-02 20:10:50 +0000329
Chris Lattner9723d6c2010-03-11 18:19:55 +0000330 return (Ty->isPromotableIntegerType() ?
331 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000332}
333
Chris Lattner0cf24192010-06-28 20:05:43 +0000334//===----------------------------------------------------------------------===//
335// X86-32 ABI Implementation
336//===----------------------------------------------------------------------===//
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000337
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000338/// X86_32ABIInfo - The X86-32 ABI information.
339class X86_32ABIInfo : public ABIInfo {
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000340 static const unsigned MinABIStackAlignInBytes = 4;
341
David Chisnallde3a0692009-08-17 23:08:21 +0000342 bool IsDarwinVectorABI;
343 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-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 Dunbar557893d2010-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 Lattner458b2aa2010-07-29 02:16:43 +0000353 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
Daniel Dunbar557893d2010-04-21 19:10:51 +0000354
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000355 /// \brief Return the alignment to use for the given type on the stack.
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000356 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000357
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000358public:
Chris Lattner2b037972010-07-29 02:01:43 +0000359
Chris Lattner458b2aa2010-07-29 02:16:43 +0000360 ABIArgInfo classifyReturnType(QualType RetTy) const;
361 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000362
Chris Lattner22326a12010-07-29 02:31:05 +0000363 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000364 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000365 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
366 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +0000367 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000368 }
369
370 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
371 CodeGenFunction &CGF) const;
372
Chris Lattner2b037972010-07-29 02:01:43 +0000373 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
374 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000375};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000376
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000377class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
378public:
Chris Lattner2b037972010-07-29 02:01:43 +0000379 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p)
380 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000381
382 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
383 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-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 Korobeynikov55bcea12010-01-10 12:58:08 +0000394};
395
396}
Anton Korobeynikov244360d2009-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 Dunbar4bd95c62010-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 Dunbar6b45b672010-05-14 03:40:53 +0000419 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000420 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000421 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-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 Kremenekc23c7e62009-07-29 21:53:49 +0000429 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000430 if (!RT) return false;
431
Anders Carlsson40446e82010-01-27 03:25:19 +0000432 // FIXME: Traverse bases here too.
433
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000434 // Structure types are passed in register if all fields would be
435 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-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 Korobeynikov244360d2009-06-05 22:08:42 +0000438 const FieldDecl *FD = *i;
439
440 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000441 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000452ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
453 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000454 return ABIArgInfo::getIgnore();
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000455
Chris Lattner458b2aa2010-07-29 02:16:43 +0000456 if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000457 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000458 if (IsDarwinVectorABI) {
Chris Lattner458b2aa2010-07-29 02:16:43 +0000459 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-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 Lattnerfe34c1d2010-07-29 06:26:06 +0000465 return ABIArgInfo::getDirect(llvm::VectorType::get(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000466 llvm::Type::getInt64Ty(getVMContext()), 2));
Anton Korobeynikov244360d2009-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 Lattnerfe34c1d2010-07-29 06:26:06 +0000472 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +0000473 Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000474
475 return ABIArgInfo::getIndirect(0);
476 }
477
478 return ABIArgInfo::getDirect();
Chris Lattner458b2aa2010-07-29 02:16:43 +0000479 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000480
John McCalla1dee5302010-08-22 10:59:02 +0000481 if (isAggregateTypeForABI(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000482 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-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. Spencerb2f376b2010-08-25 18:17:27 +0000487
Anders Carlsson5789c492009-10-20 22:07:59 +0000488 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000489 if (RT->getDecl()->hasFlexibleArrayMember())
490 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000491 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000492
David Chisnallde3a0692009-08-17 23:08:21 +0000493 // If specified, structs and unions are always indirect.
494 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000495 return ABIArgInfo::getIndirect(0);
496
497 // Classify "single element" structs as their element type.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000498 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) {
John McCall9dd450b2009-09-21 23:43:11 +0000499 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000504 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000505 return ABIArgInfo::getDirect(
Chris Lattner458b2aa2010-07-29 02:16:43 +0000506 llvm::IntegerType::get(getVMContext(), (unsigned)Size));
507 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000508
Chris Lattner458b2aa2010-07-29 02:16:43 +0000509 if (BT->getKind() == BuiltinType::Float) {
510 assert(getContext().getTypeSize(RetTy) ==
511 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000512 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000513 return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext()));
Chris Lattner458b2aa2010-07-29 02:16:43 +0000514 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000515
Chris Lattner458b2aa2010-07-29 02:16:43 +0000516 if (BT->getKind() == BuiltinType::Double) {
517 assert(getContext().getTypeSize(RetTy) ==
518 getContext().getTypeSize(SeltTy) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000519 "Unexpect single element structure size!");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000520 return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext()));
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000525 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000526 return ABIArgInfo::getDirect(PtrTy);
Anton Korobeynikov244360d2009-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 Lattner458b2aa2010-07-29 02:16:43 +0000530 uint64_t Size = getContext().getTypeSize(RetTy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000531 if (Size == 64 || Size == 128)
532 return ABIArgInfo::getIndirect(0);
533
Chris Lattner458b2aa2010-07-29 02:16:43 +0000534 return classifyReturnType(QualType(SeltTy, 0));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000535 }
536 }
537
538 // Small structures which are register sized are generally returned
539 // in a register.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000540 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
541 uint64_t Size = getContext().getTypeSize(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000542 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000543 }
544
545 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000546 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000547
Chris Lattner458b2aa2010-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 Korobeynikov244360d2009-06-05 22:08:42 +0000554}
555
Daniel Dunbared23de32010-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 Dunbardd38fbc2010-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 Dunbar8a6c91f2010-09-16 20:41:56 +0000587 if (Align <= MinABIStackAlignInBytes)
Daniel Dunbardd38fbc2010-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 Dunbar8a6c91f2010-09-16 20:41:56 +0000593 return MinABIStackAlignInBytes;
Daniel Dunbardd38fbc2010-09-16 20:42:06 +0000594 }
Daniel Dunbar8a6c91f2010-09-16 20:41:56 +0000595
Daniel Dunbared23de32010-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 Dunbar8a6c91f2010-09-16 20:41:56 +0000601}
602
Chris Lattner458b2aa2010-07-29 02:16:43 +0000603ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000604 if (!ByVal)
605 return ABIArgInfo::getIndirect(0, false);
606
Daniel Dunbardd38fbc2010-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 Dunbar557893d2010-04-21 19:10:51 +0000620}
621
Chris Lattner458b2aa2010-07-29 02:16:43 +0000622ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000623 // FIXME: Set alignment on indirect arguments.
John McCalla1dee5302010-08-22 10:59:02 +0000624 if (isAggregateTypeForABI(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000625 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-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 Lattner458b2aa2010-07-29 02:16:43 +0000630 return getIndirectResult(Ty, /*ByVal=*/false);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000631
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000632 if (RT->getDecl()->hasFlexibleArrayMember())
Chris Lattner458b2aa2010-07-29 02:16:43 +0000633 return getIndirectResult(Ty);
Anders Carlsson40446e82010-01-27 03:25:19 +0000634 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000635
636 // Ignore empty structs.
Chris Lattner458b2aa2010-07-29 02:16:43 +0000637 if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000638 return ABIArgInfo::getIgnore();
639
Daniel Dunbar11c08c82009-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 Lattner458b2aa2010-07-29 02:16:43 +0000644 if (getContext().getTypeSize(Ty) <= 4*32 &&
645 canExpandIndirectArgument(Ty, getContext()))
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000646 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000647
Chris Lattner458b2aa2010-07-29 02:16:43 +0000648 return getIndirectResult(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000649 }
650
Chris Lattnerd774ae92010-08-26 20:05:13 +0000651 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattnerd7e54802010-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 Lattnerd774ae92010-08-26 20:05:13 +0000654 if (IsDarwinVectorABI) {
655 uint64_t Size = getContext().getTypeSize(Ty);
Chris Lattnerd774ae92010-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 Lattnerd774ae92010-08-26 20:05:13 +0000660 }
661
662 return ABIArgInfo::getDirect();
663 }
664
665
Chris Lattner458b2aa2010-07-29 02:16:43 +0000666 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
667 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000668
Chris Lattner458b2aa2010-07-29 02:16:43 +0000669 return (Ty->isPromotableIntegerType() ?
670 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000671}
672
673llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
674 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000675 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000676 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000683 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-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 Lattner5e016ae2010-06-27 07:15:29 +0000689 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000690 "ap.next");
691 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
692
693 return AddrTyped;
694}
695
Charles Davis4ea31ab2010-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 McCallbeec5a02010-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. Spencerb2f376b2010-08-25 18:17:27 +0000718
John McCallbeec5a02010-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 McCall943fae92010-05-27 06:19:26 +0000722 AssignToArrayRange(Builder, Address, Four8, 0, 8);
John McCallbeec5a02010-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 McCall943fae92010-05-27 06:19:26 +0000729 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000730
John McCallbeec5a02010-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 McCall943fae92010-05-27 06:19:26 +0000740 AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
741 }
John McCallbeec5a02010-03-06 00:35:14 +0000742
743 return false;
744}
745
Chris Lattner0cf24192010-06-28 20:05:43 +0000746//===----------------------------------------------------------------------===//
747// X86-64 ABI Implementation
748//===----------------------------------------------------------------------===//
749
750
Anton Korobeynikov244360d2009-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 Lattnerd776fb12010-06-28 21:43:59 +0000774 static Class merge(Class Accum, Class Field);
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +0000797 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000798
Chris Lattner4200fe42010-07-29 04:56:46 +0000799 const llvm::Type *Get16ByteVectorType(QualType Ty) const;
Chris Lattnerc95a3982010-07-29 17:49:08 +0000800 const llvm::Type *GetSSETypeAtOffset(const llvm::Type *IRType,
Chris Lattner7f4b81a2010-07-29 18:13:09 +0000801 unsigned IROffset, QualType SourceTy,
802 unsigned SourceOffset) const;
Chris Lattner1c56d9a2010-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. Spencerb2f376b2010-08-25 18:17:27 +0000806
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000807 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000808 /// such that the argument will be returned in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000809 ABIArgInfo getIndirectReturnResult(QualType Ty) const;
Daniel Dunbar53fac692010-04-21 19:49:55 +0000810
811 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000812 /// such that the argument will be passed in memory.
Chris Lattner22a931e2010-06-29 06:01:59 +0000813 ABIArgInfo getIndirectResult(QualType Ty) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000814
Chris Lattner458b2aa2010-07-29 02:16:43 +0000815 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000816
Chris Lattner029c0f12010-07-29 04:41:05 +0000817 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &neededInt,
818 unsigned &neededSSE) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000819
820public:
Chris Lattner2b037972010-07-29 02:01:43 +0000821 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
Chris Lattner22a931e2010-06-29 06:01:59 +0000822
Chris Lattner22326a12010-07-29 02:31:05 +0000823 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000824
825 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
826 CodeGenFunction &CGF) const;
827};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000828
Chris Lattner04dc9572010-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 Korobeynikov55bcea12010-01-10 12:58:08 +0000838class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
839public:
Chris Lattner2b037972010-07-29 02:01:43 +0000840 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
841 : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {}
John McCallbeec5a02010-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. Spencerb2f376b2010-08-25 18:17:27 +0000854
John McCall943fae92010-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 McCallbeec5a02010-03-06 00:35:14 +0000858
859 return false;
860 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000861};
862
Chris Lattner04dc9572010-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 Korobeynikov244360d2009-06-05 22:08:42 +0000888}
889
Chris Lattnerd776fb12010-06-28 21:43:59 +0000890X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
Anton Korobeynikov244360d2009-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 Lattnerd776fb12010-06-28 21:43:59 +0000918 if (Field == Memory)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000919 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000920 if (Accum == NoClass)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000921 return Field;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000922 if (Accum == Integer || Field == Integer)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000923 return Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000924 if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
925 Accum == X87 || Accum == X87Up)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000926 return Memory;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000927 return SSE;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000928}
929
Chris Lattner5c740f12010-06-30 19:14:05 +0000930void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
Anton Korobeynikov244360d2009-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 McCall9dd450b2009-09-21 23:43:11 +0000945 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-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 Lattnerd776fb12010-06-28 21:43:59 +0000963 return;
964 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000965
Chris Lattnerd776fb12010-06-28 21:43:59 +0000966 if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000967 // Classify the underlying integer type.
Chris Lattner22a931e2010-06-29 06:01:59 +0000968 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
Chris Lattnerd776fb12010-06-28 21:43:59 +0000969 return;
970 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000971
Chris Lattnerd776fb12010-06-28 21:43:59 +0000972 if (Ty->hasPointerRepresentation()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000973 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000974 return;
975 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000976
Chris Lattnerd776fb12010-06-28 21:43:59 +0000977 if (Ty->isMemberPointerType()) {
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000978 if (Ty->isMemberFunctionPointerType())
979 Lo = Hi = Integer;
980 else
981 Current = Integer;
Chris Lattnerd776fb12010-06-28 21:43:59 +0000982 return;
983 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +0000984
Chris Lattnerd776fb12010-06-28 21:43:59 +0000985 if (const VectorType *VT = Ty->getAs<VectorType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +0000986 uint64_t Size = getContext().getTypeSize(VT);
Anton Korobeynikov244360d2009-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 Lattner46830f22010-08-26 18:03:20 +00001004 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
Chris Lattner69e683f2010-08-26 18:13:50 +00001005 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1006 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1007 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
Anton Korobeynikov244360d2009-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 Lattnerd776fb12010-06-28 21:43:59 +00001020 return;
1021 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001022
Chris Lattnerd776fb12010-06-28 21:43:59 +00001023 if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001024 QualType ET = getContext().getCanonicalType(CT->getElementType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001025
Chris Lattner2b037972010-07-29 02:01:43 +00001026 uint64_t Size = getContext().getTypeSize(Ty);
Douglas Gregorb90df602010-06-16 00:17:44 +00001027 if (ET->isIntegralOrEnumerationType()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001028 if (Size <= 64)
1029 Current = Integer;
1030 else if (Size <= 128)
1031 Lo = Hi = Integer;
Chris Lattner2b037972010-07-29 02:01:43 +00001032 } else if (ET == getContext().FloatTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001033 Current = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001034 else if (ET == getContext().DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001035 Lo = Hi = SSE;
Chris Lattner2b037972010-07-29 02:01:43 +00001036 else if (ET == getContext().LongDoubleTy)
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +00001042 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001043 if (Hi == NoClass && EB_Real != EB_Imag)
1044 Hi = Lo;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001045
Chris Lattnerd776fb12010-06-28 21:43:59 +00001046 return;
1047 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001048
Chris Lattner2b037972010-07-29 02:01:43 +00001049 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001050 // Arrays are treated like structures.
1051
Chris Lattner2b037972010-07-29 02:01:43 +00001052 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +00001063 if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +00001069 uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +00001073 classify(AT->getElementType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-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 Lattnerd776fb12010-06-28 21:43:59 +00001084 return;
1085 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001086
Chris Lattnerd776fb12010-06-28 21:43:59 +00001087 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Chris Lattner2b037972010-07-29 02:01:43 +00001088 uint64_t Size = getContext().getTypeSize(Ty);
Anton Korobeynikov244360d2009-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 Carlsson20759ad2009-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 Dunbare1cd0152009-11-22 23:01:23 +00001100
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +00001107 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001108
1109 // Reset Lo class, this will be recomputed.
1110 Current = NoClass;
Daniel Dunbare1cd0152009-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 Lattner22a931e2010-06-29 06:01:59 +00001128 classify(i->getType(), Offset, FieldLo, FieldHi);
Daniel Dunbare1cd0152009-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 Korobeynikov244360d2009-06-05 22:08:42 +00001137 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001138 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1139 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +00001147 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
Anton Korobeynikov244360d2009-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 Lattner2b037972010-07-29 02:01:43 +00001169 uint64_t Size =
1170 i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue();
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +00001184 classify(i->getType(), Offset, FieldLo, FieldHi);
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +00001210ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
Daniel Dunbar53fac692010-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 McCalla1dee5302010-08-22 10:59:02 +00001213 if (!isAggregateTypeForABI(Ty)) {
Daniel Dunbar53fac692010-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 Lattner22a931e2010-06-29 06:01:59 +00001225ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
Anton Korobeynikov244360d2009-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 McCalla1dee5302010-08-22 10:59:02 +00001228 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-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 Korobeynikov18adbf52009-06-06 09:36:29 +00001233 return (Ty->isPromotableIntegerType() ?
1234 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001235 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001236
Daniel Dunbar53fac692010-04-21 19:49:55 +00001237 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1238 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001239
Daniel Dunbar53fac692010-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 Lattner2b037972010-07-29 02:01:43 +00001243 unsigned Align = getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar53fac692010-04-21 19:49:55 +00001244 if (Align > MinABIAlign)
1245 return ABIArgInfo::getIndirect(Align);
1246 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001247}
1248
Chris Lattner4200fe42010-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 Lattner9fa15c32010-07-29 05:02:29 +00001253 const llvm::Type *IRType = CGT.ConvertTypeRecursive(Ty);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001254
Chris Lattner9fa15c32010-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. Spencerb2f376b2010-08-25 18:17:27 +00001262
Chris Lattner4200fe42010-07-29 04:56:46 +00001263 // If the preferred type is a 16-byte vector, prefer to pass it.
Chris Lattner9fa15c32010-07-29 05:02:29 +00001264 if (const llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
Chris Lattner4200fe42010-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. Spencerb2f376b2010-08-25 18:17:27 +00001273
Chris Lattner4200fe42010-07-29 04:56:46 +00001274 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1275}
1276
Chris Lattnerc8b7b532010-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 Lattner98076a22010-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. Spencerb2f376b2010-08-25 18:17:27 +00001302
Chris Lattner98076a22010-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. Spencerb2f376b2010-08-25 18:17:27 +00001311
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001315
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001324
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001328
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001335
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001344
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001353
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001358
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001359 return false;
1360}
1361
Chris Lattnere556a712010-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. Spencerb2f376b2010-08-25 18:17:27 +00001371
Chris Lattnere556a712010-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. Spencerb2f376b2010-08-25 18:17:27 +00001379
Chris Lattnere556a712010-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 Lattner7f4b81a2010-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 Lattner50a357e2010-07-29 18:19:50 +00001397 // The only three choices we have are either double, <2 x float>, or float. We
Chris Lattner7f4b81a2010-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. Spencerb2f376b2010-08-25 18:17:27 +00001403
Chris Lattnere556a712010-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 Lattner9f8b4512010-08-25 23:39:14 +00001408 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
1409 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001410
Chris Lattner7f4b81a2010-07-29 18:13:09 +00001411 return llvm::Type::getDoubleTy(getVMContext());
1412}
1413
1414
Chris Lattner1c56d9a2010-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 Lattnerb22f1c82010-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 Lattnerc11301c2010-07-29 02:20:19 +00001429const llvm::Type *X86_64ABIInfo::
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001430GetINTEGERTypeAtOffset(const llvm::Type *IRType, unsigned IROffset,
1431 QualType SourceTy, unsigned SourceOffset) const {
Chris Lattnerc8b7b532010-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 Lattnerb22f1c82010-07-28 22:44:07 +00001438
Chris Lattnerc8b7b532010-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. Spencerb2f376b2010-08-25 18:17:27 +00001448
Chris Lattnerc8b7b532010-07-29 07:30:00 +00001449 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1450 SourceOffset*8+64, getContext()))
1451 return IRType;
1452 }
1453 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001454
Chris Lattnerce1bd752010-07-29 04:51:12 +00001455 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001456 // If this is a struct, recurse into the field at the specified offset.
Chris Lattnerc11301c2010-07-29 02:20:19 +00001457 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001458 if (IROffset < SL->getSizeInBytes()) {
1459 unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1460 IROffset -= SL->getElementOffset(FieldIdx);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001461
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001462 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
1463 SourceTy, SourceOffset);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001464 }
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001465 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001466
Chris Lattner98076a22010-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 Lattner1c56d9a2010-07-29 17:40:35 +00001471 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
1472 SourceOffset);
Chris Lattner98076a22010-07-29 07:43:55 +00001473 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001474
Chris Lattnerb22f1c82010-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 Lattner3f763422010-07-29 17:34:39 +00001477 unsigned TySizeInBytes =
1478 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001479
Chris Lattner3f763422010-07-29 17:34:39 +00001480 assert(TySizeInBytes != SourceOffset && "Empty field?");
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001481
Chris Lattnerb22f1c82010-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 Lattner3f763422010-07-29 17:34:39 +00001484 return llvm::IntegerType::get(getVMContext(),
1485 std::min(TySizeInBytes-SourceOffset, 8U)*8);
Chris Lattner22a931e2010-06-29 06:01:59 +00001486}
1487
Chris Lattnerd426c8e2010-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 Lattner31faff52010-07-28 23:06:14 +00001532ABIArgInfo X86_64ABIInfo::
Chris Lattner458b2aa2010-07-29 02:16:43 +00001533classifyReturnType(QualType RetTy) const {
Chris Lattner31faff52010-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 Lattner31faff52010-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 Lattner8a2f3c72010-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 Lattner31faff52010-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 Lattner1c56d9a2010-07-29 17:40:35 +00001566 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0,
1567 RetTy, 0);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001568
Chris Lattner1f3a0632010-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. Spencerb2f376b2010-08-25 18:17:27 +00001575
Chris Lattner1f3a0632010-07-29 21:42:50 +00001576 if (RetTy->isIntegralOrEnumerationType() &&
1577 RetTy->isPromotableIntegerType())
1578 return ABIArgInfo::getExtend();
1579 }
Chris Lattner31faff52010-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 Lattner7f4b81a2010-07-29 18:13:09 +00001585 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(RetTy), 0, RetTy, 0);
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001586 break;
Chris Lattner31faff52010-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 Lattner2b037972010-07-29 02:01:43 +00001591 ResType = llvm::Type::getX86_FP80Ty(getVMContext());
Chris Lattnerfa560fe2010-07-28 23:12:33 +00001592 break;
Chris Lattner31faff52010-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 Lattner458b2aa2010-07-29 02:16:43 +00001599 ResType = llvm::StructType::get(getVMContext(),
Chris Lattner2b037972010-07-29 02:01:43 +00001600 llvm::Type::getX86_FP80Ty(getVMContext()),
1601 llvm::Type::getX86_FP80Ty(getVMContext()),
Chris Lattner31faff52010-07-28 23:06:14 +00001602 NULL);
1603 break;
1604 }
1605
Chris Lattner52b3c132010-09-01 00:20:33 +00001606 const llvm::Type *HighPart = 0;
Chris Lattner31faff52010-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 Lattnerfa560fe2010-07-28 23:12:33 +00001615 case NoClass:
1616 break;
Chris Lattner31faff52010-07-28 23:06:14 +00001617
Chris Lattner52b3c132010-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 Lattner31faff52010-07-28 23:06:14 +00001623 break;
Chris Lattner52b3c132010-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 Lattner31faff52010-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 Lattner4200fe42010-07-29 04:56:46 +00001636 ResType = Get16ByteVectorType(RetTy);
Chris Lattner31faff52010-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 Lattnerc95a3982010-07-29 17:49:08 +00001646 if (Lo != X87) {
Chris Lattner52b3c132010-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 Lattnerc95a3982010-07-29 17:49:08 +00001651 }
Chris Lattner31faff52010-07-28 23:06:14 +00001652 break;
1653 }
Chris Lattner52b3c132010-09-01 00:20:33 +00001654
1655 // If a high part was specified, merge it together with the low part. It is
Chris Lattnerbe5eb172010-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 Lattnerd426c8e2010-09-01 00:50:20 +00001658 if (HighPart)
1659 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattner31faff52010-07-28 23:06:14 +00001660
Chris Lattner1f3a0632010-07-29 21:42:50 +00001661 return ABIArgInfo::getDirect(ResType);
Chris Lattner31faff52010-07-28 23:06:14 +00001662}
1663
Chris Lattner458b2aa2010-07-29 02:16:43 +00001664ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
Chris Lattner029c0f12010-07-29 04:41:05 +00001665 unsigned &neededSSE) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001666 X86_64ABIInfo::Class Lo, Hi;
Chris Lattner22a931e2010-06-29 06:01:59 +00001667 classify(Ty, 0, Lo, Hi);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001668
Anton Korobeynikov244360d2009-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 Korobeynikov244360d2009-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 Lattner8a2f3c72010-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. Spencerb2f376b2010-08-25 18:17:27 +00001686
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +00001695 return getIndirectResult(Ty);
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +00001705 ++neededInt;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001706
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001707 // Pick an 8-byte type based on the preferred type.
Chris Lattner1c56d9a2010-07-29 17:40:35 +00001708 ResType = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Chris Lattner1f3a0632010-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. Spencerb2f376b2010-08-25 18:17:27 +00001716
Chris Lattner1f3a0632010-07-29 21:42:50 +00001717 if (Ty->isIntegralOrEnumerationType() &&
1718 Ty->isPromotableIntegerType())
1719 return ABIArgInfo::getExtend();
1720 }
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001721
Anton Korobeynikov244360d2009-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 Lattner7f4b81a2010-07-29 18:13:09 +00001729 ResType = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 0, Ty, 0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001730 break;
1731 }
1732
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001733 const llvm::Type *HighPart = 0;
Anton Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +00001745
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001746 case Integer:
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001747 ++neededInt;
Chris Lattnerb22f1c82010-07-28 22:44:07 +00001748 // Pick an 8-byte type based on the preferred type.
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001749 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001750
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001751 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1752 return ABIArgInfo::getDirect(HighPart, 8);
Anton Korobeynikov244360d2009-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 Lattnerbe5eb172010-09-01 00:24:35 +00001758 case SSE:
1759 HighPart = GetSSETypeAtOffset(CGT.ConvertTypeRecursive(Ty), 8, Ty, 8);
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001760
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001761 if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
1762 return ABIArgInfo::getDirect(HighPart, 8);
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001763
Anton Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +00001769 // register. This only happens when 128-bit vectors are passed.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001770 case SSEUp:
Chris Lattnerf4ba08a2010-07-28 23:47:21 +00001771 assert(Lo == SSE && "Unexpected SSEUp classification");
Chris Lattner4200fe42010-07-29 04:56:46 +00001772 ResType = Get16ByteVectorType(Ty);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001773 break;
1774 }
1775
Chris Lattnerbe5eb172010-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 Lattnerd426c8e2010-09-01 00:50:20 +00001780 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
Chris Lattnerbe5eb172010-09-01 00:24:35 +00001781
Chris Lattner1f3a0632010-07-29 21:42:50 +00001782 return ABIArgInfo::getDirect(ResType);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001783}
1784
Chris Lattner22326a12010-07-29 02:31:05 +00001785void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00001786
Chris Lattner458b2aa2010-07-29 02:16:43 +00001787 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-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 Lattner029c0f12010-07-29 04:41:05 +00001802 it->info = classifyArgumentType(it->type, neededInt, neededSSE);
Anton Korobeynikov244360d2009-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 Lattner22a931e2010-06-29 06:01:59 +00001812 it->info = getIndirectResult(it->type);
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +00001834 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001835 llvm::ConstantInt::get(CGF.Int32Ty, 15);
Anton Korobeynikov244360d2009-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 Lattner5e016ae2010-06-27 07:15:29 +00001838 CGF.Int64Ty);
1839 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL);
Anton Korobeynikov244360d2009-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 Anderson9793f0e2009-07-29 22:16:19 +00001850 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-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 Anderson41a75022009-08-13 21:57:51 +00001858 llvm::Value *Offset =
Chris Lattner5e016ae2010-06-27 07:15:29 +00001859 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +00001870 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Mike Stump11289f42009-09-09 15:08:12 +00001871
Anton Korobeynikov244360d2009-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. Spencerb2f376b2010-08-25 18:17:27 +00001880
Chris Lattner9723d6c2010-03-11 18:19:55 +00001881 Ty = CGF.getContext().getCanonicalType(Ty);
Chris Lattner029c0f12010-07-29 04:41:05 +00001882 ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
Anton Korobeynikov244360d2009-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 Lattnerd776fb12010-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 Korobeynikov244360d2009-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 Lattnerd776fb12010-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 Korobeynikov244360d2009-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 Lattnerfe34c1d2010-07-29 06:26:06 +00001944 assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
Anton Korobeynikov244360d2009-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 Lattner51e1cc22010-08-26 06:28:35 +00001950 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001951 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001952 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1953 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-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 Sands998f9d92010-02-15 16:14:01 +00001956 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1957 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-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 Anderson170229f2009-07-14 23:10:40 +00001964 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001965 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001966 } else if (neededInt) {
1967 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1968 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001969 llvm::PointerType::getUnqual(LTy));
Chris Lattner0cf24192010-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 Korobeynikov244360d2009-06-05 22:08:42 +00001974 } else {
Chris Lattner0cf24192010-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 Lattnerd776fb12010-06-28 21:43:59 +00001979 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
Chris Lattner0cf24192010-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 Korobeynikov244360d2009-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 Lattner5e016ae2010-06-27 07:15:29 +00002000 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002001 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2002 gp_offset_p);
2003 }
2004 if (neededSSE) {
Chris Lattner5e016ae2010-06-27 07:15:29 +00002005 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-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 Korobeynikov244360d2009-06-05 22:08:42 +00002024 return ResAddr;
2025}
2026
Chris Lattner04dc9572010-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 Lattner0cf24192010-06-28 20:05:43 +00002031
Chris Lattner04dc9572010-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 Lattner0cf24192010-06-28 20:05:43 +00002049
2050//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002051// PIC16 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002052//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002053
2054namespace {
2055
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002056class PIC16ABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002057public:
2058 PIC16ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002059
Chris Lattner458b2aa2010-07-29 02:16:43 +00002060 ABIArgInfo classifyReturnType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002061
Chris Lattner458b2aa2010-07-29 02:16:43 +00002062 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002063
Chris Lattner22326a12010-07-29 02:31:05 +00002064 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002065 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002066 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2067 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002068 it->info = classifyArgumentType(it->type);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002069 }
2070
2071 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2072 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002073};
2074
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002075class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
2076public:
Chris Lattner2b037972010-07-29 02:01:43 +00002077 PIC16TargetCodeGenInfo(CodeGenTypes &CGT)
2078 : TargetCodeGenInfo(new PIC16ABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002079};
2080
Daniel Dunbard59655c2009-09-12 00:59:49 +00002081}
2082
Chris Lattner458b2aa2010-07-29 02:16:43 +00002083ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002084 if (RetTy->isVoidType()) {
2085 return ABIArgInfo::getIgnore();
2086 } else {
2087 return ABIArgInfo::getDirect();
2088 }
2089}
2090
Chris Lattner458b2aa2010-07-29 02:16:43 +00002091ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002092 return ABIArgInfo::getDirect();
2093}
2094
2095llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002096 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00002097 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-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 Korobeynikov244360d2009-06-05 22:08:42 +00002117}
2118
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00002119
John McCallea8d8bb2010-03-11 00:10:12 +00002120// PowerPC-32
2121
2122namespace {
2123class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2124public:
Chris Lattner2b037972010-07-29 02:01:43 +00002125 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002126
John McCallea8d8bb2010-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. Spencerb2f376b2010-08-25 18:17:27 +00002133 llvm::Value *Address) const;
John McCallea8d8bb2010-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 McCall943fae92010-05-27 06:19:26 +00002153 AssignToArrayRange(Builder, Address, Four8, 0, 31);
John McCallea8d8bb2010-03-11 00:10:12 +00002154
2155 // 32-63: fp0-31, the 8-byte floating-point registers
John McCall943fae92010-05-27 06:19:26 +00002156 AssignToArrayRange(Builder, Address, Eight8, 32, 63);
John McCallea8d8bb2010-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 McCall943fae92010-05-27 06:19:26 +00002165 AssignToArrayRange(Builder, Address, Four8, 64, 76);
John McCallea8d8bb2010-03-11 00:10:12 +00002166
2167 // 77-108: v0-31, the 16-byte vector registers
John McCall943fae92010-05-27 06:19:26 +00002168 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
John McCallea8d8bb2010-03-11 00:10:12 +00002169
2170 // 109: vrsave
2171 // 110: vscr
2172 // 111: spe_acc
2173 // 112: spefscr
2174 // 113: sfp
John McCall943fae92010-05-27 06:19:26 +00002175 AssignToArrayRange(Builder, Address, Four8, 109, 113);
John McCallea8d8bb2010-03-11 00:10:12 +00002176
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002177 return false;
John McCallea8d8bb2010-03-11 00:10:12 +00002178}
2179
2180
Chris Lattner0cf24192010-06-28 20:05:43 +00002181//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002182// ARM ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002183//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002184
2185namespace {
2186
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002187class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-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 Lattner2b037972010-07-29 02:01:43 +00002199 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
Daniel Dunbar020daa92009-09-12 01:00:39 +00002200
2201private:
2202 ABIKind getABIKind() const { return Kind; }
2203
Chris Lattner458b2aa2010-07-29 02:16:43 +00002204 ABIArgInfo classifyReturnType(QualType RetTy) const;
2205 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002206
Chris Lattner22326a12010-07-29 02:31:05 +00002207 virtual void computeInfo(CGFunctionInfo &FI) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002208
2209 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2210 CodeGenFunction &CGF) const;
2211};
2212
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002213class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
2214public:
Chris Lattner2b037972010-07-29 02:01:43 +00002215 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2216 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00002217
2218 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2219 return 13;
2220 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002221};
2222
Daniel Dunbard59655c2009-09-12 00:59:49 +00002223}
2224
Chris Lattner22326a12010-07-29 02:31:05 +00002225void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002226 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002227 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Chris Lattner458b2aa2010-07-29 02:16:43 +00002228 it != ie; ++it)
2229 it->info = classifyArgumentType(it->type);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002230
Chris Lattner458b2aa2010-07-29 02:16:43 +00002231 const llvm::Triple &Triple(getContext().Target.getTriple());
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002232 llvm::CallingConv::ID DefaultCC;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002233 if (Triple.getEnvironmentName() == "gnueabi" ||
2234 Triple.getEnvironmentName() == "eabi")
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002235 DefaultCC = llvm::CallingConv::ARM_AAPCS;
Rafael Espindola23a8a062010-06-16 19:01:17 +00002236 else
2237 DefaultCC = llvm::CallingConv::ARM_APCS;
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002238
Daniel Dunbar020daa92009-09-12 01:00:39 +00002239 switch (getABIKind()) {
2240 case APCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002241 if (DefaultCC != llvm::CallingConv::ARM_APCS)
2242 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002243 break;
2244
2245 case AAPCS:
Rafael Espindolaa92c4422010-06-16 16:13:39 +00002246 if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
2247 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
Daniel Dunbar020daa92009-09-12 01:00:39 +00002248 break;
2249
2250 case AAPCS_VFP:
2251 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
2252 break;
2253 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002254}
2255
Chris Lattner458b2aa2010-07-29 02:16:43 +00002256ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002257 if (!isAggregateTypeForABI(Ty)) {
Douglas Gregora71cc152010-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 Korobeynikov18adbf52009-06-06 09:36:29 +00002262 return (Ty->isPromotableIntegerType() ?
2263 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002264 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002265
Daniel Dunbar09d33622009-09-14 21:54:03 +00002266 // Ignore empty records.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002267 if (isEmptyRecord(getContext(), Ty, true))
Daniel Dunbar09d33622009-09-14 21:54:03 +00002268 return ABIArgInfo::getIgnore();
2269
Rafael Espindolabbd44ef2010-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
Daniel Dunbarb34b0802010-09-23 01:54:28 +00002275 // NEON vectors are implemented as (theoretically) opaque structures wrapping
2276 // the underlying vector type. We trust the backend to pass the underlying
2277 // vectors appropriately, so we can unwrap the structs which generally will
2278 // lead to much cleaner IR.
2279 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) {
2280 if (SeltTy->isVectorType())
2281 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
2282 }
2283
2284 // Otherwise, pass by coercing to a structure of the appropriate size.
2285 //
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002286 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
2287 // backend doesn't support byval.
2288 // FIXME: This doesn't handle alignment > 64 bits.
2289 const llvm::Type* ElemTy;
2290 unsigned SizeRegs;
Chris Lattner458b2aa2010-07-29 02:16:43 +00002291 if (getContext().getTypeAlign(Ty) > 32) {
2292 ElemTy = llvm::Type::getInt64Ty(getVMContext());
2293 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002294 } else {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002295 ElemTy = llvm::Type::getInt32Ty(getVMContext());
2296 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002297 }
2298 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00002299 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Chris Lattner458b2aa2010-07-29 02:16:43 +00002300 const llvm::Type* STy = llvm::StructType::get(getVMContext(), LLVMFields,
2301 true);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002302 return ABIArgInfo::getDirect(STy);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002303}
2304
Chris Lattner458b2aa2010-07-29 02:16:43 +00002305static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002306 llvm::LLVMContext &VMContext) {
2307 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
2308 // is called integer-like if its size is less than or equal to one word, and
2309 // the offset of each of its addressable sub-fields is zero.
2310
2311 uint64_t Size = Context.getTypeSize(Ty);
2312
2313 // Check that the type fits in a word.
2314 if (Size > 32)
2315 return false;
2316
2317 // FIXME: Handle vector types!
2318 if (Ty->isVectorType())
2319 return false;
2320
Daniel Dunbard53bac72009-09-14 02:20:34 +00002321 // Float types are never treated as "integer like".
2322 if (Ty->isRealFloatingType())
2323 return false;
2324
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002325 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00002326 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002327 return true;
2328
Daniel Dunbar96ebba52010-02-01 23:31:26 +00002329 // Small complex integer types are "integer like".
2330 if (const ComplexType *CT = Ty->getAs<ComplexType>())
2331 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002332
2333 // Single element and zero sized arrays should be allowed, by the definition
2334 // above, but they are not.
2335
2336 // Otherwise, it must be a record type.
2337 const RecordType *RT = Ty->getAs<RecordType>();
2338 if (!RT) return false;
2339
2340 // Ignore records with flexible arrays.
2341 const RecordDecl *RD = RT->getDecl();
2342 if (RD->hasFlexibleArrayMember())
2343 return false;
2344
2345 // Check that all sub-fields are at offset 0, and are themselves "integer
2346 // like".
2347 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2348
2349 bool HadField = false;
2350 unsigned idx = 0;
2351 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2352 i != e; ++i, ++idx) {
2353 const FieldDecl *FD = *i;
2354
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002355 // Bit-fields are not addressable, we only need to verify they are "integer
2356 // like". We still have to disallow a subsequent non-bitfield, for example:
2357 // struct { int : 0; int x }
2358 // is non-integer like according to gcc.
2359 if (FD->isBitField()) {
2360 if (!RD->isUnion())
2361 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002362
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002363 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2364 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002365
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002366 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002367 }
2368
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002369 // Check if this field is at offset 0.
2370 if (Layout.getFieldOffset(idx) != 0)
2371 return false;
2372
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002373 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2374 return false;
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002375
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00002376 // Only allow at most one field in a structure. This doesn't match the
2377 // wording above, but follows gcc in situations with a field following an
2378 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002379 if (!RD->isUnion()) {
2380 if (HadField)
2381 return false;
2382
2383 HadField = true;
2384 }
2385 }
2386
2387 return true;
2388}
2389
Chris Lattner458b2aa2010-07-29 02:16:43 +00002390ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002391 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002392 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002393
Daniel Dunbar19964db2010-09-23 01:54:32 +00002394 // Large vector types should be returned via memory.
2395 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2396 return ABIArgInfo::getIndirect(0);
2397
John McCalla1dee5302010-08-22 10:59:02 +00002398 if (!isAggregateTypeForABI(RetTy)) {
Douglas Gregora71cc152010-02-02 20:10:50 +00002399 // Treat an enum type as its underlying type.
2400 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2401 RetTy = EnumTy->getDecl()->getIntegerType();
2402
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002403 return (RetTy->isPromotableIntegerType() ?
2404 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00002405 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002406
Rafael Espindolabbd44ef2010-06-08 02:42:08 +00002407 // Structures with either a non-trivial destructor or a non-trivial
2408 // copy constructor are always indirect.
2409 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
2410 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2411
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002412 // Are we following APCS?
2413 if (getABIKind() == APCS) {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002414 if (isEmptyRecord(getContext(), RetTy, false))
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002415 return ABIArgInfo::getIgnore();
2416
Daniel Dunbareedf1512010-02-01 23:31:19 +00002417 // Complex types are all returned as packed integers.
2418 //
2419 // FIXME: Consider using 2 x vector types if the back end handles them
2420 // correctly.
2421 if (RetTy->isAnyComplexType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002422 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
Chris Lattner458b2aa2010-07-29 02:16:43 +00002423 getContext().getTypeSize(RetTy)));
Daniel Dunbareedf1512010-02-01 23:31:19 +00002424
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002425 // Integer like structures are returned in r0.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002426 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002427 // Return in the smallest viable integer type.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002428 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002429 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002430 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002431 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002432 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2433 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002434 }
2435
2436 // Otherwise return in memory.
2437 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002438 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002439
2440 // Otherwise this is an AAPCS variant.
2441
Chris Lattner458b2aa2010-07-29 02:16:43 +00002442 if (isEmptyRecord(getContext(), RetTy, true))
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002443 return ABIArgInfo::getIgnore();
2444
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002445 // Aggregates <= 4 bytes are returned in r0; other aggregates
2446 // are returned indirectly.
Chris Lattner458b2aa2010-07-29 02:16:43 +00002447 uint64_t Size = getContext().getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002448 if (Size <= 32) {
2449 // Return in the smallest viable integer type.
2450 if (Size <= 8)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002451 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002452 if (Size <= 16)
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00002453 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2454 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00002455 }
2456
Daniel Dunbar626f1d82009-09-13 08:03:58 +00002457 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002458}
2459
2460llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
Chris Lattner5e016ae2010-06-27 07:15:29 +00002461 CodeGenFunction &CGF) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002462 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002463 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002464 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002465
2466 CGBuilderTy &Builder = CGF.Builder;
2467 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2468 "ap");
2469 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2470 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002471 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002472 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2473
2474 uint64_t Offset =
2475 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2476 llvm::Value *NextAddr =
Chris Lattner5e016ae2010-06-27 07:15:29 +00002477 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002478 "ap.next");
2479 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2480
2481 return AddrTyped;
2482}
2483
Chris Lattner458b2aa2010-07-29 02:16:43 +00002484ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
2485 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002486 return ABIArgInfo::getIgnore();
Douglas Gregora71cc152010-02-02 20:10:50 +00002487
John McCalla1dee5302010-08-22 10:59:02 +00002488 if (isAggregateTypeForABI(RetTy))
Chris Lattner458b2aa2010-07-29 02:16:43 +00002489 return ABIArgInfo::getIndirect(0);
2490
2491 // Treat an enum type as its underlying type.
2492 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2493 RetTy = EnumTy->getDecl()->getIntegerType();
2494
2495 return (RetTy->isPromotableIntegerType() ?
2496 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002497}
2498
Chris Lattner0cf24192010-06-28 20:05:43 +00002499//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002500// SystemZ ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002501//===----------------------------------------------------------------------===//
Daniel Dunbard59655c2009-09-12 00:59:49 +00002502
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002503namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002504
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002505class SystemZABIInfo : public ABIInfo {
Chris Lattner2b037972010-07-29 02:01:43 +00002506public:
2507 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2508
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002509 bool isPromotableIntegerType(QualType Ty) const;
2510
Chris Lattner458b2aa2010-07-29 02:16:43 +00002511 ABIArgInfo classifyReturnType(QualType RetTy) const;
2512 ABIArgInfo classifyArgumentType(QualType RetTy) const;
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002513
Chris Lattner22326a12010-07-29 02:31:05 +00002514 virtual void computeInfo(CGFunctionInfo &FI) const {
Chris Lattner458b2aa2010-07-29 02:16:43 +00002515 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002516 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2517 it != ie; ++it)
Chris Lattner458b2aa2010-07-29 02:16:43 +00002518 it->info = classifyArgumentType(it->type);
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002519 }
2520
2521 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2522 CodeGenFunction &CGF) const;
2523};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002524
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002525class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2526public:
Chris Lattner2b037972010-07-29 02:01:43 +00002527 SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
2528 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002529};
2530
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002531}
2532
2533bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2534 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002535 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002536 switch (BT->getKind()) {
2537 case BuiltinType::Bool:
2538 case BuiltinType::Char_S:
2539 case BuiltinType::Char_U:
2540 case BuiltinType::SChar:
2541 case BuiltinType::UChar:
2542 case BuiltinType::Short:
2543 case BuiltinType::UShort:
2544 case BuiltinType::Int:
2545 case BuiltinType::UInt:
2546 return true;
2547 default:
2548 return false;
2549 }
2550 return false;
2551}
2552
2553llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2554 CodeGenFunction &CGF) const {
2555 // FIXME: Implement
2556 return 0;
2557}
2558
2559
Chris Lattner458b2aa2010-07-29 02:16:43 +00002560ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
2561 if (RetTy->isVoidType())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002562 return ABIArgInfo::getIgnore();
John McCalla1dee5302010-08-22 10:59:02 +00002563 if (isAggregateTypeForABI(RetTy))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002564 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002565
2566 return (isPromotableIntegerType(RetTy) ?
2567 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002568}
2569
Chris Lattner458b2aa2010-07-29 02:16:43 +00002570ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
John McCalla1dee5302010-08-22 10:59:02 +00002571 if (isAggregateTypeForABI(Ty))
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002572 return ABIArgInfo::getIndirect(0);
Chris Lattner458b2aa2010-07-29 02:16:43 +00002573
2574 return (isPromotableIntegerType(Ty) ?
2575 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002576}
2577
Chris Lattner0cf24192010-06-28 20:05:43 +00002578//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002579// MSP430 ABI Implementation
Chris Lattner0cf24192010-06-28 20:05:43 +00002580//===----------------------------------------------------------------------===//
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002581
2582namespace {
2583
2584class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2585public:
Chris Lattner2b037972010-07-29 02:01:43 +00002586 MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
2587 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002588 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2589 CodeGen::CodeGenModule &M) const;
2590};
2591
2592}
2593
2594void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2595 llvm::GlobalValue *GV,
2596 CodeGen::CodeGenModule &M) const {
2597 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2598 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2599 // Handle 'interrupt' attribute:
2600 llvm::Function *F = cast<llvm::Function>(GV);
2601
2602 // Step 1: Set ISR calling convention.
2603 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2604
2605 // Step 2: Add attributes goodness.
2606 F->addFnAttr(llvm::Attribute::NoInline);
2607
2608 // Step 3: Emit ISR vector alias.
2609 unsigned Num = attr->getNumber() + 0xffe0;
2610 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2611 "vector_" +
2612 llvm::LowercaseString(llvm::utohexstr(Num)),
2613 GV, &M.getModule());
2614 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002615 }
2616}
2617
Chris Lattner0cf24192010-06-28 20:05:43 +00002618//===----------------------------------------------------------------------===//
John McCall943fae92010-05-27 06:19:26 +00002619// MIPS ABI Implementation. This works for both little-endian and
2620// big-endian variants.
Chris Lattner0cf24192010-06-28 20:05:43 +00002621//===----------------------------------------------------------------------===//
2622
John McCall943fae92010-05-27 06:19:26 +00002623namespace {
2624class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
2625public:
Chris Lattner2b037972010-07-29 02:01:43 +00002626 MIPSTargetCodeGenInfo(CodeGenTypes &CGT)
2627 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
John McCall943fae92010-05-27 06:19:26 +00002628
2629 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
2630 return 29;
2631 }
2632
2633 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
Michael J. Spencerb2f376b2010-08-25 18:17:27 +00002634 llvm::Value *Address) const;
John McCall943fae92010-05-27 06:19:26 +00002635};
2636}
2637
2638bool
2639MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2640 llvm::Value *Address) const {
2641 // This information comes from gcc's implementation, which seems to
2642 // as canonical as it gets.
2643
2644 CodeGen::CGBuilderTy &Builder = CGF.Builder;
2645 llvm::LLVMContext &Context = CGF.getLLVMContext();
2646
2647 // Everything on MIPS is 4 bytes. Double-precision FP registers
2648 // are aliased to pairs of single-precision FP registers.
2649 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
2650 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2651
2652 // 0-31 are the general purpose registers, $0 - $31.
2653 // 32-63 are the floating-point registers, $f0 - $f31.
2654 // 64 and 65 are the multiply/divide registers, $hi and $lo.
2655 // 66 is the (notional, I think) register for signal-handler return.
2656 AssignToArrayRange(Builder, Address, Four8, 0, 65);
2657
2658 // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
2659 // They are one bit wide and ignored here.
2660
2661 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
2662 // (coprocessor 1 is the FP unit)
2663 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
2664 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
2665 // 176-181 are the DSP accumulator registers.
2666 AssignToArrayRange(Builder, Address, Four8, 80, 181);
2667
2668 return false;
2669}
2670
2671
Chris Lattner2b037972010-07-29 02:01:43 +00002672const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002673 if (TheTargetCodeGenInfo)
2674 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002675
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002676 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2677 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002678
Chris Lattner22a931e2010-06-29 06:01:59 +00002679 const llvm::Triple &Triple = getContext().Target.getTriple();
Daniel Dunbar40165182009-08-24 09:10:05 +00002680 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002681 default:
Chris Lattner2b037972010-07-29 02:01:43 +00002682 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002683
John McCall943fae92010-05-27 06:19:26 +00002684 case llvm::Triple::mips:
2685 case llvm::Triple::mipsel:
Chris Lattner2b037972010-07-29 02:01:43 +00002686 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types));
John McCall943fae92010-05-27 06:19:26 +00002687
Daniel Dunbard59655c2009-09-12 00:59:49 +00002688 case llvm::Triple::arm:
2689 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002690 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002691 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002692 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002693 new ARMTargetCodeGenInfo(Types, ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002694
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002695 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002696 new ARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002697
2698 case llvm::Triple::pic16:
Chris Lattner2b037972010-07-29 02:01:43 +00002699 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002700
John McCallea8d8bb2010-03-11 00:10:12 +00002701 case llvm::Triple::ppc:
Chris Lattner2b037972010-07-29 02:01:43 +00002702 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
John McCallea8d8bb2010-03-11 00:10:12 +00002703
Daniel Dunbard59655c2009-09-12 00:59:49 +00002704 case llvm::Triple::systemz:
Chris Lattner2b037972010-07-29 02:01:43 +00002705 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002706
2707 case llvm::Triple::msp430:
Chris Lattner2b037972010-07-29 02:01:43 +00002708 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002709
Daniel Dunbar40165182009-08-24 09:10:05 +00002710 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002711 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002712 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002713 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002714 new X86_32TargetCodeGenInfo(Types, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002715 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002716 case llvm::Triple::MinGW32:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002717 case llvm::Triple::AuroraUX:
2718 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002719 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002720 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002721 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002722 new X86_32TargetCodeGenInfo(Types, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002723
2724 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002725 return *(TheTargetCodeGenInfo =
Chris Lattner2b037972010-07-29 02:01:43 +00002726 new X86_32TargetCodeGenInfo(Types, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002727 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002728
Daniel Dunbare3532f82009-08-24 08:52:16 +00002729 case llvm::Triple::x86_64:
Chris Lattner04dc9572010-08-31 16:44:54 +00002730 switch (Triple.getOS()) {
2731 case llvm::Triple::Win32:
2732 case llvm::Triple::MinGW64:
2733 case llvm::Triple::Cygwin:
2734 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
2735 default:
2736 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types));
2737 }
Daniel Dunbare3532f82009-08-24 08:52:16 +00002738 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002739}