blob: 707513a23db6b7723e9cd008a8c9eb4b1eba0b08 [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
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000170const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
171 // FIXME: Do we need to handle ObjCMethodDecl?
172 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
173
174 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
175 return getFunctionInfo(CD, GD.getCtorType());
176
177 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
178 return getFunctionInfo(DD, GD.getDtorType());
179
180 return getFunctionInfo(FD);
181}
182
Mike Stump1eb44332009-09-09 15:08:12 +0000183const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000184 const CallArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000185 CallingConv CC,
186 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000187 // FIXME: Kill copy.
188 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000189 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000190 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000191 ArgTys.push_back(i->second);
John McCall04a67a62010-02-05 21:31:56 +0000192 return getFunctionInfo(ResTy, ArgTys, CC, NoReturn);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000193}
194
Mike Stump1eb44332009-09-09 15:08:12 +0000195const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000196 const FunctionArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000197 CallingConv CC,
198 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000199 // FIXME: Kill copy.
200 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000201 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000202 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000203 ArgTys.push_back(i->second);
John McCall04a67a62010-02-05 21:31:56 +0000204 return getFunctionInfo(ResTy, ArgTys, CC, NoReturn);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000205}
206
207const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000208 const llvm::SmallVector<QualType, 16> &ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000209 CallingConv CallConv,
210 bool NoReturn) {
211 unsigned CC = ClangCallConvToLLVMCallConv(CallConv);
212
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000213 // Lookup or create unique function info.
214 llvm::FoldingSetNodeID ID;
John McCall04a67a62010-02-05 21:31:56 +0000215 CGFunctionInfo::Profile(ID, CC, NoReturn, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000216 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000217
218 void *InsertPos = 0;
219 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
220 if (FI)
221 return *FI;
222
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000223 // Construct the function info.
John McCall04a67a62010-02-05 21:31:56 +0000224 FI = new CGFunctionInfo(CC, NoReturn, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000225 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000226
227 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000228 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000229
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000230 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000231}
232
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000233CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000234 bool _NoReturn,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000235 QualType ResTy,
236 const llvm::SmallVector<QualType, 16> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000237 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000238 EffectiveCallingConvention(_CallingConvention),
239 NoReturn(_NoReturn)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000240{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000241 NumArgs = ArgTys.size();
242 Args = new ArgInfo[1 + NumArgs];
243 Args[0].type = ResTy;
244 for (unsigned i = 0; i < NumArgs; ++i)
245 Args[1 + i].type = ArgTys[i];
246}
247
248/***/
249
Mike Stump1eb44332009-09-09 15:08:12 +0000250void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000251 std::vector<const llvm::Type*> &ArgTys) {
252 const RecordType *RT = Ty->getAsStructureType();
253 assert(RT && "Can only expand structure types.");
254 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000255 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000256 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000258 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
259 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000260 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000261 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000262 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Daniel Dunbar56273772008-09-17 00:51:38 +0000264 QualType FT = FD->getType();
265 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
266 GetExpandedTypes(FT, ArgTys);
267 } else {
268 ArgTys.push_back(ConvertType(FT));
269 }
270 }
271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000274CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
275 llvm::Function::arg_iterator AI) {
276 const RecordType *RT = Ty->getAsStructureType();
277 assert(RT && "Can only expand structure types.");
278
279 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000280 assert(LV.isSimple() &&
281 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000282 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000283 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
284 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000285 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000286 QualType FT = FD->getType();
287
288 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000289 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000290 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
291 AI = ExpandTypeFromArgs(FT, LV, AI);
292 } else {
293 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
294 ++AI;
295 }
296 }
297
298 return AI;
299}
300
Mike Stump1eb44332009-09-09 15:08:12 +0000301void
302CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000303 llvm::SmallVector<llvm::Value*, 16> &Args) {
304 const RecordType *RT = Ty->getAsStructureType();
305 assert(RT && "Can only expand structure types.");
306
307 RecordDecl *RD = RT->getDecl();
308 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
309 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000310 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
311 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000312 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000313 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Daniel Dunbar56273772008-09-17 00:51:38 +0000315 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000316 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000317 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
318 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
319 } else {
320 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000321 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000322 "Unexpected non-scalar rvalue during struct expansion.");
323 Args.push_back(RV.getScalarVal());
324 }
325 }
326}
327
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000328/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
329/// a pointer to an object of type \arg Ty.
330///
331/// This safely handles the case when the src type is smaller than the
332/// destination type; in this situation the values of bits which not
333/// present in the src are undefined.
334static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
335 const llvm::Type *Ty,
336 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000337 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000338 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000339 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
340 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000341
Daniel Dunbarb225be42009-02-03 05:59:18 +0000342 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000343 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000344 // Generally SrcSize is never greater than DstSize, since this means we are
345 // losing bits. However, this can happen in cases where the structure has
346 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000347 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000348 // FIXME: Assert that we aren't truncating non-padding bits when have access
349 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000350 llvm::Value *Casted =
351 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000352 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
353 // FIXME: Use better alignment / avoid requiring aligned load.
354 Load->setAlignment(1);
355 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000356 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000357 // Otherwise do coercion through memory. This is stupid, but
358 // simple.
359 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000360 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000361 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000362 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000363 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
364 // FIXME: Use better alignment / avoid requiring aligned store.
365 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000366 return CGF.Builder.CreateLoad(Tmp);
367 }
368}
369
370/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
371/// where the source and destination may have different types.
372///
373/// This safely handles the case when the src type is larger than the
374/// destination type; the upper bits of the src will be lost.
375static void CreateCoercedStore(llvm::Value *Src,
376 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000377 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000378 CodeGenFunction &CGF) {
379 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000380 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000381 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
382
Duncan Sands9408c452009-05-09 07:08:47 +0000383 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
384 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000385
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000386 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000387 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000388 llvm::Value *Casted =
389 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000390 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000391 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000392 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000393 // Otherwise do coercion through memory. This is stupid, but
394 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000395
396 // Generally SrcSize is never greater than DstSize, since this means we are
397 // losing bits. However, this can happen in cases where the structure has
398 // additional padding, for example due to a user specified alignment.
399 //
400 // FIXME: Assert that we aren't truncating non-padding bits when have access
401 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000402 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
403 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000404 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000405 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000406 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
407 // FIXME: Use better alignment / avoid requiring aligned load.
408 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000409 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000410 }
411}
412
Daniel Dunbar56273772008-09-17 00:51:38 +0000413/***/
414
Daniel Dunbar88b53962009-02-02 22:03:45 +0000415bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000416 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000417}
418
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000419const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000420CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000421 std::vector<const llvm::Type*> ArgTys;
422
423 const llvm::Type *ResultType = 0;
424
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000425 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000426 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000427 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000428 case ABIArgInfo::Expand:
429 assert(0 && "Invalid ABI kind for return argument");
430
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000431 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000432 case ABIArgInfo::Direct:
433 ResultType = ConvertType(RetTy);
434 break;
435
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000436 case ABIArgInfo::Indirect: {
437 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000438 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000439 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000440 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
441 break;
442 }
443
Daniel Dunbar11434922009-01-26 21:26:08 +0000444 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000445 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000446 break;
447
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000448 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000449 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000450 break;
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
453 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000454 ie = FI.arg_end(); it != ie; ++it) {
455 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000457 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000458 case ABIArgInfo::Ignore:
459 break;
460
Daniel Dunbar56273772008-09-17 00:51:38 +0000461 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000462 ArgTys.push_back(AI.getCoerceToType());
463 break;
464
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000465 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000466 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000467 const llvm::Type *LTy = ConvertTypeForMem(it->type);
468 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000469 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000470 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000471
472 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000473 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000474 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000475 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000477 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000478 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000479 break;
480 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000481 }
482
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000483 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000484}
485
Anders Carlssonecf282b2009-11-24 05:08:52 +0000486static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
487 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
488 if (!TT->getDecl()->isDefinition())
489 return true;
490 }
491
492 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
493 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
494 if (!TT->getDecl()->isDefinition())
495 return true;
496 }
497 }
498
499 return false;
500}
501
502const llvm::Type *
503CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
504 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
505
506 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
507 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
508
509 return llvm::OpaqueType::get(getLLVMContext());
510}
511
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000512void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000513 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000514 AttributeListType &PAL,
515 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000516 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000517 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000518
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000519 CallingConv = FI.getEffectiveCallingConvention();
520
John McCall04a67a62010-02-05 21:31:56 +0000521 if (FI.isNoReturn())
522 FuncAttrs |= llvm::Attribute::NoReturn;
523
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000524 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000525 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000526 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000527 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000528 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000529 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000530 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000531 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000532 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000533 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000534 if (TargetDecl->hasAttr<MallocAttr>())
535 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000536 }
537
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000538 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000539 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000540 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000541 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000542 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000543 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000544
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000545 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000546 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000547 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000548 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000549 case ABIArgInfo::Extend:
550 if (RetTy->isSignedIntegerType()) {
551 RetAttrs |= llvm::Attribute::SExt;
552 } else if (RetTy->isUnsignedIntegerType()) {
553 RetAttrs |= llvm::Attribute::ZExt;
554 }
555 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000556 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000557 break;
558
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000559 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000560 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000561 llvm::Attribute::StructRet |
562 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000563 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000564 // sret disables readnone and readonly
565 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
566 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000567 break;
568
Daniel Dunbar11434922009-01-26 21:26:08 +0000569 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000570 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000571 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000572
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000573 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000574 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000575 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000576
Devang Patela2c69122008-09-26 22:53:57 +0000577 if (RetAttrs)
578 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000579
580 // FIXME: we need to honour command line settings also...
581 // FIXME: RegParm should be reduced in case of nested functions and/or global
582 // register variable.
583 signed RegParm = 0;
584 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000585 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000586 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000587 RegParm = RegParmAttr->getNumParams();
588
589 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000590 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000591 ie = FI.arg_end(); it != ie; ++it) {
592 QualType ParamType = it->type;
593 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000594 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000595
Nuno Lopes079b4952009-12-07 18:30:06 +0000596 if (ParamType.isRestrictQualified())
597 Attributes |= llvm::Attribute::NoAlias;
598
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000599 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000600 case ABIArgInfo::Coerce:
601 break;
602
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000603 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000604 if (AI.getIndirectByVal())
605 Attributes |= llvm::Attribute::ByVal;
606
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000607 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000608 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000609 // byval disables readnone and readonly.
610 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
611 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000612 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000613
614 case ABIArgInfo::Extend:
615 if (ParamType->isSignedIntegerType()) {
616 Attributes |= llvm::Attribute::SExt;
617 } else if (ParamType->isUnsignedIntegerType()) {
618 Attributes |= llvm::Attribute::ZExt;
619 }
620 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000621 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000622 if (RegParm > 0 &&
623 (ParamType->isIntegerType() || ParamType->isPointerType())) {
624 RegParm -=
625 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
626 if (RegParm >= 0)
627 Attributes |= llvm::Attribute::InReg;
628 }
629 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000630 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000631
Daniel Dunbar11434922009-01-26 21:26:08 +0000632 case ABIArgInfo::Ignore:
633 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000634 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000635
Daniel Dunbar56273772008-09-17 00:51:38 +0000636 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000637 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000638 // FIXME: This is rather inefficient. Do we ever actually need to do
639 // anything here? The result should be just reconstructed on the other
640 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000641 getTypes().GetExpandedTypes(ParamType, Tys);
642 Index += Tys.size();
643 continue;
644 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Devang Patel761d7f72008-09-25 21:02:23 +0000647 if (Attributes)
648 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000649 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000650 }
Devang Patela2c69122008-09-26 22:53:57 +0000651 if (FuncAttrs)
652 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000653}
654
Daniel Dunbar88b53962009-02-02 22:03:45 +0000655void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
656 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000657 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000658 // If this is an implicit-return-zero function, go ahead and
659 // initialize the return value. TODO: it might be nice to have
660 // a more general mechanism for this that didn't require synthesized
661 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000662 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000663 if (FD->hasImplicitReturnZero()) {
664 QualType RetTy = FD->getResultType().getUnqualifiedType();
665 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000666 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000667 Builder.CreateStore(Zero, ReturnValue);
668 }
669 }
670
Mike Stumpf5408fe2009-05-16 07:57:57 +0000671 // FIXME: We no longer need the types from FunctionArgList; lift up and
672 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000673
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000674 // Emit allocs for param decls. Give the LLVM Argument nodes names.
675 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000677 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000678 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000679 AI->setName("agg.result");
680 ++AI;
681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000683 assert(FI.arg_size() == Args.size() &&
684 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000685 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000686 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000687 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000688 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000689 QualType Ty = info_it->type;
690 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000691
692 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000693 case ABIArgInfo::Indirect: {
694 llvm::Value* V = AI;
695 if (hasAggregateLLVMType(Ty)) {
696 // Do nothing, aggregates and complex variables are accessed by
697 // reference.
698 } else {
699 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000700 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000701 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
702 // This must be a promotion, for something like
703 // "void a(x) short x; {..."
704 V = EmitScalarConversion(V, Ty, Arg->getType());
705 }
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000708 break;
709 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000710
711 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000712 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000713 assert(AI != Fn->arg_end() && "Argument mismatch!");
714 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000715 if (hasAggregateLLVMType(Ty)) {
716 // Create a temporary alloca to hold the argument; the rest of
717 // codegen expects to access aggregates & complex values by
718 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000719 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000720 Builder.CreateStore(AI, V);
721 } else {
722 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
723 // This must be a promotion, for something like
724 // "void a(x) short x; {..."
725 V = EmitScalarConversion(V, Ty, Arg->getType());
726 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000727 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000728 EmitParmDecl(*Arg, V);
729 break;
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Daniel Dunbar56273772008-09-17 00:51:38 +0000732 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000733 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000734 // we need to create a temporary and reconstruct it from the
735 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000736 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000737 Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000738 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000739 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000740 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000741 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000742
Daniel Dunbar56273772008-09-17 00:51:38 +0000743 // Name the arguments used in expansion and increment AI.
744 unsigned Index = 0;
745 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000746 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000747 continue;
748 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000749
750 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000751 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000752 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000753 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000754 } else {
755 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000758 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000759 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000760
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000761 case ABIArgInfo::Coerce: {
762 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000763 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
764 // result in a new alloca anyway, so we could just store into that
765 // directly if we broke the abstraction down more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000766 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000767 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000768 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000769 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000770 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000771 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
772 // This must be a promotion, for something like
773 // "void a(x) short x; {..."
774 V = EmitScalarConversion(V, Ty, Arg->getType());
775 }
776 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000777 EmitParmDecl(*Arg, V);
778 break;
779 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000780 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000781
782 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000783 }
784 assert(AI == Fn->arg_end() && "Argument mismatch!");
785}
786
Daniel Dunbar88b53962009-02-02 22:03:45 +0000787void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000788 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000789 llvm::Value *RV = 0;
790
791 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000792 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000793 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000794 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000795
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000796 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000797 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000798 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000799 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
800 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
801 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000802 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000803 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000804 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000805 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000806 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000807 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000808
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000809 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000810 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000811 // The internal return value temp always will have
812 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000813 RV = Builder.CreateLoad(ReturnValue);
814 break;
815
Daniel Dunbar11434922009-01-26 21:26:08 +0000816 case ABIArgInfo::Ignore:
817 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000819 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000820 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000821 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000822
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000823 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000824 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000825 }
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000828 if (RV) {
829 Builder.CreateRet(RV);
830 } else {
831 Builder.CreateRetVoid();
832 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000833}
834
Anders Carlsson0139bb92009-04-08 20:47:54 +0000835RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000836 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000837 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Anders Carlsson0139bb92009-04-08 20:47:54 +0000839 return EmitAnyExprToTemp(E);
840}
841
Daniel Dunbar88b53962009-02-02 22:03:45 +0000842RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000843 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000844 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000845 const CallArgList &CallArgs,
846 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000847 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000848 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000849
850 // Handle struct-return functions by passing a pointer to the
851 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000852 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000853 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
855
Chris Lattner5db7ae52009-06-13 00:26:38 +0000856 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000857 // alloca to hold the result, unless one is given to us.
858 if (CGM.ReturnTypeUsesSret(CallInfo)) {
859 llvm::Value *Value = ReturnValue.getValue();
860 if (!Value)
861 Value = CreateTempAlloca(ConvertTypeForMem(RetTy));
862 Args.push_back(Value);
863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000865 assert(CallInfo.arg_size() == CallArgs.size() &&
866 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000867 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000868 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000869 I != E; ++I, ++info_it) {
870 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000871 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000872
873 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000874 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000875 if (RV.isScalar() || RV.isComplex()) {
876 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000877 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000878 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000879 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000880 else
Mike Stump1eb44332009-09-09 15:08:12 +0000881 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000882 } else {
883 Args.push_back(RV.getAggregateAddr());
884 }
885 break;
886
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000887 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000888 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000889 if (RV.isScalar()) {
890 Args.push_back(RV.getScalarVal());
891 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000892 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
893 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
894 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
895 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000896 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000897 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000898 }
899 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Daniel Dunbar11434922009-01-26 21:26:08 +0000901 case ABIArgInfo::Ignore:
902 break;
903
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000904 case ABIArgInfo::Coerce: {
905 // FIXME: Avoid the conversion through memory if possible.
906 llvm::Value *SrcPtr;
907 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +0000908 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000909 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000910 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000911 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000912 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000913 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000914 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000915 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000916 *this));
917 break;
918 }
919
Daniel Dunbar56273772008-09-17 00:51:38 +0000920 case ABIArgInfo::Expand:
921 ExpandTypeToArgs(I->second, RV, Args);
922 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000923 }
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattner5db7ae52009-06-13 00:26:38 +0000926 // If the callee is a bitcast of a function to a varargs pointer to function
927 // type, check to see if we can remove the bitcast. This handles some cases
928 // with unprototyped functions.
929 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
930 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
931 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
932 const llvm::FunctionType *CurFT =
933 cast<llvm::FunctionType>(CurPT->getElementType());
934 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattner5db7ae52009-06-13 00:26:38 +0000936 if (CE->getOpcode() == llvm::Instruction::BitCast &&
937 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000938 ActualFT->getNumParams() == CurFT->getNumParams() &&
939 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000940 bool ArgsMatch = true;
941 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
942 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
943 ArgsMatch = false;
944 break;
945 }
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner5db7ae52009-06-13 00:26:38 +0000947 // Strip the cast if we can get away with it. This is a nice cleanup,
948 // but also allows us to inline the function at -O0 if it is marked
949 // always_inline.
950 if (ArgsMatch)
951 Callee = CalleeF;
952 }
953 }
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000955
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000956 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000957 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000958 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000959 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000960 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
961 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Daniel Dunbard14151d2009-03-02 04:32:35 +0000963 llvm::CallSite CS;
964 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000965 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000966 } else {
967 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000968 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000969 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000970 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000971 }
972
Daniel Dunbard14151d2009-03-02 04:32:35 +0000973 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000974 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +0000975
976 // If the call doesn't return, finish the basic block and clear the
977 // insertion point; this allows the rest of IRgen to discard
978 // unreachable code.
979 if (CS.doesNotReturn()) {
980 Builder.CreateUnreachable();
981 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Mike Stumpf5408fe2009-05-16 07:57:57 +0000983 // FIXME: For now, emit a dummy basic block because expr emitters in
984 // generally are not ready to handle emitting expressions at unreachable
985 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +0000986 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Daniel Dunbard14151d2009-03-02 04:32:35 +0000988 // Return a reasonable RValue.
989 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000990 }
Daniel Dunbard14151d2009-03-02 04:32:35 +0000991
992 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +0000993 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000994 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000995
996 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000997 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000998 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000999 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001000 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001001 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001002 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001003
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001004 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001005 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001006 if (RetTy->isAnyComplexType()) {
1007 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1008 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1009 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001010 }
1011 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001012 llvm::Value *DestPtr = ReturnValue.getValue();
1013 bool DestIsVolatile = ReturnValue.isVolatile();
1014
1015 if (!DestPtr) {
1016 DestPtr = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
1017 DestIsVolatile = false;
1018 }
1019 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1020 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001021 }
1022 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001023
Daniel Dunbar11434922009-01-26 21:26:08 +00001024 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001025 // If we are ignoring an argument that had a result, make sure to
1026 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001027 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001028
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001029 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001030 llvm::Value *DestPtr = ReturnValue.getValue();
1031 bool DestIsVolatile = ReturnValue.isVolatile();
1032
1033 if (!DestPtr) {
1034 DestPtr = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
1035 DestIsVolatile = false;
1036 }
1037
1038 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001039 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001040 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001041 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001042 return RValue::getAggregate(DestPtr);
1043 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001044 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001045
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001046 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001047 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001048 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001049
1050 assert(0 && "Unhandled ABIArgInfo::Kind");
1051 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001052}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001053
1054/* VarArg handling */
1055
1056llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1057 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1058}