blob: 1cd899318a92c453eaf5b5f5f901b6942097e8ea [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
John McCallc0bf4622010-02-23 00:48:20 +0000419const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
420 const CGFunctionInfo &FI = getFunctionInfo(GD);
421
422 // For definition purposes, don't consider a K&R function variadic.
423 bool Variadic = false;
424 if (const FunctionProtoType *FPT =
425 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
426 Variadic = FPT->isVariadic();
427
428 return GetFunctionType(FI, Variadic);
429}
430
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000431const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000432CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000433 std::vector<const llvm::Type*> ArgTys;
434
435 const llvm::Type *ResultType = 0;
436
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000437 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000438 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000439 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000440 case ABIArgInfo::Expand:
441 assert(0 && "Invalid ABI kind for return argument");
442
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000443 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000444 case ABIArgInfo::Direct:
445 ResultType = ConvertType(RetTy);
446 break;
447
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000448 case ABIArgInfo::Indirect: {
449 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000450 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000451 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000452 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
453 break;
454 }
455
Daniel Dunbar11434922009-01-26 21:26:08 +0000456 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000457 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000458 break;
459
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000460 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000461 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000462 break;
463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
465 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000466 ie = FI.arg_end(); it != ie; ++it) {
467 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000469 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000470 case ABIArgInfo::Ignore:
471 break;
472
Daniel Dunbar56273772008-09-17 00:51:38 +0000473 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000474 ArgTys.push_back(AI.getCoerceToType());
475 break;
476
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000477 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000478 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000479 const llvm::Type *LTy = ConvertTypeForMem(it->type);
480 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000481 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000482 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000483
484 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000485 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000486 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000487 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000489 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000490 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000491 break;
492 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000493 }
494
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000495 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000496}
497
Anders Carlssonecf282b2009-11-24 05:08:52 +0000498static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
499 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
500 if (!TT->getDecl()->isDefinition())
501 return true;
502 }
503
504 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
505 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
506 if (!TT->getDecl()->isDefinition())
507 return true;
508 }
509 }
510
511 return false;
512}
513
514const llvm::Type *
515CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
516 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
517
518 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
519 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
520
521 return llvm::OpaqueType::get(getLLVMContext());
522}
523
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000524void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000525 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000526 AttributeListType &PAL,
527 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000528 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000529 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000530
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000531 CallingConv = FI.getEffectiveCallingConvention();
532
John McCall04a67a62010-02-05 21:31:56 +0000533 if (FI.isNoReturn())
534 FuncAttrs |= llvm::Attribute::NoReturn;
535
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000536 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000537 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000538 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000539 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000540 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000541 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000542 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000543 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000544 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000545 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000546 if (TargetDecl->hasAttr<MallocAttr>())
547 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000548 }
549
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000550 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000551 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000552 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000553 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000554 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000555 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000556
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000557 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000558 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000559 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000560 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000561 case ABIArgInfo::Extend:
562 if (RetTy->isSignedIntegerType()) {
563 RetAttrs |= llvm::Attribute::SExt;
564 } else if (RetTy->isUnsignedIntegerType()) {
565 RetAttrs |= llvm::Attribute::ZExt;
566 }
567 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000568 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000569 break;
570
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000571 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000572 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000573 llvm::Attribute::StructRet |
574 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000575 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000576 // sret disables readnone and readonly
577 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
578 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000579 break;
580
Daniel Dunbar11434922009-01-26 21:26:08 +0000581 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000582 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000583 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000584
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000585 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000586 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000587 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000588
Devang Patela2c69122008-09-26 22:53:57 +0000589 if (RetAttrs)
590 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000591
592 // FIXME: we need to honour command line settings also...
593 // FIXME: RegParm should be reduced in case of nested functions and/or global
594 // register variable.
595 signed RegParm = 0;
596 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000597 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000598 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000599 RegParm = RegParmAttr->getNumParams();
600
601 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000602 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000603 ie = FI.arg_end(); it != ie; ++it) {
604 QualType ParamType = it->type;
605 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000606 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000607
Nuno Lopes079b4952009-12-07 18:30:06 +0000608 if (ParamType.isRestrictQualified())
609 Attributes |= llvm::Attribute::NoAlias;
610
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000611 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000612 case ABIArgInfo::Coerce:
613 break;
614
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000615 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000616 if (AI.getIndirectByVal())
617 Attributes |= llvm::Attribute::ByVal;
618
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000619 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000620 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000621 // byval disables readnone and readonly.
622 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
623 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000624 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000625
626 case ABIArgInfo::Extend:
627 if (ParamType->isSignedIntegerType()) {
628 Attributes |= llvm::Attribute::SExt;
629 } else if (ParamType->isUnsignedIntegerType()) {
630 Attributes |= llvm::Attribute::ZExt;
631 }
632 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000633 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000634 if (RegParm > 0 &&
635 (ParamType->isIntegerType() || ParamType->isPointerType())) {
636 RegParm -=
637 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
638 if (RegParm >= 0)
639 Attributes |= llvm::Attribute::InReg;
640 }
641 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000642 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000643
Daniel Dunbar11434922009-01-26 21:26:08 +0000644 case ABIArgInfo::Ignore:
645 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000646 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000647
Daniel Dunbar56273772008-09-17 00:51:38 +0000648 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000649 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000650 // FIXME: This is rather inefficient. Do we ever actually need to do
651 // anything here? The result should be just reconstructed on the other
652 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000653 getTypes().GetExpandedTypes(ParamType, Tys);
654 Index += Tys.size();
655 continue;
656 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Devang Patel761d7f72008-09-25 21:02:23 +0000659 if (Attributes)
660 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000661 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000662 }
Devang Patela2c69122008-09-26 22:53:57 +0000663 if (FuncAttrs)
664 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000665}
666
Daniel Dunbar88b53962009-02-02 22:03:45 +0000667void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
668 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000669 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000670 // If this is an implicit-return-zero function, go ahead and
671 // initialize the return value. TODO: it might be nice to have
672 // a more general mechanism for this that didn't require synthesized
673 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000674 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000675 if (FD->hasImplicitReturnZero()) {
676 QualType RetTy = FD->getResultType().getUnqualifiedType();
677 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000678 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000679 Builder.CreateStore(Zero, ReturnValue);
680 }
681 }
682
Mike Stumpf5408fe2009-05-16 07:57:57 +0000683 // FIXME: We no longer need the types from FunctionArgList; lift up and
684 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000685
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000686 // Emit allocs for param decls. Give the LLVM Argument nodes names.
687 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000689 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000690 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000691 AI->setName("agg.result");
692 ++AI;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000695 assert(FI.arg_size() == Args.size() &&
696 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000697 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000698 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000699 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000700 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000701 QualType Ty = info_it->type;
702 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000703
704 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000705 case ABIArgInfo::Indirect: {
706 llvm::Value* V = AI;
707 if (hasAggregateLLVMType(Ty)) {
708 // Do nothing, aggregates and complex variables are accessed by
709 // reference.
710 } else {
711 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000712 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000713 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
714 // This must be a promotion, for something like
715 // "void a(x) short x; {..."
716 V = EmitScalarConversion(V, Ty, Arg->getType());
717 }
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000720 break;
721 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000722
723 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000724 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000725 assert(AI != Fn->arg_end() && "Argument mismatch!");
726 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000727 if (hasAggregateLLVMType(Ty)) {
728 // Create a temporary alloca to hold the argument; the rest of
729 // codegen expects to access aggregates & complex values by
730 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000731 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000732 Builder.CreateStore(AI, V);
733 } else {
734 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
735 // This must be a promotion, for something like
736 // "void a(x) short x; {..."
737 V = EmitScalarConversion(V, Ty, Arg->getType());
738 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000739 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000740 EmitParmDecl(*Arg, V);
741 break;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Daniel Dunbar56273772008-09-17 00:51:38 +0000744 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000745 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000746 // we need to create a temporary and reconstruct it from the
747 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000748 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000749 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000750 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000751 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000752 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000753
Daniel Dunbar56273772008-09-17 00:51:38 +0000754 // Name the arguments used in expansion and increment AI.
755 unsigned Index = 0;
756 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000757 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000758 continue;
759 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000760
761 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000762 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000763 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000764 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000765 } else {
766 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000769 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000770 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000771
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000772 case ABIArgInfo::Coerce: {
773 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000774 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
775 // result in a new alloca anyway, so we could just store into that
776 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000777 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000778 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000779 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000780 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000781 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000782 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
783 // This must be a promotion, for something like
784 // "void a(x) short x; {..."
785 V = EmitScalarConversion(V, Ty, Arg->getType());
786 }
787 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000788 EmitParmDecl(*Arg, V);
789 break;
790 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000791 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000792
793 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000794 }
795 assert(AI == Fn->arg_end() && "Argument mismatch!");
796}
797
Daniel Dunbar88b53962009-02-02 22:03:45 +0000798void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000799 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000800 llvm::Value *RV = 0;
801
802 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000803 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000804 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000805 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000806
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000807 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000808 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000809 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000810 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
811 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
812 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000813 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000814 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000815 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000816 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000817 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000818 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000819
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000820 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000821 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000822 // The internal return value temp always will have
823 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000824 RV = Builder.CreateLoad(ReturnValue);
825 break;
826
Daniel Dunbar11434922009-01-26 21:26:08 +0000827 case ABIArgInfo::Ignore:
828 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000830 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000831 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000832 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000833
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000834 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000835 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000836 }
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000839 if (RV) {
840 Builder.CreateRet(RV);
841 } else {
842 Builder.CreateRetVoid();
843 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000844}
845
Anders Carlsson0139bb92009-04-08 20:47:54 +0000846RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000847 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000848 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Anders Carlsson0139bb92009-04-08 20:47:54 +0000850 return EmitAnyExprToTemp(E);
851}
852
Daniel Dunbar88b53962009-02-02 22:03:45 +0000853RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000854 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000855 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000856 const CallArgList &CallArgs,
857 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000858 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000859 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000860
861 // Handle struct-return functions by passing a pointer to the
862 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000863 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000864 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000865
866
Chris Lattner5db7ae52009-06-13 00:26:38 +0000867 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000868 // alloca to hold the result, unless one is given to us.
869 if (CGM.ReturnTypeUsesSret(CallInfo)) {
870 llvm::Value *Value = ReturnValue.getValue();
871 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000872 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +0000873 Args.push_back(Value);
874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000876 assert(CallInfo.arg_size() == CallArgs.size() &&
877 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000878 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000879 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000880 I != E; ++I, ++info_it) {
881 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000882 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000883
884 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000885 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000886 if (RV.isScalar() || RV.isComplex()) {
887 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000888 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000889 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000890 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000891 else
Mike Stump1eb44332009-09-09 15:08:12 +0000892 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000893 } else {
894 Args.push_back(RV.getAggregateAddr());
895 }
896 break;
897
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000898 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000899 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000900 if (RV.isScalar()) {
901 Args.push_back(RV.getScalarVal());
902 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000903 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
904 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
905 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
906 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000907 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000908 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000909 }
910 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Daniel Dunbar11434922009-01-26 21:26:08 +0000912 case ABIArgInfo::Ignore:
913 break;
914
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000915 case ABIArgInfo::Coerce: {
916 // FIXME: Avoid the conversion through memory if possible.
917 llvm::Value *SrcPtr;
918 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000919 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000920 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000921 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000922 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000923 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000924 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000925 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000926 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000927 *this));
928 break;
929 }
930
Daniel Dunbar56273772008-09-17 00:51:38 +0000931 case ABIArgInfo::Expand:
932 ExpandTypeToArgs(I->second, RV, Args);
933 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000934 }
935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner5db7ae52009-06-13 00:26:38 +0000937 // If the callee is a bitcast of a function to a varargs pointer to function
938 // type, check to see if we can remove the bitcast. This handles some cases
939 // with unprototyped functions.
940 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
941 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
942 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
943 const llvm::FunctionType *CurFT =
944 cast<llvm::FunctionType>(CurPT->getElementType());
945 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner5db7ae52009-06-13 00:26:38 +0000947 if (CE->getOpcode() == llvm::Instruction::BitCast &&
948 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000949 ActualFT->getNumParams() == CurFT->getNumParams() &&
950 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000951 bool ArgsMatch = true;
952 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
953 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
954 ArgsMatch = false;
955 break;
956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattner5db7ae52009-06-13 00:26:38 +0000958 // Strip the cast if we can get away with it. This is a nice cleanup,
959 // but also allows us to inline the function at -O0 if it is marked
960 // always_inline.
961 if (ArgsMatch)
962 Callee = CalleeF;
963 }
964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000966
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000967 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000968 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000969 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000970 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000971 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
972 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Daniel Dunbard14151d2009-03-02 04:32:35 +0000974 llvm::CallSite CS;
975 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000976 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000977 } else {
978 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000979 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000980 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000981 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000982 }
983
Daniel Dunbard14151d2009-03-02 04:32:35 +0000984 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000985 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +0000986
987 // If the call doesn't return, finish the basic block and clear the
988 // insertion point; this allows the rest of IRgen to discard
989 // unreachable code.
990 if (CS.doesNotReturn()) {
991 Builder.CreateUnreachable();
992 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Mike Stumpf5408fe2009-05-16 07:57:57 +0000994 // FIXME: For now, emit a dummy basic block because expr emitters in
995 // generally are not ready to handle emitting expressions at unreachable
996 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +0000997 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Daniel Dunbard14151d2009-03-02 04:32:35 +0000999 // Return a reasonable RValue.
1000 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001001 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001002
1003 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001004 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001005 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001006
1007 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001008 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001009 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001010 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001011 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001012 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001013 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001014
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001015 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001016 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001017 if (RetTy->isAnyComplexType()) {
1018 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1019 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1020 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001021 }
1022 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001023 llvm::Value *DestPtr = ReturnValue.getValue();
1024 bool DestIsVolatile = ReturnValue.isVolatile();
1025
1026 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001027 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001028 DestIsVolatile = false;
1029 }
1030 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1031 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001032 }
1033 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001034
Daniel Dunbar11434922009-01-26 21:26:08 +00001035 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001036 // If we are ignoring an argument that had a result, make sure to
1037 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001038 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001039
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001040 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001041 llvm::Value *DestPtr = ReturnValue.getValue();
1042 bool DestIsVolatile = ReturnValue.isVolatile();
1043
1044 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001045 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001046 DestIsVolatile = false;
1047 }
1048
1049 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001050 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001051 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001052 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001053 return RValue::getAggregate(DestPtr);
1054 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001055 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001056
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001057 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001058 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001059 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001060
1061 assert(0 && "Unhandled ABIArgInfo::Kind");
1062 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001063}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001064
1065/* VarArg handling */
1066
1067llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1068 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1069}