blob: bfc22d1bc795f33b022579b44c785753bb00419b [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"
Chris Lattnerce933992010-06-29 16:40:28 +000016#include "ABIInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000017#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000018#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000020#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000021#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000022#include "clang/AST/DeclObjC.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000023#include "clang/Frontend/CodeGenOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
30/***/
31
John McCall04a67a62010-02-05 21:31:56 +000032static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
33 switch (CC) {
34 default: return llvm::CallingConv::C;
35 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
36 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregorf813a2c2010-05-18 16:57:00 +000037 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
John McCall04a67a62010-02-05 21:31:56 +000038 }
39}
40
John McCall0b0ef0a2010-02-24 07:14:12 +000041/// Derives the 'this' type for codegen purposes, i.e. ignoring method
42/// qualification.
43/// FIXME: address space qualification?
John McCallead608a2010-02-26 00:48:12 +000044static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
45 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
46 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000047}
48
John McCall0b0ef0a2010-02-24 07:14:12 +000049/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000050static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
51 return MD->getType()->getCanonicalTypeUnqualified()
52 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-02-24 07:14:12 +000053}
54
55/// Returns the "extra-canonicalized" return type, which discards
56/// qualifiers on the return type. Codegen doesn't care about them,
57/// and it makes ABI code a little easier to be able to assume that
58/// all parameter and return types are top-level unqualified.
John McCallead608a2010-02-26 00:48:12 +000059static CanQualType GetReturnType(QualType RetTy) {
60 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000061}
62
63const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000064CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
65 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
66 llvm::SmallVector<CanQualType, 16>(),
Rafael Espindola264ba482010-03-30 20:24:48 +000067 FTNP->getExtInfo());
John McCall0b0ef0a2010-02-24 07:14:12 +000068}
69
70/// \param Args - contains any initial parameters besides those
71/// in the formal type
72static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000073 llvm::SmallVectorImpl<CanQualType> &ArgTys,
74 CanQual<FunctionProtoType> FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000075 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000076 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000077 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000078 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Chris Lattnerce933992010-06-29 16:40:28 +000079 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
John McCall0b0ef0a2010-02-24 07:14:12 +000080}
81
82const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000083CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
84 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +000085 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000086}
87
John McCall04a67a62010-02-05 21:31:56 +000088static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000089 // Set the appropriate calling convention for the Function.
90 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000091 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000092
93 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000094 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000095
Douglas Gregorf813a2c2010-05-18 16:57:00 +000096 if (D->hasAttr<ThisCallAttr>())
97 return CC_X86ThisCall;
98
John McCall04a67a62010-02-05 21:31:56 +000099 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000100}
101
Anders Carlsson375c31c2009-10-03 19:43:08 +0000102const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
103 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000104 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000105
Anders Carlsson375c31c2009-10-03 19:43:08 +0000106 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000107 ArgTys.push_back(GetThisType(Context, RD));
108
109 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000110 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000111}
112
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000113const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000114 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000115
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000116 // Add the 'this' pointer unless this is a static method.
117 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000118 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000119
John McCall0b0ef0a2010-02-24 07:14:12 +0000120 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000121}
122
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000123const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
124 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000125 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000126
127 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000128 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000129
130 // Check if we need to add a VTT parameter (which has type void **).
131 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
132 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000133
134 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000135}
136
137const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
138 CXXDtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000139 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000140
141 // Add the 'this' pointer.
John McCallead608a2010-02-26 00:48:12 +0000142 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000143
144 // Check if we need to add a VTT parameter (which has type void **).
145 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
146 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000147
148 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000149}
150
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000151const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000152 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000153 if (MD->isInstance())
154 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
John McCallead608a2010-02-26 00:48:12 +0000156 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
157 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000158 if (isa<FunctionNoProtoType>(FTy))
John McCallead608a2010-02-26 00:48:12 +0000159 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
160 assert(isa<FunctionProtoType>(FTy));
161 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000162}
163
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000164const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000165 llvm::SmallVector<CanQualType, 16> ArgTys;
166 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
167 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000168 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000169 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000170 e = MD->param_end(); i != e; ++i) {
171 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
172 }
173 return getFunctionInfo(GetReturnType(MD->getResultType()),
174 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000175 FunctionType::ExtInfo(
176 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000177 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000178 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) {
John McCallead608a2010-02-26 00:48:12 +0000219#ifndef NDEBUG
220 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
221 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
222 assert(I->isCanonicalAsParam());
223#endif
224
Rafael Espindola425ef722010-03-30 22:15:11 +0000225 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000226
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000227 // Lookup or create unique function info.
228 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000229 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000230 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000231
232 void *InsertPos = 0;
233 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
234 if (FI)
235 return *FI;
236
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000237 // Construct the function info.
Chris Lattnerce700162010-06-28 23:44:11 +0000238 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
239 ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000240 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000241
Chris Lattner8640cd62010-06-29 01:08:48 +0000242 // ABI lowering wants to know what our preferred type for the argument is in
243 // various situations, pass it in.
244 llvm::SmallVector<const llvm::Type *, 8> PreferredArgTypes;
245 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
246 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
247 PreferredArgTypes.push_back(ConvertType(*I));
248
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000249 // Compute ABI information.
Chris Lattner8640cd62010-06-29 01:08:48 +0000250 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext(),
251 PreferredArgTypes.data(), PreferredArgTypes.size());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000252
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000253 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000254}
255
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000256CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000257 bool _NoReturn,
Rafael Espindola425ef722010-03-30 22:15:11 +0000258 unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000259 CanQualType ResTy,
260 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000261 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000262 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000263 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000264{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000265 NumArgs = ArgTys.size();
Chris Lattnerce700162010-06-28 23:44:11 +0000266
267 // FIXME: Coallocate with the CGFunctionInfo object.
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000268 Args = new ArgInfo[1 + NumArgs];
269 Args[0].type = ResTy;
270 for (unsigned i = 0; i < NumArgs; ++i)
271 Args[1 + i].type = ArgTys[i];
272}
273
274/***/
275
Mike Stump1eb44332009-09-09 15:08:12 +0000276void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000277 std::vector<const llvm::Type*> &ArgTys) {
278 const RecordType *RT = Ty->getAsStructureType();
279 assert(RT && "Can only expand structure types.");
280 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000281 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000282 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000284 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
285 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000286 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000287 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000288 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Daniel Dunbar56273772008-09-17 00:51:38 +0000290 QualType FT = FD->getType();
291 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
292 GetExpandedTypes(FT, ArgTys);
293 } else {
294 ArgTys.push_back(ConvertType(FT));
295 }
296 }
297}
298
Mike Stump1eb44332009-09-09 15:08:12 +0000299llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000300CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
301 llvm::Function::arg_iterator AI) {
302 const RecordType *RT = Ty->getAsStructureType();
303 assert(RT && "Can only expand structure types.");
304
305 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000306 assert(LV.isSimple() &&
307 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000308 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000309 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
310 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000311 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000312 QualType FT = FD->getType();
313
314 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000315 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000316 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
317 AI = ExpandTypeFromArgs(FT, LV, AI);
318 } else {
319 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
320 ++AI;
321 }
322 }
323
324 return AI;
325}
326
Mike Stump1eb44332009-09-09 15:08:12 +0000327void
328CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000329 llvm::SmallVector<llvm::Value*, 16> &Args) {
330 const RecordType *RT = Ty->getAsStructureType();
331 assert(RT && "Can only expand structure types.");
332
333 RecordDecl *RD = RT->getDecl();
334 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
335 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000336 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
337 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000338 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000339 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Daniel Dunbar56273772008-09-17 00:51:38 +0000341 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000342 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000343 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
344 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
345 } else {
346 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000347 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000348 "Unexpected non-scalar rvalue during struct expansion.");
349 Args.push_back(RV.getScalarVal());
350 }
351 }
352}
353
Chris Lattnere7bb7772010-06-27 06:04:18 +0000354/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000355/// accessing some number of bytes out of it, try to gep into the struct to get
356/// at its inner goodness. Dive as deep as possible without entering an element
357/// with an in-memory size smaller than DstSize.
358static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000359EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
360 const llvm::StructType *SrcSTy,
361 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000362 // We can't dive into a zero-element struct.
363 if (SrcSTy->getNumElements() == 0) return SrcPtr;
364
365 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
366
367 // If the first elt is at least as large as what we're looking for, or if the
368 // first element is the same size as the whole struct, we can enter it.
369 uint64_t FirstEltSize =
370 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
371 if (FirstEltSize < DstSize &&
372 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
373 return SrcPtr;
374
375 // GEP into the first element.
376 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
377
378 // If the first element is a struct, recurse.
379 const llvm::Type *SrcTy =
380 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
381 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000382 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000383
384 return SrcPtr;
385}
386
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000387/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
388/// are either integers or pointers. This does a truncation of the value if it
389/// is too large or a zero extension if it is too small.
390static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
391 const llvm::Type *Ty,
392 CodeGenFunction &CGF) {
393 if (Val->getType() == Ty)
394 return Val;
395
396 if (isa<llvm::PointerType>(Val->getType())) {
397 // If this is Pointer->Pointer avoid conversion to and from int.
398 if (isa<llvm::PointerType>(Ty))
399 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
400
401 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000402 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000403 }
404
405 const llvm::Type *DestIntTy = Ty;
406 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000407 DestIntTy = CGF.IntPtrTy;
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000408
409 if (Val->getType() != DestIntTy)
410 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
411
412 if (isa<llvm::PointerType>(Ty))
413 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
414 return Val;
415}
416
Chris Lattner08dd2a02010-06-27 05:56:15 +0000417
418
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000419/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
420/// a pointer to an object of type \arg Ty.
421///
422/// This safely handles the case when the src type is smaller than the
423/// destination type; in this situation the values of bits which not
424/// present in the src are undefined.
425static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
426 const llvm::Type *Ty,
427 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000428 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000429 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000430
431 // If SrcTy and Ty are the same, just do a load.
432 if (SrcTy == Ty)
433 return CGF.Builder.CreateLoad(SrcPtr);
434
Duncan Sands9408c452009-05-09 07:08:47 +0000435 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000436
437 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000438 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000439 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
440 }
441
442 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000443
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000444 // If the source and destination are integer or pointer types, just do an
445 // extension or truncation to the desired type.
446 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
447 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
448 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
449 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
450 }
451
Daniel Dunbarb225be42009-02-03 05:59:18 +0000452 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000453 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000454 // Generally SrcSize is never greater than DstSize, since this means we are
455 // losing bits. However, this can happen in cases where the structure has
456 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000457 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000458 // FIXME: Assert that we aren't truncating non-padding bits when have access
459 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000460 llvm::Value *Casted =
461 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000462 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
463 // FIXME: Use better alignment / avoid requiring aligned load.
464 Load->setAlignment(1);
465 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000466 }
Chris Lattner35b21b82010-06-27 01:06:27 +0000467
468 // Otherwise do coercion through memory. This is stupid, but
469 // simple.
470 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
471 llvm::Value *Casted =
472 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
473 llvm::StoreInst *Store =
474 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
475 // FIXME: Use better alignment / avoid requiring aligned store.
476 Store->setAlignment(1);
477 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000478}
479
480/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
481/// where the source and destination may have different types.
482///
483/// This safely handles the case when the src type is larger than the
484/// destination type; the upper bits of the src will be lost.
485static void CreateCoercedStore(llvm::Value *Src,
486 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000487 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000488 CodeGenFunction &CGF) {
489 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000490 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000491 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000492 if (SrcTy == DstTy) {
493 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
494 return;
495 }
496
497 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
498
Chris Lattnere7bb7772010-06-27 06:04:18 +0000499 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
500 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
501 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
502 }
503
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000504 // If the source and destination are integer or pointer types, just do an
505 // extension or truncation to the desired type.
506 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
507 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
508 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
509 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
510 return;
511 }
512
Duncan Sands9408c452009-05-09 07:08:47 +0000513 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000514
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000515 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000516 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000517 llvm::Value *Casted =
518 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000519 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000520 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000521 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000522 // Otherwise do coercion through memory. This is stupid, but
523 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000524
525 // Generally SrcSize is never greater than DstSize, since this means we are
526 // losing bits. However, this can happen in cases where the structure has
527 // additional padding, for example due to a user specified alignment.
528 //
529 // FIXME: Assert that we aren't truncating non-padding bits when have access
530 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000531 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
532 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000533 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000534 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000535 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
536 // FIXME: Use better alignment / avoid requiring aligned load.
537 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000538 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000539 }
540}
541
Daniel Dunbar56273772008-09-17 00:51:38 +0000542/***/
543
Daniel Dunbar88b53962009-02-02 22:03:45 +0000544bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000545 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000546}
547
John McCallc0bf4622010-02-23 00:48:20 +0000548const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
549 const CGFunctionInfo &FI = getFunctionInfo(GD);
550
551 // For definition purposes, don't consider a K&R function variadic.
552 bool Variadic = false;
553 if (const FunctionProtoType *FPT =
554 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
555 Variadic = FPT->isVariadic();
556
557 return GetFunctionType(FI, Variadic);
558}
559
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000560const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000561CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000562 std::vector<const llvm::Type*> ArgTys;
563
564 const llvm::Type *ResultType = 0;
565
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000566 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000567 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000568 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000569 case ABIArgInfo::Expand:
570 assert(0 && "Invalid ABI kind for return argument");
571
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000572 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000573 case ABIArgInfo::Direct:
574 ResultType = ConvertType(RetTy);
575 break;
576
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000577 case ABIArgInfo::Indirect: {
578 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000579 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000580 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000581 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
582 break;
583 }
584
Daniel Dunbar11434922009-01-26 21:26:08 +0000585 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000586 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000587 break;
588
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000589 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000590 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000591 break;
592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
594 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000595 ie = FI.arg_end(); it != ie; ++it) {
596 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000598 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000599 case ABIArgInfo::Ignore:
600 break;
601
Chris Lattnerce700162010-06-28 23:44:11 +0000602 case ABIArgInfo::Coerce: {
603 // If the coerce-to type is a first class aggregate, flatten it. Either
604 // way is semantically identical, but fast-isel and the optimizer
605 // generally likes scalar values better than FCAs.
606 const llvm::Type *ArgTy = AI.getCoerceToType();
607 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
608 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
609 ArgTys.push_back(STy->getElementType(i));
610 } else {
611 ArgTys.push_back(ArgTy);
612 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000613 break;
Chris Lattnerce700162010-06-28 23:44:11 +0000614 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000615
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000616 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000617 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000618 const llvm::Type *LTy = ConvertTypeForMem(it->type);
619 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000620 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000621 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000622
623 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000624 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000625 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000626 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000628 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000629 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000630 break;
631 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000632 }
633
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000634 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000635}
636
Anders Carlssonecf282b2009-11-24 05:08:52 +0000637const llvm::Type *
Anders Carlsson046c2942010-04-17 20:15:18 +0000638CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlssonecf282b2009-11-24 05:08:52 +0000639 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
640
Eli Friedmanc00129a2010-05-30 06:03:20 +0000641 if (!VerifyFuncTypeComplete(FPT))
Anders Carlssonecf282b2009-11-24 05:08:52 +0000642 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
643
644 return llvm::OpaqueType::get(getLLVMContext());
645}
646
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000647void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000648 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000649 AttributeListType &PAL,
650 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000651 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000652 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000653
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000654 CallingConv = FI.getEffectiveCallingConvention();
655
John McCall04a67a62010-02-05 21:31:56 +0000656 if (FI.isNoReturn())
657 FuncAttrs |= llvm::Attribute::NoReturn;
658
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000659 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000660 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000661 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000662 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000663 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000664 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000665 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000666 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000667 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000668 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000669 if (TargetDecl->hasAttr<MallocAttr>())
670 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000671 }
672
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000673 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000674 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000675 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000676 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000677 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000678 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000679
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000680 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000681 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000682 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000683 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000684 case ABIArgInfo::Extend:
685 if (RetTy->isSignedIntegerType()) {
686 RetAttrs |= llvm::Attribute::SExt;
687 } else if (RetTy->isUnsignedIntegerType()) {
688 RetAttrs |= llvm::Attribute::ZExt;
689 }
690 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000691 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000692 break;
693
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000694 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000695 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000696 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000697 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000698 // sret disables readnone and readonly
699 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
700 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000701 break;
702
Daniel Dunbar11434922009-01-26 21:26:08 +0000703 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000704 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000705 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000706
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000707 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000708 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000709 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000710
Devang Patela2c69122008-09-26 22:53:57 +0000711 if (RetAttrs)
712 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000713
714 // FIXME: we need to honour command line settings also...
715 // FIXME: RegParm should be reduced in case of nested functions and/or global
716 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000717 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000718
719 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000720 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000721 ie = FI.arg_end(); it != ie; ++it) {
722 QualType ParamType = it->type;
723 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000724 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000725
John McCalld8e10d22010-03-27 00:47:27 +0000726 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
727 // have the corresponding parameter variable. It doesn't make
728 // sense to do it here because parameters are so fucked up.
Nuno Lopes079b4952009-12-07 18:30:06 +0000729
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000730 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000731 case ABIArgInfo::Coerce:
Chris Lattnerce700162010-06-28 23:44:11 +0000732 if (const llvm::StructType *STy =
733 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
734 Index += STy->getNumElements();
735 else
736 ++Index;
737 continue; // Skip index increment.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000738
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000739 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000740 if (AI.getIndirectByVal())
741 Attributes |= llvm::Attribute::ByVal;
742
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000743 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000744 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000745 // byval disables readnone and readonly.
746 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
747 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000748 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000749
750 case ABIArgInfo::Extend:
751 if (ParamType->isSignedIntegerType()) {
752 Attributes |= llvm::Attribute::SExt;
753 } else if (ParamType->isUnsignedIntegerType()) {
754 Attributes |= llvm::Attribute::ZExt;
755 }
756 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000757 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000758 if (RegParm > 0 &&
759 (ParamType->isIntegerType() || ParamType->isPointerType())) {
760 RegParm -=
761 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
762 if (RegParm >= 0)
763 Attributes |= llvm::Attribute::InReg;
764 }
765 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000766 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000767
Daniel Dunbar11434922009-01-26 21:26:08 +0000768 case ABIArgInfo::Ignore:
769 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000770 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000771
Daniel Dunbar56273772008-09-17 00:51:38 +0000772 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000773 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000774 // FIXME: This is rather inefficient. Do we ever actually need to do
775 // anything here? The result should be just reconstructed on the other
776 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000777 getTypes().GetExpandedTypes(ParamType, Tys);
778 Index += Tys.size();
779 continue;
780 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Devang Patel761d7f72008-09-25 21:02:23 +0000783 if (Attributes)
784 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000785 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000786 }
Devang Patela2c69122008-09-26 22:53:57 +0000787 if (FuncAttrs)
788 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000789}
790
Daniel Dunbar88b53962009-02-02 22:03:45 +0000791void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
792 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000793 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000794 // If this is an implicit-return-zero function, go ahead and
795 // initialize the return value. TODO: it might be nice to have
796 // a more general mechanism for this that didn't require synthesized
797 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000798 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000799 if (FD->hasImplicitReturnZero()) {
800 QualType RetTy = FD->getResultType().getUnqualifiedType();
801 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000802 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000803 Builder.CreateStore(Zero, ReturnValue);
804 }
805 }
806
Mike Stumpf5408fe2009-05-16 07:57:57 +0000807 // FIXME: We no longer need the types from FunctionArgList; lift up and
808 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000809
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000810 // Emit allocs for param decls. Give the LLVM Argument nodes names.
811 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000813 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000814 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000815 AI->setName("agg.result");
816 ++AI;
817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000819 assert(FI.arg_size() == Args.size() &&
820 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000821 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000822 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000823 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000824 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000825 QualType Ty = info_it->type;
826 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000827
828 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000829 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000830 llvm::Value *V = AI;
Daniel Dunbar1f745982009-02-05 09:16:39 +0000831 if (hasAggregateLLVMType(Ty)) {
832 // Do nothing, aggregates and complex variables are accessed by
833 // reference.
834 } else {
835 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000836 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000837 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
838 // This must be a promotion, for something like
839 // "void a(x) short x; {..."
840 V = EmitScalarConversion(V, Ty, Arg->getType());
841 }
842 }
Mike Stump1eb44332009-09-09 15:08:12 +0000843 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000844 break;
845 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000846
847 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000848 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000849 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattnerce700162010-06-28 23:44:11 +0000850 llvm::Value *V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000851 if (hasAggregateLLVMType(Ty)) {
852 // Create a temporary alloca to hold the argument; the rest of
853 // codegen expects to access aggregates & complex values by
854 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000855 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000856 Builder.CreateStore(AI, V);
857 } else {
John McCalld8e10d22010-03-27 00:47:27 +0000858 if (Arg->getType().isRestrictQualified())
859 AI->addAttr(llvm::Attribute::NoAlias);
860
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000861 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
862 // This must be a promotion, for something like
863 // "void a(x) short x; {..."
864 V = EmitScalarConversion(V, Ty, Arg->getType());
865 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000866 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000867 EmitParmDecl(*Arg, V);
868 break;
869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Daniel Dunbar56273772008-09-17 00:51:38 +0000871 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000872 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000873 // we need to create a temporary and reconstruct it from the
874 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000875 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000876 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000877 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000878 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000879 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000880
Daniel Dunbar56273772008-09-17 00:51:38 +0000881 // Name the arguments used in expansion and increment AI.
882 unsigned Index = 0;
883 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000884 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000885 continue;
886 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000887
888 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000889 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000890 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000891 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000892 } else {
893 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000896 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000897 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000898
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000899 case ABIArgInfo::Coerce: {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000900 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
901 // result in a new alloca anyway, so we could just store into that
902 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000903 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Chris Lattner309c59f2010-06-29 00:06:42 +0000904
905 // If the coerce-to type is a first class aggregate, we flatten it and
906 // pass the elements. Either way is semantically identical, but fast-isel
907 // and the optimizer generally likes scalar values better than FCAs.
908 if (const llvm::StructType *STy =
909 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
910 // If the argument and alloca types match up, we don't have to build the
911 // FCA at all, emit a series of GEPs and stores, which is better for
912 // fast isel.
913 if (STy == cast<llvm::PointerType>(V->getType())->getElementType()) {
914 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
915 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000916 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
Chris Lattner309c59f2010-06-29 00:06:42 +0000917 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(V, 0, i);
918 Builder.CreateStore(AI++, EltPtr);
919 }
920 } else {
921 // Reconstruct the FCA here so we can do a coerced store.
922 llvm::Value *FormalArg = llvm::UndefValue::get(STy);
923 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
924 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000925 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
Chris Lattner309c59f2010-06-29 00:06:42 +0000926 FormalArg = Builder.CreateInsertValue(FormalArg, AI++, i);
927 }
928 CreateCoercedStore(FormalArg, V, /*DestIsVolatile=*/false, *this);
929 }
930 } else {
931 // Simple case, just do a coerced store of the argument into the alloca.
932 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000933 AI->setName(Arg->getName() + ".coerce");
Chris Lattner309c59f2010-06-29 00:06:42 +0000934 CreateCoercedStore(AI++, V, /*DestIsVolatile=*/false, *this);
935 }
936
937
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000938 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000939 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000940 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000941 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
942 // This must be a promotion, for something like
943 // "void a(x) short x; {..."
944 V = EmitScalarConversion(V, Ty, Arg->getType());
945 }
946 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000947 EmitParmDecl(*Arg, V);
Chris Lattnerce700162010-06-28 23:44:11 +0000948 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000949 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000950 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000951
952 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000953 }
954 assert(AI == Fn->arg_end() && "Argument mismatch!");
955}
956
Chris Lattner35b21b82010-06-27 01:06:27 +0000957void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000958 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000959 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000960 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000961 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000962 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000963
964 llvm::Value *RV = 0;
965 QualType RetTy = FI.getReturnType();
966 const ABIArgInfo &RetAI = FI.getReturnInfo();
967
968 switch (RetAI.getKind()) {
969 case ABIArgInfo::Indirect:
970 if (RetTy->isAnyComplexType()) {
971 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
972 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
973 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
974 // Do nothing; aggregrates get evaluated directly into the destination.
975 } else {
976 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
977 false, RetTy);
978 }
979 break;
980
981 case ABIArgInfo::Extend:
Chris Lattner35b21b82010-06-27 01:06:27 +0000982 case ABIArgInfo::Direct: {
983 // The internal return value temp always will have pointer-to-return-type
984 // type, just do a load.
985
986 // If the instruction right before the insertion point is a store to the
987 // return value, we can elide the load, zap the store, and usually zap the
988 // alloca.
989 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
990 llvm::StoreInst *SI = 0;
991 if (InsertBB->empty() ||
992 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
993 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
994 RV = Builder.CreateLoad(ReturnValue);
995 } else {
996 // Get the stored value and nuke the now-dead store.
997 RV = SI->getValueOperand();
998 SI->eraseFromParent();
999
1000 // If that was the only use of the return value, nuke it as well now.
1001 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1002 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1003 ReturnValue = 0;
1004 }
1005 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001006 break;
Chris Lattner35b21b82010-06-27 01:06:27 +00001007 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001008 case ABIArgInfo::Ignore:
1009 break;
1010
1011 case ABIArgInfo::Coerce:
1012 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
1013 break;
1014
1015 case ABIArgInfo::Expand:
1016 assert(0 && "Invalid ABI kind for return argument");
1017 }
1018
1019 if (RV)
1020 Builder.CreateRet(RV);
1021 else
1022 Builder.CreateRetVoid();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001023}
1024
John McCall27360712010-05-26 22:34:26 +00001025RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1026 // StartFunction converted the ABI-lowered parameter(s) into a
1027 // local alloca. We need to turn that into an r-value suitable
1028 // for EmitCall.
1029 llvm::Value *Local = GetAddrOfLocalVar(Param);
1030
1031 QualType ArgType = Param->getType();
1032
1033 // For the most part, we just need to load the alloca, except:
1034 // 1) aggregate r-values are actually pointers to temporaries, and
1035 // 2) references to aggregates are pointers directly to the aggregate.
1036 // I don't know why references to non-aggregates are different here.
1037 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1038 if (hasAggregateLLVMType(RefType->getPointeeType()))
1039 return RValue::getAggregate(Local);
1040
1041 // Locals which are references to scalars are represented
1042 // with allocas holding the pointer.
1043 return RValue::get(Builder.CreateLoad(Local));
1044 }
1045
1046 if (ArgType->isAnyComplexType())
1047 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1048
1049 if (hasAggregateLLVMType(ArgType))
1050 return RValue::getAggregate(Local);
1051
1052 return RValue::get(EmitLoadOfScalar(Local, false, ArgType));
1053}
1054
Anders Carlsson0139bb92009-04-08 20:47:54 +00001055RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00001056 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +00001057 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Anders Carlsson0139bb92009-04-08 20:47:54 +00001059 return EmitAnyExprToTemp(E);
1060}
1061
Daniel Dunbar88b53962009-02-02 22:03:45 +00001062RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001063 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001064 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001065 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001066 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001067 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001068 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001069 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001070
1071 // Handle struct-return functions by passing a pointer to the
1072 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001073 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001074 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001075
1076
Chris Lattner5db7ae52009-06-13 00:26:38 +00001077 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001078 // alloca to hold the result, unless one is given to us.
1079 if (CGM.ReturnTypeUsesSret(CallInfo)) {
1080 llvm::Value *Value = ReturnValue.getValue();
1081 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001082 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001083 Args.push_back(Value);
1084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001086 assert(CallInfo.arg_size() == CallArgs.size() &&
1087 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001088 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001089 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001090 I != E; ++I, ++info_it) {
1091 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001092 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001093
1094 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001095 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001096 if (RV.isScalar() || RV.isComplex()) {
1097 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001098 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001099 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001100 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001101 else
Mike Stump1eb44332009-09-09 15:08:12 +00001102 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001103 } else {
1104 Args.push_back(RV.getAggregateAddr());
1105 }
1106 break;
1107
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001108 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001109 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001110 if (RV.isScalar()) {
1111 Args.push_back(RV.getScalarVal());
1112 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001113 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1114 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1115 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1116 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001117 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001118 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001119 }
1120 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Daniel Dunbar11434922009-01-26 21:26:08 +00001122 case ABIArgInfo::Ignore:
1123 break;
1124
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001125 case ABIArgInfo::Coerce: {
1126 // FIXME: Avoid the conversion through memory if possible.
1127 llvm::Value *SrcPtr;
1128 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001129 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001130 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001131 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001132 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001133 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001134 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001135 SrcPtr = RV.getAggregateAddr();
Chris Lattnerce700162010-06-28 23:44:11 +00001136
Chris Lattnerce700162010-06-28 23:44:11 +00001137 // If the coerce-to type is a first class aggregate, we flatten it and
1138 // pass the elements. Either way is semantically identical, but fast-isel
1139 // and the optimizer generally likes scalar values better than FCAs.
1140 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001141 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
1142 // If the argument and alloca types match up, we don't have to build the
1143 // FCA at all, emit a series of GEPs and loads, which is better for
1144 // fast isel.
1145 if (STy ==cast<llvm::PointerType>(SrcPtr->getType())->getElementType()){
1146 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1147 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1148 Args.push_back(Builder.CreateLoad(EltPtr));
1149 }
1150 } else {
1151 // Otherwise, do a coerced load the entire FCA and handle the pieces.
1152 llvm::Value *SrcVal =
1153 CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), *this);
1154
1155 // Extract the elements of the value to pass in.
1156 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1157 Args.push_back(Builder.CreateExtractValue(SrcVal, i));
1158 }
Chris Lattnerce700162010-06-28 23:44:11 +00001159 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001160 // In the simple case, just pass the coerced loaded value.
1161 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1162 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001163 }
1164
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001165 break;
1166 }
1167
Daniel Dunbar56273772008-09-17 00:51:38 +00001168 case ABIArgInfo::Expand:
1169 ExpandTypeToArgs(I->second, RV, Args);
1170 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001171 }
1172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Chris Lattner5db7ae52009-06-13 00:26:38 +00001174 // If the callee is a bitcast of a function to a varargs pointer to function
1175 // type, check to see if we can remove the bitcast. This handles some cases
1176 // with unprototyped functions.
1177 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1178 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1179 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1180 const llvm::FunctionType *CurFT =
1181 cast<llvm::FunctionType>(CurPT->getElementType());
1182 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattner5db7ae52009-06-13 00:26:38 +00001184 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1185 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001186 ActualFT->getNumParams() == CurFT->getNumParams() &&
1187 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001188 bool ArgsMatch = true;
1189 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1190 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1191 ArgsMatch = false;
1192 break;
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Chris Lattner5db7ae52009-06-13 00:26:38 +00001195 // Strip the cast if we can get away with it. This is a nice cleanup,
1196 // but also allows us to inline the function at -O0 if it is marked
1197 // always_inline.
1198 if (ArgsMatch)
1199 Callee = CalleeF;
1200 }
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001203
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001204 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001205 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001206 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001207 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001208 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1209 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Daniel Dunbard14151d2009-03-02 04:32:35 +00001211 llvm::CallSite CS;
1212 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001213 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001214 } else {
1215 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001216 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001217 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001218 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001219 }
Chris Lattnerce933992010-06-29 16:40:28 +00001220 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001221 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001222
Daniel Dunbard14151d2009-03-02 04:32:35 +00001223 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001224 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001225
1226 // If the call doesn't return, finish the basic block and clear the
1227 // insertion point; this allows the rest of IRgen to discard
1228 // unreachable code.
1229 if (CS.doesNotReturn()) {
1230 Builder.CreateUnreachable();
1231 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Mike Stumpf5408fe2009-05-16 07:57:57 +00001233 // FIXME: For now, emit a dummy basic block because expr emitters in
1234 // generally are not ready to handle emitting expressions at unreachable
1235 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001236 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Daniel Dunbard14151d2009-03-02 04:32:35 +00001238 // Return a reasonable RValue.
1239 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001240 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001241
1242 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001243 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001244 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001245
1246 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001247 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001248 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001249 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001250 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001251 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001252 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001253
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001254 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001255 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001256 if (RetTy->isAnyComplexType()) {
1257 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1258 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1259 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001260 }
1261 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001262 llvm::Value *DestPtr = ReturnValue.getValue();
1263 bool DestIsVolatile = ReturnValue.isVolatile();
1264
1265 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001266 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001267 DestIsVolatile = false;
1268 }
1269 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1270 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001271 }
1272 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001273
Daniel Dunbar11434922009-01-26 21:26:08 +00001274 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001275 // If we are ignoring an argument that had a result, make sure to
1276 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001277 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001278
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001279 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001280 llvm::Value *DestPtr = ReturnValue.getValue();
1281 bool DestIsVolatile = ReturnValue.isVolatile();
1282
1283 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001284 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001285 DestIsVolatile = false;
1286 }
1287
1288 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001289 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001290 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001291 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001292 return RValue::getAggregate(DestPtr);
1293 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001294 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001295
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001296 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001297 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001298 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001299
1300 assert(0 && "Unhandled ABIArgInfo::Kind");
1301 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001302}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001303
1304/* VarArg handling */
1305
1306llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1307 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1308}