blob: 4a0a298c3be3c97d47d3820f21a23561e628d7ab [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 Dunbar557893d2010-04-21 19:10:51 +000046 OS << "Indirect Align=" << getIndirectAlign()
47 << " Byal=" << getIndirectByVal();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000048 break;
49 case Expand:
Daniel Dunbar7230fa52009-12-03 09:13:49 +000050 OS << "Expand";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000051 break;
52 }
Daniel Dunbar7230fa52009-12-03 09:13:49 +000053 OS << ")\n";
Anton Korobeynikov244360d2009-06-05 22:08:42 +000054}
55
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000056TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
57
Daniel Dunbar626f1d82009-09-13 08:03:58 +000058static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000059
60/// isEmptyField - Return true iff a the field is "empty", that is it
61/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar626f1d82009-09-13 08:03:58 +000062static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
63 bool AllowArrays) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +000064 if (FD->isUnnamedBitfield())
65 return true;
66
67 QualType FT = FD->getType();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000068
Daniel Dunbar626f1d82009-09-13 08:03:58 +000069 // Constant arrays of empty records count as empty, strip them off.
70 if (AllowArrays)
71 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
72 FT = AT->getElementType();
73
74 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikov244360d2009-06-05 22:08:42 +000075}
76
77/// isEmptyRecord - Return true iff a structure contains only empty
78/// fields. Note that a structure with a flexible array member is not
79/// considered empty.
Daniel Dunbar626f1d82009-09-13 08:03:58 +000080static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +000081 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +000082 if (!RT)
83 return 0;
84 const RecordDecl *RD = RT->getDecl();
85 if (RD->hasFlexibleArrayMember())
86 return false;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +000087 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
88 i != e; ++i)
Daniel Dunbar626f1d82009-09-13 08:03:58 +000089 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikov244360d2009-06-05 22:08:42 +000090 return false;
91 return true;
92}
93
Anders Carlsson20759ad2009-09-16 15:53:40 +000094/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
95/// a non-trivial destructor or a non-trivial copy constructor.
96static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
97 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
98 if (!RD)
99 return false;
100
101 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
102}
103
104/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
105/// a record type with either a non-trivial destructor or a non-trivial copy
106/// constructor.
107static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
108 const RecordType *RT = T->getAs<RecordType>();
109 if (!RT)
110 return false;
111
112 return hasNonTrivialDestructorOrCopyConstructor(RT);
113}
114
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000115/// isSingleElementStruct - Determine if a structure is a "single
116/// element struct", i.e. it has exactly one non-empty field or
117/// exactly one field which is itself a single element
118/// struct. Structures with flexible array members are never
119/// considered single element structs.
120///
121/// \return The field declaration for the single non-empty field, if
122/// it exists.
123static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
124 const RecordType *RT = T->getAsStructureType();
125 if (!RT)
126 return 0;
127
128 const RecordDecl *RD = RT->getDecl();
129 if (RD->hasFlexibleArrayMember())
130 return 0;
131
132 const Type *Found = 0;
Daniel Dunbar12ebb472010-05-11 21:15:36 +0000133
134 // If this is a C++ record, check the bases first.
135 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
136 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
137 e = CXXRD->bases_end(); i != e; ++i) {
138 const CXXRecordDecl *Base =
139 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
140 // Ignore empty records.
141 if (Base->isEmpty())
142 continue;
143
144 // If we already found an element then this isn't a single-element struct.
145 if (Found)
146 return 0;
147
148 // If this is non-empty and not a single element struct, the composite
149 // cannot be a single element struct.
150 Found = isSingleElementStruct(i->getType(), Context);
151 if (!Found)
152 return 0;
153 }
154 }
155
156 // Check for single element.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000157 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
158 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000159 const FieldDecl *FD = *i;
160 QualType FT = FD->getType();
161
162 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000163 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000164 continue;
165
166 // If we already found an element then this isn't a single-element
167 // struct.
168 if (Found)
169 return 0;
170
171 // Treat single element arrays as the element.
172 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
173 if (AT->getSize().getZExtValue() != 1)
174 break;
175 FT = AT->getElementType();
176 }
177
178 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
179 Found = FT.getTypePtr();
180 } else {
181 Found = isSingleElementStruct(FT, Context);
182 if (!Found)
183 return 0;
184 }
185 }
186
187 return Found;
188}
189
190static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000191 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000192 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
193 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000194 return false;
195
196 uint64_t Size = Context.getTypeSize(Ty);
197 return Size == 32 || Size == 64;
198}
199
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000200/// canExpandIndirectArgument - Test whether an argument type which is to be
201/// passed indirectly (on the stack) would have the equivalent layout if it was
202/// expanded into separate arguments. If so, we prefer to do the latter to avoid
203/// inhibiting optimizations.
204///
205// FIXME: This predicate is missing many cases, currently it just follows
206// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
207// should probably make this smarter, or better yet make the LLVM backend
208// capable of handling it.
209static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
210 // We can only expand structure types.
211 const RecordType *RT = Ty->getAs<RecordType>();
212 if (!RT)
213 return false;
214
215 // We can only expand (C) structures.
216 //
217 // FIXME: This needs to be generalized to handle classes as well.
218 const RecordDecl *RD = RT->getDecl();
219 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
220 return false;
221
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000222 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
223 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000224 const FieldDecl *FD = *i;
225
226 if (!is32Or64BitBasicType(FD->getType(), Context))
227 return false;
228
229 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
230 // how to expand them yet, and the predicate for telling if a bitfield still
231 // counts as "basic" is more complicated than what we were doing previously.
232 if (FD->isBitField())
233 return false;
234 }
235
236 return true;
237}
238
Eli Friedman3192cc82009-06-13 21:37:10 +0000239static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000240 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
241 i != e; ++i) {
Eli Friedman3192cc82009-06-13 21:37:10 +0000242 const FieldDecl *FD = *i;
243
244 if (FD->getType()->isVectorType() &&
245 Context.getTypeSize(FD->getType()) >= 128)
246 return true;
247
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000248 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000249 if (typeContainsSSEVector(RT->getDecl(), Context))
250 return true;
251 }
252
253 return false;
254}
255
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000256namespace {
257/// DefaultABIInfo - The default implementation for ABI specific
258/// details. This implementation provides information which results in
259/// self-consistent and sensible LLVM IR generation, but does not
260/// conform to any particular ABI.
261class DefaultABIInfo : public ABIInfo {
262 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000263 ASTContext &Context,
264 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000265
266 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000267 ASTContext &Context,
268 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000269
Owen Anderson170229f2009-07-14 23:10:40 +0000270 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
271 llvm::LLVMContext &VMContext) const {
272 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
273 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000274 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
275 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000276 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000277 }
278
279 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
280 CodeGenFunction &CGF) const;
281};
282
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000283class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
284public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000285 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000286};
287
288llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
289 CodeGenFunction &CGF) const {
290 return 0;
291}
292
293ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
294 ASTContext &Context,
295 llvm::LLVMContext &VMContext) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000296 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000297 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000298
Chris Lattner9723d6c2010-03-11 18:19:55 +0000299 // Treat an enum type as its underlying type.
300 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
301 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000302
Chris Lattner9723d6c2010-03-11 18:19:55 +0000303 return (Ty->isPromotableIntegerType() ?
304 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000305}
306
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000307/// X86_32ABIInfo - The X86-32 ABI information.
308class X86_32ABIInfo : public ABIInfo {
309 ASTContext &Context;
David Chisnallde3a0692009-08-17 23:08:21 +0000310 bool IsDarwinVectorABI;
311 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000312
313 static bool isRegisterSize(unsigned Size) {
314 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
315 }
316
317 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
318
Daniel Dunbar557893d2010-04-21 19:10:51 +0000319 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
320 /// such that the argument will be passed in memory.
321 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
322 bool ByVal = true) const;
323
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000324public:
325 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000326 ASTContext &Context,
327 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000328
329 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000330 ASTContext &Context,
331 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000332
Owen Anderson170229f2009-07-14 23:10:40 +0000333 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
334 llvm::LLVMContext &VMContext) const {
335 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
336 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000337 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
338 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000339 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000340 }
341
342 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
343 CodeGenFunction &CGF) const;
344
David Chisnallde3a0692009-08-17 23:08:21 +0000345 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000346 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000347 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000348};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000349
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000350class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
351public:
352 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor0599df12010-01-22 15:41:14 +0000353 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000354
355 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
356 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000357
358 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
359 // Darwin uses different dwarf register numbers for EH.
360 if (CGM.isTargetDarwin()) return 5;
361
362 return 4;
363 }
364
365 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
366 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000367};
368
369}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000370
371/// shouldReturnTypeInRegister - Determine if the given type should be
372/// passed in a register (for the Darwin ABI).
373bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
374 ASTContext &Context) {
375 uint64_t Size = Context.getTypeSize(Ty);
376
377 // Type must be register sized.
378 if (!isRegisterSize(Size))
379 return false;
380
381 if (Ty->isVectorType()) {
382 // 64- and 128- bit vectors inside structures are not returned in
383 // registers.
384 if (Size == 64 || Size == 128)
385 return false;
386
387 return true;
388 }
389
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000390 // If this is a builtin, pointer, enum, complex type, member pointer, or
391 // member function pointer it is ok.
Daniel Dunbar6b45b672010-05-14 03:40:53 +0000392 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000393 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
Daniel Dunbar4bd95c62010-05-15 00:00:30 +0000394 Ty->isBlockPointerType() || Ty->isMemberPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000395 return true;
396
397 // Arrays are treated like records.
398 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
399 return shouldReturnTypeInRegister(AT->getElementType(), Context);
400
401 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000402 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000403 if (!RT) return false;
404
Anders Carlsson40446e82010-01-27 03:25:19 +0000405 // FIXME: Traverse bases here too.
406
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000407 // Structure types are passed in register if all fields would be
408 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000409 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
410 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000411 const FieldDecl *FD = *i;
412
413 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000414 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000415 continue;
416
417 // Check fields recursively.
418 if (!shouldReturnTypeInRegister(FD->getType(), Context))
419 return false;
420 }
421
422 return true;
423}
424
425ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000426 ASTContext &Context,
427 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000428 if (RetTy->isVoidType()) {
429 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000430 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000431 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000432 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000433 uint64_t Size = Context.getTypeSize(RetTy);
434
435 // 128-bit vectors are a special case; they are returned in
436 // registers and we need to make sure to pick a type the LLVM
437 // backend will like.
438 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000439 return ABIArgInfo::getCoerce(llvm::VectorType::get(
440 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000441
442 // Always return in register if it fits in a general purpose
443 // register, or if it is 64 bits and has a single element.
444 if ((Size == 8 || Size == 16 || Size == 32) ||
445 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000446 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000447
448 return ABIArgInfo::getIndirect(0);
449 }
450
451 return ABIArgInfo::getDirect();
452 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000453 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000454 // Structures with either a non-trivial destructor or a non-trivial
455 // copy constructor are always indirect.
456 if (hasNonTrivialDestructorOrCopyConstructor(RT))
457 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
458
459 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460 if (RT->getDecl()->hasFlexibleArrayMember())
461 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000462 }
463
David Chisnallde3a0692009-08-17 23:08:21 +0000464 // If specified, structs and unions are always indirect.
465 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000466 return ABIArgInfo::getIndirect(0);
467
468 // Classify "single element" structs as their element type.
469 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000470 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000471 if (BT->isIntegerType()) {
472 // We need to use the size of the structure, padding
473 // bit-fields can adjust that to be larger than the single
474 // element type.
475 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000476 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000477 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000478 } else if (BT->getKind() == BuiltinType::Float) {
479 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
480 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000481 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000482 } else if (BT->getKind() == BuiltinType::Double) {
483 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
484 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000485 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000486 }
487 } else if (SeltTy->isPointerType()) {
488 // FIXME: It would be really nice if this could come out as the proper
489 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000490 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000491 return ABIArgInfo::getCoerce(PtrTy);
492 } else if (SeltTy->isVectorType()) {
493 // 64- and 128-bit vectors are never returned in a
494 // register when inside a structure.
495 uint64_t Size = Context.getTypeSize(RetTy);
496 if (Size == 64 || Size == 128)
497 return ABIArgInfo::getIndirect(0);
498
Owen Anderson170229f2009-07-14 23:10:40 +0000499 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000500 }
501 }
502
503 // Small structures which are register sized are generally returned
504 // in a register.
505 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
506 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000507 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000508 }
509
510 return ABIArgInfo::getIndirect(0);
511 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000512 // Treat an enum type as its underlying type.
513 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
514 RetTy = EnumTy->getDecl()->getIntegerType();
515
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000516 return (RetTy->isPromotableIntegerType() ?
517 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000518 }
519}
520
Daniel Dunbar557893d2010-04-21 19:10:51 +0000521ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
522 ASTContext &Context,
523 bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000524 if (!ByVal)
525 return ABIArgInfo::getIndirect(0, false);
526
527 // Compute the byval alignment. We trust the back-end to honor the
528 // minimum ABI alignment for byval, to make cleaner IR.
529 const unsigned MinABIAlign = 4;
530 unsigned Align = Context.getTypeAlign(Ty) / 8;
531 if (Align > MinABIAlign)
532 return ABIArgInfo::getIndirect(Align);
533 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000534}
535
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000536ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000537 ASTContext &Context,
538 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000539 // FIXME: Set alignment on indirect arguments.
540 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
541 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000542 if (const RecordType *RT = Ty->getAs<RecordType>()) {
543 // Structures with either a non-trivial destructor or a non-trivial
544 // copy constructor are always indirect.
545 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbar557893d2010-04-21 19:10:51 +0000546 return getIndirectResult(Ty, Context, /*ByVal=*/false);
547
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000548 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar557893d2010-04-21 19:10:51 +0000549 return getIndirectResult(Ty, Context);
Anders Carlsson40446e82010-01-27 03:25:19 +0000550 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000551
552 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000553 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000554 return ABIArgInfo::getIgnore();
555
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000556 // Expand small (<= 128-bit) record types when we know that the stack layout
557 // of those arguments will match the struct. This is important because the
558 // LLVM backend isn't smart enough to remove byval, which inhibits many
559 // optimizations.
560 if (Context.getTypeSize(Ty) <= 4*32 &&
561 canExpandIndirectArgument(Ty, Context))
562 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000563
Daniel Dunbar557893d2010-04-21 19:10:51 +0000564 return getIndirectResult(Ty, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000565 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000566 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
567 Ty = EnumTy->getDecl()->getIntegerType();
568
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000569 return (Ty->isPromotableIntegerType() ?
570 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000571 }
572}
573
574llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
575 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000576 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000577 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000578
579 CGBuilderTy &Builder = CGF.Builder;
580 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
581 "ap");
582 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
583 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000584 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000585 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
586
587 uint64_t Offset =
588 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
589 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +0000590 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
591 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000592 "ap.next");
593 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
594
595 return AddrTyped;
596}
597
Charles Davis4ea31ab2010-02-13 15:54:06 +0000598void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
599 llvm::GlobalValue *GV,
600 CodeGen::CodeGenModule &CGM) const {
601 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
602 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
603 // Get the LLVM function.
604 llvm::Function *Fn = cast<llvm::Function>(GV);
605
606 // Now add the 'alignstack' attribute with a value of 16.
607 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
608 }
609 }
610}
611
John McCallbeec5a02010-03-06 00:35:14 +0000612bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
613 CodeGen::CodeGenFunction &CGF,
614 llvm::Value *Address) const {
615 CodeGen::CGBuilderTy &Builder = CGF.Builder;
616 llvm::LLVMContext &Context = CGF.getLLVMContext();
617
618 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
619 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
620
621 // 0-7 are the eight integer registers; the order is different
622 // on Darwin (for EH), but the range is the same.
623 // 8 is %eip.
624 for (unsigned I = 0, E = 9; I != E; ++I) {
625 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
626 Builder.CreateStore(Four8, Slot);
627 }
628
629 if (CGF.CGM.isTargetDarwin()) {
630 // 12-16 are st(0..4). Not sure why we stop at 4.
631 // These have size 16, which is sizeof(long double) on
632 // platforms with 8-byte alignment for that type.
633 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
634 for (unsigned I = 12, E = 17; I != E; ++I) {
635 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
636 Builder.CreateStore(Sixteen8, Slot);
637 }
638
639 } else {
640 // 9 is %eflags, which doesn't get a size on Darwin for some
641 // reason.
642 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
643
644 // 11-16 are st(0..5). Not sure why we stop at 5.
645 // These have size 12, which is sizeof(long double) on
646 // platforms with 4-byte alignment for that type.
647 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
648 for (unsigned I = 11, E = 17; I != E; ++I) {
649 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
650 Builder.CreateStore(Twelve8, Slot);
651 }
652 }
653
654 return false;
655}
656
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000657namespace {
658/// X86_64ABIInfo - The X86_64 ABI information.
659class X86_64ABIInfo : public ABIInfo {
660 enum Class {
661 Integer = 0,
662 SSE,
663 SSEUp,
664 X87,
665 X87Up,
666 ComplexX87,
667 NoClass,
668 Memory
669 };
670
671 /// merge - Implement the X86_64 ABI merging algorithm.
672 ///
673 /// Merge an accumulating classification \arg Accum with a field
674 /// classification \arg Field.
675 ///
676 /// \param Accum - The accumulating classification. This should
677 /// always be either NoClass or the result of a previous merge
678 /// call. In addition, this should never be Memory (the caller
679 /// should just return Memory for the aggregate).
680 Class merge(Class Accum, Class Field) const;
681
682 /// classify - Determine the x86_64 register classes in which the
683 /// given type T should be passed.
684 ///
685 /// \param Lo - The classification for the parts of the type
686 /// residing in the low word of the containing object.
687 ///
688 /// \param Hi - The classification for the parts of the type
689 /// residing in the high word of the containing object.
690 ///
691 /// \param OffsetBase - The bit offset of this type in the
692 /// containing object. Some parameters are classified different
693 /// depending on whether they straddle an eightbyte boundary.
694 ///
695 /// If a word is unused its result will be NoClass; if a type should
696 /// be passed in Memory then at least the classification of \arg Lo
697 /// will be Memory.
698 ///
699 /// The \arg Lo class will be NoClass iff the argument is ignored.
700 ///
701 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
702 /// also be ComplexX87.
703 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
704 Class &Lo, Class &Hi) const;
705
706 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
707 /// to coerce to, chose the best way to pass Ty in the same place
708 /// that \arg CoerceTo would be passed, but while keeping the
709 /// emitted code as simple as possible.
710 ///
711 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
712 /// the ways we might want to pass things, instead of constructing an LLVM
713 /// type. This makes this code more explicit, and it makes it clearer that we
714 /// are also doing this for correctness in the case of passing scalar types.
715 ABIArgInfo getCoerceResult(QualType Ty,
716 const llvm::Type *CoerceTo,
717 ASTContext &Context) const;
718
719 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000720 /// such that the argument will be returned in memory.
721 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
722
723 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000724 /// such that the argument will be passed in memory.
Daniel Dunbar557893d2010-04-21 19:10:51 +0000725 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000726
727 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000728 ASTContext &Context,
729 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000730
731 ABIArgInfo classifyArgumentType(QualType Ty,
732 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000733 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000734 unsigned &neededInt,
735 unsigned &neededSSE) const;
736
737public:
Owen Anderson170229f2009-07-14 23:10:40 +0000738 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
739 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000740
741 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
742 CodeGenFunction &CGF) const;
743};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000744
745class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
746public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000747 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCallbeec5a02010-03-06 00:35:14 +0000748
749 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
750 return 7;
751 }
752
753 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
754 llvm::Value *Address) const {
755 CodeGen::CGBuilderTy &Builder = CGF.Builder;
756 llvm::LLVMContext &Context = CGF.getLLVMContext();
757
758 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
759 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
760
761 // 0-16 are the 16 integer registers.
762 // 17 is %rip.
763 for (unsigned I = 0, E = 17; I != E; ++I) {
764 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
765 Builder.CreateStore(Eight8, Slot);
766 }
767
768 return false;
769 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000770};
771
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000772}
773
774X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
775 Class Field) const {
776 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
777 // classified recursively so that always two fields are
778 // considered. The resulting class is calculated according to
779 // the classes of the fields in the eightbyte:
780 //
781 // (a) If both classes are equal, this is the resulting class.
782 //
783 // (b) If one of the classes is NO_CLASS, the resulting class is
784 // the other class.
785 //
786 // (c) If one of the classes is MEMORY, the result is the MEMORY
787 // class.
788 //
789 // (d) If one of the classes is INTEGER, the result is the
790 // INTEGER.
791 //
792 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
793 // MEMORY is used as class.
794 //
795 // (f) Otherwise class SSE is used.
796
797 // Accum should never be memory (we should have returned) or
798 // ComplexX87 (because this cannot be passed in a structure).
799 assert((Accum != Memory && Accum != ComplexX87) &&
800 "Invalid accumulated classification during merge.");
801 if (Accum == Field || Field == NoClass)
802 return Accum;
803 else if (Field == Memory)
804 return Memory;
805 else if (Accum == NoClass)
806 return Field;
807 else if (Accum == Integer || Field == Integer)
808 return Integer;
809 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
810 Accum == X87 || Accum == X87Up)
811 return Memory;
812 else
813 return SSE;
814}
815
816void X86_64ABIInfo::classify(QualType Ty,
817 ASTContext &Context,
818 uint64_t OffsetBase,
819 Class &Lo, Class &Hi) const {
820 // FIXME: This code can be simplified by introducing a simple value class for
821 // Class pairs with appropriate constructor methods for the various
822 // situations.
823
824 // FIXME: Some of the split computations are wrong; unaligned vectors
825 // shouldn't be passed in registers for example, so there is no chance they
826 // can straddle an eightbyte. Verify & simplify.
827
828 Lo = Hi = NoClass;
829
830 Class &Current = OffsetBase < 64 ? Lo : Hi;
831 Current = Memory;
832
John McCall9dd450b2009-09-21 23:43:11 +0000833 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000834 BuiltinType::Kind k = BT->getKind();
835
836 if (k == BuiltinType::Void) {
837 Current = NoClass;
838 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
839 Lo = Integer;
840 Hi = Integer;
841 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
842 Current = Integer;
843 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
844 Current = SSE;
845 } else if (k == BuiltinType::LongDouble) {
846 Lo = X87;
847 Hi = X87Up;
848 }
849 // FIXME: _Decimal32 and _Decimal64 are SSE.
850 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall9dd450b2009-09-21 23:43:11 +0000851 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000852 // Classify the underlying integer type.
853 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
854 } else if (Ty->hasPointerRepresentation()) {
855 Current = Integer;
Daniel Dunbar36d4d152010-05-15 00:00:37 +0000856 } else if (Ty->isMemberPointerType()) {
857 if (Ty->isMemberFunctionPointerType())
858 Lo = Hi = Integer;
859 else
860 Current = Integer;
John McCall9dd450b2009-09-21 23:43:11 +0000861 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000862 uint64_t Size = Context.getTypeSize(VT);
863 if (Size == 32) {
864 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
865 // float> as integer.
866 Current = Integer;
867
868 // If this type crosses an eightbyte boundary, it should be
869 // split.
870 uint64_t EB_Real = (OffsetBase) / 64;
871 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
872 if (EB_Real != EB_Imag)
873 Hi = Lo;
874 } else if (Size == 64) {
875 // gcc passes <1 x double> in memory. :(
876 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
877 return;
878
879 // gcc passes <1 x long long> as INTEGER.
880 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
881 Current = Integer;
882 else
883 Current = SSE;
884
885 // If this type crosses an eightbyte boundary, it should be
886 // split.
887 if (OffsetBase && OffsetBase != 64)
888 Hi = Lo;
889 } else if (Size == 128) {
890 Lo = SSE;
891 Hi = SSEUp;
892 }
John McCall9dd450b2009-09-21 23:43:11 +0000893 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000894 QualType ET = Context.getCanonicalType(CT->getElementType());
895
896 uint64_t Size = Context.getTypeSize(Ty);
897 if (ET->isIntegralType()) {
898 if (Size <= 64)
899 Current = Integer;
900 else if (Size <= 128)
901 Lo = Hi = Integer;
902 } else if (ET == Context.FloatTy)
903 Current = SSE;
904 else if (ET == Context.DoubleTy)
905 Lo = Hi = SSE;
906 else if (ET == Context.LongDoubleTy)
907 Current = ComplexX87;
908
909 // If this complex type crosses an eightbyte boundary then it
910 // should be split.
911 uint64_t EB_Real = (OffsetBase) / 64;
912 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
913 if (Hi == NoClass && EB_Real != EB_Imag)
914 Hi = Lo;
915 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
916 // Arrays are treated like structures.
917
918 uint64_t Size = Context.getTypeSize(Ty);
919
920 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
921 // than two eightbytes, ..., it has class MEMORY.
922 if (Size > 128)
923 return;
924
925 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
926 // fields, it has class MEMORY.
927 //
928 // Only need to check alignment of array base.
929 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
930 return;
931
932 // Otherwise implement simplified merge. We could be smarter about
933 // this, but it isn't worth it and would be harder to verify.
934 Current = NoClass;
935 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
936 uint64_t ArraySize = AT->getSize().getZExtValue();
937 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
938 Class FieldLo, FieldHi;
939 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
940 Lo = merge(Lo, FieldLo);
941 Hi = merge(Hi, FieldHi);
942 if (Lo == Memory || Hi == Memory)
943 break;
944 }
945
946 // Do post merger cleanup (see below). Only case we worry about is Memory.
947 if (Hi == Memory)
948 Lo = Memory;
949 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000950 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000951 uint64_t Size = Context.getTypeSize(Ty);
952
953 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
954 // than two eightbytes, ..., it has class MEMORY.
955 if (Size > 128)
956 return;
957
Anders Carlsson20759ad2009-09-16 15:53:40 +0000958 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
959 // copy constructor or a non-trivial destructor, it is passed by invisible
960 // reference.
961 if (hasNonTrivialDestructorOrCopyConstructor(RT))
962 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000963
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000964 const RecordDecl *RD = RT->getDecl();
965
966 // Assume variable sized types are passed in memory.
967 if (RD->hasFlexibleArrayMember())
968 return;
969
970 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
971
972 // Reset Lo class, this will be recomputed.
973 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000974
975 // If this is a C++ record, classify the bases first.
976 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
977 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
978 e = CXXRD->bases_end(); i != e; ++i) {
979 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
980 "Unexpected base class!");
981 const CXXRecordDecl *Base =
982 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
983
984 // Classify this field.
985 //
986 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
987 // single eightbyte, each is classified separately. Each eightbyte gets
988 // initialized to class NO_CLASS.
989 Class FieldLo, FieldHi;
990 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
991 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
992 Lo = merge(Lo, FieldLo);
993 Hi = merge(Hi, FieldHi);
994 if (Lo == Memory || Hi == Memory)
995 break;
996 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +0000997
998 // If this record has no fields but isn't empty, classify as INTEGER.
999 if (RD->field_empty() && Size)
1000 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +00001001 }
1002
1003 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001004 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001005 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1006 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001007 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1008 bool BitField = i->isBitField();
1009
1010 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1011 // fields, it has class MEMORY.
1012 //
1013 // Note, skip this test for bit-fields, see below.
1014 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
1015 Lo = Memory;
1016 return;
1017 }
1018
1019 // Classify this field.
1020 //
1021 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1022 // exceeds a single eightbyte, each is classified
1023 // separately. Each eightbyte gets initialized to class
1024 // NO_CLASS.
1025 Class FieldLo, FieldHi;
1026
1027 // Bit-fields require special handling, they do not force the
1028 // structure to be passed in memory even if unaligned, and
1029 // therefore they can straddle an eightbyte.
1030 if (BitField) {
1031 // Ignore padding bit-fields.
1032 if (i->isUnnamedBitfield())
1033 continue;
1034
1035 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1036 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1037
1038 uint64_t EB_Lo = Offset / 64;
1039 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1040 FieldLo = FieldHi = NoClass;
1041 if (EB_Lo) {
1042 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1043 FieldLo = NoClass;
1044 FieldHi = Integer;
1045 } else {
1046 FieldLo = Integer;
1047 FieldHi = EB_Hi ? Integer : NoClass;
1048 }
1049 } else
1050 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1051 Lo = merge(Lo, FieldLo);
1052 Hi = merge(Hi, FieldHi);
1053 if (Lo == Memory || Hi == Memory)
1054 break;
1055 }
1056
1057 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1058 //
1059 // (a) If one of the classes is MEMORY, the whole argument is
1060 // passed in memory.
1061 //
1062 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1063
1064 // The first of these conditions is guaranteed by how we implement
1065 // the merge (just bail).
1066 //
1067 // The second condition occurs in the case of unions; for example
1068 // union { _Complex double; unsigned; }.
1069 if (Hi == Memory)
1070 Lo = Memory;
1071 if (Hi == SSEUp && Lo != SSE)
1072 Hi = SSE;
1073 }
1074}
1075
1076ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1077 const llvm::Type *CoerceTo,
1078 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +00001079 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001080 // Integer and pointer types will end up in a general purpose
1081 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001082
1083 // Treat an enum type as its underlying type.
1084 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1085 Ty = EnumTy->getDecl()->getIntegerType();
1086
Anders Carlsson03747422009-09-26 03:56:53 +00001087 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001088 return (Ty->isPromotableIntegerType() ?
1089 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +00001090 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
John McCall8ee376f2010-02-24 07:14:12 +00001091 assert(Ty.isCanonical() && "should always have a canonical type here");
1092 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001093
1094 // Float and double end up in a single SSE reg.
John McCall8ee376f2010-02-24 07:14:12 +00001095 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001096 return ABIArgInfo::getDirect();
1097
1098 }
1099
1100 return ABIArgInfo::getCoerce(CoerceTo);
1101}
1102
Daniel Dunbar53fac692010-04-21 19:49:55 +00001103ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1104 ASTContext &Context) const {
1105 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1106 // place naturally.
1107 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1108 // Treat an enum type as its underlying type.
1109 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1110 Ty = EnumTy->getDecl()->getIntegerType();
1111
1112 return (Ty->isPromotableIntegerType() ?
1113 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1114 }
1115
1116 return ABIArgInfo::getIndirect(0);
1117}
1118
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001119ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1120 ASTContext &Context) const {
1121 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1122 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001123 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1124 // Treat an enum type as its underlying type.
1125 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1126 Ty = EnumTy->getDecl()->getIntegerType();
1127
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001128 return (Ty->isPromotableIntegerType() ?
1129 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001130 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001131
Daniel Dunbar53fac692010-04-21 19:49:55 +00001132 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1133 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001134
Daniel Dunbar53fac692010-04-21 19:49:55 +00001135 // Compute the byval alignment. We trust the back-end to honor the
1136 // minimum ABI alignment for byval, to make cleaner IR.
1137 const unsigned MinABIAlign = 8;
1138 unsigned Align = Context.getTypeAlign(Ty) / 8;
1139 if (Align > MinABIAlign)
1140 return ABIArgInfo::getIndirect(Align);
1141 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001142}
1143
1144ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001145 ASTContext &Context,
1146 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001147 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1148 // classification algorithm.
1149 X86_64ABIInfo::Class Lo, Hi;
1150 classify(RetTy, Context, 0, Lo, Hi);
1151
1152 // Check some invariants.
1153 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1154 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1155 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1156
1157 const llvm::Type *ResType = 0;
1158 switch (Lo) {
1159 case NoClass:
1160 return ABIArgInfo::getIgnore();
1161
1162 case SSEUp:
1163 case X87Up:
1164 assert(0 && "Invalid classification for lo word.");
1165
1166 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1167 // hidden argument.
1168 case Memory:
Daniel Dunbar53fac692010-04-21 19:49:55 +00001169 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001170
1171 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1172 // available register of the sequence %rax, %rdx is used.
1173 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +00001174 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001175
1176 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1177 // available SSE register of the sequence %xmm0, %xmm1 is used.
1178 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +00001179 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001180
1181 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1182 // returned on the X87 stack in %st0 as 80-bit x87 number.
1183 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +00001184 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001185
1186 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1187 // part of the value is returned in %st0 and the imaginary part in
1188 // %st1.
1189 case ComplexX87:
1190 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001191 ResType = llvm::StructType::get(VMContext,
1192 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson41a75022009-08-13 21:57:51 +00001193 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001194 NULL);
1195 break;
1196 }
1197
1198 switch (Hi) {
1199 // Memory was handled previously and X87 should
1200 // never occur as a hi class.
1201 case Memory:
1202 case X87:
1203 assert(0 && "Invalid classification for hi word.");
1204
1205 case ComplexX87: // Previously handled.
1206 case NoClass: break;
1207
1208 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001209 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001210 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001211 break;
1212 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001213 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001214 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001215 break;
1216
1217 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1218 // is passed in the upper half of the last used SSE register.
1219 //
1220 // SSEUP should always be preceeded by SSE, just widen.
1221 case SSEUp:
1222 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001223 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001224 break;
1225
1226 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1227 // returned together with the previous X87 value in %st0.
1228 case X87Up:
1229 // If X87Up is preceeded by X87, we don't need to do
1230 // anything. However, in some cases with unions it may not be
1231 // preceeded by X87. In such situations we follow gcc and pass the
1232 // extra bits in an SSE reg.
1233 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +00001234 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001235 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001236 break;
1237 }
1238
1239 return getCoerceResult(RetTy, ResType, Context);
1240}
1241
1242ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001243 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001244 unsigned &neededInt,
1245 unsigned &neededSSE) const {
1246 X86_64ABIInfo::Class Lo, Hi;
1247 classify(Ty, Context, 0, Lo, Hi);
1248
1249 // Check some invariants.
1250 // FIXME: Enforce these by construction.
1251 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1252 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1253 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1254
1255 neededInt = 0;
1256 neededSSE = 0;
1257 const llvm::Type *ResType = 0;
1258 switch (Lo) {
1259 case NoClass:
1260 return ABIArgInfo::getIgnore();
1261
1262 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1263 // on the stack.
1264 case Memory:
1265
1266 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1267 // COMPLEX_X87, it is passed in memory.
1268 case X87:
1269 case ComplexX87:
1270 return getIndirectResult(Ty, Context);
1271
1272 case SSEUp:
1273 case X87Up:
1274 assert(0 && "Invalid classification for lo word.");
1275
1276 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1277 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1278 // and %r9 is used.
1279 case Integer:
1280 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +00001281 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001282 break;
1283
1284 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1285 // available SSE register is used, the registers are taken in the
1286 // order from %xmm0 to %xmm7.
1287 case SSE:
1288 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001289 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001290 break;
1291 }
1292
1293 switch (Hi) {
1294 // Memory was handled previously, ComplexX87 and X87 should
1295 // never occur as hi classes, and X87Up must be preceed by X87,
1296 // which is passed in memory.
1297 case Memory:
1298 case X87:
1299 case ComplexX87:
1300 assert(0 && "Invalid classification for hi word.");
1301 break;
1302
1303 case NoClass: break;
1304 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001305 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001306 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001307 ++neededInt;
1308 break;
1309
1310 // X87Up generally doesn't occur here (long double is passed in
1311 // memory), except in situations involving unions.
1312 case X87Up:
1313 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001314 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001315 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001316 ++neededSSE;
1317 break;
1318
1319 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1320 // eightbyte is passed in the upper half of the last used SSE
1321 // register.
1322 case SSEUp:
1323 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001324 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001325 break;
1326 }
1327
1328 return getCoerceResult(Ty, ResType, Context);
1329}
1330
Owen Anderson170229f2009-07-14 23:10:40 +00001331void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1332 llvm::LLVMContext &VMContext) const {
1333 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1334 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001335
1336 // Keep track of the number of assigned registers.
1337 unsigned freeIntRegs = 6, freeSSERegs = 8;
1338
1339 // If the return value is indirect, then the hidden argument is consuming one
1340 // integer register.
1341 if (FI.getReturnInfo().isIndirect())
1342 --freeIntRegs;
1343
1344 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1345 // get assigned (in left-to-right order) for passing as follows...
1346 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1347 it != ie; ++it) {
1348 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001349 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001350 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001351
1352 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1353 // eightbyte of an argument, the whole argument is passed on the
1354 // stack. If registers have already been assigned for some
1355 // eightbytes of such an argument, the assignments get reverted.
1356 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1357 freeIntRegs -= neededInt;
1358 freeSSERegs -= neededSSE;
1359 } else {
1360 it->info = getIndirectResult(it->type, Context);
1361 }
1362 }
1363}
1364
1365static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1366 QualType Ty,
1367 CodeGenFunction &CGF) {
1368 llvm::Value *overflow_arg_area_p =
1369 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1370 llvm::Value *overflow_arg_area =
1371 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1372
1373 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1374 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1375 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1376 if (Align > 8) {
1377 // Note that we follow the ABI & gcc here, even though the type
1378 // could in theory have an alignment greater than 16. This case
1379 // shouldn't ever matter in practice.
1380
1381 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001382 llvm::Value *Offset =
1383 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001384 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1385 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001386 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1387 llvm::Value *Mask = llvm::ConstantInt::get(
1388 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001389 overflow_arg_area =
1390 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1391 overflow_arg_area->getType(),
1392 "overflow_arg_area.align");
1393 }
1394
1395 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1396 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1397 llvm::Value *Res =
1398 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001399 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001400
1401 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1402 // l->overflow_arg_area + sizeof(type).
1403 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1404 // an 8 byte boundary.
1405
1406 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001407 llvm::Value *Offset =
1408 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001409 (SizeInBytes + 7) & ~7);
1410 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1411 "overflow_arg_area.next");
1412 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1413
1414 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1415 return Res;
1416}
1417
1418llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1419 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001420 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001421 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1422 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001423
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001424 // Assume that va_list type is correct; should be pointer to LLVM type:
1425 // struct {
1426 // i32 gp_offset;
1427 // i32 fp_offset;
1428 // i8* overflow_arg_area;
1429 // i8* reg_save_area;
1430 // };
1431 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001432
1433 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Anderson170229f2009-07-14 23:10:40 +00001434 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001435 neededInt, neededSSE);
1436
1437 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1438 // in the registers. If not go to step 7.
1439 if (!neededInt && !neededSSE)
1440 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1441
1442 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1443 // general purpose registers needed to pass type and num_fp to hold
1444 // the number of floating point registers needed.
1445
1446 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1447 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1448 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1449 //
1450 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1451 // register save space).
1452
1453 llvm::Value *InRegs = 0;
1454 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1455 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1456 if (neededInt) {
1457 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1458 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1459 InRegs =
1460 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001461 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001462 48 - neededInt * 8),
1463 "fits_in_gp");
1464 }
1465
1466 if (neededSSE) {
1467 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1468 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1469 llvm::Value *FitsInFP =
1470 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001471 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001472 176 - neededSSE * 16),
1473 "fits_in_fp");
1474 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1475 }
1476
1477 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1478 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1479 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1480 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1481
1482 // Emit code to load the value if it was passed in registers.
1483
1484 CGF.EmitBlock(InRegBlock);
1485
1486 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1487 // an offset of l->gp_offset and/or l->fp_offset. This may require
1488 // copying to a temporary location in case the parameter is passed
1489 // in different register classes or requires an alignment greater
1490 // than 8 for general purpose registers and 16 for XMM registers.
1491 //
1492 // FIXME: This really results in shameful code when we end up needing to
1493 // collect arguments from different places; often what should result in a
1494 // simple assembling of a structure from scattered addresses has many more
1495 // loads than necessary. Can we clean this up?
1496 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1497 llvm::Value *RegAddr =
1498 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1499 "reg_save_area");
1500 if (neededInt && neededSSE) {
1501 // FIXME: Cleanup.
1502 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1503 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1504 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1505 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1506 const llvm::Type *TyLo = ST->getElementType(0);
1507 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001508 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001509 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001510 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1511 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001512 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1513 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001514 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1515 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001516 llvm::Value *V =
1517 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1518 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1519 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1520 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1521
Owen Anderson170229f2009-07-14 23:10:40 +00001522 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001523 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001524 } else if (neededInt) {
1525 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1526 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001527 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001528 } else {
1529 if (neededSSE == 1) {
1530 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1531 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001532 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001533 } else {
1534 assert(neededSSE == 2 && "Invalid number of needed registers!");
1535 // SSE registers are spaced 16 bytes apart in the register save
1536 // area, we need to collect the two eightbytes together.
1537 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1538 llvm::Value *RegAddrHi =
1539 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001540 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001541 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001542 llvm::PointerType::getUnqual(DoubleTy);
1543 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1544 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001545 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1546 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1547 DblPtrTy));
1548 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1549 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1550 DblPtrTy));
1551 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1552 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001553 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001554 }
1555 }
1556
1557 // AMD64-ABI 3.5.7p5: Step 5. Set:
1558 // l->gp_offset = l->gp_offset + num_gp * 8
1559 // l->fp_offset = l->fp_offset + num_fp * 16.
1560 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001561 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001562 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1563 gp_offset_p);
1564 }
1565 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001566 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001567 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1568 fp_offset_p);
1569 }
1570 CGF.EmitBranch(ContBlock);
1571
1572 // Emit code to load the value if it was passed in memory.
1573
1574 CGF.EmitBlock(InMemBlock);
1575 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1576
1577 // Return the appropriate result.
1578
1579 CGF.EmitBlock(ContBlock);
1580 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1581 "vaarg.addr");
1582 ResAddr->reserveOperandSpace(2);
1583 ResAddr->addIncoming(RegAddr, InRegBlock);
1584 ResAddr->addIncoming(MemAddr, InMemBlock);
1585
1586 return ResAddr;
1587}
1588
Daniel Dunbard59655c2009-09-12 00:59:49 +00001589// PIC16 ABI Implementation
1590
1591namespace {
1592
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001593class PIC16ABIInfo : public ABIInfo {
1594 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001595 ASTContext &Context,
1596 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001597
1598 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001599 ASTContext &Context,
1600 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001601
Owen Anderson170229f2009-07-14 23:10:40 +00001602 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1603 llvm::LLVMContext &VMContext) const {
1604 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1605 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001606 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1607 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001608 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001609 }
1610
1611 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1612 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001613};
1614
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001615class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1616public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001617 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001618};
1619
Daniel Dunbard59655c2009-09-12 00:59:49 +00001620}
1621
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001622ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001623 ASTContext &Context,
1624 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001625 if (RetTy->isVoidType()) {
1626 return ABIArgInfo::getIgnore();
1627 } else {
1628 return ABIArgInfo::getDirect();
1629 }
1630}
1631
1632ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001633 ASTContext &Context,
1634 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001635 return ABIArgInfo::getDirect();
1636}
1637
1638llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1639 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001640 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001641 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1642
1643 CGBuilderTy &Builder = CGF.Builder;
1644 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1645 "ap");
1646 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1647 llvm::Type *PTy =
1648 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1649 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1650
1651 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1652
1653 llvm::Value *NextAddr =
1654 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1655 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1656 "ap.next");
1657 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1658
1659 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001660}
1661
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001662
John McCallea8d8bb2010-03-11 00:10:12 +00001663// PowerPC-32
1664
1665namespace {
1666class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1667public:
1668 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1669 // This is recovered from gcc output.
1670 return 1; // r1 is the dedicated stack pointer
1671 }
1672
1673 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1674 llvm::Value *Address) const;
1675};
1676
1677}
1678
1679bool
1680PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1681 llvm::Value *Address) const {
1682 // This is calculated from the LLVM and GCC tables and verified
1683 // against gcc output. AFAIK all ABIs use the same encoding.
1684
1685 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1686 llvm::LLVMContext &Context = CGF.getLLVMContext();
1687
1688 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1689 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1690 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1691 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1692
1693 // 0-31: r0-31, the 4-byte general-purpose registers
1694 for (unsigned I = 0, E = 32; I != E; ++I) {
1695 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1696 Builder.CreateStore(Four8, Slot);
1697 }
1698
1699 // 32-63: fp0-31, the 8-byte floating-point registers
1700 for (unsigned I = 32, E = 64; I != E; ++I) {
1701 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1702 Builder.CreateStore(Eight8, Slot);
1703 }
1704
1705 // 64-76 are various 4-byte special-purpose registers:
1706 // 64: mq
1707 // 65: lr
1708 // 66: ctr
1709 // 67: ap
1710 // 68-75 cr0-7
1711 // 76: xer
1712 for (unsigned I = 64, E = 77; I != E; ++I) {
1713 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1714 Builder.CreateStore(Four8, Slot);
1715 }
1716
1717 // 77-108: v0-31, the 16-byte vector registers
1718 for (unsigned I = 77, E = 109; I != E; ++I) {
1719 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1720 Builder.CreateStore(Sixteen8, Slot);
1721 }
1722
1723 // 109: vrsave
1724 // 110: vscr
1725 // 111: spe_acc
1726 // 112: spefscr
1727 // 113: sfp
1728 for (unsigned I = 109, E = 114; I != E; ++I) {
1729 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1730 Builder.CreateStore(Four8, Slot);
1731 }
1732
1733 return false;
1734}
1735
1736
Daniel Dunbard59655c2009-09-12 00:59:49 +00001737// ARM ABI Implementation
1738
1739namespace {
1740
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001741class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001742public:
1743 enum ABIKind {
1744 APCS = 0,
1745 AAPCS = 1,
1746 AAPCS_VFP
1747 };
1748
1749private:
1750 ABIKind Kind;
1751
1752public:
1753 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1754
1755private:
1756 ABIKind getABIKind() const { return Kind; }
1757
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001758 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001759 ASTContext &Context,
1760 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001761
1762 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001763 ASTContext &Context,
1764 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001765
Owen Anderson170229f2009-07-14 23:10:40 +00001766 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1767 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001768
1769 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1770 CodeGenFunction &CGF) const;
1771};
1772
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001773class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1774public:
1775 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor0599df12010-01-22 15:41:14 +00001776 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001777
1778 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1779 return 13;
1780 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001781};
1782
Daniel Dunbard59655c2009-09-12 00:59:49 +00001783}
1784
Owen Anderson170229f2009-07-14 23:10:40 +00001785void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1786 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001787 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001788 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001789 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1790 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001791 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001792 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001793
1794 // ARM always overrides the calling convention.
1795 switch (getABIKind()) {
1796 case APCS:
1797 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1798 break;
1799
1800 case AAPCS:
1801 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1802 break;
1803
1804 case AAPCS_VFP:
1805 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1806 break;
1807 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001808}
1809
1810ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001811 ASTContext &Context,
1812 llvm::LLVMContext &VMContext) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001813 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1814 // Treat an enum type as its underlying type.
1815 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1816 Ty = EnumTy->getDecl()->getIntegerType();
1817
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001818 return (Ty->isPromotableIntegerType() ?
1819 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001820 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001821
Daniel Dunbar09d33622009-09-14 21:54:03 +00001822 // Ignore empty records.
1823 if (isEmptyRecord(Context, Ty, true))
1824 return ABIArgInfo::getIgnore();
1825
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001826 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1827 // backend doesn't support byval.
1828 // FIXME: This doesn't handle alignment > 64 bits.
1829 const llvm::Type* ElemTy;
1830 unsigned SizeRegs;
1831 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001832 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001833 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1834 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001835 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001836 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1837 }
1838 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001839 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001840 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001841 return ABIArgInfo::getCoerce(STy);
1842}
1843
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001844static bool isIntegerLikeType(QualType Ty,
1845 ASTContext &Context,
1846 llvm::LLVMContext &VMContext) {
1847 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1848 // is called integer-like if its size is less than or equal to one word, and
1849 // the offset of each of its addressable sub-fields is zero.
1850
1851 uint64_t Size = Context.getTypeSize(Ty);
1852
1853 // Check that the type fits in a word.
1854 if (Size > 32)
1855 return false;
1856
1857 // FIXME: Handle vector types!
1858 if (Ty->isVectorType())
1859 return false;
1860
Daniel Dunbard53bac72009-09-14 02:20:34 +00001861 // Float types are never treated as "integer like".
1862 if (Ty->isRealFloatingType())
1863 return false;
1864
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001865 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001866 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001867 return true;
1868
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001869 // Small complex integer types are "integer like".
1870 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1871 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001872
1873 // Single element and zero sized arrays should be allowed, by the definition
1874 // above, but they are not.
1875
1876 // Otherwise, it must be a record type.
1877 const RecordType *RT = Ty->getAs<RecordType>();
1878 if (!RT) return false;
1879
1880 // Ignore records with flexible arrays.
1881 const RecordDecl *RD = RT->getDecl();
1882 if (RD->hasFlexibleArrayMember())
1883 return false;
1884
1885 // Check that all sub-fields are at offset 0, and are themselves "integer
1886 // like".
1887 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1888
1889 bool HadField = false;
1890 unsigned idx = 0;
1891 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1892 i != e; ++i, ++idx) {
1893 const FieldDecl *FD = *i;
1894
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001895 // Bit-fields are not addressable, we only need to verify they are "integer
1896 // like". We still have to disallow a subsequent non-bitfield, for example:
1897 // struct { int : 0; int x }
1898 // is non-integer like according to gcc.
1899 if (FD->isBitField()) {
1900 if (!RD->isUnion())
1901 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001902
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001903 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1904 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001905
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001906 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001907 }
1908
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001909 // Check if this field is at offset 0.
1910 if (Layout.getFieldOffset(idx) != 0)
1911 return false;
1912
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001913 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1914 return false;
1915
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001916 // Only allow at most one field in a structure. This doesn't match the
1917 // wording above, but follows gcc in situations with a field following an
1918 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001919 if (!RD->isUnion()) {
1920 if (HadField)
1921 return false;
1922
1923 HadField = true;
1924 }
1925 }
1926
1927 return true;
1928}
1929
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001930ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001931 ASTContext &Context,
1932 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001933 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001934 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001935
Douglas Gregora71cc152010-02-02 20:10:50 +00001936 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1937 // Treat an enum type as its underlying type.
1938 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1939 RetTy = EnumTy->getDecl()->getIntegerType();
1940
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001941 return (RetTy->isPromotableIntegerType() ?
1942 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001943 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001944
1945 // Are we following APCS?
1946 if (getABIKind() == APCS) {
1947 if (isEmptyRecord(Context, RetTy, false))
1948 return ABIArgInfo::getIgnore();
1949
Daniel Dunbareedf1512010-02-01 23:31:19 +00001950 // Complex types are all returned as packed integers.
1951 //
1952 // FIXME: Consider using 2 x vector types if the back end handles them
1953 // correctly.
1954 if (RetTy->isAnyComplexType())
1955 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1956 VMContext, Context.getTypeSize(RetTy)));
1957
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001958 // Integer like structures are returned in r0.
1959 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1960 // Return in the smallest viable integer type.
1961 uint64_t Size = Context.getTypeSize(RetTy);
1962 if (Size <= 8)
1963 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1964 if (Size <= 16)
1965 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1966 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1967 }
1968
1969 // Otherwise return in memory.
1970 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001971 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001972
1973 // Otherwise this is an AAPCS variant.
1974
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001975 if (isEmptyRecord(Context, RetTy, true))
1976 return ABIArgInfo::getIgnore();
1977
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001978 // Aggregates <= 4 bytes are returned in r0; other aggregates
1979 // are returned indirectly.
1980 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001981 if (Size <= 32) {
1982 // Return in the smallest viable integer type.
1983 if (Size <= 8)
1984 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1985 if (Size <= 16)
1986 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001987 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001988 }
1989
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001990 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001991}
1992
1993llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1994 CodeGenFunction &CGF) const {
1995 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001996 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00001997 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001998
1999 CGBuilderTy &Builder = CGF.Builder;
2000 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2001 "ap");
2002 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2003 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002004 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002005 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2006
2007 uint64_t Offset =
2008 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2009 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00002010 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
2011 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002012 "ap.next");
2013 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2014
2015 return AddrTyped;
2016}
2017
2018ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00002019 ASTContext &Context,
2020 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002021 if (RetTy->isVoidType()) {
2022 return ABIArgInfo::getIgnore();
2023 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2024 return ABIArgInfo::getIndirect(0);
2025 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +00002026 // Treat an enum type as its underlying type.
2027 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2028 RetTy = EnumTy->getDecl()->getIntegerType();
2029
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002030 return (RetTy->isPromotableIntegerType() ?
2031 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002032 }
2033}
2034
Daniel Dunbard59655c2009-09-12 00:59:49 +00002035// SystemZ ABI Implementation
2036
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002037namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002038
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002039class SystemZABIInfo : public ABIInfo {
2040 bool isPromotableIntegerType(QualType Ty) const;
2041
2042 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2043 llvm::LLVMContext &VMContext) const;
2044
2045 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2046 llvm::LLVMContext &VMContext) const;
2047
2048 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2049 llvm::LLVMContext &VMContext) const {
2050 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2051 Context, VMContext);
2052 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2053 it != ie; ++it)
2054 it->info = classifyArgumentType(it->type, Context, VMContext);
2055 }
2056
2057 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2058 CodeGenFunction &CGF) const;
2059};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002060
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002061class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2062public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002063 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002064};
2065
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002066}
2067
2068bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2069 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002070 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002071 switch (BT->getKind()) {
2072 case BuiltinType::Bool:
2073 case BuiltinType::Char_S:
2074 case BuiltinType::Char_U:
2075 case BuiltinType::SChar:
2076 case BuiltinType::UChar:
2077 case BuiltinType::Short:
2078 case BuiltinType::UShort:
2079 case BuiltinType::Int:
2080 case BuiltinType::UInt:
2081 return true;
2082 default:
2083 return false;
2084 }
2085 return false;
2086}
2087
2088llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2089 CodeGenFunction &CGF) const {
2090 // FIXME: Implement
2091 return 0;
2092}
2093
2094
2095ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2096 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002097 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002098 if (RetTy->isVoidType()) {
2099 return ABIArgInfo::getIgnore();
2100 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2101 return ABIArgInfo::getIndirect(0);
2102 } else {
2103 return (isPromotableIntegerType(RetTy) ?
2104 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2105 }
2106}
2107
2108ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2109 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002110 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002111 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2112 return ABIArgInfo::getIndirect(0);
2113 } else {
2114 return (isPromotableIntegerType(Ty) ?
2115 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2116 }
2117}
2118
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002119// MSP430 ABI Implementation
2120
2121namespace {
2122
2123class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2124public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002125 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002126 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2127 CodeGen::CodeGenModule &M) const;
2128};
2129
2130}
2131
2132void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2133 llvm::GlobalValue *GV,
2134 CodeGen::CodeGenModule &M) const {
2135 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2136 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2137 // Handle 'interrupt' attribute:
2138 llvm::Function *F = cast<llvm::Function>(GV);
2139
2140 // Step 1: Set ISR calling convention.
2141 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2142
2143 // Step 2: Add attributes goodness.
2144 F->addFnAttr(llvm::Attribute::NoInline);
2145
2146 // Step 3: Emit ISR vector alias.
2147 unsigned Num = attr->getNumber() + 0xffe0;
2148 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2149 "vector_" +
2150 llvm::LowercaseString(llvm::utohexstr(Num)),
2151 GV, &M.getModule());
2152 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002153 }
2154}
2155
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002156const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2157 if (TheTargetCodeGenInfo)
2158 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002159
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002160 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2161 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002162
Daniel Dunbar40165182009-08-24 09:10:05 +00002163 const llvm::Triple &Triple(getContext().Target.getTriple());
2164 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002165 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002166 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbare3532f82009-08-24 08:52:16 +00002167
Daniel Dunbard59655c2009-09-12 00:59:49 +00002168 case llvm::Triple::arm:
2169 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002170 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002171 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002172 return *(TheTargetCodeGenInfo =
2173 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002174
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002175 return *(TheTargetCodeGenInfo =
2176 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002177
2178 case llvm::Triple::pic16:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002179 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002180
John McCallea8d8bb2010-03-11 00:10:12 +00002181 case llvm::Triple::ppc:
2182 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2183
Daniel Dunbard59655c2009-09-12 00:59:49 +00002184 case llvm::Triple::systemz:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002185 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2186
2187 case llvm::Triple::msp430:
2188 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002189
Daniel Dunbar40165182009-08-24 09:10:05 +00002190 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002191 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002192 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002193 return *(TheTargetCodeGenInfo =
2194 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002195 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002196 case llvm::Triple::MinGW32:
2197 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002198 case llvm::Triple::AuroraUX:
2199 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002200 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002201 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002202 return *(TheTargetCodeGenInfo =
2203 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002204
2205 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002206 return *(TheTargetCodeGenInfo =
2207 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002208 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002209
Daniel Dunbare3532f82009-08-24 08:52:16 +00002210 case llvm::Triple::x86_64:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002211 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00002212 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002213}