blob: d861348ce84cf3677254dfbe3494307b8a55c56f [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,
178 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000179}
180
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000181const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
182 // FIXME: Do we need to handle ObjCMethodDecl?
183 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
184
185 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
186 return getFunctionInfo(CD, GD.getCtorType());
187
188 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
189 return getFunctionInfo(DD, GD.getDtorType());
190
191 return getFunctionInfo(FD);
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000195 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000196 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000197 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000198 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000199 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000200 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000201 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000202 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000203}
204
Mike Stump1eb44332009-09-09 15:08:12 +0000205const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000206 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000207 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000208 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000209 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000210 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000211 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000212 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000213 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000214}
215
John McCallead608a2010-02-26 00:48:12 +0000216const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
217 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000218 const FunctionType::ExtInfo &Info) {
219 const CallingConv CallConv = Info.getCC();
220 const bool NoReturn = Info.getNoReturn();
221
John McCallead608a2010-02-26 00:48:12 +0000222#ifndef NDEBUG
223 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
224 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
225 assert(I->isCanonicalAsParam());
226#endif
227
John McCall04a67a62010-02-05 21:31:56 +0000228 unsigned CC = ClangCallConvToLLVMCallConv(CallConv);
229
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000230 // Lookup or create unique function info.
231 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000232 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000233 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000234
235 void *InsertPos = 0;
236 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
237 if (FI)
238 return *FI;
239
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000240 // Construct the function info.
John McCall04a67a62010-02-05 21:31:56 +0000241 FI = new CGFunctionInfo(CC, NoReturn, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000242 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000243
244 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000245 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000246
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000247 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000248}
249
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000250CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000251 bool _NoReturn,
John McCallead608a2010-02-26 00:48:12 +0000252 CanQualType ResTy,
253 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000254 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000255 EffectiveCallingConvention(_CallingConvention),
256 NoReturn(_NoReturn)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000257{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000258 NumArgs = ArgTys.size();
259 Args = new ArgInfo[1 + NumArgs];
260 Args[0].type = ResTy;
261 for (unsigned i = 0; i < NumArgs; ++i)
262 Args[1 + i].type = ArgTys[i];
263}
264
265/***/
266
Mike Stump1eb44332009-09-09 15:08:12 +0000267void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000268 std::vector<const llvm::Type*> &ArgTys) {
269 const RecordType *RT = Ty->getAsStructureType();
270 assert(RT && "Can only expand structure types.");
271 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000272 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000273 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000275 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
276 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000277 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000278 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000279 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Daniel Dunbar56273772008-09-17 00:51:38 +0000281 QualType FT = FD->getType();
282 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
283 GetExpandedTypes(FT, ArgTys);
284 } else {
285 ArgTys.push_back(ConvertType(FT));
286 }
287 }
288}
289
Mike Stump1eb44332009-09-09 15:08:12 +0000290llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000291CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
292 llvm::Function::arg_iterator AI) {
293 const RecordType *RT = Ty->getAsStructureType();
294 assert(RT && "Can only expand structure types.");
295
296 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000297 assert(LV.isSimple() &&
298 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000299 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000300 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
301 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000302 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000303 QualType FT = FD->getType();
304
305 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000306 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000307 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
308 AI = ExpandTypeFromArgs(FT, LV, AI);
309 } else {
310 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
311 ++AI;
312 }
313 }
314
315 return AI;
316}
317
Mike Stump1eb44332009-09-09 15:08:12 +0000318void
319CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000320 llvm::SmallVector<llvm::Value*, 16> &Args) {
321 const RecordType *RT = Ty->getAsStructureType();
322 assert(RT && "Can only expand structure types.");
323
324 RecordDecl *RD = RT->getDecl();
325 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
326 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000327 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
328 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000329 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000330 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Daniel Dunbar56273772008-09-17 00:51:38 +0000332 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000333 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000334 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
335 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
336 } else {
337 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000338 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000339 "Unexpected non-scalar rvalue during struct expansion.");
340 Args.push_back(RV.getScalarVal());
341 }
342 }
343}
344
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000345/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
346/// a pointer to an object of type \arg Ty.
347///
348/// This safely handles the case when the src type is smaller than the
349/// destination type; in this situation the values of bits which not
350/// present in the src are undefined.
351static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
352 const llvm::Type *Ty,
353 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000354 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000355 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000356 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
357 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000358
Daniel Dunbarb225be42009-02-03 05:59:18 +0000359 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000360 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000361 // Generally SrcSize is never greater than DstSize, since this means we are
362 // losing bits. However, this can happen in cases where the structure has
363 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000364 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000365 // FIXME: Assert that we aren't truncating non-padding bits when have access
366 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000367 llvm::Value *Casted =
368 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000369 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
370 // FIXME: Use better alignment / avoid requiring aligned load.
371 Load->setAlignment(1);
372 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000373 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000374 // Otherwise do coercion through memory. This is stupid, but
375 // simple.
376 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000377 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000378 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000379 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000380 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
381 // FIXME: Use better alignment / avoid requiring aligned store.
382 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000383 return CGF.Builder.CreateLoad(Tmp);
384 }
385}
386
387/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
388/// where the source and destination may have different types.
389///
390/// This safely handles the case when the src type is larger than the
391/// destination type; the upper bits of the src will be lost.
392static void CreateCoercedStore(llvm::Value *Src,
393 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000394 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000395 CodeGenFunction &CGF) {
396 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000397 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000398 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
399
Duncan Sands9408c452009-05-09 07:08:47 +0000400 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
401 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000402
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000403 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000404 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000405 llvm::Value *Casted =
406 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000407 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000408 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000409 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000410 // Otherwise do coercion through memory. This is stupid, but
411 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000412
413 // Generally SrcSize is never greater than DstSize, since this means we are
414 // losing bits. However, this can happen in cases where the structure has
415 // additional padding, for example due to a user specified alignment.
416 //
417 // FIXME: Assert that we aren't truncating non-padding bits when have access
418 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000419 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
420 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000421 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000422 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000423 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
424 // FIXME: Use better alignment / avoid requiring aligned load.
425 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000426 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000427 }
428}
429
Daniel Dunbar56273772008-09-17 00:51:38 +0000430/***/
431
Daniel Dunbar88b53962009-02-02 22:03:45 +0000432bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000433 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000434}
435
John McCallc0bf4622010-02-23 00:48:20 +0000436const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
437 const CGFunctionInfo &FI = getFunctionInfo(GD);
438
439 // For definition purposes, don't consider a K&R function variadic.
440 bool Variadic = false;
441 if (const FunctionProtoType *FPT =
442 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
443 Variadic = FPT->isVariadic();
444
445 return GetFunctionType(FI, Variadic);
446}
447
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000448const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000449CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000450 std::vector<const llvm::Type*> ArgTys;
451
452 const llvm::Type *ResultType = 0;
453
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000454 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000455 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000456 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000457 case ABIArgInfo::Expand:
458 assert(0 && "Invalid ABI kind for return argument");
459
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000460 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000461 case ABIArgInfo::Direct:
462 ResultType = ConvertType(RetTy);
463 break;
464
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000465 case ABIArgInfo::Indirect: {
466 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000467 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000468 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000469 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
470 break;
471 }
472
Daniel Dunbar11434922009-01-26 21:26:08 +0000473 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000474 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000475 break;
476
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000477 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000478 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000479 break;
480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
482 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000483 ie = FI.arg_end(); it != ie; ++it) {
484 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000486 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000487 case ABIArgInfo::Ignore:
488 break;
489
Daniel Dunbar56273772008-09-17 00:51:38 +0000490 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000491 ArgTys.push_back(AI.getCoerceToType());
492 break;
493
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000494 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000495 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000496 const llvm::Type *LTy = ConvertTypeForMem(it->type);
497 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000498 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000499 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000500
501 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000502 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000503 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000504 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000506 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000507 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000508 break;
509 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000510 }
511
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000512 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000513}
514
Anders Carlssonecf282b2009-11-24 05:08:52 +0000515static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
516 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
517 if (!TT->getDecl()->isDefinition())
518 return true;
519 }
520
521 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
522 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
523 if (!TT->getDecl()->isDefinition())
524 return true;
525 }
526 }
527
528 return false;
529}
530
531const llvm::Type *
532CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
533 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
534
535 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
536 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
537
538 return llvm::OpaqueType::get(getLLVMContext());
539}
540
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000541void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000542 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000543 AttributeListType &PAL,
544 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000545 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000546 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000547
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000548 CallingConv = FI.getEffectiveCallingConvention();
549
John McCall04a67a62010-02-05 21:31:56 +0000550 if (FI.isNoReturn())
551 FuncAttrs |= llvm::Attribute::NoReturn;
552
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000553 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000554 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000555 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000556 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000557 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000558 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000559 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000560 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000561 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000562 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000563 if (TargetDecl->hasAttr<MallocAttr>())
564 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000565 }
566
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000567 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000568 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000569 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000570 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000571 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000572 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000573
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000574 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000575 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000576 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000577 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000578 case ABIArgInfo::Extend:
579 if (RetTy->isSignedIntegerType()) {
580 RetAttrs |= llvm::Attribute::SExt;
581 } else if (RetTy->isUnsignedIntegerType()) {
582 RetAttrs |= llvm::Attribute::ZExt;
583 }
584 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000585 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000586 break;
587
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000588 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000589 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000590 llvm::Attribute::StructRet |
591 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000592 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000593 // sret disables readnone and readonly
594 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
595 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000596 break;
597
Daniel Dunbar11434922009-01-26 21:26:08 +0000598 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000599 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000600 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000601
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000602 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000603 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000604 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000605
Devang Patela2c69122008-09-26 22:53:57 +0000606 if (RetAttrs)
607 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000608
609 // FIXME: we need to honour command line settings also...
610 // FIXME: RegParm should be reduced in case of nested functions and/or global
611 // register variable.
612 signed RegParm = 0;
613 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000614 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000615 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000616 RegParm = RegParmAttr->getNumParams();
617
618 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000619 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000620 ie = FI.arg_end(); it != ie; ++it) {
621 QualType ParamType = it->type;
622 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000623 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000624
John McCalld8e10d22010-03-27 00:47:27 +0000625 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
626 // have the corresponding parameter variable. It doesn't make
627 // sense to do it here because parameters are so fucked up.
Nuno Lopes079b4952009-12-07 18:30:06 +0000628
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000629 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000630 case ABIArgInfo::Coerce:
631 break;
632
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000633 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000634 if (AI.getIndirectByVal())
635 Attributes |= llvm::Attribute::ByVal;
636
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000637 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000638 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000639 // byval disables readnone and readonly.
640 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
641 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000642 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000643
644 case ABIArgInfo::Extend:
645 if (ParamType->isSignedIntegerType()) {
646 Attributes |= llvm::Attribute::SExt;
647 } else if (ParamType->isUnsignedIntegerType()) {
648 Attributes |= llvm::Attribute::ZExt;
649 }
650 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000651 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000652 if (RegParm > 0 &&
653 (ParamType->isIntegerType() || ParamType->isPointerType())) {
654 RegParm -=
655 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
656 if (RegParm >= 0)
657 Attributes |= llvm::Attribute::InReg;
658 }
659 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000660 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000661
Daniel Dunbar11434922009-01-26 21:26:08 +0000662 case ABIArgInfo::Ignore:
663 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000664 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000665
Daniel Dunbar56273772008-09-17 00:51:38 +0000666 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000667 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000668 // FIXME: This is rather inefficient. Do we ever actually need to do
669 // anything here? The result should be just reconstructed on the other
670 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000671 getTypes().GetExpandedTypes(ParamType, Tys);
672 Index += Tys.size();
673 continue;
674 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Devang Patel761d7f72008-09-25 21:02:23 +0000677 if (Attributes)
678 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000679 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000680 }
Devang Patela2c69122008-09-26 22:53:57 +0000681 if (FuncAttrs)
682 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000683}
684
Daniel Dunbar88b53962009-02-02 22:03:45 +0000685void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
686 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000687 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000688 // If this is an implicit-return-zero function, go ahead and
689 // initialize the return value. TODO: it might be nice to have
690 // a more general mechanism for this that didn't require synthesized
691 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000692 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000693 if (FD->hasImplicitReturnZero()) {
694 QualType RetTy = FD->getResultType().getUnqualifiedType();
695 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000696 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000697 Builder.CreateStore(Zero, ReturnValue);
698 }
699 }
700
Mike Stumpf5408fe2009-05-16 07:57:57 +0000701 // FIXME: We no longer need the types from FunctionArgList; lift up and
702 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000703
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000704 // Emit allocs for param decls. Give the LLVM Argument nodes names.
705 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000707 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000708 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000709 AI->setName("agg.result");
710 ++AI;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000713 assert(FI.arg_size() == Args.size() &&
714 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000715 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000716 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000717 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000718 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000719 QualType Ty = info_it->type;
720 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000721
722 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000723 case ABIArgInfo::Indirect: {
724 llvm::Value* V = AI;
725 if (hasAggregateLLVMType(Ty)) {
726 // Do nothing, aggregates and complex variables are accessed by
727 // reference.
728 } else {
729 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000730 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000731 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
732 // This must be a promotion, for something like
733 // "void a(x) short x; {..."
734 V = EmitScalarConversion(V, Ty, Arg->getType());
735 }
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000738 break;
739 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000740
741 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000742 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000743 assert(AI != Fn->arg_end() && "Argument mismatch!");
744 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000745 if (hasAggregateLLVMType(Ty)) {
746 // Create a temporary alloca to hold the argument; the rest of
747 // codegen expects to access aggregates & complex values by
748 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000749 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000750 Builder.CreateStore(AI, V);
751 } else {
John McCalld8e10d22010-03-27 00:47:27 +0000752 if (Arg->getType().isRestrictQualified())
753 AI->addAttr(llvm::Attribute::NoAlias);
754
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000755 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
756 // This must be a promotion, for something like
757 // "void a(x) short x; {..."
758 V = EmitScalarConversion(V, Ty, Arg->getType());
759 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000760 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000761 EmitParmDecl(*Arg, V);
762 break;
763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Daniel Dunbar56273772008-09-17 00:51:38 +0000765 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000766 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000767 // we need to create a temporary and reconstruct it from the
768 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000769 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000770 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000771 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000772 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000773 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000774
Daniel Dunbar56273772008-09-17 00:51:38 +0000775 // Name the arguments used in expansion and increment AI.
776 unsigned Index = 0;
777 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000778 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000779 continue;
780 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000781
782 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000783 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000784 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000785 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000786 } else {
787 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000790 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000791 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000792
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000793 case ABIArgInfo::Coerce: {
794 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000795 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
796 // result in a new alloca anyway, so we could just store into that
797 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000798 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000799 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000800 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000801 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000802 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000803 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
804 // This must be a promotion, for something like
805 // "void a(x) short x; {..."
806 V = EmitScalarConversion(V, Ty, Arg->getType());
807 }
808 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000809 EmitParmDecl(*Arg, V);
810 break;
811 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000812 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000813
814 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000815 }
816 assert(AI == Fn->arg_end() && "Argument mismatch!");
817}
818
Daniel Dunbar88b53962009-02-02 22:03:45 +0000819void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000820 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000821 llvm::Value *RV = 0;
822
823 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000824 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000825 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000826 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000827
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000828 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000829 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000830 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000831 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
832 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
833 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000834 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000835 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000836 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000837 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000838 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000839 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000840
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000841 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000842 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000843 // The internal return value temp always will have
844 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000845 RV = Builder.CreateLoad(ReturnValue);
846 break;
847
Daniel Dunbar11434922009-01-26 21:26:08 +0000848 case ABIArgInfo::Ignore:
849 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000851 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000852 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000853 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000854
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000855 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000856 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000857 }
858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000860 if (RV) {
861 Builder.CreateRet(RV);
862 } else {
863 Builder.CreateRetVoid();
864 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000865}
866
Anders Carlsson0139bb92009-04-08 20:47:54 +0000867RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000868 if (ArgType->isReferenceType())
Anders Carlssona64a8692010-02-03 16:38:03 +0000869 return EmitReferenceBindingToExpr(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Anders Carlsson0139bb92009-04-08 20:47:54 +0000871 return EmitAnyExprToTemp(E);
872}
873
Daniel Dunbar88b53962009-02-02 22:03:45 +0000874RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000875 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000876 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000877 const CallArgList &CallArgs,
878 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000879 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000880 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000881
882 // Handle struct-return functions by passing a pointer to the
883 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000884 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000885 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000886
887
Chris Lattner5db7ae52009-06-13 00:26:38 +0000888 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000889 // alloca to hold the result, unless one is given to us.
890 if (CGM.ReturnTypeUsesSret(CallInfo)) {
891 llvm::Value *Value = ReturnValue.getValue();
892 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000893 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +0000894 Args.push_back(Value);
895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000897 assert(CallInfo.arg_size() == CallArgs.size() &&
898 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000899 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000900 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000901 I != E; ++I, ++info_it) {
902 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000903 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000904
905 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000906 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000907 if (RV.isScalar() || RV.isComplex()) {
908 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000909 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000910 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000911 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000912 else
Mike Stump1eb44332009-09-09 15:08:12 +0000913 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000914 } else {
915 Args.push_back(RV.getAggregateAddr());
916 }
917 break;
918
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000919 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000920 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000921 if (RV.isScalar()) {
922 Args.push_back(RV.getScalarVal());
923 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000924 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
925 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
926 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
927 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000928 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000929 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000930 }
931 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Daniel Dunbar11434922009-01-26 21:26:08 +0000933 case ABIArgInfo::Ignore:
934 break;
935
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000936 case ABIArgInfo::Coerce: {
937 // FIXME: Avoid the conversion through memory if possible.
938 llvm::Value *SrcPtr;
939 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000940 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000941 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000942 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000943 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000944 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000945 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000946 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000947 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000948 *this));
949 break;
950 }
951
Daniel Dunbar56273772008-09-17 00:51:38 +0000952 case ABIArgInfo::Expand:
953 ExpandTypeToArgs(I->second, RV, Args);
954 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000955 }
956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattner5db7ae52009-06-13 00:26:38 +0000958 // If the callee is a bitcast of a function to a varargs pointer to function
959 // type, check to see if we can remove the bitcast. This handles some cases
960 // with unprototyped functions.
961 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
962 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
963 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
964 const llvm::FunctionType *CurFT =
965 cast<llvm::FunctionType>(CurPT->getElementType());
966 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner5db7ae52009-06-13 00:26:38 +0000968 if (CE->getOpcode() == llvm::Instruction::BitCast &&
969 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000970 ActualFT->getNumParams() == CurFT->getNumParams() &&
971 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000972 bool ArgsMatch = true;
973 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
974 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
975 ArgsMatch = false;
976 break;
977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner5db7ae52009-06-13 00:26:38 +0000979 // Strip the cast if we can get away with it. This is a nice cleanup,
980 // but also allows us to inline the function at -O0 if it is marked
981 // always_inline.
982 if (ArgsMatch)
983 Callee = CalleeF;
984 }
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000987
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000988 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000989 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000990 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000991 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000992 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
993 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Daniel Dunbard14151d2009-03-02 04:32:35 +0000995 llvm::CallSite CS;
996 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000997 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000998 } else {
999 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001000 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001001 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001002 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001003 }
1004
Daniel Dunbard14151d2009-03-02 04:32:35 +00001005 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001006 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001007
1008 // If the call doesn't return, finish the basic block and clear the
1009 // insertion point; this allows the rest of IRgen to discard
1010 // unreachable code.
1011 if (CS.doesNotReturn()) {
1012 Builder.CreateUnreachable();
1013 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Mike Stumpf5408fe2009-05-16 07:57:57 +00001015 // FIXME: For now, emit a dummy basic block because expr emitters in
1016 // generally are not ready to handle emitting expressions at unreachable
1017 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001018 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Daniel Dunbard14151d2009-03-02 04:32:35 +00001020 // Return a reasonable RValue.
1021 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001022 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001023
1024 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001025 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001026 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001027
1028 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001029 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001030 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001031 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001032 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001033 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001034 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001035
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001036 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001037 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001038 if (RetTy->isAnyComplexType()) {
1039 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1040 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1041 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001042 }
1043 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001044 llvm::Value *DestPtr = ReturnValue.getValue();
1045 bool DestIsVolatile = ReturnValue.isVolatile();
1046
1047 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001048 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001049 DestIsVolatile = false;
1050 }
1051 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1052 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001053 }
1054 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001055
Daniel Dunbar11434922009-01-26 21:26:08 +00001056 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001057 // If we are ignoring an argument that had a result, make sure to
1058 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001059 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001060
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001061 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001062 llvm::Value *DestPtr = ReturnValue.getValue();
1063 bool DestIsVolatile = ReturnValue.isVolatile();
1064
1065 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001066 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001067 DestIsVolatile = false;
1068 }
1069
1070 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001071 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001072 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001073 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001074 return RValue::getAggregate(DestPtr);
1075 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001076 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001077
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001078 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001079 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001080 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001081
1082 assert(0 && "Unhandled ABIArgInfo::Kind");
1083 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001084}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001085
1086/* VarArg handling */
1087
1088llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1089 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1090}