blob: 072b1f6585fdaba89223cb5f9b9a41b385c001ba [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000021#include "clang/AST/DeclObjC.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000022#include "clang/CodeGen/CodeGenOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000024#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000026
27#include "ABIInfo.h"
28
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034// FIXME: Use iterator and sidestep silly type array creation.
35
John McCall04a67a62010-02-05 21:31:56 +000036static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
37 switch (CC) {
38 default: return llvm::CallingConv::C;
39 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
40 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
41 }
42}
43
John McCall0b0ef0a2010-02-24 07:14:12 +000044/// Derives the 'this' type for codegen purposes, i.e. ignoring method
45/// qualification.
46/// FIXME: address space qualification?
John McCallead608a2010-02-26 00:48:12 +000047static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
48 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
49 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050}
51
John McCall0b0ef0a2010-02-24 07:14:12 +000052/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000053static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
54 return MD->getType()->getCanonicalTypeUnqualified()
55 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-02-24 07:14:12 +000056}
57
58/// Returns the "extra-canonicalized" return type, which discards
59/// qualifiers on the return type. Codegen doesn't care about them,
60/// and it makes ABI code a little easier to be able to assume that
61/// all parameter and return types are top-level unqualified.
John McCallead608a2010-02-26 00:48:12 +000062static CanQualType GetReturnType(QualType RetTy) {
63 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000064}
65
66const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000067CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
68 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
69 llvm::SmallVector<CanQualType, 16>(),
John McCall0b0ef0a2010-02-24 07:14:12 +000070 FTNP->getCallConv(),
71 FTNP->getNoReturnAttr());
72}
73
74/// \param Args - contains any initial parameters besides those
75/// in the formal type
76static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000077 llvm::SmallVectorImpl<CanQualType> &ArgTys,
78 CanQual<FunctionProtoType> FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000080 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000082 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
83 return CGT.getFunctionInfo(ResTy, ArgTys,
John McCall0b0ef0a2010-02-24 07:14:12 +000084 FTP->getCallConv(),
85 FTP->getNoReturnAttr());
86}
87
88const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000089CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
90 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +000091 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000092}
93
John McCall04a67a62010-02-05 21:31:56 +000094static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000095 // Set the appropriate calling convention for the Function.
96 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000097 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000098
99 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +0000100 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000101
John McCall04a67a62010-02-05 21:31:56 +0000102 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000103}
104
Anders Carlsson375c31c2009-10-03 19:43:08 +0000105const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
106 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000107 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000108
Anders Carlsson375c31c2009-10-03 19:43:08 +0000109 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000110 ArgTys.push_back(GetThisType(Context, RD));
111
112 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000113 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000114}
115
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000116const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000117 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000118
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000119 // Add the 'this' pointer unless this is a static method.
120 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000121 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000122
John McCall0b0ef0a2010-02-24 07:14:12 +0000123 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000124}
125
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000126const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
127 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000128 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000129
130 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000131 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000132
133 // Check if we need to add a VTT parameter (which has type void **).
134 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
135 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000136
137 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000138}
139
140const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
141 CXXDtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000142 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000143
144 // Add the 'this' pointer.
John McCallead608a2010-02-26 00:48:12 +0000145 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000146
147 // Check if we need to add a VTT parameter (which has type void **).
148 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
149 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000150
151 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000152}
153
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000154const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000155 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000156 if (MD->isInstance())
157 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
John McCallead608a2010-02-26 00:48:12 +0000159 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
160 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000161 if (isa<FunctionNoProtoType>(FTy))
John McCallead608a2010-02-26 00:48:12 +0000162 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
163 assert(isa<FunctionProtoType>(FTy));
164 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000165}
166
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000167const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000168 llvm::SmallVector<CanQualType, 16> ArgTys;
169 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
170 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000171 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000172 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000173 e = MD->param_end(); i != e; ++i) {
174 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
175 }
176 return getFunctionInfo(GetReturnType(MD->getResultType()),
177 ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000178 getCallingConventionForDecl(MD),
179 /*NoReturn*/ false);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000180}
181
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000182const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
183 // FIXME: Do we need to handle ObjCMethodDecl?
184 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
185
186 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
187 return getFunctionInfo(CD, GD.getCtorType());
188
189 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
190 return getFunctionInfo(DD, GD.getDtorType());
191
192 return getFunctionInfo(FD);
193}
194
Mike Stump1eb44332009-09-09 15:08:12 +0000195const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000196 const CallArgList &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.
John McCallead608a2010-02-26 00:48:12 +0000200 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000201 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000202 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000203 ArgTys.push_back(Context.getCanonicalParamType(i->second));
204 return getFunctionInfo(GetReturnType(ResTy), ArgTys, CC, NoReturn);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000205}
206
Mike Stump1eb44332009-09-09 15:08:12 +0000207const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000208 const FunctionArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000209 CallingConv CC,
210 bool NoReturn) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000211 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000212 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000213 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000214 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000215 ArgTys.push_back(Context.getCanonicalParamType(i->second));
216 return getFunctionInfo(GetReturnType(ResTy), ArgTys, CC, NoReturn);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000217}
218
John McCallead608a2010-02-26 00:48:12 +0000219const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
220 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000221 CallingConv CallConv,
222 bool NoReturn) {
John McCallead608a2010-02-26 00:48:12 +0000223#ifndef NDEBUG
224 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
225 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
226 assert(I->isCanonicalAsParam());
227#endif
228
John McCall04a67a62010-02-05 21:31:56 +0000229 unsigned CC = ClangCallConvToLLVMCallConv(CallConv);
230
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000231 // Lookup or create unique function info.
232 llvm::FoldingSetNodeID ID;
John McCall04a67a62010-02-05 21:31:56 +0000233 CGFunctionInfo::Profile(ID, CC, NoReturn, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000234 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000235
236 void *InsertPos = 0;
237 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
238 if (FI)
239 return *FI;
240
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000241 // Construct the function info.
John McCall04a67a62010-02-05 21:31:56 +0000242 FI = new CGFunctionInfo(CC, NoReturn, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000243 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000244
245 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000246 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000247
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000248 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000249}
250
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000251CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000252 bool _NoReturn,
John McCallead608a2010-02-26 00:48:12 +0000253 CanQualType ResTy,
254 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000255 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000256 EffectiveCallingConvention(_CallingConvention),
257 NoReturn(_NoReturn)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000258{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000259 NumArgs = ArgTys.size();
260 Args = new ArgInfo[1 + NumArgs];
261 Args[0].type = ResTy;
262 for (unsigned i = 0; i < NumArgs; ++i)
263 Args[1 + i].type = ArgTys[i];
264}
265
266/***/
267
Mike Stump1eb44332009-09-09 15:08:12 +0000268void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000269 std::vector<const llvm::Type*> &ArgTys) {
270 const RecordType *RT = Ty->getAsStructureType();
271 assert(RT && "Can only expand structure types.");
272 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000273 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000274 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000276 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
277 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000278 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000279 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000280 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Daniel Dunbar56273772008-09-17 00:51:38 +0000282 QualType FT = FD->getType();
283 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
284 GetExpandedTypes(FT, ArgTys);
285 } else {
286 ArgTys.push_back(ConvertType(FT));
287 }
288 }
289}
290
Mike Stump1eb44332009-09-09 15:08:12 +0000291llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000292CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
293 llvm::Function::arg_iterator AI) {
294 const RecordType *RT = Ty->getAsStructureType();
295 assert(RT && "Can only expand structure types.");
296
297 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000298 assert(LV.isSimple() &&
299 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000300 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000301 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
302 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000303 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000304 QualType FT = FD->getType();
305
306 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000307 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000308 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
309 AI = ExpandTypeFromArgs(FT, LV, AI);
310 } else {
311 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
312 ++AI;
313 }
314 }
315
316 return AI;
317}
318
Mike Stump1eb44332009-09-09 15:08:12 +0000319void
320CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000321 llvm::SmallVector<llvm::Value*, 16> &Args) {
322 const RecordType *RT = Ty->getAsStructureType();
323 assert(RT && "Can only expand structure types.");
324
325 RecordDecl *RD = RT->getDecl();
326 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
327 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000328 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
329 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000330 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000331 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Daniel Dunbar56273772008-09-17 00:51:38 +0000333 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000334 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000335 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
336 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
337 } else {
338 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000339 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000340 "Unexpected non-scalar rvalue during struct expansion.");
341 Args.push_back(RV.getScalarVal());
342 }
343 }
344}
345
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000346/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
347/// a pointer to an object of type \arg Ty.
348///
349/// This safely handles the case when the src type is smaller than the
350/// destination type; in this situation the values of bits which not
351/// present in the src are undefined.
352static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
353 const llvm::Type *Ty,
354 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000355 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000356 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000357 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
358 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000359
Daniel Dunbarb225be42009-02-03 05:59:18 +0000360 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000361 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000362 // Generally SrcSize is never greater than DstSize, since this means we are
363 // losing bits. However, this can happen in cases where the structure has
364 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000365 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000366 // FIXME: Assert that we aren't truncating non-padding bits when have access
367 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000368 llvm::Value *Casted =
369 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000370 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
371 // FIXME: Use better alignment / avoid requiring aligned load.
372 Load->setAlignment(1);
373 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000374 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000375 // Otherwise do coercion through memory. This is stupid, but
376 // simple.
377 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000378 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000379 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000380 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000381 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
382 // FIXME: Use better alignment / avoid requiring aligned store.
383 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000384 return CGF.Builder.CreateLoad(Tmp);
385 }
386}
387
388/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
389/// where the source and destination may have different types.
390///
391/// This safely handles the case when the src type is larger than the
392/// destination type; the upper bits of the src will be lost.
393static void CreateCoercedStore(llvm::Value *Src,
394 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000395 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000396 CodeGenFunction &CGF) {
397 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000398 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000399 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
400
Duncan Sands9408c452009-05-09 07:08:47 +0000401 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
402 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000403
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000404 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000405 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000406 llvm::Value *Casted =
407 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000408 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000409 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000410 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000411 // Otherwise do coercion through memory. This is stupid, but
412 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000413
414 // Generally SrcSize is never greater than DstSize, since this means we are
415 // losing bits. However, this can happen in cases where the structure has
416 // additional padding, for example due to a user specified alignment.
417 //
418 // FIXME: Assert that we aren't truncating non-padding bits when have access
419 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000420 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
421 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000422 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000423 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000424 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
425 // FIXME: Use better alignment / avoid requiring aligned load.
426 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000427 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000428 }
429}
430
Daniel Dunbar56273772008-09-17 00:51:38 +0000431/***/
432
Daniel Dunbar88b53962009-02-02 22:03:45 +0000433bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000434 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000435}
436
John McCallc0bf4622010-02-23 00:48:20 +0000437const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
438 const CGFunctionInfo &FI = getFunctionInfo(GD);
439
440 // For definition purposes, don't consider a K&R function variadic.
441 bool Variadic = false;
442 if (const FunctionProtoType *FPT =
443 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
444 Variadic = FPT->isVariadic();
445
446 return GetFunctionType(FI, Variadic);
447}
448
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000449const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000450CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000451 std::vector<const llvm::Type*> ArgTys;
452
453 const llvm::Type *ResultType = 0;
454
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000455 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000456 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000457 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000458 case ABIArgInfo::Expand:
459 assert(0 && "Invalid ABI kind for return argument");
460
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000461 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000462 case ABIArgInfo::Direct:
463 ResultType = ConvertType(RetTy);
464 break;
465
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000466 case ABIArgInfo::Indirect: {
467 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000468 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000469 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000470 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
471 break;
472 }
473
Daniel Dunbar11434922009-01-26 21:26:08 +0000474 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000475 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000476 break;
477
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000478 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000479 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000480 break;
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
483 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000484 ie = FI.arg_end(); it != ie; ++it) {
485 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000487 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000488 case ABIArgInfo::Ignore:
489 break;
490
Daniel Dunbar56273772008-09-17 00:51:38 +0000491 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000492 ArgTys.push_back(AI.getCoerceToType());
493 break;
494
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000495 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000496 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000497 const llvm::Type *LTy = ConvertTypeForMem(it->type);
498 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000499 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000500 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000501
502 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000503 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000504 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000505 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000507 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000508 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000509 break;
510 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000511 }
512
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000513 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000514}
515
Anders Carlssonecf282b2009-11-24 05:08:52 +0000516static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
517 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
518 if (!TT->getDecl()->isDefinition())
519 return true;
520 }
521
522 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
523 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
524 if (!TT->getDecl()->isDefinition())
525 return true;
526 }
527 }
528
529 return false;
530}
531
532const llvm::Type *
533CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
534 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
535
536 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
537 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
538
539 return llvm::OpaqueType::get(getLLVMContext());
540}
541
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000542void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000543 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000544 AttributeListType &PAL,
545 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000546 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000547 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000548
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000549 CallingConv = FI.getEffectiveCallingConvention();
550
John McCall04a67a62010-02-05 21:31:56 +0000551 if (FI.isNoReturn())
552 FuncAttrs |= llvm::Attribute::NoReturn;
553
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000554 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000555 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000556 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000557 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000558 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000559 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000560 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000561 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000562 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000563 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000564 if (TargetDecl->hasAttr<MallocAttr>())
565 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000566 }
567
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000568 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000569 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000570 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000571 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000572 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000573 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000574
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000575 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000576 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000577 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000578 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000579 case ABIArgInfo::Extend:
580 if (RetTy->isSignedIntegerType()) {
581 RetAttrs |= llvm::Attribute::SExt;
582 } else if (RetTy->isUnsignedIntegerType()) {
583 RetAttrs |= llvm::Attribute::ZExt;
584 }
585 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000586 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000587 break;
588
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000589 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000590 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000591 llvm::Attribute::StructRet |
592 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000593 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000594 // sret disables readnone and readonly
595 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
596 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000597 break;
598
Daniel Dunbar11434922009-01-26 21:26:08 +0000599 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000600 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000601 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000602
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000603 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000604 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000605 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000606
Devang Patela2c69122008-09-26 22:53:57 +0000607 if (RetAttrs)
608 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000609
610 // FIXME: we need to honour command line settings also...
611 // FIXME: RegParm should be reduced in case of nested functions and/or global
612 // register variable.
613 signed RegParm = 0;
614 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000615 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000616 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000617 RegParm = RegParmAttr->getNumParams();
618
619 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000620 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000621 ie = FI.arg_end(); it != ie; ++it) {
622 QualType ParamType = it->type;
623 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000624 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000625
Nuno Lopes079b4952009-12-07 18:30:06 +0000626 if (ParamType.isRestrictQualified())
627 Attributes |= llvm::Attribute::NoAlias;
628
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000629 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000630 case ABIArgInfo::Coerce:
631 break;
632
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000633 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000634 if (AI.getIndirectByVal())
635 Attributes |= llvm::Attribute::ByVal;
636
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000637 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000638 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000639 // byval disables readnone and readonly.
640 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
641 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000642 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000643
644 case ABIArgInfo::Extend:
645 if (ParamType->isSignedIntegerType()) {
646 Attributes |= llvm::Attribute::SExt;
647 } else if (ParamType->isUnsignedIntegerType()) {
648 Attributes |= llvm::Attribute::ZExt;
649 }
650 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000651 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000652 if (RegParm > 0 &&
653 (ParamType->isIntegerType() || ParamType->isPointerType())) {
654 RegParm -=
655 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
656 if (RegParm >= 0)
657 Attributes |= llvm::Attribute::InReg;
658 }
659 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000660 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000661
Daniel Dunbar11434922009-01-26 21:26:08 +0000662 case ABIArgInfo::Ignore:
663 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000664 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000665
Daniel Dunbar56273772008-09-17 00:51:38 +0000666 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000667 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000668 // FIXME: This is rather inefficient. Do we ever actually need to do
669 // anything here? The result should be just reconstructed on the other
670 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000671 getTypes().GetExpandedTypes(ParamType, Tys);
672 Index += Tys.size();
673 continue;
674 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Devang Patel761d7f72008-09-25 21:02:23 +0000677 if (Attributes)
678 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000679 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000680 }
Devang Patela2c69122008-09-26 22:53:57 +0000681 if (FuncAttrs)
682 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000683}
684
Daniel Dunbar88b53962009-02-02 22:03:45 +0000685void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
686 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000687 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000688 // If this is an implicit-return-zero function, go ahead and
689 // initialize the return value. TODO: it might be nice to have
690 // a more general mechanism for this that didn't require synthesized
691 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000692 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000693 if (FD->hasImplicitReturnZero()) {
694 QualType RetTy = FD->getResultType().getUnqualifiedType();
695 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000696 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000697 Builder.CreateStore(Zero, ReturnValue);
698 }
699 }
700
Mike Stumpf5408fe2009-05-16 07:57:57 +0000701 // FIXME: We no longer need the types from FunctionArgList; lift up and
702 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000703
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000704 // Emit allocs for param decls. Give the LLVM Argument nodes names.
705 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000707 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000708 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000709 AI->setName("agg.result");
710 ++AI;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000713 assert(FI.arg_size() == Args.size() &&
714 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000715 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000716 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000717 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000718 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000719 QualType Ty = info_it->type;
720 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000721
722 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000723 case ABIArgInfo::Indirect: {
724 llvm::Value* V = AI;
725 if (hasAggregateLLVMType(Ty)) {
726 // Do nothing, aggregates and complex variables are accessed by
727 // reference.
728 } else {
729 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000730 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000731 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
732 // This must be a promotion, for something like
733 // "void a(x) short x; {..."
734 V = EmitScalarConversion(V, Ty, Arg->getType());
735 }
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000738 break;
739 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000740
741 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000742 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000743 assert(AI != Fn->arg_end() && "Argument mismatch!");
744 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000745 if (hasAggregateLLVMType(Ty)) {
746 // Create a temporary alloca to hold the argument; the rest of
747 // codegen expects to access aggregates & complex values by
748 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000749 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000750 Builder.CreateStore(AI, V);
751 } else {
752 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
753 // This must be a promotion, for something like
754 // "void a(x) short x; {..."
755 V = EmitScalarConversion(V, Ty, Arg->getType());
756 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000757 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000758 EmitParmDecl(*Arg, V);
759 break;
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Daniel Dunbar56273772008-09-17 00:51:38 +0000762 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000763 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000764 // we need to create a temporary and reconstruct it from the
765 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000766 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000767 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000768 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000769 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000770 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000771
Daniel Dunbar56273772008-09-17 00:51:38 +0000772 // Name the arguments used in expansion and increment AI.
773 unsigned Index = 0;
774 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000775 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000776 continue;
777 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000778
779 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000780 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000781 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000782 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000783 } else {
784 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000787 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000788 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000789
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000790 case ABIArgInfo::Coerce: {
791 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000792 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
793 // result in a new alloca anyway, so we could just store into that
794 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000795 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000796 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000797 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000798 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000799 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000800 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
801 // This must be a promotion, for something like
802 // "void a(x) short x; {..."
803 V = EmitScalarConversion(V, Ty, Arg->getType());
804 }
805 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000806 EmitParmDecl(*Arg, V);
807 break;
808 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000809 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000810
811 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000812 }
813 assert(AI == Fn->arg_end() && "Argument mismatch!");
814}
815
Daniel Dunbar88b53962009-02-02 22:03:45 +0000816void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000817 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000818 llvm::Value *RV = 0;
819
820 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000821 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000822 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000823 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000824
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000825 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000826 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000827 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000828 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
829 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
830 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000831 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000832 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000833 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000834 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000835 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000836 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000837
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000838 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000839 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000840 // The internal return value temp always will have
841 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000842 RV = Builder.CreateLoad(ReturnValue);
843 break;
844
Daniel Dunbar11434922009-01-26 21:26:08 +0000845 case ABIArgInfo::Ignore:
846 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000848 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000849 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000850 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000851
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000852 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000853 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000854 }
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000857 if (RV) {
858 Builder.CreateRet(RV);
859 } else {
860 Builder.CreateRetVoid();
861 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000862}
863
Anders Carlsson0139bb92009-04-08 20:47:54 +0000864RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000865 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000866 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Anders Carlsson0139bb92009-04-08 20:47:54 +0000868 return EmitAnyExprToTemp(E);
869}
870
Daniel Dunbar88b53962009-02-02 22:03:45 +0000871RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000872 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000873 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000874 const CallArgList &CallArgs,
875 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000876 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000877 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000878
879 // Handle struct-return functions by passing a pointer to the
880 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000881 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000882 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000883
884
Chris Lattner5db7ae52009-06-13 00:26:38 +0000885 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000886 // alloca to hold the result, unless one is given to us.
887 if (CGM.ReturnTypeUsesSret(CallInfo)) {
888 llvm::Value *Value = ReturnValue.getValue();
889 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000890 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +0000891 Args.push_back(Value);
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000894 assert(CallInfo.arg_size() == CallArgs.size() &&
895 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000896 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000897 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000898 I != E; ++I, ++info_it) {
899 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000900 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000901
902 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000903 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000904 if (RV.isScalar() || RV.isComplex()) {
905 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000906 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000907 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000908 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000909 else
Mike Stump1eb44332009-09-09 15:08:12 +0000910 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000911 } else {
912 Args.push_back(RV.getAggregateAddr());
913 }
914 break;
915
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000916 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000917 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000918 if (RV.isScalar()) {
919 Args.push_back(RV.getScalarVal());
920 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000921 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
922 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
923 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
924 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000925 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000926 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000927 }
928 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Daniel Dunbar11434922009-01-26 21:26:08 +0000930 case ABIArgInfo::Ignore:
931 break;
932
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000933 case ABIArgInfo::Coerce: {
934 // FIXME: Avoid the conversion through memory if possible.
935 llvm::Value *SrcPtr;
936 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000937 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000938 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000939 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000940 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000941 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000942 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000943 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000944 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000945 *this));
946 break;
947 }
948
Daniel Dunbar56273772008-09-17 00:51:38 +0000949 case ABIArgInfo::Expand:
950 ExpandTypeToArgs(I->second, RV, Args);
951 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000952 }
953 }
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Chris Lattner5db7ae52009-06-13 00:26:38 +0000955 // If the callee is a bitcast of a function to a varargs pointer to function
956 // type, check to see if we can remove the bitcast. This handles some cases
957 // with unprototyped functions.
958 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
959 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
960 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
961 const llvm::FunctionType *CurFT =
962 cast<llvm::FunctionType>(CurPT->getElementType());
963 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner5db7ae52009-06-13 00:26:38 +0000965 if (CE->getOpcode() == llvm::Instruction::BitCast &&
966 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000967 ActualFT->getNumParams() == CurFT->getNumParams() &&
968 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000969 bool ArgsMatch = true;
970 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
971 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
972 ArgsMatch = false;
973 break;
974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner5db7ae52009-06-13 00:26:38 +0000976 // Strip the cast if we can get away with it. This is a nice cleanup,
977 // but also allows us to inline the function at -O0 if it is marked
978 // always_inline.
979 if (ArgsMatch)
980 Callee = CalleeF;
981 }
982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000984
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000985 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000986 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000987 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000988 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000989 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
990 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Daniel Dunbard14151d2009-03-02 04:32:35 +0000992 llvm::CallSite CS;
993 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000994 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000995 } else {
996 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000997 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000998 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000999 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001000 }
1001
Daniel Dunbard14151d2009-03-02 04:32:35 +00001002 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001003 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001004
1005 // If the call doesn't return, finish the basic block and clear the
1006 // insertion point; this allows the rest of IRgen to discard
1007 // unreachable code.
1008 if (CS.doesNotReturn()) {
1009 Builder.CreateUnreachable();
1010 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Mike Stumpf5408fe2009-05-16 07:57:57 +00001012 // FIXME: For now, emit a dummy basic block because expr emitters in
1013 // generally are not ready to handle emitting expressions at unreachable
1014 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001015 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Daniel Dunbard14151d2009-03-02 04:32:35 +00001017 // Return a reasonable RValue.
1018 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001019 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001020
1021 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001022 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001023 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001024
1025 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001026 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001027 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001028 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001029 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001030 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001031 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001032
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001033 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001034 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001035 if (RetTy->isAnyComplexType()) {
1036 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1037 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1038 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001039 }
1040 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
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, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001046 DestIsVolatile = false;
1047 }
1048 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1049 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001050 }
1051 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001052
Daniel Dunbar11434922009-01-26 21:26:08 +00001053 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001054 // If we are ignoring an argument that had a result, make sure to
1055 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001056 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001057
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001058 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001059 llvm::Value *DestPtr = ReturnValue.getValue();
1060 bool DestIsVolatile = ReturnValue.isVolatile();
1061
1062 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001063 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001064 DestIsVolatile = false;
1065 }
1066
1067 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001068 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001069 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001070 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001071 return RValue::getAggregate(DestPtr);
1072 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001073 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001074
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001075 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001076 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001077 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001078
1079 assert(0 && "Unhandled ABIArgInfo::Kind");
1080 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001081}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001082
1083/* VarArg handling */
1084
1085llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1086 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1087}