blob: cbcd3d12337da223991eb3a6a386122b192d8330 [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 &
Chris Lattner376fe5e2010-06-29 17:56:33 +000064CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
65 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000066 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
67 llvm::SmallVector<CanQualType, 16>(),
Chris Lattner376fe5e2010-06-29 17:56:33 +000068 FTNP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000069}
70
71/// \param Args - contains any initial parameters besides those
72/// in the formal type
73static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000074 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner376fe5e2010-06-29 17:56:33 +000075 CanQual<FunctionProtoType> FTP,
76 bool IsRecursive = false) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000077 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000078 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000080 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Chris Lattner376fe5e2010-06-29 17:56:33 +000081 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000082}
83
84const CGFunctionInfo &
Chris Lattner376fe5e2010-06-29 17:56:33 +000085CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
86 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000087 llvm::SmallVector<CanQualType, 16> ArgTys;
Chris Lattner376fe5e2010-06-29 17:56:33 +000088 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000089}
90
John McCall04a67a62010-02-05 21:31:56 +000091static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000092 // Set the appropriate calling convention for the Function.
93 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000094 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000095
96 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000097 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000098
Douglas Gregorf813a2c2010-05-18 16:57:00 +000099 if (D->hasAttr<ThisCallAttr>())
100 return CC_X86ThisCall;
101
John McCall04a67a62010-02-05 21:31:56 +0000102 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000103}
104
Anders Carlsson375c31c2009-10-03 19:43:08 +0000105const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
106 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000107 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000108
Anders Carlsson375c31c2009-10-03 19:43:08 +0000109 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000110 ArgTys.push_back(GetThisType(Context, RD));
111
112 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000113 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000114}
115
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000116const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000117 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000118
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000119 // Add the 'this' pointer unless this is a static method.
120 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000121 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000122
John McCall0b0ef0a2010-02-24 07:14:12 +0000123 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000124}
125
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000126const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
127 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000128 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000129
130 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000131 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000132
133 // Check if we need to add a VTT parameter (which has type void **).
134 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
135 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000136
137 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000138}
139
140const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
141 CXXDtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000142 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000143
144 // Add the 'this' pointer.
John McCallead608a2010-02-26 00:48:12 +0000145 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000146
147 // Check if we need to add a VTT parameter (which has type void **).
148 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
149 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000150
151 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000152}
153
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000154const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000155 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000156 if (MD->isInstance())
157 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
John McCallead608a2010-02-26 00:48:12 +0000159 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
160 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000161 if (isa<FunctionNoProtoType>(FTy))
John McCallead608a2010-02-26 00:48:12 +0000162 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
163 assert(isa<FunctionProtoType>(FTy));
164 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000165}
166
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000167const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000168 llvm::SmallVector<CanQualType, 16> ArgTys;
169 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
170 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000171 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000172 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000173 e = MD->param_end(); i != e; ++i) {
174 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
175 }
176 return getFunctionInfo(GetReturnType(MD->getResultType()),
177 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000178 FunctionType::ExtInfo(
179 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000180 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000181 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000182}
183
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000184const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
185 // FIXME: Do we need to handle ObjCMethodDecl?
186 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
187
188 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
189 return getFunctionInfo(CD, GD.getCtorType());
190
191 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
192 return getFunctionInfo(DD, GD.getDtorType());
193
194 return getFunctionInfo(FD);
195}
196
Mike Stump1eb44332009-09-09 15:08:12 +0000197const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000198 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000199 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000200 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000201 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000202 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000203 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000204 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000205 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000209 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000210 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000211 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000212 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000213 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000214 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000215 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000216 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000217}
218
John McCallead608a2010-02-26 00:48:12 +0000219const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
220 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner376fe5e2010-06-29 17:56:33 +0000221 const FunctionType::ExtInfo &Info,
222 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +0000223#ifndef NDEBUG
224 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
225 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
226 assert(I->isCanonicalAsParam());
227#endif
228
Rafael Espindola425ef722010-03-30 22:15:11 +0000229 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000230
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000231 // Lookup or create unique function info.
232 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000233 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000234 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000235
236 void *InsertPos = 0;
237 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
238 if (FI)
239 return *FI;
240
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000241 // Construct the function info.
Chris Lattnerce700162010-06-28 23:44:11 +0000242 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
243 ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000244 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000245
Chris Lattner8640cd62010-06-29 01:08:48 +0000246 // ABI lowering wants to know what our preferred type for the argument is in
247 // various situations, pass it in.
248 llvm::SmallVector<const llvm::Type *, 8> PreferredArgTypes;
249 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
Chris Lattner376fe5e2010-06-29 17:56:33 +0000250 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I) {
251 // If this is being called from the guts of the ConvertType loop, make sure
252 // to call ConvertTypeRecursive so we don't get into issues with cyclic
253 // pointer type structures.
254 const llvm::Type *ArgType;
255 if (IsRecursive)
256 ArgType = ConvertTypeRecursive(*I);
257 else
258 ArgType = ConvertType(*I);
259 PreferredArgTypes.push_back(ArgType);
260 }
Chris Lattner8640cd62010-06-29 01:08:48 +0000261
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000262 // Compute ABI information.
Chris Lattner8640cd62010-06-29 01:08:48 +0000263 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext(),
264 PreferredArgTypes.data(), PreferredArgTypes.size());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000265
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000266 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000267}
268
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000269CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCall04a67a62010-02-05 21:31:56 +0000270 bool _NoReturn,
Rafael Espindola425ef722010-03-30 22:15:11 +0000271 unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000272 CanQualType ResTy,
273 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000274 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000275 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000276 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000277{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000278 NumArgs = ArgTys.size();
Chris Lattnerce700162010-06-28 23:44:11 +0000279
280 // FIXME: Coallocate with the CGFunctionInfo object.
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000281 Args = new ArgInfo[1 + NumArgs];
282 Args[0].type = ResTy;
283 for (unsigned i = 0; i < NumArgs; ++i)
284 Args[1 + i].type = ArgTys[i];
285}
286
287/***/
288
Mike Stump1eb44332009-09-09 15:08:12 +0000289void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000290 std::vector<const llvm::Type*> &ArgTys) {
291 const RecordType *RT = Ty->getAsStructureType();
292 assert(RT && "Can only expand structure types.");
293 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000294 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000295 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000297 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
298 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000299 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000300 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000301 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Daniel Dunbar56273772008-09-17 00:51:38 +0000303 QualType FT = FD->getType();
304 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
305 GetExpandedTypes(FT, ArgTys);
306 } else {
307 ArgTys.push_back(ConvertType(FT));
308 }
309 }
310}
311
Mike Stump1eb44332009-09-09 15:08:12 +0000312llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000313CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
314 llvm::Function::arg_iterator AI) {
315 const RecordType *RT = Ty->getAsStructureType();
316 assert(RT && "Can only expand structure types.");
317
318 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000319 assert(LV.isSimple() &&
320 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000321 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000322 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
323 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000324 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000325 QualType FT = FD->getType();
326
327 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000328 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000329 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
330 AI = ExpandTypeFromArgs(FT, LV, AI);
331 } else {
332 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
333 ++AI;
334 }
335 }
336
337 return AI;
338}
339
Mike Stump1eb44332009-09-09 15:08:12 +0000340void
341CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000342 llvm::SmallVector<llvm::Value*, 16> &Args) {
343 const RecordType *RT = Ty->getAsStructureType();
344 assert(RT && "Can only expand structure types.");
345
346 RecordDecl *RD = RT->getDecl();
347 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
348 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000349 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
350 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000351 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000352 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Daniel Dunbar56273772008-09-17 00:51:38 +0000354 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000355 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000356 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
357 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
358 } else {
359 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000360 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000361 "Unexpected non-scalar rvalue during struct expansion.");
362 Args.push_back(RV.getScalarVal());
363 }
364 }
365}
366
Chris Lattnere7bb7772010-06-27 06:04:18 +0000367/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000368/// accessing some number of bytes out of it, try to gep into the struct to get
369/// at its inner goodness. Dive as deep as possible without entering an element
370/// with an in-memory size smaller than DstSize.
371static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000372EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
373 const llvm::StructType *SrcSTy,
374 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000375 // We can't dive into a zero-element struct.
376 if (SrcSTy->getNumElements() == 0) return SrcPtr;
377
378 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
379
380 // If the first elt is at least as large as what we're looking for, or if the
381 // first element is the same size as the whole struct, we can enter it.
382 uint64_t FirstEltSize =
383 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
384 if (FirstEltSize < DstSize &&
385 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
386 return SrcPtr;
387
388 // GEP into the first element.
389 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
390
391 // If the first element is a struct, recurse.
392 const llvm::Type *SrcTy =
393 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
394 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000395 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000396
397 return SrcPtr;
398}
399
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000400/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
401/// are either integers or pointers. This does a truncation of the value if it
402/// is too large or a zero extension if it is too small.
403static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
404 const llvm::Type *Ty,
405 CodeGenFunction &CGF) {
406 if (Val->getType() == Ty)
407 return Val;
408
409 if (isa<llvm::PointerType>(Val->getType())) {
410 // If this is Pointer->Pointer avoid conversion to and from int.
411 if (isa<llvm::PointerType>(Ty))
412 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
413
414 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000415 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000416 }
417
418 const llvm::Type *DestIntTy = Ty;
419 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000420 DestIntTy = CGF.IntPtrTy;
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000421
422 if (Val->getType() != DestIntTy)
423 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
424
425 if (isa<llvm::PointerType>(Ty))
426 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
427 return Val;
428}
429
Chris Lattner08dd2a02010-06-27 05:56:15 +0000430
431
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000432/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
433/// a pointer to an object of type \arg Ty.
434///
435/// This safely handles the case when the src type is smaller than the
436/// destination type; in this situation the values of bits which not
437/// present in the src are undefined.
438static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
439 const llvm::Type *Ty,
440 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000441 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000442 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000443
444 // If SrcTy and Ty are the same, just do a load.
445 if (SrcTy == Ty)
446 return CGF.Builder.CreateLoad(SrcPtr);
447
Duncan Sands9408c452009-05-09 07:08:47 +0000448 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000449
450 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000451 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000452 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
453 }
454
455 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000456
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000457 // If the source and destination are integer or pointer types, just do an
458 // extension or truncation to the desired type.
459 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
460 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
461 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
462 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
463 }
464
Daniel Dunbarb225be42009-02-03 05:59:18 +0000465 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000466 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000467 // Generally SrcSize is never greater than DstSize, since this means we are
468 // losing bits. However, this can happen in cases where the structure has
469 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000470 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000471 // FIXME: Assert that we aren't truncating non-padding bits when have access
472 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000473 llvm::Value *Casted =
474 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000475 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
476 // FIXME: Use better alignment / avoid requiring aligned load.
477 Load->setAlignment(1);
478 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000479 }
Chris Lattner35b21b82010-06-27 01:06:27 +0000480
481 // Otherwise do coercion through memory. This is stupid, but
482 // simple.
483 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
484 llvm::Value *Casted =
485 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
486 llvm::StoreInst *Store =
487 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
488 // FIXME: Use better alignment / avoid requiring aligned store.
489 Store->setAlignment(1);
490 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000491}
492
493/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
494/// where the source and destination may have different types.
495///
496/// This safely handles the case when the src type is larger than the
497/// destination type; the upper bits of the src will be lost.
498static void CreateCoercedStore(llvm::Value *Src,
499 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000500 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000501 CodeGenFunction &CGF) {
502 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000503 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000504 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000505 if (SrcTy == DstTy) {
506 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
507 return;
508 }
509
510 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
511
Chris Lattnere7bb7772010-06-27 06:04:18 +0000512 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
513 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
514 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
515 }
516
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000517 // If the source and destination are integer or pointer types, just do an
518 // extension or truncation to the desired type.
519 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
520 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
521 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
522 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
523 return;
524 }
525
Duncan Sands9408c452009-05-09 07:08:47 +0000526 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000527
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000528 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000529 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000530 llvm::Value *Casted =
531 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000532 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000533 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000534 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000535 // Otherwise do coercion through memory. This is stupid, but
536 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000537
538 // Generally SrcSize is never greater than DstSize, since this means we are
539 // losing bits. However, this can happen in cases where the structure has
540 // additional padding, for example due to a user specified alignment.
541 //
542 // FIXME: Assert that we aren't truncating non-padding bits when have access
543 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000544 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
545 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000546 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000547 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000548 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
549 // FIXME: Use better alignment / avoid requiring aligned load.
550 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000551 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000552 }
553}
554
Daniel Dunbar56273772008-09-17 00:51:38 +0000555/***/
556
Daniel Dunbar88b53962009-02-02 22:03:45 +0000557bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000558 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000559}
560
John McCallc0bf4622010-02-23 00:48:20 +0000561const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
562 const CGFunctionInfo &FI = getFunctionInfo(GD);
563
564 // For definition purposes, don't consider a K&R function variadic.
565 bool Variadic = false;
566 if (const FunctionProtoType *FPT =
567 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
568 Variadic = FPT->isVariadic();
569
570 return GetFunctionType(FI, Variadic);
571}
572
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000573const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000574CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000575 std::vector<const llvm::Type*> ArgTys;
576
577 const llvm::Type *ResultType = 0;
578
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000579 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000580 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000581 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000582 case ABIArgInfo::Expand:
583 assert(0 && "Invalid ABI kind for return argument");
584
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000585 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000586 case ABIArgInfo::Direct:
587 ResultType = ConvertType(RetTy);
588 break;
589
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000590 case ABIArgInfo::Indirect: {
591 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000592 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000593 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000594 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
595 break;
596 }
597
Daniel Dunbar11434922009-01-26 21:26:08 +0000598 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000599 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000600 break;
601
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000602 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000603 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000604 break;
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
607 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000608 ie = FI.arg_end(); it != ie; ++it) {
609 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000611 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000612 case ABIArgInfo::Ignore:
613 break;
614
Chris Lattnerce700162010-06-28 23:44:11 +0000615 case ABIArgInfo::Coerce: {
616 // If the coerce-to type is a first class aggregate, flatten it. Either
617 // way is semantically identical, but fast-isel and the optimizer
618 // generally likes scalar values better than FCAs.
619 const llvm::Type *ArgTy = AI.getCoerceToType();
620 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
621 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
622 ArgTys.push_back(STy->getElementType(i));
623 } else {
624 ArgTys.push_back(ArgTy);
625 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000626 break;
Chris Lattnerce700162010-06-28 23:44:11 +0000627 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000628
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000629 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000630 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000631 const llvm::Type *LTy = ConvertTypeForMem(it->type);
632 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000633 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000634 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000635
636 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000637 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000638 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000639 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000641 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000642 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000643 break;
644 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000645 }
646
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000647 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000648}
649
Anders Carlssonecf282b2009-11-24 05:08:52 +0000650const llvm::Type *
Anders Carlsson046c2942010-04-17 20:15:18 +0000651CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlssonecf282b2009-11-24 05:08:52 +0000652 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
653
Eli Friedmanc00129a2010-05-30 06:03:20 +0000654 if (!VerifyFuncTypeComplete(FPT))
Anders Carlssonecf282b2009-11-24 05:08:52 +0000655 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
656
657 return llvm::OpaqueType::get(getLLVMContext());
658}
659
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000660void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000661 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000662 AttributeListType &PAL,
663 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000664 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000665 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000666
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000667 CallingConv = FI.getEffectiveCallingConvention();
668
John McCall04a67a62010-02-05 21:31:56 +0000669 if (FI.isNoReturn())
670 FuncAttrs |= llvm::Attribute::NoReturn;
671
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000672 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000673 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000674 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000675 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000676 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000677 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000678 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000679 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000680 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000681 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000682 if (TargetDecl->hasAttr<MallocAttr>())
683 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000684 }
685
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000686 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000687 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000688 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000689 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000690 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000691 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000692
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000693 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000694 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000695 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000696 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000697 case ABIArgInfo::Extend:
698 if (RetTy->isSignedIntegerType()) {
699 RetAttrs |= llvm::Attribute::SExt;
700 } else if (RetTy->isUnsignedIntegerType()) {
701 RetAttrs |= llvm::Attribute::ZExt;
702 }
703 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000704 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000705 break;
706
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000707 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000708 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000709 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000710 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000711 // sret disables readnone and readonly
712 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
713 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000714 break;
715
Daniel Dunbar11434922009-01-26 21:26:08 +0000716 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000717 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000718 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000719
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000720 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000721 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000722 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000723
Devang Patela2c69122008-09-26 22:53:57 +0000724 if (RetAttrs)
725 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000726
727 // FIXME: we need to honour command line settings also...
728 // FIXME: RegParm should be reduced in case of nested functions and/or global
729 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000730 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000731
732 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000733 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000734 ie = FI.arg_end(); it != ie; ++it) {
735 QualType ParamType = it->type;
736 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000737 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000738
John McCalld8e10d22010-03-27 00:47:27 +0000739 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
740 // have the corresponding parameter variable. It doesn't make
741 // sense to do it here because parameters are so fucked up.
Nuno Lopes079b4952009-12-07 18:30:06 +0000742
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000743 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000744 case ABIArgInfo::Coerce:
Chris Lattnerce700162010-06-28 23:44:11 +0000745 if (const llvm::StructType *STy =
746 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
747 Index += STy->getNumElements();
748 else
749 ++Index;
750 continue; // Skip index increment.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000751
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000752 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000753 if (AI.getIndirectByVal())
754 Attributes |= llvm::Attribute::ByVal;
755
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000756 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000757 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000758 // byval disables readnone and readonly.
759 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
760 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000761 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000762
763 case ABIArgInfo::Extend:
764 if (ParamType->isSignedIntegerType()) {
765 Attributes |= llvm::Attribute::SExt;
766 } else if (ParamType->isUnsignedIntegerType()) {
767 Attributes |= llvm::Attribute::ZExt;
768 }
769 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000770 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000771 if (RegParm > 0 &&
772 (ParamType->isIntegerType() || ParamType->isPointerType())) {
773 RegParm -=
774 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
775 if (RegParm >= 0)
776 Attributes |= llvm::Attribute::InReg;
777 }
778 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000779 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000780
Daniel Dunbar11434922009-01-26 21:26:08 +0000781 case ABIArgInfo::Ignore:
782 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000783 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000784
Daniel Dunbar56273772008-09-17 00:51:38 +0000785 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000786 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000787 // FIXME: This is rather inefficient. Do we ever actually need to do
788 // anything here? The result should be just reconstructed on the other
789 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000790 getTypes().GetExpandedTypes(ParamType, Tys);
791 Index += Tys.size();
792 continue;
793 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Devang Patel761d7f72008-09-25 21:02:23 +0000796 if (Attributes)
797 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000798 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000799 }
Devang Patela2c69122008-09-26 22:53:57 +0000800 if (FuncAttrs)
801 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000802}
803
Daniel Dunbar88b53962009-02-02 22:03:45 +0000804void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
805 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000806 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000807 // If this is an implicit-return-zero function, go ahead and
808 // initialize the return value. TODO: it might be nice to have
809 // a more general mechanism for this that didn't require synthesized
810 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000811 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000812 if (FD->hasImplicitReturnZero()) {
813 QualType RetTy = FD->getResultType().getUnqualifiedType();
814 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000815 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000816 Builder.CreateStore(Zero, ReturnValue);
817 }
818 }
819
Mike Stumpf5408fe2009-05-16 07:57:57 +0000820 // FIXME: We no longer need the types from FunctionArgList; lift up and
821 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000822
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000823 // Emit allocs for param decls. Give the LLVM Argument nodes names.
824 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000826 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000827 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000828 AI->setName("agg.result");
829 ++AI;
830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000832 assert(FI.arg_size() == Args.size() &&
833 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000834 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000835 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000836 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000837 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000838 QualType Ty = info_it->type;
839 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000840
841 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000842 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000843 llvm::Value *V = AI;
Daniel Dunbar1f745982009-02-05 09:16:39 +0000844 if (hasAggregateLLVMType(Ty)) {
845 // Do nothing, aggregates and complex variables are accessed by
846 // reference.
847 } else {
848 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000849 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000850 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
851 // This must be a promotion, for something like
852 // "void a(x) short x; {..."
853 V = EmitScalarConversion(V, Ty, Arg->getType());
854 }
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000857 break;
858 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000859
860 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000861 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000862 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattnerce700162010-06-28 23:44:11 +0000863 llvm::Value *V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000864 if (hasAggregateLLVMType(Ty)) {
865 // Create a temporary alloca to hold the argument; the rest of
866 // codegen expects to access aggregates & complex values by
867 // reference.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000868 V = CreateMemTemp(Ty);
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000869 Builder.CreateStore(AI, V);
870 } else {
John McCalld8e10d22010-03-27 00:47:27 +0000871 if (Arg->getType().isRestrictQualified())
872 AI->addAttr(llvm::Attribute::NoAlias);
873
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000874 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
875 // This must be a promotion, for something like
876 // "void a(x) short x; {..."
877 V = EmitScalarConversion(V, Ty, Arg->getType());
878 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000879 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000880 EmitParmDecl(*Arg, V);
881 break;
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Daniel Dunbar56273772008-09-17 00:51:38 +0000884 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000885 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000886 // we need to create a temporary and reconstruct it from the
887 // arguments.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000888 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000889 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000890 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000891 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000892 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000893
Daniel Dunbar56273772008-09-17 00:51:38 +0000894 // Name the arguments used in expansion and increment AI.
895 unsigned Index = 0;
896 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000897 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000898 continue;
899 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000900
901 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000902 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000903 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar195337d2010-02-09 02:48:28 +0000904 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000905 } else {
906 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000909 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000910 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000911
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000912 case ABIArgInfo::Coerce: {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000913 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
914 // result in a new alloca anyway, so we could just store into that
915 // directly if we broke the abstraction down more.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000916 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Chris Lattner309c59f2010-06-29 00:06:42 +0000917
918 // If the coerce-to type is a first class aggregate, we flatten it and
919 // pass the elements. Either way is semantically identical, but fast-isel
920 // and the optimizer generally likes scalar values better than FCAs.
921 if (const llvm::StructType *STy =
922 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
923 // If the argument and alloca types match up, we don't have to build the
924 // FCA at all, emit a series of GEPs and stores, which is better for
925 // fast isel.
926 if (STy == cast<llvm::PointerType>(V->getType())->getElementType()) {
927 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
928 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000929 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
Chris Lattner309c59f2010-06-29 00:06:42 +0000930 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(V, 0, i);
931 Builder.CreateStore(AI++, EltPtr);
932 }
933 } else {
934 // Reconstruct the FCA here so we can do a coerced store.
935 llvm::Value *FormalArg = llvm::UndefValue::get(STy);
936 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
937 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000938 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
Chris Lattner309c59f2010-06-29 00:06:42 +0000939 FormalArg = Builder.CreateInsertValue(FormalArg, AI++, i);
940 }
941 CreateCoercedStore(FormalArg, V, /*DestIsVolatile=*/false, *this);
942 }
943 } else {
944 // Simple case, just do a coerced store of the argument into the alloca.
945 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000946 AI->setName(Arg->getName() + ".coerce");
Chris Lattner309c59f2010-06-29 00:06:42 +0000947 CreateCoercedStore(AI++, V, /*DestIsVolatile=*/false, *this);
948 }
949
950
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000951 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000952 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000953 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000954 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
955 // This must be a promotion, for something like
956 // "void a(x) short x; {..."
957 V = EmitScalarConversion(V, Ty, Arg->getType());
958 }
959 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000960 EmitParmDecl(*Arg, V);
Chris Lattnerce700162010-06-28 23:44:11 +0000961 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000962 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000963 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000964
965 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000966 }
967 assert(AI == Fn->arg_end() && "Argument mismatch!");
968}
969
Chris Lattner35b21b82010-06-27 01:06:27 +0000970void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000971 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000972 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000973 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000974 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000975 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000976
977 llvm::Value *RV = 0;
978 QualType RetTy = FI.getReturnType();
979 const ABIArgInfo &RetAI = FI.getReturnInfo();
980
981 switch (RetAI.getKind()) {
982 case ABIArgInfo::Indirect:
983 if (RetTy->isAnyComplexType()) {
984 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
985 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
986 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
987 // Do nothing; aggregrates get evaluated directly into the destination.
988 } else {
989 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
990 false, RetTy);
991 }
992 break;
993
994 case ABIArgInfo::Extend:
Chris Lattner35b21b82010-06-27 01:06:27 +0000995 case ABIArgInfo::Direct: {
996 // The internal return value temp always will have pointer-to-return-type
997 // type, just do a load.
998
999 // If the instruction right before the insertion point is a store to the
1000 // return value, we can elide the load, zap the store, and usually zap the
1001 // alloca.
1002 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1003 llvm::StoreInst *SI = 0;
1004 if (InsertBB->empty() ||
1005 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1006 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1007 RV = Builder.CreateLoad(ReturnValue);
1008 } else {
1009 // Get the stored value and nuke the now-dead store.
1010 RV = SI->getValueOperand();
1011 SI->eraseFromParent();
1012
1013 // If that was the only use of the return value, nuke it as well now.
1014 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1015 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1016 ReturnValue = 0;
1017 }
1018 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001019 break;
Chris Lattner35b21b82010-06-27 01:06:27 +00001020 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001021 case ABIArgInfo::Ignore:
1022 break;
1023
1024 case ABIArgInfo::Coerce:
1025 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
1026 break;
1027
1028 case ABIArgInfo::Expand:
1029 assert(0 && "Invalid ABI kind for return argument");
1030 }
1031
1032 if (RV)
1033 Builder.CreateRet(RV);
1034 else
1035 Builder.CreateRetVoid();
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001036}
1037
John McCall27360712010-05-26 22:34:26 +00001038RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1039 // StartFunction converted the ABI-lowered parameter(s) into a
1040 // local alloca. We need to turn that into an r-value suitable
1041 // for EmitCall.
1042 llvm::Value *Local = GetAddrOfLocalVar(Param);
1043
1044 QualType ArgType = Param->getType();
1045
1046 // For the most part, we just need to load the alloca, except:
1047 // 1) aggregate r-values are actually pointers to temporaries, and
1048 // 2) references to aggregates are pointers directly to the aggregate.
1049 // I don't know why references to non-aggregates are different here.
1050 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1051 if (hasAggregateLLVMType(RefType->getPointeeType()))
1052 return RValue::getAggregate(Local);
1053
1054 // Locals which are references to scalars are represented
1055 // with allocas holding the pointer.
1056 return RValue::get(Builder.CreateLoad(Local));
1057 }
1058
1059 if (ArgType->isAnyComplexType())
1060 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1061
1062 if (hasAggregateLLVMType(ArgType))
1063 return RValue::getAggregate(Local);
1064
1065 return RValue::get(EmitLoadOfScalar(Local, false, ArgType));
1066}
1067
Anders Carlsson0139bb92009-04-08 20:47:54 +00001068RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00001069 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +00001070 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Anders Carlsson0139bb92009-04-08 20:47:54 +00001072 return EmitAnyExprToTemp(E);
1073}
1074
Daniel Dunbar88b53962009-02-02 22:03:45 +00001075RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001076 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001077 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001078 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001079 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001080 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001081 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001082 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001083
1084 // Handle struct-return functions by passing a pointer to the
1085 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001086 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001087 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001088
1089
Chris Lattner5db7ae52009-06-13 00:26:38 +00001090 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001091 // alloca to hold the result, unless one is given to us.
1092 if (CGM.ReturnTypeUsesSret(CallInfo)) {
1093 llvm::Value *Value = ReturnValue.getValue();
1094 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001095 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001096 Args.push_back(Value);
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001099 assert(CallInfo.arg_size() == CallArgs.size() &&
1100 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001101 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001102 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001103 I != E; ++I, ++info_it) {
1104 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001105 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001106
1107 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001108 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +00001109 if (RV.isScalar() || RV.isComplex()) {
1110 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001111 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001112 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001113 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001114 else
Mike Stump1eb44332009-09-09 15:08:12 +00001115 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001116 } else {
1117 Args.push_back(RV.getAggregateAddr());
1118 }
1119 break;
1120
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001121 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001122 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +00001123 if (RV.isScalar()) {
1124 Args.push_back(RV.getScalarVal());
1125 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001126 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1127 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1128 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1129 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +00001130 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001131 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +00001132 }
1133 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Daniel Dunbar11434922009-01-26 21:26:08 +00001135 case ABIArgInfo::Ignore:
1136 break;
1137
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001138 case ABIArgInfo::Coerce: {
1139 // FIXME: Avoid the conversion through memory if possible.
1140 llvm::Value *SrcPtr;
1141 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001142 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +00001143 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001144 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001145 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001146 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001147 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001148 SrcPtr = RV.getAggregateAddr();
Chris Lattnerce700162010-06-28 23:44:11 +00001149
Chris Lattnerce700162010-06-28 23:44:11 +00001150 // If the coerce-to type is a first class aggregate, we flatten it and
1151 // pass the elements. Either way is semantically identical, but fast-isel
1152 // and the optimizer generally likes scalar values better than FCAs.
1153 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001154 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
1155 // If the argument and alloca types match up, we don't have to build the
1156 // FCA at all, emit a series of GEPs and loads, which is better for
1157 // fast isel.
1158 if (STy ==cast<llvm::PointerType>(SrcPtr->getType())->getElementType()){
1159 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1160 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1161 Args.push_back(Builder.CreateLoad(EltPtr));
1162 }
1163 } else {
1164 // Otherwise, do a coerced load the entire FCA and handle the pieces.
1165 llvm::Value *SrcVal =
1166 CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), *this);
1167
1168 // Extract the elements of the value to pass in.
1169 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1170 Args.push_back(Builder.CreateExtractValue(SrcVal, i));
1171 }
Chris Lattnerce700162010-06-28 23:44:11 +00001172 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001173 // In the simple case, just pass the coerced loaded value.
1174 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1175 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001176 }
1177
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001178 break;
1179 }
1180
Daniel Dunbar56273772008-09-17 00:51:38 +00001181 case ABIArgInfo::Expand:
1182 ExpandTypeToArgs(I->second, RV, Args);
1183 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001184 }
1185 }
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Chris Lattner5db7ae52009-06-13 00:26:38 +00001187 // If the callee is a bitcast of a function to a varargs pointer to function
1188 // type, check to see if we can remove the bitcast. This handles some cases
1189 // with unprototyped functions.
1190 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1191 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1192 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1193 const llvm::FunctionType *CurFT =
1194 cast<llvm::FunctionType>(CurPT->getElementType());
1195 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Chris Lattner5db7ae52009-06-13 00:26:38 +00001197 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1198 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001199 ActualFT->getNumParams() == CurFT->getNumParams() &&
1200 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001201 bool ArgsMatch = true;
1202 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1203 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1204 ArgsMatch = false;
1205 break;
1206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Chris Lattner5db7ae52009-06-13 00:26:38 +00001208 // Strip the cast if we can get away with it. This is a nice cleanup,
1209 // but also allows us to inline the function at -O0 if it is marked
1210 // always_inline.
1211 if (ArgsMatch)
1212 Callee = CalleeF;
1213 }
1214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001216
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001217 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001218 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001219 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001220 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001221 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1222 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Daniel Dunbard14151d2009-03-02 04:32:35 +00001224 llvm::CallSite CS;
1225 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001226 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001227 } else {
1228 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001229 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001230 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001231 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001232 }
Chris Lattnerce933992010-06-29 16:40:28 +00001233 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001234 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001235
Daniel Dunbard14151d2009-03-02 04:32:35 +00001236 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001237 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001238
1239 // If the call doesn't return, finish the basic block and clear the
1240 // insertion point; this allows the rest of IRgen to discard
1241 // unreachable code.
1242 if (CS.doesNotReturn()) {
1243 Builder.CreateUnreachable();
1244 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Mike Stumpf5408fe2009-05-16 07:57:57 +00001246 // FIXME: For now, emit a dummy basic block because expr emitters in
1247 // generally are not ready to handle emitting expressions at unreachable
1248 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001249 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Daniel Dunbard14151d2009-03-02 04:32:35 +00001251 // Return a reasonable RValue.
1252 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001253 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001254
1255 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001256 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001257 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001258
1259 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +00001260 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001261 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001262 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001263 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001264 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +00001265 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001266
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001267 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +00001268 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +00001269 if (RetTy->isAnyComplexType()) {
1270 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1271 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1272 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +00001273 }
1274 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001275 llvm::Value *DestPtr = ReturnValue.getValue();
1276 bool DestIsVolatile = ReturnValue.isVolatile();
1277
1278 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001279 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlssond2490a92009-12-24 20:40:36 +00001280 DestIsVolatile = false;
1281 }
1282 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1283 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +00001284 }
1285 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001286
Daniel Dunbar11434922009-01-26 21:26:08 +00001287 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001288 // If we are ignoring an argument that had a result, make sure to
1289 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001290 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +00001291
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001292 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +00001293 llvm::Value *DestPtr = ReturnValue.getValue();
1294 bool DestIsVolatile = ReturnValue.isVolatile();
1295
1296 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001297 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001298 DestIsVolatile = false;
1299 }
1300
1301 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001302 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001303 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001304 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001305 return RValue::getAggregate(DestPtr);
1306 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001307 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001308
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001309 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001310 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001311 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001312
1313 assert(0 && "Unhandled ABIArgInfo::Kind");
1314 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001315}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001316
1317/* VarArg handling */
1318
1319llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1320 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1321}