blob: 05c11ceac62aaa2b4f74a5f0d46e9a2db3b9fc49 [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"
Eli Friedmanf7456192011-06-15 22:09:18 +000028#include "llvm/Transforms/Utils/Local.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
John McCallab26cfa2010-02-05 21:31:56 +000034static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
35 switch (CC) {
36 default: return llvm::CallingConv::C;
37 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
38 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregora941dca2010-05-18 16:57:00 +000039 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
Anton Korobeynikov231e8752011-04-14 20:06:49 +000040 case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
41 case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Dawn Perchik335e16b2010-09-03 01:29:35 +000042 // TODO: add support for CC_X86Pascal to llvm
John McCallab26cfa2010-02-05 21:31:56 +000043 }
44}
45
John McCall8ee376f2010-02-24 07:14:12 +000046/// Derives the 'this' type for codegen purposes, i.e. ignoring method
47/// qualification.
48/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000049static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
50 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
51 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000052}
53
John McCall8ee376f2010-02-24 07:14:12 +000054/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000055static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
56 return MD->getType()->getCanonicalTypeUnqualified()
57 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000058}
59
60/// Returns the "extra-canonicalized" return type, which discards
61/// qualifiers on the return type. Codegen doesn't care about them,
62/// and it makes ABI code a little easier to be able to assume that
63/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000064static CanQualType GetReturnType(QualType RetTy) {
65 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000066}
67
68const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000069CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
70 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000071 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
72 llvm::SmallVector<CanQualType, 16>(),
Chris Lattner5c740f12010-06-30 19:14:05 +000073 FTNP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000074}
75
76/// \param Args - contains any initial parameters besides those
77/// in the formal type
78static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCall2da83a32010-02-26 00:48:12 +000079 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +000080 CanQual<FunctionProtoType> FTP,
81 bool IsRecursive = false) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000082 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000083 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000084 ArgTys.push_back(FTP->getArgType(i));
John McCall2da83a32010-02-26 00:48:12 +000085 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Tilmann Scheller99cc30c2011-03-02 21:36:49 +000086 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000087}
88
89const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000090CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
91 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000092 llvm::SmallVector<CanQualType, 16> ArgTys;
Tilmann Scheller99cc30c2011-03-02 21:36:49 +000093 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbar7feafc72009-09-11 22:24:53 +000094}
95
John McCallab26cfa2010-02-05 21:31:56 +000096static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000097 // Set the appropriate calling convention for the Function.
98 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000099 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000100
101 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +0000102 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000103
Douglas Gregora941dca2010-05-18 16:57:00 +0000104 if (D->hasAttr<ThisCallAttr>())
105 return CC_X86ThisCall;
106
Dawn Perchik335e16b2010-09-03 01:29:35 +0000107 if (D->hasAttr<PascalAttr>())
108 return CC_X86Pascal;
109
Anton Korobeynikov231e8752011-04-14 20:06:49 +0000110 if (PcsAttr *PCS = D->getAttr<PcsAttr>())
111 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
112
John McCallab26cfa2010-02-05 21:31:56 +0000113 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000114}
115
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000116const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
117 const FunctionProtoType *FTP) {
John McCall2da83a32010-02-26 00:48:12 +0000118 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000119
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000120 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000121 ArgTys.push_back(GetThisType(Context, RD));
122
123 return ::getFunctionInfo(*this, ArgTys,
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000124 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000125}
126
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000127const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000128 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000129
John McCall0d635f52010-09-03 01:26:39 +0000130 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
131 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
132
Chris Lattnerbea5b622009-05-12 20:27:19 +0000133 // Add the 'this' pointer unless this is a static method.
134 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000135 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000136
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000137 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000138}
139
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000140const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000141 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000142 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000143 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000144 CanQualType ResTy = Context.VoidTy;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000145
John McCall5d865c322010-08-31 07:33:07 +0000146 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall8ee376f2010-02-24 07:14:12 +0000147
John McCall5d865c322010-08-31 07:33:07 +0000148 CanQual<FunctionProtoType> FTP = GetFormalType(D);
149
150 // Add the formal parameters.
151 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
152 ArgTys.push_back(FTP->getArgType(i));
153
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000154 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000155}
156
157const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
158 CXXDtorType Type) {
John McCall5d865c322010-08-31 07:33:07 +0000159 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCall2da83a32010-02-26 00:48:12 +0000160 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000161 CanQualType ResTy = Context.VoidTy;
John McCall8ee376f2010-02-24 07:14:12 +0000162
John McCall5d865c322010-08-31 07:33:07 +0000163 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
164
165 CanQual<FunctionProtoType> FTP = GetFormalType(D);
166 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
167
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000168 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000169}
170
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000171const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000172 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000173 if (MD->isInstance())
174 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000175
John McCall2da83a32010-02-26 00:48:12 +0000176 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
177 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000178 if (isa<FunctionNoProtoType>(FTy))
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000179 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
John McCall2da83a32010-02-26 00:48:12 +0000180 assert(isa<FunctionProtoType>(FTy));
181 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000182}
183
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000184const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000185 llvm::SmallVector<CanQualType, 16> ArgTys;
186 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
187 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000188 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000189 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000190 e = MD->param_end(); i != e; ++i) {
191 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
192 }
193 return getFunctionInfo(GetReturnType(MD->getResultType()),
194 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000195 FunctionType::ExtInfo(
196 /*NoReturn*/ false,
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000197 /*HasRegParm*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000198 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000199 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000200}
201
Anders Carlsson6710c532010-02-06 02:44:09 +0000202const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
203 // FIXME: Do we need to handle ObjCMethodDecl?
204 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000205
Anders Carlsson6710c532010-02-06 02:44:09 +0000206 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
207 return getFunctionInfo(CD, GD.getCtorType());
208
209 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
210 return getFunctionInfo(DD, GD.getDtorType());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000211
Anders Carlsson6710c532010-02-06 02:44:09 +0000212 return getFunctionInfo(FD);
213}
214
Mike Stump11289f42009-09-09 15:08:12 +0000215const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000216 const CallArgList &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 (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000221 i != e; ++i)
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000222 ArgTys.push_back(Context.getCanonicalParamType(i->Ty));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000223 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000224}
225
Mike Stump11289f42009-09-09 15:08:12 +0000226const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000227 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000228 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000229 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000230 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000231 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000232 i != e; ++i)
John McCalla738c252011-03-09 04:27:21 +0000233 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000234 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000235}
236
John McCalla738c252011-03-09 04:27:21 +0000237const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
238 llvm::SmallVector<CanQualType, 1> args;
239 return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
240}
241
John McCall2da83a32010-02-26 00:48:12 +0000242const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
243 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +0000244 const FunctionType::ExtInfo &Info,
245 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +0000246#ifndef NDEBUG
247 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
248 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
249 assert(I->isCanonicalAsParam());
250#endif
251
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000252 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000253
Daniel Dunbare0be8292009-02-03 00:07:12 +0000254 // Lookup or create unique function info.
255 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000256 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000257 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000258
259 void *InsertPos = 0;
260 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
261 if (FI)
262 return *FI;
263
Daniel Dunbar313321e2009-02-03 05:31:23 +0000264 // Construct the function info.
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000265 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getHasRegParm(), Info.getRegParm(), ResTy,
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000266 ArgTys.data(), ArgTys.size());
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000267 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000268
269 // Compute ABI information.
Chris Lattner22326a12010-07-29 02:31:05 +0000270 getABIInfo().computeInfo(*FI);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000271
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000272 // Loop over all of the computed argument and return value info. If any of
273 // them are direct or extend without a specified coerce type, specify the
274 // default now.
275 ABIArgInfo &RetInfo = FI->getReturnInfo();
276 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
277 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000278
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000279 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
280 I != E; ++I)
281 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
282 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar313321e2009-02-03 05:31:23 +0000283
Chris Lattner0e7929f2010-07-01 06:20:47 +0000284 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
285 // types, resolve them now. These pointers may point to this function, which
286 // we *just* filled in the FunctionInfo for.
Chris Lattner22326a12010-07-29 02:31:05 +0000287 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattner0e7929f2010-07-01 06:20:47 +0000288 HandleLateResolvedPointers();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000289
Daniel Dunbare0be8292009-02-03 00:07:12 +0000290 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000291}
292
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000293CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000294 bool _NoReturn, bool _HasRegParm, unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000295 CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000296 const CanQualType *ArgTys,
297 unsigned NumArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000298 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000299 EffectiveCallingConvention(_CallingConvention),
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000300 NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000301{
Chris Lattner34d62812010-06-29 18:13:52 +0000302 NumArgs = NumArgTys;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000303
Chris Lattner3dd716c2010-06-28 23:44:11 +0000304 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattner34d62812010-06-29 18:13:52 +0000305 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar313321e2009-02-03 05:31:23 +0000306 Args[0].type = ResTy;
Chris Lattner34d62812010-06-29 18:13:52 +0000307 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar313321e2009-02-03 05:31:23 +0000308 Args[1 + i].type = ArgTys[i];
309}
310
311/***/
312
John McCall85dd2c52011-05-15 02:19:42 +0000313void CodeGenTypes::GetExpandedTypes(QualType type,
314 llvm::SmallVectorImpl<const llvm::Type*> &expandedTypes,
315 bool isRecursive) {
316 const RecordType *RT = type->getAsStructureType();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000317 assert(RT && "Can only expand structure types.");
318 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000319 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000320 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000321
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000322 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
323 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000324 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000325 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000326 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000327
John McCall85dd2c52011-05-15 02:19:42 +0000328 QualType fieldType = FD->getType();
329 if (fieldType->isRecordType())
330 GetExpandedTypes(fieldType, expandedTypes, isRecursive);
Chris Lattnerff941a62010-07-28 18:24:28 +0000331 else
John McCall85dd2c52011-05-15 02:19:42 +0000332 expandedTypes.push_back(ConvertType(fieldType, isRecursive));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000333 }
334}
335
Mike Stump11289f42009-09-09 15:08:12 +0000336llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000337CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
338 llvm::Function::arg_iterator AI) {
339 const RecordType *RT = Ty->getAsStructureType();
340 assert(RT && "Can only expand structure types.");
341
342 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000343 assert(LV.isSimple() &&
344 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000345 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000346 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
347 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000348 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000349 QualType FT = FD->getType();
350
351 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000352 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000353 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
354 AI = ExpandTypeFromArgs(FT, LV, AI);
355 } else {
356 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
357 ++AI;
358 }
359 }
360
361 return AI;
362}
363
Mike Stump11289f42009-09-09 15:08:12 +0000364void
365CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000366 llvm::SmallVector<llvm::Value*, 16> &Args) {
367 const RecordType *RT = Ty->getAsStructureType();
368 assert(RT && "Can only expand structure types.");
369
370 RecordDecl *RD = RT->getDecl();
371 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
372 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000373 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
374 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000375 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000376 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000377
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000378 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000379 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000380 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
381 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
382 } else {
383 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000384 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000385 "Unexpected non-scalar rvalue during struct expansion.");
386 Args.push_back(RV.getScalarVal());
387 }
388 }
389}
390
Chris Lattner895c52b2010-06-27 06:04:18 +0000391/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000392/// accessing some number of bytes out of it, try to gep into the struct to get
393/// at its inner goodness. Dive as deep as possible without entering an element
394/// with an in-memory size smaller than DstSize.
395static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000396EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
397 const llvm::StructType *SrcSTy,
398 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000399 // We can't dive into a zero-element struct.
400 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000401
Chris Lattner1cd66982010-06-27 05:56:15 +0000402 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000403
Chris Lattner1cd66982010-06-27 05:56:15 +0000404 // If the first elt is at least as large as what we're looking for, or if the
405 // first element is the same size as the whole struct, we can enter it.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000406 uint64_t FirstEltSize =
Chris Lattner1cd66982010-06-27 05:56:15 +0000407 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000408 if (FirstEltSize < DstSize &&
Chris Lattner1cd66982010-06-27 05:56:15 +0000409 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
410 return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000411
Chris Lattner1cd66982010-06-27 05:56:15 +0000412 // GEP into the first element.
413 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000414
Chris Lattner1cd66982010-06-27 05:56:15 +0000415 // If the first element is a struct, recurse.
416 const llvm::Type *SrcTy =
417 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
418 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000419 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000420
421 return SrcPtr;
422}
423
Chris Lattner055097f2010-06-27 06:26:04 +0000424/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
425/// are either integers or pointers. This does a truncation of the value if it
426/// is too large or a zero extension if it is too small.
427static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
428 const llvm::Type *Ty,
429 CodeGenFunction &CGF) {
430 if (Val->getType() == Ty)
431 return Val;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000432
Chris Lattner055097f2010-06-27 06:26:04 +0000433 if (isa<llvm::PointerType>(Val->getType())) {
434 // If this is Pointer->Pointer avoid conversion to and from int.
435 if (isa<llvm::PointerType>(Ty))
436 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000437
Chris Lattner055097f2010-06-27 06:26:04 +0000438 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +0000439 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +0000440 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000441
Chris Lattner055097f2010-06-27 06:26:04 +0000442 const llvm::Type *DestIntTy = Ty;
443 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +0000444 DestIntTy = CGF.IntPtrTy;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000445
Chris Lattner055097f2010-06-27 06:26:04 +0000446 if (Val->getType() != DestIntTy)
447 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000448
Chris Lattner055097f2010-06-27 06:26:04 +0000449 if (isa<llvm::PointerType>(Ty))
450 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
451 return Val;
452}
453
Chris Lattner1cd66982010-06-27 05:56:15 +0000454
455
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000456/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
457/// a pointer to an object of type \arg Ty.
458///
459/// This safely handles the case when the src type is smaller than the
460/// destination type; in this situation the values of bits which not
461/// present in the src are undefined.
462static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
463 const llvm::Type *Ty,
464 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000465 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000466 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000467
Chris Lattnerd200eda2010-06-28 22:51:39 +0000468 // If SrcTy and Ty are the same, just do a load.
469 if (SrcTy == Ty)
470 return CGF.Builder.CreateLoad(SrcPtr);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000471
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000472 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000473
Chris Lattner1cd66982010-06-27 05:56:15 +0000474 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000475 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000476 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
477 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000478
Chris Lattner1cd66982010-06-27 05:56:15 +0000479 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000480
Chris Lattner055097f2010-06-27 06:26:04 +0000481 // If the source and destination are integer or pointer types, just do an
482 // extension or truncation to the desired type.
483 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
484 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
485 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
486 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
487 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000488
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000489 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000490 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000491 // Generally SrcSize is never greater than DstSize, since this means we are
492 // losing bits. However, this can happen in cases where the structure has
493 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000494 //
Mike Stump18bb9282009-05-16 07:57:57 +0000495 // FIXME: Assert that we aren't truncating non-padding bits when have access
496 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000497 llvm::Value *Casted =
498 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000499 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
500 // FIXME: Use better alignment / avoid requiring aligned load.
501 Load->setAlignment(1);
502 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000503 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000504
Chris Lattner3fcc7902010-06-27 01:06:27 +0000505 // Otherwise do coercion through memory. This is stupid, but
506 // simple.
507 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
508 llvm::Value *Casted =
509 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
510 llvm::StoreInst *Store =
511 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
512 // FIXME: Use better alignment / avoid requiring aligned store.
513 Store->setAlignment(1);
514 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000515}
516
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000517// Function to store a first-class aggregate into memory. We prefer to
518// store the elements rather than the aggregate to be more friendly to
519// fast-isel.
520// FIXME: Do we need to recurse here?
521static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
522 llvm::Value *DestPtr, bool DestIsVolatile,
523 bool LowAlignment) {
524 // Prefer scalar stores to first-class aggregate stores.
525 if (const llvm::StructType *STy =
526 dyn_cast<llvm::StructType>(Val->getType())) {
527 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
528 llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
529 llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
530 llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
531 DestIsVolatile);
532 if (LowAlignment)
533 SI->setAlignment(1);
534 }
535 } else {
536 CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
537 }
538}
539
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000540/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
541/// where the source and destination may have different types.
542///
543/// This safely handles the case when the src type is larger than the
544/// destination type; the upper bits of the src will be lost.
545static void CreateCoercedStore(llvm::Value *Src,
546 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000547 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000548 CodeGenFunction &CGF) {
549 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000550 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000551 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000552 if (SrcTy == DstTy) {
553 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
554 return;
555 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000556
Chris Lattnerd200eda2010-06-28 22:51:39 +0000557 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000558
Chris Lattner895c52b2010-06-27 06:04:18 +0000559 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
560 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
561 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
562 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000563
Chris Lattner055097f2010-06-27 06:26:04 +0000564 // If the source and destination are integer or pointer types, just do an
565 // extension or truncation to the desired type.
566 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
567 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
568 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
569 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
570 return;
571 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000572
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000573 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000574
Daniel Dunbar313321e2009-02-03 05:31:23 +0000575 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000576 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000577 llvm::Value *Casted =
578 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000579 // FIXME: Use better alignment / avoid requiring aligned store.
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000580 BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000581 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000582 // Otherwise do coercion through memory. This is stupid, but
583 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000584
585 // Generally SrcSize is never greater than DstSize, since this means we are
586 // losing bits. However, this can happen in cases where the structure has
587 // additional padding, for example due to a user specified alignment.
588 //
589 // FIXME: Assert that we aren't truncating non-padding bits when have access
590 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000591 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
592 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000593 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000594 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000595 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
596 // FIXME: Use better alignment / avoid requiring aligned load.
597 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000598 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000599 }
600}
601
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000602/***/
603
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000604bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000605 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000606}
607
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000608bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
609 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
610 switch (BT->getKind()) {
611 default:
612 return false;
613 case BuiltinType::Float:
614 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
615 case BuiltinType::Double:
616 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
617 case BuiltinType::LongDouble:
618 return getContext().Target.useObjCFPRetForRealType(
619 TargetInfo::LongDouble);
620 }
621 }
622
623 return false;
624}
625
John McCallf8ff7b92010-02-23 00:48:20 +0000626const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
627 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000628
John McCallf8ff7b92010-02-23 00:48:20 +0000629 // For definition purposes, don't consider a K&R function variadic.
630 bool Variadic = false;
631 if (const FunctionProtoType *FPT =
632 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
633 Variadic = FPT->isVariadic();
634
Chris Lattner5c740f12010-06-30 19:14:05 +0000635 return GetFunctionType(FI, Variadic, false);
John McCallf8ff7b92010-02-23 00:48:20 +0000636}
637
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000638const llvm::FunctionType *
John McCall85dd2c52011-05-15 02:19:42 +0000639CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic,
640 bool isRecursive) {
641 llvm::SmallVector<const llvm::Type*, 8> argTypes;
642 const llvm::Type *resultType = 0;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000643
John McCall85dd2c52011-05-15 02:19:42 +0000644 const ABIArgInfo &retAI = FI.getReturnInfo();
645 switch (retAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000646 case ABIArgInfo::Expand:
John McCall85dd2c52011-05-15 02:19:42 +0000647 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbard3674e62008-09-11 01:48:57 +0000648
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000649 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000650 case ABIArgInfo::Direct:
John McCall85dd2c52011-05-15 02:19:42 +0000651 resultType = retAI.getCoerceToType();
Daniel Dunbar67dace892009-02-03 06:17:37 +0000652 break;
653
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000654 case ABIArgInfo::Indirect: {
John McCall85dd2c52011-05-15 02:19:42 +0000655 assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
656 resultType = llvm::Type::getVoidTy(getLLVMContext());
657
658 QualType ret = FI.getReturnType();
659 const llvm::Type *ty = ConvertType(ret, isRecursive);
660 unsigned addressSpace = Context.getTargetAddressSpace(ret);
661 argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000662 break;
663 }
664
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000665 case ABIArgInfo::Ignore:
John McCall85dd2c52011-05-15 02:19:42 +0000666 resultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000667 break;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
670 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000671 ie = FI.arg_end(); it != ie; ++it) {
John McCall85dd2c52011-05-15 02:19:42 +0000672 const ABIArgInfo &argAI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000673
John McCall85dd2c52011-05-15 02:19:42 +0000674 switch (argAI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000675 case ABIArgInfo::Ignore:
676 break;
677
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000678 case ABIArgInfo::Indirect: {
679 // indirect arguments are always on the stack, which is addr space #0.
John McCall85dd2c52011-05-15 02:19:42 +0000680 const llvm::Type *LTy = ConvertTypeForMem(it->type, isRecursive);
681 argTypes.push_back(LTy->getPointerTo());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000682 break;
683 }
684
685 case ABIArgInfo::Extend:
Chris Lattner2cdfda42010-07-29 06:44:09 +0000686 case ABIArgInfo::Direct: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000687 // If the coerce-to type is a first class aggregate, flatten it. Either
688 // way is semantically identical, but fast-isel and the optimizer
689 // generally likes scalar values better than FCAs.
John McCall85dd2c52011-05-15 02:19:42 +0000690 const llvm::Type *argType = argAI.getCoerceToType();
691 if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
692 for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
693 argTypes.push_back(st->getElementType(i));
Chris Lattner3dd716c2010-06-28 23:44:11 +0000694 } else {
John McCall85dd2c52011-05-15 02:19:42 +0000695 argTypes.push_back(argType);
Chris Lattner3dd716c2010-06-28 23:44:11 +0000696 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000697 break;
Chris Lattner2cdfda42010-07-29 06:44:09 +0000698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Daniel Dunbard3674e62008-09-11 01:48:57 +0000700 case ABIArgInfo::Expand:
John McCall85dd2c52011-05-15 02:19:42 +0000701 GetExpandedTypes(it->type, argTypes, isRecursive);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000702 break;
703 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000704 }
705
John McCall85dd2c52011-05-15 02:19:42 +0000706 return llvm::FunctionType::get(resultType, argTypes, isVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000707}
708
John McCall5d865c322010-08-31 07:33:07 +0000709const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
710 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson64457732009-11-24 05:08:52 +0000711 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000712
John McCall5d865c322010-08-31 07:33:07 +0000713 if (!VerifyFuncTypeComplete(FPT)) {
714 const CGFunctionInfo *Info;
715 if (isa<CXXDestructorDecl>(MD))
716 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
717 else
718 Info = &getFunctionInfo(MD);
719 return GetFunctionType(*Info, FPT->isVariadic(), false);
720 }
Anders Carlsson64457732009-11-24 05:08:52 +0000721
722 return llvm::OpaqueType::get(getLLVMContext());
723}
724
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000725void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000726 const Decl *TargetDecl,
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000727 AttributeListType &PAL,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000728 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000729 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000730 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000731
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000732 CallingConv = FI.getEffectiveCallingConvention();
733
John McCallab26cfa2010-02-05 21:31:56 +0000734 if (FI.isNoReturn())
735 FuncAttrs |= llvm::Attribute::NoReturn;
736
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000737 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000738 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000739 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000740 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCallbe349de2010-07-08 06:48:12 +0000741 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
742 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl31ad7542011-03-13 17:09:40 +0000743 if (FPT && FPT->isNothrow(getContext()))
John McCallbe349de2010-07-08 06:48:12 +0000744 FuncAttrs |= llvm::Attribute::NoUnwind;
745 }
746
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000747 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000748 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000749 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000750 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000751 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000752 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000753 if (TargetDecl->hasAttr<MallocAttr>())
754 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000755 }
756
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000757 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000758 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000759 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000760 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000761 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000762 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000763
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000764 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000765 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000766 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000767 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000768 case ABIArgInfo::Extend:
Chris Lattner4b8585e2010-07-28 23:46:15 +0000769 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000770 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner4b8585e2010-07-28 23:46:15 +0000771 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000772 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000773 break;
Daniel Dunbar67dace892009-02-03 06:17:37 +0000774 case ABIArgInfo::Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000775 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000776 break;
777
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000778 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000779 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000780 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000781 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000782 // sret disables readnone and readonly
783 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
784 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000785 break;
786
Daniel Dunbard3674e62008-09-11 01:48:57 +0000787 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000788 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000789 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000790
Devang Patel597e7082008-09-26 22:53:57 +0000791 if (RetAttrs)
792 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000793
Daniel Dunbar0bb03312011-02-09 17:54:19 +0000794 // FIXME: RegParm should be reduced in case of global register variable.
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000795 signed RegParm;
796 if (FI.getHasRegParm())
797 RegParm = FI.getRegParm();
798 else
Daniel Dunbar0bb03312011-02-09 17:54:19 +0000799 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000800
801 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000802 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000803 ie = FI.arg_end(); it != ie; ++it) {
804 QualType ParamType = it->type;
805 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000806 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000807
John McCall39ec71f2010-03-27 00:47:27 +0000808 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
809 // have the corresponding parameter variable. It doesn't make
Daniel Dunbarcb2b3d02011-02-10 18:10:07 +0000810 // sense to do it here because parameters are so messed up.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000811 switch (AI.getKind()) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000812 case ABIArgInfo::Extend:
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000813 if (ParamType->isSignedIntegerOrEnumerationType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000814 Attributes |= llvm::Attribute::SExt;
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000815 else if (ParamType->isUnsignedIntegerOrEnumerationType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000816 Attributes |= llvm::Attribute::ZExt;
817 // FALL THROUGH
818 case ABIArgInfo::Direct:
819 if (RegParm > 0 &&
820 (ParamType->isIntegerType() || ParamType->isPointerType())) {
821 RegParm -=
822 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
823 if (RegParm >= 0)
824 Attributes |= llvm::Attribute::InReg;
825 }
826 // FIXME: handle sseregparm someday...
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000827
Chris Lattner3dd716c2010-06-28 23:44:11 +0000828 if (const llvm::StructType *STy =
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000829 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
830 Index += STy->getNumElements()-1; // 1 will be added below.
831 break;
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000832
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000833 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000834 if (AI.getIndirectByVal())
835 Attributes |= llvm::Attribute::ByVal;
836
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000837 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000838 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000839 // byval disables readnone and readonly.
840 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
841 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000842 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000843
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000844 case ABIArgInfo::Ignore:
845 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000846 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000847
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000848 case ABIArgInfo::Expand: {
John McCall85dd2c52011-05-15 02:19:42 +0000849 llvm::SmallVector<const llvm::Type*, 8> types;
Mike Stump18bb9282009-05-16 07:57:57 +0000850 // FIXME: This is rather inefficient. Do we ever actually need to do
851 // anything here? The result should be just reconstructed on the other
852 // side, so extension should be a non-issue.
John McCall85dd2c52011-05-15 02:19:42 +0000853 getTypes().GetExpandedTypes(ParamType, types, false);
854 Index += types.size();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000855 continue;
856 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000857 }
Mike Stump11289f42009-09-09 15:08:12 +0000858
Devang Patel322300d2008-09-25 21:02:23 +0000859 if (Attributes)
860 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000861 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000862 }
Devang Patel597e7082008-09-26 22:53:57 +0000863 if (FuncAttrs)
864 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000865}
866
John McCalla738c252011-03-09 04:27:21 +0000867/// An argument came in as a promoted argument; demote it back to its
868/// declared type.
869static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
870 const VarDecl *var,
871 llvm::Value *value) {
872 const llvm::Type *varType = CGF.ConvertType(var->getType());
873
874 // This can happen with promotions that actually don't change the
875 // underlying type, like the enum promotions.
876 if (value->getType() == varType) return value;
877
878 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
879 && "unexpected promotion type");
880
881 if (isa<llvm::IntegerType>(varType))
882 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
883
884 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
885}
886
Daniel Dunbard931a872009-02-02 22:03:45 +0000887void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
888 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000889 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000890 // If this is an implicit-return-zero function, go ahead and
891 // initialize the return value. TODO: it might be nice to have
892 // a more general mechanism for this that didn't require synthesized
893 // return statements.
Chris Lattnerc401de92010-07-05 20:21:00 +0000894 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000895 if (FD->hasImplicitReturnZero()) {
896 QualType RetTy = FD->getResultType().getUnqualifiedType();
897 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000898 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000899 Builder.CreateStore(Zero, ReturnValue);
900 }
901 }
902
Mike Stump18bb9282009-05-16 07:57:57 +0000903 // FIXME: We no longer need the types from FunctionArgList; lift up and
904 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000905
Daniel Dunbar613855c2008-09-09 23:27:19 +0000906 // Emit allocs for param decls. Give the LLVM Argument nodes names.
907 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000908
Daniel Dunbar613855c2008-09-09 23:27:19 +0000909 // Name the struct return argument.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000910 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000911 AI->setName("agg.result");
912 ++AI;
913 }
Mike Stump11289f42009-09-09 15:08:12 +0000914
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000915 assert(FI.arg_size() == Args.size() &&
916 "Mismatch between function signature & arguments.");
Devang Patel68a15252011-03-03 20:13:15 +0000917 unsigned ArgNo = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000918 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel68a15252011-03-03 20:13:15 +0000919 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
920 i != e; ++i, ++info_it, ++ArgNo) {
John McCalla738c252011-03-09 04:27:21 +0000921 const VarDecl *Arg = *i;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000922 QualType Ty = info_it->type;
923 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000924
John McCalla738c252011-03-09 04:27:21 +0000925 bool isPromoted =
926 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
927
Daniel Dunbard3674e62008-09-11 01:48:57 +0000928 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000929 case ABIArgInfo::Indirect: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000930 llvm::Value *V = AI;
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000931
Daniel Dunbar747865a2009-02-05 09:16:39 +0000932 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000933 // Aggregates and complex variables are accessed by reference. All we
934 // need to do is realign the value, if requested
935 if (ArgI.getIndirectRealign()) {
936 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
937
938 // Copy from the incoming argument pointer to the temporary with the
939 // appropriate alignment.
940 //
941 // FIXME: We should have a common utility for generating an aggregate
942 // copy.
Benjamin Krameracc6b4e2010-12-30 00:13:21 +0000943 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyck705ba072011-01-19 01:58:38 +0000944 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumidd634362011-03-10 14:02:21 +0000945 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
946 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
947 Builder.CreateMemCpy(Dst,
948 Src,
Ken Dyck705ba072011-01-19 01:58:38 +0000949 llvm::ConstantInt::get(IntPtrTy,
950 Size.getQuantity()),
Benjamin Krameracc6b4e2010-12-30 00:13:21 +0000951 ArgI.getIndirectAlign(),
952 false);
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000953 V = AlignedTemp;
954 }
Daniel Dunbar747865a2009-02-05 09:16:39 +0000955 } else {
956 // Load scalar value from indirect argument.
Ken Dyck705ba072011-01-19 01:58:38 +0000957 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
958 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
John McCalla738c252011-03-09 04:27:21 +0000959
960 if (isPromoted)
961 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000962 }
Devang Patel68a15252011-03-03 20:13:15 +0000963 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000964 break;
965 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000966
967 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000968 case ABIArgInfo::Direct: {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000969 // If we have the trivial case, handle it with no muss and fuss.
970 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000971 ArgI.getCoerceToType() == ConvertType(Ty) &&
972 ArgI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000973 assert(AI != Fn->arg_end() && "Argument mismatch!");
974 llvm::Value *V = AI;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000975
John McCall39ec71f2010-03-27 00:47:27 +0000976 if (Arg->getType().isRestrictQualified())
977 AI->addAttr(llvm::Attribute::NoAlias);
978
John McCalla738c252011-03-09 04:27:21 +0000979 if (isPromoted)
980 V = emitArgumentDemotion(*this, Arg, V);
981
Devang Patel68a15252011-03-03 20:13:15 +0000982 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000983 break;
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000984 }
Mike Stump11289f42009-09-09 15:08:12 +0000985
Chris Lattnerc401de92010-07-05 20:21:00 +0000986 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000987
Chris Lattnerff941a62010-07-28 18:24:28 +0000988 // The alignment we need to use is the max of the requested alignment for
989 // the argument plus the alignment required by our access code below.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000990 unsigned AlignmentToUse =
John McCallad7c5c12011-02-08 08:22:06 +0000991 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerff941a62010-07-28 18:24:28 +0000992 AlignmentToUse = std::max(AlignmentToUse,
993 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000994
Chris Lattnerff941a62010-07-28 18:24:28 +0000995 Alloca->setAlignment(AlignmentToUse);
Chris Lattnerc401de92010-07-05 20:21:00 +0000996 llvm::Value *V = Alloca;
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000997 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000998
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000999 // If the value is offset in memory, apply the offset now.
1000 if (unsigned Offs = ArgI.getDirectOffset()) {
1001 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1002 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001003 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001004 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1005 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001006
Chris Lattner15ec3612010-06-29 00:06:42 +00001007 // If the coerce-to type is a first class aggregate, we flatten it and
1008 // pass the elements. Either way is semantically identical, but fast-isel
1009 // and the optimizer generally likes scalar values better than FCAs.
1010 if (const llvm::StructType *STy =
1011 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001012 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001013
Chris Lattnerceddafb2010-07-05 20:41:41 +00001014 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1015 assert(AI != Fn->arg_end() && "Argument mismatch!");
1016 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
1017 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1018 Builder.CreateStore(AI++, EltPtr);
Chris Lattner15ec3612010-06-29 00:06:42 +00001019 }
1020 } else {
1021 // Simple case, just do a coerced store of the argument into the alloca.
1022 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner9e748e92010-06-29 00:14:52 +00001023 AI->setName(Arg->getName() + ".coerce");
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001024 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner15ec3612010-06-29 00:06:42 +00001025 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001026
1027
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001028 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +00001029 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001030 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
John McCalla738c252011-03-09 04:27:21 +00001031 if (isPromoted)
1032 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +00001033 }
Devang Patel68a15252011-03-03 20:13:15 +00001034 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner3dd716c2010-06-28 23:44:11 +00001035 continue; // Skip ++AI increment, already done.
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001036 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001037
1038 case ABIArgInfo::Expand: {
1039 // If this structure was expanded into multiple arguments then
1040 // we need to create a temporary and reconstruct it from the
1041 // arguments.
1042 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001043 llvm::Function::arg_iterator End =
Daniel Dunbar2e442a02010-08-21 03:15:20 +00001044 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel68a15252011-03-03 20:13:15 +00001045 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001046
1047 // Name the arguments used in expansion and increment AI.
1048 unsigned Index = 0;
1049 for (; AI != End; ++AI, ++Index)
1050 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1051 continue;
1052 }
1053
1054 case ABIArgInfo::Ignore:
1055 // Initialize the local variable appropriately.
1056 if (hasAggregateLLVMType(Ty))
Devang Patel68a15252011-03-03 20:13:15 +00001057 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001058 else
Devang Patel68a15252011-03-03 20:13:15 +00001059 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1060 ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001061
1062 // Skip increment, no matching LLVM parameter.
1063 continue;
Daniel Dunbard3674e62008-09-11 01:48:57 +00001064 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001065
1066 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001067 }
1068 assert(AI == Fn->arg_end() && "Argument mismatch!");
1069}
1070
Chris Lattner3fcc7902010-06-27 01:06:27 +00001071void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001072 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +00001073 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001074 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +00001075 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001076 }
Daniel Dunbar6696e222010-06-30 21:27:58 +00001077
Dan Gohman481e40c2010-07-20 20:13:52 +00001078 llvm::DebugLoc RetDbgLoc;
Chris Lattner726b3d02010-06-26 23:13:19 +00001079 llvm::Value *RV = 0;
1080 QualType RetTy = FI.getReturnType();
1081 const ABIArgInfo &RetAI = FI.getReturnInfo();
1082
1083 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001084 case ABIArgInfo::Indirect: {
1085 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattner726b3d02010-06-26 23:13:19 +00001086 if (RetTy->isAnyComplexType()) {
1087 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1088 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1089 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1090 // Do nothing; aggregrates get evaluated directly into the destination.
1091 } else {
1092 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar03816342010-08-21 02:24:36 +00001093 false, Alignment, RetTy);
Chris Lattner726b3d02010-06-26 23:13:19 +00001094 }
1095 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001096 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001097
1098 case ABIArgInfo::Extend:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001099 case ABIArgInfo::Direct:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001100 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1101 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001102 // The internal return value temp always will have pointer-to-return-type
1103 // type, just do a load.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001104
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001105 // If the instruction right before the insertion point is a store to the
1106 // return value, we can elide the load, zap the store, and usually zap the
1107 // alloca.
1108 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1109 llvm::StoreInst *SI = 0;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001110 if (InsertBB->empty() ||
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001111 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1112 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1113 RV = Builder.CreateLoad(ReturnValue);
1114 } else {
1115 // Get the stored value and nuke the now-dead store.
1116 RetDbgLoc = SI->getDebugLoc();
1117 RV = SI->getValueOperand();
1118 SI->eraseFromParent();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001119
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001120 // If that was the only use of the return value, nuke it as well now.
1121 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1122 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1123 ReturnValue = 0;
1124 }
Chris Lattner3fcc7902010-06-27 01:06:27 +00001125 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001126 } else {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001127 llvm::Value *V = ReturnValue;
1128 // If the value is offset in memory, apply the offset now.
1129 if (unsigned Offs = RetAI.getDirectOffset()) {
1130 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1131 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001132 V = Builder.CreateBitCast(V,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001133 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1134 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001135
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001136 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner3fcc7902010-06-27 01:06:27 +00001137 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001138 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00001139
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001140 case ABIArgInfo::Ignore:
Chris Lattner726b3d02010-06-26 23:13:19 +00001141 break;
1142
1143 case ABIArgInfo::Expand:
1144 assert(0 && "Invalid ABI kind for return argument");
1145 }
1146
Daniel Dunbar6696e222010-06-30 21:27:58 +00001147 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Patel65497582010-07-21 18:08:50 +00001148 if (!RetDbgLoc.isUnknown())
1149 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001150}
1151
John McCall32ea9692011-03-11 20:59:21 +00001152void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1153 const VarDecl *param) {
John McCall23f66262010-05-26 22:34:26 +00001154 // StartFunction converted the ABI-lowered parameter(s) into a
1155 // local alloca. We need to turn that into an r-value suitable
1156 // for EmitCall.
John McCall32ea9692011-03-11 20:59:21 +00001157 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall23f66262010-05-26 22:34:26 +00001158
John McCall32ea9692011-03-11 20:59:21 +00001159 QualType type = param->getType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001160
John McCall23f66262010-05-26 22:34:26 +00001161 // For the most part, we just need to load the alloca, except:
1162 // 1) aggregate r-values are actually pointers to temporaries, and
1163 // 2) references to aggregates are pointers directly to the aggregate.
1164 // I don't know why references to non-aggregates are different here.
John McCall32ea9692011-03-11 20:59:21 +00001165 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1166 if (hasAggregateLLVMType(ref->getPointeeType()))
1167 return args.add(RValue::getAggregate(local), type);
John McCall23f66262010-05-26 22:34:26 +00001168
1169 // Locals which are references to scalars are represented
1170 // with allocas holding the pointer.
John McCall32ea9692011-03-11 20:59:21 +00001171 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall23f66262010-05-26 22:34:26 +00001172 }
1173
John McCall32ea9692011-03-11 20:59:21 +00001174 if (type->isAnyComplexType()) {
1175 ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1176 return args.add(RValue::getComplex(complex), type);
1177 }
John McCall23f66262010-05-26 22:34:26 +00001178
John McCall32ea9692011-03-11 20:59:21 +00001179 if (hasAggregateLLVMType(type))
1180 return args.add(RValue::getAggregate(local), type);
John McCall23f66262010-05-26 22:34:26 +00001181
John McCall32ea9692011-03-11 20:59:21 +00001182 unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1183 llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1184 return args.add(RValue::get(value), type);
John McCall23f66262010-05-26 22:34:26 +00001185}
1186
John McCall32ea9692011-03-11 20:59:21 +00001187void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1188 QualType type) {
1189 if (type->isReferenceType())
1190 return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1191 type);
Mike Stump11289f42009-09-09 15:08:12 +00001192
Eli Friedman7e68c882011-06-15 18:26:32 +00001193 if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() &&
1194 isa<ImplicitCastExpr>(E) &&
Eli Friedmandf968192011-05-26 00:10:27 +00001195 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
1196 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
1197 assert(L.isSimple());
1198 args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()),
1199 type, /*NeedsCopy*/true);
1200 return;
1201 }
1202
John McCall32ea9692011-03-11 20:59:21 +00001203 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001204}
1205
John McCallbd309292010-07-06 01:34:17 +00001206/// Emits a call or invoke instruction to the given function, depending
1207/// on the current state of the EH stack.
1208llvm::CallSite
1209CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1210 llvm::Value * const *ArgBegin,
1211 llvm::Value * const *ArgEnd,
1212 const llvm::Twine &Name) {
1213 llvm::BasicBlock *InvokeDest = getInvokeDest();
1214 if (!InvokeDest)
1215 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1216
1217 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1218 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1219 ArgBegin, ArgEnd, Name);
1220 EmitBlock(ContBB);
1221 return Invoke;
1222}
1223
Daniel Dunbard931a872009-02-02 22:03:45 +00001224RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001225 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00001226 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001227 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00001228 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00001229 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00001230 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00001231 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001232
1233 // Handle struct-return functions by passing a pointer to the
1234 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001235 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001236 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001237
1238
Chris Lattner4ca97c32009-06-13 00:26:38 +00001239 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00001240 // alloca to hold the result, unless one is given to us.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001241 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001242 llvm::Value *Value = ReturnValue.getValue();
1243 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +00001244 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +00001245 Args.push_back(Value);
1246 }
Mike Stump11289f42009-09-09 15:08:12 +00001247
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001248 assert(CallInfo.arg_size() == CallArgs.size() &&
1249 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001250 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001251 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001252 I != E; ++I, ++info_it) {
1253 const ABIArgInfo &ArgInfo = info_it->info;
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001254 RValue RV = I->RV;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001255
Eli Friedmanf7456192011-06-15 22:09:18 +00001256 unsigned TypeAlign =
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001257 getContext().getTypeAlignInChars(I->Ty).getQuantity();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001258 switch (ArgInfo.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001259 case ABIArgInfo::Indirect: {
Daniel Dunbar747865a2009-02-05 09:16:39 +00001260 if (RV.isScalar() || RV.isComplex()) {
1261 // Make a temporary alloca to pass the argument.
Eli Friedman7e68c882011-06-15 18:26:32 +00001262 llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
1263 if (ArgInfo.getIndirectAlign() > AI->getAlignment())
1264 AI->setAlignment(ArgInfo.getIndirectAlign());
1265 Args.push_back(AI);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001266 if (RV.isScalar())
Daniel Dunbar03816342010-08-21 02:24:36 +00001267 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
Eli Friedmanf7456192011-06-15 22:09:18 +00001268 TypeAlign, I->Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001269 else
Mike Stump11289f42009-09-09 15:08:12 +00001270 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001271 } else {
Eli Friedmaneb7fab62011-06-14 01:37:52 +00001272 // We want to avoid creating an unnecessary temporary+copy here;
1273 // however, we need one in two cases:
1274 // 1. If the argument is not byval, and we are required to copy the
1275 // source. (This case doesn't occur on any common architecture.)
1276 // 2. If the argument is byval, RV is not sufficiently aligned, and
1277 // we cannot force it to be sufficiently aligned.
Eli Friedmanf7456192011-06-15 22:09:18 +00001278 llvm::Value *Addr = RV.getAggregateAddr();
1279 unsigned Align = ArgInfo.getIndirectAlign();
1280 const llvm::TargetData *TD = &CGM.getTargetData();
1281 if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
1282 (ArgInfo.getIndirectByVal() && TypeAlign < Align &&
1283 llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) {
Eli Friedmaneb7fab62011-06-14 01:37:52 +00001284 // Create an aligned temporary, and copy to it.
Eli Friedmanf7456192011-06-15 22:09:18 +00001285 llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
1286 if (Align > AI->getAlignment())
1287 AI->setAlignment(Align);
Eli Friedmaneb7fab62011-06-14 01:37:52 +00001288 Args.push_back(AI);
Eli Friedmanf7456192011-06-15 22:09:18 +00001289 EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
Eli Friedmaneb7fab62011-06-14 01:37:52 +00001290 } else {
1291 // Skip the extra memcpy call.
Eli Friedmanf7456192011-06-15 22:09:18 +00001292 Args.push_back(Addr);
Eli Friedmaneb7fab62011-06-14 01:37:52 +00001293 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00001294 }
1295 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001296 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00001297
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001298 case ABIArgInfo::Ignore:
1299 break;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001300
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001301 case ABIArgInfo::Extend:
1302 case ABIArgInfo::Direct: {
1303 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001304 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1305 ArgInfo.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001306 if (RV.isScalar())
1307 Args.push_back(RV.getScalarVal());
1308 else
1309 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1310 break;
1311 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001312
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001313 // FIXME: Avoid the conversion through memory if possible.
1314 llvm::Value *SrcPtr;
1315 if (RV.isScalar()) {
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001316 SrcPtr = CreateMemTemp(I->Ty, "coerce");
Eli Friedmanf7456192011-06-15 22:09:18 +00001317 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty);
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001318 } else if (RV.isComplex()) {
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001319 SrcPtr = CreateMemTemp(I->Ty, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001320 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001321 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001322 SrcPtr = RV.getAggregateAddr();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001323
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001324 // If the value is offset in memory, apply the offset now.
1325 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1326 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1327 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001328 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001329 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1330
1331 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001332
Chris Lattner3dd716c2010-06-28 23:44:11 +00001333 // If the coerce-to type is a first class aggregate, we flatten it and
1334 // pass the elements. Either way is semantically identical, but fast-isel
1335 // and the optimizer generally likes scalar values better than FCAs.
1336 if (const llvm::StructType *STy =
Chris Lattner15ec3612010-06-29 00:06:42 +00001337 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001338 SrcPtr = Builder.CreateBitCast(SrcPtr,
1339 llvm::PointerType::getUnqual(STy));
1340 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1341 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerff941a62010-07-28 18:24:28 +00001342 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1343 // We don't know what we're loading from.
1344 LI->setAlignment(1);
1345 Args.push_back(LI);
Chris Lattner15ec3612010-06-29 00:06:42 +00001346 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00001347 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00001348 // In the simple case, just pass the coerced loaded value.
1349 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1350 *this));
Chris Lattner3dd716c2010-06-28 23:44:11 +00001351 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001352
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001353 break;
1354 }
1355
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001356 case ABIArgInfo::Expand:
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001357 ExpandTypeToArgs(I->Ty, RV, Args);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001358 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001359 }
1360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattner4ca97c32009-06-13 00:26:38 +00001362 // If the callee is a bitcast of a function to a varargs pointer to function
1363 // type, check to see if we can remove the bitcast. This handles some cases
1364 // with unprototyped functions.
1365 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1366 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1367 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1368 const llvm::FunctionType *CurFT =
1369 cast<llvm::FunctionType>(CurPT->getElementType());
1370 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattner4ca97c32009-06-13 00:26:38 +00001372 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1373 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00001374 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00001375 ActualFT->getNumParams() == Args.size() &&
1376 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00001377 bool ArgsMatch = true;
1378 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1379 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1380 ArgsMatch = false;
1381 break;
1382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner4ca97c32009-06-13 00:26:38 +00001384 // Strip the cast if we can get away with it. This is a nice cleanup,
1385 // but also allows us to inline the function at -O0 if it is marked
1386 // always_inline.
1387 if (ArgsMatch)
1388 Callee = CalleeF;
1389 }
1390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Daniel Dunbar613855c2008-09-09 23:27:19 +00001392
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001393 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00001394 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001395 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +00001396 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1397 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +00001398
John McCallbd309292010-07-06 01:34:17 +00001399 llvm::BasicBlock *InvokeDest = 0;
1400 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1401 InvokeDest = getInvokeDest();
1402
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001403 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00001404 if (!InvokeDest) {
Jay Foad7d0479f2009-05-21 09:52:38 +00001405 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001406 } else {
1407 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +00001408 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +00001409 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001410 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001411 }
Chris Lattnere70a0072010-06-29 16:40:28 +00001412 if (callOrInvoke)
David Chisnallff5f88c2010-05-02 13:41:58 +00001413 *callOrInvoke = CS.getInstruction();
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001414
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001415 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001416 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001417
1418 // If the call doesn't return, finish the basic block and clear the
1419 // insertion point; this allows the rest of IRgen to discard
1420 // unreachable code.
1421 if (CS.doesNotReturn()) {
1422 Builder.CreateUnreachable();
1423 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001424
Mike Stump18bb9282009-05-16 07:57:57 +00001425 // FIXME: For now, emit a dummy basic block because expr emitters in
1426 // generally are not ready to handle emitting expressions at unreachable
1427 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001428 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001429
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001430 // Return a reasonable RValue.
1431 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001432 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001433
1434 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001435 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001436 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001437
1438 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001439 case ABIArgInfo::Indirect: {
1440 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001441 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001442 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001443 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001444 return RValue::getAggregate(Args[0]);
Daniel Dunbar03816342010-08-21 02:24:36 +00001445 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1446 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001447
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001448 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001449 // If we are ignoring an argument that had a result, make sure to
1450 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001451 return GetUndefRValue(RetTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001452
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001453 case ABIArgInfo::Extend:
1454 case ABIArgInfo::Direct: {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001455 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1456 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001457 if (RetTy->isAnyComplexType()) {
1458 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1459 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1460 return RValue::getComplex(std::make_pair(Real, Imag));
1461 }
1462 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1463 llvm::Value *DestPtr = ReturnValue.getValue();
1464 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001465
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001466 if (!DestPtr) {
1467 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1468 DestIsVolatile = false;
1469 }
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001470 BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001471 return RValue::getAggregate(DestPtr);
1472 }
1473 return RValue::get(CI);
1474 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001475
Anders Carlsson17490832009-12-24 20:40:36 +00001476 llvm::Value *DestPtr = ReturnValue.getValue();
1477 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001478
Anders Carlsson17490832009-12-24 20:40:36 +00001479 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001480 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001481 DestIsVolatile = false;
1482 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001483
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001484 // If the value is offset in memory, apply the offset now.
1485 llvm::Value *StorePtr = DestPtr;
1486 if (unsigned Offs = RetAI.getDirectOffset()) {
1487 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1488 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001489 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001490 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1491 }
1492 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001493
Daniel Dunbar03816342010-08-21 02:24:36 +00001494 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001495 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001496 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001497 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001498 return RValue::getAggregate(DestPtr);
Daniel Dunbar03816342010-08-21 02:24:36 +00001499 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001500 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001501
Daniel Dunbard3674e62008-09-11 01:48:57 +00001502 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001503 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001504 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001505
1506 assert(0 && "Unhandled ABIArgInfo::Kind");
1507 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001508}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001509
1510/* VarArg handling */
1511
1512llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1513 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1514}