blob: 475dfa5c102a720b350bd9695ac87c7e534d0519 [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"
John McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Chris Lattnerce933992010-06-29 16:40:28 +000017#include "ABIInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000018#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000019#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000020#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000021#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000023#include "clang/AST/DeclObjC.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000028using namespace clang;
29using namespace CodeGen;
30
31/***/
32
John McCall04a67a62010-02-05 21:31:56 +000033static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
34 switch (CC) {
35 default: return llvm::CallingConv::C;
36 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
37 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregorf813a2c2010-05-18 16:57:00 +000038 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
Dawn Perchik52fc3142010-09-03 01:29:35 +000039 // TODO: add support for CC_X86Pascal to llvm
John McCall04a67a62010-02-05 21:31:56 +000040 }
41}
42
John McCall0b0ef0a2010-02-24 07:14:12 +000043/// Derives the 'this' type for codegen purposes, i.e. ignoring method
44/// qualification.
45/// FIXME: address space qualification?
John McCallead608a2010-02-26 00:48:12 +000046static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
47 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
48 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049}
50
John McCall0b0ef0a2010-02-24 07:14:12 +000051/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000052static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
53 return MD->getType()->getCanonicalTypeUnqualified()
54 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-02-24 07:14:12 +000055}
56
57/// Returns the "extra-canonicalized" return type, which discards
58/// qualifiers on the return type. Codegen doesn't care about them,
59/// and it makes ABI code a little easier to be able to assume that
60/// all parameter and return types are top-level unqualified.
John McCallead608a2010-02-26 00:48:12 +000061static CanQualType GetReturnType(QualType RetTy) {
62 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000063}
64
65const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000066CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
67 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000068 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
69 llvm::SmallVector<CanQualType, 16>(),
Chris Lattnerbcaedae2010-06-30 19:14:05 +000070 FTNP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000071}
72
73/// \param Args - contains any initial parameters besides those
74/// in the formal type
75static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000076 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +000077 CanQual<FunctionProtoType> FTP,
78 bool IsRecursive = false) {
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();
Chris Lattnerbcaedae2010-06-30 19:14:05 +000083 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000084}
85
86const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000087CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
88 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000089 llvm::SmallVector<CanQualType, 16> ArgTys;
Chris Lattnerbcaedae2010-06-30 19:14:05 +000090 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
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
Dawn Perchik52fc3142010-09-03 01:29:35 +0000104 if (D->hasAttr<PascalAttr>())
105 return CC_X86Pascal;
106
John McCall04a67a62010-02-05 21:31:56 +0000107 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000108}
109
Anders Carlsson375c31c2009-10-03 19:43:08 +0000110const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
111 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000112 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000113
Anders Carlsson375c31c2009-10-03 19:43:08 +0000114 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000115 ArgTys.push_back(GetThisType(Context, RD));
116
117 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000118 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000119}
120
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000121const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000122 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000123
John McCallfc400282010-09-03 01:26:39 +0000124 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
125 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
126
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000127 // Add the 'this' pointer unless this is a static method.
128 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000129 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000130
John McCall0b0ef0a2010-02-24 07:14:12 +0000131 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000132}
133
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000134const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
135 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000136 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000137 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000138 CanQualType ResTy = Context.VoidTy;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000139
John McCall4c40d982010-08-31 07:33:07 +0000140 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall0b0ef0a2010-02-24 07:14:12 +0000141
John McCall4c40d982010-08-31 07:33:07 +0000142 CanQual<FunctionProtoType> FTP = GetFormalType(D);
143
144 // Add the formal parameters.
145 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
146 ArgTys.push_back(FTP->getArgType(i));
147
148 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000149}
150
151const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
152 CXXDtorType Type) {
John McCall4c40d982010-08-31 07:33:07 +0000153 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCallead608a2010-02-26 00:48:12 +0000154 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000155 CanQualType ResTy = Context.VoidTy;
John McCall0b0ef0a2010-02-24 07:14:12 +0000156
John McCall4c40d982010-08-31 07:33:07 +0000157 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
158
159 CanQual<FunctionProtoType> FTP = GetFormalType(D);
160 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
161
162 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000163}
164
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000165const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000166 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000167 if (MD->isInstance())
168 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
John McCallead608a2010-02-26 00:48:12 +0000170 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
171 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000172 if (isa<FunctionNoProtoType>(FTy))
John McCallead608a2010-02-26 00:48:12 +0000173 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
174 assert(isa<FunctionProtoType>(FTy));
175 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000176}
177
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000178const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000179 llvm::SmallVector<CanQualType, 16> ArgTys;
180 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
181 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000182 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000183 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000184 e = MD->param_end(); i != e; ++i) {
185 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
186 }
187 return getFunctionInfo(GetReturnType(MD->getResultType()),
188 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000189 FunctionType::ExtInfo(
190 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000191 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000192 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000193}
194
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000195const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
196 // FIXME: Do we need to handle ObjCMethodDecl?
197 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
198
199 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
200 return getFunctionInfo(CD, GD.getCtorType());
201
202 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
203 return getFunctionInfo(DD, GD.getDtorType());
204
205 return getFunctionInfo(FD);
206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000209 const CallArgList &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 (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +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 Dunbar725ad312009-01-31 02:19:00 +0000217}
218
Mike Stump1eb44332009-09-09 15:08:12 +0000219const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000220 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000221 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000222 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000223 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000224 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000225 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000226 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000227 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000228}
229
John McCallead608a2010-02-26 00:48:12 +0000230const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
231 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000232 const FunctionType::ExtInfo &Info,
233 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +0000234#ifndef NDEBUG
235 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
236 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
237 assert(I->isCanonicalAsParam());
238#endif
239
Rafael Espindola425ef722010-03-30 22:15:11 +0000240 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000241
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000242 // Lookup or create unique function info.
243 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000244 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000245 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000246
247 void *InsertPos = 0;
248 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
249 if (FI)
250 return *FI;
251
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000252 // Construct the function info.
Chris Lattnerce700162010-06-28 23:44:11 +0000253 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000254 ArgTys.data(), ArgTys.size());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000255 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000256
257 // Compute ABI information.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000258 getABIInfo().computeInfo(*FI);
Chris Lattner800588f2010-07-29 06:26:06 +0000259
260 // Loop over all of the computed argument and return value info. If any of
261 // them are direct or extend without a specified coerce type, specify the
262 // default now.
263 ABIArgInfo &RetInfo = FI->getReturnInfo();
264 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
265 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
266
267 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
268 I != E; ++I)
269 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
270 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000271
Chris Lattnera9fa8582010-07-01 06:20:47 +0000272 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
273 // types, resolve them now. These pointers may point to this function, which
274 // we *just* filled in the FunctionInfo for.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000275 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattnera9fa8582010-07-01 06:20:47 +0000276 HandleLateResolvedPointers();
Chris Lattnera9fa8582010-07-01 06:20:47 +0000277
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000278 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000279}
280
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000281CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattnerbb521142010-06-29 18:13:52 +0000282 bool _NoReturn, unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000283 CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000284 const CanQualType *ArgTys,
285 unsigned NumArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000286 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000287 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000288 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000289{
Chris Lattnerbb521142010-06-29 18:13:52 +0000290 NumArgs = NumArgTys;
Chris Lattnerce700162010-06-28 23:44:11 +0000291
292 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattnerbb521142010-06-29 18:13:52 +0000293 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000294 Args[0].type = ResTy;
Chris Lattnerbb521142010-06-29 18:13:52 +0000295 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000296 Args[1 + i].type = ArgTys[i];
297}
298
299/***/
300
Mike Stump1eb44332009-09-09 15:08:12 +0000301void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000302 std::vector<const llvm::Type*> &ArgTys,
303 bool IsRecursive) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000304 const RecordType *RT = Ty->getAsStructureType();
305 assert(RT && "Can only expand structure types.");
306 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000307 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000308 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000310 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
311 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000312 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000313 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000314 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Daniel Dunbar56273772008-09-17 00:51:38 +0000316 QualType FT = FD->getType();
Chris Lattnerdeabde22010-07-28 18:24:28 +0000317 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000318 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000319 else
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000320 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar56273772008-09-17 00:51:38 +0000321 }
322}
323
Mike Stump1eb44332009-09-09 15:08:12 +0000324llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000325CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
326 llvm::Function::arg_iterator AI) {
327 const RecordType *RT = Ty->getAsStructureType();
328 assert(RT && "Can only expand structure types.");
329
330 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000331 assert(LV.isSimple() &&
332 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000333 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000334 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
335 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000336 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000337 QualType FT = FD->getType();
338
339 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000340 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000341 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
342 AI = ExpandTypeFromArgs(FT, LV, AI);
343 } else {
344 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
345 ++AI;
346 }
347 }
348
349 return AI;
350}
351
Mike Stump1eb44332009-09-09 15:08:12 +0000352void
353CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000354 llvm::SmallVector<llvm::Value*, 16> &Args) {
355 const RecordType *RT = Ty->getAsStructureType();
356 assert(RT && "Can only expand structure types.");
357
358 RecordDecl *RD = RT->getDecl();
359 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
360 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000361 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
362 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000364 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Daniel Dunbar56273772008-09-17 00:51:38 +0000366 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000367 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000368 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
369 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
370 } else {
371 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000372 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000373 "Unexpected non-scalar rvalue during struct expansion.");
374 Args.push_back(RV.getScalarVal());
375 }
376 }
377}
378
Chris Lattnere7bb7772010-06-27 06:04:18 +0000379/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000380/// accessing some number of bytes out of it, try to gep into the struct to get
381/// at its inner goodness. Dive as deep as possible without entering an element
382/// with an in-memory size smaller than DstSize.
383static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000384EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
385 const llvm::StructType *SrcSTy,
386 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000387 // We can't dive into a zero-element struct.
388 if (SrcSTy->getNumElements() == 0) return SrcPtr;
389
390 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
391
392 // If the first elt is at least as large as what we're looking for, or if the
393 // first element is the same size as the whole struct, we can enter it.
394 uint64_t FirstEltSize =
395 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
396 if (FirstEltSize < DstSize &&
397 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
398 return SrcPtr;
399
400 // GEP into the first element.
401 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
402
403 // If the first element is a struct, recurse.
404 const llvm::Type *SrcTy =
405 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
406 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000407 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000408
409 return SrcPtr;
410}
411
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000412/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
413/// are either integers or pointers. This does a truncation of the value if it
414/// is too large or a zero extension if it is too small.
415static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
416 const llvm::Type *Ty,
417 CodeGenFunction &CGF) {
418 if (Val->getType() == Ty)
419 return Val;
420
421 if (isa<llvm::PointerType>(Val->getType())) {
422 // If this is Pointer->Pointer avoid conversion to and from int.
423 if (isa<llvm::PointerType>(Ty))
424 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
425
426 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000427 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000428 }
429
430 const llvm::Type *DestIntTy = Ty;
431 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000432 DestIntTy = CGF.IntPtrTy;
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000433
434 if (Val->getType() != DestIntTy)
435 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
436
437 if (isa<llvm::PointerType>(Ty))
438 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
439 return Val;
440}
441
Chris Lattner08dd2a02010-06-27 05:56:15 +0000442
443
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000444/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
445/// a pointer to an object of type \arg Ty.
446///
447/// This safely handles the case when the src type is smaller than the
448/// destination type; in this situation the values of bits which not
449/// present in the src are undefined.
450static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
451 const llvm::Type *Ty,
452 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000453 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000454 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000455
456 // If SrcTy and Ty are the same, just do a load.
457 if (SrcTy == Ty)
458 return CGF.Builder.CreateLoad(SrcPtr);
459
Duncan Sands9408c452009-05-09 07:08:47 +0000460 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000461
462 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000463 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000464 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
465 }
466
467 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000468
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000469 // If the source and destination are integer or pointer types, just do an
470 // extension or truncation to the desired type.
471 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
472 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
473 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
474 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
475 }
476
Daniel Dunbarb225be42009-02-03 05:59:18 +0000477 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000478 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000479 // Generally SrcSize is never greater than DstSize, since this means we are
480 // losing bits. However, this can happen in cases where the structure has
481 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000482 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000483 // FIXME: Assert that we aren't truncating non-padding bits when have access
484 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000485 llvm::Value *Casted =
486 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000487 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
488 // FIXME: Use better alignment / avoid requiring aligned load.
489 Load->setAlignment(1);
490 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000491 }
Chris Lattner35b21b82010-06-27 01:06:27 +0000492
493 // Otherwise do coercion through memory. This is stupid, but
494 // simple.
495 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
496 llvm::Value *Casted =
497 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
498 llvm::StoreInst *Store =
499 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
500 // FIXME: Use better alignment / avoid requiring aligned store.
501 Store->setAlignment(1);
502 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000503}
504
505/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
506/// where the source and destination may have different types.
507///
508/// This safely handles the case when the src type is larger than the
509/// destination type; the upper bits of the src will be lost.
510static void CreateCoercedStore(llvm::Value *Src,
511 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000512 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000513 CodeGenFunction &CGF) {
514 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000515 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000516 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000517 if (SrcTy == DstTy) {
518 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
519 return;
520 }
521
522 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
523
Chris Lattnere7bb7772010-06-27 06:04:18 +0000524 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
525 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
526 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
527 }
528
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000529 // If the source and destination are integer or pointer types, just do an
530 // extension or truncation to the desired type.
531 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
532 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
533 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
534 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
535 return;
536 }
537
Duncan Sands9408c452009-05-09 07:08:47 +0000538 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000539
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000540 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000541 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000542 llvm::Value *Casted =
543 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000544 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000545 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000546 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000547 // Otherwise do coercion through memory. This is stupid, but
548 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000549
550 // Generally SrcSize is never greater than DstSize, since this means we are
551 // losing bits. However, this can happen in cases where the structure has
552 // additional padding, for example due to a user specified alignment.
553 //
554 // FIXME: Assert that we aren't truncating non-padding bits when have access
555 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000556 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
557 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000558 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000559 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000560 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
561 // FIXME: Use better alignment / avoid requiring aligned load.
562 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000563 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000564 }
565}
566
Daniel Dunbar56273772008-09-17 00:51:38 +0000567/***/
568
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000569bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000570 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000571}
572
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000573bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
574 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
575 switch (BT->getKind()) {
576 default:
577 return false;
578 case BuiltinType::Float:
579 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
580 case BuiltinType::Double:
581 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
582 case BuiltinType::LongDouble:
583 return getContext().Target.useObjCFPRetForRealType(
584 TargetInfo::LongDouble);
585 }
586 }
587
588 return false;
589}
590
John McCallc0bf4622010-02-23 00:48:20 +0000591const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
592 const CGFunctionInfo &FI = getFunctionInfo(GD);
593
594 // For definition purposes, don't consider a K&R function variadic.
595 bool Variadic = false;
596 if (const FunctionProtoType *FPT =
597 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
598 Variadic = FPT->isVariadic();
599
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000600 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000601}
602
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000603const llvm::FunctionType *
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000604CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
605 bool IsRecursive) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000606 std::vector<const llvm::Type*> ArgTys;
607
608 const llvm::Type *ResultType = 0;
609
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000610 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000611 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000612 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000613 case ABIArgInfo::Expand:
614 assert(0 && "Invalid ABI kind for return argument");
615
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000616 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000617 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000618 ResultType = RetAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000619 break;
620
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000621 case ABIArgInfo::Indirect: {
622 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000623 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000624 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000625 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
626 break;
627 }
628
Daniel Dunbar11434922009-01-26 21:26:08 +0000629 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000630 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000631 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000635 ie = FI.arg_end(); it != ie; ++it) {
636 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000638 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000639 case ABIArgInfo::Ignore:
640 break;
641
Chris Lattner800588f2010-07-29 06:26:06 +0000642 case ABIArgInfo::Indirect: {
643 // indirect arguments are always on the stack, which is addr space #0.
644 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
645 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
646 break;
647 }
648
649 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000650 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000651 // If the coerce-to type is a first class aggregate, flatten it. Either
652 // way is semantically identical, but fast-isel and the optimizer
653 // generally likes scalar values better than FCAs.
654 const llvm::Type *ArgTy = AI.getCoerceToType();
655 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
656 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
657 ArgTys.push_back(STy->getElementType(i));
658 } else {
659 ArgTys.push_back(ArgTy);
660 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000661 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000664 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000665 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000666 break;
667 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000668 }
669
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000670 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000671}
672
John McCall4c40d982010-08-31 07:33:07 +0000673const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
674 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000675 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
676
John McCall4c40d982010-08-31 07:33:07 +0000677 if (!VerifyFuncTypeComplete(FPT)) {
678 const CGFunctionInfo *Info;
679 if (isa<CXXDestructorDecl>(MD))
680 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
681 else
682 Info = &getFunctionInfo(MD);
683 return GetFunctionType(*Info, FPT->isVariadic(), false);
684 }
Anders Carlssonecf282b2009-11-24 05:08:52 +0000685
686 return llvm::OpaqueType::get(getLLVMContext());
687}
688
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000689void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000690 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000691 AttributeListType &PAL,
692 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000693 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000694 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000695
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000696 CallingConv = FI.getEffectiveCallingConvention();
697
John McCall04a67a62010-02-05 21:31:56 +0000698 if (FI.isNoReturn())
699 FuncAttrs |= llvm::Attribute::NoReturn;
700
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000701 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000702 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000703 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000704 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000705 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
706 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
707 if (FPT && FPT->hasEmptyExceptionSpec())
708 FuncAttrs |= llvm::Attribute::NoUnwind;
709 }
710
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000711 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000712 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000713 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000714 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000715 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000716 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000717 if (TargetDecl->hasAttr<MallocAttr>())
718 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000719 }
720
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000721 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000722 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000723 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000724 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000725 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000726 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000727
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000728 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000729 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000730 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000731 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000732 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000733 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000734 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000735 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000736 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000737 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000738 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000739 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000740 break;
741
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000742 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000743 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000744 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000745 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000746 // sret disables readnone and readonly
747 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
748 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000749 break;
750
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000751 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000752 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000753 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000754
Devang Patela2c69122008-09-26 22:53:57 +0000755 if (RetAttrs)
756 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000757
Chris Lattnerdeabde22010-07-28 18:24:28 +0000758 // FIXME: we need to honor command line settings also.
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000759 // FIXME: RegParm should be reduced in case of nested functions and/or global
760 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000761 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000762
763 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000764 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000765 ie = FI.arg_end(); it != ie; ++it) {
766 QualType ParamType = it->type;
767 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000768 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000769
John McCalld8e10d22010-03-27 00:47:27 +0000770 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
771 // have the corresponding parameter variable. It doesn't make
772 // sense to do it here because parameters are so fucked up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000773 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000774 case ABIArgInfo::Extend:
775 if (ParamType->isSignedIntegerType())
776 Attributes |= llvm::Attribute::SExt;
777 else if (ParamType->isUnsignedIntegerType())
778 Attributes |= llvm::Attribute::ZExt;
779 // FALL THROUGH
780 case ABIArgInfo::Direct:
781 if (RegParm > 0 &&
782 (ParamType->isIntegerType() || ParamType->isPointerType())) {
783 RegParm -=
784 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
785 if (RegParm >= 0)
786 Attributes |= llvm::Attribute::InReg;
787 }
788 // FIXME: handle sseregparm someday...
789
Chris Lattnerce700162010-06-28 23:44:11 +0000790 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000791 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
792 Index += STy->getNumElements()-1; // 1 will be added below.
793 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000794
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000795 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000796 if (AI.getIndirectByVal())
797 Attributes |= llvm::Attribute::ByVal;
798
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000799 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000800 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000801 // byval disables readnone and readonly.
802 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
803 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000804 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000805
Daniel Dunbar11434922009-01-26 21:26:08 +0000806 case ABIArgInfo::Ignore:
807 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000808 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000809
Daniel Dunbar56273772008-09-17 00:51:38 +0000810 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000811 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000812 // FIXME: This is rather inefficient. Do we ever actually need to do
813 // anything here? The result should be just reconstructed on the other
814 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000815 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000816 Index += Tys.size();
817 continue;
818 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Devang Patel761d7f72008-09-25 21:02:23 +0000821 if (Attributes)
822 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000823 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000824 }
Devang Patela2c69122008-09-26 22:53:57 +0000825 if (FuncAttrs)
826 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000827}
828
Daniel Dunbar88b53962009-02-02 22:03:45 +0000829void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
830 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000831 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000832 // If this is an implicit-return-zero function, go ahead and
833 // initialize the return value. TODO: it might be nice to have
834 // a more general mechanism for this that didn't require synthesized
835 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000836 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000837 if (FD->hasImplicitReturnZero()) {
838 QualType RetTy = FD->getResultType().getUnqualifiedType();
839 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000840 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000841 Builder.CreateStore(Zero, ReturnValue);
842 }
843 }
844
Mike Stumpf5408fe2009-05-16 07:57:57 +0000845 // FIXME: We no longer need the types from FunctionArgList; lift up and
846 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000847
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000848 // Emit allocs for param decls. Give the LLVM Argument nodes names.
849 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000851 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000852 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000853 AI->setName("agg.result");
854 ++AI;
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000857 assert(FI.arg_size() == Args.size() &&
858 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000859 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000860 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000861 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000862 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000863 QualType Ty = info_it->type;
864 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000865
866 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000867 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000868 llvm::Value *V = AI;
Daniel Dunbar1f745982009-02-05 09:16:39 +0000869 if (hasAggregateLLVMType(Ty)) {
870 // Do nothing, aggregates and complex variables are accessed by
871 // reference.
872 } else {
873 // Load scalar value from indirect argument.
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000874 unsigned Alignment = getContext().getTypeAlignInChars(Ty).getQuantity();
875 V = EmitLoadOfScalar(V, false, Alignment, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000876 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
877 // This must be a promotion, for something like
878 // "void a(x) short x; {..."
879 V = EmitScalarConversion(V, Ty, Arg->getType());
880 }
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000883 break;
884 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000885
886 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000887 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000888 // If we have the trivial case, handle it with no muss and fuss.
889 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000890 ArgI.getCoerceToType() == ConvertType(Ty) &&
891 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000892 assert(AI != Fn->arg_end() && "Argument mismatch!");
893 llvm::Value *V = AI;
894
John McCalld8e10d22010-03-27 00:47:27 +0000895 if (Arg->getType().isRestrictQualified())
896 AI->addAttr(llvm::Attribute::NoAlias);
897
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000898 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
899 // This must be a promotion, for something like
900 // "void a(x) short x; {..."
901 V = EmitScalarConversion(V, Ty, Arg->getType());
902 }
Chris Lattner800588f2010-07-29 06:26:06 +0000903 EmitParmDecl(*Arg, V);
904 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner121b3fa2010-07-05 20:21:00 +0000907 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Chris Lattnerdeabde22010-07-28 18:24:28 +0000908
909 // The alignment we need to use is the max of the requested alignment for
910 // the argument plus the alignment required by our access code below.
911 unsigned AlignmentToUse =
912 CGF.CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
913 AlignmentToUse = std::max(AlignmentToUse,
914 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
915
916 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000917 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000918 llvm::Value *Ptr = V; // Pointer to store into.
919
920 // If the value is offset in memory, apply the offset now.
921 if (unsigned Offs = ArgI.getDirectOffset()) {
922 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
923 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
924 Ptr = Builder.CreateBitCast(Ptr,
925 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
926 }
Chris Lattner309c59f2010-06-29 00:06:42 +0000927
928 // If the coerce-to type is a first class aggregate, we flatten it and
929 // pass the elements. Either way is semantically identical, but fast-isel
930 // and the optimizer generally likes scalar values better than FCAs.
931 if (const llvm::StructType *STy =
932 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000933 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
934
935 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
936 assert(AI != Fn->arg_end() && "Argument mismatch!");
937 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
938 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
939 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000940 }
941 } else {
942 // Simple case, just do a coerced store of the argument into the alloca.
943 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000944 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +0000945 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +0000946 }
947
948
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000949 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000950 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000951 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000952 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
953 // This must be a promotion, for something like
954 // "void a(x) short x; {..."
955 V = EmitScalarConversion(V, Ty, Arg->getType());
956 }
957 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000958 EmitParmDecl(*Arg, V);
Chris Lattnerce700162010-06-28 23:44:11 +0000959 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000960 }
Chris Lattner800588f2010-07-29 06:26:06 +0000961
962 case ABIArgInfo::Expand: {
963 // If this structure was expanded into multiple arguments then
964 // we need to create a temporary and reconstruct it from the
965 // arguments.
966 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattner800588f2010-07-29 06:26:06 +0000967 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +0000968 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Chris Lattner800588f2010-07-29 06:26:06 +0000969 EmitParmDecl(*Arg, Temp);
970
971 // Name the arguments used in expansion and increment AI.
972 unsigned Index = 0;
973 for (; AI != End; ++AI, ++Index)
974 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
975 continue;
976 }
977
978 case ABIArgInfo::Ignore:
979 // Initialize the local variable appropriately.
980 if (hasAggregateLLVMType(Ty))
981 EmitParmDecl(*Arg, CreateMemTemp(Ty));
982 else
983 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
984
985 // Skip increment, no matching LLVM parameter.
986 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000987 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000988
989 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000990 }
991 assert(AI == Fn->arg_end() && "Argument mismatch!");
992}
993
Chris Lattner35b21b82010-06-27 01:06:27 +0000994void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000995 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000996 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000997 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000998 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000999 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001000
Dan Gohman4751a532010-07-20 20:13:52 +00001001 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001002 llvm::Value *RV = 0;
1003 QualType RetTy = FI.getReturnType();
1004 const ABIArgInfo &RetAI = FI.getReturnInfo();
1005
1006 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001007 case ABIArgInfo::Indirect: {
1008 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001009 if (RetTy->isAnyComplexType()) {
1010 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1011 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1012 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1013 // Do nothing; aggregrates get evaluated directly into the destination.
1014 } else {
1015 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001016 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001017 }
1018 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001019 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001020
1021 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001022 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001023 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1024 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001025 // The internal return value temp always will have pointer-to-return-type
1026 // type, just do a load.
1027
1028 // If the instruction right before the insertion point is a store to the
1029 // return value, we can elide the load, zap the store, and usually zap the
1030 // alloca.
1031 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1032 llvm::StoreInst *SI = 0;
1033 if (InsertBB->empty() ||
1034 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1035 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1036 RV = Builder.CreateLoad(ReturnValue);
1037 } else {
1038 // Get the stored value and nuke the now-dead store.
1039 RetDbgLoc = SI->getDebugLoc();
1040 RV = SI->getValueOperand();
1041 SI->eraseFromParent();
1042
1043 // If that was the only use of the return value, nuke it as well now.
1044 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1045 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1046 ReturnValue = 0;
1047 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001048 }
Chris Lattner800588f2010-07-29 06:26:06 +00001049 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001050 llvm::Value *V = ReturnValue;
1051 // If the value is offset in memory, apply the offset now.
1052 if (unsigned Offs = RetAI.getDirectOffset()) {
1053 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1054 V = Builder.CreateConstGEP1_32(V, Offs);
1055 V = Builder.CreateBitCast(V,
1056 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1057 }
1058
1059 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001060 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001061 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001062
Chris Lattner800588f2010-07-29 06:26:06 +00001063 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001064 break;
1065
1066 case ABIArgInfo::Expand:
1067 assert(0 && "Invalid ABI kind for return argument");
1068 }
1069
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001070 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001071 if (!RetDbgLoc.isUnknown())
1072 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001073}
1074
John McCall27360712010-05-26 22:34:26 +00001075RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1076 // StartFunction converted the ABI-lowered parameter(s) into a
1077 // local alloca. We need to turn that into an r-value suitable
1078 // for EmitCall.
1079 llvm::Value *Local = GetAddrOfLocalVar(Param);
1080
1081 QualType ArgType = Param->getType();
1082
1083 // For the most part, we just need to load the alloca, except:
1084 // 1) aggregate r-values are actually pointers to temporaries, and
1085 // 2) references to aggregates are pointers directly to the aggregate.
1086 // I don't know why references to non-aggregates are different here.
1087 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1088 if (hasAggregateLLVMType(RefType->getPointeeType()))
1089 return RValue::getAggregate(Local);
1090
1091 // Locals which are references to scalars are represented
1092 // with allocas holding the pointer.
1093 return RValue::get(Builder.CreateLoad(Local));
1094 }
1095
1096 if (ArgType->isAnyComplexType())
1097 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1098
1099 if (hasAggregateLLVMType(ArgType))
1100 return RValue::getAggregate(Local);
1101
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001102 unsigned Alignment = getContext().getDeclAlign(Param).getQuantity();
1103 return RValue::get(EmitLoadOfScalar(Local, false, Alignment, ArgType));
John McCall27360712010-05-26 22:34:26 +00001104}
1105
Anders Carlsson0139bb92009-04-08 20:47:54 +00001106RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00001107 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +00001108 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Anders Carlsson0139bb92009-04-08 20:47:54 +00001110 return EmitAnyExprToTemp(E);
1111}
1112
John McCallf1549f62010-07-06 01:34:17 +00001113/// Emits a call or invoke instruction to the given function, depending
1114/// on the current state of the EH stack.
1115llvm::CallSite
1116CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1117 llvm::Value * const *ArgBegin,
1118 llvm::Value * const *ArgEnd,
1119 const llvm::Twine &Name) {
1120 llvm::BasicBlock *InvokeDest = getInvokeDest();
1121 if (!InvokeDest)
1122 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1123
1124 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1125 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1126 ArgBegin, ArgEnd, Name);
1127 EmitBlock(ContBB);
1128 return Invoke;
1129}
1130
Daniel Dunbar88b53962009-02-02 22:03:45 +00001131RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001132 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001133 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001134 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001135 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001136 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001137 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001138 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001139
1140 // Handle struct-return functions by passing a pointer to the
1141 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001142 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001143 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001144
1145
Chris Lattner5db7ae52009-06-13 00:26:38 +00001146 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001147 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001148 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001149 llvm::Value *Value = ReturnValue.getValue();
1150 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001151 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001152 Args.push_back(Value);
1153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001155 assert(CallInfo.arg_size() == CallArgs.size() &&
1156 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001157 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001158 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001159 I != E; ++I, ++info_it) {
1160 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001161 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001162
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001163 unsigned Alignment =
1164 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001165 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001166 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001167 if (RV.isScalar() || RV.isComplex()) {
1168 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001169 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001170 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001171 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1172 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001173 else
Mike Stump1eb44332009-09-09 15:08:12 +00001174 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001175 } else {
1176 Args.push_back(RV.getAggregateAddr());
1177 }
1178 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001179 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001180
Daniel Dunbar11434922009-01-26 21:26:08 +00001181 case ABIArgInfo::Ignore:
1182 break;
Chris Lattner800588f2010-07-29 06:26:06 +00001183
1184 case ABIArgInfo::Extend:
1185 case ABIArgInfo::Direct: {
1186 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001187 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1188 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001189 if (RV.isScalar())
1190 Args.push_back(RV.getScalarVal());
1191 else
1192 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1193 break;
1194 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001195
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001196 // FIXME: Avoid the conversion through memory if possible.
1197 llvm::Value *SrcPtr;
1198 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001199 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001200 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1201 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001202 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001203 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001204 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001205 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001206 SrcPtr = RV.getAggregateAddr();
Chris Lattnerce700162010-06-28 23:44:11 +00001207
Chris Lattner117e3f42010-07-30 04:02:24 +00001208 // If the value is offset in memory, apply the offset now.
1209 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1210 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1211 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1212 SrcPtr = Builder.CreateBitCast(SrcPtr,
1213 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1214
1215 }
1216
Chris Lattnerce700162010-06-28 23:44:11 +00001217 // If the coerce-to type is a first class aggregate, we flatten it and
1218 // pass the elements. Either way is semantically identical, but fast-isel
1219 // and the optimizer generally likes scalar values better than FCAs.
1220 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001221 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001222 SrcPtr = Builder.CreateBitCast(SrcPtr,
1223 llvm::PointerType::getUnqual(STy));
1224 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1225 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001226 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1227 // We don't know what we're loading from.
1228 LI->setAlignment(1);
1229 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001230 }
Chris Lattnerce700162010-06-28 23:44:11 +00001231 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001232 // In the simple case, just pass the coerced loaded value.
1233 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1234 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001235 }
1236
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001237 break;
1238 }
1239
Daniel Dunbar56273772008-09-17 00:51:38 +00001240 case ABIArgInfo::Expand:
1241 ExpandTypeToArgs(I->second, RV, Args);
1242 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001243 }
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattner5db7ae52009-06-13 00:26:38 +00001246 // If the callee is a bitcast of a function to a varargs pointer to function
1247 // type, check to see if we can remove the bitcast. This handles some cases
1248 // with unprototyped functions.
1249 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1250 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1251 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1252 const llvm::FunctionType *CurFT =
1253 cast<llvm::FunctionType>(CurPT->getElementType());
1254 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Chris Lattner5db7ae52009-06-13 00:26:38 +00001256 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1257 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001258 ActualFT->getNumParams() == CurFT->getNumParams() &&
1259 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001260 bool ArgsMatch = true;
1261 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1262 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1263 ArgsMatch = false;
1264 break;
1265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Chris Lattner5db7ae52009-06-13 00:26:38 +00001267 // Strip the cast if we can get away with it. This is a nice cleanup,
1268 // but also allows us to inline the function at -O0 if it is marked
1269 // always_inline.
1270 if (ArgsMatch)
1271 Callee = CalleeF;
1272 }
1273 }
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001275
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001276 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001277 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001278 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001279 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1280 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001281
John McCallf1549f62010-07-06 01:34:17 +00001282 llvm::BasicBlock *InvokeDest = 0;
1283 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1284 InvokeDest = getInvokeDest();
1285
Daniel Dunbard14151d2009-03-02 04:32:35 +00001286 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001287 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001288 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001289 } else {
1290 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001291 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001292 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001293 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001294 }
Chris Lattnerce933992010-06-29 16:40:28 +00001295 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001296 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001297
Daniel Dunbard14151d2009-03-02 04:32:35 +00001298 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001299 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001300
1301 // If the call doesn't return, finish the basic block and clear the
1302 // insertion point; this allows the rest of IRgen to discard
1303 // unreachable code.
1304 if (CS.doesNotReturn()) {
1305 Builder.CreateUnreachable();
1306 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Mike Stumpf5408fe2009-05-16 07:57:57 +00001308 // FIXME: For now, emit a dummy basic block because expr emitters in
1309 // generally are not ready to handle emitting expressions at unreachable
1310 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001311 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Daniel Dunbard14151d2009-03-02 04:32:35 +00001313 // Return a reasonable RValue.
1314 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001315 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001316
1317 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001318 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001319 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001320
1321 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001322 case ABIArgInfo::Indirect: {
1323 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001324 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001325 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001326 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001327 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001328 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1329 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001330
Daniel Dunbar11434922009-01-26 21:26:08 +00001331 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001332 // If we are ignoring an argument that had a result, make sure to
1333 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001334 return GetUndefRValue(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +00001335
1336 case ABIArgInfo::Extend:
1337 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001338 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1339 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001340 if (RetTy->isAnyComplexType()) {
1341 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1342 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1343 return RValue::getComplex(std::make_pair(Real, Imag));
1344 }
1345 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1346 llvm::Value *DestPtr = ReturnValue.getValue();
1347 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001348
Chris Lattner800588f2010-07-29 06:26:06 +00001349 if (!DestPtr) {
1350 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1351 DestIsVolatile = false;
1352 }
1353 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1354 return RValue::getAggregate(DestPtr);
1355 }
1356 return RValue::get(CI);
1357 }
1358
Anders Carlssond2490a92009-12-24 20:40:36 +00001359 llvm::Value *DestPtr = ReturnValue.getValue();
1360 bool DestIsVolatile = ReturnValue.isVolatile();
1361
1362 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001363 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001364 DestIsVolatile = false;
1365 }
1366
Chris Lattner117e3f42010-07-30 04:02:24 +00001367 // If the value is offset in memory, apply the offset now.
1368 llvm::Value *StorePtr = DestPtr;
1369 if (unsigned Offs = RetAI.getDirectOffset()) {
1370 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1371 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1372 StorePtr = Builder.CreateBitCast(StorePtr,
1373 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1374 }
1375 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1376
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001377 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001378 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001379 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001380 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001381 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001382 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001383 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001384
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001385 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001386 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001387 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001388
1389 assert(0 && "Unhandled ABIArgInfo::Kind");
1390 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001391}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001392
1393/* VarArg handling */
1394
1395llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1396 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1397}