blob: 1df3803b77330b037742241bb9b5a555c72264ab [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"
Chris Lattnere70a0072010-06-29 16:40:28 +000016#include "ABIInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000017#include "CodeGenFunction.h"
Daniel Dunbarc68897d2008-09-10 00:41:16 +000018#include "CodeGenModule.h"
Daniel Dunbard9eff3d2008-10-13 17:02:26 +000019#include "clang/Basic/TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000020#include "clang/AST/Decl.h"
Anders Carlssonb15b55c2009-04-03 22:48:58 +000021#include "clang/AST/DeclCXX.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000022#include "clang/AST/DeclObjC.h"
Chandler Carruth85098242010-06-15 23:19:56 +000023#include "clang/Frontend/CodeGenOptions.h"
Devang Patel3e1f51b2008-09-24 01:01:36 +000024#include "llvm/Attributes.h"
Daniel Dunbarb960b7b2009-03-02 04:32:35 +000025#include "llvm/Support/CallSite.h"
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000027using namespace clang;
28using namespace CodeGen;
29
30/***/
31
John McCallab26cfa2010-02-05 21:31:56 +000032static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
33 switch (CC) {
34 default: return llvm::CallingConv::C;
35 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
36 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregora941dca2010-05-18 16:57:00 +000037 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
John McCallab26cfa2010-02-05 21:31:56 +000038 }
39}
40
John McCall8ee376f2010-02-24 07:14:12 +000041/// Derives the 'this' type for codegen purposes, i.e. ignoring method
42/// qualification.
43/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000044static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
45 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
46 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000047}
48
John McCall8ee376f2010-02-24 07:14:12 +000049/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000050static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
51 return MD->getType()->getCanonicalTypeUnqualified()
52 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000053}
54
55/// Returns the "extra-canonicalized" return type, which discards
56/// qualifiers on the return type. Codegen doesn't care about them,
57/// and it makes ABI code a little easier to be able to assume that
58/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000059static CanQualType GetReturnType(QualType RetTy) {
60 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000061}
62
63const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000064CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
65 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000066 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
67 llvm::SmallVector<CanQualType, 16>(),
Chris Lattner5c740f12010-06-30 19:14:05 +000068 FTNP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000069}
70
71/// \param Args - contains any initial parameters besides those
72/// in the formal type
73static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCall2da83a32010-02-26 00:48:12 +000074 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +000075 CanQual<FunctionProtoType> FTP,
76 bool IsRecursive = false) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000077 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000078 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000079 ArgTys.push_back(FTP->getArgType(i));
John McCall2da83a32010-02-26 00:48:12 +000080 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Chris Lattner5c740f12010-06-30 19:14:05 +000081 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000082}
83
84const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000085CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
86 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000087 llvm::SmallVector<CanQualType, 16> ArgTys;
Chris Lattner5c740f12010-06-30 19:14:05 +000088 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbar7feafc72009-09-11 22:24:53 +000089}
90
John McCallab26cfa2010-02-05 21:31:56 +000091static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000092 // Set the appropriate calling convention for the Function.
93 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000094 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000095
96 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000097 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000098
Douglas Gregora941dca2010-05-18 16:57:00 +000099 if (D->hasAttr<ThisCallAttr>())
100 return CC_X86ThisCall;
101
John McCallab26cfa2010-02-05 21:31:56 +0000102 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000103}
104
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000105const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
106 const FunctionProtoType *FTP) {
John McCall2da83a32010-02-26 00:48:12 +0000107 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000108
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000109 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000110 ArgTys.push_back(GetThisType(Context, RD));
111
112 return ::getFunctionInfo(*this, ArgTys,
John McCall2da83a32010-02-26 00:48:12 +0000113 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000114}
115
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000116const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000117 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000118
Chris Lattnerbea5b622009-05-12 20:27:19 +0000119 // Add the 'this' pointer unless this is a static method.
120 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000121 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000122
John McCall8ee376f2010-02-24 07:14:12 +0000123 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000124}
125
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000126const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
127 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000128 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000129
130 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000131 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000132
133 // Check if we need to add a VTT parameter (which has type void **).
134 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
135 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall8ee376f2010-02-24 07:14:12 +0000136
137 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000138}
139
140const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
141 CXXDtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000142 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000143
144 // Add the 'this' pointer.
John McCall2da83a32010-02-26 00:48:12 +0000145 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000146
147 // Check if we need to add a VTT parameter (which has type void **).
148 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
149 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall8ee376f2010-02-24 07:14:12 +0000150
151 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000152}
153
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000154const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000155 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000156 if (MD->isInstance())
157 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000158
John McCall2da83a32010-02-26 00:48:12 +0000159 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
160 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000161 if (isa<FunctionNoProtoType>(FTy))
John McCall2da83a32010-02-26 00:48:12 +0000162 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
163 assert(isa<FunctionProtoType>(FTy));
164 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000165}
166
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000167const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000168 llvm::SmallVector<CanQualType, 16> ArgTys;
169 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
170 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000171 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000172 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000173 e = MD->param_end(); i != e; ++i) {
174 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
175 }
176 return getFunctionInfo(GetReturnType(MD->getResultType()),
177 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000178 FunctionType::ExtInfo(
179 /*NoReturn*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000180 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000181 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000182}
183
Anders Carlsson6710c532010-02-06 02:44:09 +0000184const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
185 // FIXME: Do we need to handle ObjCMethodDecl?
186 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
187
188 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
189 return getFunctionInfo(CD, GD.getCtorType());
190
191 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
192 return getFunctionInfo(DD, GD.getDtorType());
193
194 return getFunctionInfo(FD);
195}
196
Mike Stump11289f42009-09-09 15:08:12 +0000197const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000198 const CallArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000199 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000200 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000201 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000202 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000203 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000204 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000205 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000206}
207
Mike Stump11289f42009-09-09 15:08:12 +0000208const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000209 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000210 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000211 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000212 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000213 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000214 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000215 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000216 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000217}
218
John McCall2da83a32010-02-26 00:48:12 +0000219const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
220 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +0000221 const FunctionType::ExtInfo &Info,
222 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +0000223#ifndef NDEBUG
224 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
225 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
226 assert(I->isCanonicalAsParam());
227#endif
228
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000229 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000230
Daniel Dunbare0be8292009-02-03 00:07:12 +0000231 // Lookup or create unique function info.
232 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000233 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000234 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000235
236 void *InsertPos = 0;
237 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
238 if (FI)
239 return *FI;
240
Daniel Dunbar313321e2009-02-03 05:31:23 +0000241 // Construct the function info.
Chris Lattner3dd716c2010-06-28 23:44:11 +0000242 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000243 ArgTys.data(), ArgTys.size());
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000244 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000245
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000246 // ABI lowering wants to know what our preferred type for the argument is in
247 // various situations, pass it in.
248 llvm::SmallVector<const llvm::Type *, 8> PreferredArgTypes;
249 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
Chris Lattner5c740f12010-06-30 19:14:05 +0000250 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I) {
251 // If this is being called from the guts of the ConvertType loop, make sure
252 // to call ConvertTypeRecursive so we don't get into issues with cyclic
253 // pointer type structures.
Chris Lattner0e7929f2010-07-01 06:20:47 +0000254 PreferredArgTypes.push_back(ConvertTypeRecursive(*I));
Chris Lattner5c740f12010-06-30 19:14:05 +0000255 }
Chris Lattner0e7929f2010-07-01 06:20:47 +0000256
Daniel Dunbar313321e2009-02-03 05:31:23 +0000257 // Compute ABI information.
Chris Lattner1d7c9f72010-06-29 01:08:48 +0000258 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext(),
259 PreferredArgTypes.data(), PreferredArgTypes.size());
Daniel Dunbar313321e2009-02-03 05:31:23 +0000260
Chris Lattner0e7929f2010-07-01 06:20:47 +0000261 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
262 // types, resolve them now. These pointers may point to this function, which
263 // we *just* filled in the FunctionInfo for.
264 if (!IsRecursive && !PointersToResolve.empty()) {
265 // Use PATypeHolder's so that our preferred types don't dangle under
266 // refinement.
267 llvm::SmallVector<llvm::PATypeHolder, 8> Handles(PreferredArgTypes.begin(),
268 PreferredArgTypes.end());
269 HandleLateResolvedPointers();
270 PreferredArgTypes.clear();
271 PreferredArgTypes.append(Handles.begin(), Handles.end());
272 }
273
274
Daniel Dunbare0be8292009-02-03 00:07:12 +0000275 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000276}
277
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000278CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattner34d62812010-06-29 18:13:52 +0000279 bool _NoReturn, unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000280 CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000281 const CanQualType *ArgTys,
282 unsigned NumArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000283 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000284 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000285 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000286{
Chris Lattner34d62812010-06-29 18:13:52 +0000287 NumArgs = NumArgTys;
Chris Lattner3dd716c2010-06-28 23:44:11 +0000288
289 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattner34d62812010-06-29 18:13:52 +0000290 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar313321e2009-02-03 05:31:23 +0000291 Args[0].type = ResTy;
Chris Lattner34d62812010-06-29 18:13:52 +0000292 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar313321e2009-02-03 05:31:23 +0000293 Args[1 + i].type = ArgTys[i];
294}
295
296/***/
297
Mike Stump11289f42009-09-09 15:08:12 +0000298void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattner5c740f12010-06-30 19:14:05 +0000299 std::vector<const llvm::Type*> &ArgTys,
300 bool IsRecursive) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000301 const RecordType *RT = Ty->getAsStructureType();
302 assert(RT && "Can only expand structure types.");
303 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000304 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000305 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000306
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000307 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
308 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000309 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000310 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000311 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000312
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000313 QualType FT = FD->getType();
Chris Lattnerff941a62010-07-28 18:24:28 +0000314 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattner5c740f12010-06-30 19:14:05 +0000315 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerff941a62010-07-28 18:24:28 +0000316 else
Chris Lattner5c740f12010-06-30 19:14:05 +0000317 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000318 }
319}
320
Mike Stump11289f42009-09-09 15:08:12 +0000321llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000322CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
323 llvm::Function::arg_iterator AI) {
324 const RecordType *RT = Ty->getAsStructureType();
325 assert(RT && "Can only expand structure types.");
326
327 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000328 assert(LV.isSimple() &&
329 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000330 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000331 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
332 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000333 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000334 QualType FT = FD->getType();
335
336 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000337 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000338 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
339 AI = ExpandTypeFromArgs(FT, LV, AI);
340 } else {
341 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
342 ++AI;
343 }
344 }
345
346 return AI;
347}
348
Mike Stump11289f42009-09-09 15:08:12 +0000349void
350CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000351 llvm::SmallVector<llvm::Value*, 16> &Args) {
352 const RecordType *RT = Ty->getAsStructureType();
353 assert(RT && "Can only expand structure types.");
354
355 RecordDecl *RD = RT->getDecl();
356 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
357 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000358 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
359 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000360 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000361 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000362
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000363 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000364 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000365 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
366 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
367 } else {
368 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000369 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000370 "Unexpected non-scalar rvalue during struct expansion.");
371 Args.push_back(RV.getScalarVal());
372 }
373 }
374}
375
Chris Lattner895c52b2010-06-27 06:04:18 +0000376/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000377/// accessing some number of bytes out of it, try to gep into the struct to get
378/// at its inner goodness. Dive as deep as possible without entering an element
379/// with an in-memory size smaller than DstSize.
380static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000381EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
382 const llvm::StructType *SrcSTy,
383 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000384 // We can't dive into a zero-element struct.
385 if (SrcSTy->getNumElements() == 0) return SrcPtr;
386
387 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
388
389 // If the first elt is at least as large as what we're looking for, or if the
390 // first element is the same size as the whole struct, we can enter it.
391 uint64_t FirstEltSize =
392 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
393 if (FirstEltSize < DstSize &&
394 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
395 return SrcPtr;
396
397 // GEP into the first element.
398 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
399
400 // If the first element is a struct, recurse.
401 const llvm::Type *SrcTy =
402 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
403 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000404 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000405
406 return SrcPtr;
407}
408
Chris Lattner055097f2010-06-27 06:26:04 +0000409/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
410/// are either integers or pointers. This does a truncation of the value if it
411/// is too large or a zero extension if it is too small.
412static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
413 const llvm::Type *Ty,
414 CodeGenFunction &CGF) {
415 if (Val->getType() == Ty)
416 return Val;
417
418 if (isa<llvm::PointerType>(Val->getType())) {
419 // If this is Pointer->Pointer avoid conversion to and from int.
420 if (isa<llvm::PointerType>(Ty))
421 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
422
423 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +0000424 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +0000425 }
426
427 const llvm::Type *DestIntTy = Ty;
428 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +0000429 DestIntTy = CGF.IntPtrTy;
Chris Lattner055097f2010-06-27 06:26:04 +0000430
431 if (Val->getType() != DestIntTy)
432 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
433
434 if (isa<llvm::PointerType>(Ty))
435 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
436 return Val;
437}
438
Chris Lattner1cd66982010-06-27 05:56:15 +0000439
440
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000441/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
442/// a pointer to an object of type \arg Ty.
443///
444/// This safely handles the case when the src type is smaller than the
445/// destination type; in this situation the values of bits which not
446/// present in the src are undefined.
447static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
448 const llvm::Type *Ty,
449 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000450 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000451 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000452
453 // If SrcTy and Ty are the same, just do a load.
454 if (SrcTy == Ty)
455 return CGF.Builder.CreateLoad(SrcPtr);
456
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000457 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner1cd66982010-06-27 05:56:15 +0000458
459 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000460 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000461 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
462 }
463
464 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000465
Chris Lattner055097f2010-06-27 06:26:04 +0000466 // If the source and destination are integer or pointer types, just do an
467 // extension or truncation to the desired type.
468 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
469 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
470 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
471 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
472 }
473
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000474 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000475 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000476 // Generally SrcSize is never greater than DstSize, since this means we are
477 // losing bits. However, this can happen in cases where the structure has
478 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000479 //
Mike Stump18bb9282009-05-16 07:57:57 +0000480 // FIXME: Assert that we aren't truncating non-padding bits when have access
481 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000482 llvm::Value *Casted =
483 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000484 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
485 // FIXME: Use better alignment / avoid requiring aligned load.
486 Load->setAlignment(1);
487 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000488 }
Chris Lattner3fcc7902010-06-27 01:06:27 +0000489
490 // Otherwise do coercion through memory. This is stupid, but
491 // simple.
492 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
493 llvm::Value *Casted =
494 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
495 llvm::StoreInst *Store =
496 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
497 // FIXME: Use better alignment / avoid requiring aligned store.
498 Store->setAlignment(1);
499 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000500}
501
502/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
503/// where the source and destination may have different types.
504///
505/// This safely handles the case when the src type is larger than the
506/// destination type; the upper bits of the src will be lost.
507static void CreateCoercedStore(llvm::Value *Src,
508 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000509 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000510 CodeGenFunction &CGF) {
511 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000512 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000513 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000514 if (SrcTy == DstTy) {
515 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
516 return;
517 }
518
519 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
520
Chris Lattner895c52b2010-06-27 06:04:18 +0000521 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
522 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
523 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
524 }
525
Chris Lattner055097f2010-06-27 06:26:04 +0000526 // If the source and destination are integer or pointer types, just do an
527 // extension or truncation to the desired type.
528 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
529 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
530 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
531 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
532 return;
533 }
534
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000535 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000536
Daniel Dunbar313321e2009-02-03 05:31:23 +0000537 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000538 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000539 llvm::Value *Casted =
540 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000541 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlsson17490832009-12-24 20:40:36 +0000542 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000543 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000544 // Otherwise do coercion through memory. This is stupid, but
545 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000546
547 // Generally SrcSize is never greater than DstSize, since this means we are
548 // losing bits. However, this can happen in cases where the structure has
549 // additional padding, for example due to a user specified alignment.
550 //
551 // FIXME: Assert that we aren't truncating non-padding bits when have access
552 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000553 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
554 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000555 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000556 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000557 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
558 // FIXME: Use better alignment / avoid requiring aligned load.
559 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000560 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000561 }
562}
563
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000564/***/
565
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000566bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000567 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000568}
569
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000570bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
571 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
572 switch (BT->getKind()) {
573 default:
574 return false;
575 case BuiltinType::Float:
576 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
577 case BuiltinType::Double:
578 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
579 case BuiltinType::LongDouble:
580 return getContext().Target.useObjCFPRetForRealType(
581 TargetInfo::LongDouble);
582 }
583 }
584
585 return false;
586}
587
John McCallf8ff7b92010-02-23 00:48:20 +0000588const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
589 const CGFunctionInfo &FI = getFunctionInfo(GD);
590
591 // For definition purposes, don't consider a K&R function variadic.
592 bool Variadic = false;
593 if (const FunctionProtoType *FPT =
594 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
595 Variadic = FPT->isVariadic();
596
Chris Lattner5c740f12010-06-30 19:14:05 +0000597 return GetFunctionType(FI, Variadic, false);
John McCallf8ff7b92010-02-23 00:48:20 +0000598}
599
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000600const llvm::FunctionType *
Chris Lattner5c740f12010-06-30 19:14:05 +0000601CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
602 bool IsRecursive) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000603 std::vector<const llvm::Type*> ArgTys;
604
605 const llvm::Type *ResultType = 0;
606
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000607 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000608 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbard3674e62008-09-11 01:48:57 +0000609 switch (RetAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000610 case ABIArgInfo::Expand:
611 assert(0 && "Invalid ABI kind for return argument");
612
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000613 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000614 case ABIArgInfo::Direct:
Chris Lattner5c740f12010-06-30 19:14:05 +0000615 ResultType = ConvertType(RetTy, IsRecursive);
Daniel Dunbar67dace892009-02-03 06:17:37 +0000616 break;
617
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000618 case ABIArgInfo::Indirect: {
619 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson41a75022009-08-13 21:57:51 +0000620 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattner5c740f12010-06-30 19:14:05 +0000621 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000622 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
623 break;
624 }
625
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000626 case ABIArgInfo::Ignore:
Owen Anderson41a75022009-08-13 21:57:51 +0000627 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000628 break;
629
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000630 case ABIArgInfo::Coerce:
Daniel Dunbar573884e2008-09-10 07:04:09 +0000631 ResultType = RetAI.getCoerceToType();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000632 break;
633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
635 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000636 ie = FI.arg_end(); it != ie; ++it) {
637 const ABIArgInfo &AI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000638
Daniel Dunbard3674e62008-09-11 01:48:57 +0000639 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000640 case ABIArgInfo::Ignore:
641 break;
642
Chris Lattner3dd716c2010-06-28 23:44:11 +0000643 case ABIArgInfo::Coerce: {
644 // 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 Lattner3dd716c2010-06-28 23:44:11 +0000655 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000656
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000657 case ABIArgInfo::Indirect: {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000658 // indirect arguments are always on the stack, which is addr space #0.
Chris Lattner5c740f12010-06-30 19:14:05 +0000659 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000660 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000661 break;
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000662 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000663
664 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000665 case ABIArgInfo::Direct:
Chris Lattner5c740f12010-06-30 19:14:05 +0000666 ArgTys.push_back(ConvertType(it->type, IsRecursive));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000667 break;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Daniel Dunbard3674e62008-09-11 01:48:57 +0000669 case ABIArgInfo::Expand:
Chris Lattner5c740f12010-06-30 19:14:05 +0000670 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000671 break;
672 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000673 }
674
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000675 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000676}
677
Anders Carlsson64457732009-11-24 05:08:52 +0000678const llvm::Type *
Anders Carlsson11e51402010-04-17 20:15:18 +0000679CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlsson64457732009-11-24 05:08:52 +0000680 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
681
Eli Friedmanc8731be2010-05-30 06:03:20 +0000682 if (!VerifyFuncTypeComplete(FPT))
Chris Lattner5c740f12010-06-30 19:14:05 +0000683 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic(), false);
Anders Carlsson64457732009-11-24 05:08:52 +0000684
685 return llvm::OpaqueType::get(getLLVMContext());
686}
687
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000688void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000689 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000690 AttributeListType &PAL,
691 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000692 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000693 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000694
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000695 CallingConv = FI.getEffectiveCallingConvention();
696
John McCallab26cfa2010-02-05 21:31:56 +0000697 if (FI.isNoReturn())
698 FuncAttrs |= llvm::Attribute::NoReturn;
699
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000700 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000701 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000702 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000703 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCallbe349de2010-07-08 06:48:12 +0000704 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
705 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
706 if (FPT && FPT->hasEmptyExceptionSpec())
707 FuncAttrs |= llvm::Attribute::NoUnwind;
708 }
709
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000710 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000711 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000712 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000713 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000714 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000715 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000716 if (TargetDecl->hasAttr<MallocAttr>())
717 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000718 }
719
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000720 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000721 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000722 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000723 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000724 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000725 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000726
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000727 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000728 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000729 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000730 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000731 case ABIArgInfo::Extend:
Chris Lattner4b8585e2010-07-28 23:46:15 +0000732 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000733 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner4b8585e2010-07-28 23:46:15 +0000734 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000735 RetAttrs |= llvm::Attribute::ZExt;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000736 // FALLTHROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000737 case ABIArgInfo::Direct:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000738 break;
739
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000740 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000741 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000742 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000743 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000744 // sret disables readnone and readonly
745 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
746 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000747 break;
748
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000749 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000750 case ABIArgInfo::Coerce:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000751 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000752
Daniel Dunbard3674e62008-09-11 01:48:57 +0000753 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000754 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000755 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000756
Devang Patel597e7082008-09-26 22:53:57 +0000757 if (RetAttrs)
758 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000759
Chris Lattnerff941a62010-07-28 18:24:28 +0000760 // FIXME: we need to honor command line settings also.
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000761 // FIXME: RegParm should be reduced in case of nested functions and/or global
762 // register variable.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000763 signed RegParm = FI.getRegParm();
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000764
765 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000766 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000767 ie = FI.arg_end(); it != ie; ++it) {
768 QualType ParamType = it->type;
769 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000770 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000771
John McCall39ec71f2010-03-27 00:47:27 +0000772 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
773 // have the corresponding parameter variable. It doesn't make
774 // sense to do it here because parameters are so fucked up.
Nuno Lopes72513272009-12-07 18:30:06 +0000775
Daniel Dunbard3674e62008-09-11 01:48:57 +0000776 switch (AI.getKind()) {
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000777 case ABIArgInfo::Coerce:
Chris Lattner3dd716c2010-06-28 23:44:11 +0000778 if (const llvm::StructType *STy =
779 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
780 Index += STy->getNumElements();
781 else
782 ++Index;
783 continue; // Skip index increment.
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000784
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000785 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000786 if (AI.getIndirectByVal())
787 Attributes |= llvm::Attribute::ByVal;
788
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000789 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000790 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000791 // byval disables readnone and readonly.
792 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
793 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000794 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000795
796 case ABIArgInfo::Extend:
Chris Lattner4b8585e2010-07-28 23:46:15 +0000797 if (ParamType->isSignedIntegerType())
798 Attributes |= llvm::Attribute::SExt;
799 else if (ParamType->isUnsignedIntegerType())
800 Attributes |= llvm::Attribute::ZExt;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000801 // FALLS THROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000802 case ABIArgInfo::Direct:
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000803 if (RegParm > 0 &&
804 (ParamType->isIntegerType() || ParamType->isPointerType())) {
805 RegParm -=
806 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
807 if (RegParm >= 0)
808 Attributes |= llvm::Attribute::InReg;
809 }
810 // FIXME: handle sseregparm someday...
Daniel Dunbard3674e62008-09-11 01:48:57 +0000811 break;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000812
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000813 case ABIArgInfo::Ignore:
814 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000815 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000816
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000817 case ABIArgInfo::Expand: {
Mike Stump11289f42009-09-09 15:08:12 +0000818 std::vector<const llvm::Type*> Tys;
Mike Stump18bb9282009-05-16 07:57:57 +0000819 // FIXME: This is rather inefficient. Do we ever actually need to do
820 // anything here? The result should be just reconstructed on the other
821 // side, so extension should be a non-issue.
Chris Lattner5c740f12010-06-30 19:14:05 +0000822 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000823 Index += Tys.size();
824 continue;
825 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Devang Patel322300d2008-09-25 21:02:23 +0000828 if (Attributes)
829 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000830 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000831 }
Devang Patel597e7082008-09-26 22:53:57 +0000832 if (FuncAttrs)
833 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000834}
835
Daniel Dunbard931a872009-02-02 22:03:45 +0000836void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
837 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000838 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000839 // If this is an implicit-return-zero function, go ahead and
840 // initialize the return value. TODO: it might be nice to have
841 // a more general mechanism for this that didn't require synthesized
842 // return statements.
Chris Lattnerc401de92010-07-05 20:21:00 +0000843 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000844 if (FD->hasImplicitReturnZero()) {
845 QualType RetTy = FD->getResultType().getUnqualifiedType();
846 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000847 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000848 Builder.CreateStore(Zero, ReturnValue);
849 }
850 }
851
Mike Stump18bb9282009-05-16 07:57:57 +0000852 // FIXME: We no longer need the types from FunctionArgList; lift up and
853 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000854
Daniel Dunbar613855c2008-09-09 23:27:19 +0000855 // Emit allocs for param decls. Give the LLVM Argument nodes names.
856 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000857
Daniel Dunbar613855c2008-09-09 23:27:19 +0000858 // Name the struct return argument.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000859 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000860 AI->setName("agg.result");
861 ++AI;
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000864 assert(FI.arg_size() == Args.size() &&
865 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000866 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000867 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000868 i != e; ++i, ++info_it) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000869 const VarDecl *Arg = i->first;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000870 QualType Ty = info_it->type;
871 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000872
873 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000874 case ABIArgInfo::Indirect: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000875 llvm::Value *V = AI;
Daniel Dunbar747865a2009-02-05 09:16:39 +0000876 if (hasAggregateLLVMType(Ty)) {
877 // Do nothing, aggregates and complex variables are accessed by
878 // reference.
879 } else {
880 // Load scalar value from indirect argument.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000881 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000882 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
883 // This must be a promotion, for something like
884 // "void a(x) short x; {..."
885 V = EmitScalarConversion(V, Ty, Arg->getType());
886 }
887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888 EmitParmDecl(*Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000889 break;
890 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000891
892 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000893 case ABIArgInfo::Direct: {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000894 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner3dd716c2010-06-28 23:44:11 +0000895 llvm::Value *V = AI;
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000896 if (hasAggregateLLVMType(Ty)) {
897 // Create a temporary alloca to hold the argument; the rest of
898 // codegen expects to access aggregates & complex values by
899 // reference.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000900 V = CreateMemTemp(Ty);
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000901 Builder.CreateStore(AI, V);
902 } else {
John McCall39ec71f2010-03-27 00:47:27 +0000903 if (Arg->getType().isRestrictQualified())
904 AI->addAttr(llvm::Attribute::NoAlias);
905
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000906 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
907 // This must be a promotion, for something like
908 // "void a(x) short x; {..."
909 V = EmitScalarConversion(V, Ty, Arg->getType());
910 }
Daniel Dunbar613855c2008-09-09 23:27:19 +0000911 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000912 EmitParmDecl(*Arg, V);
913 break;
914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000916 case ABIArgInfo::Expand: {
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000917 // If this structure was expanded into multiple arguments then
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000918 // we need to create a temporary and reconstruct it from the
919 // arguments.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000920 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000921 // FIXME: What are the right qualifiers here?
Mike Stump11289f42009-09-09 15:08:12 +0000922 llvm::Function::arg_iterator End =
John McCall8ccfcb52009-09-24 19:53:00 +0000923 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000924 EmitParmDecl(*Arg, Temp);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000925
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000926 // Name the arguments used in expansion and increment AI.
927 unsigned Index = 0;
928 for (; AI != End; ++AI, ++Index)
Daniel Dunbarb5aacc22009-10-19 01:21:05 +0000929 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000930 continue;
931 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000932
933 case ABIArgInfo::Ignore:
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000934 // Initialize the local variable appropriately.
Mike Stump11289f42009-09-09 15:08:12 +0000935 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbara7566f12010-02-09 02:48:28 +0000936 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000937 } else {
938 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
939 }
Mike Stump11289f42009-09-09 15:08:12 +0000940
Daniel Dunbarfc7c7612009-02-03 20:00:13 +0000941 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000942 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000943
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000944 case ABIArgInfo::Coerce: {
Chris Lattnerc401de92010-07-05 20:21:00 +0000945 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Chris Lattnerff941a62010-07-28 18:24:28 +0000946
947 // The alignment we need to use is the max of the requested alignment for
948 // the argument plus the alignment required by our access code below.
949 unsigned AlignmentToUse =
950 CGF.CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
951 AlignmentToUse = std::max(AlignmentToUse,
952 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
953
954 Alloca->setAlignment(AlignmentToUse);
Chris Lattnerc401de92010-07-05 20:21:00 +0000955 llvm::Value *V = Alloca;
Chris Lattner15ec3612010-06-29 00:06:42 +0000956
957 // If the coerce-to type is a first class aggregate, we flatten it and
958 // pass the elements. Either way is semantically identical, but fast-isel
959 // and the optimizer generally likes scalar values better than FCAs.
960 if (const llvm::StructType *STy =
961 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +0000962 llvm::Value *Ptr = V;
963 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
964
965 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
966 assert(AI != Fn->arg_end() && "Argument mismatch!");
967 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
968 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
969 Builder.CreateStore(AI++, EltPtr);
Chris Lattner15ec3612010-06-29 00:06:42 +0000970 }
971 } else {
972 // Simple case, just do a coerced store of the argument into the alloca.
973 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner9e748e92010-06-29 00:14:52 +0000974 AI->setName(Arg->getName() + ".coerce");
Chris Lattner15ec3612010-06-29 00:06:42 +0000975 CreateCoercedStore(AI++, V, /*DestIsVolatile=*/false, *this);
976 }
977
978
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000979 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000980 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000981 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000982 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
983 // This must be a promotion, for something like
984 // "void a(x) short x; {..."
985 V = EmitScalarConversion(V, Ty, Arg->getType());
986 }
987 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000988 EmitParmDecl(*Arg, V);
Chris Lattner3dd716c2010-06-28 23:44:11 +0000989 continue; // Skip ++AI increment, already done.
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000990 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000991 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000992
993 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000994 }
995 assert(AI == Fn->arg_end() && "Argument mismatch!");
996}
997
Chris Lattner3fcc7902010-06-27 01:06:27 +0000998void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000999 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +00001000 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001001 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +00001002 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001003 }
Daniel Dunbar6696e222010-06-30 21:27:58 +00001004
Dan Gohman481e40c2010-07-20 20:13:52 +00001005 llvm::DebugLoc RetDbgLoc;
Chris Lattner726b3d02010-06-26 23:13:19 +00001006 llvm::Value *RV = 0;
1007 QualType RetTy = FI.getReturnType();
1008 const ABIArgInfo &RetAI = FI.getReturnInfo();
1009
1010 switch (RetAI.getKind()) {
1011 case ABIArgInfo::Indirect:
1012 if (RetTy->isAnyComplexType()) {
1013 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1014 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1015 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1016 // Do nothing; aggregrates get evaluated directly into the destination.
1017 } else {
1018 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1019 false, RetTy);
1020 }
1021 break;
1022
1023 case ABIArgInfo::Extend:
Chris Lattner3fcc7902010-06-27 01:06:27 +00001024 case ABIArgInfo::Direct: {
1025 // The internal return value temp always will have pointer-to-return-type
1026 // type, just do a load.
1027
1028 // If the instruction right before the insertion point is a store to the
1029 // return value, we can elide the load, zap the store, and usually zap the
1030 // alloca.
1031 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1032 llvm::StoreInst *SI = 0;
1033 if (InsertBB->empty() ||
1034 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1035 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1036 RV = Builder.CreateLoad(ReturnValue);
1037 } else {
1038 // Get the stored value and nuke the now-dead store.
Dan Gohman481e40c2010-07-20 20:13:52 +00001039 RetDbgLoc = SI->getDebugLoc();
Chris Lattner3fcc7902010-06-27 01:06:27 +00001040 RV = SI->getValueOperand();
1041 SI->eraseFromParent();
1042
1043 // If that was the only use of the return value, nuke it as well now.
1044 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1045 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1046 ReturnValue = 0;
1047 }
1048 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001049 break;
Chris Lattner3fcc7902010-06-27 01:06:27 +00001050 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001051 case ABIArgInfo::Ignore:
1052 break;
1053
1054 case ABIArgInfo::Coerce:
1055 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
1056 break;
1057
1058 case ABIArgInfo::Expand:
1059 assert(0 && "Invalid ABI kind for return argument");
1060 }
1061
Daniel Dunbar6696e222010-06-30 21:27:58 +00001062 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Patel65497582010-07-21 18:08:50 +00001063 if (!RetDbgLoc.isUnknown())
1064 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001065}
1066
John McCall23f66262010-05-26 22:34:26 +00001067RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1068 // StartFunction converted the ABI-lowered parameter(s) into a
1069 // local alloca. We need to turn that into an r-value suitable
1070 // for EmitCall.
1071 llvm::Value *Local = GetAddrOfLocalVar(Param);
1072
1073 QualType ArgType = Param->getType();
1074
1075 // For the most part, we just need to load the alloca, except:
1076 // 1) aggregate r-values are actually pointers to temporaries, and
1077 // 2) references to aggregates are pointers directly to the aggregate.
1078 // I don't know why references to non-aggregates are different here.
1079 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1080 if (hasAggregateLLVMType(RefType->getPointeeType()))
1081 return RValue::getAggregate(Local);
1082
1083 // Locals which are references to scalars are represented
1084 // with allocas holding the pointer.
1085 return RValue::get(Builder.CreateLoad(Local));
1086 }
1087
1088 if (ArgType->isAnyComplexType())
1089 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1090
1091 if (hasAggregateLLVMType(ArgType))
1092 return RValue::getAggregate(Local);
1093
1094 return RValue::get(EmitLoadOfScalar(Local, false, ArgType));
1095}
1096
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001097RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson6f5a0152009-05-20 00:24:07 +00001098 if (ArgType->isReferenceType())
Anders Carlsson04775f82010-06-26 16:35:32 +00001099 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump11289f42009-09-09 15:08:12 +00001100
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001101 return EmitAnyExprToTemp(E);
1102}
1103
John McCallbd309292010-07-06 01:34:17 +00001104/// Emits a call or invoke instruction to the given function, depending
1105/// on the current state of the EH stack.
1106llvm::CallSite
1107CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1108 llvm::Value * const *ArgBegin,
1109 llvm::Value * const *ArgEnd,
1110 const llvm::Twine &Name) {
1111 llvm::BasicBlock *InvokeDest = getInvokeDest();
1112 if (!InvokeDest)
1113 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1114
1115 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1116 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1117 ArgBegin, ArgEnd, Name);
1118 EmitBlock(ContBB);
1119 return Invoke;
1120}
1121
Daniel Dunbard931a872009-02-02 22:03:45 +00001122RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001123 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00001124 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001125 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00001126 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00001127 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00001128 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00001129 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001130
1131 // Handle struct-return functions by passing a pointer to the
1132 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001133 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001134 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001135
1136
Chris Lattner4ca97c32009-06-13 00:26:38 +00001137 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00001138 // alloca to hold the result, unless one is given to us.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001139 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001140 llvm::Value *Value = ReturnValue.getValue();
1141 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +00001142 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +00001143 Args.push_back(Value);
1144 }
Mike Stump11289f42009-09-09 15:08:12 +00001145
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001146 assert(CallInfo.arg_size() == CallArgs.size() &&
1147 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001148 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001149 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001150 I != E; ++I, ++info_it) {
1151 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001152 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001153
1154 switch (ArgInfo.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001155 case ABIArgInfo::Indirect:
Daniel Dunbar747865a2009-02-05 09:16:39 +00001156 if (RV.isScalar() || RV.isComplex()) {
1157 // Make a temporary alloca to pass the argument.
Daniel Dunbara7566f12010-02-09 02:48:28 +00001158 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001159 if (RV.isScalar())
Anders Carlsson83709642009-05-19 18:50:41 +00001160 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001161 else
Mike Stump11289f42009-09-09 15:08:12 +00001162 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001163 } else {
1164 Args.push_back(RV.getAggregateAddr());
1165 }
1166 break;
1167
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001168 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001169 case ABIArgInfo::Direct:
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001170 if (RV.isScalar()) {
1171 Args.push_back(RV.getScalarVal());
1172 } else if (RV.isComplex()) {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001173 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1174 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1175 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1176 Args.push_back(Tmp);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001177 } else {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001178 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001179 }
1180 break;
Mike Stump11289f42009-09-09 15:08:12 +00001181
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001182 case ABIArgInfo::Ignore:
1183 break;
1184
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001185 case ABIArgInfo::Coerce: {
1186 // FIXME: Avoid the conversion through memory if possible.
1187 llvm::Value *SrcPtr;
1188 if (RV.isScalar()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001189 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlsson83709642009-05-19 18:50:41 +00001190 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001191 } else if (RV.isComplex()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001192 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001193 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001194 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001195 SrcPtr = RV.getAggregateAddr();
Chris Lattner3dd716c2010-06-28 23:44:11 +00001196
Chris Lattner3dd716c2010-06-28 23:44:11 +00001197 // If the coerce-to type is a first class aggregate, we flatten it and
1198 // pass the elements. Either way is semantically identical, but fast-isel
1199 // and the optimizer generally likes scalar values better than FCAs.
1200 if (const llvm::StructType *STy =
Chris Lattner15ec3612010-06-29 00:06:42 +00001201 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001202 SrcPtr = Builder.CreateBitCast(SrcPtr,
1203 llvm::PointerType::getUnqual(STy));
1204 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1205 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerff941a62010-07-28 18:24:28 +00001206 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1207 // We don't know what we're loading from.
1208 LI->setAlignment(1);
1209 Args.push_back(LI);
Chris Lattner15ec3612010-06-29 00:06:42 +00001210 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00001211 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00001212 // In the simple case, just pass the coerced loaded value.
1213 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1214 *this));
Chris Lattner3dd716c2010-06-28 23:44:11 +00001215 }
1216
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001217 break;
1218 }
1219
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001220 case ABIArgInfo::Expand:
1221 ExpandTypeToArgs(I->second, RV, Args);
1222 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001223 }
1224 }
Mike Stump11289f42009-09-09 15:08:12 +00001225
Chris Lattner4ca97c32009-06-13 00:26:38 +00001226 // If the callee is a bitcast of a function to a varargs pointer to function
1227 // type, check to see if we can remove the bitcast. This handles some cases
1228 // with unprototyped functions.
1229 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1230 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1231 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1232 const llvm::FunctionType *CurFT =
1233 cast<llvm::FunctionType>(CurPT->getElementType());
1234 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001235
Chris Lattner4ca97c32009-06-13 00:26:38 +00001236 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1237 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00001238 ActualFT->getNumParams() == CurFT->getNumParams() &&
1239 ActualFT->getNumParams() == Args.size()) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00001240 bool ArgsMatch = true;
1241 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1242 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1243 ArgsMatch = false;
1244 break;
1245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattner4ca97c32009-06-13 00:26:38 +00001247 // Strip the cast if we can get away with it. This is a nice cleanup,
1248 // but also allows us to inline the function at -O0 if it is marked
1249 // always_inline.
1250 if (ArgsMatch)
1251 Callee = CalleeF;
1252 }
1253 }
Mike Stump11289f42009-09-09 15:08:12 +00001254
Daniel Dunbar613855c2008-09-09 23:27:19 +00001255
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001256 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00001257 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001258 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +00001259 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1260 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +00001261
John McCallbd309292010-07-06 01:34:17 +00001262 llvm::BasicBlock *InvokeDest = 0;
1263 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1264 InvokeDest = getInvokeDest();
1265
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001266 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00001267 if (!InvokeDest) {
Jay Foad7d0479f2009-05-21 09:52:38 +00001268 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001269 } else {
1270 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +00001271 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +00001272 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001273 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001274 }
Chris Lattnere70a0072010-06-29 16:40:28 +00001275 if (callOrInvoke)
David Chisnallff5f88c2010-05-02 13:41:58 +00001276 *callOrInvoke = CS.getInstruction();
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001277
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001278 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001279 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001280
1281 // If the call doesn't return, finish the basic block and clear the
1282 // insertion point; this allows the rest of IRgen to discard
1283 // unreachable code.
1284 if (CS.doesNotReturn()) {
1285 Builder.CreateUnreachable();
1286 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001287
Mike Stump18bb9282009-05-16 07:57:57 +00001288 // FIXME: For now, emit a dummy basic block because expr emitters in
1289 // generally are not ready to handle emitting expressions at unreachable
1290 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001291 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001292
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001293 // Return a reasonable RValue.
1294 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001295 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001296
1297 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001298 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001299 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001300
1301 switch (RetAI.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001302 case ABIArgInfo::Indirect:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001303 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001304 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001305 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001306 return RValue::getAggregate(Args[0]);
Chris Lattnere09ad902009-03-22 00:32:22 +00001307 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +00001308
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001309 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001310 case ABIArgInfo::Direct:
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001311 if (RetTy->isAnyComplexType()) {
1312 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1313 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1314 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattnere09ad902009-03-22 00:32:22 +00001315 }
1316 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001317 llvm::Value *DestPtr = ReturnValue.getValue();
1318 bool DestIsVolatile = ReturnValue.isVolatile();
1319
1320 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001321 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlsson17490832009-12-24 20:40:36 +00001322 DestIsVolatile = false;
1323 }
1324 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1325 return RValue::getAggregate(DestPtr);
Chris Lattnere09ad902009-03-22 00:32:22 +00001326 }
1327 return RValue::get(CI);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001328
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001329 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001330 // If we are ignoring an argument that had a result, make sure to
1331 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001332 return GetUndefRValue(RetTy);
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001333
Daniel Dunbar573884e2008-09-10 07:04:09 +00001334 case ABIArgInfo::Coerce: {
Anders Carlsson17490832009-12-24 20:40:36 +00001335 llvm::Value *DestPtr = ReturnValue.getValue();
1336 bool DestIsVolatile = ReturnValue.isVolatile();
1337
1338 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001339 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001340 DestIsVolatile = false;
1341 }
1342
1343 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001344 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001345 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001346 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001347 return RValue::getAggregate(DestPtr);
1348 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001349 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001350
Daniel Dunbard3674e62008-09-11 01:48:57 +00001351 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001352 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001353 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001354
1355 assert(0 && "Unhandled ABIArgInfo::Kind");
1356 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001357}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001358
1359/* VarArg handling */
1360
1361llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1362 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1363}