blob: 4db36d9140ce58715e2b8113d2d16629d6533d2f [file] [log] [blame]
Daniel Dunbara8f02052008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
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
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
Daniel Dunbar51a2d192009-01-29 08:13:58 +000022#include "clang/AST/RecordLayout.h"
Daniel Dunbar04d35782008-09-17 00:51:38 +000023#include "llvm/ADT/StringExtras.h"
Devang Patel98bfe502008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbar90e43452009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbare09a9692009-01-24 08:32:22 +000026#include "llvm/Support/CommandLine.h"
Daniel Dunbar3cfcec72009-02-12 09:04:14 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbar9f4874e2009-02-04 23:24:38 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000029#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000030
31#include "ABIInfo.h"
32
Daniel Dunbara8f02052008-09-08 21:33:45 +000033using namespace clang;
34using namespace CodeGen;
35
36/***/
37
Daniel Dunbara8f02052008-09-08 21:33:45 +000038// FIXME: Use iterator and sidestep silly type array creation.
39
Daniel Dunbar34bda882009-02-02 23:23:47 +000040const
Douglas Gregor4fa58902009-02-26 23:50:07 +000041CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000042 return getFunctionInfo(FTNP->getResultType(),
43 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000044}
45
Daniel Dunbar34bda882009-02-02 23:23:47 +000046const
Douglas Gregor4fa58902009-02-26 23:50:07 +000047CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000048 llvm::SmallVector<QualType, 16> ArgTys;
49 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000050 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000051 ArgTys.push_back(FTP->getArgType(i));
52 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000053}
54
Daniel Dunbar34bda882009-02-02 23:23:47 +000055const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Daniel Dunbara8f02052008-09-08 21:33:45 +000056 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +000057 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar34bda882009-02-02 23:23:47 +000058 return getFunctionInfo(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +000059 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000060}
61
Daniel Dunbar34bda882009-02-02 23:23:47 +000062const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
63 llvm::SmallVector<QualType, 16> ArgTys;
64 ArgTys.push_back(MD->getSelfDecl()->getType());
65 ArgTys.push_back(Context.getObjCSelType());
66 // FIXME: Kill copy?
Chris Lattner9408eb12009-02-20 06:23:21 +000067 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000068 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000069 ArgTys.push_back((*i)->getType());
70 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000071}
72
Daniel Dunbar34bda882009-02-02 23:23:47 +000073const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
74 const CallArgList &Args) {
75 // FIXME: Kill copy.
76 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000077 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000079 ArgTys.push_back(i->second);
80 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000081}
82
Daniel Dunbar34bda882009-02-02 23:23:47 +000083const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
84 const FunctionArgList &Args) {
85 // FIXME: Kill copy.
86 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar9fc15a82009-02-02 21:43:58 +000087 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000089 ArgTys.push_back(i->second);
90 return getFunctionInfo(ResTy, ArgTys);
91}
92
93const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbardcf19d12009-02-03 00:07:12 +000095 // Lookup or create unique function info.
96 llvm::FoldingSetNodeID ID;
97 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99 void *InsertPos = 0;
100 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101 if (FI)
102 return *FI;
103
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000104 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000105 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000106 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000107
108 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000109 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000110
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000111 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000112}
113
114/***/
115
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000116ABIInfo::~ABIInfo() {}
117
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000118void ABIArgInfo::dump() const {
119 fprintf(stderr, "(ABIArgInfo Kind=");
120 switch (TheKind) {
121 case Direct:
122 fprintf(stderr, "Direct");
123 break;
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000124 case Ignore:
125 fprintf(stderr, "Ignore");
126 break;
127 case Coerce:
128 fprintf(stderr, "Coerce Type=");
129 getCoerceToType()->print(llvm::errs());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000130 break;
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000131 case Indirect:
132 fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
Daniel Dunbar9f4874e2009-02-04 23:24:38 +0000133 break;
134 case Expand:
135 fprintf(stderr, "Expand");
136 break;
137 }
138 fprintf(stderr, ")\n");
139}
140
141/***/
142
Daniel Dunbara7446422009-03-31 19:01:39 +0000143/// isEmptyRecord - Return true iff a structure has no non-empty
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
Daniel Dunbara7446422009-03-31 19:01:39 +0000146static bool isEmptyRecord(QualType T) {
147 const RecordType *RT = T->getAsRecordType();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000148 if (!RT)
149 return 0;
150 const RecordDecl *RD = RT->getDecl();
151 if (RD->hasFlexibleArrayMember())
152 return false;
Douglas Gregor5d764842009-01-09 17:18:27 +0000153 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000154 e = RD->field_end(); i != e; ++i) {
155 const FieldDecl *FD = *i;
Daniel Dunbara7446422009-03-31 19:01:39 +0000156 if (!isEmptyRecord(FD->getType()))
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000157 return false;
158 }
159 return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000170static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000171 const RecordType *RT = T->getAsStructureType();
172 if (!RT)
173 return 0;
174
175 const RecordDecl *RD = RT->getDecl();
176 if (RD->hasFlexibleArrayMember())
177 return 0;
178
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000179 const Type *Found = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000180 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000181 e = RD->field_end(); i != e; ++i) {
182 const FieldDecl *FD = *i;
183 QualType FT = FD->getType();
184
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000185 // Treat single element arrays as the element
186 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
187 if (AT->getSize().getZExtValue() == 1)
188 FT = AT->getElementType();
189
Daniel Dunbara7446422009-03-31 19:01:39 +0000190 if (isEmptyRecord(FT)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000191 // Ignore
192 } else if (Found) {
193 return 0;
194 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000195 Found = FT.getTypePtr();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000196 } else {
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000197 Found = isSingleElementStruct(FT, Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000198 if (!Found)
199 return 0;
200 }
201 }
202
203 return Found;
204}
205
206static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
207 if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
208 return false;
209
210 uint64_t Size = Context.getTypeSize(Ty);
211 return Size == 32 || Size == 64;
212}
213
214static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
215 ASTContext &Context) {
Douglas Gregor5d764842009-01-09 17:18:27 +0000216 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000217 e = RD->field_end(); i != e; ++i) {
218 const FieldDecl *FD = *i;
219
220 if (!is32Or64BitBasicType(FD->getType(), Context))
221 return false;
222
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000223 // FIXME: Reject bitfields wholesale; there are two problems, we
224 // don't know how to expand them yet, and the predicate for
225 // telling if a bitfield still counts as "basic" is more
226 // complicated than what we were doing previously.
227 if (FD->isBitField())
228 return false;
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000229 }
Daniel Dunbar9f052cb2009-03-11 22:05:26 +0000230
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000231 return true;
232}
233
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000234namespace {
235/// DefaultABIInfo - The default implementation for ABI specific
236/// details. This implementation provides information which results in
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000237/// self-consistent and sensible LLVM IR generation, but does not
238/// conform to any particular ABI.
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000239class DefaultABIInfo : public ABIInfo {
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000240 ABIArgInfo classifyReturnType(QualType RetTy,
241 ASTContext &Context) const;
242
243 ABIArgInfo classifyArgumentType(QualType RetTy,
244 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000245
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000246 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
247 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
248 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
249 it != ie; ++it)
250 it->info = classifyArgumentType(it->type, Context);
251 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000252
253 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
254 CodeGenFunction &CGF) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000255};
256
257/// X86_32ABIInfo - The X86-32 ABI information.
258class X86_32ABIInfo : public ABIInfo {
Eli Friedman5e175802009-03-23 23:26:24 +0000259 bool IsDarwin;
260
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000261 static bool isRegisterSize(unsigned Size) {
262 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
263 }
264
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000265 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
266
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000267public:
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000268 ABIArgInfo classifyReturnType(QualType RetTy,
269 ASTContext &Context) const;
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000270
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000271 ABIArgInfo classifyArgumentType(QualType RetTy,
272 ASTContext &Context) const;
273
274 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
275 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
276 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
277 it != ie; ++it)
278 it->info = classifyArgumentType(it->type, Context);
279 }
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000280
281 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
282 CodeGenFunction &CGF) const;
Eli Friedman5e175802009-03-23 23:26:24 +0000283
284 X86_32ABIInfo(bool d) : ABIInfo(), IsDarwin(d) {}
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000285};
286}
287
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000288
289/// shouldReturnTypeInRegister - Determine if the given type should be
290/// passed in a register (for the Darwin ABI).
291bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
292 ASTContext &Context) {
293 uint64_t Size = Context.getTypeSize(Ty);
294
295 // Type must be register sized.
296 if (!isRegisterSize(Size))
297 return false;
298
299 if (Ty->isVectorType()) {
300 // 64- and 128- bit vectors inside structures are not returned in
301 // registers.
302 if (Size == 64 || Size == 128)
303 return false;
304
305 return true;
306 }
307
308 // If this is a builtin, pointer, or complex type, it is ok.
309 if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType())
310 return true;
311
312 // Arrays are treated like records.
313 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
314 return shouldReturnTypeInRegister(AT->getElementType(), Context);
315
316 // Otherwise, it must be a record type.
317 const RecordType *RT = Ty->getAsRecordType();
318 if (!RT) return false;
319
320 // Structure types are passed in register if all fields would be
321 // passed in a register.
322 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
323 e = RT->getDecl()->field_end(); i != e; ++i) {
324 const FieldDecl *FD = *i;
325
326 // FIXME: Reject bitfields wholesale for now; this is incorrect.
327 if (FD->isBitField())
328 return false;
329
330 // Empty structures are ignored.
331 if (isEmptyRecord(FD->getType()))
332 continue;
333
334 // Check fields recursively.
335 if (!shouldReturnTypeInRegister(FD->getType(), Context))
336 return false;
337 }
338
339 return true;
340}
341
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000342ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
343 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +0000344 if (RetTy->isVoidType()) {
345 return ABIArgInfo::getIgnore();
Daniel Dunbar2a7bb3f2009-04-01 06:13:08 +0000346 } else if (const VectorType *VT = RetTy->getAsVectorType()) {
347 // On Darwin, some vectors are returned in registers.
348 if (IsDarwin) {
349 uint64_t Size = Context.getTypeSize(RetTy);
350
351 // 128-bit vectors are a special case; they are returned in
352 // registers and we need to make sure to pick a type the LLVM
353 // backend will like.
354 if (Size == 128)
355 return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty,
356 2));
357
358 // Always return in register if it fits in a general purpose
359 // register, or if it is 64 bits and has a single element.
360 if ((Size == 8 || Size == 16 || Size == 32) ||
361 (Size == 64 && VT->getNumElements() == 1))
362 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
363
364 return ABIArgInfo::getIndirect(0);
365 }
366
367 return ABIArgInfo::getDirect();
Daniel Dunbareec02622009-02-03 06:30:17 +0000368 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedman5e175802009-03-23 23:26:24 +0000369 // Outside of Darwin, structs and unions are always indirect.
370 if (!IsDarwin && !RetTy->isAnyComplexType())
371 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000372 // Classify "single element" structs as their element type.
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000373 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000374 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
375 // FIXME: This is gross, it would be nice if we could just
376 // pass back SeltTy and have clients deal with it. Is it worth
377 // supporting coerce to both LLVM and clang Types?
378 if (BT->isIntegerType()) {
379 uint64_t Size = Context.getTypeSize(SeltTy);
380 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
381 } else if (BT->getKind() == BuiltinType::Float) {
382 return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
383 } else if (BT->getKind() == BuiltinType::Double) {
384 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
385 }
386 } else if (SeltTy->isPointerType()) {
387 // FIXME: It would be really nice if this could come out as
388 // the proper pointer type.
389 llvm::Type *PtrTy =
390 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
391 return ABIArgInfo::getCoerce(PtrTy);
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000392 } else if (SeltTy->isVectorType()) {
393 // 64- and 128-bit vectors are never returned in a
394 // register when inside a structure.
395 uint64_t Size = Context.getTypeSize(RetTy);
396 if (Size == 64 || Size == 128)
397 return ABIArgInfo::getIndirect(0);
398
399 return classifyReturnType(QualType(SeltTy, 0), Context);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000400 }
401 }
402
Daniel Dunbar73d66602008-09-10 07:04:09 +0000403 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar558e7fb2009-04-01 07:45:00 +0000404 if (isRegisterSize(Size)) {
405 // Always return in register for unions for now.
406 // FIXME: This is wrong, but better than treating as a
407 // structure.
408 if (RetTy->isUnionType())
409 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
410
411 // Small structures which are register sized are generally returned
412 // in a register.
413 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context))
414 return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size));
415 }
Daniel Dunbar49b32d42009-04-01 07:08:38 +0000416
417 return ABIArgInfo::getIndirect(0);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000418 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000419 return ABIArgInfo::getDirect();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000420 }
421}
422
Daniel Dunbarf98eeff2008-10-13 17:02:26 +0000423ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000424 ASTContext &Context) const {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000425 // FIXME: Set alignment on indirect arguments.
Daniel Dunbar3158c592008-09-17 20:11:04 +0000426 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000427 // Structures with flexible arrays are always indirect.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000428 if (const RecordType *RT = Ty->getAsStructureType())
429 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000430 return ABIArgInfo::getIndirect(0);
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000431
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000432 // Ignore empty structs.
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000433 uint64_t Size = Context.getTypeSize(Ty);
434 if (Ty->isStructureType() && Size == 0)
Daniel Dunbar33b189a2009-02-05 01:50:07 +0000435 return ABIArgInfo::getIgnore();
Daniel Dunbar99eebc62008-09-17 21:22:33 +0000436
437 // Expand structs with size <= 128-bits which consist only of
438 // basic types (int, long long, float, double, xxx*). This is
439 // non-recursive and does not ignore empty fields.
440 if (const RecordType *RT = Ty->getAsStructureType()) {
441 if (Context.getTypeSize(Ty) <= 4*32 &&
442 areAllFields32Or64BitBasicType(RT->getDecl(), Context))
443 return ABIArgInfo::getExpand();
444 }
445
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000446 return ABIArgInfo::getIndirect(0);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000447 } else {
Daniel Dunbareec02622009-02-03 06:30:17 +0000448 return ABIArgInfo::getDirect();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000449 }
450}
451
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000452llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
453 CodeGenFunction &CGF) const {
454 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
455 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
456
457 CGBuilderTy &Builder = CGF.Builder;
458 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
459 "ap");
460 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
461 llvm::Type *PTy =
462 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
463 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
464
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000465 uint64_t Offset =
466 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000467 llvm::Value *NextAddr =
468 Builder.CreateGEP(Addr,
Daniel Dunbarbae4b662009-02-18 22:28:45 +0000469 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000470 "ap.next");
471 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
472
473 return AddrTyped;
474}
475
Daniel Dunbare09a9692009-01-24 08:32:22 +0000476namespace {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000477/// X86_64ABIInfo - The X86_64 ABI information.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000478class X86_64ABIInfo : public ABIInfo {
479 enum Class {
480 Integer = 0,
481 SSE,
482 SSEUp,
483 X87,
484 X87Up,
485 ComplexX87,
486 NoClass,
487 Memory
488 };
489
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000490 /// merge - Implement the X86_64 ABI merging algorithm.
491 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000492 /// Merge an accumulating classification \arg Accum with a field
493 /// classification \arg Field.
494 ///
495 /// \param Accum - The accumulating classification. This should
496 /// always be either NoClass or the result of a previous merge
497 /// call. In addition, this should never be Memory (the caller
498 /// should just return Memory for the aggregate).
499 Class merge(Class Accum, Class Field) const;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000500
Daniel Dunbare09a9692009-01-24 08:32:22 +0000501 /// classify - Determine the x86_64 register classes in which the
502 /// given type T should be passed.
503 ///
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000504 /// \param Lo - The classification for the parts of the type
505 /// residing in the low word of the containing object.
506 ///
507 /// \param Hi - The classification for the parts of the type
508 /// residing in the high word of the containing object.
509 ///
510 /// \param OffsetBase - The bit offset of this type in the
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000511 /// containing object. Some parameters are classified different
512 /// depending on whether they straddle an eightbyte boundary.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000513 ///
514 /// If a word is unused its result will be NoClass; if a type should
515 /// be passed in Memory then at least the classification of \arg Lo
516 /// will be Memory.
517 ///
518 /// The \arg Lo class will be NoClass iff the argument is ignored.
519 ///
520 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
Daniel Dunbar92e88642009-02-17 07:55:55 +0000521 /// also be ComplexX87.
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000522 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000523 Class &Lo, Class &Hi) const;
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000524
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000525 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
526 /// to coerce to, chose the best way to pass Ty in the same place
527 /// that \arg CoerceTo would be passed, but while keeping the
528 /// emitted code as simple as possible.
529 ///
530 /// FIXME: Note, this should be cleaned up to just take an
531 /// enumeration of all the ways we might want to pass things,
532 /// instead of constructing an LLVM type. This makes this code more
533 /// explicit, and it makes it clearer that we are also doing this
534 /// for correctness in the case of passing scalar types.
535 ABIArgInfo getCoerceResult(QualType Ty,
536 const llvm::Type *CoerceTo,
537 ASTContext &Context) const;
538
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000539 ABIArgInfo classifyReturnType(QualType RetTy,
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000540 ASTContext &Context) const;
541
542 ABIArgInfo classifyArgumentType(QualType Ty,
543 ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000544 unsigned &neededInt,
545 unsigned &neededSSE) const;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000546
547public:
548 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000549
550 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
551 CodeGenFunction &CGF) const;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000552};
553}
554
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000555X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
556 Class Field) const {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000557 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
558 // classified recursively so that always two fields are
559 // considered. The resulting class is calculated according to
560 // the classes of the fields in the eightbyte:
561 //
562 // (a) If both classes are equal, this is the resulting class.
563 //
564 // (b) If one of the classes is NO_CLASS, the resulting class is
565 // the other class.
566 //
567 // (c) If one of the classes is MEMORY, the result is the MEMORY
568 // class.
569 //
570 // (d) If one of the classes is INTEGER, the result is the
571 // INTEGER.
572 //
573 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
574 // MEMORY is used as class.
575 //
576 // (f) Otherwise class SSE is used.
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000577
578 // Accum should never be memory (we should have returned) or
579 // ComplexX87 (because this cannot be passed in a structure).
580 assert((Accum != Memory && Accum != ComplexX87) &&
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000581 "Invalid accumulated classification during merge.");
582 if (Accum == Field || Field == NoClass)
583 return Accum;
584 else if (Field == Memory)
585 return Memory;
586 else if (Accum == NoClass)
587 return Field;
588 else if (Accum == Integer || Field == Integer)
589 return Integer;
590 else if (Field == X87 || Field == X87Up || Field == ComplexX87)
591 return Memory;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000592 else
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000593 return SSE;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000594}
595
Daniel Dunbare09a9692009-01-24 08:32:22 +0000596void X86_64ABIInfo::classify(QualType Ty,
597 ASTContext &Context,
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000598 uint64_t OffsetBase,
Daniel Dunbare09a9692009-01-24 08:32:22 +0000599 Class &Lo, Class &Hi) const {
Daniel Dunbar36b378e2009-02-02 18:06:39 +0000600 // FIXME: This code can be simplified by introducing a simple value
601 // class for Class pairs with appropriate constructor methods for
602 // the various situations.
603
Daniel Dunbard97f5952009-02-22 04:48:22 +0000604 // FIXME: Some of the split computations are wrong; unaligned
605 // vectors shouldn't be passed in registers for example, so there is
606 // no chance they can straddle an eightbyte. Verify & simplify.
607
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000608 Lo = Hi = NoClass;
609
610 Class &Current = OffsetBase < 64 ? Lo : Hi;
611 Current = Memory;
612
Daniel Dunbare09a9692009-01-24 08:32:22 +0000613 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
614 BuiltinType::Kind k = BT->getKind();
615
Daniel Dunbar1358b202009-01-26 21:26:08 +0000616 if (k == BuiltinType::Void) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000617 Current = NoClass;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000618 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000619 Current = Integer;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000620 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000621 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000622 } else if (k == BuiltinType::LongDouble) {
623 Lo = X87;
624 Hi = X87Up;
625 }
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000626 // FIXME: _Decimal32 and _Decimal64 are SSE.
627 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
Daniel Dunbare09a9692009-01-24 08:32:22 +0000628 // FIXME: __int128 is (Integer, Integer).
Anders Carlsson1d234462009-02-26 17:31:15 +0000629 } else if (const EnumType *ET = Ty->getAsEnumType()) {
630 // Classify the underlying integer type.
631 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
Daniel Dunbarfc096bf2009-02-26 20:52:22 +0000632 } else if (Ty->hasPointerRepresentation()) {
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000633 Current = Integer;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000634 } else if (const VectorType *VT = Ty->getAsVectorType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000635 uint64_t Size = Context.getTypeSize(VT);
Daniel Dunbard97f5952009-02-22 04:48:22 +0000636 if (Size == 32) {
637 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
638 // float> as integer.
639 Current = Integer;
640
641 // If this type crosses an eightbyte boundary, it should be
642 // split.
643 uint64_t EB_Real = (OffsetBase) / 64;
644 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
645 if (EB_Real != EB_Imag)
646 Hi = Lo;
647 } else if (Size == 64) {
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000648 // gcc passes <1 x double> in memory. :(
649 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
Daniel Dunbarcdf91e82009-01-30 19:38:39 +0000650 return;
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000651
652 // gcc passes <1 x long long> as INTEGER.
653 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
654 Current = Integer;
655 else
656 Current = SSE;
Daniel Dunbare413f532009-01-30 18:40:10 +0000657
658 // If this type crosses an eightbyte boundary, it should be
659 // split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000660 if (OffsetBase && OffsetBase != 64)
Daniel Dunbare413f532009-01-30 18:40:10 +0000661 Hi = Lo;
Daniel Dunbarcf1f3be2009-01-27 02:01:34 +0000662 } else if (Size == 128) {
663 Lo = SSE;
664 Hi = SSEUp;
665 }
Daniel Dunbare09a9692009-01-24 08:32:22 +0000666 } else if (const ComplexType *CT = Ty->getAsComplexType()) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000667 QualType ET = Context.getCanonicalType(CT->getElementType());
Daniel Dunbare09a9692009-01-24 08:32:22 +0000668
Daniel Dunbare413f532009-01-30 18:40:10 +0000669 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000670 if (ET->isIntegralType()) {
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000671 if (Size <= 64)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000672 Current = Integer;
Daniel Dunbar28770fc2009-01-29 07:22:20 +0000673 else if (Size <= 128)
674 Lo = Hi = Integer;
675 } else if (ET == Context.FloatTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000676 Current = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000677 else if (ET == Context.DoubleTy)
678 Lo = Hi = SSE;
679 else if (ET == Context.LongDoubleTy)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000680 Current = ComplexX87;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000681
682 // If this complex type crosses an eightbyte boundary then it
683 // should be split.
Daniel Dunbar2a2dce32009-01-30 22:40:15 +0000684 uint64_t EB_Real = (OffsetBase) / 64;
685 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000686 if (Hi == NoClass && EB_Real != EB_Imag)
687 Hi = Lo;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000688 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
689 // Arrays are treated like structures.
690
691 uint64_t Size = Context.getTypeSize(Ty);
692
693 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
694 // than two eightbytes, ..., it has class MEMORY.
695 if (Size > 128)
696 return;
697
698 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
699 // fields, it has class MEMORY.
700 //
701 // Only need to check alignment of array base.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000702 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000703 return;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000704
705 // Otherwise implement simplified merge. We could be smarter about
706 // this, but it isn't worth it and would be harder to verify.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000707 Current = NoClass;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000708 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
709 uint64_t ArraySize = AT->getSize().getZExtValue();
710 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
711 Class FieldLo, FieldHi;
712 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000713 Lo = merge(Lo, FieldLo);
714 Hi = merge(Hi, FieldHi);
715 if (Lo == Memory || Hi == Memory)
716 break;
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000717 }
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000718
719 // Do post merger cleanup (see below). Only case we worry about is Memory.
720 if (Hi == Memory)
721 Lo = Memory;
722 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000723 } else if (const RecordType *RT = Ty->getAsRecordType()) {
Daniel Dunbar1aa2be92009-01-30 00:47:38 +0000724 uint64_t Size = Context.getTypeSize(Ty);
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000725
726 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
727 // than two eightbytes, ..., it has class MEMORY.
728 if (Size > 128)
729 return;
730
731 const RecordDecl *RD = RT->getDecl();
732
733 // Assume variable sized types are passed in memory.
734 if (RD->hasFlexibleArrayMember())
735 return;
736
737 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
738
739 // Reset Lo class, this will be recomputed.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000740 Current = NoClass;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000741 unsigned idx = 0;
742 for (RecordDecl::field_iterator i = RD->field_begin(),
743 e = RD->field_end(); i != e; ++i, ++idx) {
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000744 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000745 bool BitField = i->isBitField();
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000746
Daniel Dunbar11dc6772009-01-30 08:09:32 +0000747 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
748 // fields, it has class MEMORY.
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000749 //
750 // Note, skip this test for bitfields, see below.
751 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000752 Lo = Memory;
753 return;
754 }
755
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000756 // Classify this field.
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000757 //
758 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
759 // exceeds a single eightbyte, each is classified
760 // separately. Each eightbyte gets initialized to class
761 // NO_CLASS.
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000762 Class FieldLo, FieldHi;
Daniel Dunbard6fb35c2009-02-17 02:45:44 +0000763
764 // Bitfields require special handling, they do not force the
765 // structure to be passed in memory even if unaligned, and
766 // therefore they can straddle an eightbyte.
767 if (BitField) {
768 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
769 uint64_t Size =
770 i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
771
772 uint64_t EB_Lo = Offset / 64;
773 uint64_t EB_Hi = (Offset + Size - 1) / 64;
774 FieldLo = FieldHi = NoClass;
775 if (EB_Lo) {
776 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
777 FieldLo = NoClass;
778 FieldHi = Integer;
779 } else {
780 FieldLo = Integer;
781 FieldHi = EB_Hi ? Integer : NoClass;
782 }
783 } else
784 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000785 Lo = merge(Lo, FieldLo);
786 Hi = merge(Hi, FieldHi);
787 if (Lo == Memory || Hi == Memory)
788 break;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000789 }
790
791 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
792 //
793 // (a) If one of the classes is MEMORY, the whole argument is
794 // passed in memory.
795 //
796 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
797
798 // The first of these conditions is guaranteed by how we implement
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000799 // the merge (just bail).
800 //
801 // The second condition occurs in the case of unions; for example
802 // union { _Complex double; unsigned; }.
803 if (Hi == Memory)
804 Lo = Memory;
Daniel Dunbar51a2d192009-01-29 08:13:58 +0000805 if (Hi == SSEUp && Lo != SSE)
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000806 Hi = SSE;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000807 }
808}
809
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000810ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
811 const llvm::Type *CoerceTo,
812 ASTContext &Context) const {
813 if (CoerceTo == llvm::Type::Int64Ty) {
814 // Integer and pointer types will end up in a general purpose
815 // register.
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000816 if (Ty->isIntegralType() || Ty->isPointerType())
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000817 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000818
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000819 } else if (CoerceTo == llvm::Type::DoubleTy) {
Daniel Dunbare60d5332009-02-14 02:45:45 +0000820 // FIXME: It would probably be better to make CGFunctionInfo only
821 // map using canonical types than to canonize here.
822 QualType CTy = Context.getCanonicalType(Ty);
823
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000824 // Float and double end up in a single SSE reg.
Daniel Dunbare60d5332009-02-14 02:45:45 +0000825 if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000826 return ABIArgInfo::getDirect();
Daniel Dunbarb341feb2009-02-22 04:16:10 +0000827
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000828 }
829
830 return ABIArgInfo::getCoerce(CoerceTo);
831}
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000832
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000833ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
834 ASTContext &Context) const {
Daniel Dunbare09a9692009-01-24 08:32:22 +0000835 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
836 // classification algorithm.
837 X86_64ABIInfo::Class Lo, Hi;
Daniel Dunbar6a7f8b32009-01-29 09:42:07 +0000838 classify(RetTy, Context, 0, Lo, Hi);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000839
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000840 // Check some invariants.
841 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
842 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
843 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
844
Daniel Dunbare09a9692009-01-24 08:32:22 +0000845 const llvm::Type *ResType = 0;
846 switch (Lo) {
847 case NoClass:
Daniel Dunbar1358b202009-01-26 21:26:08 +0000848 return ABIArgInfo::getIgnore();
Daniel Dunbare09a9692009-01-24 08:32:22 +0000849
850 case SSEUp:
851 case X87Up:
852 assert(0 && "Invalid classification for lo word.");
853
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000854 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000855 // hidden argument.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000856 case Memory:
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000857 return ABIArgInfo::getIndirect(0);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000858
859 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
860 // available register of the sequence %rax, %rdx is used.
861 case Integer:
862 ResType = llvm::Type::Int64Ty; break;
863
864 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
865 // available SSE register of the sequence %xmm0, %xmm1 is used.
866 case SSE:
867 ResType = llvm::Type::DoubleTy; break;
868
869 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
870 // returned on the X87 stack in %st0 as 80-bit x87 number.
871 case X87:
872 ResType = llvm::Type::X86_FP80Ty; break;
873
Daniel Dunbar64b132f2009-01-31 00:06:58 +0000874 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
875 // part of the value is returned in %st0 and the imaginary part in
876 // %st1.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000877 case ComplexX87:
Daniel Dunbar92e88642009-02-17 07:55:55 +0000878 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Daniel Dunbar4fc0d492009-02-18 03:44:19 +0000879 ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
880 llvm::Type::X86_FP80Ty,
881 NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000882 break;
883 }
884
885 switch (Hi) {
Daniel Dunbar92e88642009-02-17 07:55:55 +0000886 // Memory was handled previously and X87 should
887 // never occur as a hi class.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000888 case Memory:
889 case X87:
Daniel Dunbare09a9692009-01-24 08:32:22 +0000890 assert(0 && "Invalid classification for hi word.");
891
Daniel Dunbar92e88642009-02-17 07:55:55 +0000892 case ComplexX87: // Previously handled.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000893 case NoClass: break;
Daniel Dunbar92e88642009-02-17 07:55:55 +0000894
Daniel Dunbare09a9692009-01-24 08:32:22 +0000895 case Integer:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000896 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
897 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000898 case SSE:
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000899 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
900 break;
Daniel Dunbare09a9692009-01-24 08:32:22 +0000901
902 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
903 // is passed in the upper half of the last used SSE register.
904 //
905 // SSEUP should always be preceeded by SSE, just widen.
906 case SSEUp:
907 assert(Lo == SSE && "Unexpected SSEUp classification.");
908 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
909 break;
910
911 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
Daniel Dunbar7e8a7022009-01-29 07:36:07 +0000912 // returned together with the previous X87 value in %st0.
Daniel Dunbare09a9692009-01-24 08:32:22 +0000913 case X87Up:
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000914 // If X87Up is preceeded by X87, we don't need to do
915 // anything. However, in some cases with unions it may not be
916 // preceeded by X87. In such situations we follow gcc and pass the
917 // extra bits in an SSE reg.
918 if (Lo != X87)
919 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
Daniel Dunbare09a9692009-01-24 08:32:22 +0000920 break;
921 }
922
Daniel Dunbar87c4dc92009-02-14 02:09:24 +0000923 return getCoerceResult(RetTy, ResType, Context);
Daniel Dunbarb6d5c442009-01-15 18:18:40 +0000924}
925
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000926ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Daniel Dunbare978cb92009-02-10 17:06:09 +0000927 unsigned &neededInt,
928 unsigned &neededSSE) const {
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000929 X86_64ABIInfo::Class Lo, Hi;
930 classify(Ty, Context, 0, Lo, Hi);
931
932 // Check some invariants.
933 // FIXME: Enforce these by construction.
934 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
935 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
936 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
937
Daniel Dunbare978cb92009-02-10 17:06:09 +0000938 neededInt = 0;
939 neededSSE = 0;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000940 const llvm::Type *ResType = 0;
941 switch (Lo) {
942 case NoClass:
943 return ABIArgInfo::getIgnore();
944
945 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
946 // on the stack.
947 case Memory:
948
949 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
950 // COMPLEX_X87, it is passed in memory.
951 case X87:
952 case ComplexX87:
Daniel Dunbard0536ac2009-02-22 08:17:51 +0000953 return ABIArgInfo::getIndirect(0);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000954
955 case SSEUp:
956 case X87Up:
957 assert(0 && "Invalid classification for lo word.");
958
959 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
960 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
961 // and %r9 is used.
962 case Integer:
963 ++neededInt;
964 ResType = llvm::Type::Int64Ty;
965 break;
966
967 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
968 // available SSE register is used, the registers are taken in the
969 // order from %xmm0 to %xmm7.
970 case SSE:
971 ++neededSSE;
972 ResType = llvm::Type::DoubleTy;
973 break;
Daniel Dunbareec02622009-02-03 06:30:17 +0000974 }
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000975
976 switch (Hi) {
977 // Memory was handled previously, ComplexX87 and X87 should
978 // never occur as hi classes, and X87Up must be preceed by X87,
979 // which is passed in memory.
980 case Memory:
981 case X87:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000982 case ComplexX87:
983 assert(0 && "Invalid classification for hi word.");
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000984 break;
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000985
986 case NoClass: break;
987 case Integer:
988 ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
989 ++neededInt;
990 break;
Daniel Dunbar78d7d452009-03-06 17:50:25 +0000991
992 // X87Up generally doesn't occur here (long double is passed in
993 // memory), except in situations involving unions.
994 case X87Up:
995 case SSE:
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000996 ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
997 ++neededSSE;
998 break;
999
1000 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1001 // eightbyte is passed in the upper half of the last used SSE
1002 // register.
1003 case SSEUp:
1004 assert(Lo == SSE && "Unexpected SSEUp classification.");
1005 ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
1006 break;
1007 }
1008
Daniel Dunbar87c4dc92009-02-14 02:09:24 +00001009 return getCoerceResult(Ty, ResType, Context);
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001010}
1011
1012void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1013 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1014
1015 // Keep track of the number of assigned registers.
1016 unsigned freeIntRegs = 6, freeSSERegs = 8;
1017
1018 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1019 // get assigned (in left-to-right order) for passing as follows...
1020 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
Daniel Dunbare978cb92009-02-10 17:06:09 +00001021 it != ie; ++it) {
1022 unsigned neededInt, neededSSE;
1023 it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
1024
1025 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1026 // eightbyte of an argument, the whole argument is passed on the
1027 // stack. If registers have already been assigned for some
1028 // eightbytes of such an argument, the assignments get reverted.
1029 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1030 freeIntRegs -= neededInt;
1031 freeSSERegs -= neededSSE;
1032 } else {
Daniel Dunbard0536ac2009-02-22 08:17:51 +00001033 it->info = ABIArgInfo::getIndirect(0);
Daniel Dunbare978cb92009-02-10 17:06:09 +00001034 }
1035 }
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001036}
1037
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001038static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1039 QualType Ty,
1040 CodeGenFunction &CGF) {
1041 llvm::Value *overflow_arg_area_p =
1042 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1043 llvm::Value *overflow_arg_area =
1044 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1045
1046 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1047 // byte boundary if alignment needed by type exceeds 8 byte boundary.
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001048 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001049 if (Align > 8) {
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001050 // Note that we follow the ABI & gcc here, even though the type
1051 // could in theory have an alignment greater than 16. This case
1052 // shouldn't ever matter in practice.
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001053
Daniel Dunbar2ab71bd2009-02-16 23:38:56 +00001054 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
1055 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
1056 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1057 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
1058 llvm::Type::Int64Ty);
1059 llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
1060 overflow_arg_area =
1061 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1062 overflow_arg_area->getType(),
1063 "overflow_arg_area.align");
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001064 }
1065
1066 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1067 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1068 llvm::Value *Res =
1069 CGF.Builder.CreateBitCast(overflow_arg_area,
1070 llvm::PointerType::getUnqual(LTy));
1071
1072 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1073 // l->overflow_arg_area + sizeof(type).
1074 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1075 // an 8 byte boundary.
1076
1077 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
1078 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1079 (SizeInBytes + 7) & ~7);
1080 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1081 "overflow_arg_area.next");
1082 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1083
1084 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1085 return Res;
1086}
1087
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001088llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1089 CodeGenFunction &CGF) const {
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001090 // Assume that va_list type is correct; should be pointer to LLVM type:
1091 // struct {
1092 // i32 gp_offset;
1093 // i32 fp_offset;
1094 // i8* overflow_arg_area;
1095 // i8* reg_save_area;
1096 // };
1097 unsigned neededInt, neededSSE;
1098 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
1099 neededInt, neededSSE);
1100
1101 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1102 // in the registers. If not go to step 7.
1103 if (!neededInt && !neededSSE)
1104 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1105
1106 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1107 // general purpose registers needed to pass type and num_fp to hold
1108 // the number of floating point registers needed.
1109
1110 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1111 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1112 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1113 //
1114 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1115 // register save space).
1116
1117 llvm::Value *InRegs = 0;
1118 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1119 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1120 if (neededInt) {
1121 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1122 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1123 InRegs =
1124 CGF.Builder.CreateICmpULE(gp_offset,
1125 llvm::ConstantInt::get(llvm::Type::Int32Ty,
1126 48 - neededInt * 8),
1127 "fits_in_gp");
1128 }
1129
1130 if (neededSSE) {
1131 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1132 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1133 llvm::Value *FitsInFP =
1134 CGF.Builder.CreateICmpULE(fp_offset,
1135 llvm::ConstantInt::get(llvm::Type::Int32Ty,
Daniel Dunbar63118762009-02-18 22:19:44 +00001136 176 - neededSSE * 16),
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001137 "fits_in_fp");
Daniel Dunbar72198842009-02-18 22:05:01 +00001138 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001139 }
1140
1141 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1142 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1143 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1144 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1145
1146 // Emit code to load the value if it was passed in registers.
1147
1148 CGF.EmitBlock(InRegBlock);
1149
1150 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1151 // an offset of l->gp_offset and/or l->fp_offset. This may require
1152 // copying to a temporary location in case the parameter is passed
1153 // in different register classes or requires an alignment greater
1154 // than 8 for general purpose registers and 16 for XMM registers.
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001155 //
1156 // FIXME: This really results in shameful code when we end up
1157 // needing to collect arguments from different places; often what
1158 // should result in a simple assembling of a structure from
1159 // scattered addresses has many more loads than necessary. Can we
1160 // clean this up?
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001161 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1162 llvm::Value *RegAddr =
1163 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1164 "reg_save_area");
1165 if (neededInt && neededSSE) {
Daniel Dunbara96ec382009-02-13 17:46:31 +00001166 // FIXME: Cleanup.
1167 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1168 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1169 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1170 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1171 const llvm::Type *TyLo = ST->getElementType(0);
1172 const llvm::Type *TyHi = ST->getElementType(1);
1173 assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1174 "Unexpected ABI info for mixed regs");
1175 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1176 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1177 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1178 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1179 llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1180 llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1181 llvm::Value *V =
1182 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1183 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1184 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1185 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1186
1187 RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001188 } else if (neededInt) {
1189 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1190 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1191 llvm::PointerType::getUnqual(LTy));
1192 } else {
Daniel Dunbar4fc0d492009-02-18 03:44:19 +00001193 if (neededSSE == 1) {
1194 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1195 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1196 llvm::PointerType::getUnqual(LTy));
1197 } else {
1198 assert(neededSSE == 2 && "Invalid number of needed registers!");
1199 // SSE registers are spaced 16 bytes apart in the register save
1200 // area, we need to collect the two eightbytes together.
1201 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1202 llvm::Value *RegAddrHi =
1203 CGF.Builder.CreateGEP(RegAddrLo,
1204 llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1205 const llvm::Type *DblPtrTy =
1206 llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1207 const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1208 llvm::Type::DoubleTy,
1209 NULL);
1210 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1211 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1212 DblPtrTy));
1213 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1214 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1215 DblPtrTy));
1216 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1217 RegAddr = CGF.Builder.CreateBitCast(Tmp,
1218 llvm::PointerType::getUnqual(LTy));
1219 }
Daniel Dunbar3cfcec72009-02-12 09:04:14 +00001220 }
1221
1222 // AMD64-ABI 3.5.7p5: Step 5. Set:
1223 // l->gp_offset = l->gp_offset + num_gp * 8
1224 // l->fp_offset = l->fp_offset + num_fp * 16.
1225 if (neededInt) {
1226 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1227 neededInt * 8);
1228 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1229 gp_offset_p);
1230 }
1231 if (neededSSE) {
1232 llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1233 neededSSE * 16);
1234 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1235 fp_offset_p);
1236 }
1237 CGF.EmitBranch(ContBlock);
1238
1239 // Emit code to load the value if it was passed in memory.
1240
1241 CGF.EmitBlock(InMemBlock);
1242 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1243
1244 // Return the appropriate result.
1245
1246 CGF.EmitBlock(ContBlock);
1247 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1248 "vaarg.addr");
1249 ResAddr->reserveOperandSpace(2);
1250 ResAddr->addIncoming(RegAddr, InRegBlock);
1251 ResAddr->addIncoming(MemAddr, InMemBlock);
1252
1253 return ResAddr;
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001254}
1255
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001256class ARMABIInfo : public ABIInfo {
1257 ABIArgInfo classifyReturnType(QualType RetTy,
1258 ASTContext &Context) const;
1259
1260 ABIArgInfo classifyArgumentType(QualType RetTy,
1261 ASTContext &Context) const;
1262
1263 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
1264
1265 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1266 CodeGenFunction &CGF) const;
1267};
1268
1269void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
1270 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
1271 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1272 it != ie; ++it) {
1273 it->info = classifyArgumentType(it->type, Context);
1274 }
1275}
1276
1277ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
1278 ASTContext &Context) const {
1279 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1280 return ABIArgInfo::getDirect();
1281 }
1282 // FIXME: This is kind of nasty... but there isn't much choice
1283 // because the ARM backend doesn't support byval.
1284 // FIXME: This doesn't handle alignment > 64 bits.
1285 const llvm::Type* ElemTy;
1286 unsigned SizeRegs;
1287 if (Context.getTypeAlign(Ty) > 32) {
1288 ElemTy = llvm::Type::Int64Ty;
1289 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1290 } else {
1291 ElemTy = llvm::Type::Int32Ty;
1292 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1293 }
1294 std::vector<const llvm::Type*> LLVMFields;
1295 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
1296 const llvm::Type* STy = llvm::StructType::get(LLVMFields, true);
1297 return ABIArgInfo::getCoerce(STy);
1298}
1299
1300ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
1301 ASTContext &Context) const {
1302 if (RetTy->isVoidType()) {
1303 return ABIArgInfo::getIgnore();
1304 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1305 // Aggregates <= 4 bytes are returned in r0; other aggregates
1306 // are returned indirectly.
1307 uint64_t Size = Context.getTypeSize(RetTy);
1308 if (Size <= 32)
1309 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
1310 return ABIArgInfo::getIndirect(0);
1311 } else {
1312 return ABIArgInfo::getDirect();
1313 }
1314}
1315
1316llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1317 CodeGenFunction &CGF) const {
1318 // FIXME: Need to handle alignment
1319 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1320 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1321
1322 CGBuilderTy &Builder = CGF.Builder;
1323 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1324 "ap");
1325 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1326 llvm::Type *PTy =
1327 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1328 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1329
1330 uint64_t Offset =
1331 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1332 llvm::Value *NextAddr =
1333 Builder.CreateGEP(Addr,
1334 llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
1335 "ap.next");
1336 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1337
1338 return AddrTyped;
1339}
1340
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001341ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001342 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001343 if (RetTy->isVoidType()) {
1344 return ABIArgInfo::getIgnore();
1345 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001346 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001347 } else {
1348 return ABIArgInfo::getDirect();
1349 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001350}
1351
1352ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001353 ASTContext &Context) const {
Daniel Dunbareec02622009-02-03 06:30:17 +00001354 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001355 return ABIArgInfo::getIndirect(0);
Daniel Dunbareec02622009-02-03 06:30:17 +00001356 } else {
1357 return ABIArgInfo::getDirect();
1358 }
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001359}
1360
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00001361llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1362 CodeGenFunction &CGF) const {
1363 return 0;
1364}
1365
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001366const ABIInfo &CodeGenTypes::getABIInfo() const {
1367 if (TheABIInfo)
1368 return *TheABIInfo;
1369
1370 // For now we just cache this in the CodeGenTypes and don't bother
1371 // to free it.
1372 const char *TargetPrefix = getContext().Target.getTargetPrefix();
1373 if (strcmp(TargetPrefix, "x86") == 0) {
Eli Friedman5e175802009-03-23 23:26:24 +00001374 bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin");
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001375 switch (getContext().Target.getPointerWidth(0)) {
1376 case 32:
Eli Friedman5e175802009-03-23 23:26:24 +00001377 return *(TheABIInfo = new X86_32ABIInfo(IsDarwin));
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001378 case 64:
Daniel Dunbar56555952009-01-30 18:47:53 +00001379 return *(TheABIInfo = new X86_64ABIInfo());
Daniel Dunbarb6d5c442009-01-15 18:18:40 +00001380 }
Eli Friedmanac90d8e2009-03-29 00:15:25 +00001381 } else if (strcmp(TargetPrefix, "arm") == 0) {
1382 // FIXME: Support for OABI?
1383 return *(TheABIInfo = new ARMABIInfo());
Daniel Dunbarf98eeff2008-10-13 17:02:26 +00001384 }
1385
1386 return *(TheABIInfo = new DefaultABIInfo);
1387}
1388
Daniel Dunbare126ab12008-09-10 02:41:04 +00001389/***/
1390
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001391CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1392 const llvm::SmallVector<QualType, 16> &ArgTys) {
1393 NumArgs = ArgTys.size();
1394 Args = new ArgInfo[1 + NumArgs];
1395 Args[0].type = ResTy;
1396 for (unsigned i = 0; i < NumArgs; ++i)
1397 Args[1 + i].type = ArgTys[i];
1398}
1399
1400/***/
1401
Daniel Dunbar04d35782008-09-17 00:51:38 +00001402void CodeGenTypes::GetExpandedTypes(QualType Ty,
1403 std::vector<const llvm::Type*> &ArgTys) {
1404 const RecordType *RT = Ty->getAsStructureType();
1405 assert(RT && "Can only expand structure types.");
1406 const RecordDecl *RD = RT->getDecl();
1407 assert(!RD->hasFlexibleArrayMember() &&
1408 "Cannot expand structure with flexible array.");
1409
Douglas Gregor5d764842009-01-09 17:18:27 +00001410 for (RecordDecl::field_iterator i = RD->field_begin(),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001411 e = RD->field_end(); i != e; ++i) {
1412 const FieldDecl *FD = *i;
1413 assert(!FD->isBitField() &&
1414 "Cannot expand structure with bit-field members.");
1415
1416 QualType FT = FD->getType();
1417 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1418 GetExpandedTypes(FT, ArgTys);
1419 } else {
1420 ArgTys.push_back(ConvertType(FT));
1421 }
1422 }
1423}
1424
1425llvm::Function::arg_iterator
1426CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1427 llvm::Function::arg_iterator AI) {
1428 const RecordType *RT = Ty->getAsStructureType();
1429 assert(RT && "Can only expand structure types.");
1430
1431 RecordDecl *RD = RT->getDecl();
1432 assert(LV.isSimple() &&
1433 "Unexpected non-simple lvalue during struct expansion.");
1434 llvm::Value *Addr = LV.getAddress();
1435 for (RecordDecl::field_iterator i = RD->field_begin(),
1436 e = RD->field_end(); i != e; ++i) {
1437 FieldDecl *FD = *i;
1438 QualType FT = FD->getType();
1439
1440 // FIXME: What are the right qualifiers here?
1441 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1442 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1443 AI = ExpandTypeFromArgs(FT, LV, AI);
1444 } else {
1445 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1446 ++AI;
1447 }
1448 }
1449
1450 return AI;
1451}
1452
1453void
1454CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1455 llvm::SmallVector<llvm::Value*, 16> &Args) {
1456 const RecordType *RT = Ty->getAsStructureType();
1457 assert(RT && "Can only expand structure types.");
1458
1459 RecordDecl *RD = RT->getDecl();
1460 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1461 llvm::Value *Addr = RV.getAggregateAddr();
1462 for (RecordDecl::field_iterator i = RD->field_begin(),
1463 e = RD->field_end(); i != e; ++i) {
1464 FieldDecl *FD = *i;
1465 QualType FT = FD->getType();
1466
1467 // FIXME: What are the right qualifiers here?
1468 LValue LV = EmitLValueForField(Addr, FD, false, 0);
1469 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1470 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1471 } else {
1472 RValue RV = EmitLoadOfLValue(LV, FT);
1473 assert(RV.isScalar() &&
1474 "Unexpected non-scalar rvalue during struct expansion.");
1475 Args.push_back(RV.getScalarVal());
1476 }
1477 }
1478}
1479
Daniel Dunbar84379912009-02-02 19:06:38 +00001480/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1481/// a pointer to an object of type \arg Ty.
1482///
1483/// This safely handles the case when the src type is smaller than the
1484/// destination type; in this situation the values of bits which not
1485/// present in the src are undefined.
1486static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1487 const llvm::Type *Ty,
1488 CodeGenFunction &CGF) {
1489 const llvm::Type *SrcTy =
1490 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1491 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1492 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1493
Daniel Dunbar77071992009-02-03 05:59:18 +00001494 // If load is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001495 if (SrcSize == DstSize) {
1496 llvm::Value *Casted =
1497 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001498 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1499 // FIXME: Use better alignment / avoid requiring aligned load.
1500 Load->setAlignment(1);
1501 return Load;
Daniel Dunbar84379912009-02-02 19:06:38 +00001502 } else {
1503 assert(SrcSize < DstSize && "Coercion is losing source bits!");
1504
1505 // Otherwise do coercion through memory. This is stupid, but
1506 // simple.
1507 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1508 llvm::Value *Casted =
1509 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001510 llvm::StoreInst *Store =
1511 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1512 // FIXME: Use better alignment / avoid requiring aligned store.
1513 Store->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001514 return CGF.Builder.CreateLoad(Tmp);
1515 }
1516}
1517
1518/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1519/// where the source and destination may have different types.
1520///
1521/// This safely handles the case when the src type is larger than the
1522/// destination type; the upper bits of the src will be lost.
1523static void CreateCoercedStore(llvm::Value *Src,
1524 llvm::Value *DstPtr,
1525 CodeGenFunction &CGF) {
1526 const llvm::Type *SrcTy = Src->getType();
1527 const llvm::Type *DstTy =
1528 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1529
1530 uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1531 uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1532
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001533 // If store is legal, just bitcast the src pointer.
Daniel Dunbar84379912009-02-02 19:06:38 +00001534 if (SrcSize == DstSize) {
1535 llvm::Value *Casted =
1536 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001537 // FIXME: Use better alignment / avoid requiring aligned store.
1538 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +00001539 } else {
1540 assert(SrcSize > DstSize && "Coercion is missing bits!");
1541
1542 // Otherwise do coercion through memory. This is stupid, but
1543 // simple.
1544 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1545 CGF.Builder.CreateStore(Src, Tmp);
1546 llvm::Value *Casted =
1547 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +00001548 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1549 // FIXME: Use better alignment / avoid requiring aligned load.
1550 Load->setAlignment(1);
1551 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar84379912009-02-02 19:06:38 +00001552 }
1553}
1554
Daniel Dunbar04d35782008-09-17 00:51:38 +00001555/***/
1556
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001557bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001558 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001559}
1560
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001561const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001562CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001563 std::vector<const llvm::Type*> ArgTys;
1564
1565 const llvm::Type *ResultType = 0;
1566
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001567 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001568 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +00001569 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001570 case ABIArgInfo::Expand:
1571 assert(0 && "Invalid ABI kind for return argument");
1572
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001573 case ABIArgInfo::Direct:
1574 ResultType = ConvertType(RetTy);
1575 break;
1576
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001577 case ABIArgInfo::Indirect: {
1578 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001579 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +00001580 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001581 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1582 break;
1583 }
1584
Daniel Dunbar1358b202009-01-26 21:26:08 +00001585 case ABIArgInfo::Ignore:
1586 ResultType = llvm::Type::VoidTy;
1587 break;
1588
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001589 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +00001590 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001591 break;
1592 }
1593
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001594 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1595 ie = FI.arg_end(); it != ie; ++it) {
1596 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001597
1598 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +00001599 case ABIArgInfo::Ignore:
1600 break;
1601
Daniel Dunbar04d35782008-09-17 00:51:38 +00001602 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001603 ArgTys.push_back(AI.getCoerceToType());
1604 break;
1605
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001606 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001607 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001608 const llvm::Type *LTy = ConvertTypeForMem(it->type);
1609 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001610 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001611 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001612
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001613 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001614 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +00001615 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001616
1617 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001618 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001619 break;
1620 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001621 }
1622
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001623 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +00001624}
1625
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001626void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001627 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +00001628 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001629 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +00001630 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001631
1632 if (TargetDecl) {
1633 if (TargetDecl->getAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001634 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001635 if (TargetDecl->getAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +00001636 FuncAttrs |= llvm::Attribute::NoReturn;
Anders Carlssondd6791c2008-10-05 23:32:53 +00001637 if (TargetDecl->getAttr<PureAttr>())
1638 FuncAttrs |= llvm::Attribute::ReadOnly;
1639 if (TargetDecl->getAttr<ConstAttr>())
1640 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001641 }
1642
Daniel Dunbar0b37ca82009-02-02 23:43:58 +00001643 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001644 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +00001645 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001646 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001647 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001648 if (RetTy->isPromotableIntegerType()) {
1649 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001650 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001651 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +00001652 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +00001653 }
1654 }
1655 break;
1656
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001657 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001658 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001659 llvm::Attribute::StructRet |
1660 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001661 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001662 // sret disables readnone and readonly
1663 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1664 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +00001665 break;
1666
Daniel Dunbar1358b202009-01-26 21:26:08 +00001667 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001668 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +00001669 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001670
Daniel Dunbar22e30052008-09-11 01:48:57 +00001671 case ABIArgInfo::Expand:
1672 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001673 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001674
Devang Patel2bb6eb82008-09-26 22:53:57 +00001675 if (RetAttrs)
1676 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbare92e0ab2009-02-03 05:31:23 +00001677 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1678 ie = FI.arg_end(); it != ie; ++it) {
1679 QualType ParamType = it->type;
1680 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +00001681 unsigned Attributes = 0;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001682
1683 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001684 case ABIArgInfo::Coerce:
1685 break;
1686
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001687 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +00001688 Attributes |= llvm::Attribute::ByVal;
Daniel Dunbarb3f651a2009-02-05 01:31:19 +00001689 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001690 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +00001691 // byval disables readnone and readonly.
1692 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1693 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001694 break;
1695
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001696 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +00001697 if (ParamType->isPromotableIntegerType()) {
1698 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001699 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001700 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +00001701 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001702 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001703 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001704 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001705
Daniel Dunbar1358b202009-01-26 21:26:08 +00001706 case ABIArgInfo::Ignore:
1707 // Skip increment, no matching LLVM parameter.
1708 continue;
1709
Daniel Dunbar04d35782008-09-17 00:51:38 +00001710 case ABIArgInfo::Expand: {
1711 std::vector<const llvm::Type*> Tys;
1712 // FIXME: This is rather inefficient. Do we ever actually need
1713 // to do anything here? The result should be just reconstructed
1714 // on the other side, so extension should be a non-issue.
1715 getTypes().GetExpandedTypes(ParamType, Tys);
1716 Index += Tys.size();
1717 continue;
1718 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001719 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001720
Devang Patela85a9ef2008-09-25 21:02:23 +00001721 if (Attributes)
1722 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001723 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001724 }
Devang Patel2bb6eb82008-09-26 22:53:57 +00001725 if (FuncAttrs)
1726 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +00001727}
1728
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001729void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1730 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001731 const FunctionArgList &Args) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001732 // FIXME: We no longer need the types from FunctionArgList; lift up
1733 // and simplify.
1734
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001735 // Emit allocs for param decls. Give the LLVM Argument nodes names.
1736 llvm::Function::arg_iterator AI = Fn->arg_begin();
1737
1738 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001739 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001740 AI->setName("agg.result");
1741 ++AI;
1742 }
Daniel Dunbar77071992009-02-03 05:59:18 +00001743
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001744 assert(FI.arg_size() == Args.size() &&
1745 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001746 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001747 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001748 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001749 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +00001750 QualType Ty = info_it->type;
1751 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001752
1753 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001754 case ABIArgInfo::Indirect: {
1755 llvm::Value* V = AI;
1756 if (hasAggregateLLVMType(Ty)) {
1757 // Do nothing, aggregates and complex variables are accessed by
1758 // reference.
1759 } else {
1760 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001761 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001762 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1763 // This must be a promotion, for something like
1764 // "void a(x) short x; {..."
1765 V = EmitScalarConversion(V, Ty, Arg->getType());
1766 }
1767 }
1768 EmitParmDecl(*Arg, V);
1769 break;
1770 }
1771
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001772 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +00001773 assert(AI != Fn->arg_end() && "Argument mismatch!");
1774 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +00001775 if (hasAggregateLLVMType(Ty)) {
1776 // Create a temporary alloca to hold the argument; the rest of
1777 // codegen expects to access aggregates & complex values by
1778 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001779 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +00001780 Builder.CreateStore(AI, V);
1781 } else {
1782 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1783 // This must be a promotion, for something like
1784 // "void a(x) short x; {..."
1785 V = EmitScalarConversion(V, Ty, Arg->getType());
1786 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001787 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001788 EmitParmDecl(*Arg, V);
1789 break;
1790 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001791
1792 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +00001793 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +00001794 // we need to create a temporary and reconstruct it from the
1795 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +00001796 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001797 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +00001798 (Name + ".addr").c_str());
1799 // FIXME: What are the right qualifiers here?
1800 llvm::Function::arg_iterator End =
1801 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1802 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001803
Daniel Dunbar04d35782008-09-17 00:51:38 +00001804 // Name the arguments used in expansion and increment AI.
1805 unsigned Index = 0;
1806 for (; AI != End; ++AI, ++Index)
1807 AI->setName(Name + "." + llvm::utostr(Index));
1808 continue;
1809 }
Daniel Dunbar1358b202009-01-26 21:26:08 +00001810
1811 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001812 // Initialize the local variable appropriately.
1813 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001814 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +00001815 } else {
1816 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1817 }
1818
Daniel Dunbar015bc8e2009-02-03 20:00:13 +00001819 // Skip increment, no matching LLVM parameter.
1820 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +00001821
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001822 case ABIArgInfo::Coerce: {
1823 assert(AI != Fn->arg_end() && "Argument mismatch!");
1824 // FIXME: This is very wasteful; EmitParmDecl is just going to
1825 // drop the result in a new alloca anyway, so we could just
1826 // store into that directly if we broke the abstraction down
1827 // more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001828 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001829 CreateCoercedStore(AI, V, *this);
1830 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001831 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001832 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +00001833 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1834 // This must be a promotion, for something like
1835 // "void a(x) short x; {..."
1836 V = EmitScalarConversion(V, Ty, Arg->getType());
1837 }
1838 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001839 EmitParmDecl(*Arg, V);
1840 break;
1841 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00001842 }
Daniel Dunbar04d35782008-09-17 00:51:38 +00001843
1844 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001845 }
1846 assert(AI == Fn->arg_end() && "Argument mismatch!");
1847}
1848
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001849void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001850 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +00001851 llvm::Value *RV = 0;
1852
1853 // Functions with no result always return void.
1854 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001855 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001856 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +00001857
1858 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001859 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +00001860 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +00001861 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1862 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1863 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1864 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1865 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001866 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1867 false);
Daniel Dunbar17d35372008-12-18 04:52:14 +00001868 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001869 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001870
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001871 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00001872 // The internal return value temp always will have
1873 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +00001874 RV = Builder.CreateLoad(ReturnValue);
1875 break;
1876
Daniel Dunbar1358b202009-01-26 21:26:08 +00001877 case ABIArgInfo::Ignore:
1878 break;
1879
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001880 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +00001881 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +00001882 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +00001883
Daniel Dunbar22e30052008-09-11 01:48:57 +00001884 case ABIArgInfo::Expand:
1885 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001886 }
1887 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00001888
1889 if (RV) {
1890 Builder.CreateRet(RV);
1891 } else {
1892 Builder.CreateRetVoid();
1893 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001894}
1895
Daniel Dunbar6ee022b2009-02-02 22:03:45 +00001896RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1897 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001898 const CallArgList &CallArgs,
1899 const Decl *TargetDecl) {
Daniel Dunbar5b7ac652009-02-03 06:02:10 +00001900 // FIXME: We no longer need the types from CallArgs; lift up and
1901 // simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001902 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001903
1904 // Handle struct-return functions by passing a pointer to the
1905 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001906 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +00001907 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +00001908 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001909 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001910 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001911 }
1912
Daniel Dunbar14c884a2009-02-04 21:17:21 +00001913 assert(CallInfo.arg_size() == CallArgs.size() &&
1914 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +00001915 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001916 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +00001917 I != E; ++I, ++info_it) {
1918 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001919 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +00001920
1921 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00001922 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001923 if (RV.isScalar() || RV.isComplex()) {
1924 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001925 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001926 if (RV.isScalar())
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001927 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
Daniel Dunbar6f56e452009-02-05 09:16:39 +00001928 else
1929 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1930 } else {
1931 Args.push_back(RV.getAggregateAddr());
1932 }
1933 break;
1934
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00001935 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +00001936 if (RV.isScalar()) {
1937 Args.push_back(RV.getScalarVal());
1938 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001939 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1940 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1941 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1942 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +00001943 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +00001944 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +00001945 }
1946 break;
1947
Daniel Dunbar1358b202009-01-26 21:26:08 +00001948 case ABIArgInfo::Ignore:
1949 break;
1950
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001951 case ABIArgInfo::Coerce: {
1952 // FIXME: Avoid the conversion through memory if possible.
1953 llvm::Value *SrcPtr;
1954 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +00001955 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001956 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001957 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00001958 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +00001959 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1960 } else
1961 SrcPtr = RV.getAggregateAddr();
1962 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1963 *this));
1964 break;
1965 }
1966
Daniel Dunbar04d35782008-09-17 00:51:38 +00001967 case ABIArgInfo::Expand:
1968 ExpandTypeToArgs(I->second, RV, Args);
1969 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001970 }
1971 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00001972
Daniel Dunbar0a067402009-02-23 17:26:39 +00001973 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +00001974 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +00001975 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +00001976 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1977 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +00001978
Daniel Dunbar90e43452009-03-02 04:32:35 +00001979 llvm::CallSite CS;
1980 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1981 CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001982 } else {
1983 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +00001984 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1985 &Args[0], &Args[0]+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +00001986 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +00001987 }
1988
Daniel Dunbar90e43452009-03-02 04:32:35 +00001989 CS.setAttributes(Attrs);
1990 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1991 CS.setCallingConv(F->getCallingConv());
1992
1993 // If the call doesn't return, finish the basic block and clear the
1994 // insertion point; this allows the rest of IRgen to discard
1995 // unreachable code.
1996 if (CS.doesNotReturn()) {
1997 Builder.CreateUnreachable();
1998 Builder.ClearInsertionPoint();
1999
2000 // FIXME: For now, emit a dummy basic block because expr
2001 // emitters in generally are not ready to handle emitting
2002 // expressions at unreachable points.
2003 EnsureInsertPoint();
2004
2005 // Return a reasonable RValue.
2006 return GetUndefRValue(RetTy);
2007 }
2008
2009 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +00002010 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002011 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +00002012
2013 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +00002014 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +00002015 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +00002016 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +00002017 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +00002018 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +00002019 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +00002020
Daniel Dunbarb1a60c02009-02-03 06:17:37 +00002021 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +00002022 if (RetTy->isAnyComplexType()) {
2023 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2024 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2025 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +00002026 }
2027 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002028 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +00002029 Builder.CreateStore(CI, V);
2030 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002031 }
2032 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +00002033
Daniel Dunbar1358b202009-01-26 21:26:08 +00002034 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +00002035 // If we are ignoring an argument that had a result, make sure to
2036 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +00002037 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +00002038
Daniel Dunbar73d66602008-09-10 07:04:09 +00002039 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +00002040 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +00002041 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +00002042 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +00002043 if (RetTy->isAnyComplexType())
2044 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +00002045 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +00002046 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +00002047 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +00002048 }
Daniel Dunbar22e30052008-09-11 01:48:57 +00002049
Daniel Dunbar22e30052008-09-11 01:48:57 +00002050 case ABIArgInfo::Expand:
2051 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002052 }
Daniel Dunbare126ab12008-09-10 02:41:04 +00002053
2054 assert(0 && "Unhandled ABIArgInfo::Kind");
2055 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +00002056}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +00002057
2058/* VarArg handling */
2059
2060llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2061 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2062}