blob: 6072c1c499d587c998fd3c0ee1cd6c28de19929d [file] [log] [blame]
Daniel Dunbar3d7c90b2008-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 McCall5d865c322010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Chris Lattnere70a0072010-06-29 16:40:28 +000017#include "ABIInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000018#include "CodeGenFunction.h"
Daniel Dunbarc68897d2008-09-10 00:41:16 +000019#include "CodeGenModule.h"
Daniel Dunbard9eff3d2008-10-13 17:02:26 +000020#include "clang/Basic/TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000021#include "clang/AST/Decl.h"
Anders Carlssonb15b55c2009-04-03 22:48:58 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000023#include "clang/AST/DeclObjC.h"
Chandler Carruth85098242010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Devang Patel3e1f51b2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbarb960b7b2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000028using namespace clang;
29using namespace CodeGen;
30
31/***/
32
John McCallab26cfa2010-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 Gregora941dca2010-05-18 16:57:00 +000038 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
John McCallab26cfa2010-02-05 21:31:56 +000039 }
40}
41
John McCall8ee376f2010-02-24 07:14:12 +000042/// Derives the 'this' type for codegen purposes, i.e. ignoring method
43/// qualification.
44/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000045static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
46 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
47 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000048}
49
John McCall8ee376f2010-02-24 07:14:12 +000050/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000051static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
52 return MD->getType()->getCanonicalTypeUnqualified()
53 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000054}
55
56/// Returns the "extra-canonicalized" return type, which discards
57/// qualifiers on the return type. Codegen doesn't care about them,
58/// and it makes ABI code a little easier to be able to assume that
59/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000060static CanQualType GetReturnType(QualType RetTy) {
61 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000062}
63
64const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000065CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
66 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000067 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
68 llvm::SmallVector<CanQualType, 16>(),
Chris Lattner5c740f12010-06-30 19:14:05 +000069 FTNP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000070}
71
72/// \param Args - contains any initial parameters besides those
73/// in the formal type
74static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCall2da83a32010-02-26 00:48:12 +000075 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +000076 CanQual<FunctionProtoType> FTP,
77 bool IsRecursive = false) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000078 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000079 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000080 ArgTys.push_back(FTP->getArgType(i));
John McCall2da83a32010-02-26 00:48:12 +000081 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Chris Lattner5c740f12010-06-30 19:14:05 +000082 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000083}
84
85const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000086CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
87 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000088 llvm::SmallVector<CanQualType, 16> ArgTys;
Chris Lattner5c740f12010-06-30 19:14:05 +000089 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbar7feafc72009-09-11 22:24:53 +000090}
91
John McCallab26cfa2010-02-05 21:31:56 +000092static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000093 // Set the appropriate calling convention for the Function.
94 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000095 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000096
97 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000098 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000099
Douglas Gregora941dca2010-05-18 16:57:00 +0000100 if (D->hasAttr<ThisCallAttr>())
101 return CC_X86ThisCall;
102
John McCallab26cfa2010-02-05 21:31:56 +0000103 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000104}
105
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000106const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
107 const FunctionProtoType *FTP) {
John McCall2da83a32010-02-26 00:48:12 +0000108 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000109
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000110 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000111 ArgTys.push_back(GetThisType(Context, RD));
112
113 return ::getFunctionInfo(*this, ArgTys,
John McCall2da83a32010-02-26 00:48:12 +0000114 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000115}
116
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000117const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000118 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000119
Chris Lattnerbea5b622009-05-12 20:27:19 +0000120 // Add the 'this' pointer unless this is a static method.
121 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000122 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000123
John McCall8ee376f2010-02-24 07:14:12 +0000124 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000125}
126
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000127const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
128 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000129 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000130 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000131 CanQualType ResTy = Context.VoidTy;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000132
John McCall5d865c322010-08-31 07:33:07 +0000133 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall8ee376f2010-02-24 07:14:12 +0000134
John McCall5d865c322010-08-31 07:33:07 +0000135 CanQual<FunctionProtoType> FTP = GetFormalType(D);
136
137 // Add the formal parameters.
138 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
139 ArgTys.push_back(FTP->getArgType(i));
140
141 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000142}
143
144const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
145 CXXDtorType Type) {
John McCall5d865c322010-08-31 07:33:07 +0000146 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCall2da83a32010-02-26 00:48:12 +0000147 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000148 CanQualType ResTy = Context.VoidTy;
John McCall8ee376f2010-02-24 07:14:12 +0000149
John McCall5d865c322010-08-31 07:33:07 +0000150 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
151
152 CanQual<FunctionProtoType> FTP = GetFormalType(D);
153 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
154
155 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000156}
157
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000158const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000159 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000160 if (MD->isInstance())
161 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000162
John McCall2da83a32010-02-26 00:48:12 +0000163 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
164 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000165 if (isa<FunctionNoProtoType>(FTy))
John McCall2da83a32010-02-26 00:48:12 +0000166 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
167 assert(isa<FunctionProtoType>(FTy));
168 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000169}
170
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000171const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000172 llvm::SmallVector<CanQualType, 16> ArgTys;
173 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
174 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000175 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000176 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000177 e = MD->param_end(); i != e; ++i) {
178 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
179 }
180 return getFunctionInfo(GetReturnType(MD->getResultType()),
181 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000182 FunctionType::ExtInfo(
183 /*NoReturn*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000184 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000185 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000186}
187
Anders Carlsson6710c532010-02-06 02:44:09 +0000188const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
189 // FIXME: Do we need to handle ObjCMethodDecl?
190 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
191
192 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
193 return getFunctionInfo(CD, GD.getCtorType());
194
195 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
196 return getFunctionInfo(DD, GD.getDtorType());
197
198 return getFunctionInfo(FD);
199}
200
Mike Stump11289f42009-09-09 15:08:12 +0000201const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000202 const CallArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000203 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000204 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000205 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000206 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000207 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000208 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000209 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000210}
211
Mike Stump11289f42009-09-09 15:08:12 +0000212const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000213 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000214 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000215 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000216 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000217 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000218 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000219 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000220 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000221}
222
John McCall2da83a32010-02-26 00:48:12 +0000223const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
224 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +0000225 const FunctionType::ExtInfo &Info,
226 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +0000227#ifndef NDEBUG
228 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
229 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
230 assert(I->isCanonicalAsParam());
231#endif
232
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000233 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000234
Daniel Dunbare0be8292009-02-03 00:07:12 +0000235 // Lookup or create unique function info.
236 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000237 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000238 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000239
240 void *InsertPos = 0;
241 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
242 if (FI)
243 return *FI;
244
Daniel Dunbar313321e2009-02-03 05:31:23 +0000245 // Construct the function info.
Chris Lattner3dd716c2010-06-28 23:44:11 +0000246 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000247 ArgTys.data(), ArgTys.size());
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000248 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000249
250 // Compute ABI information.
Chris Lattner22326a12010-07-29 02:31:05 +0000251 getABIInfo().computeInfo(*FI);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000252
253 // Loop over all of the computed argument and return value info. If any of
254 // them are direct or extend without a specified coerce type, specify the
255 // default now.
256 ABIArgInfo &RetInfo = FI->getReturnInfo();
257 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
258 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
259
260 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
261 I != E; ++I)
262 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
263 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar313321e2009-02-03 05:31:23 +0000264
Chris Lattner0e7929f2010-07-01 06:20:47 +0000265 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
266 // types, resolve them now. These pointers may point to this function, which
267 // we *just* filled in the FunctionInfo for.
Chris Lattner22326a12010-07-29 02:31:05 +0000268 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattner0e7929f2010-07-01 06:20:47 +0000269 HandleLateResolvedPointers();
Chris Lattner0e7929f2010-07-01 06:20:47 +0000270
Daniel Dunbare0be8292009-02-03 00:07:12 +0000271 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000272}
273
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000274CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattner34d62812010-06-29 18:13:52 +0000275 bool _NoReturn, unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000276 CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000277 const CanQualType *ArgTys,
278 unsigned NumArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000279 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000280 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000281 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000282{
Chris Lattner34d62812010-06-29 18:13:52 +0000283 NumArgs = NumArgTys;
Chris Lattner3dd716c2010-06-28 23:44:11 +0000284
285 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattner34d62812010-06-29 18:13:52 +0000286 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar313321e2009-02-03 05:31:23 +0000287 Args[0].type = ResTy;
Chris Lattner34d62812010-06-29 18:13:52 +0000288 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar313321e2009-02-03 05:31:23 +0000289 Args[1 + i].type = ArgTys[i];
290}
291
292/***/
293
Mike Stump11289f42009-09-09 15:08:12 +0000294void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattner5c740f12010-06-30 19:14:05 +0000295 std::vector<const llvm::Type*> &ArgTys,
296 bool IsRecursive) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000297 const RecordType *RT = Ty->getAsStructureType();
298 assert(RT && "Can only expand structure types.");
299 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000300 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000301 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000302
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000303 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
304 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000305 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000306 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000307 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000308
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000309 QualType FT = FD->getType();
Chris Lattnerff941a62010-07-28 18:24:28 +0000310 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattner5c740f12010-06-30 19:14:05 +0000311 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerff941a62010-07-28 18:24:28 +0000312 else
Chris Lattner5c740f12010-06-30 19:14:05 +0000313 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000314 }
315}
316
Mike Stump11289f42009-09-09 15:08:12 +0000317llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000318CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
319 llvm::Function::arg_iterator AI) {
320 const RecordType *RT = Ty->getAsStructureType();
321 assert(RT && "Can only expand structure types.");
322
323 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000324 assert(LV.isSimple() &&
325 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000326 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000327 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
328 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000329 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000330 QualType FT = FD->getType();
331
332 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000333 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000334 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
335 AI = ExpandTypeFromArgs(FT, LV, AI);
336 } else {
337 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
338 ++AI;
339 }
340 }
341
342 return AI;
343}
344
Mike Stump11289f42009-09-09 15:08:12 +0000345void
346CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000347 llvm::SmallVector<llvm::Value*, 16> &Args) {
348 const RecordType *RT = Ty->getAsStructureType();
349 assert(RT && "Can only expand structure types.");
350
351 RecordDecl *RD = RT->getDecl();
352 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
353 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000354 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
355 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000356 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000357 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000358
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000359 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000360 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000361 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
362 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
363 } else {
364 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000365 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000366 "Unexpected non-scalar rvalue during struct expansion.");
367 Args.push_back(RV.getScalarVal());
368 }
369 }
370}
371
Chris Lattner895c52b2010-06-27 06:04:18 +0000372/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000373/// accessing some number of bytes out of it, try to gep into the struct to get
374/// at its inner goodness. Dive as deep as possible without entering an element
375/// with an in-memory size smaller than DstSize.
376static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000377EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
378 const llvm::StructType *SrcSTy,
379 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000380 // We can't dive into a zero-element struct.
381 if (SrcSTy->getNumElements() == 0) return SrcPtr;
382
383 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
384
385 // If the first elt is at least as large as what we're looking for, or if the
386 // first element is the same size as the whole struct, we can enter it.
387 uint64_t FirstEltSize =
388 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
389 if (FirstEltSize < DstSize &&
390 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
391 return SrcPtr;
392
393 // GEP into the first element.
394 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
395
396 // If the first element is a struct, recurse.
397 const llvm::Type *SrcTy =
398 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
399 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000400 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000401
402 return SrcPtr;
403}
404
Chris Lattner055097f2010-06-27 06:26:04 +0000405/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
406/// are either integers or pointers. This does a truncation of the value if it
407/// is too large or a zero extension if it is too small.
408static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
409 const llvm::Type *Ty,
410 CodeGenFunction &CGF) {
411 if (Val->getType() == Ty)
412 return Val;
413
414 if (isa<llvm::PointerType>(Val->getType())) {
415 // If this is Pointer->Pointer avoid conversion to and from int.
416 if (isa<llvm::PointerType>(Ty))
417 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
418
419 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +0000420 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +0000421 }
422
423 const llvm::Type *DestIntTy = Ty;
424 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +0000425 DestIntTy = CGF.IntPtrTy;
Chris Lattner055097f2010-06-27 06:26:04 +0000426
427 if (Val->getType() != DestIntTy)
428 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
429
430 if (isa<llvm::PointerType>(Ty))
431 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
432 return Val;
433}
434
Chris Lattner1cd66982010-06-27 05:56:15 +0000435
436
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000437/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
438/// a pointer to an object of type \arg Ty.
439///
440/// This safely handles the case when the src type is smaller than the
441/// destination type; in this situation the values of bits which not
442/// present in the src are undefined.
443static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
444 const llvm::Type *Ty,
445 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000446 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000447 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000448
449 // If SrcTy and Ty are the same, just do a load.
450 if (SrcTy == Ty)
451 return CGF.Builder.CreateLoad(SrcPtr);
452
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000453 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner1cd66982010-06-27 05:56:15 +0000454
455 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000456 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000457 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
458 }
459
460 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000461
Chris Lattner055097f2010-06-27 06:26:04 +0000462 // If the source and destination are integer or pointer types, just do an
463 // extension or truncation to the desired type.
464 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
465 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
466 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
467 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
468 }
469
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000470 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000471 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000472 // Generally SrcSize is never greater than DstSize, since this means we are
473 // losing bits. However, this can happen in cases where the structure has
474 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000475 //
Mike Stump18bb9282009-05-16 07:57:57 +0000476 // FIXME: Assert that we aren't truncating non-padding bits when have access
477 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000478 llvm::Value *Casted =
479 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000480 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
481 // FIXME: Use better alignment / avoid requiring aligned load.
482 Load->setAlignment(1);
483 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000484 }
Chris Lattner3fcc7902010-06-27 01:06:27 +0000485
486 // Otherwise do coercion through memory. This is stupid, but
487 // simple.
488 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
489 llvm::Value *Casted =
490 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
491 llvm::StoreInst *Store =
492 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
493 // FIXME: Use better alignment / avoid requiring aligned store.
494 Store->setAlignment(1);
495 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000496}
497
498/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
499/// where the source and destination may have different types.
500///
501/// This safely handles the case when the src type is larger than the
502/// destination type; the upper bits of the src will be lost.
503static void CreateCoercedStore(llvm::Value *Src,
504 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000505 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000506 CodeGenFunction &CGF) {
507 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000508 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000509 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000510 if (SrcTy == DstTy) {
511 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
512 return;
513 }
514
515 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
516
Chris Lattner895c52b2010-06-27 06:04:18 +0000517 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
518 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
519 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
520 }
521
Chris Lattner055097f2010-06-27 06:26:04 +0000522 // If the source and destination are integer or pointer types, just do an
523 // extension or truncation to the desired type.
524 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
525 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
526 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
527 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
528 return;
529 }
530
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000531 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000532
Daniel Dunbar313321e2009-02-03 05:31:23 +0000533 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000534 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000535 llvm::Value *Casted =
536 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000537 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlsson17490832009-12-24 20:40:36 +0000538 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000539 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000540 // Otherwise do coercion through memory. This is stupid, but
541 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000542
543 // Generally SrcSize is never greater than DstSize, since this means we are
544 // losing bits. However, this can happen in cases where the structure has
545 // additional padding, for example due to a user specified alignment.
546 //
547 // FIXME: Assert that we aren't truncating non-padding bits when have access
548 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000549 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
550 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000551 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000552 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000553 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
554 // FIXME: Use better alignment / avoid requiring aligned load.
555 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000556 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000557 }
558}
559
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000560/***/
561
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000562bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000563 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000564}
565
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000566bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
567 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
568 switch (BT->getKind()) {
569 default:
570 return false;
571 case BuiltinType::Float:
572 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
573 case BuiltinType::Double:
574 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
575 case BuiltinType::LongDouble:
576 return getContext().Target.useObjCFPRetForRealType(
577 TargetInfo::LongDouble);
578 }
579 }
580
581 return false;
582}
583
John McCallf8ff7b92010-02-23 00:48:20 +0000584const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
585 const CGFunctionInfo &FI = getFunctionInfo(GD);
586
587 // For definition purposes, don't consider a K&R function variadic.
588 bool Variadic = false;
589 if (const FunctionProtoType *FPT =
590 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
591 Variadic = FPT->isVariadic();
592
Chris Lattner5c740f12010-06-30 19:14:05 +0000593 return GetFunctionType(FI, Variadic, false);
John McCallf8ff7b92010-02-23 00:48:20 +0000594}
595
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000596const llvm::FunctionType *
Chris Lattner5c740f12010-06-30 19:14:05 +0000597CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
598 bool IsRecursive) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000599 std::vector<const llvm::Type*> ArgTys;
600
601 const llvm::Type *ResultType = 0;
602
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000603 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000604 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbard3674e62008-09-11 01:48:57 +0000605 switch (RetAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000606 case ABIArgInfo::Expand:
607 assert(0 && "Invalid ABI kind for return argument");
608
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000609 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000610 case ABIArgInfo::Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000611 ResultType = RetAI.getCoerceToType();
Daniel Dunbar67dace892009-02-03 06:17:37 +0000612 break;
613
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000614 case ABIArgInfo::Indirect: {
615 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson41a75022009-08-13 21:57:51 +0000616 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattner5c740f12010-06-30 19:14:05 +0000617 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000618 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
619 break;
620 }
621
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000622 case ABIArgInfo::Ignore:
Owen Anderson41a75022009-08-13 21:57:51 +0000623 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000624 break;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000625 }
Mike Stump11289f42009-09-09 15:08:12 +0000626
627 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000628 ie = FI.arg_end(); it != ie; ++it) {
629 const ABIArgInfo &AI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Daniel Dunbard3674e62008-09-11 01:48:57 +0000631 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000632 case ABIArgInfo::Ignore:
633 break;
634
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000635 case ABIArgInfo::Indirect: {
636 // indirect arguments are always on the stack, which is addr space #0.
637 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
638 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
639 break;
640 }
641
642 case ABIArgInfo::Extend:
Chris Lattner2cdfda42010-07-29 06:44:09 +0000643 case ABIArgInfo::Direct: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000644 // If the coerce-to type is a first class aggregate, flatten it. Either
645 // way is semantically identical, but fast-isel and the optimizer
646 // generally likes scalar values better than FCAs.
647 const llvm::Type *ArgTy = AI.getCoerceToType();
648 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
649 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
650 ArgTys.push_back(STy->getElementType(i));
651 } else {
652 ArgTys.push_back(ArgTy);
653 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000654 break;
Chris Lattner2cdfda42010-07-29 06:44:09 +0000655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Daniel Dunbard3674e62008-09-11 01:48:57 +0000657 case ABIArgInfo::Expand:
Chris Lattner5c740f12010-06-30 19:14:05 +0000658 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000659 break;
660 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000661 }
662
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000663 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000664}
665
John McCall5d865c322010-08-31 07:33:07 +0000666const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
667 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson64457732009-11-24 05:08:52 +0000668 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
669
John McCall5d865c322010-08-31 07:33:07 +0000670 if (!VerifyFuncTypeComplete(FPT)) {
671 const CGFunctionInfo *Info;
672 if (isa<CXXDestructorDecl>(MD))
673 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
674 else
675 Info = &getFunctionInfo(MD);
676 return GetFunctionType(*Info, FPT->isVariadic(), false);
677 }
Anders Carlsson64457732009-11-24 05:08:52 +0000678
679 return llvm::OpaqueType::get(getLLVMContext());
680}
681
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000682void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000683 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000684 AttributeListType &PAL,
685 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000686 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000687 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000688
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000689 CallingConv = FI.getEffectiveCallingConvention();
690
John McCallab26cfa2010-02-05 21:31:56 +0000691 if (FI.isNoReturn())
692 FuncAttrs |= llvm::Attribute::NoReturn;
693
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000694 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000695 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000696 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000697 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCallbe349de2010-07-08 06:48:12 +0000698 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
699 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
700 if (FPT && FPT->hasEmptyExceptionSpec())
701 FuncAttrs |= llvm::Attribute::NoUnwind;
702 }
703
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000704 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000705 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000706 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000707 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000708 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000709 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000710 if (TargetDecl->hasAttr<MallocAttr>())
711 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000712 }
713
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000714 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000715 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000716 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000717 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000718 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000719 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000720
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000721 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000722 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000723 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000724 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000725 case ABIArgInfo::Extend:
Chris Lattner4b8585e2010-07-28 23:46:15 +0000726 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000727 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner4b8585e2010-07-28 23:46:15 +0000728 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000729 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000730 break;
Daniel Dunbar67dace892009-02-03 06:17:37 +0000731 case ABIArgInfo::Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000732 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000733 break;
734
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000735 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000736 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000737 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000738 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000739 // sret disables readnone and readonly
740 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
741 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000742 break;
743
Daniel Dunbard3674e62008-09-11 01:48:57 +0000744 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000745 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000746 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000747
Devang Patel597e7082008-09-26 22:53:57 +0000748 if (RetAttrs)
749 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000750
Chris Lattnerff941a62010-07-28 18:24:28 +0000751 // FIXME: we need to honor command line settings also.
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000752 // FIXME: RegParm should be reduced in case of nested functions and/or global
753 // register variable.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000754 signed RegParm = FI.getRegParm();
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000755
756 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000757 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000758 ie = FI.arg_end(); it != ie; ++it) {
759 QualType ParamType = it->type;
760 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000761 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000762
John McCall39ec71f2010-03-27 00:47:27 +0000763 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
764 // have the corresponding parameter variable. It doesn't make
765 // sense to do it here because parameters are so fucked up.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000766 switch (AI.getKind()) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000767 case ABIArgInfo::Extend:
768 if (ParamType->isSignedIntegerType())
769 Attributes |= llvm::Attribute::SExt;
770 else if (ParamType->isUnsignedIntegerType())
771 Attributes |= llvm::Attribute::ZExt;
772 // FALL THROUGH
773 case ABIArgInfo::Direct:
774 if (RegParm > 0 &&
775 (ParamType->isIntegerType() || ParamType->isPointerType())) {
776 RegParm -=
777 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
778 if (RegParm >= 0)
779 Attributes |= llvm::Attribute::InReg;
780 }
781 // FIXME: handle sseregparm someday...
782
Chris Lattner3dd716c2010-06-28 23:44:11 +0000783 if (const llvm::StructType *STy =
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000784 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
785 Index += STy->getNumElements()-1; // 1 will be added below.
786 break;
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000787
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000788 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000789 if (AI.getIndirectByVal())
790 Attributes |= llvm::Attribute::ByVal;
791
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000792 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000793 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000794 // byval disables readnone and readonly.
795 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
796 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000797 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000798
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000799 case ABIArgInfo::Ignore:
800 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000801 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000802
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000803 case ABIArgInfo::Expand: {
Mike Stump11289f42009-09-09 15:08:12 +0000804 std::vector<const llvm::Type*> Tys;
Mike Stump18bb9282009-05-16 07:57:57 +0000805 // FIXME: This is rather inefficient. Do we ever actually need to do
806 // anything here? The result should be just reconstructed on the other
807 // side, so extension should be a non-issue.
Chris Lattner5c740f12010-06-30 19:14:05 +0000808 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000809 Index += Tys.size();
810 continue;
811 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000812 }
Mike Stump11289f42009-09-09 15:08:12 +0000813
Devang Patel322300d2008-09-25 21:02:23 +0000814 if (Attributes)
815 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000816 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000817 }
Devang Patel597e7082008-09-26 22:53:57 +0000818 if (FuncAttrs)
819 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000820}
821
Daniel Dunbard931a872009-02-02 22:03:45 +0000822void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
823 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000824 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000825 // If this is an implicit-return-zero function, go ahead and
826 // initialize the return value. TODO: it might be nice to have
827 // a more general mechanism for this that didn't require synthesized
828 // return statements.
Chris Lattnerc401de92010-07-05 20:21:00 +0000829 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000830 if (FD->hasImplicitReturnZero()) {
831 QualType RetTy = FD->getResultType().getUnqualifiedType();
832 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000833 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000834 Builder.CreateStore(Zero, ReturnValue);
835 }
836 }
837
Mike Stump18bb9282009-05-16 07:57:57 +0000838 // FIXME: We no longer need the types from FunctionArgList; lift up and
839 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000840
Daniel Dunbar613855c2008-09-09 23:27:19 +0000841 // Emit allocs for param decls. Give the LLVM Argument nodes names.
842 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000843
Daniel Dunbar613855c2008-09-09 23:27:19 +0000844 // Name the struct return argument.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000845 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000846 AI->setName("agg.result");
847 ++AI;
848 }
Mike Stump11289f42009-09-09 15:08:12 +0000849
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000850 assert(FI.arg_size() == Args.size() &&
851 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000852 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000853 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000854 i != e; ++i, ++info_it) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000855 const VarDecl *Arg = i->first;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000856 QualType Ty = info_it->type;
857 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000858
859 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000860 case ABIArgInfo::Indirect: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000861 llvm::Value *V = AI;
Daniel Dunbar747865a2009-02-05 09:16:39 +0000862 if (hasAggregateLLVMType(Ty)) {
863 // Do nothing, aggregates and complex variables are accessed by
864 // reference.
865 } else {
866 // Load scalar value from indirect argument.
Daniel Dunbar03816342010-08-21 02:24:36 +0000867 unsigned Alignment = getContext().getTypeAlignInChars(Ty).getQuantity();
868 V = EmitLoadOfScalar(V, false, Alignment, Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000869 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
870 // This must be a promotion, for something like
871 // "void a(x) short x; {..."
872 V = EmitScalarConversion(V, Ty, Arg->getType());
873 }
874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875 EmitParmDecl(*Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000876 break;
877 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000878
879 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000880 case ABIArgInfo::Direct: {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000881 // If we have the trivial case, handle it with no muss and fuss.
882 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000883 ArgI.getCoerceToType() == ConvertType(Ty) &&
884 ArgI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000885 assert(AI != Fn->arg_end() && "Argument mismatch!");
886 llvm::Value *V = AI;
887
John McCall39ec71f2010-03-27 00:47:27 +0000888 if (Arg->getType().isRestrictQualified())
889 AI->addAttr(llvm::Attribute::NoAlias);
890
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000891 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
892 // This must be a promotion, for something like
893 // "void a(x) short x; {..."
894 V = EmitScalarConversion(V, Ty, Arg->getType());
895 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000896 EmitParmDecl(*Arg, V);
897 break;
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Chris Lattnerc401de92010-07-05 20:21:00 +0000900 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Chris Lattnerff941a62010-07-28 18:24:28 +0000901
902 // The alignment we need to use is the max of the requested alignment for
903 // the argument plus the alignment required by our access code below.
904 unsigned AlignmentToUse =
905 CGF.CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
906 AlignmentToUse = std::max(AlignmentToUse,
907 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
908
909 Alloca->setAlignment(AlignmentToUse);
Chris Lattnerc401de92010-07-05 20:21:00 +0000910 llvm::Value *V = Alloca;
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000911 llvm::Value *Ptr = V; // Pointer to store into.
912
913 // If the value is offset in memory, apply the offset now.
914 if (unsigned Offs = ArgI.getDirectOffset()) {
915 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
916 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
917 Ptr = Builder.CreateBitCast(Ptr,
918 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
919 }
Chris Lattner15ec3612010-06-29 00:06:42 +0000920
921 // If the coerce-to type is a first class aggregate, we flatten it and
922 // pass the elements. Either way is semantically identical, but fast-isel
923 // and the optimizer generally likes scalar values better than FCAs.
924 if (const llvm::StructType *STy =
925 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +0000926 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
927
928 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
929 assert(AI != Fn->arg_end() && "Argument mismatch!");
930 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
931 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
932 Builder.CreateStore(AI++, EltPtr);
Chris Lattner15ec3612010-06-29 00:06:42 +0000933 }
934 } else {
935 // Simple case, just do a coerced store of the argument into the alloca.
936 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner9e748e92010-06-29 00:14:52 +0000937 AI->setName(Arg->getName() + ".coerce");
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000938 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner15ec3612010-06-29 00:06:42 +0000939 }
940
941
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000942 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000943 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar03816342010-08-21 02:24:36 +0000944 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000945 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
946 // This must be a promotion, for something like
947 // "void a(x) short x; {..."
948 V = EmitScalarConversion(V, Ty, Arg->getType());
949 }
950 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000951 EmitParmDecl(*Arg, V);
Chris Lattner3dd716c2010-06-28 23:44:11 +0000952 continue; // Skip ++AI increment, already done.
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000953 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000954
955 case ABIArgInfo::Expand: {
956 // If this structure was expanded into multiple arguments then
957 // we need to create a temporary and reconstruct it from the
958 // arguments.
959 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000960 llvm::Function::arg_iterator End =
Daniel Dunbar2e442a02010-08-21 03:15:20 +0000961 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000962 EmitParmDecl(*Arg, Temp);
963
964 // Name the arguments used in expansion and increment AI.
965 unsigned Index = 0;
966 for (; AI != End; ++AI, ++Index)
967 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
968 continue;
969 }
970
971 case ABIArgInfo::Ignore:
972 // Initialize the local variable appropriately.
973 if (hasAggregateLLVMType(Ty))
974 EmitParmDecl(*Arg, CreateMemTemp(Ty));
975 else
976 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
977
978 // Skip increment, no matching LLVM parameter.
979 continue;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000980 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000981
982 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000983 }
984 assert(AI == Fn->arg_end() && "Argument mismatch!");
985}
986
Chris Lattner3fcc7902010-06-27 01:06:27 +0000987void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000988 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +0000989 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000990 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +0000991 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000992 }
Daniel Dunbar6696e222010-06-30 21:27:58 +0000993
Dan Gohman481e40c2010-07-20 20:13:52 +0000994 llvm::DebugLoc RetDbgLoc;
Chris Lattner726b3d02010-06-26 23:13:19 +0000995 llvm::Value *RV = 0;
996 QualType RetTy = FI.getReturnType();
997 const ABIArgInfo &RetAI = FI.getReturnInfo();
998
999 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001000 case ABIArgInfo::Indirect: {
1001 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattner726b3d02010-06-26 23:13:19 +00001002 if (RetTy->isAnyComplexType()) {
1003 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1004 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1005 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1006 // Do nothing; aggregrates get evaluated directly into the destination.
1007 } else {
1008 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar03816342010-08-21 02:24:36 +00001009 false, Alignment, RetTy);
Chris Lattner726b3d02010-06-26 23:13:19 +00001010 }
1011 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001012 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001013
1014 case ABIArgInfo::Extend:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001015 case ABIArgInfo::Direct:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001016 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1017 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001018 // The internal return value temp always will have pointer-to-return-type
1019 // type, just do a load.
1020
1021 // If the instruction right before the insertion point is a store to the
1022 // return value, we can elide the load, zap the store, and usually zap the
1023 // alloca.
1024 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1025 llvm::StoreInst *SI = 0;
1026 if (InsertBB->empty() ||
1027 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1028 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1029 RV = Builder.CreateLoad(ReturnValue);
1030 } else {
1031 // Get the stored value and nuke the now-dead store.
1032 RetDbgLoc = SI->getDebugLoc();
1033 RV = SI->getValueOperand();
1034 SI->eraseFromParent();
1035
1036 // If that was the only use of the return value, nuke it as well now.
1037 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1038 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1039 ReturnValue = 0;
1040 }
Chris Lattner3fcc7902010-06-27 01:06:27 +00001041 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001042 } else {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001043 llvm::Value *V = ReturnValue;
1044 // If the value is offset in memory, apply the offset now.
1045 if (unsigned Offs = RetAI.getDirectOffset()) {
1046 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1047 V = Builder.CreateConstGEP1_32(V, Offs);
1048 V = Builder.CreateBitCast(V,
1049 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1050 }
1051
1052 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner3fcc7902010-06-27 01:06:27 +00001053 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001054 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00001055
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001056 case ABIArgInfo::Ignore:
Chris Lattner726b3d02010-06-26 23:13:19 +00001057 break;
1058
1059 case ABIArgInfo::Expand:
1060 assert(0 && "Invalid ABI kind for return argument");
1061 }
1062
Daniel Dunbar6696e222010-06-30 21:27:58 +00001063 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Patel65497582010-07-21 18:08:50 +00001064 if (!RetDbgLoc.isUnknown())
1065 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001066}
1067
John McCall23f66262010-05-26 22:34:26 +00001068RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1069 // StartFunction converted the ABI-lowered parameter(s) into a
1070 // local alloca. We need to turn that into an r-value suitable
1071 // for EmitCall.
1072 llvm::Value *Local = GetAddrOfLocalVar(Param);
1073
1074 QualType ArgType = Param->getType();
1075
1076 // For the most part, we just need to load the alloca, except:
1077 // 1) aggregate r-values are actually pointers to temporaries, and
1078 // 2) references to aggregates are pointers directly to the aggregate.
1079 // I don't know why references to non-aggregates are different here.
1080 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1081 if (hasAggregateLLVMType(RefType->getPointeeType()))
1082 return RValue::getAggregate(Local);
1083
1084 // Locals which are references to scalars are represented
1085 // with allocas holding the pointer.
1086 return RValue::get(Builder.CreateLoad(Local));
1087 }
1088
1089 if (ArgType->isAnyComplexType())
1090 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1091
1092 if (hasAggregateLLVMType(ArgType))
1093 return RValue::getAggregate(Local);
1094
Daniel Dunbar03816342010-08-21 02:24:36 +00001095 unsigned Alignment = getContext().getDeclAlign(Param).getQuantity();
1096 return RValue::get(EmitLoadOfScalar(Local, false, Alignment, ArgType));
John McCall23f66262010-05-26 22:34:26 +00001097}
1098
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001099RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson6f5a0152009-05-20 00:24:07 +00001100 if (ArgType->isReferenceType())
Anders Carlsson04775f82010-06-26 16:35:32 +00001101 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump11289f42009-09-09 15:08:12 +00001102
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001103 return EmitAnyExprToTemp(E);
1104}
1105
John McCallbd309292010-07-06 01:34:17 +00001106/// Emits a call or invoke instruction to the given function, depending
1107/// on the current state of the EH stack.
1108llvm::CallSite
1109CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1110 llvm::Value * const *ArgBegin,
1111 llvm::Value * const *ArgEnd,
1112 const llvm::Twine &Name) {
1113 llvm::BasicBlock *InvokeDest = getInvokeDest();
1114 if (!InvokeDest)
1115 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1116
1117 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1118 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1119 ArgBegin, ArgEnd, Name);
1120 EmitBlock(ContBB);
1121 return Invoke;
1122}
1123
Daniel Dunbard931a872009-02-02 22:03:45 +00001124RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001125 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00001126 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001127 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00001128 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00001129 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00001130 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00001131 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001132
1133 // Handle struct-return functions by passing a pointer to the
1134 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001135 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001136 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001137
1138
Chris Lattner4ca97c32009-06-13 00:26:38 +00001139 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00001140 // alloca to hold the result, unless one is given to us.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001141 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001142 llvm::Value *Value = ReturnValue.getValue();
1143 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +00001144 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +00001145 Args.push_back(Value);
1146 }
Mike Stump11289f42009-09-09 15:08:12 +00001147
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001148 assert(CallInfo.arg_size() == CallArgs.size() &&
1149 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001150 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001151 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001152 I != E; ++I, ++info_it) {
1153 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001154 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001155
Daniel Dunbar03816342010-08-21 02:24:36 +00001156 unsigned Alignment =
1157 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001158 switch (ArgInfo.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001159 case ABIArgInfo::Indirect: {
Daniel Dunbar747865a2009-02-05 09:16:39 +00001160 if (RV.isScalar() || RV.isComplex()) {
1161 // Make a temporary alloca to pass the argument.
Daniel Dunbara7566f12010-02-09 02:48:28 +00001162 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001163 if (RV.isScalar())
Daniel Dunbar03816342010-08-21 02:24:36 +00001164 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1165 Alignment, I->second);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001166 else
Mike Stump11289f42009-09-09 15:08:12 +00001167 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001168 } else {
1169 Args.push_back(RV.getAggregateAddr());
1170 }
1171 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001172 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00001173
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001174 case ABIArgInfo::Ignore:
1175 break;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001176
1177 case ABIArgInfo::Extend:
1178 case ABIArgInfo::Direct: {
1179 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001180 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1181 ArgInfo.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001182 if (RV.isScalar())
1183 Args.push_back(RV.getScalarVal());
1184 else
1185 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1186 break;
1187 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001188
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001189 // FIXME: Avoid the conversion through memory if possible.
1190 llvm::Value *SrcPtr;
1191 if (RV.isScalar()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001192 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar03816342010-08-21 02:24:36 +00001193 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1194 I->second);
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001195 } else if (RV.isComplex()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001196 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001197 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001198 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001199 SrcPtr = RV.getAggregateAddr();
Chris Lattner3dd716c2010-06-28 23:44:11 +00001200
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001201 // If the value is offset in memory, apply the offset now.
1202 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1203 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1204 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1205 SrcPtr = Builder.CreateBitCast(SrcPtr,
1206 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1207
1208 }
1209
Chris Lattner3dd716c2010-06-28 23:44:11 +00001210 // If the coerce-to type is a first class aggregate, we flatten it and
1211 // pass the elements. Either way is semantically identical, but fast-isel
1212 // and the optimizer generally likes scalar values better than FCAs.
1213 if (const llvm::StructType *STy =
Chris Lattner15ec3612010-06-29 00:06:42 +00001214 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001215 SrcPtr = Builder.CreateBitCast(SrcPtr,
1216 llvm::PointerType::getUnqual(STy));
1217 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1218 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerff941a62010-07-28 18:24:28 +00001219 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1220 // We don't know what we're loading from.
1221 LI->setAlignment(1);
1222 Args.push_back(LI);
Chris Lattner15ec3612010-06-29 00:06:42 +00001223 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00001224 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00001225 // In the simple case, just pass the coerced loaded value.
1226 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1227 *this));
Chris Lattner3dd716c2010-06-28 23:44:11 +00001228 }
1229
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001230 break;
1231 }
1232
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001233 case ABIArgInfo::Expand:
1234 ExpandTypeToArgs(I->second, RV, Args);
1235 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001236 }
1237 }
Mike Stump11289f42009-09-09 15:08:12 +00001238
Chris Lattner4ca97c32009-06-13 00:26:38 +00001239 // If the callee is a bitcast of a function to a varargs pointer to function
1240 // type, check to see if we can remove the bitcast. This handles some cases
1241 // with unprototyped functions.
1242 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1243 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1244 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1245 const llvm::FunctionType *CurFT =
1246 cast<llvm::FunctionType>(CurPT->getElementType());
1247 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattner4ca97c32009-06-13 00:26:38 +00001249 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1250 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00001251 ActualFT->getNumParams() == CurFT->getNumParams() &&
1252 ActualFT->getNumParams() == Args.size()) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00001253 bool ArgsMatch = true;
1254 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1255 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1256 ArgsMatch = false;
1257 break;
1258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Chris Lattner4ca97c32009-06-13 00:26:38 +00001260 // Strip the cast if we can get away with it. This is a nice cleanup,
1261 // but also allows us to inline the function at -O0 if it is marked
1262 // always_inline.
1263 if (ArgsMatch)
1264 Callee = CalleeF;
1265 }
1266 }
Mike Stump11289f42009-09-09 15:08:12 +00001267
Daniel Dunbar613855c2008-09-09 23:27:19 +00001268
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001269 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00001270 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001271 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +00001272 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1273 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +00001274
John McCallbd309292010-07-06 01:34:17 +00001275 llvm::BasicBlock *InvokeDest = 0;
1276 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1277 InvokeDest = getInvokeDest();
1278
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001279 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00001280 if (!InvokeDest) {
Jay Foad7d0479f2009-05-21 09:52:38 +00001281 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001282 } else {
1283 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +00001284 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +00001285 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001286 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001287 }
Chris Lattnere70a0072010-06-29 16:40:28 +00001288 if (callOrInvoke)
David Chisnallff5f88c2010-05-02 13:41:58 +00001289 *callOrInvoke = CS.getInstruction();
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001290
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001291 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001292 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001293
1294 // If the call doesn't return, finish the basic block and clear the
1295 // insertion point; this allows the rest of IRgen to discard
1296 // unreachable code.
1297 if (CS.doesNotReturn()) {
1298 Builder.CreateUnreachable();
1299 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001300
Mike Stump18bb9282009-05-16 07:57:57 +00001301 // FIXME: For now, emit a dummy basic block because expr emitters in
1302 // generally are not ready to handle emitting expressions at unreachable
1303 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001304 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001305
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001306 // Return a reasonable RValue.
1307 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001308 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001309
1310 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001311 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001312 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001313
1314 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001315 case ABIArgInfo::Indirect: {
1316 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001317 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001318 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001319 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001320 return RValue::getAggregate(Args[0]);
Daniel Dunbar03816342010-08-21 02:24:36 +00001321 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1322 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001323
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001324 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001325 // If we are ignoring an argument that had a result, make sure to
1326 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001327 return GetUndefRValue(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001328
1329 case ABIArgInfo::Extend:
1330 case ABIArgInfo::Direct: {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001331 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1332 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001333 if (RetTy->isAnyComplexType()) {
1334 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1335 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1336 return RValue::getComplex(std::make_pair(Real, Imag));
1337 }
1338 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1339 llvm::Value *DestPtr = ReturnValue.getValue();
1340 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001341
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001342 if (!DestPtr) {
1343 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1344 DestIsVolatile = false;
1345 }
1346 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1347 return RValue::getAggregate(DestPtr);
1348 }
1349 return RValue::get(CI);
1350 }
1351
Anders Carlsson17490832009-12-24 20:40:36 +00001352 llvm::Value *DestPtr = ReturnValue.getValue();
1353 bool DestIsVolatile = ReturnValue.isVolatile();
1354
1355 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001356 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001357 DestIsVolatile = false;
1358 }
1359
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001360 // If the value is offset in memory, apply the offset now.
1361 llvm::Value *StorePtr = DestPtr;
1362 if (unsigned Offs = RetAI.getDirectOffset()) {
1363 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1364 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1365 StorePtr = Builder.CreateBitCast(StorePtr,
1366 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1367 }
1368 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1369
Daniel Dunbar03816342010-08-21 02:24:36 +00001370 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001371 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001372 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001373 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001374 return RValue::getAggregate(DestPtr);
Daniel Dunbar03816342010-08-21 02:24:36 +00001375 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001376 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001377
Daniel Dunbard3674e62008-09-11 01:48:57 +00001378 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001379 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001380 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001381
1382 assert(0 && "Unhandled ABIArgInfo::Kind");
1383 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001384}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001385
1386/* VarArg handling */
1387
1388llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1389 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1390}