blob: f698d146f1b0c2823674b1f09d6c19fb12aeaea4 [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
John McCall0d635f52010-09-03 01:26:39 +0000120 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
121 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
122
Chris Lattnerbea5b622009-05-12 20:27:19 +0000123 // Add the 'this' pointer unless this is a static method.
124 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000125 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000126
John McCall8ee376f2010-02-24 07:14:12 +0000127 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000128}
129
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000130const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
131 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000132 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000133 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000134 CanQualType ResTy = Context.VoidTy;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000135
John McCall5d865c322010-08-31 07:33:07 +0000136 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall8ee376f2010-02-24 07:14:12 +0000137
John McCall5d865c322010-08-31 07:33:07 +0000138 CanQual<FunctionProtoType> FTP = GetFormalType(D);
139
140 // Add the formal parameters.
141 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
142 ArgTys.push_back(FTP->getArgType(i));
143
144 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000145}
146
147const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
148 CXXDtorType Type) {
John McCall5d865c322010-08-31 07:33:07 +0000149 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCall2da83a32010-02-26 00:48:12 +0000150 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000151 CanQualType ResTy = Context.VoidTy;
John McCall8ee376f2010-02-24 07:14:12 +0000152
John McCall5d865c322010-08-31 07:33:07 +0000153 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
154
155 CanQual<FunctionProtoType> FTP = GetFormalType(D);
156 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
157
158 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000159}
160
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000161const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000162 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000163 if (MD->isInstance())
164 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000165
John McCall2da83a32010-02-26 00:48:12 +0000166 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
167 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000168 if (isa<FunctionNoProtoType>(FTy))
John McCall2da83a32010-02-26 00:48:12 +0000169 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
170 assert(isa<FunctionProtoType>(FTy));
171 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000172}
173
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000174const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000175 llvm::SmallVector<CanQualType, 16> ArgTys;
176 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
177 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000178 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000179 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000180 e = MD->param_end(); i != e; ++i) {
181 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
182 }
183 return getFunctionInfo(GetReturnType(MD->getResultType()),
184 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000185 FunctionType::ExtInfo(
186 /*NoReturn*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000187 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000188 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000189}
190
Anders Carlsson6710c532010-02-06 02:44:09 +0000191const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
192 // FIXME: Do we need to handle ObjCMethodDecl?
193 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
194
195 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
196 return getFunctionInfo(CD, GD.getCtorType());
197
198 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
199 return getFunctionInfo(DD, GD.getDtorType());
200
201 return getFunctionInfo(FD);
202}
203
Mike Stump11289f42009-09-09 15:08:12 +0000204const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000205 const CallArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000206 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000207 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000208 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000209 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000210 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000211 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000212 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000213}
214
Mike Stump11289f42009-09-09 15:08:12 +0000215const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000216 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000217 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000218 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000219 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000220 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000221 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000222 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000223 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000224}
225
John McCall2da83a32010-02-26 00:48:12 +0000226const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
227 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +0000228 const FunctionType::ExtInfo &Info,
229 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +0000230#ifndef NDEBUG
231 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
232 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
233 assert(I->isCanonicalAsParam());
234#endif
235
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000236 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000237
Daniel Dunbare0be8292009-02-03 00:07:12 +0000238 // Lookup or create unique function info.
239 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000240 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000241 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000242
243 void *InsertPos = 0;
244 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
245 if (FI)
246 return *FI;
247
Daniel Dunbar313321e2009-02-03 05:31:23 +0000248 // Construct the function info.
Chris Lattner3dd716c2010-06-28 23:44:11 +0000249 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000250 ArgTys.data(), ArgTys.size());
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000251 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000252
253 // Compute ABI information.
Chris Lattner22326a12010-07-29 02:31:05 +0000254 getABIInfo().computeInfo(*FI);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000255
256 // Loop over all of the computed argument and return value info. If any of
257 // them are direct or extend without a specified coerce type, specify the
258 // default now.
259 ABIArgInfo &RetInfo = FI->getReturnInfo();
260 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
261 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
262
263 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
264 I != E; ++I)
265 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
266 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar313321e2009-02-03 05:31:23 +0000267
Chris Lattner0e7929f2010-07-01 06:20:47 +0000268 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
269 // types, resolve them now. These pointers may point to this function, which
270 // we *just* filled in the FunctionInfo for.
Chris Lattner22326a12010-07-29 02:31:05 +0000271 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattner0e7929f2010-07-01 06:20:47 +0000272 HandleLateResolvedPointers();
Chris Lattner0e7929f2010-07-01 06:20:47 +0000273
Daniel Dunbare0be8292009-02-03 00:07:12 +0000274 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000275}
276
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000277CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattner34d62812010-06-29 18:13:52 +0000278 bool _NoReturn, unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000279 CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000280 const CanQualType *ArgTys,
281 unsigned NumArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000282 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000283 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000284 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000285{
Chris Lattner34d62812010-06-29 18:13:52 +0000286 NumArgs = NumArgTys;
Chris Lattner3dd716c2010-06-28 23:44:11 +0000287
288 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattner34d62812010-06-29 18:13:52 +0000289 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar313321e2009-02-03 05:31:23 +0000290 Args[0].type = ResTy;
Chris Lattner34d62812010-06-29 18:13:52 +0000291 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar313321e2009-02-03 05:31:23 +0000292 Args[1 + i].type = ArgTys[i];
293}
294
295/***/
296
Mike Stump11289f42009-09-09 15:08:12 +0000297void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattner5c740f12010-06-30 19:14:05 +0000298 std::vector<const llvm::Type*> &ArgTys,
299 bool IsRecursive) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000300 const RecordType *RT = Ty->getAsStructureType();
301 assert(RT && "Can only expand structure types.");
302 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000303 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000304 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000305
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000306 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
307 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000308 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000309 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000310 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000311
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000312 QualType FT = FD->getType();
Chris Lattnerff941a62010-07-28 18:24:28 +0000313 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattner5c740f12010-06-30 19:14:05 +0000314 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerff941a62010-07-28 18:24:28 +0000315 else
Chris Lattner5c740f12010-06-30 19:14:05 +0000316 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000317 }
318}
319
Mike Stump11289f42009-09-09 15:08:12 +0000320llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000321CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
322 llvm::Function::arg_iterator AI) {
323 const RecordType *RT = Ty->getAsStructureType();
324 assert(RT && "Can only expand structure types.");
325
326 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000327 assert(LV.isSimple() &&
328 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000329 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000330 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
331 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000332 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000333 QualType FT = FD->getType();
334
335 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000336 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000337 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
338 AI = ExpandTypeFromArgs(FT, LV, AI);
339 } else {
340 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
341 ++AI;
342 }
343 }
344
345 return AI;
346}
347
Mike Stump11289f42009-09-09 15:08:12 +0000348void
349CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000350 llvm::SmallVector<llvm::Value*, 16> &Args) {
351 const RecordType *RT = Ty->getAsStructureType();
352 assert(RT && "Can only expand structure types.");
353
354 RecordDecl *RD = RT->getDecl();
355 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
356 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000357 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
358 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000359 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000360 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000361
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000362 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000363 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000364 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
365 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
366 } else {
367 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000368 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000369 "Unexpected non-scalar rvalue during struct expansion.");
370 Args.push_back(RV.getScalarVal());
371 }
372 }
373}
374
Chris Lattner895c52b2010-06-27 06:04:18 +0000375/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000376/// accessing some number of bytes out of it, try to gep into the struct to get
377/// at its inner goodness. Dive as deep as possible without entering an element
378/// with an in-memory size smaller than DstSize.
379static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000380EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
381 const llvm::StructType *SrcSTy,
382 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000383 // We can't dive into a zero-element struct.
384 if (SrcSTy->getNumElements() == 0) return SrcPtr;
385
386 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
387
388 // If the first elt is at least as large as what we're looking for, or if the
389 // first element is the same size as the whole struct, we can enter it.
390 uint64_t FirstEltSize =
391 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
392 if (FirstEltSize < DstSize &&
393 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
394 return SrcPtr;
395
396 // GEP into the first element.
397 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
398
399 // If the first element is a struct, recurse.
400 const llvm::Type *SrcTy =
401 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
402 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000403 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000404
405 return SrcPtr;
406}
407
Chris Lattner055097f2010-06-27 06:26:04 +0000408/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
409/// are either integers or pointers. This does a truncation of the value if it
410/// is too large or a zero extension if it is too small.
411static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
412 const llvm::Type *Ty,
413 CodeGenFunction &CGF) {
414 if (Val->getType() == Ty)
415 return Val;
416
417 if (isa<llvm::PointerType>(Val->getType())) {
418 // If this is Pointer->Pointer avoid conversion to and from int.
419 if (isa<llvm::PointerType>(Ty))
420 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
421
422 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +0000423 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +0000424 }
425
426 const llvm::Type *DestIntTy = Ty;
427 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +0000428 DestIntTy = CGF.IntPtrTy;
Chris Lattner055097f2010-06-27 06:26:04 +0000429
430 if (Val->getType() != DestIntTy)
431 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
432
433 if (isa<llvm::PointerType>(Ty))
434 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
435 return Val;
436}
437
Chris Lattner1cd66982010-06-27 05:56:15 +0000438
439
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000440/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
441/// a pointer to an object of type \arg Ty.
442///
443/// This safely handles the case when the src type is smaller than the
444/// destination type; in this situation the values of bits which not
445/// present in the src are undefined.
446static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
447 const llvm::Type *Ty,
448 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000449 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000450 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000451
452 // If SrcTy and Ty are the same, just do a load.
453 if (SrcTy == Ty)
454 return CGF.Builder.CreateLoad(SrcPtr);
455
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000456 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner1cd66982010-06-27 05:56:15 +0000457
458 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000459 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000460 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
461 }
462
463 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000464
Chris Lattner055097f2010-06-27 06:26:04 +0000465 // If the source and destination are integer or pointer types, just do an
466 // extension or truncation to the desired type.
467 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
468 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
469 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
470 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
471 }
472
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000473 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000474 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000475 // Generally SrcSize is never greater than DstSize, since this means we are
476 // losing bits. However, this can happen in cases where the structure has
477 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000478 //
Mike Stump18bb9282009-05-16 07:57:57 +0000479 // FIXME: Assert that we aren't truncating non-padding bits when have access
480 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000481 llvm::Value *Casted =
482 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000483 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
484 // FIXME: Use better alignment / avoid requiring aligned load.
485 Load->setAlignment(1);
486 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000487 }
Chris Lattner3fcc7902010-06-27 01:06:27 +0000488
489 // Otherwise do coercion through memory. This is stupid, but
490 // simple.
491 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
492 llvm::Value *Casted =
493 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
494 llvm::StoreInst *Store =
495 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
496 // FIXME: Use better alignment / avoid requiring aligned store.
497 Store->setAlignment(1);
498 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000499}
500
501/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
502/// where the source and destination may have different types.
503///
504/// This safely handles the case when the src type is larger than the
505/// destination type; the upper bits of the src will be lost.
506static void CreateCoercedStore(llvm::Value *Src,
507 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000508 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000509 CodeGenFunction &CGF) {
510 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000511 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000512 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000513 if (SrcTy == DstTy) {
514 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
515 return;
516 }
517
518 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
519
Chris Lattner895c52b2010-06-27 06:04:18 +0000520 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
521 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
522 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
523 }
524
Chris Lattner055097f2010-06-27 06:26:04 +0000525 // If the source and destination are integer or pointer types, just do an
526 // extension or truncation to the desired type.
527 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
528 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
529 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
530 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
531 return;
532 }
533
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000534 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000535
Daniel Dunbar313321e2009-02-03 05:31:23 +0000536 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000537 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000538 llvm::Value *Casted =
539 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000540 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlsson17490832009-12-24 20:40:36 +0000541 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000542 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000543 // Otherwise do coercion through memory. This is stupid, but
544 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000545
546 // Generally SrcSize is never greater than DstSize, since this means we are
547 // losing bits. However, this can happen in cases where the structure has
548 // additional padding, for example due to a user specified alignment.
549 //
550 // FIXME: Assert that we aren't truncating non-padding bits when have access
551 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000552 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
553 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000554 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000555 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000556 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
557 // FIXME: Use better alignment / avoid requiring aligned load.
558 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000559 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000560 }
561}
562
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000563/***/
564
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000565bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000566 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000567}
568
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000569bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
570 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
571 switch (BT->getKind()) {
572 default:
573 return false;
574 case BuiltinType::Float:
575 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
576 case BuiltinType::Double:
577 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
578 case BuiltinType::LongDouble:
579 return getContext().Target.useObjCFPRetForRealType(
580 TargetInfo::LongDouble);
581 }
582 }
583
584 return false;
585}
586
John McCallf8ff7b92010-02-23 00:48:20 +0000587const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
588 const CGFunctionInfo &FI = getFunctionInfo(GD);
589
590 // For definition purposes, don't consider a K&R function variadic.
591 bool Variadic = false;
592 if (const FunctionProtoType *FPT =
593 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
594 Variadic = FPT->isVariadic();
595
Chris Lattner5c740f12010-06-30 19:14:05 +0000596 return GetFunctionType(FI, Variadic, false);
John McCallf8ff7b92010-02-23 00:48:20 +0000597}
598
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000599const llvm::FunctionType *
Chris Lattner5c740f12010-06-30 19:14:05 +0000600CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
601 bool IsRecursive) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000602 std::vector<const llvm::Type*> ArgTys;
603
604 const llvm::Type *ResultType = 0;
605
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000606 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000607 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbard3674e62008-09-11 01:48:57 +0000608 switch (RetAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000609 case ABIArgInfo::Expand:
610 assert(0 && "Invalid ABI kind for return argument");
611
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000612 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000613 case ABIArgInfo::Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000614 ResultType = RetAI.getCoerceToType();
Daniel Dunbar67dace892009-02-03 06:17:37 +0000615 break;
616
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000617 case ABIArgInfo::Indirect: {
618 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson41a75022009-08-13 21:57:51 +0000619 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattner5c740f12010-06-30 19:14:05 +0000620 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000621 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
622 break;
623 }
624
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000625 case ABIArgInfo::Ignore:
Owen Anderson41a75022009-08-13 21:57:51 +0000626 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000627 break;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
630 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000631 ie = FI.arg_end(); it != ie; ++it) {
632 const ABIArgInfo &AI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000633
Daniel Dunbard3674e62008-09-11 01:48:57 +0000634 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000635 case ABIArgInfo::Ignore:
636 break;
637
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000638 case ABIArgInfo::Indirect: {
639 // indirect arguments are always on the stack, which is addr space #0.
640 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
641 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
642 break;
643 }
644
645 case ABIArgInfo::Extend:
Chris Lattner2cdfda42010-07-29 06:44:09 +0000646 case ABIArgInfo::Direct: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000647 // If the coerce-to type is a first class aggregate, flatten it. Either
648 // way is semantically identical, but fast-isel and the optimizer
649 // generally likes scalar values better than FCAs.
650 const llvm::Type *ArgTy = AI.getCoerceToType();
651 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
652 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
653 ArgTys.push_back(STy->getElementType(i));
654 } else {
655 ArgTys.push_back(ArgTy);
656 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000657 break;
Chris Lattner2cdfda42010-07-29 06:44:09 +0000658 }
Mike Stump11289f42009-09-09 15:08:12 +0000659
Daniel Dunbard3674e62008-09-11 01:48:57 +0000660 case ABIArgInfo::Expand:
Chris Lattner5c740f12010-06-30 19:14:05 +0000661 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000662 break;
663 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000664 }
665
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000666 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000667}
668
John McCall5d865c322010-08-31 07:33:07 +0000669const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
670 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson64457732009-11-24 05:08:52 +0000671 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
672
John McCall5d865c322010-08-31 07:33:07 +0000673 if (!VerifyFuncTypeComplete(FPT)) {
674 const CGFunctionInfo *Info;
675 if (isa<CXXDestructorDecl>(MD))
676 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
677 else
678 Info = &getFunctionInfo(MD);
679 return GetFunctionType(*Info, FPT->isVariadic(), false);
680 }
Anders Carlsson64457732009-11-24 05:08:52 +0000681
682 return llvm::OpaqueType::get(getLLVMContext());
683}
684
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000685void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000686 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000687 AttributeListType &PAL,
688 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000689 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000690 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000691
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000692 CallingConv = FI.getEffectiveCallingConvention();
693
John McCallab26cfa2010-02-05 21:31:56 +0000694 if (FI.isNoReturn())
695 FuncAttrs |= llvm::Attribute::NoReturn;
696
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000697 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000698 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000699 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000700 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCallbe349de2010-07-08 06:48:12 +0000701 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
702 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
703 if (FPT && FPT->hasEmptyExceptionSpec())
704 FuncAttrs |= llvm::Attribute::NoUnwind;
705 }
706
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000707 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000708 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000709 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000710 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000711 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000712 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000713 if (TargetDecl->hasAttr<MallocAttr>())
714 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000715 }
716
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000717 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000718 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000719 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000720 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000721 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000722 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000723
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000724 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000725 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000726 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000727 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000728 case ABIArgInfo::Extend:
Chris Lattner4b8585e2010-07-28 23:46:15 +0000729 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000730 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner4b8585e2010-07-28 23:46:15 +0000731 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000732 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000733 break;
Daniel Dunbar67dace892009-02-03 06:17:37 +0000734 case ABIArgInfo::Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000735 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000736 break;
737
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000738 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000739 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000740 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000741 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000742 // sret disables readnone and readonly
743 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
744 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000745 break;
746
Daniel Dunbard3674e62008-09-11 01:48:57 +0000747 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000748 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000749 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000750
Devang Patel597e7082008-09-26 22:53:57 +0000751 if (RetAttrs)
752 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000753
Chris Lattnerff941a62010-07-28 18:24:28 +0000754 // FIXME: we need to honor command line settings also.
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000755 // FIXME: RegParm should be reduced in case of nested functions and/or global
756 // register variable.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000757 signed RegParm = FI.getRegParm();
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000758
759 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000760 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000761 ie = FI.arg_end(); it != ie; ++it) {
762 QualType ParamType = it->type;
763 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000764 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000765
John McCall39ec71f2010-03-27 00:47:27 +0000766 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
767 // have the corresponding parameter variable. It doesn't make
768 // sense to do it here because parameters are so fucked up.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000769 switch (AI.getKind()) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000770 case ABIArgInfo::Extend:
771 if (ParamType->isSignedIntegerType())
772 Attributes |= llvm::Attribute::SExt;
773 else if (ParamType->isUnsignedIntegerType())
774 Attributes |= llvm::Attribute::ZExt;
775 // FALL THROUGH
776 case ABIArgInfo::Direct:
777 if (RegParm > 0 &&
778 (ParamType->isIntegerType() || ParamType->isPointerType())) {
779 RegParm -=
780 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
781 if (RegParm >= 0)
782 Attributes |= llvm::Attribute::InReg;
783 }
784 // FIXME: handle sseregparm someday...
785
Chris Lattner3dd716c2010-06-28 23:44:11 +0000786 if (const llvm::StructType *STy =
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000787 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
788 Index += STy->getNumElements()-1; // 1 will be added below.
789 break;
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000790
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000791 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000792 if (AI.getIndirectByVal())
793 Attributes |= llvm::Attribute::ByVal;
794
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000795 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000796 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000797 // byval disables readnone and readonly.
798 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
799 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000800 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000801
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000802 case ABIArgInfo::Ignore:
803 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000804 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000805
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000806 case ABIArgInfo::Expand: {
Mike Stump11289f42009-09-09 15:08:12 +0000807 std::vector<const llvm::Type*> Tys;
Mike Stump18bb9282009-05-16 07:57:57 +0000808 // FIXME: This is rather inefficient. Do we ever actually need to do
809 // anything here? The result should be just reconstructed on the other
810 // side, so extension should be a non-issue.
Chris Lattner5c740f12010-06-30 19:14:05 +0000811 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000812 Index += Tys.size();
813 continue;
814 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000815 }
Mike Stump11289f42009-09-09 15:08:12 +0000816
Devang Patel322300d2008-09-25 21:02:23 +0000817 if (Attributes)
818 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000819 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000820 }
Devang Patel597e7082008-09-26 22:53:57 +0000821 if (FuncAttrs)
822 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000823}
824
Daniel Dunbard931a872009-02-02 22:03:45 +0000825void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
826 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000827 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000828 // If this is an implicit-return-zero function, go ahead and
829 // initialize the return value. TODO: it might be nice to have
830 // a more general mechanism for this that didn't require synthesized
831 // return statements.
Chris Lattnerc401de92010-07-05 20:21:00 +0000832 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000833 if (FD->hasImplicitReturnZero()) {
834 QualType RetTy = FD->getResultType().getUnqualifiedType();
835 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000836 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000837 Builder.CreateStore(Zero, ReturnValue);
838 }
839 }
840
Mike Stump18bb9282009-05-16 07:57:57 +0000841 // FIXME: We no longer need the types from FunctionArgList; lift up and
842 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000843
Daniel Dunbar613855c2008-09-09 23:27:19 +0000844 // Emit allocs for param decls. Give the LLVM Argument nodes names.
845 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000846
Daniel Dunbar613855c2008-09-09 23:27:19 +0000847 // Name the struct return argument.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000848 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000849 AI->setName("agg.result");
850 ++AI;
851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000853 assert(FI.arg_size() == Args.size() &&
854 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000855 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000856 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000857 i != e; ++i, ++info_it) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000858 const VarDecl *Arg = i->first;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000859 QualType Ty = info_it->type;
860 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000861
862 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000863 case ABIArgInfo::Indirect: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000864 llvm::Value *V = AI;
Daniel Dunbar747865a2009-02-05 09:16:39 +0000865 if (hasAggregateLLVMType(Ty)) {
866 // Do nothing, aggregates and complex variables are accessed by
867 // reference.
868 } else {
869 // Load scalar value from indirect argument.
Daniel Dunbar03816342010-08-21 02:24:36 +0000870 unsigned Alignment = getContext().getTypeAlignInChars(Ty).getQuantity();
871 V = EmitLoadOfScalar(V, false, Alignment, Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000872 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
873 // This must be a promotion, for something like
874 // "void a(x) short x; {..."
875 V = EmitScalarConversion(V, Ty, Arg->getType());
876 }
877 }
Mike Stump11289f42009-09-09 15:08:12 +0000878 EmitParmDecl(*Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000879 break;
880 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000881
882 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000883 case ABIArgInfo::Direct: {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000884 // If we have the trivial case, handle it with no muss and fuss.
885 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000886 ArgI.getCoerceToType() == ConvertType(Ty) &&
887 ArgI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000888 assert(AI != Fn->arg_end() && "Argument mismatch!");
889 llvm::Value *V = AI;
890
John McCall39ec71f2010-03-27 00:47:27 +0000891 if (Arg->getType().isRestrictQualified())
892 AI->addAttr(llvm::Attribute::NoAlias);
893
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000894 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
895 // This must be a promotion, for something like
896 // "void a(x) short x; {..."
897 V = EmitScalarConversion(V, Ty, Arg->getType());
898 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000899 EmitParmDecl(*Arg, V);
900 break;
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattnerc401de92010-07-05 20:21:00 +0000903 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Chris Lattnerff941a62010-07-28 18:24:28 +0000904
905 // The alignment we need to use is the max of the requested alignment for
906 // the argument plus the alignment required by our access code below.
907 unsigned AlignmentToUse =
908 CGF.CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
909 AlignmentToUse = std::max(AlignmentToUse,
910 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
911
912 Alloca->setAlignment(AlignmentToUse);
Chris Lattnerc401de92010-07-05 20:21:00 +0000913 llvm::Value *V = Alloca;
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000914 llvm::Value *Ptr = V; // Pointer to store into.
915
916 // If the value is offset in memory, apply the offset now.
917 if (unsigned Offs = ArgI.getDirectOffset()) {
918 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
919 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
920 Ptr = Builder.CreateBitCast(Ptr,
921 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
922 }
Chris Lattner15ec3612010-06-29 00:06:42 +0000923
924 // If the coerce-to type is a first class aggregate, we flatten it and
925 // pass the elements. Either way is semantically identical, but fast-isel
926 // and the optimizer generally likes scalar values better than FCAs.
927 if (const llvm::StructType *STy =
928 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +0000929 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
930
931 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
932 assert(AI != Fn->arg_end() && "Argument mismatch!");
933 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
934 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
935 Builder.CreateStore(AI++, EltPtr);
Chris Lattner15ec3612010-06-29 00:06:42 +0000936 }
937 } else {
938 // Simple case, just do a coerced store of the argument into the alloca.
939 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner9e748e92010-06-29 00:14:52 +0000940 AI->setName(Arg->getName() + ".coerce");
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000941 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner15ec3612010-06-29 00:06:42 +0000942 }
943
944
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000945 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000946 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar03816342010-08-21 02:24:36 +0000947 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000948 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
949 // This must be a promotion, for something like
950 // "void a(x) short x; {..."
951 V = EmitScalarConversion(V, Ty, Arg->getType());
952 }
953 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000954 EmitParmDecl(*Arg, V);
Chris Lattner3dd716c2010-06-28 23:44:11 +0000955 continue; // Skip ++AI increment, already done.
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000956 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000957
958 case ABIArgInfo::Expand: {
959 // If this structure was expanded into multiple arguments then
960 // we need to create a temporary and reconstruct it from the
961 // arguments.
962 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000963 llvm::Function::arg_iterator End =
Daniel Dunbar2e442a02010-08-21 03:15:20 +0000964 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000965 EmitParmDecl(*Arg, Temp);
966
967 // Name the arguments used in expansion and increment AI.
968 unsigned Index = 0;
969 for (; AI != End; ++AI, ++Index)
970 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
971 continue;
972 }
973
974 case ABIArgInfo::Ignore:
975 // Initialize the local variable appropriately.
976 if (hasAggregateLLVMType(Ty))
977 EmitParmDecl(*Arg, CreateMemTemp(Ty));
978 else
979 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
980
981 // Skip increment, no matching LLVM parameter.
982 continue;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000983 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000984
985 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000986 }
987 assert(AI == Fn->arg_end() && "Argument mismatch!");
988}
989
Chris Lattner3fcc7902010-06-27 01:06:27 +0000990void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000991 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +0000992 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000993 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +0000994 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000995 }
Daniel Dunbar6696e222010-06-30 21:27:58 +0000996
Dan Gohman481e40c2010-07-20 20:13:52 +0000997 llvm::DebugLoc RetDbgLoc;
Chris Lattner726b3d02010-06-26 23:13:19 +0000998 llvm::Value *RV = 0;
999 QualType RetTy = FI.getReturnType();
1000 const ABIArgInfo &RetAI = FI.getReturnInfo();
1001
1002 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001003 case ABIArgInfo::Indirect: {
1004 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattner726b3d02010-06-26 23:13:19 +00001005 if (RetTy->isAnyComplexType()) {
1006 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1007 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1008 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1009 // Do nothing; aggregrates get evaluated directly into the destination.
1010 } else {
1011 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar03816342010-08-21 02:24:36 +00001012 false, Alignment, RetTy);
Chris Lattner726b3d02010-06-26 23:13:19 +00001013 }
1014 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001015 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001016
1017 case ABIArgInfo::Extend:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001018 case ABIArgInfo::Direct:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001019 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1020 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001021 // The internal return value temp always will have pointer-to-return-type
1022 // type, just do a load.
1023
1024 // If the instruction right before the insertion point is a store to the
1025 // return value, we can elide the load, zap the store, and usually zap the
1026 // alloca.
1027 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1028 llvm::StoreInst *SI = 0;
1029 if (InsertBB->empty() ||
1030 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1031 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1032 RV = Builder.CreateLoad(ReturnValue);
1033 } else {
1034 // Get the stored value and nuke the now-dead store.
1035 RetDbgLoc = SI->getDebugLoc();
1036 RV = SI->getValueOperand();
1037 SI->eraseFromParent();
1038
1039 // If that was the only use of the return value, nuke it as well now.
1040 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1041 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1042 ReturnValue = 0;
1043 }
Chris Lattner3fcc7902010-06-27 01:06:27 +00001044 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001045 } else {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001046 llvm::Value *V = ReturnValue;
1047 // If the value is offset in memory, apply the offset now.
1048 if (unsigned Offs = RetAI.getDirectOffset()) {
1049 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1050 V = Builder.CreateConstGEP1_32(V, Offs);
1051 V = Builder.CreateBitCast(V,
1052 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1053 }
1054
1055 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner3fcc7902010-06-27 01:06:27 +00001056 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001057 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00001058
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001059 case ABIArgInfo::Ignore:
Chris Lattner726b3d02010-06-26 23:13:19 +00001060 break;
1061
1062 case ABIArgInfo::Expand:
1063 assert(0 && "Invalid ABI kind for return argument");
1064 }
1065
Daniel Dunbar6696e222010-06-30 21:27:58 +00001066 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Patel65497582010-07-21 18:08:50 +00001067 if (!RetDbgLoc.isUnknown())
1068 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001069}
1070
John McCall23f66262010-05-26 22:34:26 +00001071RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1072 // StartFunction converted the ABI-lowered parameter(s) into a
1073 // local alloca. We need to turn that into an r-value suitable
1074 // for EmitCall.
1075 llvm::Value *Local = GetAddrOfLocalVar(Param);
1076
1077 QualType ArgType = Param->getType();
1078
1079 // For the most part, we just need to load the alloca, except:
1080 // 1) aggregate r-values are actually pointers to temporaries, and
1081 // 2) references to aggregates are pointers directly to the aggregate.
1082 // I don't know why references to non-aggregates are different here.
1083 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1084 if (hasAggregateLLVMType(RefType->getPointeeType()))
1085 return RValue::getAggregate(Local);
1086
1087 // Locals which are references to scalars are represented
1088 // with allocas holding the pointer.
1089 return RValue::get(Builder.CreateLoad(Local));
1090 }
1091
1092 if (ArgType->isAnyComplexType())
1093 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1094
1095 if (hasAggregateLLVMType(ArgType))
1096 return RValue::getAggregate(Local);
1097
Daniel Dunbar03816342010-08-21 02:24:36 +00001098 unsigned Alignment = getContext().getDeclAlign(Param).getQuantity();
1099 return RValue::get(EmitLoadOfScalar(Local, false, Alignment, ArgType));
John McCall23f66262010-05-26 22:34:26 +00001100}
1101
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001102RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson6f5a0152009-05-20 00:24:07 +00001103 if (ArgType->isReferenceType())
Anders Carlsson04775f82010-06-26 16:35:32 +00001104 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001106 return EmitAnyExprToTemp(E);
1107}
1108
John McCallbd309292010-07-06 01:34:17 +00001109/// Emits a call or invoke instruction to the given function, depending
1110/// on the current state of the EH stack.
1111llvm::CallSite
1112CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1113 llvm::Value * const *ArgBegin,
1114 llvm::Value * const *ArgEnd,
1115 const llvm::Twine &Name) {
1116 llvm::BasicBlock *InvokeDest = getInvokeDest();
1117 if (!InvokeDest)
1118 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1119
1120 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1121 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1122 ArgBegin, ArgEnd, Name);
1123 EmitBlock(ContBB);
1124 return Invoke;
1125}
1126
Daniel Dunbard931a872009-02-02 22:03:45 +00001127RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001128 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00001129 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001130 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00001131 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00001132 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00001133 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00001134 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001135
1136 // Handle struct-return functions by passing a pointer to the
1137 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001138 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001139 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001140
1141
Chris Lattner4ca97c32009-06-13 00:26:38 +00001142 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00001143 // alloca to hold the result, unless one is given to us.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001144 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001145 llvm::Value *Value = ReturnValue.getValue();
1146 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +00001147 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +00001148 Args.push_back(Value);
1149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001151 assert(CallInfo.arg_size() == CallArgs.size() &&
1152 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001153 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001154 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001155 I != E; ++I, ++info_it) {
1156 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001157 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001158
Daniel Dunbar03816342010-08-21 02:24:36 +00001159 unsigned Alignment =
1160 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001161 switch (ArgInfo.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001162 case ABIArgInfo::Indirect: {
Daniel Dunbar747865a2009-02-05 09:16:39 +00001163 if (RV.isScalar() || RV.isComplex()) {
1164 // Make a temporary alloca to pass the argument.
Daniel Dunbara7566f12010-02-09 02:48:28 +00001165 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001166 if (RV.isScalar())
Daniel Dunbar03816342010-08-21 02:24:36 +00001167 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1168 Alignment, I->second);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001169 else
Mike Stump11289f42009-09-09 15:08:12 +00001170 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001171 } else {
1172 Args.push_back(RV.getAggregateAddr());
1173 }
1174 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001175 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00001176
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001177 case ABIArgInfo::Ignore:
1178 break;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001179
1180 case ABIArgInfo::Extend:
1181 case ABIArgInfo::Direct: {
1182 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001183 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1184 ArgInfo.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001185 if (RV.isScalar())
1186 Args.push_back(RV.getScalarVal());
1187 else
1188 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1189 break;
1190 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001191
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001192 // FIXME: Avoid the conversion through memory if possible.
1193 llvm::Value *SrcPtr;
1194 if (RV.isScalar()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001195 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar03816342010-08-21 02:24:36 +00001196 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1197 I->second);
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001198 } else if (RV.isComplex()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001199 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001200 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001201 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001202 SrcPtr = RV.getAggregateAddr();
Chris Lattner3dd716c2010-06-28 23:44:11 +00001203
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001204 // If the value is offset in memory, apply the offset now.
1205 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1206 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1207 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1208 SrcPtr = Builder.CreateBitCast(SrcPtr,
1209 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1210
1211 }
1212
Chris Lattner3dd716c2010-06-28 23:44:11 +00001213 // If the coerce-to type is a first class aggregate, we flatten it and
1214 // pass the elements. Either way is semantically identical, but fast-isel
1215 // and the optimizer generally likes scalar values better than FCAs.
1216 if (const llvm::StructType *STy =
Chris Lattner15ec3612010-06-29 00:06:42 +00001217 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001218 SrcPtr = Builder.CreateBitCast(SrcPtr,
1219 llvm::PointerType::getUnqual(STy));
1220 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1221 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerff941a62010-07-28 18:24:28 +00001222 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1223 // We don't know what we're loading from.
1224 LI->setAlignment(1);
1225 Args.push_back(LI);
Chris Lattner15ec3612010-06-29 00:06:42 +00001226 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00001227 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00001228 // In the simple case, just pass the coerced loaded value.
1229 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1230 *this));
Chris Lattner3dd716c2010-06-28 23:44:11 +00001231 }
1232
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001233 break;
1234 }
1235
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001236 case ABIArgInfo::Expand:
1237 ExpandTypeToArgs(I->second, RV, Args);
1238 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001239 }
1240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Chris Lattner4ca97c32009-06-13 00:26:38 +00001242 // If the callee is a bitcast of a function to a varargs pointer to function
1243 // type, check to see if we can remove the bitcast. This handles some cases
1244 // with unprototyped functions.
1245 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1246 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1247 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1248 const llvm::FunctionType *CurFT =
1249 cast<llvm::FunctionType>(CurPT->getElementType());
1250 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001251
Chris Lattner4ca97c32009-06-13 00:26:38 +00001252 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1253 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00001254 ActualFT->getNumParams() == CurFT->getNumParams() &&
1255 ActualFT->getNumParams() == Args.size()) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00001256 bool ArgsMatch = true;
1257 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1258 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1259 ArgsMatch = false;
1260 break;
1261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Chris Lattner4ca97c32009-06-13 00:26:38 +00001263 // Strip the cast if we can get away with it. This is a nice cleanup,
1264 // but also allows us to inline the function at -O0 if it is marked
1265 // always_inline.
1266 if (ArgsMatch)
1267 Callee = CalleeF;
1268 }
1269 }
Mike Stump11289f42009-09-09 15:08:12 +00001270
Daniel Dunbar613855c2008-09-09 23:27:19 +00001271
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001272 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00001273 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001274 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +00001275 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1276 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +00001277
John McCallbd309292010-07-06 01:34:17 +00001278 llvm::BasicBlock *InvokeDest = 0;
1279 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1280 InvokeDest = getInvokeDest();
1281
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001282 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00001283 if (!InvokeDest) {
Jay Foad7d0479f2009-05-21 09:52:38 +00001284 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001285 } else {
1286 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +00001287 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +00001288 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001289 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001290 }
Chris Lattnere70a0072010-06-29 16:40:28 +00001291 if (callOrInvoke)
David Chisnallff5f88c2010-05-02 13:41:58 +00001292 *callOrInvoke = CS.getInstruction();
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001293
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001294 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001295 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001296
1297 // If the call doesn't return, finish the basic block and clear the
1298 // insertion point; this allows the rest of IRgen to discard
1299 // unreachable code.
1300 if (CS.doesNotReturn()) {
1301 Builder.CreateUnreachable();
1302 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001303
Mike Stump18bb9282009-05-16 07:57:57 +00001304 // FIXME: For now, emit a dummy basic block because expr emitters in
1305 // generally are not ready to handle emitting expressions at unreachable
1306 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001307 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001308
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001309 // Return a reasonable RValue.
1310 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001311 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001312
1313 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001314 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001315 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001316
1317 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001318 case ABIArgInfo::Indirect: {
1319 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001320 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001321 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001322 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001323 return RValue::getAggregate(Args[0]);
Daniel Dunbar03816342010-08-21 02:24:36 +00001324 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1325 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001326
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001327 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001328 // If we are ignoring an argument that had a result, make sure to
1329 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001330 return GetUndefRValue(RetTy);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001331
1332 case ABIArgInfo::Extend:
1333 case ABIArgInfo::Direct: {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001334 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1335 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001336 if (RetTy->isAnyComplexType()) {
1337 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1338 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1339 return RValue::getComplex(std::make_pair(Real, Imag));
1340 }
1341 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1342 llvm::Value *DestPtr = ReturnValue.getValue();
1343 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001344
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001345 if (!DestPtr) {
1346 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1347 DestIsVolatile = false;
1348 }
1349 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1350 return RValue::getAggregate(DestPtr);
1351 }
1352 return RValue::get(CI);
1353 }
1354
Anders Carlsson17490832009-12-24 20:40:36 +00001355 llvm::Value *DestPtr = ReturnValue.getValue();
1356 bool DestIsVolatile = ReturnValue.isVolatile();
1357
1358 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001359 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001360 DestIsVolatile = false;
1361 }
1362
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001363 // If the value is offset in memory, apply the offset now.
1364 llvm::Value *StorePtr = DestPtr;
1365 if (unsigned Offs = RetAI.getDirectOffset()) {
1366 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1367 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1368 StorePtr = Builder.CreateBitCast(StorePtr,
1369 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1370 }
1371 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1372
Daniel Dunbar03816342010-08-21 02:24:36 +00001373 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001374 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001375 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001376 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001377 return RValue::getAggregate(DestPtr);
Daniel Dunbar03816342010-08-21 02:24:36 +00001378 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001379 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001380
Daniel Dunbard3674e62008-09-11 01:48:57 +00001381 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001382 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001383 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001384
1385 assert(0 && "Unhandled ABIArgInfo::Kind");
1386 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001387}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001388
1389/* VarArg handling */
1390
1391llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1392 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1393}