blob: ec865e58c6dd2ebf80ec7dbe2f173fe41219fdc5 [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 Carruth06057ce2010-06-15 23:19:56 +000022#include "clang/Frontend/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;
Douglas Gregorf813a2c2010-05-18 16:57:00 +000041 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
John McCall04a67a62010-02-05 21:31:56 +000042 }
43}
44
John McCall0b0ef0a2010-02-24 07:14:12 +000045/// Derives the 'this' type for codegen purposes, i.e. ignoring method
46/// qualification.
47/// FIXME: address space qualification?
John McCallead608a2010-02-26 00:48:12 +000048static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
49 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
50 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000051}
52
John McCall0b0ef0a2010-02-24 07:14:12 +000053/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000054static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
55 return MD->getType()->getCanonicalTypeUnqualified()
56 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-02-24 07:14:12 +000057}
58
59/// Returns the "extra-canonicalized" return type, which discards
60/// qualifiers on the return type. Codegen doesn't care about them,
61/// and it makes ABI code a little easier to be able to assume that
62/// all parameter and return types are top-level unqualified.
John McCallead608a2010-02-26 00:48:12 +000063static CanQualType GetReturnType(QualType RetTy) {
64 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000065}
66
67const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000068CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
69 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
70 llvm::SmallVector<CanQualType, 16>(),
Rafael Espindola264ba482010-03-30 20:24:48 +000071 FTNP->getExtInfo());
John McCall0b0ef0a2010-02-24 07:14:12 +000072}
73
74/// \param Args - contains any initial parameters besides those
75/// in the formal type
76static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000077 llvm::SmallVectorImpl<CanQualType> &ArgTys,
78 CanQual<FunctionProtoType> FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000080 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000082 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
83 return CGT.getFunctionInfo(ResTy, ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +000084 FTP->getExtInfo());
John McCall0b0ef0a2010-02-24 07:14:12 +000085}
86
87const CGFunctionInfo &
John McCallead608a2010-02-26 00:48:12 +000088CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
89 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +000090 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000091}
92
John McCall04a67a62010-02-05 21:31:56 +000093static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000094 // Set the appropriate calling convention for the Function.
95 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000096 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000097
98 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000099 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000100
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000101 if (D->hasAttr<ThisCallAttr>())
102 return CC_X86ThisCall;
103
John McCall04a67a62010-02-05 21:31:56 +0000104 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000105}
106
Anders Carlsson375c31c2009-10-03 19:43:08 +0000107const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
108 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000109 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000110
Anders Carlsson375c31c2009-10-03 19:43:08 +0000111 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000112 ArgTys.push_back(GetThisType(Context, RD));
113
114 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000115 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000116}
117
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000118const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000119 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000120
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000121 // Add the 'this' pointer unless this is a static method.
122 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000123 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000124
John McCall0b0ef0a2010-02-24 07:14:12 +0000125 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000126}
127
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000128const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
129 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000130 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000131
132 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000133 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000134
135 // Check if we need to add a VTT parameter (which has type void **).
136 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
137 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000138
139 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000140}
141
142const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
143 CXXDtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000144 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000145
146 // Add the 'this' pointer.
John McCallead608a2010-02-26 00:48:12 +0000147 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000148
149 // Check if we need to add a VTT parameter (which has type void **).
150 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
151 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000152
153 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000154}
155
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000156const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000157 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000158 if (MD->isInstance())
159 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000160
John McCallead608a2010-02-26 00:48:12 +0000161 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
162 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000163 if (isa<FunctionNoProtoType>(FTy))
John McCallead608a2010-02-26 00:48:12 +0000164 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
165 assert(isa<FunctionProtoType>(FTy));
166 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000167}
168
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000169const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000170 llvm::SmallVector<CanQualType, 16> ArgTys;
171 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
172 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000173 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000174 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000175 e = MD->param_end(); i != e; ++i) {
176 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
177 }
178 return getFunctionInfo(GetReturnType(MD->getResultType()),
179 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000180 FunctionType::ExtInfo(
181 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000182 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000183 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000184}
185
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000186const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
187 // FIXME: Do we need to handle ObjCMethodDecl?
188 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
189
190 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
191 return getFunctionInfo(CD, GD.getCtorType());
192
193 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
194 return getFunctionInfo(DD, GD.getDtorType());
195
196 return getFunctionInfo(FD);
197}
198
Mike Stump1eb44332009-09-09 15:08:12 +0000199const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000200 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000201 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000202 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000203 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000204 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000205 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000206 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000207 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000208}
209
Mike Stump1eb44332009-09-09 15:08:12 +0000210const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000211 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000212 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000213 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000214 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000215 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000216 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000217 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000218 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000219}
220
John McCallead608a2010-02-26 00:48:12 +0000221const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
222 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000223 const FunctionType::ExtInfo &Info) {
John McCallead608a2010-02-26 00:48:12 +0000224#ifndef NDEBUG
225 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
226 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
227 assert(I->isCanonicalAsParam());
228#endif
229
Rafael Espindola425ef722010-03-30 22:15:11 +0000230 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000231
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000232 // Lookup or create unique function info.
233 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000234 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000235 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000236
237 void *InsertPos = 0;
238 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
239 if (FI)
240 return *FI;
241
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000242 // Construct the function info.
Rafael Espindola425ef722010-03-30 22:15:11 +0000243 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000244 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000245
246 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000247 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000248
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000249 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000250}
251
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000252CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000253 bool _NoReturn,
Rafael Espindola425ef722010-03-30 22:15:11 +0000254 unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000255 CanQualType ResTy,
256 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000257 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000258 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000259 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000260{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000261 NumArgs = ArgTys.size();
262 Args = new ArgInfo[1 + NumArgs];
263 Args[0].type = ResTy;
264 for (unsigned i = 0; i < NumArgs; ++i)
265 Args[1 + i].type = ArgTys[i];
266}
267
268/***/
269
Mike Stump1eb44332009-09-09 15:08:12 +0000270void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000271 std::vector<const llvm::Type*> &ArgTys) {
272 const RecordType *RT = Ty->getAsStructureType();
273 assert(RT && "Can only expand structure types.");
274 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000275 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000276 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000278 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
279 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000280 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000281 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000282 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Daniel Dunbar56273772008-09-17 00:51:38 +0000284 QualType FT = FD->getType();
285 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
286 GetExpandedTypes(FT, ArgTys);
287 } else {
288 ArgTys.push_back(ConvertType(FT));
289 }
290 }
291}
292
Mike Stump1eb44332009-09-09 15:08:12 +0000293llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000294CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
295 llvm::Function::arg_iterator AI) {
296 const RecordType *RT = Ty->getAsStructureType();
297 assert(RT && "Can only expand structure types.");
298
299 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000300 assert(LV.isSimple() &&
301 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000302 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000303 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
304 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000305 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000306 QualType FT = FD->getType();
307
308 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000309 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000310 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
311 AI = ExpandTypeFromArgs(FT, LV, AI);
312 } else {
313 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
314 ++AI;
315 }
316 }
317
318 return AI;
319}
320
Mike Stump1eb44332009-09-09 15:08:12 +0000321void
322CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000323 llvm::SmallVector<llvm::Value*, 16> &Args) {
324 const RecordType *RT = Ty->getAsStructureType();
325 assert(RT && "Can only expand structure types.");
326
327 RecordDecl *RD = RT->getDecl();
328 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
329 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000330 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
331 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000332 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000333 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Daniel Dunbar56273772008-09-17 00:51:38 +0000335 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000336 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000337 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
338 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
339 } else {
340 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000341 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000342 "Unexpected non-scalar rvalue during struct expansion.");
343 Args.push_back(RV.getScalarVal());
344 }
345 }
346}
347
Chris Lattnere7bb7772010-06-27 06:04:18 +0000348/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000349/// accessing some number of bytes out of it, try to gep into the struct to get
350/// at its inner goodness. Dive as deep as possible without entering an element
351/// with an in-memory size smaller than DstSize.
352static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000353EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
354 const llvm::StructType *SrcSTy,
355 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000356 // We can't dive into a zero-element struct.
357 if (SrcSTy->getNumElements() == 0) return SrcPtr;
358
359 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
360
361 // If the first elt is at least as large as what we're looking for, or if the
362 // first element is the same size as the whole struct, we can enter it.
363 uint64_t FirstEltSize =
364 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
365 if (FirstEltSize < DstSize &&
366 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
367 return SrcPtr;
368
369 // GEP into the first element.
370 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
371
372 // If the first element is a struct, recurse.
373 const llvm::Type *SrcTy =
374 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
375 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000376 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000377
378 return SrcPtr;
379}
380
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000381/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
382/// are either integers or pointers. This does a truncation of the value if it
383/// is too large or a zero extension if it is too small.
384static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
385 const llvm::Type *Ty,
386 CodeGenFunction &CGF) {
387 if (Val->getType() == Ty)
388 return Val;
389
390 if (isa<llvm::PointerType>(Val->getType())) {
391 // If this is Pointer->Pointer avoid conversion to and from int.
392 if (isa<llvm::PointerType>(Ty))
393 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
394
395 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000396 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000397 }
398
399 const llvm::Type *DestIntTy = Ty;
400 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000401 DestIntTy = CGF.IntPtrTy;
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000402
403 if (Val->getType() != DestIntTy)
404 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
405
406 if (isa<llvm::PointerType>(Ty))
407 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
408 return Val;
409}
410
Chris Lattner08dd2a02010-06-27 05:56:15 +0000411
412
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000413/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
414/// a pointer to an object of type \arg Ty.
415///
416/// This safely handles the case when the src type is smaller than the
417/// destination type; in this situation the values of bits which not
418/// present in the src are undefined.
419static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
420 const llvm::Type *Ty,
421 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000422 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000423 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000424 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000425
426 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000427 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000428 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
429 }
430
431 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000432
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000433 // If the source and destination are integer or pointer types, just do an
434 // extension or truncation to the desired type.
435 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
436 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
437 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
438 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
439 }
440
Daniel Dunbarb225be42009-02-03 05:59:18 +0000441 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000442 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000443 // Generally SrcSize is never greater than DstSize, since this means we are
444 // losing bits. However, this can happen in cases where the structure has
445 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000446 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000447 // FIXME: Assert that we aren't truncating non-padding bits when have access
448 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000449 llvm::Value *Casted =
450 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000451 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
452 // FIXME: Use better alignment / avoid requiring aligned load.
453 Load->setAlignment(1);
454 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000455 }
Chris Lattner35b21b82010-06-27 01:06:27 +0000456
457 // Otherwise do coercion through memory. This is stupid, but
458 // simple.
459 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
460 llvm::Value *Casted =
461 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
462 llvm::StoreInst *Store =
463 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
464 // FIXME: Use better alignment / avoid requiring aligned store.
465 Store->setAlignment(1);
466 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000467}
468
469/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
470/// where the source and destination may have different types.
471///
472/// This safely handles the case when the src type is larger than the
473/// destination type; the upper bits of the src will be lost.
474static void CreateCoercedStore(llvm::Value *Src,
475 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000476 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000477 CodeGenFunction &CGF) {
478 const llvm::Type *SrcTy = Src->getType();
Chris Lattnere7bb7772010-06-27 06:04:18 +0000479 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
480
Mike Stump1eb44332009-09-09 15:08:12 +0000481 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000482 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
483
Chris Lattnere7bb7772010-06-27 06:04:18 +0000484 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
485 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
486 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
487 }
488
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000489 // If the source and destination are integer or pointer types, just do an
490 // extension or truncation to the desired type.
491 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
492 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
493 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
494 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
495 return;
496 }
497
Duncan Sands9408c452009-05-09 07:08:47 +0000498 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000499
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000500 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000501 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000502 llvm::Value *Casted =
503 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000504 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000505 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000506 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000507 // Otherwise do coercion through memory. This is stupid, but
508 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000509
510 // Generally SrcSize is never greater than DstSize, since this means we are
511 // losing bits. However, this can happen in cases where the structure has
512 // additional padding, for example due to a user specified alignment.
513 //
514 // FIXME: Assert that we aren't truncating non-padding bits when have access
515 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000516 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
517 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000518 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000519 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000520 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
521 // FIXME: Use better alignment / avoid requiring aligned load.
522 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000523 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000524 }
525}
526
Daniel Dunbar56273772008-09-17 00:51:38 +0000527/***/
528
Daniel Dunbar88b53962009-02-02 22:03:45 +0000529bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000530 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000531}
532
John McCallc0bf4622010-02-23 00:48:20 +0000533const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
534 const CGFunctionInfo &FI = getFunctionInfo(GD);
535
536 // For definition purposes, don't consider a K&R function variadic.
537 bool Variadic = false;
538 if (const FunctionProtoType *FPT =
539 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
540 Variadic = FPT->isVariadic();
541
542 return GetFunctionType(FI, Variadic);
543}
544
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000545const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000546CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000547 std::vector<const llvm::Type*> ArgTys;
548
549 const llvm::Type *ResultType = 0;
550
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000551 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000552 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000553 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000554 case ABIArgInfo::Expand:
555 assert(0 && "Invalid ABI kind for return argument");
556
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000557 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000558 case ABIArgInfo::Direct:
559 ResultType = ConvertType(RetTy);
560 break;
561
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000562 case ABIArgInfo::Indirect: {
563 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000564 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000565 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000566 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
567 break;
568 }
569
Daniel Dunbar11434922009-01-26 21:26:08 +0000570 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000571 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000572 break;
573
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000574 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000575 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000576 break;
577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000580 ie = FI.arg_end(); it != ie; ++it) {
581 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000583 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000584 case ABIArgInfo::Ignore:
585 break;
586
Daniel Dunbar56273772008-09-17 00:51:38 +0000587 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000588 ArgTys.push_back(AI.getCoerceToType());
589 break;
590
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000591 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000592 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000593 const llvm::Type *LTy = ConvertTypeForMem(it->type);
594 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000595 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000596 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000597
598 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000599 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000600 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000601 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000603 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000604 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000605 break;
606 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000607 }
608
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000609 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000610}
611
Anders Carlssonecf282b2009-11-24 05:08:52 +0000612const llvm::Type *
Anders Carlsson046c2942010-04-17 20:15:18 +0000613CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlssonecf282b2009-11-24 05:08:52 +0000614 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
615
Eli Friedmanc00129a2010-05-30 06:03:20 +0000616 if (!VerifyFuncTypeComplete(FPT))
Anders Carlssonecf282b2009-11-24 05:08:52 +0000617 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
618
619 return llvm::OpaqueType::get(getLLVMContext());
620}
621
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000622void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000623 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000624 AttributeListType &PAL,
625 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000626 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000627 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000628
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000629 CallingConv = FI.getEffectiveCallingConvention();
630
John McCall04a67a62010-02-05 21:31:56 +0000631 if (FI.isNoReturn())
632 FuncAttrs |= llvm::Attribute::NoReturn;
633
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000634 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000635 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000636 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000637 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000638 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000639 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000640 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000641 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000642 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000643 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000644 if (TargetDecl->hasAttr<MallocAttr>())
645 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000646 }
647
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000648 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000649 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000650 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000651 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000652 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000653 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000654
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000655 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000656 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000657 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000658 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000659 case ABIArgInfo::Extend:
660 if (RetTy->isSignedIntegerType()) {
661 RetAttrs |= llvm::Attribute::SExt;
662 } else if (RetTy->isUnsignedIntegerType()) {
663 RetAttrs |= llvm::Attribute::ZExt;
664 }
665 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000666 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000667 break;
668
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000669 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000670 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000671 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000672 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000673 // sret disables readnone and readonly
674 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
675 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000676 break;
677
Daniel Dunbar11434922009-01-26 21:26:08 +0000678 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000679 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000680 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000681
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000682 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000683 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000684 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000685
Devang Patela2c69122008-09-26 22:53:57 +0000686 if (RetAttrs)
687 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000688
689 // FIXME: we need to honour command line settings also...
690 // FIXME: RegParm should be reduced in case of nested functions and/or global
691 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000692 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000693
694 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000695 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000696 ie = FI.arg_end(); it != ie; ++it) {
697 QualType ParamType = it->type;
698 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000699 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000700
John McCalld8e10d22010-03-27 00:47:27 +0000701 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
702 // have the corresponding parameter variable. It doesn't make
703 // sense to do it here because parameters are so fucked up.
Nuno Lopes079b4952009-12-07 18:30:06 +0000704
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000705 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000706 case ABIArgInfo::Coerce:
707 break;
708
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000709 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000710 if (AI.getIndirectByVal())
711 Attributes |= llvm::Attribute::ByVal;
712
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000713 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000714 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000715 // byval disables readnone and readonly.
716 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
717 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000718 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000719
720 case ABIArgInfo::Extend:
721 if (ParamType->isSignedIntegerType()) {
722 Attributes |= llvm::Attribute::SExt;
723 } else if (ParamType->isUnsignedIntegerType()) {
724 Attributes |= llvm::Attribute::ZExt;
725 }
726 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000727 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000728 if (RegParm > 0 &&
729 (ParamType->isIntegerType() || ParamType->isPointerType())) {
730 RegParm -=
731 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
732 if (RegParm >= 0)
733 Attributes |= llvm::Attribute::InReg;
734 }
735 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000736 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000737
Daniel Dunbar11434922009-01-26 21:26:08 +0000738 case ABIArgInfo::Ignore:
739 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000740 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000741
Daniel Dunbar56273772008-09-17 00:51:38 +0000742 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000743 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000744 // FIXME: This is rather inefficient. Do we ever actually need to do
745 // anything here? The result should be just reconstructed on the other
746 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000747 getTypes().GetExpandedTypes(ParamType, Tys);
748 Index += Tys.size();
749 continue;
750 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000751 }
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Devang Patel761d7f72008-09-25 21:02:23 +0000753 if (Attributes)
754 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000755 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000756 }
Devang Patela2c69122008-09-26 22:53:57 +0000757 if (FuncAttrs)
758 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000759}
760
Daniel Dunbar88b53962009-02-02 22:03:45 +0000761void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
762 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000763 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000764 // If this is an implicit-return-zero function, go ahead and
765 // initialize the return value. TODO: it might be nice to have
766 // a more general mechanism for this that didn't require synthesized
767 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000768 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000769 if (FD->hasImplicitReturnZero()) {
770 QualType RetTy = FD->getResultType().getUnqualifiedType();
771 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000772 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000773 Builder.CreateStore(Zero, ReturnValue);
774 }
775 }
776
Mike Stumpf5408fe2009-05-16 07:57:57 +0000777 // FIXME: We no longer need the types from FunctionArgList; lift up and
778 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000779
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000780 // Emit allocs for param decls. Give the LLVM Argument nodes names.
781 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000783 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000784 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000785 AI->setName("agg.result");
786 ++AI;
787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000789 assert(FI.arg_size() == Args.size() &&
790 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000791 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000792 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000793 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000794 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000795 QualType Ty = info_it->type;
796 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000797
798 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000799 case ABIArgInfo::Indirect: {
800 llvm::Value* V = AI;
801 if (hasAggregateLLVMType(Ty)) {
802 // Do nothing, aggregates and complex variables are accessed by
803 // reference.
804 } else {
805 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000806 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000807 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
808 // This must be a promotion, for something like
809 // "void a(x) short x; {..."
810 V = EmitScalarConversion(V, Ty, Arg->getType());
811 }
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000814 break;
815 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000816
817 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000818 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000819 assert(AI != Fn->arg_end() && "Argument mismatch!");
820 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000821 if (hasAggregateLLVMType(Ty)) {
822 // Create a temporary alloca to hold the argument; the rest of
823 // codegen expects to access aggregates & complex values by
824 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000825 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000826 Builder.CreateStore(AI, V);
827 } else {
John McCalld8e10d22010-03-27 00:47:27 +0000828 if (Arg->getType().isRestrictQualified())
829 AI->addAttr(llvm::Attribute::NoAlias);
830
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000831 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
832 // This must be a promotion, for something like
833 // "void a(x) short x; {..."
834 V = EmitScalarConversion(V, Ty, Arg->getType());
835 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000836 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000837 EmitParmDecl(*Arg, V);
838 break;
839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Daniel Dunbar56273772008-09-17 00:51:38 +0000841 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000842 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000843 // we need to create a temporary and reconstruct it from the
844 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000845 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000846 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000847 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000848 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000849 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000850
Daniel Dunbar56273772008-09-17 00:51:38 +0000851 // Name the arguments used in expansion and increment AI.
852 unsigned Index = 0;
853 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000854 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000855 continue;
856 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000857
858 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000859 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000860 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000861 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000862 } else {
863 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000866 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000867 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000868
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000869 case ABIArgInfo::Coerce: {
870 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000871 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
872 // result in a new alloca anyway, so we could just store into that
873 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000874 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000875 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000876 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000877 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000878 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000879 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
880 // This must be a promotion, for something like
881 // "void a(x) short x; {..."
882 V = EmitScalarConversion(V, Ty, Arg->getType());
883 }
884 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000885 EmitParmDecl(*Arg, V);
886 break;
887 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000888 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000889
890 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000891 }
892 assert(AI == Fn->arg_end() && "Argument mismatch!");
893}
894
Chris Lattner35b21b82010-06-27 01:06:27 +0000895void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000896 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000897 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000898 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000899 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000900 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000901
902 llvm::Value *RV = 0;
903 QualType RetTy = FI.getReturnType();
904 const ABIArgInfo &RetAI = FI.getReturnInfo();
905
906 switch (RetAI.getKind()) {
907 case ABIArgInfo::Indirect:
908 if (RetTy->isAnyComplexType()) {
909 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
910 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
911 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
912 // Do nothing; aggregrates get evaluated directly into the destination.
913 } else {
914 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
915 false, RetTy);
916 }
917 break;
918
919 case ABIArgInfo::Extend:
Chris Lattner35b21b82010-06-27 01:06:27 +0000920 case ABIArgInfo::Direct: {
921 // The internal return value temp always will have pointer-to-return-type
922 // type, just do a load.
923
924 // If the instruction right before the insertion point is a store to the
925 // return value, we can elide the load, zap the store, and usually zap the
926 // alloca.
927 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
928 llvm::StoreInst *SI = 0;
929 if (InsertBB->empty() ||
930 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
931 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
932 RV = Builder.CreateLoad(ReturnValue);
933 } else {
934 // Get the stored value and nuke the now-dead store.
935 RV = SI->getValueOperand();
936 SI->eraseFromParent();
937
938 // If that was the only use of the return value, nuke it as well now.
939 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
940 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
941 ReturnValue = 0;
942 }
943 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000944 break;
Chris Lattner35b21b82010-06-27 01:06:27 +0000945 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000946 case ABIArgInfo::Ignore:
947 break;
948
949 case ABIArgInfo::Coerce:
950 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
951 break;
952
953 case ABIArgInfo::Expand:
954 assert(0 && "Invalid ABI kind for return argument");
955 }
956
957 if (RV)
958 Builder.CreateRet(RV);
959 else
960 Builder.CreateRetVoid();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000961}
962
John McCall27360712010-05-26 22:34:26 +0000963RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
964 // StartFunction converted the ABI-lowered parameter(s) into a
965 // local alloca. We need to turn that into an r-value suitable
966 // for EmitCall.
967 llvm::Value *Local = GetAddrOfLocalVar(Param);
968
969 QualType ArgType = Param->getType();
970
971 // For the most part, we just need to load the alloca, except:
972 // 1) aggregate r-values are actually pointers to temporaries, and
973 // 2) references to aggregates are pointers directly to the aggregate.
974 // I don't know why references to non-aggregates are different here.
975 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
976 if (hasAggregateLLVMType(RefType->getPointeeType()))
977 return RValue::getAggregate(Local);
978
979 // Locals which are references to scalars are represented
980 // with allocas holding the pointer.
981 return RValue::get(Builder.CreateLoad(Local));
982 }
983
984 if (ArgType->isAnyComplexType())
985 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
986
987 if (hasAggregateLLVMType(ArgType))
988 return RValue::getAggregate(Local);
989
990 return RValue::get(EmitLoadOfScalar(Local, false, ArgType));
991}
992
Anders Carlsson0139bb92009-04-08 20:47:54 +0000993RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000994 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +0000995 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Anders Carlsson0139bb92009-04-08 20:47:54 +0000997 return EmitAnyExprToTemp(E);
998}
999
Daniel Dunbar88b53962009-02-02 22:03:45 +00001000RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001001 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001002 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001003 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001004 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001005 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001006 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001007 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001008
1009 // Handle struct-return functions by passing a pointer to the
1010 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001011 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001012 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001013
1014
Chris Lattner5db7ae52009-06-13 00:26:38 +00001015 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001016 // alloca to hold the result, unless one is given to us.
1017 if (CGM.ReturnTypeUsesSret(CallInfo)) {
1018 llvm::Value *Value = ReturnValue.getValue();
1019 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001020 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001021 Args.push_back(Value);
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001024 assert(CallInfo.arg_size() == CallArgs.size() &&
1025 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001026 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001027 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001028 I != E; ++I, ++info_it) {
1029 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001030 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001031
1032 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001033 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001034 if (RV.isScalar() || RV.isComplex()) {
1035 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001036 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001037 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001038 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001039 else
Mike Stump1eb44332009-09-09 15:08:12 +00001040 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001041 } else {
1042 Args.push_back(RV.getAggregateAddr());
1043 }
1044 break;
1045
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001046 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001047 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001048 if (RV.isScalar()) {
1049 Args.push_back(RV.getScalarVal());
1050 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001051 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1052 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1053 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1054 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001055 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001056 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001057 }
1058 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Daniel Dunbar11434922009-01-26 21:26:08 +00001060 case ABIArgInfo::Ignore:
1061 break;
1062
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001063 case ABIArgInfo::Coerce: {
1064 // FIXME: Avoid the conversion through memory if possible.
1065 llvm::Value *SrcPtr;
1066 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001067 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001068 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001069 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001070 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001071 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001072 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001073 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +00001074 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001075 *this));
1076 break;
1077 }
1078
Daniel Dunbar56273772008-09-17 00:51:38 +00001079 case ABIArgInfo::Expand:
1080 ExpandTypeToArgs(I->second, RV, Args);
1081 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001082 }
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner5db7ae52009-06-13 00:26:38 +00001085 // If the callee is a bitcast of a function to a varargs pointer to function
1086 // type, check to see if we can remove the bitcast. This handles some cases
1087 // with unprototyped functions.
1088 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1089 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1090 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1091 const llvm::FunctionType *CurFT =
1092 cast<llvm::FunctionType>(CurPT->getElementType());
1093 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner5db7ae52009-06-13 00:26:38 +00001095 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1096 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001097 ActualFT->getNumParams() == CurFT->getNumParams() &&
1098 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001099 bool ArgsMatch = true;
1100 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1101 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1102 ArgsMatch = false;
1103 break;
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner5db7ae52009-06-13 00:26:38 +00001106 // Strip the cast if we can get away with it. This is a nice cleanup,
1107 // but also allows us to inline the function at -O0 if it is marked
1108 // always_inline.
1109 if (ArgsMatch)
1110 Callee = CalleeF;
1111 }
1112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001114
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001115 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001116 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001117 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001118 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001119 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1120 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Daniel Dunbard14151d2009-03-02 04:32:35 +00001122 llvm::CallSite CS;
1123 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001124 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001125 } else {
1126 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001127 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001128 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001129 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001130 }
David Chisnall4b02afc2010-05-02 13:41:58 +00001131 if (callOrInvoke) {
1132 *callOrInvoke = CS.getInstruction();
David Chisnalldd5c98f2010-05-01 11:15:56 +00001133 }
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001134
Daniel Dunbard14151d2009-03-02 04:32:35 +00001135 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001136 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001137
1138 // If the call doesn't return, finish the basic block and clear the
1139 // insertion point; this allows the rest of IRgen to discard
1140 // unreachable code.
1141 if (CS.doesNotReturn()) {
1142 Builder.CreateUnreachable();
1143 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Mike Stumpf5408fe2009-05-16 07:57:57 +00001145 // FIXME: For now, emit a dummy basic block because expr emitters in
1146 // generally are not ready to handle emitting expressions at unreachable
1147 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001148 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Daniel Dunbard14151d2009-03-02 04:32:35 +00001150 // Return a reasonable RValue.
1151 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001152 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001153
1154 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001155 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001156 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001157
1158 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001159 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001160 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001161 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001162 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001163 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001164 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001165
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001166 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001167 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001168 if (RetTy->isAnyComplexType()) {
1169 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1170 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1171 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001172 }
1173 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001174 llvm::Value *DestPtr = ReturnValue.getValue();
1175 bool DestIsVolatile = ReturnValue.isVolatile();
1176
1177 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001178 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001179 DestIsVolatile = false;
1180 }
1181 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1182 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001183 }
1184 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001185
Daniel Dunbar11434922009-01-26 21:26:08 +00001186 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001187 // If we are ignoring an argument that had a result, make sure to
1188 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001189 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001190
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001191 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001192 llvm::Value *DestPtr = ReturnValue.getValue();
1193 bool DestIsVolatile = ReturnValue.isVolatile();
1194
1195 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001196 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001197 DestIsVolatile = false;
1198 }
1199
1200 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001201 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001202 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001203 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001204 return RValue::getAggregate(DestPtr);
1205 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001206 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001207
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001208 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001209 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001210 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001211
1212 assert(0 && "Unhandled ABIArgInfo::Kind");
1213 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001214}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001215
1216/* VarArg handling */
1217
1218llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1219 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1220}