blob: 6e32e1e1b4e6f6bfbad0bbaf2425b25aaeb6f208 [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 Lattnerbcaedae2010-06-30 19:14:05 +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 Lattnerbcaedae2010-06-30 19:14:05 +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 Lattnerbcaedae2010-06-30 19:14:05 +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 Lattnerbcaedae2010-06-30 19:14:05 +000081 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000082}
83
84const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000085CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
86 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000087 llvm::SmallVector<CanQualType, 16> ArgTys;
Chris Lattnerbcaedae2010-06-30 19:14:05 +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 Lattnerbcaedae2010-06-30 19:14:05 +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,
Chris Lattnerbb521142010-06-29 18:13:52 +0000243 ArgTys.data(), ArgTys.size());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000244 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000245
246 // Compute ABI information.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000247 getABIInfo().computeInfo(*FI);
Chris Lattner800588f2010-07-29 06:26:06 +0000248
249 // Loop over all of the computed argument and return value info. If any of
250 // them are direct or extend without a specified coerce type, specify the
251 // default now.
252 ABIArgInfo &RetInfo = FI->getReturnInfo();
253 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
254 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
255
256 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
257 I != E; ++I)
258 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
259 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000260
Chris Lattnera9fa8582010-07-01 06:20:47 +0000261 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
262 // types, resolve them now. These pointers may point to this function, which
263 // we *just* filled in the FunctionInfo for.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000264 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattnera9fa8582010-07-01 06:20:47 +0000265 HandleLateResolvedPointers();
Chris Lattnera9fa8582010-07-01 06:20:47 +0000266
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000267 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000268}
269
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000270CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattnerbb521142010-06-29 18:13:52 +0000271 bool _NoReturn, unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000272 CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000273 const CanQualType *ArgTys,
274 unsigned NumArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000275 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000276 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000277 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000278{
Chris Lattnerbb521142010-06-29 18:13:52 +0000279 NumArgs = NumArgTys;
Chris Lattnerce700162010-06-28 23:44:11 +0000280
281 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattnerbb521142010-06-29 18:13:52 +0000282 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000283 Args[0].type = ResTy;
Chris Lattnerbb521142010-06-29 18:13:52 +0000284 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000285 Args[1 + i].type = ArgTys[i];
286}
287
288/***/
289
Mike Stump1eb44332009-09-09 15:08:12 +0000290void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000291 std::vector<const llvm::Type*> &ArgTys,
292 bool IsRecursive) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000293 const RecordType *RT = Ty->getAsStructureType();
294 assert(RT && "Can only expand structure types.");
295 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000296 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000297 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000299 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
300 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000301 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000302 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000303 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Daniel Dunbar56273772008-09-17 00:51:38 +0000305 QualType FT = FD->getType();
Chris Lattnerdeabde22010-07-28 18:24:28 +0000306 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000307 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000308 else
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000309 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar56273772008-09-17 00:51:38 +0000310 }
311}
312
Mike Stump1eb44332009-09-09 15:08:12 +0000313llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000314CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
315 llvm::Function::arg_iterator AI) {
316 const RecordType *RT = Ty->getAsStructureType();
317 assert(RT && "Can only expand structure types.");
318
319 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000320 assert(LV.isSimple() &&
321 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000322 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000323 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
324 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000325 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000326 QualType FT = FD->getType();
327
328 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000329 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000330 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
331 AI = ExpandTypeFromArgs(FT, LV, AI);
332 } else {
333 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
334 ++AI;
335 }
336 }
337
338 return AI;
339}
340
Mike Stump1eb44332009-09-09 15:08:12 +0000341void
342CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000343 llvm::SmallVector<llvm::Value*, 16> &Args) {
344 const RecordType *RT = Ty->getAsStructureType();
345 assert(RT && "Can only expand structure types.");
346
347 RecordDecl *RD = RT->getDecl();
348 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
349 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000350 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
351 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000352 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000353 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Daniel Dunbar56273772008-09-17 00:51:38 +0000355 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000356 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000357 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
358 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
359 } else {
360 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000361 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000362 "Unexpected non-scalar rvalue during struct expansion.");
363 Args.push_back(RV.getScalarVal());
364 }
365 }
366}
367
Chris Lattnere7bb7772010-06-27 06:04:18 +0000368/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000369/// accessing some number of bytes out of it, try to gep into the struct to get
370/// at its inner goodness. Dive as deep as possible without entering an element
371/// with an in-memory size smaller than DstSize.
372static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000373EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
374 const llvm::StructType *SrcSTy,
375 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000376 // We can't dive into a zero-element struct.
377 if (SrcSTy->getNumElements() == 0) return SrcPtr;
378
379 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
380
381 // If the first elt is at least as large as what we're looking for, or if the
382 // first element is the same size as the whole struct, we can enter it.
383 uint64_t FirstEltSize =
384 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
385 if (FirstEltSize < DstSize &&
386 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
387 return SrcPtr;
388
389 // GEP into the first element.
390 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
391
392 // If the first element is a struct, recurse.
393 const llvm::Type *SrcTy =
394 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
395 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000396 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000397
398 return SrcPtr;
399}
400
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000401/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
402/// are either integers or pointers. This does a truncation of the value if it
403/// is too large or a zero extension if it is too small.
404static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
405 const llvm::Type *Ty,
406 CodeGenFunction &CGF) {
407 if (Val->getType() == Ty)
408 return Val;
409
410 if (isa<llvm::PointerType>(Val->getType())) {
411 // If this is Pointer->Pointer avoid conversion to and from int.
412 if (isa<llvm::PointerType>(Ty))
413 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
414
415 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000416 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000417 }
418
419 const llvm::Type *DestIntTy = Ty;
420 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000421 DestIntTy = CGF.IntPtrTy;
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000422
423 if (Val->getType() != DestIntTy)
424 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
425
426 if (isa<llvm::PointerType>(Ty))
427 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
428 return Val;
429}
430
Chris Lattner08dd2a02010-06-27 05:56:15 +0000431
432
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000433/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
434/// a pointer to an object of type \arg Ty.
435///
436/// This safely handles the case when the src type is smaller than the
437/// destination type; in this situation the values of bits which not
438/// present in the src are undefined.
439static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
440 const llvm::Type *Ty,
441 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000442 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000443 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000444
445 // If SrcTy and Ty are the same, just do a load.
446 if (SrcTy == Ty)
447 return CGF.Builder.CreateLoad(SrcPtr);
448
Duncan Sands9408c452009-05-09 07:08:47 +0000449 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000450
451 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000452 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000453 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
454 }
455
456 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000457
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000458 // If the source and destination are integer or pointer types, just do an
459 // extension or truncation to the desired type.
460 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
461 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
462 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
463 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
464 }
465
Daniel Dunbarb225be42009-02-03 05:59:18 +0000466 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000467 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000468 // Generally SrcSize is never greater than DstSize, since this means we are
469 // losing bits. However, this can happen in cases where the structure has
470 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000471 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000472 // FIXME: Assert that we aren't truncating non-padding bits when have access
473 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000474 llvm::Value *Casted =
475 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000476 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
477 // FIXME: Use better alignment / avoid requiring aligned load.
478 Load->setAlignment(1);
479 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000480 }
Chris Lattner35b21b82010-06-27 01:06:27 +0000481
482 // Otherwise do coercion through memory. This is stupid, but
483 // simple.
484 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
485 llvm::Value *Casted =
486 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
487 llvm::StoreInst *Store =
488 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
489 // FIXME: Use better alignment / avoid requiring aligned store.
490 Store->setAlignment(1);
491 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000492}
493
494/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
495/// where the source and destination may have different types.
496///
497/// This safely handles the case when the src type is larger than the
498/// destination type; the upper bits of the src will be lost.
499static void CreateCoercedStore(llvm::Value *Src,
500 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000501 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000502 CodeGenFunction &CGF) {
503 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000504 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000505 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000506 if (SrcTy == DstTy) {
507 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
508 return;
509 }
510
511 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
512
Chris Lattnere7bb7772010-06-27 06:04:18 +0000513 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
514 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
515 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
516 }
517
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000518 // If the source and destination are integer or pointer types, just do an
519 // extension or truncation to the desired type.
520 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
521 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
522 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
523 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
524 return;
525 }
526
Duncan Sands9408c452009-05-09 07:08:47 +0000527 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000528
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000529 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000530 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000531 llvm::Value *Casted =
532 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000533 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000534 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000535 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000536 // Otherwise do coercion through memory. This is stupid, but
537 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000538
539 // Generally SrcSize is never greater than DstSize, since this means we are
540 // losing bits. However, this can happen in cases where the structure has
541 // additional padding, for example due to a user specified alignment.
542 //
543 // FIXME: Assert that we aren't truncating non-padding bits when have access
544 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000545 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
546 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000547 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000548 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000549 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
550 // FIXME: Use better alignment / avoid requiring aligned load.
551 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000552 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000553 }
554}
555
Daniel Dunbar56273772008-09-17 00:51:38 +0000556/***/
557
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000558bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000559 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000560}
561
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000562bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
563 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
564 switch (BT->getKind()) {
565 default:
566 return false;
567 case BuiltinType::Float:
568 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
569 case BuiltinType::Double:
570 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
571 case BuiltinType::LongDouble:
572 return getContext().Target.useObjCFPRetForRealType(
573 TargetInfo::LongDouble);
574 }
575 }
576
577 return false;
578}
579
John McCallc0bf4622010-02-23 00:48:20 +0000580const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
581 const CGFunctionInfo &FI = getFunctionInfo(GD);
582
583 // For definition purposes, don't consider a K&R function variadic.
584 bool Variadic = false;
585 if (const FunctionProtoType *FPT =
586 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
587 Variadic = FPT->isVariadic();
588
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000589 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000590}
591
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000592const llvm::FunctionType *
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000593CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
594 bool IsRecursive) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000595 std::vector<const llvm::Type*> ArgTys;
596
597 const llvm::Type *ResultType = 0;
598
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000599 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000600 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000601 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000602 case ABIArgInfo::Expand:
603 assert(0 && "Invalid ABI kind for return argument");
604
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000605 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000606 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000607 ResultType = RetAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000608 break;
609
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000610 case ABIArgInfo::Indirect: {
611 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000612 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000613 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000614 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
615 break;
616 }
617
Daniel Dunbar11434922009-01-26 21:26:08 +0000618 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000619 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000620 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
623 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000624 ie = FI.arg_end(); it != ie; ++it) {
625 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000627 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000628 case ABIArgInfo::Ignore:
629 break;
630
Chris Lattner800588f2010-07-29 06:26:06 +0000631 case ABIArgInfo::Indirect: {
632 // indirect arguments are always on the stack, which is addr space #0.
633 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
634 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
635 break;
636 }
637
638 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000639 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000640 // If the coerce-to type is a first class aggregate, flatten it. Either
641 // way is semantically identical, but fast-isel and the optimizer
642 // generally likes scalar values better than FCAs.
643 const llvm::Type *ArgTy = AI.getCoerceToType();
644 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
645 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
646 ArgTys.push_back(STy->getElementType(i));
647 } else {
648 ArgTys.push_back(ArgTy);
649 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000650 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000653 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000654 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000655 break;
656 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000657 }
658
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000659 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000660}
661
Anders Carlssonecf282b2009-11-24 05:08:52 +0000662const llvm::Type *
Anders Carlsson046c2942010-04-17 20:15:18 +0000663CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlssonecf282b2009-11-24 05:08:52 +0000664 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
665
Eli Friedmanc00129a2010-05-30 06:03:20 +0000666 if (!VerifyFuncTypeComplete(FPT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000667 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic(), false);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000668
669 return llvm::OpaqueType::get(getLLVMContext());
670}
671
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000672void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000673 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000674 AttributeListType &PAL,
675 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000676 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000677 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000678
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000679 CallingConv = FI.getEffectiveCallingConvention();
680
John McCall04a67a62010-02-05 21:31:56 +0000681 if (FI.isNoReturn())
682 FuncAttrs |= llvm::Attribute::NoReturn;
683
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000684 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000685 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000686 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000687 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000688 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
689 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
690 if (FPT && FPT->hasEmptyExceptionSpec())
691 FuncAttrs |= llvm::Attribute::NoUnwind;
692 }
693
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000694 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000695 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000696 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000697 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000698 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000699 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000700 if (TargetDecl->hasAttr<MallocAttr>())
701 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000702 }
703
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000704 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000705 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000706 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000707 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000708 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000709 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000710
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000711 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000712 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000713 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000714 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000715 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000716 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000717 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000718 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000719 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000720 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000721 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000722 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000723 break;
724
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000725 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000726 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000727 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000728 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000729 // sret disables readnone and readonly
730 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
731 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000732 break;
733
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000734 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000735 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000736 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000737
Devang Patela2c69122008-09-26 22:53:57 +0000738 if (RetAttrs)
739 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000740
Chris Lattnerdeabde22010-07-28 18:24:28 +0000741 // FIXME: we need to honor command line settings also.
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000742 // FIXME: RegParm should be reduced in case of nested functions and/or global
743 // register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000744 signed RegParm = FI.getRegParm();
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000745
746 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000747 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000748 ie = FI.arg_end(); it != ie; ++it) {
749 QualType ParamType = it->type;
750 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000751 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000752
John McCalld8e10d22010-03-27 00:47:27 +0000753 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
754 // have the corresponding parameter variable. It doesn't make
755 // sense to do it here because parameters are so fucked up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000756 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000757 case ABIArgInfo::Extend:
758 if (ParamType->isSignedIntegerType())
759 Attributes |= llvm::Attribute::SExt;
760 else if (ParamType->isUnsignedIntegerType())
761 Attributes |= llvm::Attribute::ZExt;
762 // FALL THROUGH
763 case ABIArgInfo::Direct:
764 if (RegParm > 0 &&
765 (ParamType->isIntegerType() || ParamType->isPointerType())) {
766 RegParm -=
767 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
768 if (RegParm >= 0)
769 Attributes |= llvm::Attribute::InReg;
770 }
771 // FIXME: handle sseregparm someday...
772
Chris Lattnerce700162010-06-28 23:44:11 +0000773 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000774 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
775 Index += STy->getNumElements()-1; // 1 will be added below.
776 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000777
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000778 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000779 if (AI.getIndirectByVal())
780 Attributes |= llvm::Attribute::ByVal;
781
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000782 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000783 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000784 // byval disables readnone and readonly.
785 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
786 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000787 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000788
Daniel Dunbar11434922009-01-26 21:26:08 +0000789 case ABIArgInfo::Ignore:
790 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000791 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000792
Daniel Dunbar56273772008-09-17 00:51:38 +0000793 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000794 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000795 // FIXME: This is rather inefficient. Do we ever actually need to do
796 // anything here? The result should be just reconstructed on the other
797 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000798 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000799 Index += Tys.size();
800 continue;
801 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Devang Patel761d7f72008-09-25 21:02:23 +0000804 if (Attributes)
805 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000806 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000807 }
Devang Patela2c69122008-09-26 22:53:57 +0000808 if (FuncAttrs)
809 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000810}
811
Daniel Dunbar88b53962009-02-02 22:03:45 +0000812void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
813 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000814 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000815 // If this is an implicit-return-zero function, go ahead and
816 // initialize the return value. TODO: it might be nice to have
817 // a more general mechanism for this that didn't require synthesized
818 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000819 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000820 if (FD->hasImplicitReturnZero()) {
821 QualType RetTy = FD->getResultType().getUnqualifiedType();
822 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000823 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000824 Builder.CreateStore(Zero, ReturnValue);
825 }
826 }
827
Mike Stumpf5408fe2009-05-16 07:57:57 +0000828 // FIXME: We no longer need the types from FunctionArgList; lift up and
829 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000830
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000831 // Emit allocs for param decls. Give the LLVM Argument nodes names.
832 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000834 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000835 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000836 AI->setName("agg.result");
837 ++AI;
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000840 assert(FI.arg_size() == Args.size() &&
841 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000842 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000843 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000844 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000845 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000846 QualType Ty = info_it->type;
847 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000848
849 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000850 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000851 llvm::Value *V = AI;
Daniel Dunbar1f745982009-02-05 09:16:39 +0000852 if (hasAggregateLLVMType(Ty)) {
853 // Do nothing, aggregates and complex variables are accessed by
854 // reference.
855 } else {
856 // Load scalar value from indirect argument.
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000857 unsigned Alignment = getContext().getTypeAlignInChars(Ty).getQuantity();
858 V = EmitLoadOfScalar(V, false, Alignment, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000859 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
860 // This must be a promotion, for something like
861 // "void a(x) short x; {..."
862 V = EmitScalarConversion(V, Ty, Arg->getType());
863 }
864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000866 break;
867 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000868
869 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000870 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000871 // If we have the trivial case, handle it with no muss and fuss.
872 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000873 ArgI.getCoerceToType() == ConvertType(Ty) &&
874 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000875 assert(AI != Fn->arg_end() && "Argument mismatch!");
876 llvm::Value *V = AI;
877
John McCalld8e10d22010-03-27 00:47:27 +0000878 if (Arg->getType().isRestrictQualified())
879 AI->addAttr(llvm::Attribute::NoAlias);
880
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000881 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
882 // This must be a promotion, for something like
883 // "void a(x) short x; {..."
884 V = EmitScalarConversion(V, Ty, Arg->getType());
885 }
Chris Lattner800588f2010-07-29 06:26:06 +0000886 EmitParmDecl(*Arg, V);
887 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000888 }
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Chris Lattner121b3fa2010-07-05 20:21:00 +0000890 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Chris Lattnerdeabde22010-07-28 18:24:28 +0000891
892 // The alignment we need to use is the max of the requested alignment for
893 // the argument plus the alignment required by our access code below.
894 unsigned AlignmentToUse =
895 CGF.CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
896 AlignmentToUse = std::max(AlignmentToUse,
897 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
898
899 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000900 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000901 llvm::Value *Ptr = V; // Pointer to store into.
902
903 // If the value is offset in memory, apply the offset now.
904 if (unsigned Offs = ArgI.getDirectOffset()) {
905 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
906 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
907 Ptr = Builder.CreateBitCast(Ptr,
908 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
909 }
Chris Lattner309c59f2010-06-29 00:06:42 +0000910
911 // If the coerce-to type is a first class aggregate, we flatten it and
912 // pass the elements. Either way is semantically identical, but fast-isel
913 // and the optimizer generally likes scalar values better than FCAs.
914 if (const llvm::StructType *STy =
915 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000916 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
917
918 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
919 assert(AI != Fn->arg_end() && "Argument mismatch!");
920 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
921 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
922 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000923 }
924 } else {
925 // Simple case, just do a coerced store of the argument into the alloca.
926 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000927 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +0000928 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +0000929 }
930
931
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000932 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000933 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000934 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000935 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
936 // This must be a promotion, for something like
937 // "void a(x) short x; {..."
938 V = EmitScalarConversion(V, Ty, Arg->getType());
939 }
940 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000941 EmitParmDecl(*Arg, V);
Chris Lattnerce700162010-06-28 23:44:11 +0000942 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000943 }
Chris Lattner800588f2010-07-29 06:26:06 +0000944
945 case ABIArgInfo::Expand: {
946 // If this structure was expanded into multiple arguments then
947 // we need to create a temporary and reconstruct it from the
948 // arguments.
949 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
950 // FIXME: What are the right qualifiers here?
951 llvm::Function::arg_iterator End =
952 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
953 EmitParmDecl(*Arg, Temp);
954
955 // Name the arguments used in expansion and increment AI.
956 unsigned Index = 0;
957 for (; AI != End; ++AI, ++Index)
958 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
959 continue;
960 }
961
962 case ABIArgInfo::Ignore:
963 // Initialize the local variable appropriately.
964 if (hasAggregateLLVMType(Ty))
965 EmitParmDecl(*Arg, CreateMemTemp(Ty));
966 else
967 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
968
969 // Skip increment, no matching LLVM parameter.
970 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000971 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000972
973 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000974 }
975 assert(AI == Fn->arg_end() && "Argument mismatch!");
976}
977
Chris Lattner35b21b82010-06-27 01:06:27 +0000978void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000979 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000980 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000981 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000982 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000983 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +0000984
Dan Gohman4751a532010-07-20 20:13:52 +0000985 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000986 llvm::Value *RV = 0;
987 QualType RetTy = FI.getReturnType();
988 const ABIArgInfo &RetAI = FI.getReturnInfo();
989
990 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000991 case ABIArgInfo::Indirect: {
992 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +0000993 if (RetTy->isAnyComplexType()) {
994 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
995 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
996 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
997 // Do nothing; aggregrates get evaluated directly into the destination.
998 } else {
999 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001000 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001001 }
1002 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001003 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001004
1005 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001006 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001007 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1008 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001009 // The internal return value temp always will have pointer-to-return-type
1010 // type, just do a load.
1011
1012 // If the instruction right before the insertion point is a store to the
1013 // return value, we can elide the load, zap the store, and usually zap the
1014 // alloca.
1015 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1016 llvm::StoreInst *SI = 0;
1017 if (InsertBB->empty() ||
1018 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1019 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1020 RV = Builder.CreateLoad(ReturnValue);
1021 } else {
1022 // Get the stored value and nuke the now-dead store.
1023 RetDbgLoc = SI->getDebugLoc();
1024 RV = SI->getValueOperand();
1025 SI->eraseFromParent();
1026
1027 // If that was the only use of the return value, nuke it as well now.
1028 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1029 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1030 ReturnValue = 0;
1031 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001032 }
Chris Lattner800588f2010-07-29 06:26:06 +00001033 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001034 llvm::Value *V = ReturnValue;
1035 // If the value is offset in memory, apply the offset now.
1036 if (unsigned Offs = RetAI.getDirectOffset()) {
1037 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1038 V = Builder.CreateConstGEP1_32(V, Offs);
1039 V = Builder.CreateBitCast(V,
1040 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1041 }
1042
1043 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001044 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001045 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001046
Chris Lattner800588f2010-07-29 06:26:06 +00001047 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001048 break;
1049
1050 case ABIArgInfo::Expand:
1051 assert(0 && "Invalid ABI kind for return argument");
1052 }
1053
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001054 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001055 if (!RetDbgLoc.isUnknown())
1056 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001057}
1058
John McCall27360712010-05-26 22:34:26 +00001059RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1060 // StartFunction converted the ABI-lowered parameter(s) into a
1061 // local alloca. We need to turn that into an r-value suitable
1062 // for EmitCall.
1063 llvm::Value *Local = GetAddrOfLocalVar(Param);
1064
1065 QualType ArgType = Param->getType();
1066
1067 // For the most part, we just need to load the alloca, except:
1068 // 1) aggregate r-values are actually pointers to temporaries, and
1069 // 2) references to aggregates are pointers directly to the aggregate.
1070 // I don't know why references to non-aggregates are different here.
1071 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1072 if (hasAggregateLLVMType(RefType->getPointeeType()))
1073 return RValue::getAggregate(Local);
1074
1075 // Locals which are references to scalars are represented
1076 // with allocas holding the pointer.
1077 return RValue::get(Builder.CreateLoad(Local));
1078 }
1079
1080 if (ArgType->isAnyComplexType())
1081 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1082
1083 if (hasAggregateLLVMType(ArgType))
1084 return RValue::getAggregate(Local);
1085
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001086 unsigned Alignment = getContext().getDeclAlign(Param).getQuantity();
1087 return RValue::get(EmitLoadOfScalar(Local, false, Alignment, ArgType));
John McCall27360712010-05-26 22:34:26 +00001088}
1089
Anders Carlsson0139bb92009-04-08 20:47:54 +00001090RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00001091 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +00001092 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Anders Carlsson0139bb92009-04-08 20:47:54 +00001094 return EmitAnyExprToTemp(E);
1095}
1096
John McCallf1549f62010-07-06 01:34:17 +00001097/// Emits a call or invoke instruction to the given function, depending
1098/// on the current state of the EH stack.
1099llvm::CallSite
1100CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1101 llvm::Value * const *ArgBegin,
1102 llvm::Value * const *ArgEnd,
1103 const llvm::Twine &Name) {
1104 llvm::BasicBlock *InvokeDest = getInvokeDest();
1105 if (!InvokeDest)
1106 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1107
1108 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1109 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1110 ArgBegin, ArgEnd, Name);
1111 EmitBlock(ContBB);
1112 return Invoke;
1113}
1114
Daniel Dunbar88b53962009-02-02 22:03:45 +00001115RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001116 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001117 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001118 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001119 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001120 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001121 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001122 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001123
1124 // Handle struct-return functions by passing a pointer to the
1125 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001126 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001127 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001128
1129
Chris Lattner5db7ae52009-06-13 00:26:38 +00001130 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001131 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001132 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001133 llvm::Value *Value = ReturnValue.getValue();
1134 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001135 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001136 Args.push_back(Value);
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001139 assert(CallInfo.arg_size() == CallArgs.size() &&
1140 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001141 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001142 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001143 I != E; ++I, ++info_it) {
1144 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001145 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001146
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001147 unsigned Alignment =
1148 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001149 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001150 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001151 if (RV.isScalar() || RV.isComplex()) {
1152 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001153 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001154 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001155 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1156 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001157 else
Mike Stump1eb44332009-09-09 15:08:12 +00001158 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001159 } else {
1160 Args.push_back(RV.getAggregateAddr());
1161 }
1162 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001163 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001164
Daniel Dunbar11434922009-01-26 21:26:08 +00001165 case ABIArgInfo::Ignore:
1166 break;
Chris Lattner800588f2010-07-29 06:26:06 +00001167
1168 case ABIArgInfo::Extend:
1169 case ABIArgInfo::Direct: {
1170 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001171 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1172 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001173 if (RV.isScalar())
1174 Args.push_back(RV.getScalarVal());
1175 else
1176 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1177 break;
1178 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001179
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001180 // FIXME: Avoid the conversion through memory if possible.
1181 llvm::Value *SrcPtr;
1182 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001183 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001184 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1185 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001186 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001187 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001188 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001189 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001190 SrcPtr = RV.getAggregateAddr();
Chris Lattnerce700162010-06-28 23:44:11 +00001191
Chris Lattner117e3f42010-07-30 04:02:24 +00001192 // If the value is offset in memory, apply the offset now.
1193 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1194 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1195 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1196 SrcPtr = Builder.CreateBitCast(SrcPtr,
1197 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1198
1199 }
1200
Chris Lattnerce700162010-06-28 23:44:11 +00001201 // If the coerce-to type is a first class aggregate, we flatten it and
1202 // pass the elements. Either way is semantically identical, but fast-isel
1203 // and the optimizer generally likes scalar values better than FCAs.
1204 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001205 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001206 SrcPtr = Builder.CreateBitCast(SrcPtr,
1207 llvm::PointerType::getUnqual(STy));
1208 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1209 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001210 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1211 // We don't know what we're loading from.
1212 LI->setAlignment(1);
1213 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001214 }
Chris Lattnerce700162010-06-28 23:44:11 +00001215 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001216 // In the simple case, just pass the coerced loaded value.
1217 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1218 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001219 }
1220
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001221 break;
1222 }
1223
Daniel Dunbar56273772008-09-17 00:51:38 +00001224 case ABIArgInfo::Expand:
1225 ExpandTypeToArgs(I->second, RV, Args);
1226 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001227 }
1228 }
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Chris Lattner5db7ae52009-06-13 00:26:38 +00001230 // If the callee is a bitcast of a function to a varargs pointer to function
1231 // type, check to see if we can remove the bitcast. This handles some cases
1232 // with unprototyped functions.
1233 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1234 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1235 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1236 const llvm::FunctionType *CurFT =
1237 cast<llvm::FunctionType>(CurPT->getElementType());
1238 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Chris Lattner5db7ae52009-06-13 00:26:38 +00001240 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1241 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001242 ActualFT->getNumParams() == CurFT->getNumParams() &&
1243 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001244 bool ArgsMatch = true;
1245 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1246 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1247 ArgsMatch = false;
1248 break;
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Chris Lattner5db7ae52009-06-13 00:26:38 +00001251 // Strip the cast if we can get away with it. This is a nice cleanup,
1252 // but also allows us to inline the function at -O0 if it is marked
1253 // always_inline.
1254 if (ArgsMatch)
1255 Callee = CalleeF;
1256 }
1257 }
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001259
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001260 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001261 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001262 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001263 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1264 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001265
John McCallf1549f62010-07-06 01:34:17 +00001266 llvm::BasicBlock *InvokeDest = 0;
1267 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1268 InvokeDest = getInvokeDest();
1269
Daniel Dunbard14151d2009-03-02 04:32:35 +00001270 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001271 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001272 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001273 } else {
1274 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001275 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001276 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001277 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001278 }
Chris Lattnerce933992010-06-29 16:40:28 +00001279 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001280 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001281
Daniel Dunbard14151d2009-03-02 04:32:35 +00001282 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001283 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001284
1285 // If the call doesn't return, finish the basic block and clear the
1286 // insertion point; this allows the rest of IRgen to discard
1287 // unreachable code.
1288 if (CS.doesNotReturn()) {
1289 Builder.CreateUnreachable();
1290 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Mike Stumpf5408fe2009-05-16 07:57:57 +00001292 // FIXME: For now, emit a dummy basic block because expr emitters in
1293 // generally are not ready to handle emitting expressions at unreachable
1294 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001295 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Daniel Dunbard14151d2009-03-02 04:32:35 +00001297 // Return a reasonable RValue.
1298 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001299 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001300
1301 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001302 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001303 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001304
1305 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001306 case ABIArgInfo::Indirect: {
1307 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001308 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001309 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001310 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001311 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001312 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1313 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001314
Daniel Dunbar11434922009-01-26 21:26:08 +00001315 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001316 // If we are ignoring an argument that had a result, make sure to
1317 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001318 return GetUndefRValue(RetTy);
Chris Lattner800588f2010-07-29 06:26:06 +00001319
1320 case ABIArgInfo::Extend:
1321 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001322 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1323 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001324 if (RetTy->isAnyComplexType()) {
1325 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1326 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1327 return RValue::getComplex(std::make_pair(Real, Imag));
1328 }
1329 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1330 llvm::Value *DestPtr = ReturnValue.getValue();
1331 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001332
Chris Lattner800588f2010-07-29 06:26:06 +00001333 if (!DestPtr) {
1334 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1335 DestIsVolatile = false;
1336 }
1337 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1338 return RValue::getAggregate(DestPtr);
1339 }
1340 return RValue::get(CI);
1341 }
1342
Anders Carlssond2490a92009-12-24 20:40:36 +00001343 llvm::Value *DestPtr = ReturnValue.getValue();
1344 bool DestIsVolatile = ReturnValue.isVolatile();
1345
1346 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001347 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001348 DestIsVolatile = false;
1349 }
1350
Chris Lattner117e3f42010-07-30 04:02:24 +00001351 // If the value is offset in memory, apply the offset now.
1352 llvm::Value *StorePtr = DestPtr;
1353 if (unsigned Offs = RetAI.getDirectOffset()) {
1354 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1355 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1356 StorePtr = Builder.CreateBitCast(StorePtr,
1357 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1358 }
1359 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1360
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001361 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001362 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001363 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001364 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001365 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001366 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001367 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001368
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001369 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001370 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001371 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001372
1373 assert(0 && "Unhandled ABIArgInfo::Kind");
1374 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001375}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001376
1377/* VarArg handling */
1378
1379llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1380 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1381}