blob: cb1ecc1aa616e1a1d10e3c82046e3dadf8b80f62 [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 *
531CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
532 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,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000589 llvm::Attribute::StructRet |
590 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000591 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000592 // sret disables readnone and readonly
593 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
594 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000595 break;
596
Daniel Dunbar11434922009-01-26 21:26:08 +0000597 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000598 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000599 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000600
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000601 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000602 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000603 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000604
Devang Patela2c69122008-09-26 22:53:57 +0000605 if (RetAttrs)
606 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000607
608 // FIXME: we need to honour command line settings also...
609 // FIXME: RegParm should be reduced in case of nested functions and/or global
610 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000611 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000612
613 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000614 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000615 ie = FI.arg_end(); it != ie; ++it) {
616 QualType ParamType = it->type;
617 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000618 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000619
John McCalld8e10d22010-03-27 00:47:27 +0000620 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
621 // have the corresponding parameter variable. It doesn't make
622 // sense to do it here because parameters are so fucked up.
Nuno Lopes079b4952009-12-07 18:30:06 +0000623
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000624 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000625 case ABIArgInfo::Coerce:
626 break;
627
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000628 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000629 if (AI.getIndirectByVal())
630 Attributes |= llvm::Attribute::ByVal;
631
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000632 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000633 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000634 // byval disables readnone and readonly.
635 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
636 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000637 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000638
639 case ABIArgInfo::Extend:
640 if (ParamType->isSignedIntegerType()) {
641 Attributes |= llvm::Attribute::SExt;
642 } else if (ParamType->isUnsignedIntegerType()) {
643 Attributes |= llvm::Attribute::ZExt;
644 }
645 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000646 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000647 if (RegParm > 0 &&
648 (ParamType->isIntegerType() || ParamType->isPointerType())) {
649 RegParm -=
650 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
651 if (RegParm >= 0)
652 Attributes |= llvm::Attribute::InReg;
653 }
654 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000655 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000656
Daniel Dunbar11434922009-01-26 21:26:08 +0000657 case ABIArgInfo::Ignore:
658 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000659 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000660
Daniel Dunbar56273772008-09-17 00:51:38 +0000661 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000662 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000663 // FIXME: This is rather inefficient. Do we ever actually need to do
664 // anything here? The result should be just reconstructed on the other
665 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000666 getTypes().GetExpandedTypes(ParamType, Tys);
667 Index += Tys.size();
668 continue;
669 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Devang Patel761d7f72008-09-25 21:02:23 +0000672 if (Attributes)
673 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000674 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000675 }
Devang Patela2c69122008-09-26 22:53:57 +0000676 if (FuncAttrs)
677 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000678}
679
Daniel Dunbar88b53962009-02-02 22:03:45 +0000680void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
681 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000682 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000683 // If this is an implicit-return-zero function, go ahead and
684 // initialize the return value. TODO: it might be nice to have
685 // a more general mechanism for this that didn't require synthesized
686 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000687 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000688 if (FD->hasImplicitReturnZero()) {
689 QualType RetTy = FD->getResultType().getUnqualifiedType();
690 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000691 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000692 Builder.CreateStore(Zero, ReturnValue);
693 }
694 }
695
Mike Stumpf5408fe2009-05-16 07:57:57 +0000696 // FIXME: We no longer need the types from FunctionArgList; lift up and
697 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000698
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000699 // Emit allocs for param decls. Give the LLVM Argument nodes names.
700 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000702 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000703 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000704 AI->setName("agg.result");
705 ++AI;
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000708 assert(FI.arg_size() == Args.size() &&
709 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000710 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000711 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000712 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000713 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000714 QualType Ty = info_it->type;
715 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000716
717 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000718 case ABIArgInfo::Indirect: {
719 llvm::Value* V = AI;
720 if (hasAggregateLLVMType(Ty)) {
721 // Do nothing, aggregates and complex variables are accessed by
722 // reference.
723 } else {
724 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000725 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000726 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
727 // This must be a promotion, for something like
728 // "void a(x) short x; {..."
729 V = EmitScalarConversion(V, Ty, Arg->getType());
730 }
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000733 break;
734 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000735
736 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000737 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000738 assert(AI != Fn->arg_end() && "Argument mismatch!");
739 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000740 if (hasAggregateLLVMType(Ty)) {
741 // Create a temporary alloca to hold the argument; the rest of
742 // codegen expects to access aggregates & complex values by
743 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000744 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000745 Builder.CreateStore(AI, V);
746 } else {
John McCalld8e10d22010-03-27 00:47:27 +0000747 if (Arg->getType().isRestrictQualified())
748 AI->addAttr(llvm::Attribute::NoAlias);
749
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000750 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
751 // This must be a promotion, for something like
752 // "void a(x) short x; {..."
753 V = EmitScalarConversion(V, Ty, Arg->getType());
754 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000755 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000756 EmitParmDecl(*Arg, V);
757 break;
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Daniel Dunbar56273772008-09-17 00:51:38 +0000760 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000761 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000762 // we need to create a temporary and reconstruct it from the
763 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000764 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000765 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000766 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000767 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000768 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000769
Daniel Dunbar56273772008-09-17 00:51:38 +0000770 // Name the arguments used in expansion and increment AI.
771 unsigned Index = 0;
772 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000773 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000774 continue;
775 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000776
777 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000778 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000779 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000780 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000781 } else {
782 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000785 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000786 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000787
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000788 case ABIArgInfo::Coerce: {
789 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000790 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
791 // result in a new alloca anyway, so we could just store into that
792 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000793 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000794 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000795 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000796 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000797 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000798 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
799 // This must be a promotion, for something like
800 // "void a(x) short x; {..."
801 V = EmitScalarConversion(V, Ty, Arg->getType());
802 }
803 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000804 EmitParmDecl(*Arg, V);
805 break;
806 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000807 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000808
809 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000810 }
811 assert(AI == Fn->arg_end() && "Argument mismatch!");
812}
813
Daniel Dunbar88b53962009-02-02 22:03:45 +0000814void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000815 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000816 llvm::Value *RV = 0;
817
818 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000819 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000820 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000821 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000822
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000823 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000824 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000825 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000826 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
827 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
828 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000829 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000830 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000831 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000832 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000833 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000834 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000835
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000836 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000837 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000838 // The internal return value temp always will have
839 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000840 RV = Builder.CreateLoad(ReturnValue);
841 break;
842
Daniel Dunbar11434922009-01-26 21:26:08 +0000843 case ABIArgInfo::Ignore:
844 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000846 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000847 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000848 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000849
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000850 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000851 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000852 }
853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000855 if (RV) {
856 Builder.CreateRet(RV);
857 } else {
858 Builder.CreateRetVoid();
859 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000860}
861
Anders Carlsson0139bb92009-04-08 20:47:54 +0000862RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000863 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000864 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Anders Carlsson0139bb92009-04-08 20:47:54 +0000866 return EmitAnyExprToTemp(E);
867}
868
Daniel Dunbar88b53962009-02-02 22:03:45 +0000869RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000870 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000871 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000872 const CallArgList &CallArgs,
873 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000874 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000875 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000876
877 // Handle struct-return functions by passing a pointer to the
878 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000879 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000880 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000881
882
Chris Lattner5db7ae52009-06-13 00:26:38 +0000883 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000884 // alloca to hold the result, unless one is given to us.
885 if (CGM.ReturnTypeUsesSret(CallInfo)) {
886 llvm::Value *Value = ReturnValue.getValue();
887 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000888 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +0000889 Args.push_back(Value);
890 }
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000892 assert(CallInfo.arg_size() == CallArgs.size() &&
893 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000894 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000895 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000896 I != E; ++I, ++info_it) {
897 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000898 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000899
900 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000901 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000902 if (RV.isScalar() || RV.isComplex()) {
903 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000904 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000905 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000906 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000907 else
Mike Stump1eb44332009-09-09 15:08:12 +0000908 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000909 } else {
910 Args.push_back(RV.getAggregateAddr());
911 }
912 break;
913
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000914 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000915 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000916 if (RV.isScalar()) {
917 Args.push_back(RV.getScalarVal());
918 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000919 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
920 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
921 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
922 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000923 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000924 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000925 }
926 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Daniel Dunbar11434922009-01-26 21:26:08 +0000928 case ABIArgInfo::Ignore:
929 break;
930
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000931 case ABIArgInfo::Coerce: {
932 // FIXME: Avoid the conversion through memory if possible.
933 llvm::Value *SrcPtr;
934 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000935 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000936 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000937 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000938 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000939 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000940 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000941 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000942 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000943 *this));
944 break;
945 }
946
Daniel Dunbar56273772008-09-17 00:51:38 +0000947 case ABIArgInfo::Expand:
948 ExpandTypeToArgs(I->second, RV, Args);
949 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000950 }
951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner5db7ae52009-06-13 00:26:38 +0000953 // If the callee is a bitcast of a function to a varargs pointer to function
954 // type, check to see if we can remove the bitcast. This handles some cases
955 // with unprototyped functions.
956 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
957 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
958 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
959 const llvm::FunctionType *CurFT =
960 cast<llvm::FunctionType>(CurPT->getElementType());
961 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Chris Lattner5db7ae52009-06-13 00:26:38 +0000963 if (CE->getOpcode() == llvm::Instruction::BitCast &&
964 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000965 ActualFT->getNumParams() == CurFT->getNumParams() &&
966 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000967 bool ArgsMatch = true;
968 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
969 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
970 ArgsMatch = false;
971 break;
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattner5db7ae52009-06-13 00:26:38 +0000974 // Strip the cast if we can get away with it. This is a nice cleanup,
975 // but also allows us to inline the function at -O0 if it is marked
976 // always_inline.
977 if (ArgsMatch)
978 Callee = CalleeF;
979 }
980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000982
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000983 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000984 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000985 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000986 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000987 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
988 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Daniel Dunbard14151d2009-03-02 04:32:35 +0000990 llvm::CallSite CS;
991 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000992 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000993 } else {
994 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000995 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000996 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000997 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000998 }
999
Daniel Dunbard14151d2009-03-02 04:32:35 +00001000 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001001 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001002
1003 // If the call doesn't return, finish the basic block and clear the
1004 // insertion point; this allows the rest of IRgen to discard
1005 // unreachable code.
1006 if (CS.doesNotReturn()) {
1007 Builder.CreateUnreachable();
1008 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Mike Stumpf5408fe2009-05-16 07:57:57 +00001010 // FIXME: For now, emit a dummy basic block because expr emitters in
1011 // generally are not ready to handle emitting expressions at unreachable
1012 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001013 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Daniel Dunbard14151d2009-03-02 04:32:35 +00001015 // Return a reasonable RValue.
1016 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001017 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001018
1019 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001020 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001021 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001022
1023 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001024 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001025 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001026 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001027 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001028 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001029 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001030
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001031 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001032 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001033 if (RetTy->isAnyComplexType()) {
1034 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1035 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1036 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001037 }
1038 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001039 llvm::Value *DestPtr = ReturnValue.getValue();
1040 bool DestIsVolatile = ReturnValue.isVolatile();
1041
1042 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001043 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001044 DestIsVolatile = false;
1045 }
1046 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1047 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001048 }
1049 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001050
Daniel Dunbar11434922009-01-26 21:26:08 +00001051 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001052 // If we are ignoring an argument that had a result, make sure to
1053 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001054 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001055
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001056 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001057 llvm::Value *DestPtr = ReturnValue.getValue();
1058 bool DestIsVolatile = ReturnValue.isVolatile();
1059
1060 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001061 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001062 DestIsVolatile = false;
1063 }
1064
1065 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001066 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001067 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001068 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001069 return RValue::getAggregate(DestPtr);
1070 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001071 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001072
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001073 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001074 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001075 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001076
1077 assert(0 && "Unhandled ABIArgInfo::Kind");
1078 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001079}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001080
1081/* VarArg handling */
1082
1083llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1084 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1085}