blob: 8b5c3a0f6c46de0d6d20d8b62b8aceabff6b8c2d [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>(),
Rafael Espindola264ba482010-03-30 20:24:48 +000070 FTNP->getExtInfo());
John McCall0b0ef0a2010-02-24 07:14:12 +000071}
72
73/// \param Args - contains any initial parameters besides those
74/// in the formal type
75static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000076 llvm::SmallVectorImpl<CanQualType> &ArgTys,
77 CanQual<FunctionProtoType> FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000078 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000079 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000080 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000081 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
82 return CGT.getFunctionInfo(ResTy, ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +000083 FTP->getExtInfo());
John McCall0b0ef0a2010-02-24 07:14:12 +000084}
85
86const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000087CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
88 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +000089 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000090}
91
John McCall04a67a62010-02-05 21:31:56 +000092static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000093 // Set the appropriate calling convention for the Function.
94 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000095 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000096
97 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000098 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000099
John McCall04a67a62010-02-05 21:31:56 +0000100 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000101}
102
Anders Carlsson375c31c2009-10-03 19:43:08 +0000103const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
104 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000105 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000106
Anders Carlsson375c31c2009-10-03 19:43:08 +0000107 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000108 ArgTys.push_back(GetThisType(Context, RD));
109
110 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000111 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000112}
113
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000114const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000115 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000116
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000117 // Add the 'this' pointer unless this is a static method.
118 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000119 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000120
John McCall0b0ef0a2010-02-24 07:14:12 +0000121 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000122}
123
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000124const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
125 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000126 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000127
128 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000129 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000130
131 // Check if we need to add a VTT parameter (which has type void **).
132 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
133 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000134
135 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000136}
137
138const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
139 CXXDtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000140 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000141
142 // Add the 'this' pointer.
John McCallead608a2010-02-26 00:48:12 +0000143 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000144
145 // Check if we need to add a VTT parameter (which has type void **).
146 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
147 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000148
149 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000150}
151
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000152const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000153 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000154 if (MD->isInstance())
155 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
John McCallead608a2010-02-26 00:48:12 +0000157 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
158 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000159 if (isa<FunctionNoProtoType>(FTy))
John McCallead608a2010-02-26 00:48:12 +0000160 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
161 assert(isa<FunctionProtoType>(FTy));
162 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000163}
164
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000165const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000166 llvm::SmallVector<CanQualType, 16> ArgTys;
167 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
168 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000169 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000170 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000171 e = MD->param_end(); i != e; ++i) {
172 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
173 }
174 return getFunctionInfo(GetReturnType(MD->getResultType()),
175 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000176 FunctionType::ExtInfo(
177 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000178 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000179 getCallingConventionForDecl(MD)));
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,
Rafael Espindola264ba482010-03-30 20:24:48 +0000197 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000198 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000199 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000200 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000201 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000202 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000203 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000204}
205
Mike Stump1eb44332009-09-09 15:08:12 +0000206const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000207 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000208 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000209 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000210 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000211 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000212 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000213 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000214 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000215}
216
John McCallead608a2010-02-26 00:48:12 +0000217const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
218 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000219 const FunctionType::ExtInfo &Info) {
John McCallead608a2010-02-26 00:48:12 +0000220#ifndef NDEBUG
221 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
222 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
223 assert(I->isCanonicalAsParam());
224#endif
225
Rafael Espindola425ef722010-03-30 22:15:11 +0000226 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000227
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000228 // Lookup or create unique function info.
229 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000230 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000231 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000232
233 void *InsertPos = 0;
234 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
235 if (FI)
236 return *FI;
237
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000238 // Construct the function info.
Rafael Espindola425ef722010-03-30 22:15:11 +0000239 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000240 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000241
242 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000243 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000244
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000245 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000246}
247
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000248CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000249 bool _NoReturn,
Rafael Espindola425ef722010-03-30 22:15:11 +0000250 unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000251 CanQualType ResTy,
252 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000253 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000254 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000255 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000256{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000257 NumArgs = ArgTys.size();
258 Args = new ArgInfo[1 + NumArgs];
259 Args[0].type = ResTy;
260 for (unsigned i = 0; i < NumArgs; ++i)
261 Args[1 + i].type = ArgTys[i];
262}
263
264/***/
265
Mike Stump1eb44332009-09-09 15:08:12 +0000266void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000267 std::vector<const llvm::Type*> &ArgTys) {
268 const RecordType *RT = Ty->getAsStructureType();
269 assert(RT && "Can only expand structure types.");
270 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000271 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000272 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000274 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
275 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000276 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000277 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000278 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Daniel Dunbar56273772008-09-17 00:51:38 +0000280 QualType FT = FD->getType();
281 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
282 GetExpandedTypes(FT, ArgTys);
283 } else {
284 ArgTys.push_back(ConvertType(FT));
285 }
286 }
287}
288
Mike Stump1eb44332009-09-09 15:08:12 +0000289llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000290CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
291 llvm::Function::arg_iterator AI) {
292 const RecordType *RT = Ty->getAsStructureType();
293 assert(RT && "Can only expand structure types.");
294
295 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000296 assert(LV.isSimple() &&
297 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000298 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000299 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
300 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000301 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000302 QualType FT = FD->getType();
303
304 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000305 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000306 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
307 AI = ExpandTypeFromArgs(FT, LV, AI);
308 } else {
309 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
310 ++AI;
311 }
312 }
313
314 return AI;
315}
316
Mike Stump1eb44332009-09-09 15:08:12 +0000317void
318CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000319 llvm::SmallVector<llvm::Value*, 16> &Args) {
320 const RecordType *RT = Ty->getAsStructureType();
321 assert(RT && "Can only expand structure types.");
322
323 RecordDecl *RD = RT->getDecl();
324 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
325 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000326 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
327 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000328 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000329 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Daniel Dunbar56273772008-09-17 00:51:38 +0000331 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000332 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000333 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
334 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
335 } else {
336 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000337 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000338 "Unexpected non-scalar rvalue during struct expansion.");
339 Args.push_back(RV.getScalarVal());
340 }
341 }
342}
343
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000344/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
345/// a pointer to an object of type \arg Ty.
346///
347/// This safely handles the case when the src type is smaller than the
348/// destination type; in this situation the values of bits which not
349/// present in the src are undefined.
350static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
351 const llvm::Type *Ty,
352 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000353 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000354 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000355 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
356 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000357
Daniel Dunbarb225be42009-02-03 05:59:18 +0000358 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000359 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000360 // Generally SrcSize is never greater than DstSize, since this means we are
361 // losing bits. However, this can happen in cases where the structure has
362 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000363 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000364 // FIXME: Assert that we aren't truncating non-padding bits when have access
365 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000366 llvm::Value *Casted =
367 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000368 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
369 // FIXME: Use better alignment / avoid requiring aligned load.
370 Load->setAlignment(1);
371 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000372 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000373 // Otherwise do coercion through memory. This is stupid, but
374 // simple.
375 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000376 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000377 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000378 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000379 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
380 // FIXME: Use better alignment / avoid requiring aligned store.
381 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000382 return CGF.Builder.CreateLoad(Tmp);
383 }
384}
385
386/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
387/// where the source and destination may have different types.
388///
389/// This safely handles the case when the src type is larger than the
390/// destination type; the upper bits of the src will be lost.
391static void CreateCoercedStore(llvm::Value *Src,
392 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000393 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000394 CodeGenFunction &CGF) {
395 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000396 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000397 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
398
Duncan Sands9408c452009-05-09 07:08:47 +0000399 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
400 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000401
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000402 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000403 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000404 llvm::Value *Casted =
405 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000406 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000407 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000408 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000409 // Otherwise do coercion through memory. This is stupid, but
410 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000411
412 // Generally SrcSize is never greater than DstSize, since this means we are
413 // losing bits. However, this can happen in cases where the structure has
414 // additional padding, for example due to a user specified alignment.
415 //
416 // FIXME: Assert that we aren't truncating non-padding bits when have access
417 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000418 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
419 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000420 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000421 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000422 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
423 // FIXME: Use better alignment / avoid requiring aligned load.
424 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000425 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000426 }
427}
428
Daniel Dunbar56273772008-09-17 00:51:38 +0000429/***/
430
Daniel Dunbar88b53962009-02-02 22:03:45 +0000431bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000432 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000433}
434
John McCallc0bf4622010-02-23 00:48:20 +0000435const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
436 const CGFunctionInfo &FI = getFunctionInfo(GD);
437
438 // For definition purposes, don't consider a K&R function variadic.
439 bool Variadic = false;
440 if (const FunctionProtoType *FPT =
441 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
442 Variadic = FPT->isVariadic();
443
444 return GetFunctionType(FI, Variadic);
445}
446
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000447const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000448CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000449 std::vector<const llvm::Type*> ArgTys;
450
451 const llvm::Type *ResultType = 0;
452
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000453 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000454 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000455 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000456 case ABIArgInfo::Expand:
457 assert(0 && "Invalid ABI kind for return argument");
458
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000459 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000460 case ABIArgInfo::Direct:
461 ResultType = ConvertType(RetTy);
462 break;
463
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000464 case ABIArgInfo::Indirect: {
465 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000466 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000467 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000468 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
469 break;
470 }
471
Daniel Dunbar11434922009-01-26 21:26:08 +0000472 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000473 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000474 break;
475
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000476 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000477 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000478 break;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
481 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000482 ie = FI.arg_end(); it != ie; ++it) {
483 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000485 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000486 case ABIArgInfo::Ignore:
487 break;
488
Daniel Dunbar56273772008-09-17 00:51:38 +0000489 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000490 ArgTys.push_back(AI.getCoerceToType());
491 break;
492
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000493 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000494 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000495 const llvm::Type *LTy = ConvertTypeForMem(it->type);
496 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000497 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000498 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000499
500 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000501 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000502 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000503 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000505 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000506 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000507 break;
508 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000509 }
510
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000511 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000512}
513
Anders Carlssonecf282b2009-11-24 05:08:52 +0000514static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
515 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
516 if (!TT->getDecl()->isDefinition())
517 return true;
518 }
519
520 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
521 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
522 if (!TT->getDecl()->isDefinition())
523 return true;
524 }
525 }
526
527 return false;
528}
529
530const llvm::Type *
Anders Carlsson046c2942010-04-17 20:15:18 +0000531CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlssonecf282b2009-11-24 05:08:52 +0000532 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
533
534 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
535 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
536
537 return llvm::OpaqueType::get(getLLVMContext());
538}
539
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000540void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000541 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000542 AttributeListType &PAL,
543 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000544 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000545 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000546
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000547 CallingConv = FI.getEffectiveCallingConvention();
548
John McCall04a67a62010-02-05 21:31:56 +0000549 if (FI.isNoReturn())
550 FuncAttrs |= llvm::Attribute::NoReturn;
551
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000552 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000553 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000554 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000555 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000556 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000557 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000558 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000559 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000560 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000561 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000562 if (TargetDecl->hasAttr<MallocAttr>())
563 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000564 }
565
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000566 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000567 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000568 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000569 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000570 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000571 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000572
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000573 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000574 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000575 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000576 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000577 case ABIArgInfo::Extend:
578 if (RetTy->isSignedIntegerType()) {
579 RetAttrs |= llvm::Attribute::SExt;
580 } else if (RetTy->isUnsignedIntegerType()) {
581 RetAttrs |= llvm::Attribute::ZExt;
582 }
583 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000584 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000585 break;
586
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000587 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000588 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000589 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000590 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000591 // sret disables readnone and readonly
592 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
593 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000594 break;
595
Daniel Dunbar11434922009-01-26 21:26:08 +0000596 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000597 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000598 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000599
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000600 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000601 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000602 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000603
Devang Patela2c69122008-09-26 22:53:57 +0000604 if (RetAttrs)
605 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000606
607 // FIXME: we need to honour command line settings also...
608 // FIXME: RegParm should be reduced in case of nested functions and/or global
609 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000610 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000611
612 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000613 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000614 ie = FI.arg_end(); it != ie; ++it) {
615 QualType ParamType = it->type;
616 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000617 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000618
John McCalld8e10d22010-03-27 00:47:27 +0000619 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
620 // have the corresponding parameter variable. It doesn't make
621 // sense to do it here because parameters are so fucked up.
Nuno Lopes079b4952009-12-07 18:30:06 +0000622
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000623 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000624 case ABIArgInfo::Coerce:
625 break;
626
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000627 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000628 if (AI.getIndirectByVal())
629 Attributes |= llvm::Attribute::ByVal;
630
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000631 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000632 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000633 // byval disables readnone and readonly.
634 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
635 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000636 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000637
638 case ABIArgInfo::Extend:
639 if (ParamType->isSignedIntegerType()) {
640 Attributes |= llvm::Attribute::SExt;
641 } else if (ParamType->isUnsignedIntegerType()) {
642 Attributes |= llvm::Attribute::ZExt;
643 }
644 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000645 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000646 if (RegParm > 0 &&
647 (ParamType->isIntegerType() || ParamType->isPointerType())) {
648 RegParm -=
649 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
650 if (RegParm >= 0)
651 Attributes |= llvm::Attribute::InReg;
652 }
653 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000654 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000655
Daniel Dunbar11434922009-01-26 21:26:08 +0000656 case ABIArgInfo::Ignore:
657 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000658 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000659
Daniel Dunbar56273772008-09-17 00:51:38 +0000660 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000661 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000662 // FIXME: This is rather inefficient. Do we ever actually need to do
663 // anything here? The result should be just reconstructed on the other
664 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000665 getTypes().GetExpandedTypes(ParamType, Tys);
666 Index += Tys.size();
667 continue;
668 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Devang Patel761d7f72008-09-25 21:02:23 +0000671 if (Attributes)
672 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000673 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000674 }
Devang Patela2c69122008-09-26 22:53:57 +0000675 if (FuncAttrs)
676 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000677}
678
Daniel Dunbar88b53962009-02-02 22:03:45 +0000679void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
680 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000681 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000682 // If this is an implicit-return-zero function, go ahead and
683 // initialize the return value. TODO: it might be nice to have
684 // a more general mechanism for this that didn't require synthesized
685 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000686 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000687 if (FD->hasImplicitReturnZero()) {
688 QualType RetTy = FD->getResultType().getUnqualifiedType();
689 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000690 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000691 Builder.CreateStore(Zero, ReturnValue);
692 }
693 }
694
Mike Stumpf5408fe2009-05-16 07:57:57 +0000695 // FIXME: We no longer need the types from FunctionArgList; lift up and
696 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000697
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000698 // Emit allocs for param decls. Give the LLVM Argument nodes names.
699 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000701 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000702 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000703 AI->setName("agg.result");
704 ++AI;
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000707 assert(FI.arg_size() == Args.size() &&
708 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000709 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000710 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000711 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000712 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000713 QualType Ty = info_it->type;
714 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000715
716 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000717 case ABIArgInfo::Indirect: {
718 llvm::Value* V = AI;
719 if (hasAggregateLLVMType(Ty)) {
720 // Do nothing, aggregates and complex variables are accessed by
721 // reference.
722 } else {
723 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000724 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000725 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
726 // This must be a promotion, for something like
727 // "void a(x) short x; {..."
728 V = EmitScalarConversion(V, Ty, Arg->getType());
729 }
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000732 break;
733 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000734
735 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000736 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000737 assert(AI != Fn->arg_end() && "Argument mismatch!");
738 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000739 if (hasAggregateLLVMType(Ty)) {
740 // Create a temporary alloca to hold the argument; the rest of
741 // codegen expects to access aggregates & complex values by
742 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000743 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000744 Builder.CreateStore(AI, V);
745 } else {
John McCalld8e10d22010-03-27 00:47:27 +0000746 if (Arg->getType().isRestrictQualified())
747 AI->addAttr(llvm::Attribute::NoAlias);
748
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000749 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
750 // This must be a promotion, for something like
751 // "void a(x) short x; {..."
752 V = EmitScalarConversion(V, Ty, Arg->getType());
753 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000754 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000755 EmitParmDecl(*Arg, V);
756 break;
757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Daniel Dunbar56273772008-09-17 00:51:38 +0000759 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000760 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000761 // we need to create a temporary and reconstruct it from the
762 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000763 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000764 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000765 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000766 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000767 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000768
Daniel Dunbar56273772008-09-17 00:51:38 +0000769 // Name the arguments used in expansion and increment AI.
770 unsigned Index = 0;
771 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000772 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000773 continue;
774 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000775
776 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000777 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000778 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000779 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000780 } else {
781 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000784 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000785 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000786
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000787 case ABIArgInfo::Coerce: {
788 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000789 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
790 // result in a new alloca anyway, so we could just store into that
791 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000792 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000793 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000794 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000795 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000796 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000797 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
798 // This must be a promotion, for something like
799 // "void a(x) short x; {..."
800 V = EmitScalarConversion(V, Ty, Arg->getType());
801 }
802 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000803 EmitParmDecl(*Arg, V);
804 break;
805 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000806 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000807
808 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000809 }
810 assert(AI == Fn->arg_end() && "Argument mismatch!");
811}
812
Daniel Dunbar88b53962009-02-02 22:03:45 +0000813void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000814 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000815 llvm::Value *RV = 0;
816
817 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000818 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000819 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000820 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000821
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000822 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000823 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000824 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000825 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
826 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
827 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000828 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000829 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000830 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000831 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000832 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000833 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000834
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000835 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000836 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000837 // The internal return value temp always will have
838 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000839 RV = Builder.CreateLoad(ReturnValue);
840 break;
841
Daniel Dunbar11434922009-01-26 21:26:08 +0000842 case ABIArgInfo::Ignore:
843 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000845 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000846 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000847 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000848
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000849 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000850 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000851 }
852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000854 if (RV) {
855 Builder.CreateRet(RV);
856 } else {
857 Builder.CreateRetVoid();
858 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000859}
860
Anders Carlsson0139bb92009-04-08 20:47:54 +0000861RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000862 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000863 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Anders Carlsson0139bb92009-04-08 20:47:54 +0000865 return EmitAnyExprToTemp(E);
866}
867
Daniel Dunbar88b53962009-02-02 22:03:45 +0000868RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000869 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000870 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000871 const CallArgList &CallArgs,
872 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000873 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000874 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000875
876 // Handle struct-return functions by passing a pointer to the
877 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000878 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000879 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000880
881
Chris Lattner5db7ae52009-06-13 00:26:38 +0000882 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000883 // alloca to hold the result, unless one is given to us.
884 if (CGM.ReturnTypeUsesSret(CallInfo)) {
885 llvm::Value *Value = ReturnValue.getValue();
886 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000887 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +0000888 Args.push_back(Value);
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000891 assert(CallInfo.arg_size() == CallArgs.size() &&
892 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000893 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000894 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000895 I != E; ++I, ++info_it) {
896 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000897 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000898
899 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000900 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000901 if (RV.isScalar() || RV.isComplex()) {
902 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000903 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000904 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000905 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000906 else
Mike Stump1eb44332009-09-09 15:08:12 +0000907 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000908 } else {
909 Args.push_back(RV.getAggregateAddr());
910 }
911 break;
912
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000913 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000914 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000915 if (RV.isScalar()) {
916 Args.push_back(RV.getScalarVal());
917 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000918 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
919 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
920 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
921 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000922 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000923 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000924 }
925 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Daniel Dunbar11434922009-01-26 21:26:08 +0000927 case ABIArgInfo::Ignore:
928 break;
929
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000930 case ABIArgInfo::Coerce: {
931 // FIXME: Avoid the conversion through memory if possible.
932 llvm::Value *SrcPtr;
933 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000934 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000935 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000936 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000937 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000938 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000939 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000940 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000941 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000942 *this));
943 break;
944 }
945
Daniel Dunbar56273772008-09-17 00:51:38 +0000946 case ABIArgInfo::Expand:
947 ExpandTypeToArgs(I->second, RV, Args);
948 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000949 }
950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Chris Lattner5db7ae52009-06-13 00:26:38 +0000952 // If the callee is a bitcast of a function to a varargs pointer to function
953 // type, check to see if we can remove the bitcast. This handles some cases
954 // with unprototyped functions.
955 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
956 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
957 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
958 const llvm::FunctionType *CurFT =
959 cast<llvm::FunctionType>(CurPT->getElementType());
960 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattner5db7ae52009-06-13 00:26:38 +0000962 if (CE->getOpcode() == llvm::Instruction::BitCast &&
963 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000964 ActualFT->getNumParams() == CurFT->getNumParams() &&
965 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000966 bool ArgsMatch = true;
967 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
968 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
969 ArgsMatch = false;
970 break;
971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattner5db7ae52009-06-13 00:26:38 +0000973 // Strip the cast if we can get away with it. This is a nice cleanup,
974 // but also allows us to inline the function at -O0 if it is marked
975 // always_inline.
976 if (ArgsMatch)
977 Callee = CalleeF;
978 }
979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000981
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000982 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000983 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000984 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000985 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000986 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
987 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Daniel Dunbard14151d2009-03-02 04:32:35 +0000989 llvm::CallSite CS;
990 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000991 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000992 } else {
993 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000994 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000995 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000996 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000997 }
998
Daniel Dunbard14151d2009-03-02 04:32:35 +0000999 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001000 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001001
1002 // If the call doesn't return, finish the basic block and clear the
1003 // insertion point; this allows the rest of IRgen to discard
1004 // unreachable code.
1005 if (CS.doesNotReturn()) {
1006 Builder.CreateUnreachable();
1007 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Mike Stumpf5408fe2009-05-16 07:57:57 +00001009 // FIXME: For now, emit a dummy basic block because expr emitters in
1010 // generally are not ready to handle emitting expressions at unreachable
1011 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001012 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Daniel Dunbard14151d2009-03-02 04:32:35 +00001014 // Return a reasonable RValue.
1015 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001016 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001017
1018 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001019 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001020 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001021
1022 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001023 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001024 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001025 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001026 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001027 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001028 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001029
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001030 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001031 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001032 if (RetTy->isAnyComplexType()) {
1033 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1034 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1035 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001036 }
1037 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001038 llvm::Value *DestPtr = ReturnValue.getValue();
1039 bool DestIsVolatile = ReturnValue.isVolatile();
1040
1041 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001042 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001043 DestIsVolatile = false;
1044 }
1045 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1046 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001047 }
1048 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001049
Daniel Dunbar11434922009-01-26 21:26:08 +00001050 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001051 // If we are ignoring an argument that had a result, make sure to
1052 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001053 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001054
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001055 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001056 llvm::Value *DestPtr = ReturnValue.getValue();
1057 bool DestIsVolatile = ReturnValue.isVolatile();
1058
1059 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001060 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001061 DestIsVolatile = false;
1062 }
1063
1064 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001065 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001066 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001067 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001068 return RValue::getAggregate(DestPtr);
1069 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001070 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001071
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001072 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001073 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001074 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001075
1076 assert(0 && "Unhandled ABIArgInfo::Kind");
1077 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001078}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001079
1080/* VarArg handling */
1081
1082llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1083 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1084}