blob: 3f106128325b2178bdc380b24a7182dd09816beb [file] [log] [blame]
Daniel Dunbar0dbe2272008-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 Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000021#include "clang/AST/DeclObjC.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000022#include "clang/CodeGen/CodeGenOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000024#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000026
27#include "ABIInfo.h"
28
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034// FIXME: Use iterator and sidestep silly type array creation.
35
John McCall04a67a62010-02-05 21:31:56 +000036static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
37 switch (CC) {
38 default: return llvm::CallingConv::C;
39 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
40 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
41 }
42}
43
John McCall0b0ef0a2010-02-24 07:14:12 +000044/// Derives the 'this' type for codegen purposes, i.e. ignoring method
45/// qualification.
46/// FIXME: address space qualification?
47static QualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
48 return Context.getPointerType(Context.getTagDeclType(RD));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049}
50
John McCall0b0ef0a2010-02-24 07:14:12 +000051/// Returns the canonical formal type of the given C++ method.
52static const FunctionProtoType *GetFormalType(const CXXMethodDecl *MD) {
53 return cast<FunctionProtoType>(MD->getType()->getCanonicalTypeInternal());
54}
55
56/// Returns the "extra-canonicalized" return type, which discards
57/// qualifiers on the return type. Codegen doesn't care about them,
58/// and it makes ABI code a little easier to be able to assume that
59/// all parameter and return types are top-level unqualified.
60static QualType GetReturnType(QualType RetTy) {
61 return RetTy->getCanonicalTypeInternal().getUnqualifiedType();
62}
63
64const CGFunctionInfo &
65CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
66 assert(FTNP->isCanonicalUnqualified() && "type must be canonical");
67 return getFunctionInfo(GetReturnType(FTNP->getResultType()),
68 llvm::SmallVector<QualType, 16>(),
69 FTNP->getCallConv(),
70 FTNP->getNoReturnAttr());
71}
72
73/// \param Args - contains any initial parameters besides those
74/// in the formal type
75static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
76 llvm::SmallVectorImpl<QualType> &ArgTys,
77 const FunctionProtoType *FTP) {
78 assert(FTP->isCanonicalUnqualified() && "type must be canonical");
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000080 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 ArgTys.push_back(FTP->getArgType(i));
John McCall0b0ef0a2010-02-24 07:14:12 +000082 return CGT.getFunctionInfo(GetReturnType(FTP->getResultType()),
83 ArgTys,
84 FTP->getCallConv(),
85 FTP->getNoReturnAttr());
86}
87
88const CGFunctionInfo &
89CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
90 llvm::SmallVector<QualType, 16> ArgTys;
91 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000092}
93
John McCall04a67a62010-02-05 21:31:56 +000094static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000095 // Set the appropriate calling convention for the Function.
96 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000097 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000098
99 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +0000100 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000101
John McCall04a67a62010-02-05 21:31:56 +0000102 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000103}
104
Anders Carlsson375c31c2009-10-03 19:43:08 +0000105const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
106 const FunctionProtoType *FTP) {
107 llvm::SmallVector<QualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000108
Anders Carlsson375c31c2009-10-03 19:43:08 +0000109 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000110 ArgTys.push_back(GetThisType(Context, RD));
111
112 return ::getFunctionInfo(*this, ArgTys,
113 cast<FunctionProtoType>(FTP->getCanonicalTypeInternal()));
Anders Carlsson375c31c2009-10-03 19:43:08 +0000114}
115
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000116const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
117 llvm::SmallVector<QualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000118
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000119 // Add the 'this' pointer unless this is a static method.
120 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000121 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000122
John McCall0b0ef0a2010-02-24 07:14:12 +0000123 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000124}
125
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000126const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
127 CXXCtorType Type) {
128 llvm::SmallVector<QualType, 16> ArgTys;
129
130 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000131 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000132
133 // Check if we need to add a VTT parameter (which has type void **).
134 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
135 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000136
137 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000138}
139
140const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
141 CXXDtorType Type) {
142 llvm::SmallVector<QualType, 16> ArgTys;
143
144 // Add the 'this' pointer.
145 ArgTys.push_back(D->getThisType(Context));
146
147 // Check if we need to add a VTT parameter (which has type void **).
148 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
149 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000150
151 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000152}
153
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000154const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000155 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000156 if (MD->isInstance())
157 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
John McCall0b0ef0a2010-02-24 07:14:12 +0000159 const FunctionType *FTy
160 = cast<FunctionType>(FD->getType()->getCanonicalTypeInternal());
161 if (isa<FunctionNoProtoType>(FTy))
162 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
163 return getFunctionInfo(cast<FunctionProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000164}
165
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000166const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
167 llvm::SmallVector<QualType, 16> ArgTys;
168 ArgTys.push_back(MD->getSelfDecl()->getType());
169 ArgTys.push_back(Context.getObjCSelType());
170 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000171 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000172 e = MD->param_end(); i != e; ++i) {
173 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
174 }
175 return getFunctionInfo(GetReturnType(MD->getResultType()),
176 ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000177 getCallingConventionForDecl(MD),
178 /*NoReturn*/ false);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000179}
180
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000181const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
182 // FIXME: Do we need to handle ObjCMethodDecl?
183 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
184
185 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
186 return getFunctionInfo(CD, GD.getCtorType());
187
188 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
189 return getFunctionInfo(DD, GD.getDtorType());
190
191 return getFunctionInfo(FD);
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000195 const CallArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000196 CallingConv CC,
197 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000198 // FIXME: Kill copy.
199 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000200 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000201 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000202 ArgTys.push_back(Context.getCanonicalParamType(i->second));
203 return getFunctionInfo(GetReturnType(ResTy), ArgTys, CC, NoReturn);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000204}
205
Mike Stump1eb44332009-09-09 15:08:12 +0000206const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000207 const FunctionArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000208 CallingConv CC,
209 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000210 // FIXME: Kill copy.
211 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000212 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000213 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000214 ArgTys.push_back(Context.getCanonicalParamType(i->second));
215 return getFunctionInfo(GetReturnType(ResTy), ArgTys, CC, NoReturn);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000216}
217
218const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
John McCall0b0ef0a2010-02-24 07:14:12 +0000219 const llvm::SmallVectorImpl<QualType> &ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000220 CallingConv CallConv,
221 bool NoReturn) {
222 unsigned CC = ClangCallConvToLLVMCallConv(CallConv);
223
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000224 // Lookup or create unique function info.
225 llvm::FoldingSetNodeID ID;
John McCall04a67a62010-02-05 21:31:56 +0000226 CGFunctionInfo::Profile(ID, CC, NoReturn, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000227 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000228
229 void *InsertPos = 0;
230 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
231 if (FI)
232 return *FI;
233
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000234 // Construct the function info.
John McCall04a67a62010-02-05 21:31:56 +0000235 FI = new CGFunctionInfo(CC, NoReturn, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000236 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000237
238 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000239 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000240
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000241 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000242}
243
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000244CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000245 bool _NoReturn,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000246 QualType ResTy,
John McCall0b0ef0a2010-02-24 07:14:12 +0000247 const llvm::SmallVectorImpl<QualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000248 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000249 EffectiveCallingConvention(_CallingConvention),
250 NoReturn(_NoReturn)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000251{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000252 NumArgs = ArgTys.size();
253 Args = new ArgInfo[1 + NumArgs];
254 Args[0].type = ResTy;
255 for (unsigned i = 0; i < NumArgs; ++i)
256 Args[1 + i].type = ArgTys[i];
257}
258
259/***/
260
Mike Stump1eb44332009-09-09 15:08:12 +0000261void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000262 std::vector<const llvm::Type*> &ArgTys) {
263 const RecordType *RT = Ty->getAsStructureType();
264 assert(RT && "Can only expand structure types.");
265 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000266 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000267 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000269 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
270 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000271 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000272 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000273 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Daniel Dunbar56273772008-09-17 00:51:38 +0000275 QualType FT = FD->getType();
276 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
277 GetExpandedTypes(FT, ArgTys);
278 } else {
279 ArgTys.push_back(ConvertType(FT));
280 }
281 }
282}
283
Mike Stump1eb44332009-09-09 15:08:12 +0000284llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000285CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
286 llvm::Function::arg_iterator AI) {
287 const RecordType *RT = Ty->getAsStructureType();
288 assert(RT && "Can only expand structure types.");
289
290 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000291 assert(LV.isSimple() &&
292 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000293 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000294 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
295 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000296 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000297 QualType FT = FD->getType();
298
299 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000300 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000301 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
302 AI = ExpandTypeFromArgs(FT, LV, AI);
303 } else {
304 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
305 ++AI;
306 }
307 }
308
309 return AI;
310}
311
Mike Stump1eb44332009-09-09 15:08:12 +0000312void
313CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000314 llvm::SmallVector<llvm::Value*, 16> &Args) {
315 const RecordType *RT = Ty->getAsStructureType();
316 assert(RT && "Can only expand structure types.");
317
318 RecordDecl *RD = RT->getDecl();
319 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
320 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000321 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
322 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000323 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000324 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Daniel Dunbar56273772008-09-17 00:51:38 +0000326 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000327 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000328 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
329 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
330 } else {
331 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000332 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000333 "Unexpected non-scalar rvalue during struct expansion.");
334 Args.push_back(RV.getScalarVal());
335 }
336 }
337}
338
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000339/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
340/// a pointer to an object of type \arg Ty.
341///
342/// This safely handles the case when the src type is smaller than the
343/// destination type; in this situation the values of bits which not
344/// present in the src are undefined.
345static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
346 const llvm::Type *Ty,
347 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000348 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000349 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000350 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
351 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000352
Daniel Dunbarb225be42009-02-03 05:59:18 +0000353 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000354 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000355 // Generally SrcSize is never greater than DstSize, since this means we are
356 // losing bits. However, this can happen in cases where the structure has
357 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000358 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000359 // FIXME: Assert that we aren't truncating non-padding bits when have access
360 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000361 llvm::Value *Casted =
362 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000363 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
364 // FIXME: Use better alignment / avoid requiring aligned load.
365 Load->setAlignment(1);
366 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000367 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000368 // Otherwise do coercion through memory. This is stupid, but
369 // simple.
370 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000371 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000372 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000373 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000374 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
375 // FIXME: Use better alignment / avoid requiring aligned store.
376 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000377 return CGF.Builder.CreateLoad(Tmp);
378 }
379}
380
381/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
382/// where the source and destination may have different types.
383///
384/// This safely handles the case when the src type is larger than the
385/// destination type; the upper bits of the src will be lost.
386static void CreateCoercedStore(llvm::Value *Src,
387 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000388 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000389 CodeGenFunction &CGF) {
390 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000391 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000392 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
393
Duncan Sands9408c452009-05-09 07:08:47 +0000394 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
395 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000396
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000397 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000398 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000399 llvm::Value *Casted =
400 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000401 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000402 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000403 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000404 // Otherwise do coercion through memory. This is stupid, but
405 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000406
407 // Generally SrcSize is never greater than DstSize, since this means we are
408 // losing bits. However, this can happen in cases where the structure has
409 // additional padding, for example due to a user specified alignment.
410 //
411 // FIXME: Assert that we aren't truncating non-padding bits when have access
412 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000413 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
414 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000415 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000416 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000417 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
418 // FIXME: Use better alignment / avoid requiring aligned load.
419 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000420 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000421 }
422}
423
Daniel Dunbar56273772008-09-17 00:51:38 +0000424/***/
425
Daniel Dunbar88b53962009-02-02 22:03:45 +0000426bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000427 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000428}
429
John McCallc0bf4622010-02-23 00:48:20 +0000430const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
431 const CGFunctionInfo &FI = getFunctionInfo(GD);
432
433 // For definition purposes, don't consider a K&R function variadic.
434 bool Variadic = false;
435 if (const FunctionProtoType *FPT =
436 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
437 Variadic = FPT->isVariadic();
438
439 return GetFunctionType(FI, Variadic);
440}
441
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000442const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000443CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000444 std::vector<const llvm::Type*> ArgTys;
445
446 const llvm::Type *ResultType = 0;
447
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000448 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000449 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000450 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000451 case ABIArgInfo::Expand:
452 assert(0 && "Invalid ABI kind for return argument");
453
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000454 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000455 case ABIArgInfo::Direct:
456 ResultType = ConvertType(RetTy);
457 break;
458
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000459 case ABIArgInfo::Indirect: {
460 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000461 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000462 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000463 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
464 break;
465 }
466
Daniel Dunbar11434922009-01-26 21:26:08 +0000467 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000468 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000469 break;
470
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000471 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000472 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000473 break;
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
476 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000477 ie = FI.arg_end(); it != ie; ++it) {
478 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000480 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000481 case ABIArgInfo::Ignore:
482 break;
483
Daniel Dunbar56273772008-09-17 00:51:38 +0000484 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000485 ArgTys.push_back(AI.getCoerceToType());
486 break;
487
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000488 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000489 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000490 const llvm::Type *LTy = ConvertTypeForMem(it->type);
491 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000492 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000493 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000494
495 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000496 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000497 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000498 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000500 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000501 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000502 break;
503 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000504 }
505
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000506 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000507}
508
Anders Carlssonecf282b2009-11-24 05:08:52 +0000509static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
510 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
511 if (!TT->getDecl()->isDefinition())
512 return true;
513 }
514
515 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
516 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
517 if (!TT->getDecl()->isDefinition())
518 return true;
519 }
520 }
521
522 return false;
523}
524
525const llvm::Type *
526CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
527 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
528
529 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
530 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
531
532 return llvm::OpaqueType::get(getLLVMContext());
533}
534
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000535void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000536 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000537 AttributeListType &PAL,
538 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000539 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000540 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000541
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000542 CallingConv = FI.getEffectiveCallingConvention();
543
John McCall04a67a62010-02-05 21:31:56 +0000544 if (FI.isNoReturn())
545 FuncAttrs |= llvm::Attribute::NoReturn;
546
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000547 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000548 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000549 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000550 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000551 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000552 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000553 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000554 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000555 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000556 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000557 if (TargetDecl->hasAttr<MallocAttr>())
558 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000559 }
560
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000561 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000562 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000563 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000564 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000565 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000566 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000567
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000568 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000569 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000570 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000571 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000572 case ABIArgInfo::Extend:
573 if (RetTy->isSignedIntegerType()) {
574 RetAttrs |= llvm::Attribute::SExt;
575 } else if (RetTy->isUnsignedIntegerType()) {
576 RetAttrs |= llvm::Attribute::ZExt;
577 }
578 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000579 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000580 break;
581
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000582 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000583 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000584 llvm::Attribute::StructRet |
585 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000586 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000587 // sret disables readnone and readonly
588 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
589 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000590 break;
591
Daniel Dunbar11434922009-01-26 21:26:08 +0000592 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000593 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000594 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000595
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000596 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000597 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000598 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000599
Devang Patela2c69122008-09-26 22:53:57 +0000600 if (RetAttrs)
601 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000602
603 // FIXME: we need to honour command line settings also...
604 // FIXME: RegParm should be reduced in case of nested functions and/or global
605 // register variable.
606 signed RegParm = 0;
607 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000608 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000609 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000610 RegParm = RegParmAttr->getNumParams();
611
612 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000613 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000614 ie = FI.arg_end(); it != ie; ++it) {
615 QualType ParamType = it->type;
616 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000617 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000618
Nuno Lopes079b4952009-12-07 18:30:06 +0000619 if (ParamType.isRestrictQualified())
620 Attributes |= llvm::Attribute::NoAlias;
621
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000622 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000623 case ABIArgInfo::Coerce:
624 break;
625
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000626 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000627 if (AI.getIndirectByVal())
628 Attributes |= llvm::Attribute::ByVal;
629
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000630 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000631 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000632 // byval disables readnone and readonly.
633 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
634 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000635 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000636
637 case ABIArgInfo::Extend:
638 if (ParamType->isSignedIntegerType()) {
639 Attributes |= llvm::Attribute::SExt;
640 } else if (ParamType->isUnsignedIntegerType()) {
641 Attributes |= llvm::Attribute::ZExt;
642 }
643 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000644 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000645 if (RegParm > 0 &&
646 (ParamType->isIntegerType() || ParamType->isPointerType())) {
647 RegParm -=
648 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
649 if (RegParm >= 0)
650 Attributes |= llvm::Attribute::InReg;
651 }
652 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000653 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000654
Daniel Dunbar11434922009-01-26 21:26:08 +0000655 case ABIArgInfo::Ignore:
656 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000657 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000658
Daniel Dunbar56273772008-09-17 00:51:38 +0000659 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000660 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000661 // FIXME: This is rather inefficient. Do we ever actually need to do
662 // anything here? The result should be just reconstructed on the other
663 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000664 getTypes().GetExpandedTypes(ParamType, Tys);
665 Index += Tys.size();
666 continue;
667 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Devang Patel761d7f72008-09-25 21:02:23 +0000670 if (Attributes)
671 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000672 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000673 }
Devang Patela2c69122008-09-26 22:53:57 +0000674 if (FuncAttrs)
675 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000676}
677
Daniel Dunbar88b53962009-02-02 22:03:45 +0000678void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
679 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000680 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000681 // If this is an implicit-return-zero function, go ahead and
682 // initialize the return value. TODO: it might be nice to have
683 // a more general mechanism for this that didn't require synthesized
684 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000685 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000686 if (FD->hasImplicitReturnZero()) {
687 QualType RetTy = FD->getResultType().getUnqualifiedType();
688 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000689 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000690 Builder.CreateStore(Zero, ReturnValue);
691 }
692 }
693
Mike Stumpf5408fe2009-05-16 07:57:57 +0000694 // FIXME: We no longer need the types from FunctionArgList; lift up and
695 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000696
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000697 // Emit allocs for param decls. Give the LLVM Argument nodes names.
698 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000700 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000701 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000702 AI->setName("agg.result");
703 ++AI;
704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000706 assert(FI.arg_size() == Args.size() &&
707 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000708 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000709 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000710 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000711 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000712 QualType Ty = info_it->type;
713 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000714
715 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000716 case ABIArgInfo::Indirect: {
717 llvm::Value* V = AI;
718 if (hasAggregateLLVMType(Ty)) {
719 // Do nothing, aggregates and complex variables are accessed by
720 // reference.
721 } else {
722 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000723 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000724 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
725 // This must be a promotion, for something like
726 // "void a(x) short x; {..."
727 V = EmitScalarConversion(V, Ty, Arg->getType());
728 }
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000731 break;
732 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000733
734 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000735 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000736 assert(AI != Fn->arg_end() && "Argument mismatch!");
737 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000738 if (hasAggregateLLVMType(Ty)) {
739 // Create a temporary alloca to hold the argument; the rest of
740 // codegen expects to access aggregates & complex values by
741 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000742 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000743 Builder.CreateStore(AI, V);
744 } else {
745 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
746 // This must be a promotion, for something like
747 // "void a(x) short x; {..."
748 V = EmitScalarConversion(V, Ty, Arg->getType());
749 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000750 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000751 EmitParmDecl(*Arg, V);
752 break;
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Daniel Dunbar56273772008-09-17 00:51:38 +0000755 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000756 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000757 // we need to create a temporary and reconstruct it from the
758 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000759 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000760 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000761 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000762 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000763 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000764
Daniel Dunbar56273772008-09-17 00:51:38 +0000765 // Name the arguments used in expansion and increment AI.
766 unsigned Index = 0;
767 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000768 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000769 continue;
770 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000771
772 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000773 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000774 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000775 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000776 } else {
777 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000780 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000781 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000782
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000783 case ABIArgInfo::Coerce: {
784 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000785 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
786 // result in a new alloca anyway, so we could just store into that
787 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000788 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000789 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000790 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000791 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000792 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000793 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
794 // This must be a promotion, for something like
795 // "void a(x) short x; {..."
796 V = EmitScalarConversion(V, Ty, Arg->getType());
797 }
798 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000799 EmitParmDecl(*Arg, V);
800 break;
801 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000802 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000803
804 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000805 }
806 assert(AI == Fn->arg_end() && "Argument mismatch!");
807}
808
Daniel Dunbar88b53962009-02-02 22:03:45 +0000809void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000810 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000811 llvm::Value *RV = 0;
812
813 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000814 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000815 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000816 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000817
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000818 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000819 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000820 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000821 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
822 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
823 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000824 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000825 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000826 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000827 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000828 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000829 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000830
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000831 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000832 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000833 // The internal return value temp always will have
834 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000835 RV = Builder.CreateLoad(ReturnValue);
836 break;
837
Daniel Dunbar11434922009-01-26 21:26:08 +0000838 case ABIArgInfo::Ignore:
839 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000841 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000842 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000843 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000844
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000845 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000846 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000847 }
848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000850 if (RV) {
851 Builder.CreateRet(RV);
852 } else {
853 Builder.CreateRetVoid();
854 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000855}
856
Anders Carlsson0139bb92009-04-08 20:47:54 +0000857RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000858 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000859 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Anders Carlsson0139bb92009-04-08 20:47:54 +0000861 return EmitAnyExprToTemp(E);
862}
863
Daniel Dunbar88b53962009-02-02 22:03:45 +0000864RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000865 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000866 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000867 const CallArgList &CallArgs,
868 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000869 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000870 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000871
872 // Handle struct-return functions by passing a pointer to the
873 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000874 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000875 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000876
877
Chris Lattner5db7ae52009-06-13 00:26:38 +0000878 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000879 // alloca to hold the result, unless one is given to us.
880 if (CGM.ReturnTypeUsesSret(CallInfo)) {
881 llvm::Value *Value = ReturnValue.getValue();
882 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000883 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +0000884 Args.push_back(Value);
885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000887 assert(CallInfo.arg_size() == CallArgs.size() &&
888 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000889 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000890 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000891 I != E; ++I, ++info_it) {
892 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000893 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000894
895 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000896 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000897 if (RV.isScalar() || RV.isComplex()) {
898 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000899 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000900 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000901 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000902 else
Mike Stump1eb44332009-09-09 15:08:12 +0000903 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000904 } else {
905 Args.push_back(RV.getAggregateAddr());
906 }
907 break;
908
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000909 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000910 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000911 if (RV.isScalar()) {
912 Args.push_back(RV.getScalarVal());
913 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000914 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
915 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
916 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
917 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000918 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000919 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000920 }
921 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Daniel Dunbar11434922009-01-26 21:26:08 +0000923 case ABIArgInfo::Ignore:
924 break;
925
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000926 case ABIArgInfo::Coerce: {
927 // FIXME: Avoid the conversion through memory if possible.
928 llvm::Value *SrcPtr;
929 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000930 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000931 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000932 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000933 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000934 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000935 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000936 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000937 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000938 *this));
939 break;
940 }
941
Daniel Dunbar56273772008-09-17 00:51:38 +0000942 case ABIArgInfo::Expand:
943 ExpandTypeToArgs(I->second, RV, Args);
944 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000945 }
946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner5db7ae52009-06-13 00:26:38 +0000948 // If the callee is a bitcast of a function to a varargs pointer to function
949 // type, check to see if we can remove the bitcast. This handles some cases
950 // with unprototyped functions.
951 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
952 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
953 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
954 const llvm::FunctionType *CurFT =
955 cast<llvm::FunctionType>(CurPT->getElementType());
956 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattner5db7ae52009-06-13 00:26:38 +0000958 if (CE->getOpcode() == llvm::Instruction::BitCast &&
959 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000960 ActualFT->getNumParams() == CurFT->getNumParams() &&
961 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000962 bool ArgsMatch = true;
963 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
964 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
965 ArgsMatch = false;
966 break;
967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattner5db7ae52009-06-13 00:26:38 +0000969 // Strip the cast if we can get away with it. This is a nice cleanup,
970 // but also allows us to inline the function at -O0 if it is marked
971 // always_inline.
972 if (ArgsMatch)
973 Callee = CalleeF;
974 }
975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000977
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000978 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000979 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000980 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000981 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000982 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
983 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Daniel Dunbard14151d2009-03-02 04:32:35 +0000985 llvm::CallSite CS;
986 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000987 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000988 } else {
989 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000990 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000991 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000992 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000993 }
994
Daniel Dunbard14151d2009-03-02 04:32:35 +0000995 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000996 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +0000997
998 // If the call doesn't return, finish the basic block and clear the
999 // insertion point; this allows the rest of IRgen to discard
1000 // unreachable code.
1001 if (CS.doesNotReturn()) {
1002 Builder.CreateUnreachable();
1003 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Mike Stumpf5408fe2009-05-16 07:57:57 +00001005 // FIXME: For now, emit a dummy basic block because expr emitters in
1006 // generally are not ready to handle emitting expressions at unreachable
1007 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001008 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Daniel Dunbard14151d2009-03-02 04:32:35 +00001010 // Return a reasonable RValue.
1011 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001012 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001013
1014 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001015 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001016 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001017
1018 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001019 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001020 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001021 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001022 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001023 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001024 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001025
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001026 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001027 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001028 if (RetTy->isAnyComplexType()) {
1029 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1030 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1031 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001032 }
1033 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001034 llvm::Value *DestPtr = ReturnValue.getValue();
1035 bool DestIsVolatile = ReturnValue.isVolatile();
1036
1037 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001038 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001039 DestIsVolatile = false;
1040 }
1041 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1042 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001043 }
1044 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001045
Daniel Dunbar11434922009-01-26 21:26:08 +00001046 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001047 // If we are ignoring an argument that had a result, make sure to
1048 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001049 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001050
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001051 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001052 llvm::Value *DestPtr = ReturnValue.getValue();
1053 bool DestIsVolatile = ReturnValue.isVolatile();
1054
1055 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001056 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001057 DestIsVolatile = false;
1058 }
1059
1060 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001061 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001062 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001063 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001064 return RValue::getAggregate(DestPtr);
1065 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001066 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001067
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001068 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001069 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001070 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001071
1072 assert(0 && "Unhandled ABIArgInfo::Kind");
1073 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001074}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001075
1076/* VarArg handling */
1077
1078llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1079 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1080}