blob: c0b5f655fa6f2348f3b73cdf2c8265f683cb257f [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"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3532f82009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar7230fa52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov244360d2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
26ABIInfo::~ABIInfo() {}
27
28void ABIArgInfo::dump() const {
Daniel Dunbar7230fa52009-12-03 09:13:49 +000029 llvm::raw_ostream &OS = llvm::errs();
30 OS << "(ABIArgInfo Kind=";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000031 switch (TheKind) {
32 case Direct:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000033 OS << "Direct";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000034 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000035 case Extend:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000036 OS << "Extend";
Anton Korobeynikov18adbf52009-06-06 09:36:29 +000037 break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +000038 case Ignore:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000039 OS << "Ignore";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000040 break;
41 case Coerce:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000042 OS << "Coerce Type=";
43 getCoerceToType()->print(OS);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000044 break;
45 case Indirect:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000046 OS << "Indirect Align=" << getIndirectAlign();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000047 break;
48 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000049 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000050 break;
51 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000052 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000053}
54
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000055TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
56
Daniel Dunbar626f1d82009-09-13 08:03:58 +000057static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000058
59/// isEmptyField - Return true iff a the field is "empty", that is it
60/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000061static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
62 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000063 if (FD->isUnnamedBitfield())
64 return true;
65
66 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000067
Daniel Dunbar626f1d82009-09-13 08:03:58 +000068 // Constant arrays of empty records count as empty, strip them off.
69 if (AllowArrays)
70 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
71 FT = AT->getElementType();
72
73 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000074}
75
76/// isEmptyRecord - Return true iff a structure contains only empty
77/// fields. Note that a structure with a flexible array member is not
78/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +000079static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +000080 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000081 if (!RT)
82 return 0;
83 const RecordDecl *RD = RT->getDecl();
84 if (RD->hasFlexibleArrayMember())
85 return false;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000086 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
87 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +000088 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +000089 return false;
90 return true;
91}
92
Anders Carlsson20759ad2009-09-16 15:53:40 +000093/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
94/// a non-trivial destructor or a non-trivial copy constructor.
95static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
96 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
97 if (!RD)
98 return false;
99
100 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
101}
102
103/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
104/// a record type with either a non-trivial destructor or a non-trivial copy
105/// constructor.
106static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
107 const RecordType *RT = T->getAs<RecordType>();
108 if (!RT)
109 return false;
110
111 return hasNonTrivialDestructorOrCopyConstructor(RT);
112}
113
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000114/// isSingleElementStruct - Determine if a structure is a "single
115/// element struct", i.e. it has exactly one non-empty field or
116/// exactly one field which is itself a single element
117/// struct. Structures with flexible array members are never
118/// considered single element structs.
119///
120/// \return The field declaration for the single non-empty field, if
121/// it exists.
122static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
123 const RecordType *RT = T->getAsStructureType();
124 if (!RT)
125 return 0;
126
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return 0;
130
131 const Type *Found = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000132 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000134 const FieldDecl *FD = *i;
135 QualType FT = FD->getType();
136
137 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000138 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000139 continue;
140
141 // If we already found an element then this isn't a single-element
142 // struct.
143 if (Found)
144 return 0;
145
146 // Treat single element arrays as the element.
147 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
148 if (AT->getSize().getZExtValue() != 1)
149 break;
150 FT = AT->getElementType();
151 }
152
153 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
154 Found = FT.getTypePtr();
155 } else {
156 Found = isSingleElementStruct(FT, Context);
157 if (!Found)
158 return 0;
159 }
160 }
161
162 return Found;
163}
164
165static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000166 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
167 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
168 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000169 return false;
170
171 uint64_t Size = Context.getTypeSize(Ty);
172 return Size == 32 || Size == 64;
173}
174
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000175/// canExpandIndirectArgument - Test whether an argument type which is to be
176/// passed indirectly (on the stack) would have the equivalent layout if it was
177/// expanded into separate arguments. If so, we prefer to do the latter to avoid
178/// inhibiting optimizations.
179///
180// FIXME: This predicate is missing many cases, currently it just follows
181// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
182// should probably make this smarter, or better yet make the LLVM backend
183// capable of handling it.
184static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
185 // We can only expand structure types.
186 const RecordType *RT = Ty->getAs<RecordType>();
187 if (!RT)
188 return false;
189
190 // We can only expand (C) structures.
191 //
192 // FIXME: This needs to be generalized to handle classes as well.
193 const RecordDecl *RD = RT->getDecl();
194 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
195 return false;
196
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
198 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000199 const FieldDecl *FD = *i;
200
201 if (!is32Or64BitBasicType(FD->getType(), Context))
202 return false;
203
204 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
205 // how to expand them yet, and the predicate for telling if a bitfield still
206 // counts as "basic" is more complicated than what we were doing previously.
207 if (FD->isBitField())
208 return false;
209 }
210
211 return true;
212}
213
Eli Friedman3192cc82009-06-13 21:37:10 +0000214static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000215 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
216 i != e; ++i) {
Eli Friedman3192cc82009-06-13 21:37:10 +0000217 const FieldDecl *FD = *i;
218
219 if (FD->getType()->isVectorType() &&
220 Context.getTypeSize(FD->getType()) >= 128)
221 return true;
222
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000223 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000224 if (typeContainsSSEVector(RT->getDecl(), Context))
225 return true;
226 }
227
228 return false;
229}
230
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000231namespace {
232/// DefaultABIInfo - The default implementation for ABI specific
233/// details. This implementation provides information which results in
234/// self-consistent and sensible LLVM IR generation, but does not
235/// conform to any particular ABI.
236class DefaultABIInfo : public ABIInfo {
237 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000238 ASTContext &Context,
239 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000240
241 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000242 ASTContext &Context,
243 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000244
Owen Anderson170229f2009-07-14 23:10:40 +0000245 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
246 llvm::LLVMContext &VMContext) const {
247 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
248 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000249 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
250 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000251 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000252 }
253
254 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
255 CodeGenFunction &CGF) const;
256};
257
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000258class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
259public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000260 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000261};
262
263llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
264 CodeGenFunction &CGF) const {
265 return 0;
266}
267
268ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
269 ASTContext &Context,
270 llvm::LLVMContext &VMContext) const {
271 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
272 return ABIArgInfo::getIndirect(0);
273 } else {
274 return (Ty->isPromotableIntegerType() ?
275 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
276 }
277}
278
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000279/// X86_32ABIInfo - The X86-32 ABI information.
280class X86_32ABIInfo : public ABIInfo {
281 ASTContext &Context;
David Chisnallde3a0692009-08-17 23:08:21 +0000282 bool IsDarwinVectorABI;
283 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000284
285 static bool isRegisterSize(unsigned Size) {
286 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
287 }
288
289 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
290
Eli Friedman3192cc82009-06-13 21:37:10 +0000291 static unsigned getIndirectArgumentAlignment(QualType Ty,
292 ASTContext &Context);
293
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000294public:
295 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000296 ASTContext &Context,
297 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000298
299 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000300 ASTContext &Context,
301 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000302
Owen Anderson170229f2009-07-14 23:10:40 +0000303 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
304 llvm::LLVMContext &VMContext) const {
305 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
306 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000307 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
308 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000309 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000310 }
311
312 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
313 CodeGenFunction &CGF) const;
314
David Chisnallde3a0692009-08-17 23:08:21 +0000315 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000316 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000317 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000318};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000319
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000320class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
321public:
322 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor0599df12010-01-22 15:41:14 +0000323 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000324};
325
326}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000327
328/// shouldReturnTypeInRegister - Determine if the given type should be
329/// passed in a register (for the Darwin ABI).
330bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
331 ASTContext &Context) {
332 uint64_t Size = Context.getTypeSize(Ty);
333
334 // Type must be register sized.
335 if (!isRegisterSize(Size))
336 return false;
337
338 if (Ty->isVectorType()) {
339 // 64- and 128- bit vectors inside structures are not returned in
340 // registers.
341 if (Size == 64 || Size == 128)
342 return false;
343
344 return true;
345 }
346
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000347 // If this is a builtin, pointer, enum, or complex type, it is ok.
348 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
349 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
350 Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000351 return true;
352
353 // Arrays are treated like records.
354 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
355 return shouldReturnTypeInRegister(AT->getElementType(), Context);
356
357 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000358 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000359 if (!RT) return false;
360
Anders Carlsson40446e82010-01-27 03:25:19 +0000361 // FIXME: Traverse bases here too.
362
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000363 // Structure types are passed in register if all fields would be
364 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000365 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
366 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000367 const FieldDecl *FD = *i;
368
369 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000370 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000371 continue;
372
373 // Check fields recursively.
374 if (!shouldReturnTypeInRegister(FD->getType(), Context))
375 return false;
376 }
377
378 return true;
379}
380
381ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000382 ASTContext &Context,
383 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000384 if (RetTy->isVoidType()) {
385 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000386 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000387 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000388 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000389 uint64_t Size = Context.getTypeSize(RetTy);
390
391 // 128-bit vectors are a special case; they are returned in
392 // registers and we need to make sure to pick a type the LLVM
393 // backend will like.
394 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000395 return ABIArgInfo::getCoerce(llvm::VectorType::get(
396 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000397
398 // Always return in register if it fits in a general purpose
399 // register, or if it is 64 bits and has a single element.
400 if ((Size == 8 || Size == 16 || Size == 32) ||
401 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000402 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000403
404 return ABIArgInfo::getIndirect(0);
405 }
406
407 return ABIArgInfo::getDirect();
408 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000409 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000410 // Structures with either a non-trivial destructor or a non-trivial
411 // copy constructor are always indirect.
412 if (hasNonTrivialDestructorOrCopyConstructor(RT))
413 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
414
415 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000416 if (RT->getDecl()->hasFlexibleArrayMember())
417 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000418 }
419
David Chisnallde3a0692009-08-17 23:08:21 +0000420 // If specified, structs and unions are always indirect.
421 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000422 return ABIArgInfo::getIndirect(0);
423
424 // Classify "single element" structs as their element type.
425 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000426 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000427 if (BT->isIntegerType()) {
428 // We need to use the size of the structure, padding
429 // bit-fields can adjust that to be larger than the single
430 // element type.
431 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000432 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000433 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000434 } else if (BT->getKind() == BuiltinType::Float) {
435 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
436 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000437 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000438 } else if (BT->getKind() == BuiltinType::Double) {
439 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
440 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000441 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000442 }
443 } else if (SeltTy->isPointerType()) {
444 // FIXME: It would be really nice if this could come out as the proper
445 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000446 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000447 return ABIArgInfo::getCoerce(PtrTy);
448 } else if (SeltTy->isVectorType()) {
449 // 64- and 128-bit vectors are never returned in a
450 // register when inside a structure.
451 uint64_t Size = Context.getTypeSize(RetTy);
452 if (Size == 64 || Size == 128)
453 return ABIArgInfo::getIndirect(0);
454
Owen Anderson170229f2009-07-14 23:10:40 +0000455 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000456 }
457 }
458
459 // Small structures which are register sized are generally returned
460 // in a register.
461 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
462 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000463 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000464 }
465
466 return ABIArgInfo::getIndirect(0);
467 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000468 return (RetTy->isPromotableIntegerType() ?
469 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000470 }
471}
472
Eli Friedman3192cc82009-06-13 21:37:10 +0000473unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
474 ASTContext &Context) {
475 unsigned Align = Context.getTypeAlign(Ty);
476 if (Align < 128) return 0;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000477 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000478 if (typeContainsSSEVector(RT->getDecl(), Context))
479 return 16;
480 return 0;
481}
482
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000483ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000484 ASTContext &Context,
485 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000486 // FIXME: Set alignment on indirect arguments.
487 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
488 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000489 if (const RecordType *RT = Ty->getAs<RecordType>()) {
490 // Structures with either a non-trivial destructor or a non-trivial
491 // copy constructor are always indirect.
492 if (hasNonTrivialDestructorOrCopyConstructor(RT))
493 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
494
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000495 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump11289f42009-09-09 15:08:12 +0000496 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedman3192cc82009-06-13 21:37:10 +0000497 Context));
Anders Carlsson40446e82010-01-27 03:25:19 +0000498 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000499
500 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000501 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000502 return ABIArgInfo::getIgnore();
503
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000504 // Expand small (<= 128-bit) record types when we know that the stack layout
505 // of those arguments will match the struct. This is important because the
506 // LLVM backend isn't smart enough to remove byval, which inhibits many
507 // optimizations.
508 if (Context.getTypeSize(Ty) <= 4*32 &&
509 canExpandIndirectArgument(Ty, Context))
510 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000511
Eli Friedman3192cc82009-06-13 21:37:10 +0000512 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000513 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000514 return (Ty->isPromotableIntegerType() ?
515 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000516 }
517}
518
519llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
520 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000521 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000522 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000523
524 CGBuilderTy &Builder = CGF.Builder;
525 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
526 "ap");
527 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
528 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000529 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000530 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
531
532 uint64_t Offset =
533 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
534 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +0000535 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
536 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000537 "ap.next");
538 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
539
540 return AddrTyped;
541}
542
543namespace {
544/// X86_64ABIInfo - The X86_64 ABI information.
545class X86_64ABIInfo : public ABIInfo {
546 enum Class {
547 Integer = 0,
548 SSE,
549 SSEUp,
550 X87,
551 X87Up,
552 ComplexX87,
553 NoClass,
554 Memory
555 };
556
557 /// merge - Implement the X86_64 ABI merging algorithm.
558 ///
559 /// Merge an accumulating classification \arg Accum with a field
560 /// classification \arg Field.
561 ///
562 /// \param Accum - The accumulating classification. This should
563 /// always be either NoClass or the result of a previous merge
564 /// call. In addition, this should never be Memory (the caller
565 /// should just return Memory for the aggregate).
566 Class merge(Class Accum, Class Field) const;
567
568 /// classify - Determine the x86_64 register classes in which the
569 /// given type T should be passed.
570 ///
571 /// \param Lo - The classification for the parts of the type
572 /// residing in the low word of the containing object.
573 ///
574 /// \param Hi - The classification for the parts of the type
575 /// residing in the high word of the containing object.
576 ///
577 /// \param OffsetBase - The bit offset of this type in the
578 /// containing object. Some parameters are classified different
579 /// depending on whether they straddle an eightbyte boundary.
580 ///
581 /// If a word is unused its result will be NoClass; if a type should
582 /// be passed in Memory then at least the classification of \arg Lo
583 /// will be Memory.
584 ///
585 /// The \arg Lo class will be NoClass iff the argument is ignored.
586 ///
587 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
588 /// also be ComplexX87.
589 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
590 Class &Lo, Class &Hi) const;
591
592 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
593 /// to coerce to, chose the best way to pass Ty in the same place
594 /// that \arg CoerceTo would be passed, but while keeping the
595 /// emitted code as simple as possible.
596 ///
597 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
598 /// the ways we might want to pass things, instead of constructing an LLVM
599 /// type. This makes this code more explicit, and it makes it clearer that we
600 /// are also doing this for correctness in the case of passing scalar types.
601 ABIArgInfo getCoerceResult(QualType Ty,
602 const llvm::Type *CoerceTo,
603 ASTContext &Context) const;
604
605 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
606 /// such that the argument will be passed in memory.
607 ABIArgInfo getIndirectResult(QualType Ty,
608 ASTContext &Context) const;
609
610 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000611 ASTContext &Context,
612 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000613
614 ABIArgInfo classifyArgumentType(QualType Ty,
615 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000616 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000617 unsigned &neededInt,
618 unsigned &neededSSE) const;
619
620public:
Owen Anderson170229f2009-07-14 23:10:40 +0000621 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
622 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000623
624 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
625 CodeGenFunction &CGF) const;
626};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000627
628class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
629public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000630 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000631};
632
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000633}
634
635X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
636 Class Field) const {
637 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
638 // classified recursively so that always two fields are
639 // considered. The resulting class is calculated according to
640 // the classes of the fields in the eightbyte:
641 //
642 // (a) If both classes are equal, this is the resulting class.
643 //
644 // (b) If one of the classes is NO_CLASS, the resulting class is
645 // the other class.
646 //
647 // (c) If one of the classes is MEMORY, the result is the MEMORY
648 // class.
649 //
650 // (d) If one of the classes is INTEGER, the result is the
651 // INTEGER.
652 //
653 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
654 // MEMORY is used as class.
655 //
656 // (f) Otherwise class SSE is used.
657
658 // Accum should never be memory (we should have returned) or
659 // ComplexX87 (because this cannot be passed in a structure).
660 assert((Accum != Memory && Accum != ComplexX87) &&
661 "Invalid accumulated classification during merge.");
662 if (Accum == Field || Field == NoClass)
663 return Accum;
664 else if (Field == Memory)
665 return Memory;
666 else if (Accum == NoClass)
667 return Field;
668 else if (Accum == Integer || Field == Integer)
669 return Integer;
670 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
671 Accum == X87 || Accum == X87Up)
672 return Memory;
673 else
674 return SSE;
675}
676
677void X86_64ABIInfo::classify(QualType Ty,
678 ASTContext &Context,
679 uint64_t OffsetBase,
680 Class &Lo, Class &Hi) const {
681 // FIXME: This code can be simplified by introducing a simple value class for
682 // Class pairs with appropriate constructor methods for the various
683 // situations.
684
685 // FIXME: Some of the split computations are wrong; unaligned vectors
686 // shouldn't be passed in registers for example, so there is no chance they
687 // can straddle an eightbyte. Verify & simplify.
688
689 Lo = Hi = NoClass;
690
691 Class &Current = OffsetBase < 64 ? Lo : Hi;
692 Current = Memory;
693
John McCall9dd450b2009-09-21 23:43:11 +0000694 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000695 BuiltinType::Kind k = BT->getKind();
696
697 if (k == BuiltinType::Void) {
698 Current = NoClass;
699 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
700 Lo = Integer;
701 Hi = Integer;
702 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
703 Current = Integer;
704 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
705 Current = SSE;
706 } else if (k == BuiltinType::LongDouble) {
707 Lo = X87;
708 Hi = X87Up;
709 }
710 // FIXME: _Decimal32 and _Decimal64 are SSE.
711 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall9dd450b2009-09-21 23:43:11 +0000712 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000713 // Classify the underlying integer type.
714 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
715 } else if (Ty->hasPointerRepresentation()) {
716 Current = Integer;
John McCall9dd450b2009-09-21 23:43:11 +0000717 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000718 uint64_t Size = Context.getTypeSize(VT);
719 if (Size == 32) {
720 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
721 // float> as integer.
722 Current = Integer;
723
724 // If this type crosses an eightbyte boundary, it should be
725 // split.
726 uint64_t EB_Real = (OffsetBase) / 64;
727 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
728 if (EB_Real != EB_Imag)
729 Hi = Lo;
730 } else if (Size == 64) {
731 // gcc passes <1 x double> in memory. :(
732 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
733 return;
734
735 // gcc passes <1 x long long> as INTEGER.
736 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
737 Current = Integer;
738 else
739 Current = SSE;
740
741 // If this type crosses an eightbyte boundary, it should be
742 // split.
743 if (OffsetBase && OffsetBase != 64)
744 Hi = Lo;
745 } else if (Size == 128) {
746 Lo = SSE;
747 Hi = SSEUp;
748 }
John McCall9dd450b2009-09-21 23:43:11 +0000749 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000750 QualType ET = Context.getCanonicalType(CT->getElementType());
751
752 uint64_t Size = Context.getTypeSize(Ty);
753 if (ET->isIntegralType()) {
754 if (Size <= 64)
755 Current = Integer;
756 else if (Size <= 128)
757 Lo = Hi = Integer;
758 } else if (ET == Context.FloatTy)
759 Current = SSE;
760 else if (ET == Context.DoubleTy)
761 Lo = Hi = SSE;
762 else if (ET == Context.LongDoubleTy)
763 Current = ComplexX87;
764
765 // If this complex type crosses an eightbyte boundary then it
766 // should be split.
767 uint64_t EB_Real = (OffsetBase) / 64;
768 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
769 if (Hi == NoClass && EB_Real != EB_Imag)
770 Hi = Lo;
771 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
772 // Arrays are treated like structures.
773
774 uint64_t Size = Context.getTypeSize(Ty);
775
776 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
777 // than two eightbytes, ..., it has class MEMORY.
778 if (Size > 128)
779 return;
780
781 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
782 // fields, it has class MEMORY.
783 //
784 // Only need to check alignment of array base.
785 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
786 return;
787
788 // Otherwise implement simplified merge. We could be smarter about
789 // this, but it isn't worth it and would be harder to verify.
790 Current = NoClass;
791 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
792 uint64_t ArraySize = AT->getSize().getZExtValue();
793 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
794 Class FieldLo, FieldHi;
795 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
796 Lo = merge(Lo, FieldLo);
797 Hi = merge(Hi, FieldHi);
798 if (Lo == Memory || Hi == Memory)
799 break;
800 }
801
802 // Do post merger cleanup (see below). Only case we worry about is Memory.
803 if (Hi == Memory)
804 Lo = Memory;
805 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000806 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000807 uint64_t Size = Context.getTypeSize(Ty);
808
809 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
810 // than two eightbytes, ..., it has class MEMORY.
811 if (Size > 128)
812 return;
813
Anders Carlsson20759ad2009-09-16 15:53:40 +0000814 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
815 // copy constructor or a non-trivial destructor, it is passed by invisible
816 // reference.
817 if (hasNonTrivialDestructorOrCopyConstructor(RT))
818 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000819
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000820 const RecordDecl *RD = RT->getDecl();
821
822 // Assume variable sized types are passed in memory.
823 if (RD->hasFlexibleArrayMember())
824 return;
825
826 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
827
828 // Reset Lo class, this will be recomputed.
829 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000830
831 // If this is a C++ record, classify the bases first.
832 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
833 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
834 e = CXXRD->bases_end(); i != e; ++i) {
835 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
836 "Unexpected base class!");
837 const CXXRecordDecl *Base =
838 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
839
840 // Classify this field.
841 //
842 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
843 // single eightbyte, each is classified separately. Each eightbyte gets
844 // initialized to class NO_CLASS.
845 Class FieldLo, FieldHi;
846 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
847 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
848 Lo = merge(Lo, FieldLo);
849 Hi = merge(Hi, FieldHi);
850 if (Lo == Memory || Hi == Memory)
851 break;
852 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +0000853
854 // If this record has no fields but isn't empty, classify as INTEGER.
855 if (RD->field_empty() && Size)
856 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000857 }
858
859 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000860 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000861 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
862 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000863 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
864 bool BitField = i->isBitField();
865
866 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
867 // fields, it has class MEMORY.
868 //
869 // Note, skip this test for bit-fields, see below.
870 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
871 Lo = Memory;
872 return;
873 }
874
875 // Classify this field.
876 //
877 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
878 // exceeds a single eightbyte, each is classified
879 // separately. Each eightbyte gets initialized to class
880 // NO_CLASS.
881 Class FieldLo, FieldHi;
882
883 // Bit-fields require special handling, they do not force the
884 // structure to be passed in memory even if unaligned, and
885 // therefore they can straddle an eightbyte.
886 if (BitField) {
887 // Ignore padding bit-fields.
888 if (i->isUnnamedBitfield())
889 continue;
890
891 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
892 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
893
894 uint64_t EB_Lo = Offset / 64;
895 uint64_t EB_Hi = (Offset + Size - 1) / 64;
896 FieldLo = FieldHi = NoClass;
897 if (EB_Lo) {
898 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
899 FieldLo = NoClass;
900 FieldHi = Integer;
901 } else {
902 FieldLo = Integer;
903 FieldHi = EB_Hi ? Integer : NoClass;
904 }
905 } else
906 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
907 Lo = merge(Lo, FieldLo);
908 Hi = merge(Hi, FieldHi);
909 if (Lo == Memory || Hi == Memory)
910 break;
911 }
912
913 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
914 //
915 // (a) If one of the classes is MEMORY, the whole argument is
916 // passed in memory.
917 //
918 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
919
920 // The first of these conditions is guaranteed by how we implement
921 // the merge (just bail).
922 //
923 // The second condition occurs in the case of unions; for example
924 // union { _Complex double; unsigned; }.
925 if (Hi == Memory)
926 Lo = Memory;
927 if (Hi == SSEUp && Lo != SSE)
928 Hi = SSE;
929 }
930}
931
932ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
933 const llvm::Type *CoerceTo,
934 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +0000935 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000936 // Integer and pointer types will end up in a general purpose
937 // register.
Anders Carlsson03747422009-09-26 03:56:53 +0000938 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000939 return (Ty->isPromotableIntegerType() ?
940 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +0000941 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000942 // FIXME: It would probably be better to make CGFunctionInfo only map using
943 // canonical types than to canonize here.
944 QualType CTy = Context.getCanonicalType(Ty);
945
946 // Float and double end up in a single SSE reg.
947 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
948 return ABIArgInfo::getDirect();
949
950 }
951
952 return ABIArgInfo::getCoerce(CoerceTo);
953}
954
955ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
956 ASTContext &Context) const {
957 // If this is a scalar LLVM value then assume LLVM will pass it in the right
958 // place naturally.
959 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000960 return (Ty->isPromotableIntegerType() ?
961 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000962
Anders Carlsson20759ad2009-09-16 15:53:40 +0000963 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
964
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000965 // FIXME: Set alignment correctly.
Anders Carlsson20759ad2009-09-16 15:53:40 +0000966 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000967}
968
969ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000970 ASTContext &Context,
971 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000972 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
973 // classification algorithm.
974 X86_64ABIInfo::Class Lo, Hi;
975 classify(RetTy, Context, 0, Lo, Hi);
976
977 // Check some invariants.
978 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
979 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
980 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
981
982 const llvm::Type *ResType = 0;
983 switch (Lo) {
984 case NoClass:
985 return ABIArgInfo::getIgnore();
986
987 case SSEUp:
988 case X87Up:
989 assert(0 && "Invalid classification for lo word.");
990
991 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
992 // hidden argument.
993 case Memory:
994 return getIndirectResult(RetTy, Context);
995
996 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
997 // available register of the sequence %rax, %rdx is used.
998 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +0000999 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001000
1001 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1002 // available SSE register of the sequence %xmm0, %xmm1 is used.
1003 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +00001004 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001005
1006 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1007 // returned on the X87 stack in %st0 as 80-bit x87 number.
1008 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +00001009 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001010
1011 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1012 // part of the value is returned in %st0 and the imaginary part in
1013 // %st1.
1014 case ComplexX87:
1015 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001016 ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext),
1017 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001018 NULL);
1019 break;
1020 }
1021
1022 switch (Hi) {
1023 // Memory was handled previously and X87 should
1024 // never occur as a hi class.
1025 case Memory:
1026 case X87:
1027 assert(0 && "Invalid classification for hi word.");
1028
1029 case ComplexX87: // Previously handled.
1030 case NoClass: break;
1031
1032 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001033 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001034 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001035 break;
1036 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001037 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001038 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001039 break;
1040
1041 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1042 // is passed in the upper half of the last used SSE register.
1043 //
1044 // SSEUP should always be preceeded by SSE, just widen.
1045 case SSEUp:
1046 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001047 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001048 break;
1049
1050 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1051 // returned together with the previous X87 value in %st0.
1052 case X87Up:
1053 // If X87Up is preceeded by X87, we don't need to do
1054 // anything. However, in some cases with unions it may not be
1055 // preceeded by X87. In such situations we follow gcc and pass the
1056 // extra bits in an SSE reg.
1057 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +00001058 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001059 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001060 break;
1061 }
1062
1063 return getCoerceResult(RetTy, ResType, Context);
1064}
1065
1066ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001067 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001068 unsigned &neededInt,
1069 unsigned &neededSSE) const {
1070 X86_64ABIInfo::Class Lo, Hi;
1071 classify(Ty, Context, 0, Lo, Hi);
1072
1073 // Check some invariants.
1074 // FIXME: Enforce these by construction.
1075 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1076 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1077 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1078
1079 neededInt = 0;
1080 neededSSE = 0;
1081 const llvm::Type *ResType = 0;
1082 switch (Lo) {
1083 case NoClass:
1084 return ABIArgInfo::getIgnore();
1085
1086 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1087 // on the stack.
1088 case Memory:
1089
1090 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1091 // COMPLEX_X87, it is passed in memory.
1092 case X87:
1093 case ComplexX87:
1094 return getIndirectResult(Ty, Context);
1095
1096 case SSEUp:
1097 case X87Up:
1098 assert(0 && "Invalid classification for lo word.");
1099
1100 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1101 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1102 // and %r9 is used.
1103 case Integer:
1104 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +00001105 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001106 break;
1107
1108 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1109 // available SSE register is used, the registers are taken in the
1110 // order from %xmm0 to %xmm7.
1111 case SSE:
1112 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001113 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001114 break;
1115 }
1116
1117 switch (Hi) {
1118 // Memory was handled previously, ComplexX87 and X87 should
1119 // never occur as hi classes, and X87Up must be preceed by X87,
1120 // which is passed in memory.
1121 case Memory:
1122 case X87:
1123 case ComplexX87:
1124 assert(0 && "Invalid classification for hi word.");
1125 break;
1126
1127 case NoClass: break;
1128 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001129 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001130 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001131 ++neededInt;
1132 break;
1133
1134 // X87Up generally doesn't occur here (long double is passed in
1135 // memory), except in situations involving unions.
1136 case X87Up:
1137 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001138 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001139 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001140 ++neededSSE;
1141 break;
1142
1143 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1144 // eightbyte is passed in the upper half of the last used SSE
1145 // register.
1146 case SSEUp:
1147 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001148 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001149 break;
1150 }
1151
1152 return getCoerceResult(Ty, ResType, Context);
1153}
1154
Owen Anderson170229f2009-07-14 23:10:40 +00001155void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1156 llvm::LLVMContext &VMContext) const {
1157 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1158 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001159
1160 // Keep track of the number of assigned registers.
1161 unsigned freeIntRegs = 6, freeSSERegs = 8;
1162
1163 // If the return value is indirect, then the hidden argument is consuming one
1164 // integer register.
1165 if (FI.getReturnInfo().isIndirect())
1166 --freeIntRegs;
1167
1168 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1169 // get assigned (in left-to-right order) for passing as follows...
1170 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1171 it != ie; ++it) {
1172 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001173 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001174 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001175
1176 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1177 // eightbyte of an argument, the whole argument is passed on the
1178 // stack. If registers have already been assigned for some
1179 // eightbytes of such an argument, the assignments get reverted.
1180 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1181 freeIntRegs -= neededInt;
1182 freeSSERegs -= neededSSE;
1183 } else {
1184 it->info = getIndirectResult(it->type, Context);
1185 }
1186 }
1187}
1188
1189static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1190 QualType Ty,
1191 CodeGenFunction &CGF) {
1192 llvm::Value *overflow_arg_area_p =
1193 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1194 llvm::Value *overflow_arg_area =
1195 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1196
1197 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1198 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1199 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1200 if (Align > 8) {
1201 // Note that we follow the ABI & gcc here, even though the type
1202 // could in theory have an alignment greater than 16. This case
1203 // shouldn't ever matter in practice.
1204
1205 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001206 llvm::Value *Offset =
1207 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001208 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1209 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001210 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1211 llvm::Value *Mask = llvm::ConstantInt::get(
1212 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001213 overflow_arg_area =
1214 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1215 overflow_arg_area->getType(),
1216 "overflow_arg_area.align");
1217 }
1218
1219 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1220 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1221 llvm::Value *Res =
1222 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001223 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001224
1225 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1226 // l->overflow_arg_area + sizeof(type).
1227 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1228 // an 8 byte boundary.
1229
1230 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001231 llvm::Value *Offset =
1232 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001233 (SizeInBytes + 7) & ~7);
1234 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1235 "overflow_arg_area.next");
1236 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1237
1238 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1239 return Res;
1240}
1241
1242llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1243 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001244 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001245 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1246 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001247
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001248 // Assume that va_list type is correct; should be pointer to LLVM type:
1249 // struct {
1250 // i32 gp_offset;
1251 // i32 fp_offset;
1252 // i8* overflow_arg_area;
1253 // i8* reg_save_area;
1254 // };
1255 unsigned neededInt, neededSSE;
Owen Anderson170229f2009-07-14 23:10:40 +00001256 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001257 neededInt, neededSSE);
1258
1259 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1260 // in the registers. If not go to step 7.
1261 if (!neededInt && !neededSSE)
1262 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1263
1264 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1265 // general purpose registers needed to pass type and num_fp to hold
1266 // the number of floating point registers needed.
1267
1268 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1269 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1270 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1271 //
1272 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1273 // register save space).
1274
1275 llvm::Value *InRegs = 0;
1276 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1277 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1278 if (neededInt) {
1279 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1280 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1281 InRegs =
1282 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001283 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001284 48 - neededInt * 8),
1285 "fits_in_gp");
1286 }
1287
1288 if (neededSSE) {
1289 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1290 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1291 llvm::Value *FitsInFP =
1292 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001293 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001294 176 - neededSSE * 16),
1295 "fits_in_fp");
1296 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1297 }
1298
1299 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1300 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1301 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1302 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1303
1304 // Emit code to load the value if it was passed in registers.
1305
1306 CGF.EmitBlock(InRegBlock);
1307
1308 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1309 // an offset of l->gp_offset and/or l->fp_offset. This may require
1310 // copying to a temporary location in case the parameter is passed
1311 // in different register classes or requires an alignment greater
1312 // than 8 for general purpose registers and 16 for XMM registers.
1313 //
1314 // FIXME: This really results in shameful code when we end up needing to
1315 // collect arguments from different places; often what should result in a
1316 // simple assembling of a structure from scattered addresses has many more
1317 // loads than necessary. Can we clean this up?
1318 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1319 llvm::Value *RegAddr =
1320 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1321 "reg_save_area");
1322 if (neededInt && neededSSE) {
1323 // FIXME: Cleanup.
1324 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1325 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1326 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1327 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1328 const llvm::Type *TyLo = ST->getElementType(0);
1329 const llvm::Type *TyHi = ST->getElementType(1);
1330 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1331 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001332 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1333 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001334 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1335 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1336 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1337 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1338 llvm::Value *V =
1339 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1340 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1341 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1342 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1343
Owen Anderson170229f2009-07-14 23:10:40 +00001344 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001345 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001346 } else if (neededInt) {
1347 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1348 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001349 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001350 } else {
1351 if (neededSSE == 1) {
1352 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1353 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001354 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001355 } else {
1356 assert(neededSSE == 2 && "Invalid number of needed registers!");
1357 // SSE registers are spaced 16 bytes apart in the register save
1358 // area, we need to collect the two eightbytes together.
1359 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1360 llvm::Value *RegAddrHi =
1361 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001362 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001363 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001364 llvm::PointerType::getUnqual(DoubleTy);
1365 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1366 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001367 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1368 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1369 DblPtrTy));
1370 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1371 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1372 DblPtrTy));
1373 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1374 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001375 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001376 }
1377 }
1378
1379 // AMD64-ABI 3.5.7p5: Step 5. Set:
1380 // l->gp_offset = l->gp_offset + num_gp * 8
1381 // l->fp_offset = l->fp_offset + num_fp * 16.
1382 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001383 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001384 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1385 gp_offset_p);
1386 }
1387 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001388 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001389 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1390 fp_offset_p);
1391 }
1392 CGF.EmitBranch(ContBlock);
1393
1394 // Emit code to load the value if it was passed in memory.
1395
1396 CGF.EmitBlock(InMemBlock);
1397 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1398
1399 // Return the appropriate result.
1400
1401 CGF.EmitBlock(ContBlock);
1402 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1403 "vaarg.addr");
1404 ResAddr->reserveOperandSpace(2);
1405 ResAddr->addIncoming(RegAddr, InRegBlock);
1406 ResAddr->addIncoming(MemAddr, InMemBlock);
1407
1408 return ResAddr;
1409}
1410
Daniel Dunbard59655c2009-09-12 00:59:49 +00001411// PIC16 ABI Implementation
1412
1413namespace {
1414
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001415class PIC16ABIInfo : public ABIInfo {
1416 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001417 ASTContext &Context,
1418 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001419
1420 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001421 ASTContext &Context,
1422 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001423
Owen Anderson170229f2009-07-14 23:10:40 +00001424 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1425 llvm::LLVMContext &VMContext) const {
1426 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1427 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001428 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1429 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001430 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001431 }
1432
1433 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1434 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001435};
1436
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001437class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1438public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001439 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001440};
1441
Daniel Dunbard59655c2009-09-12 00:59:49 +00001442}
1443
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001444ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001445 ASTContext &Context,
1446 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001447 if (RetTy->isVoidType()) {
1448 return ABIArgInfo::getIgnore();
1449 } else {
1450 return ABIArgInfo::getDirect();
1451 }
1452}
1453
1454ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001455 ASTContext &Context,
1456 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001457 return ABIArgInfo::getDirect();
1458}
1459
1460llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1461 CodeGenFunction &CGF) const {
1462 return 0;
1463}
1464
Daniel Dunbard59655c2009-09-12 00:59:49 +00001465// ARM ABI Implementation
1466
1467namespace {
1468
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001469class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001470public:
1471 enum ABIKind {
1472 APCS = 0,
1473 AAPCS = 1,
1474 AAPCS_VFP
1475 };
1476
1477private:
1478 ABIKind Kind;
1479
1480public:
1481 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1482
1483private:
1484 ABIKind getABIKind() const { return Kind; }
1485
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001486 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001487 ASTContext &Context,
1488 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001489
1490 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001491 ASTContext &Context,
1492 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001493
Owen Anderson170229f2009-07-14 23:10:40 +00001494 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1495 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001496
1497 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1498 CodeGenFunction &CGF) const;
1499};
1500
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001501class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1502public:
1503 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor0599df12010-01-22 15:41:14 +00001504 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001505};
1506
Daniel Dunbard59655c2009-09-12 00:59:49 +00001507}
1508
Owen Anderson170229f2009-07-14 23:10:40 +00001509void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1510 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001511 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001512 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001513 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1514 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001515 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001516 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001517
1518 // ARM always overrides the calling convention.
1519 switch (getABIKind()) {
1520 case APCS:
1521 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1522 break;
1523
1524 case AAPCS:
1525 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1526 break;
1527
1528 case AAPCS_VFP:
1529 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1530 break;
1531 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001532}
1533
1534ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001535 ASTContext &Context,
1536 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001537 if (!CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001538 return (Ty->isPromotableIntegerType() ?
1539 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001540
Daniel Dunbar09d33622009-09-14 21:54:03 +00001541 // Ignore empty records.
1542 if (isEmptyRecord(Context, Ty, true))
1543 return ABIArgInfo::getIgnore();
1544
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001545 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1546 // backend doesn't support byval.
1547 // FIXME: This doesn't handle alignment > 64 bits.
1548 const llvm::Type* ElemTy;
1549 unsigned SizeRegs;
1550 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001551 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001552 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1553 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001554 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001555 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1556 }
1557 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001558 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001559 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001560 return ABIArgInfo::getCoerce(STy);
1561}
1562
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001563static bool isIntegerLikeType(QualType Ty,
1564 ASTContext &Context,
1565 llvm::LLVMContext &VMContext) {
1566 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1567 // is called integer-like if its size is less than or equal to one word, and
1568 // the offset of each of its addressable sub-fields is zero.
1569
1570 uint64_t Size = Context.getTypeSize(Ty);
1571
1572 // Check that the type fits in a word.
1573 if (Size > 32)
1574 return false;
1575
1576 // FIXME: Handle vector types!
1577 if (Ty->isVectorType())
1578 return false;
1579
Daniel Dunbard53bac72009-09-14 02:20:34 +00001580 // Float types are never treated as "integer like".
1581 if (Ty->isRealFloatingType())
1582 return false;
1583
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001584 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001585 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001586 return true;
1587
1588 // Complex types "should" be ok by the definition above, but they are not.
1589 if (Ty->isAnyComplexType())
1590 return false;
1591
1592 // Single element and zero sized arrays should be allowed, by the definition
1593 // above, but they are not.
1594
1595 // Otherwise, it must be a record type.
1596 const RecordType *RT = Ty->getAs<RecordType>();
1597 if (!RT) return false;
1598
1599 // Ignore records with flexible arrays.
1600 const RecordDecl *RD = RT->getDecl();
1601 if (RD->hasFlexibleArrayMember())
1602 return false;
1603
1604 // Check that all sub-fields are at offset 0, and are themselves "integer
1605 // like".
1606 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1607
1608 bool HadField = false;
1609 unsigned idx = 0;
1610 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1611 i != e; ++i, ++idx) {
1612 const FieldDecl *FD = *i;
1613
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001614 // Bit-fields are not addressable, we only need to verify they are "integer
1615 // like". We still have to disallow a subsequent non-bitfield, for example:
1616 // struct { int : 0; int x }
1617 // is non-integer like according to gcc.
1618 if (FD->isBitField()) {
1619 if (!RD->isUnion())
1620 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001621
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001622 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1623 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001624
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001625 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001626 }
1627
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001628 // Check if this field is at offset 0.
1629 if (Layout.getFieldOffset(idx) != 0)
1630 return false;
1631
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001632 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1633 return false;
1634
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001635 // Only allow at most one field in a structure. This doesn't match the
1636 // wording above, but follows gcc in situations with a field following an
1637 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001638 if (!RD->isUnion()) {
1639 if (HadField)
1640 return false;
1641
1642 HadField = true;
1643 }
1644 }
1645
1646 return true;
1647}
1648
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001649ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001650 ASTContext &Context,
1651 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001652 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001653 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001654
1655 if (!CodeGenFunction::hasAggregateLLVMType(RetTy))
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001656 return (RetTy->isPromotableIntegerType() ?
1657 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001658
1659 // Are we following APCS?
1660 if (getABIKind() == APCS) {
1661 if (isEmptyRecord(Context, RetTy, false))
1662 return ABIArgInfo::getIgnore();
1663
Daniel Dunbareedf1512010-02-01 23:31:19 +00001664 // Complex types are all returned as packed integers.
1665 //
1666 // FIXME: Consider using 2 x vector types if the back end handles them
1667 // correctly.
1668 if (RetTy->isAnyComplexType())
1669 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1670 VMContext, Context.getTypeSize(RetTy)));
1671
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001672 // Integer like structures are returned in r0.
1673 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1674 // Return in the smallest viable integer type.
1675 uint64_t Size = Context.getTypeSize(RetTy);
1676 if (Size <= 8)
1677 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1678 if (Size <= 16)
1679 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1680 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1681 }
1682
1683 // Otherwise return in memory.
1684 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001685 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001686
1687 // Otherwise this is an AAPCS variant.
1688
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001689 if (isEmptyRecord(Context, RetTy, true))
1690 return ABIArgInfo::getIgnore();
1691
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001692 // Aggregates <= 4 bytes are returned in r0; other aggregates
1693 // are returned indirectly.
1694 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001695 if (Size <= 32) {
1696 // Return in the smallest viable integer type.
1697 if (Size <= 8)
1698 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1699 if (Size <= 16)
1700 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001701 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001702 }
1703
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001704 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001705}
1706
1707llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1708 CodeGenFunction &CGF) const {
1709 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001710 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00001711 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001712
1713 CGBuilderTy &Builder = CGF.Builder;
1714 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1715 "ap");
1716 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1717 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001718 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001719 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1720
1721 uint64_t Offset =
1722 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1723 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00001724 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1725 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001726 "ap.next");
1727 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1728
1729 return AddrTyped;
1730}
1731
1732ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001733 ASTContext &Context,
1734 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001735 if (RetTy->isVoidType()) {
1736 return ABIArgInfo::getIgnore();
1737 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1738 return ABIArgInfo::getIndirect(0);
1739 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001740 return (RetTy->isPromotableIntegerType() ?
1741 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001742 }
1743}
1744
Daniel Dunbard59655c2009-09-12 00:59:49 +00001745// SystemZ ABI Implementation
1746
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001747namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001748
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001749class SystemZABIInfo : public ABIInfo {
1750 bool isPromotableIntegerType(QualType Ty) const;
1751
1752 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1753 llvm::LLVMContext &VMContext) const;
1754
1755 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1756 llvm::LLVMContext &VMContext) const;
1757
1758 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1759 llvm::LLVMContext &VMContext) const {
1760 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1761 Context, VMContext);
1762 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1763 it != ie; ++it)
1764 it->info = classifyArgumentType(it->type, Context, VMContext);
1765 }
1766
1767 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1768 CodeGenFunction &CGF) const;
1769};
Daniel Dunbard59655c2009-09-12 00:59:49 +00001770
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001771class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
1772public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001773 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001774};
1775
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001776}
1777
1778bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
1779 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00001780 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001781 switch (BT->getKind()) {
1782 case BuiltinType::Bool:
1783 case BuiltinType::Char_S:
1784 case BuiltinType::Char_U:
1785 case BuiltinType::SChar:
1786 case BuiltinType::UChar:
1787 case BuiltinType::Short:
1788 case BuiltinType::UShort:
1789 case BuiltinType::Int:
1790 case BuiltinType::UInt:
1791 return true;
1792 default:
1793 return false;
1794 }
1795 return false;
1796}
1797
1798llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1799 CodeGenFunction &CGF) const {
1800 // FIXME: Implement
1801 return 0;
1802}
1803
1804
1805ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
1806 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001807 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001808 if (RetTy->isVoidType()) {
1809 return ABIArgInfo::getIgnore();
1810 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1811 return ABIArgInfo::getIndirect(0);
1812 } else {
1813 return (isPromotableIntegerType(RetTy) ?
1814 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1815 }
1816}
1817
1818ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
1819 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001820 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001821 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1822 return ABIArgInfo::getIndirect(0);
1823 } else {
1824 return (isPromotableIntegerType(Ty) ?
1825 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1826 }
1827}
1828
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001829// MSP430 ABI Implementation
1830
1831namespace {
1832
1833class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
1834public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001835 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001836 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1837 CodeGen::CodeGenModule &M) const;
1838};
1839
1840}
1841
1842void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1843 llvm::GlobalValue *GV,
1844 CodeGen::CodeGenModule &M) const {
1845 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1846 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
1847 // Handle 'interrupt' attribute:
1848 llvm::Function *F = cast<llvm::Function>(GV);
1849
1850 // Step 1: Set ISR calling convention.
1851 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
1852
1853 // Step 2: Add attributes goodness.
1854 F->addFnAttr(llvm::Attribute::NoInline);
1855
1856 // Step 3: Emit ISR vector alias.
1857 unsigned Num = attr->getNumber() + 0xffe0;
1858 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
1859 "vector_" +
1860 llvm::LowercaseString(llvm::utohexstr(Num)),
1861 GV, &M.getModule());
1862 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001863 }
1864}
1865
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001866const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
1867 if (TheTargetCodeGenInfo)
1868 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001869
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001870 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
1871 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00001872
Daniel Dunbar40165182009-08-24 09:10:05 +00001873 const llvm::Triple &Triple(getContext().Target.getTriple());
1874 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00001875 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001876 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbare3532f82009-08-24 08:52:16 +00001877
Daniel Dunbard59655c2009-09-12 00:59:49 +00001878 case llvm::Triple::arm:
1879 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00001880 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00001881 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001882 return *(TheTargetCodeGenInfo =
1883 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00001884
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001885 return *(TheTargetCodeGenInfo =
1886 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00001887
1888 case llvm::Triple::pic16:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001889 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00001890
1891 case llvm::Triple::systemz:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001892 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
1893
1894 case llvm::Triple::msp430:
1895 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00001896
Daniel Dunbar40165182009-08-24 09:10:05 +00001897 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00001898 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00001899 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001900 return *(TheTargetCodeGenInfo =
1901 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00001902 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00001903 case llvm::Triple::MinGW32:
1904 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00001905 case llvm::Triple::AuroraUX:
1906 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00001907 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00001908 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001909 return *(TheTargetCodeGenInfo =
1910 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00001911
1912 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001913 return *(TheTargetCodeGenInfo =
1914 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001915 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001916
Daniel Dunbare3532f82009-08-24 08:52:16 +00001917 case llvm::Triple::x86_64:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001918 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00001919 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001920}