blob: 7a47d5bcc05db9b07104a793b67774b124347c03 [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
Mike Stump1eb44332009-09-09 15:08:12 +000044const
Douglas Gregor72564e72009-02-26 23:50:07 +000045CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Mike Stump1eb44332009-09-09 15:08:12 +000046 return getFunctionInfo(FTNP->getResultType(),
John McCall04a67a62010-02-05 21:31:56 +000047 llvm::SmallVector<QualType, 16>(),
48 FTNP->getCallConv(), FTNP->getNoReturnAttr());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049}
50
Mike Stump1eb44332009-09-09 15:08:12 +000051const
Douglas Gregor72564e72009-02-26 23:50:07 +000052CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000053 llvm::SmallVector<QualType, 16> ArgTys;
54 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000055 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000056 ArgTys.push_back(FTP->getArgType(i));
John McCall04a67a62010-02-05 21:31:56 +000057 return getFunctionInfo(FTP->getResultType(), ArgTys,
58 FTP->getCallConv(), FTP->getNoReturnAttr());
Daniel Dunbarbac7c252009-09-11 22:24:53 +000059}
60
John McCall04a67a62010-02-05 21:31:56 +000061static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000062 // Set the appropriate calling convention for the Function.
63 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000064 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000065
66 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000067 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000068
John McCall04a67a62010-02-05 21:31:56 +000069 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000070}
71
Anders Carlsson375c31c2009-10-03 19:43:08 +000072const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
73 const FunctionProtoType *FTP) {
74 llvm::SmallVector<QualType, 16> ArgTys;
75
76 // Add the 'this' pointer.
77 ArgTys.push_back(Context.getPointerType(Context.getTagDeclType(RD)));
78
79 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
80 ArgTys.push_back(FTP->getArgType(i));
81
82 // FIXME: Set calling convention correctly, it needs to be associated with the
83 // type somehow.
John McCall04a67a62010-02-05 21:31:56 +000084 return getFunctionInfo(FTP->getResultType(), ArgTys,
85 FTP->getCallConv(), FTP->getNoReturnAttr());
Anders Carlsson375c31c2009-10-03 19:43:08 +000086}
87
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000088const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
89 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattner3eb67ca2009-05-12 20:27:19 +000090 // Add the 'this' pointer unless this is a static method.
91 if (MD->isInstance())
92 ArgTys.push_back(MD->getThisType(Context));
Mike Stump1eb44332009-09-09 15:08:12 +000093
John McCall183700f2009-09-21 23:43:11 +000094 const FunctionProtoType *FTP = MD->getType()->getAs<FunctionProtoType>();
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000095 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
96 ArgTys.push_back(FTP->getArgType(i));
John McCall04a67a62010-02-05 21:31:56 +000097 return getFunctionInfo(FTP->getResultType(), ArgTys, FTP->getCallConv(),
98 FTP->getNoReturnAttr());
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000099}
100
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000101const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
102 CXXCtorType Type) {
103 llvm::SmallVector<QualType, 16> ArgTys;
104
105 // Add the 'this' pointer.
106 ArgTys.push_back(D->getThisType(Context));
107
108 // Check if we need to add a VTT parameter (which has type void **).
109 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
110 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
111
112 const FunctionProtoType *FTP = D->getType()->getAs<FunctionProtoType>();
113 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
114 ArgTys.push_back(FTP->getArgType(i));
John McCall04a67a62010-02-05 21:31:56 +0000115 return getFunctionInfo(FTP->getResultType(), ArgTys, FTP->getCallConv(),
116 FTP->getNoReturnAttr());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000117}
118
119const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
120 CXXDtorType Type) {
121 llvm::SmallVector<QualType, 16> ArgTys;
122
123 // Add the 'this' pointer.
124 ArgTys.push_back(D->getThisType(Context));
125
126 // Check if we need to add a VTT parameter (which has type void **).
127 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
128 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
129
130 const FunctionProtoType *FTP = D->getType()->getAs<FunctionProtoType>();
131 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
132 ArgTys.push_back(FTP->getArgType(i));
John McCall04a67a62010-02-05 21:31:56 +0000133 return getFunctionInfo(FTP->getResultType(), ArgTys, FTP->getCallConv(),
134 FTP->getNoReturnAttr());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000135}
136
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000137const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000138 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000139 if (MD->isInstance())
140 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
John McCall183700f2009-09-21 23:43:11 +0000142 const FunctionType *FTy = FD->getType()->getAs<FunctionType>();
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000143 if (const FunctionNoProtoType *FNTP = dyn_cast<FunctionNoProtoType>(FTy))
144 return getFunctionInfo(FNTP->getResultType(),
145 llvm::SmallVector<QualType, 16>(),
John McCall04a67a62010-02-05 21:31:56 +0000146 FNTP->getCallConv(), FNTP->getNoReturnAttr());
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000147
148 const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy);
149 llvm::SmallVector<QualType, 16> ArgTys;
150 // FIXME: Kill copy.
151 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
152 ArgTys.push_back(FPT->getArgType(i));
John McCall04a67a62010-02-05 21:31:56 +0000153 return getFunctionInfo(FPT->getResultType(), ArgTys,
154 FPT->getCallConv(), FPT->getNoReturnAttr());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000155}
156
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000157const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
158 llvm::SmallVector<QualType, 16> ArgTys;
159 ArgTys.push_back(MD->getSelfDecl()->getType());
160 ArgTys.push_back(Context.getObjCSelType());
161 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000162 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000163 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000164 ArgTys.push_back((*i)->getType());
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000165 return getFunctionInfo(MD->getResultType(), ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000166 getCallingConventionForDecl(MD),
167 /*NoReturn*/ false);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000168}
169
Mike Stump1eb44332009-09-09 15:08:12 +0000170const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000171 const CallArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000172 CallingConv CC,
173 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000174 // FIXME: Kill copy.
175 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000176 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000177 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000178 ArgTys.push_back(i->second);
John McCall04a67a62010-02-05 21:31:56 +0000179 return getFunctionInfo(ResTy, ArgTys, CC, NoReturn);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000180}
181
Mike Stump1eb44332009-09-09 15:08:12 +0000182const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000183 const FunctionArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000184 CallingConv CC,
185 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000186 // FIXME: Kill copy.
187 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000188 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000189 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000190 ArgTys.push_back(i->second);
John McCall04a67a62010-02-05 21:31:56 +0000191 return getFunctionInfo(ResTy, ArgTys, CC, NoReturn);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000192}
193
194const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000195 const llvm::SmallVector<QualType, 16> &ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000196 CallingConv CallConv,
197 bool NoReturn) {
198 unsigned CC = ClangCallConvToLLVMCallConv(CallConv);
199
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000200 // Lookup or create unique function info.
201 llvm::FoldingSetNodeID ID;
John McCall04a67a62010-02-05 21:31:56 +0000202 CGFunctionInfo::Profile(ID, CC, NoReturn, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000203 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000204
205 void *InsertPos = 0;
206 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
207 if (FI)
208 return *FI;
209
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000210 // Construct the function info.
John McCall04a67a62010-02-05 21:31:56 +0000211 FI = new CGFunctionInfo(CC, NoReturn, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000212 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000213
214 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000215 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000216
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000217 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000218}
219
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000220CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000221 bool _NoReturn,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000222 QualType ResTy,
223 const llvm::SmallVector<QualType, 16> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000224 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000225 EffectiveCallingConvention(_CallingConvention),
226 NoReturn(_NoReturn)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000227{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000228 NumArgs = ArgTys.size();
229 Args = new ArgInfo[1 + NumArgs];
230 Args[0].type = ResTy;
231 for (unsigned i = 0; i < NumArgs; ++i)
232 Args[1 + i].type = ArgTys[i];
233}
234
235/***/
236
Mike Stump1eb44332009-09-09 15:08:12 +0000237void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000238 std::vector<const llvm::Type*> &ArgTys) {
239 const RecordType *RT = Ty->getAsStructureType();
240 assert(RT && "Can only expand structure types.");
241 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000242 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000243 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000245 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
246 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000247 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000248 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000249 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Daniel Dunbar56273772008-09-17 00:51:38 +0000251 QualType FT = FD->getType();
252 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
253 GetExpandedTypes(FT, ArgTys);
254 } else {
255 ArgTys.push_back(ConvertType(FT));
256 }
257 }
258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000261CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
262 llvm::Function::arg_iterator AI) {
263 const RecordType *RT = Ty->getAsStructureType();
264 assert(RT && "Can only expand structure types.");
265
266 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000267 assert(LV.isSimple() &&
268 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000269 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000270 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
271 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000272 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000273 QualType FT = FD->getType();
274
275 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000276 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000277 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
278 AI = ExpandTypeFromArgs(FT, LV, AI);
279 } else {
280 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
281 ++AI;
282 }
283 }
284
285 return AI;
286}
287
Mike Stump1eb44332009-09-09 15:08:12 +0000288void
289CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000290 llvm::SmallVector<llvm::Value*, 16> &Args) {
291 const RecordType *RT = Ty->getAsStructureType();
292 assert(RT && "Can only expand structure types.");
293
294 RecordDecl *RD = RT->getDecl();
295 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
296 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000297 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
298 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000299 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000300 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Daniel Dunbar56273772008-09-17 00:51:38 +0000302 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000303 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000304 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
305 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
306 } else {
307 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000308 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000309 "Unexpected non-scalar rvalue during struct expansion.");
310 Args.push_back(RV.getScalarVal());
311 }
312 }
313}
314
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000315/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
316/// a pointer to an object of type \arg Ty.
317///
318/// This safely handles the case when the src type is smaller than the
319/// destination type; in this situation the values of bits which not
320/// present in the src are undefined.
321static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
322 const llvm::Type *Ty,
323 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000324 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000325 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000326 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
327 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000328
Daniel Dunbarb225be42009-02-03 05:59:18 +0000329 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000330 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000331 // Generally SrcSize is never greater than DstSize, since this means we are
332 // losing bits. However, this can happen in cases where the structure has
333 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000334 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000335 // FIXME: Assert that we aren't truncating non-padding bits when have access
336 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000337 llvm::Value *Casted =
338 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000339 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
340 // FIXME: Use better alignment / avoid requiring aligned load.
341 Load->setAlignment(1);
342 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000343 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000344 // Otherwise do coercion through memory. This is stupid, but
345 // simple.
346 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000347 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000348 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000349 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000350 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
351 // FIXME: Use better alignment / avoid requiring aligned store.
352 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000353 return CGF.Builder.CreateLoad(Tmp);
354 }
355}
356
357/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
358/// where the source and destination may have different types.
359///
360/// This safely handles the case when the src type is larger than the
361/// destination type; the upper bits of the src will be lost.
362static void CreateCoercedStore(llvm::Value *Src,
363 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000364 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000365 CodeGenFunction &CGF) {
366 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000367 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000368 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
369
Duncan Sands9408c452009-05-09 07:08:47 +0000370 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
371 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000372
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000373 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000374 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000375 llvm::Value *Casted =
376 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000377 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000378 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000379 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000380 // Otherwise do coercion through memory. This is stupid, but
381 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000382
383 // Generally SrcSize is never greater than DstSize, since this means we are
384 // losing bits. However, this can happen in cases where the structure has
385 // additional padding, for example due to a user specified alignment.
386 //
387 // FIXME: Assert that we aren't truncating non-padding bits when have access
388 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000389 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
390 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000391 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000392 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000393 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
394 // FIXME: Use better alignment / avoid requiring aligned load.
395 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000396 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000397 }
398}
399
Daniel Dunbar56273772008-09-17 00:51:38 +0000400/***/
401
Daniel Dunbar88b53962009-02-02 22:03:45 +0000402bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000403 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000404}
405
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000406const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000407CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000408 std::vector<const llvm::Type*> ArgTys;
409
410 const llvm::Type *ResultType = 0;
411
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000412 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000413 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000414 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000415 case ABIArgInfo::Expand:
416 assert(0 && "Invalid ABI kind for return argument");
417
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000418 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000419 case ABIArgInfo::Direct:
420 ResultType = ConvertType(RetTy);
421 break;
422
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000423 case ABIArgInfo::Indirect: {
424 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000425 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000426 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000427 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
428 break;
429 }
430
Daniel Dunbar11434922009-01-26 21:26:08 +0000431 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000432 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000433 break;
434
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000435 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000436 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000437 break;
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
440 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000441 ie = FI.arg_end(); it != ie; ++it) {
442 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000444 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000445 case ABIArgInfo::Ignore:
446 break;
447
Daniel Dunbar56273772008-09-17 00:51:38 +0000448 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000449 ArgTys.push_back(AI.getCoerceToType());
450 break;
451
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000452 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000453 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000454 const llvm::Type *LTy = ConvertTypeForMem(it->type);
455 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000456 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000457 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000458
459 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000460 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000461 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000462 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000464 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000465 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000466 break;
467 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000468 }
469
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000470 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000471}
472
Anders Carlssonecf282b2009-11-24 05:08:52 +0000473static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
474 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
475 if (!TT->getDecl()->isDefinition())
476 return true;
477 }
478
479 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
480 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
481 if (!TT->getDecl()->isDefinition())
482 return true;
483 }
484 }
485
486 return false;
487}
488
489const llvm::Type *
490CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
491 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
492
493 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
494 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
495
496 return llvm::OpaqueType::get(getLLVMContext());
497}
498
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000499void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000500 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000501 AttributeListType &PAL,
502 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000503 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000504 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000505
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000506 CallingConv = FI.getEffectiveCallingConvention();
507
John McCall04a67a62010-02-05 21:31:56 +0000508 if (FI.isNoReturn())
509 FuncAttrs |= llvm::Attribute::NoReturn;
510
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000511 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000512 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000513 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000514 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000515 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000516 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000517 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000518 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000519 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000520 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000521 if (TargetDecl->hasAttr<MallocAttr>())
522 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000523 }
524
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000525 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000526 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000527 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000528 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000529 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000530 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000531
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000532 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000533 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000534 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000535 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000536 case ABIArgInfo::Extend:
537 if (RetTy->isSignedIntegerType()) {
538 RetAttrs |= llvm::Attribute::SExt;
539 } else if (RetTy->isUnsignedIntegerType()) {
540 RetAttrs |= llvm::Attribute::ZExt;
541 }
542 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000543 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000544 break;
545
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000546 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000547 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000548 llvm::Attribute::StructRet |
549 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000550 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000551 // sret disables readnone and readonly
552 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
553 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000554 break;
555
Daniel Dunbar11434922009-01-26 21:26:08 +0000556 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000557 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000558 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000559
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000560 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000561 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000562 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000563
Devang Patela2c69122008-09-26 22:53:57 +0000564 if (RetAttrs)
565 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000566
567 // FIXME: we need to honour command line settings also...
568 // FIXME: RegParm should be reduced in case of nested functions and/or global
569 // register variable.
570 signed RegParm = 0;
571 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000572 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000573 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000574 RegParm = RegParmAttr->getNumParams();
575
576 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000577 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000578 ie = FI.arg_end(); it != ie; ++it) {
579 QualType ParamType = it->type;
580 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000581 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000582
Nuno Lopes079b4952009-12-07 18:30:06 +0000583 if (ParamType.isRestrictQualified())
584 Attributes |= llvm::Attribute::NoAlias;
585
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000586 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000587 case ABIArgInfo::Coerce:
588 break;
589
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000590 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000591 if (AI.getIndirectByVal())
592 Attributes |= llvm::Attribute::ByVal;
593
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000594 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000595 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000596 // byval disables readnone and readonly.
597 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
598 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000599 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000600
601 case ABIArgInfo::Extend:
602 if (ParamType->isSignedIntegerType()) {
603 Attributes |= llvm::Attribute::SExt;
604 } else if (ParamType->isUnsignedIntegerType()) {
605 Attributes |= llvm::Attribute::ZExt;
606 }
607 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000608 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000609 if (RegParm > 0 &&
610 (ParamType->isIntegerType() || ParamType->isPointerType())) {
611 RegParm -=
612 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
613 if (RegParm >= 0)
614 Attributes |= llvm::Attribute::InReg;
615 }
616 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000617 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000618
Daniel Dunbar11434922009-01-26 21:26:08 +0000619 case ABIArgInfo::Ignore:
620 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000621 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000622
Daniel Dunbar56273772008-09-17 00:51:38 +0000623 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000624 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000625 // FIXME: This is rather inefficient. Do we ever actually need to do
626 // anything here? The result should be just reconstructed on the other
627 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000628 getTypes().GetExpandedTypes(ParamType, Tys);
629 Index += Tys.size();
630 continue;
631 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Devang Patel761d7f72008-09-25 21:02:23 +0000634 if (Attributes)
635 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000636 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000637 }
Devang Patela2c69122008-09-26 22:53:57 +0000638 if (FuncAttrs)
639 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000640}
641
Daniel Dunbar88b53962009-02-02 22:03:45 +0000642void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
643 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000644 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000645 // If this is an implicit-return-zero function, go ahead and
646 // initialize the return value. TODO: it might be nice to have
647 // a more general mechanism for this that didn't require synthesized
648 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000649 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000650 if (FD->hasImplicitReturnZero()) {
651 QualType RetTy = FD->getResultType().getUnqualifiedType();
652 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000653 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000654 Builder.CreateStore(Zero, ReturnValue);
655 }
656 }
657
Mike Stumpf5408fe2009-05-16 07:57:57 +0000658 // FIXME: We no longer need the types from FunctionArgList; lift up and
659 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000660
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000661 // Emit allocs for param decls. Give the LLVM Argument nodes names.
662 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000664 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000665 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000666 AI->setName("agg.result");
667 ++AI;
668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000670 assert(FI.arg_size() == Args.size() &&
671 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000672 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000673 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000674 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000675 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000676 QualType Ty = info_it->type;
677 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000678
679 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000680 case ABIArgInfo::Indirect: {
681 llvm::Value* V = AI;
682 if (hasAggregateLLVMType(Ty)) {
683 // Do nothing, aggregates and complex variables are accessed by
684 // reference.
685 } else {
686 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000687 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000688 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
689 // This must be a promotion, for something like
690 // "void a(x) short x; {..."
691 V = EmitScalarConversion(V, Ty, Arg->getType());
692 }
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000695 break;
696 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000697
698 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000699 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000700 assert(AI != Fn->arg_end() && "Argument mismatch!");
701 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000702 if (hasAggregateLLVMType(Ty)) {
703 // Create a temporary alloca to hold the argument; the rest of
704 // codegen expects to access aggregates & complex values by
705 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000706 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000707 Builder.CreateStore(AI, V);
708 } else {
709 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
710 // This must be a promotion, for something like
711 // "void a(x) short x; {..."
712 V = EmitScalarConversion(V, Ty, Arg->getType());
713 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000714 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000715 EmitParmDecl(*Arg, V);
716 break;
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Daniel Dunbar56273772008-09-17 00:51:38 +0000719 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000720 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000721 // we need to create a temporary and reconstruct it from the
722 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000723 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000724 Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000725 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000726 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000727 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000728 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000729
Daniel Dunbar56273772008-09-17 00:51:38 +0000730 // Name the arguments used in expansion and increment AI.
731 unsigned Index = 0;
732 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000733 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000734 continue;
735 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000736
737 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000738 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000739 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000740 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000741 } else {
742 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000745 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000746 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000747
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000748 case ABIArgInfo::Coerce: {
749 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000750 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
751 // result in a new alloca anyway, so we could just store into that
752 // directly if we broke the abstraction down more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000753 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000754 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000755 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000756 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000757 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000758 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
759 // This must be a promotion, for something like
760 // "void a(x) short x; {..."
761 V = EmitScalarConversion(V, Ty, Arg->getType());
762 }
763 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000764 EmitParmDecl(*Arg, V);
765 break;
766 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000767 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000768
769 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000770 }
771 assert(AI == Fn->arg_end() && "Argument mismatch!");
772}
773
Daniel Dunbar88b53962009-02-02 22:03:45 +0000774void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000775 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000776 llvm::Value *RV = 0;
777
778 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000779 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000780 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000781 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000782
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000783 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000784 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000785 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000786 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
787 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
788 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000789 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000790 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000791 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000792 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000793 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000794 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000795
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000796 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000797 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000798 // The internal return value temp always will have
799 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000800 RV = Builder.CreateLoad(ReturnValue);
801 break;
802
Daniel Dunbar11434922009-01-26 21:26:08 +0000803 case ABIArgInfo::Ignore:
804 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000806 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000807 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000808 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000809
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000810 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000811 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000812 }
813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000815 if (RV) {
816 Builder.CreateRet(RV);
817 } else {
818 Builder.CreateRetVoid();
819 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000820}
821
Anders Carlsson0139bb92009-04-08 20:47:54 +0000822RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000823 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000824 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Anders Carlsson0139bb92009-04-08 20:47:54 +0000826 return EmitAnyExprToTemp(E);
827}
828
Daniel Dunbar88b53962009-02-02 22:03:45 +0000829RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000830 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000831 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000832 const CallArgList &CallArgs,
833 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000834 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000835 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000836
837 // Handle struct-return functions by passing a pointer to the
838 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000839 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000840 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000841
842
Chris Lattner5db7ae52009-06-13 00:26:38 +0000843 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000844 // alloca to hold the result, unless one is given to us.
845 if (CGM.ReturnTypeUsesSret(CallInfo)) {
846 llvm::Value *Value = ReturnValue.getValue();
847 if (!Value)
848 Value = CreateTempAlloca(ConvertTypeForMem(RetTy));
849 Args.push_back(Value);
850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000852 assert(CallInfo.arg_size() == CallArgs.size() &&
853 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000854 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000855 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000856 I != E; ++I, ++info_it) {
857 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000858 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000859
860 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000861 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000862 if (RV.isScalar() || RV.isComplex()) {
863 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000864 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000865 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000866 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000867 else
Mike Stump1eb44332009-09-09 15:08:12 +0000868 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000869 } else {
870 Args.push_back(RV.getAggregateAddr());
871 }
872 break;
873
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000874 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000875 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000876 if (RV.isScalar()) {
877 Args.push_back(RV.getScalarVal());
878 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000879 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
880 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
881 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
882 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000883 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000884 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000885 }
886 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Daniel Dunbar11434922009-01-26 21:26:08 +0000888 case ABIArgInfo::Ignore:
889 break;
890
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000891 case ABIArgInfo::Coerce: {
892 // FIXME: Avoid the conversion through memory if possible.
893 llvm::Value *SrcPtr;
894 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +0000895 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000896 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000897 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000898 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000899 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000900 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000901 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000902 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000903 *this));
904 break;
905 }
906
Daniel Dunbar56273772008-09-17 00:51:38 +0000907 case ABIArgInfo::Expand:
908 ExpandTypeToArgs(I->second, RV, Args);
909 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000910 }
911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattner5db7ae52009-06-13 00:26:38 +0000913 // If the callee is a bitcast of a function to a varargs pointer to function
914 // type, check to see if we can remove the bitcast. This handles some cases
915 // with unprototyped functions.
916 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
917 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
918 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
919 const llvm::FunctionType *CurFT =
920 cast<llvm::FunctionType>(CurPT->getElementType());
921 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Chris Lattner5db7ae52009-06-13 00:26:38 +0000923 if (CE->getOpcode() == llvm::Instruction::BitCast &&
924 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000925 ActualFT->getNumParams() == CurFT->getNumParams() &&
926 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000927 bool ArgsMatch = true;
928 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
929 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
930 ArgsMatch = false;
931 break;
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner5db7ae52009-06-13 00:26:38 +0000934 // Strip the cast if we can get away with it. This is a nice cleanup,
935 // but also allows us to inline the function at -O0 if it is marked
936 // always_inline.
937 if (ArgsMatch)
938 Callee = CalleeF;
939 }
940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000942
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000943 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000944 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000945 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000946 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000947 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
948 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Daniel Dunbard14151d2009-03-02 04:32:35 +0000950 llvm::CallSite CS;
951 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000952 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000953 } else {
954 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000955 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000956 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000957 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000958 }
959
Daniel Dunbard14151d2009-03-02 04:32:35 +0000960 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000961 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +0000962
963 // If the call doesn't return, finish the basic block and clear the
964 // insertion point; this allows the rest of IRgen to discard
965 // unreachable code.
966 if (CS.doesNotReturn()) {
967 Builder.CreateUnreachable();
968 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Mike Stumpf5408fe2009-05-16 07:57:57 +0000970 // FIXME: For now, emit a dummy basic block because expr emitters in
971 // generally are not ready to handle emitting expressions at unreachable
972 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +0000973 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Daniel Dunbard14151d2009-03-02 04:32:35 +0000975 // Return a reasonable RValue.
976 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000977 }
Daniel Dunbard14151d2009-03-02 04:32:35 +0000978
979 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +0000980 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000981 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000982
983 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000984 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000985 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000986 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +0000987 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +0000988 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +0000989 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000990
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000991 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000992 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000993 if (RetTy->isAnyComplexType()) {
994 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
995 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
996 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +0000997 }
998 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +0000999 llvm::Value *DestPtr = ReturnValue.getValue();
1000 bool DestIsVolatile = ReturnValue.isVolatile();
1001
1002 if (!DestPtr) {
1003 DestPtr = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
1004 DestIsVolatile = false;
1005 }
1006 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1007 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001008 }
1009 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001010
Daniel Dunbar11434922009-01-26 21:26:08 +00001011 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001012 // If we are ignoring an argument that had a result, make sure to
1013 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001014 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001015
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001016 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001017 llvm::Value *DestPtr = ReturnValue.getValue();
1018 bool DestIsVolatile = ReturnValue.isVolatile();
1019
1020 if (!DestPtr) {
1021 DestPtr = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
1022 DestIsVolatile = false;
1023 }
1024
1025 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001026 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001027 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001028 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001029 return RValue::getAggregate(DestPtr);
1030 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001031 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001032
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001033 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001034 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001035 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001036
1037 assert(0 && "Unhandled ABIArgInfo::Kind");
1038 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001039}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001040
1041/* VarArg handling */
1042
1043llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1044 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1045}