blob: 712ae89a48887d50442fecff93374ffc76d9a850 [file] [log] [blame]
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
John McCall5d865c322010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Chris Lattnere70a0072010-06-29 16:40:28 +000017#include "ABIInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000018#include "CodeGenFunction.h"
Daniel Dunbarc68897d2008-09-10 00:41:16 +000019#include "CodeGenModule.h"
Daniel Dunbard9eff3d2008-10-13 17:02:26 +000020#include "clang/Basic/TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000021#include "clang/AST/Decl.h"
Anders Carlssonb15b55c2009-04-03 22:48:58 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000023#include "clang/AST/DeclObjC.h"
Chandler Carruth85098242010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Devang Patel3e1f51b2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbarb960b7b2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000028using namespace clang;
29using namespace CodeGen;
30
31/***/
32
John McCallab26cfa2010-02-05 21:31:56 +000033static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
34 switch (CC) {
35 default: return llvm::CallingConv::C;
36 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
37 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregora941dca2010-05-18 16:57:00 +000038 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
Anton Korobeynikov231e8752011-04-14 20:06:49 +000039 case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
40 case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
Dawn Perchik335e16b2010-09-03 01:29:35 +000041 // TODO: add support for CC_X86Pascal to llvm
John McCallab26cfa2010-02-05 21:31:56 +000042 }
43}
44
John McCall8ee376f2010-02-24 07:14:12 +000045/// Derives the 'this' type for codegen purposes, i.e. ignoring method
46/// qualification.
47/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000048static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
49 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
50 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000051}
52
John McCall8ee376f2010-02-24 07:14:12 +000053/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000054static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
55 return MD->getType()->getCanonicalTypeUnqualified()
56 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000057}
58
59/// Returns the "extra-canonicalized" return type, which discards
60/// qualifiers on the return type. Codegen doesn't care about them,
61/// and it makes ABI code a little easier to be able to assume that
62/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000063static CanQualType GetReturnType(QualType RetTy) {
64 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000065}
66
67const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000068CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
69 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000070 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
71 llvm::SmallVector<CanQualType, 16>(),
Chris Lattner5c740f12010-06-30 19:14:05 +000072 FTNP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000073}
74
75/// \param Args - contains any initial parameters besides those
76/// in the formal type
77static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCall2da83a32010-02-26 00:48:12 +000078 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +000079 CanQual<FunctionProtoType> FTP,
80 bool IsRecursive = false) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000081 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000082 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000083 ArgTys.push_back(FTP->getArgType(i));
John McCall2da83a32010-02-26 00:48:12 +000084 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Tilmann Scheller99cc30c2011-03-02 21:36:49 +000085 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall8ee376f2010-02-24 07:14:12 +000086}
87
88const CGFunctionInfo &
Chris Lattner5c740f12010-06-30 19:14:05 +000089CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
90 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +000091 llvm::SmallVector<CanQualType, 16> ArgTys;
Tilmann Scheller99cc30c2011-03-02 21:36:49 +000092 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbar7feafc72009-09-11 22:24:53 +000093}
94
John McCallab26cfa2010-02-05 21:31:56 +000095static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000096 // Set the appropriate calling convention for the Function.
97 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000098 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000099
100 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +0000101 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000102
Douglas Gregora941dca2010-05-18 16:57:00 +0000103 if (D->hasAttr<ThisCallAttr>())
104 return CC_X86ThisCall;
105
Dawn Perchik335e16b2010-09-03 01:29:35 +0000106 if (D->hasAttr<PascalAttr>())
107 return CC_X86Pascal;
108
Anton Korobeynikov231e8752011-04-14 20:06:49 +0000109 if (PcsAttr *PCS = D->getAttr<PcsAttr>())
110 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
111
John McCallab26cfa2010-02-05 21:31:56 +0000112 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000113}
114
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000115const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
116 const FunctionProtoType *FTP) {
John McCall2da83a32010-02-26 00:48:12 +0000117 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000118
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000119 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000120 ArgTys.push_back(GetThisType(Context, RD));
121
122 return ::getFunctionInfo(*this, ArgTys,
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000123 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000124}
125
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000126const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000127 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000128
John McCall0d635f52010-09-03 01:26:39 +0000129 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
130 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
131
Chris Lattnerbea5b622009-05-12 20:27:19 +0000132 // Add the 'this' pointer unless this is a static method.
133 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000134 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000135
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000136 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000137}
138
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000139const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000140 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000141 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000142 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000143 CanQualType ResTy = Context.VoidTy;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000144
John McCall5d865c322010-08-31 07:33:07 +0000145 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall8ee376f2010-02-24 07:14:12 +0000146
John McCall5d865c322010-08-31 07:33:07 +0000147 CanQual<FunctionProtoType> FTP = GetFormalType(D);
148
149 // Add the formal parameters.
150 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
151 ArgTys.push_back(FTP->getArgType(i));
152
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000153 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000154}
155
156const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
157 CXXDtorType Type) {
John McCall5d865c322010-08-31 07:33:07 +0000158 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCall2da83a32010-02-26 00:48:12 +0000159 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall5d865c322010-08-31 07:33:07 +0000160 CanQualType ResTy = Context.VoidTy;
John McCall8ee376f2010-02-24 07:14:12 +0000161
John McCall5d865c322010-08-31 07:33:07 +0000162 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
163
164 CanQual<FunctionProtoType> FTP = GetFormalType(D);
165 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
166
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000167 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000168}
169
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000170const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000171 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000172 if (MD->isInstance())
173 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000174
John McCall2da83a32010-02-26 00:48:12 +0000175 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
176 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000177 if (isa<FunctionNoProtoType>(FTy))
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000178 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
John McCall2da83a32010-02-26 00:48:12 +0000179 assert(isa<FunctionProtoType>(FTy));
180 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000181}
182
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000183const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000184 llvm::SmallVector<CanQualType, 16> ArgTys;
185 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
186 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000187 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000188 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000189 e = MD->param_end(); i != e; ++i) {
190 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
191 }
192 return getFunctionInfo(GetReturnType(MD->getResultType()),
193 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000194 FunctionType::ExtInfo(
195 /*NoReturn*/ false,
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000196 /*HasRegParm*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000197 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000198 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000199}
200
Anders Carlsson6710c532010-02-06 02:44:09 +0000201const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
202 // FIXME: Do we need to handle ObjCMethodDecl?
203 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000204
Anders Carlsson6710c532010-02-06 02:44:09 +0000205 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
206 return getFunctionInfo(CD, GD.getCtorType());
207
208 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
209 return getFunctionInfo(DD, GD.getDtorType());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000210
Anders Carlsson6710c532010-02-06 02:44:09 +0000211 return getFunctionInfo(FD);
212}
213
Mike Stump11289f42009-09-09 15:08:12 +0000214const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000215 const CallArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000216 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000217 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000218 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000219 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000220 i != e; ++i)
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000221 ArgTys.push_back(Context.getCanonicalParamType(i->Ty));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000222 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000223}
224
Mike Stump11289f42009-09-09 15:08:12 +0000225const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000226 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000227 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000228 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000229 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000230 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000231 i != e; ++i)
John McCalla738c252011-03-09 04:27:21 +0000232 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000233 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000234}
235
John McCalla738c252011-03-09 04:27:21 +0000236const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
237 llvm::SmallVector<CanQualType, 1> args;
238 return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
239}
240
John McCall2da83a32010-02-26 00:48:12 +0000241const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
242 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner5c740f12010-06-30 19:14:05 +0000243 const FunctionType::ExtInfo &Info,
244 bool IsRecursive) {
John McCall2da83a32010-02-26 00:48:12 +0000245#ifndef NDEBUG
246 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
247 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
248 assert(I->isCanonicalAsParam());
249#endif
250
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000251 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000252
Daniel Dunbare0be8292009-02-03 00:07:12 +0000253 // Lookup or create unique function info.
254 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000255 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000256 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000257
258 void *InsertPos = 0;
259 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
260 if (FI)
261 return *FI;
262
Daniel Dunbar313321e2009-02-03 05:31:23 +0000263 // Construct the function info.
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000264 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getHasRegParm(), Info.getRegParm(), ResTy,
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000265 ArgTys.data(), ArgTys.size());
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000266 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000267
268 // Compute ABI information.
Chris Lattner22326a12010-07-29 02:31:05 +0000269 getABIInfo().computeInfo(*FI);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000270
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000271 // Loop over all of the computed argument and return value info. If any of
272 // them are direct or extend without a specified coerce type, specify the
273 // default now.
274 ABIArgInfo &RetInfo = FI->getReturnInfo();
275 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
276 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000277
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000278 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
279 I != E; ++I)
280 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
281 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar313321e2009-02-03 05:31:23 +0000282
Chris Lattner0e7929f2010-07-01 06:20:47 +0000283 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
284 // types, resolve them now. These pointers may point to this function, which
285 // we *just* filled in the FunctionInfo for.
Chris Lattner22326a12010-07-29 02:31:05 +0000286 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattner0e7929f2010-07-01 06:20:47 +0000287 HandleLateResolvedPointers();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000288
Daniel Dunbare0be8292009-02-03 00:07:12 +0000289 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000290}
291
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000292CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000293 bool _NoReturn, bool _HasRegParm, unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000294 CanQualType ResTy,
Chris Lattner34d62812010-06-29 18:13:52 +0000295 const CanQualType *ArgTys,
296 unsigned NumArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000297 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000298 EffectiveCallingConvention(_CallingConvention),
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000299 NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000300{
Chris Lattner34d62812010-06-29 18:13:52 +0000301 NumArgs = NumArgTys;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000302
Chris Lattner3dd716c2010-06-28 23:44:11 +0000303 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattner34d62812010-06-29 18:13:52 +0000304 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar313321e2009-02-03 05:31:23 +0000305 Args[0].type = ResTy;
Chris Lattner34d62812010-06-29 18:13:52 +0000306 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar313321e2009-02-03 05:31:23 +0000307 Args[1 + i].type = ArgTys[i];
308}
309
310/***/
311
John McCall85dd2c52011-05-15 02:19:42 +0000312void CodeGenTypes::GetExpandedTypes(QualType type,
313 llvm::SmallVectorImpl<const llvm::Type*> &expandedTypes,
314 bool isRecursive) {
315 const RecordType *RT = type->getAsStructureType();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000316 assert(RT && "Can only expand structure types.");
317 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000318 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000319 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000320
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000321 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
322 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000323 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000324 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000325 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000326
John McCall85dd2c52011-05-15 02:19:42 +0000327 QualType fieldType = FD->getType();
328 if (fieldType->isRecordType())
329 GetExpandedTypes(fieldType, expandedTypes, isRecursive);
Chris Lattnerff941a62010-07-28 18:24:28 +0000330 else
John McCall85dd2c52011-05-15 02:19:42 +0000331 expandedTypes.push_back(ConvertType(fieldType, isRecursive));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000332 }
333}
334
Mike Stump11289f42009-09-09 15:08:12 +0000335llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000336CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
337 llvm::Function::arg_iterator AI) {
338 const RecordType *RT = Ty->getAsStructureType();
339 assert(RT && "Can only expand structure types.");
340
341 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000342 assert(LV.isSimple() &&
343 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000344 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000345 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
346 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000347 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000348 QualType FT = FD->getType();
349
350 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000351 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000352 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
353 AI = ExpandTypeFromArgs(FT, LV, AI);
354 } else {
355 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
356 ++AI;
357 }
358 }
359
360 return AI;
361}
362
Mike Stump11289f42009-09-09 15:08:12 +0000363void
364CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000365 llvm::SmallVector<llvm::Value*, 16> &Args) {
366 const RecordType *RT = Ty->getAsStructureType();
367 assert(RT && "Can only expand structure types.");
368
369 RecordDecl *RD = RT->getDecl();
370 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
371 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000372 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
373 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000374 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000375 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000376
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000377 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000378 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000379 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
380 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
381 } else {
382 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000383 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000384 "Unexpected non-scalar rvalue during struct expansion.");
385 Args.push_back(RV.getScalarVal());
386 }
387 }
388}
389
Chris Lattner895c52b2010-06-27 06:04:18 +0000390/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000391/// accessing some number of bytes out of it, try to gep into the struct to get
392/// at its inner goodness. Dive as deep as possible without entering an element
393/// with an in-memory size smaller than DstSize.
394static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000395EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
396 const llvm::StructType *SrcSTy,
397 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000398 // We can't dive into a zero-element struct.
399 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000400
Chris Lattner1cd66982010-06-27 05:56:15 +0000401 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000402
Chris Lattner1cd66982010-06-27 05:56:15 +0000403 // If the first elt is at least as large as what we're looking for, or if the
404 // first element is the same size as the whole struct, we can enter it.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000405 uint64_t FirstEltSize =
Chris Lattner1cd66982010-06-27 05:56:15 +0000406 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000407 if (FirstEltSize < DstSize &&
Chris Lattner1cd66982010-06-27 05:56:15 +0000408 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
409 return SrcPtr;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000410
Chris Lattner1cd66982010-06-27 05:56:15 +0000411 // GEP into the first element.
412 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000413
Chris Lattner1cd66982010-06-27 05:56:15 +0000414 // If the first element is a struct, recurse.
415 const llvm::Type *SrcTy =
416 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
417 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000418 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000419
420 return SrcPtr;
421}
422
Chris Lattner055097f2010-06-27 06:26:04 +0000423/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
424/// are either integers or pointers. This does a truncation of the value if it
425/// is too large or a zero extension if it is too small.
426static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
427 const llvm::Type *Ty,
428 CodeGenFunction &CGF) {
429 if (Val->getType() == Ty)
430 return Val;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000431
Chris Lattner055097f2010-06-27 06:26:04 +0000432 if (isa<llvm::PointerType>(Val->getType())) {
433 // If this is Pointer->Pointer avoid conversion to and from int.
434 if (isa<llvm::PointerType>(Ty))
435 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000436
Chris Lattner055097f2010-06-27 06:26:04 +0000437 // Convert the pointer to an integer so we can play with its width.
Chris Lattner5e016ae2010-06-27 07:15:29 +0000438 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner055097f2010-06-27 06:26:04 +0000439 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000440
Chris Lattner055097f2010-06-27 06:26:04 +0000441 const llvm::Type *DestIntTy = Ty;
442 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner5e016ae2010-06-27 07:15:29 +0000443 DestIntTy = CGF.IntPtrTy;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000444
Chris Lattner055097f2010-06-27 06:26:04 +0000445 if (Val->getType() != DestIntTy)
446 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000447
Chris Lattner055097f2010-06-27 06:26:04 +0000448 if (isa<llvm::PointerType>(Ty))
449 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
450 return Val;
451}
452
Chris Lattner1cd66982010-06-27 05:56:15 +0000453
454
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000455/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
456/// a pointer to an object of type \arg Ty.
457///
458/// This safely handles the case when the src type is smaller than the
459/// destination type; in this situation the values of bits which not
460/// present in the src are undefined.
461static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
462 const llvm::Type *Ty,
463 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000464 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000465 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000466
Chris Lattnerd200eda2010-06-28 22:51:39 +0000467 // If SrcTy and Ty are the same, just do a load.
468 if (SrcTy == Ty)
469 return CGF.Builder.CreateLoad(SrcPtr);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000470
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000471 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000472
Chris Lattner1cd66982010-06-27 05:56:15 +0000473 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000474 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000475 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
476 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000477
Chris Lattner1cd66982010-06-27 05:56:15 +0000478 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000479
Chris Lattner055097f2010-06-27 06:26:04 +0000480 // If the source and destination are integer or pointer types, just do an
481 // extension or truncation to the desired type.
482 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
483 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
484 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
485 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
486 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000487
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000488 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000489 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000490 // Generally SrcSize is never greater than DstSize, since this means we are
491 // losing bits. However, this can happen in cases where the structure has
492 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000493 //
Mike Stump18bb9282009-05-16 07:57:57 +0000494 // FIXME: Assert that we aren't truncating non-padding bits when have access
495 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000496 llvm::Value *Casted =
497 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000498 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
499 // FIXME: Use better alignment / avoid requiring aligned load.
500 Load->setAlignment(1);
501 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000502 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000503
Chris Lattner3fcc7902010-06-27 01:06:27 +0000504 // Otherwise do coercion through memory. This is stupid, but
505 // simple.
506 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
507 llvm::Value *Casted =
508 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
509 llvm::StoreInst *Store =
510 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
511 // FIXME: Use better alignment / avoid requiring aligned store.
512 Store->setAlignment(1);
513 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000514}
515
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000516// Function to store a first-class aggregate into memory. We prefer to
517// store the elements rather than the aggregate to be more friendly to
518// fast-isel.
519// FIXME: Do we need to recurse here?
520static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
521 llvm::Value *DestPtr, bool DestIsVolatile,
522 bool LowAlignment) {
523 // Prefer scalar stores to first-class aggregate stores.
524 if (const llvm::StructType *STy =
525 dyn_cast<llvm::StructType>(Val->getType())) {
526 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
527 llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
528 llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
529 llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
530 DestIsVolatile);
531 if (LowAlignment)
532 SI->setAlignment(1);
533 }
534 } else {
535 CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
536 }
537}
538
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000539/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
540/// where the source and destination may have different types.
541///
542/// This safely handles the case when the src type is larger than the
543/// destination type; the upper bits of the src will be lost.
544static void CreateCoercedStore(llvm::Value *Src,
545 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000546 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000547 CodeGenFunction &CGF) {
548 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000549 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000550 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattnerd200eda2010-06-28 22:51:39 +0000551 if (SrcTy == DstTy) {
552 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
553 return;
554 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000555
Chris Lattnerd200eda2010-06-28 22:51:39 +0000556 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000557
Chris Lattner895c52b2010-06-27 06:04:18 +0000558 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
559 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
560 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
561 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000562
Chris Lattner055097f2010-06-27 06:26:04 +0000563 // If the source and destination are integer or pointer types, just do an
564 // extension or truncation to the desired type.
565 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
566 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
567 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
568 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
569 return;
570 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000571
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000572 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000573
Daniel Dunbar313321e2009-02-03 05:31:23 +0000574 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000575 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000576 llvm::Value *Casted =
577 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000578 // FIXME: Use better alignment / avoid requiring aligned store.
Eli Friedmanaf9b3252011-05-17 21:08:01 +0000579 BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000580 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000581 // Otherwise do coercion through memory. This is stupid, but
582 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000583
584 // Generally SrcSize is never greater than DstSize, since this means we are
585 // losing bits. However, this can happen in cases where the structure has
586 // additional padding, for example due to a user specified alignment.
587 //
588 // FIXME: Assert that we aren't truncating non-padding bits when have access
589 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000590 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
591 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000592 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000593 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000594 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
595 // FIXME: Use better alignment / avoid requiring aligned load.
596 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000597 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000598 }
599}
600
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000601/***/
602
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000603bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000604 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000605}
606
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000607bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
608 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
609 switch (BT->getKind()) {
610 default:
611 return false;
612 case BuiltinType::Float:
613 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
614 case BuiltinType::Double:
615 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
616 case BuiltinType::LongDouble:
617 return getContext().Target.useObjCFPRetForRealType(
618 TargetInfo::LongDouble);
619 }
620 }
621
622 return false;
623}
624
John McCallf8ff7b92010-02-23 00:48:20 +0000625const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
626 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000627
John McCallf8ff7b92010-02-23 00:48:20 +0000628 // For definition purposes, don't consider a K&R function variadic.
629 bool Variadic = false;
630 if (const FunctionProtoType *FPT =
631 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
632 Variadic = FPT->isVariadic();
633
Chris Lattner5c740f12010-06-30 19:14:05 +0000634 return GetFunctionType(FI, Variadic, false);
John McCallf8ff7b92010-02-23 00:48:20 +0000635}
636
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000637const llvm::FunctionType *
John McCall85dd2c52011-05-15 02:19:42 +0000638CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic,
639 bool isRecursive) {
640 llvm::SmallVector<const llvm::Type*, 8> argTypes;
641 const llvm::Type *resultType = 0;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000642
John McCall85dd2c52011-05-15 02:19:42 +0000643 const ABIArgInfo &retAI = FI.getReturnInfo();
644 switch (retAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000645 case ABIArgInfo::Expand:
John McCall85dd2c52011-05-15 02:19:42 +0000646 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbard3674e62008-09-11 01:48:57 +0000647
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000648 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000649 case ABIArgInfo::Direct:
John McCall85dd2c52011-05-15 02:19:42 +0000650 resultType = retAI.getCoerceToType();
Daniel Dunbar67dace892009-02-03 06:17:37 +0000651 break;
652
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000653 case ABIArgInfo::Indirect: {
John McCall85dd2c52011-05-15 02:19:42 +0000654 assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
655 resultType = llvm::Type::getVoidTy(getLLVMContext());
656
657 QualType ret = FI.getReturnType();
658 const llvm::Type *ty = ConvertType(ret, isRecursive);
659 unsigned addressSpace = Context.getTargetAddressSpace(ret);
660 argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000661 break;
662 }
663
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000664 case ABIArgInfo::Ignore:
John McCall85dd2c52011-05-15 02:19:42 +0000665 resultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000666 break;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
669 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000670 ie = FI.arg_end(); it != ie; ++it) {
John McCall85dd2c52011-05-15 02:19:42 +0000671 const ABIArgInfo &argAI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000672
John McCall85dd2c52011-05-15 02:19:42 +0000673 switch (argAI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000674 case ABIArgInfo::Ignore:
675 break;
676
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000677 case ABIArgInfo::Indirect: {
678 // indirect arguments are always on the stack, which is addr space #0.
John McCall85dd2c52011-05-15 02:19:42 +0000679 const llvm::Type *LTy = ConvertTypeForMem(it->type, isRecursive);
680 argTypes.push_back(LTy->getPointerTo());
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000681 break;
682 }
683
684 case ABIArgInfo::Extend:
Chris Lattner2cdfda42010-07-29 06:44:09 +0000685 case ABIArgInfo::Direct: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000686 // If the coerce-to type is a first class aggregate, flatten it. Either
687 // way is semantically identical, but fast-isel and the optimizer
688 // generally likes scalar values better than FCAs.
John McCall85dd2c52011-05-15 02:19:42 +0000689 const llvm::Type *argType = argAI.getCoerceToType();
690 if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
691 for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
692 argTypes.push_back(st->getElementType(i));
Chris Lattner3dd716c2010-06-28 23:44:11 +0000693 } else {
John McCall85dd2c52011-05-15 02:19:42 +0000694 argTypes.push_back(argType);
Chris Lattner3dd716c2010-06-28 23:44:11 +0000695 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000696 break;
Chris Lattner2cdfda42010-07-29 06:44:09 +0000697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Daniel Dunbard3674e62008-09-11 01:48:57 +0000699 case ABIArgInfo::Expand:
John McCall85dd2c52011-05-15 02:19:42 +0000700 GetExpandedTypes(it->type, argTypes, isRecursive);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000701 break;
702 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000703 }
704
John McCall85dd2c52011-05-15 02:19:42 +0000705 return llvm::FunctionType::get(resultType, argTypes, isVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000706}
707
John McCall5d865c322010-08-31 07:33:07 +0000708const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
709 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson64457732009-11-24 05:08:52 +0000710 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000711
John McCall5d865c322010-08-31 07:33:07 +0000712 if (!VerifyFuncTypeComplete(FPT)) {
713 const CGFunctionInfo *Info;
714 if (isa<CXXDestructorDecl>(MD))
715 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
716 else
717 Info = &getFunctionInfo(MD);
718 return GetFunctionType(*Info, FPT->isVariadic(), false);
719 }
Anders Carlsson64457732009-11-24 05:08:52 +0000720
721 return llvm::OpaqueType::get(getLLVMContext());
722}
723
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000724void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000725 const Decl *TargetDecl,
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000726 AttributeListType &PAL,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000727 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000728 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000729 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000730
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000731 CallingConv = FI.getEffectiveCallingConvention();
732
John McCallab26cfa2010-02-05 21:31:56 +0000733 if (FI.isNoReturn())
734 FuncAttrs |= llvm::Attribute::NoReturn;
735
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000736 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000737 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000738 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000739 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCallbe349de2010-07-08 06:48:12 +0000740 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
741 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl31ad7542011-03-13 17:09:40 +0000742 if (FPT && FPT->isNothrow(getContext()))
John McCallbe349de2010-07-08 06:48:12 +0000743 FuncAttrs |= llvm::Attribute::NoUnwind;
744 }
745
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000746 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000747 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000748 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000749 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000750 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000751 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000752 if (TargetDecl->hasAttr<MallocAttr>())
753 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000754 }
755
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000756 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000757 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000758 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000759 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000760 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000761 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000762
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000763 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000764 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000765 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000766 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000767 case ABIArgInfo::Extend:
Chris Lattner4b8585e2010-07-28 23:46:15 +0000768 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000769 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner4b8585e2010-07-28 23:46:15 +0000770 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000771 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000772 break;
Daniel Dunbar67dace892009-02-03 06:17:37 +0000773 case ABIArgInfo::Direct:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000774 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000775 break;
776
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000777 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000778 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000779 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000780 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000781 // sret disables readnone and readonly
782 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
783 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000784 break;
785
Daniel Dunbard3674e62008-09-11 01:48:57 +0000786 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000787 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000788 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000789
Devang Patel597e7082008-09-26 22:53:57 +0000790 if (RetAttrs)
791 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000792
Daniel Dunbar0bb03312011-02-09 17:54:19 +0000793 // FIXME: RegParm should be reduced in case of global register variable.
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000794 signed RegParm;
795 if (FI.getHasRegParm())
796 RegParm = FI.getRegParm();
797 else
Daniel Dunbar0bb03312011-02-09 17:54:19 +0000798 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000799
800 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000801 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000802 ie = FI.arg_end(); it != ie; ++it) {
803 QualType ParamType = it->type;
804 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000805 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000806
John McCall39ec71f2010-03-27 00:47:27 +0000807 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
808 // have the corresponding parameter variable. It doesn't make
Daniel Dunbarcb2b3d02011-02-10 18:10:07 +0000809 // sense to do it here because parameters are so messed up.
Daniel Dunbard3674e62008-09-11 01:48:57 +0000810 switch (AI.getKind()) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000811 case ABIArgInfo::Extend:
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000812 if (ParamType->isSignedIntegerOrEnumerationType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000813 Attributes |= llvm::Attribute::SExt;
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000814 else if (ParamType->isUnsignedIntegerOrEnumerationType())
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000815 Attributes |= llvm::Attribute::ZExt;
816 // FALL THROUGH
817 case ABIArgInfo::Direct:
818 if (RegParm > 0 &&
819 (ParamType->isIntegerType() || ParamType->isPointerType())) {
820 RegParm -=
821 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
822 if (RegParm >= 0)
823 Attributes |= llvm::Attribute::InReg;
824 }
825 // FIXME: handle sseregparm someday...
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000826
Chris Lattner3dd716c2010-06-28 23:44:11 +0000827 if (const llvm::StructType *STy =
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000828 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
829 Index += STy->getNumElements()-1; // 1 will be added below.
830 break;
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000831
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000832 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000833 if (AI.getIndirectByVal())
834 Attributes |= llvm::Attribute::ByVal;
835
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000836 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000837 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000838 // byval disables readnone and readonly.
839 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
840 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000841 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000842
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000843 case ABIArgInfo::Ignore:
844 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000845 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000846
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000847 case ABIArgInfo::Expand: {
John McCall85dd2c52011-05-15 02:19:42 +0000848 llvm::SmallVector<const llvm::Type*, 8> types;
Mike Stump18bb9282009-05-16 07:57:57 +0000849 // FIXME: This is rather inefficient. Do we ever actually need to do
850 // anything here? The result should be just reconstructed on the other
851 // side, so extension should be a non-issue.
John McCall85dd2c52011-05-15 02:19:42 +0000852 getTypes().GetExpandedTypes(ParamType, types, false);
853 Index += types.size();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000854 continue;
855 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Devang Patel322300d2008-09-25 21:02:23 +0000858 if (Attributes)
859 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000860 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000861 }
Devang Patel597e7082008-09-26 22:53:57 +0000862 if (FuncAttrs)
863 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000864}
865
John McCalla738c252011-03-09 04:27:21 +0000866/// An argument came in as a promoted argument; demote it back to its
867/// declared type.
868static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
869 const VarDecl *var,
870 llvm::Value *value) {
871 const llvm::Type *varType = CGF.ConvertType(var->getType());
872
873 // This can happen with promotions that actually don't change the
874 // underlying type, like the enum promotions.
875 if (value->getType() == varType) return value;
876
877 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
878 && "unexpected promotion type");
879
880 if (isa<llvm::IntegerType>(varType))
881 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
882
883 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
884}
885
Daniel Dunbard931a872009-02-02 22:03:45 +0000886void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
887 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000888 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000889 // If this is an implicit-return-zero function, go ahead and
890 // initialize the return value. TODO: it might be nice to have
891 // a more general mechanism for this that didn't require synthesized
892 // return statements.
Chris Lattnerc401de92010-07-05 20:21:00 +0000893 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000894 if (FD->hasImplicitReturnZero()) {
895 QualType RetTy = FD->getResultType().getUnqualifiedType();
896 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000897 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000898 Builder.CreateStore(Zero, ReturnValue);
899 }
900 }
901
Mike Stump18bb9282009-05-16 07:57:57 +0000902 // FIXME: We no longer need the types from FunctionArgList; lift up and
903 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000904
Daniel Dunbar613855c2008-09-09 23:27:19 +0000905 // Emit allocs for param decls. Give the LLVM Argument nodes names.
906 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000907
Daniel Dunbar613855c2008-09-09 23:27:19 +0000908 // Name the struct return argument.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +0000909 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000910 AI->setName("agg.result");
911 ++AI;
912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000914 assert(FI.arg_size() == Args.size() &&
915 "Mismatch between function signature & arguments.");
Devang Patel68a15252011-03-03 20:13:15 +0000916 unsigned ArgNo = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000917 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel68a15252011-03-03 20:13:15 +0000918 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
919 i != e; ++i, ++info_it, ++ArgNo) {
John McCalla738c252011-03-09 04:27:21 +0000920 const VarDecl *Arg = *i;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000921 QualType Ty = info_it->type;
922 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000923
John McCalla738c252011-03-09 04:27:21 +0000924 bool isPromoted =
925 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
926
Daniel Dunbard3674e62008-09-11 01:48:57 +0000927 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000928 case ABIArgInfo::Indirect: {
Chris Lattner3dd716c2010-06-28 23:44:11 +0000929 llvm::Value *V = AI;
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000930
Daniel Dunbar747865a2009-02-05 09:16:39 +0000931 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000932 // Aggregates and complex variables are accessed by reference. All we
933 // need to do is realign the value, if requested
934 if (ArgI.getIndirectRealign()) {
935 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
936
937 // Copy from the incoming argument pointer to the temporary with the
938 // appropriate alignment.
939 //
940 // FIXME: We should have a common utility for generating an aggregate
941 // copy.
Benjamin Krameracc6b4e2010-12-30 00:13:21 +0000942 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyck705ba072011-01-19 01:58:38 +0000943 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumidd634362011-03-10 14:02:21 +0000944 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
945 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
946 Builder.CreateMemCpy(Dst,
947 Src,
Ken Dyck705ba072011-01-19 01:58:38 +0000948 llvm::ConstantInt::get(IntPtrTy,
949 Size.getQuantity()),
Benjamin Krameracc6b4e2010-12-30 00:13:21 +0000950 ArgI.getIndirectAlign(),
951 false);
Daniel Dunbar7b7c2932010-09-16 20:42:02 +0000952 V = AlignedTemp;
953 }
Daniel Dunbar747865a2009-02-05 09:16:39 +0000954 } else {
955 // Load scalar value from indirect argument.
Ken Dyck705ba072011-01-19 01:58:38 +0000956 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
957 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
John McCalla738c252011-03-09 04:27:21 +0000958
959 if (isPromoted)
960 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000961 }
Devang Patel68a15252011-03-03 20:13:15 +0000962 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000963 break;
964 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000965
966 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000967 case ABIArgInfo::Direct: {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000968 // If we have the trivial case, handle it with no muss and fuss.
969 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000970 ArgI.getCoerceToType() == ConvertType(Ty) &&
971 ArgI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000972 assert(AI != Fn->arg_end() && "Argument mismatch!");
973 llvm::Value *V = AI;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000974
John McCall39ec71f2010-03-27 00:47:27 +0000975 if (Arg->getType().isRestrictQualified())
976 AI->addAttr(llvm::Attribute::NoAlias);
977
John McCalla738c252011-03-09 04:27:21 +0000978 if (isPromoted)
979 V = emitArgumentDemotion(*this, Arg, V);
980
Devang Patel68a15252011-03-03 20:13:15 +0000981 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +0000982 break;
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattnerc401de92010-07-05 20:21:00 +0000985 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000986
Chris Lattnerff941a62010-07-28 18:24:28 +0000987 // The alignment we need to use is the max of the requested alignment for
988 // the argument plus the alignment required by our access code below.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000989 unsigned AlignmentToUse =
John McCallad7c5c12011-02-08 08:22:06 +0000990 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerff941a62010-07-28 18:24:28 +0000991 AlignmentToUse = std::max(AlignmentToUse,
992 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000993
Chris Lattnerff941a62010-07-28 18:24:28 +0000994 Alloca->setAlignment(AlignmentToUse);
Chris Lattnerc401de92010-07-05 20:21:00 +0000995 llvm::Value *V = Alloca;
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000996 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +0000997
Chris Lattner8a2f3c72010-07-30 04:02:24 +0000998 // If the value is offset in memory, apply the offset now.
999 if (unsigned Offs = ArgI.getDirectOffset()) {
1000 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1001 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001002 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001003 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1004 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001005
Chris Lattner15ec3612010-06-29 00:06:42 +00001006 // If the coerce-to type is a first class aggregate, we flatten it and
1007 // pass the elements. Either way is semantically identical, but fast-isel
1008 // and the optimizer generally likes scalar values better than FCAs.
1009 if (const llvm::StructType *STy =
1010 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001011 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001012
Chris Lattnerceddafb2010-07-05 20:41:41 +00001013 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1014 assert(AI != Fn->arg_end() && "Argument mismatch!");
1015 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
1016 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1017 Builder.CreateStore(AI++, EltPtr);
Chris Lattner15ec3612010-06-29 00:06:42 +00001018 }
1019 } else {
1020 // Simple case, just do a coerced store of the argument into the alloca.
1021 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner9e748e92010-06-29 00:14:52 +00001022 AI->setName(Arg->getName() + ".coerce");
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001023 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner15ec3612010-06-29 00:06:42 +00001024 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001025
1026
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001027 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +00001028 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001029 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
John McCalla738c252011-03-09 04:27:21 +00001030 if (isPromoted)
1031 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +00001032 }
Devang Patel68a15252011-03-03 20:13:15 +00001033 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner3dd716c2010-06-28 23:44:11 +00001034 continue; // Skip ++AI increment, already done.
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001035 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001036
1037 case ABIArgInfo::Expand: {
1038 // If this structure was expanded into multiple arguments then
1039 // we need to create a temporary and reconstruct it from the
1040 // arguments.
1041 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001042 llvm::Function::arg_iterator End =
Daniel Dunbar2e442a02010-08-21 03:15:20 +00001043 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel68a15252011-03-03 20:13:15 +00001044 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001045
1046 // Name the arguments used in expansion and increment AI.
1047 unsigned Index = 0;
1048 for (; AI != End; ++AI, ++Index)
1049 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1050 continue;
1051 }
1052
1053 case ABIArgInfo::Ignore:
1054 // Initialize the local variable appropriately.
1055 if (hasAggregateLLVMType(Ty))
Devang Patel68a15252011-03-03 20:13:15 +00001056 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001057 else
Devang Patel68a15252011-03-03 20:13:15 +00001058 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1059 ArgNo);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001060
1061 // Skip increment, no matching LLVM parameter.
1062 continue;
Daniel Dunbard3674e62008-09-11 01:48:57 +00001063 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001064
1065 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001066 }
1067 assert(AI == Fn->arg_end() && "Argument mismatch!");
1068}
1069
Chris Lattner3fcc7902010-06-27 01:06:27 +00001070void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001071 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +00001072 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001073 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +00001074 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001075 }
Daniel Dunbar6696e222010-06-30 21:27:58 +00001076
Dan Gohman481e40c2010-07-20 20:13:52 +00001077 llvm::DebugLoc RetDbgLoc;
Chris Lattner726b3d02010-06-26 23:13:19 +00001078 llvm::Value *RV = 0;
1079 QualType RetTy = FI.getReturnType();
1080 const ABIArgInfo &RetAI = FI.getReturnInfo();
1081
1082 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001083 case ABIArgInfo::Indirect: {
1084 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattner726b3d02010-06-26 23:13:19 +00001085 if (RetTy->isAnyComplexType()) {
1086 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1087 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1088 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1089 // Do nothing; aggregrates get evaluated directly into the destination.
1090 } else {
1091 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar03816342010-08-21 02:24:36 +00001092 false, Alignment, RetTy);
Chris Lattner726b3d02010-06-26 23:13:19 +00001093 }
1094 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001095 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001096
1097 case ABIArgInfo::Extend:
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001098 case ABIArgInfo::Direct:
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001099 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1100 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001101 // The internal return value temp always will have pointer-to-return-type
1102 // type, just do a load.
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001103
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001104 // If the instruction right before the insertion point is a store to the
1105 // return value, we can elide the load, zap the store, and usually zap the
1106 // alloca.
1107 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1108 llvm::StoreInst *SI = 0;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001109 if (InsertBB->empty() ||
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001110 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1111 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1112 RV = Builder.CreateLoad(ReturnValue);
1113 } else {
1114 // Get the stored value and nuke the now-dead store.
1115 RetDbgLoc = SI->getDebugLoc();
1116 RV = SI->getValueOperand();
1117 SI->eraseFromParent();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001118
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001119 // If that was the only use of the return value, nuke it as well now.
1120 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1121 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1122 ReturnValue = 0;
1123 }
Chris Lattner3fcc7902010-06-27 01:06:27 +00001124 }
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001125 } else {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001126 llvm::Value *V = ReturnValue;
1127 // If the value is offset in memory, apply the offset now.
1128 if (unsigned Offs = RetAI.getDirectOffset()) {
1129 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1130 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001131 V = Builder.CreateBitCast(V,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001132 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1133 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001134
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001135 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner3fcc7902010-06-27 01:06:27 +00001136 }
Chris Lattner726b3d02010-06-26 23:13:19 +00001137 break;
Chris Lattner726b3d02010-06-26 23:13:19 +00001138
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001139 case ABIArgInfo::Ignore:
Chris Lattner726b3d02010-06-26 23:13:19 +00001140 break;
1141
1142 case ABIArgInfo::Expand:
1143 assert(0 && "Invalid ABI kind for return argument");
1144 }
1145
Daniel Dunbar6696e222010-06-30 21:27:58 +00001146 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Patel65497582010-07-21 18:08:50 +00001147 if (!RetDbgLoc.isUnknown())
1148 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001149}
1150
John McCall32ea9692011-03-11 20:59:21 +00001151void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1152 const VarDecl *param) {
John McCall23f66262010-05-26 22:34:26 +00001153 // StartFunction converted the ABI-lowered parameter(s) into a
1154 // local alloca. We need to turn that into an r-value suitable
1155 // for EmitCall.
John McCall32ea9692011-03-11 20:59:21 +00001156 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall23f66262010-05-26 22:34:26 +00001157
John McCall32ea9692011-03-11 20:59:21 +00001158 QualType type = param->getType();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001159
John McCall23f66262010-05-26 22:34:26 +00001160 // For the most part, we just need to load the alloca, except:
1161 // 1) aggregate r-values are actually pointers to temporaries, and
1162 // 2) references to aggregates are pointers directly to the aggregate.
1163 // I don't know why references to non-aggregates are different here.
John McCall32ea9692011-03-11 20:59:21 +00001164 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1165 if (hasAggregateLLVMType(ref->getPointeeType()))
1166 return args.add(RValue::getAggregate(local), type);
John McCall23f66262010-05-26 22:34:26 +00001167
1168 // Locals which are references to scalars are represented
1169 // with allocas holding the pointer.
John McCall32ea9692011-03-11 20:59:21 +00001170 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall23f66262010-05-26 22:34:26 +00001171 }
1172
John McCall32ea9692011-03-11 20:59:21 +00001173 if (type->isAnyComplexType()) {
1174 ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1175 return args.add(RValue::getComplex(complex), type);
1176 }
John McCall23f66262010-05-26 22:34:26 +00001177
John McCall32ea9692011-03-11 20:59:21 +00001178 if (hasAggregateLLVMType(type))
1179 return args.add(RValue::getAggregate(local), type);
John McCall23f66262010-05-26 22:34:26 +00001180
John McCall32ea9692011-03-11 20:59:21 +00001181 unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1182 llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1183 return args.add(RValue::get(value), type);
John McCall23f66262010-05-26 22:34:26 +00001184}
1185
John McCall32ea9692011-03-11 20:59:21 +00001186void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1187 QualType type) {
1188 if (type->isReferenceType())
1189 return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1190 type);
Mike Stump11289f42009-09-09 15:08:12 +00001191
Eli Friedmandf968192011-05-26 00:10:27 +00001192 if (hasAggregateLLVMType(type) && isa<ImplicitCastExpr>(E) &&
1193 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
1194 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
1195 assert(L.isSimple());
1196 args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()),
1197 type, /*NeedsCopy*/true);
1198 return;
1199 }
1200
John McCall32ea9692011-03-11 20:59:21 +00001201 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson60ce3fe2009-04-08 20:47:54 +00001202}
1203
John McCallbd309292010-07-06 01:34:17 +00001204/// Emits a call or invoke instruction to the given function, depending
1205/// on the current state of the EH stack.
1206llvm::CallSite
1207CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1208 llvm::Value * const *ArgBegin,
1209 llvm::Value * const *ArgEnd,
1210 const llvm::Twine &Name) {
1211 llvm::BasicBlock *InvokeDest = getInvokeDest();
1212 if (!InvokeDest)
1213 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1214
1215 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1216 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1217 ArgBegin, ArgEnd, Name);
1218 EmitBlock(ContBB);
1219 return Invoke;
1220}
1221
Daniel Dunbard931a872009-02-02 22:03:45 +00001222RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001223 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00001224 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001225 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00001226 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00001227 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00001228 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00001229 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001230
1231 // Handle struct-return functions by passing a pointer to the
1232 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001233 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001234 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001235
1236
Chris Lattner4ca97c32009-06-13 00:26:38 +00001237 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00001238 // alloca to hold the result, unless one is given to us.
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001239 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001240 llvm::Value *Value = ReturnValue.getValue();
1241 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +00001242 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +00001243 Args.push_back(Value);
1244 }
Mike Stump11289f42009-09-09 15:08:12 +00001245
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001246 assert(CallInfo.arg_size() == CallArgs.size() &&
1247 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001248 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001249 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001250 I != E; ++I, ++info_it) {
1251 const ABIArgInfo &ArgInfo = info_it->info;
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001252 RValue RV = I->RV;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001253
Daniel Dunbar03816342010-08-21 02:24:36 +00001254 unsigned Alignment =
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001255 getContext().getTypeAlignInChars(I->Ty).getQuantity();
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001256 switch (ArgInfo.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001257 case ABIArgInfo::Indirect: {
Daniel Dunbar747865a2009-02-05 09:16:39 +00001258 if (RV.isScalar() || RV.isComplex()) {
1259 // Make a temporary alloca to pass the argument.
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001260 Args.push_back(CreateMemTemp(I->Ty));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001261 if (RV.isScalar())
Daniel Dunbar03816342010-08-21 02:24:36 +00001262 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001263 Alignment, I->Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001264 else
Mike Stump11289f42009-09-09 15:08:12 +00001265 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Eli Friedmandf968192011-05-26 00:10:27 +00001266 } else if (I->NeedsCopy && !ArgInfo.getIndirectByVal()) {
1267 Args.push_back(CreateMemTemp(I->Ty));
1268 EmitAggregateCopy(Args.back(), RV.getAggregateAddr(), I->Ty,
1269 RV.isVolatileQualified());
Daniel Dunbar747865a2009-02-05 09:16:39 +00001270 } else {
1271 Args.push_back(RV.getAggregateAddr());
1272 }
1273 break;
Daniel Dunbar03816342010-08-21 02:24:36 +00001274 }
Daniel Dunbar747865a2009-02-05 09:16:39 +00001275
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001276 case ABIArgInfo::Ignore:
1277 break;
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001278
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001279 case ABIArgInfo::Extend:
1280 case ABIArgInfo::Direct: {
1281 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001282 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1283 ArgInfo.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001284 if (RV.isScalar())
1285 Args.push_back(RV.getScalarVal());
1286 else
1287 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1288 break;
1289 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001290
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001291 // FIXME: Avoid the conversion through memory if possible.
1292 llvm::Value *SrcPtr;
1293 if (RV.isScalar()) {
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001294 SrcPtr = CreateMemTemp(I->Ty, "coerce");
1295 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment, I->Ty);
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001296 } else if (RV.isComplex()) {
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001297 SrcPtr = CreateMemTemp(I->Ty, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001298 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001299 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001300 SrcPtr = RV.getAggregateAddr();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001301
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001302 // If the value is offset in memory, apply the offset now.
1303 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1304 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1305 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001306 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001307 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1308
1309 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001310
Chris Lattner3dd716c2010-06-28 23:44:11 +00001311 // If the coerce-to type is a first class aggregate, we flatten it and
1312 // pass the elements. Either way is semantically identical, but fast-isel
1313 // and the optimizer generally likes scalar values better than FCAs.
1314 if (const llvm::StructType *STy =
Chris Lattner15ec3612010-06-29 00:06:42 +00001315 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattnerceddafb2010-07-05 20:41:41 +00001316 SrcPtr = Builder.CreateBitCast(SrcPtr,
1317 llvm::PointerType::getUnqual(STy));
1318 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1319 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerff941a62010-07-28 18:24:28 +00001320 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1321 // We don't know what we're loading from.
1322 LI->setAlignment(1);
1323 Args.push_back(LI);
Chris Lattner15ec3612010-06-29 00:06:42 +00001324 }
Chris Lattner3dd716c2010-06-28 23:44:11 +00001325 } else {
Chris Lattner15ec3612010-06-29 00:06:42 +00001326 // In the simple case, just pass the coerced loaded value.
1327 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1328 *this));
Chris Lattner3dd716c2010-06-28 23:44:11 +00001329 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001330
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001331 break;
1332 }
1333
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001334 case ABIArgInfo::Expand:
Eli Friedmanf4258eb2011-05-02 18:05:27 +00001335 ExpandTypeToArgs(I->Ty, RV, Args);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001336 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001337 }
1338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Chris Lattner4ca97c32009-06-13 00:26:38 +00001340 // If the callee is a bitcast of a function to a varargs pointer to function
1341 // type, check to see if we can remove the bitcast. This handles some cases
1342 // with unprototyped functions.
1343 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1344 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1345 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1346 const llvm::FunctionType *CurFT =
1347 cast<llvm::FunctionType>(CurPT->getElementType());
1348 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001349
Chris Lattner4ca97c32009-06-13 00:26:38 +00001350 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1351 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00001352 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahaniancf7f66f2011-03-01 17:28:13 +00001353 ActualFT->getNumParams() == Args.size() &&
1354 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00001355 bool ArgsMatch = true;
1356 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1357 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1358 ArgsMatch = false;
1359 break;
1360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattner4ca97c32009-06-13 00:26:38 +00001362 // Strip the cast if we can get away with it. This is a nice cleanup,
1363 // but also allows us to inline the function at -O0 if it is marked
1364 // always_inline.
1365 if (ArgsMatch)
1366 Callee = CalleeF;
1367 }
1368 }
Mike Stump11289f42009-09-09 15:08:12 +00001369
Daniel Dunbar613855c2008-09-09 23:27:19 +00001370
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001371 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00001372 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001373 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +00001374 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1375 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +00001376
John McCallbd309292010-07-06 01:34:17 +00001377 llvm::BasicBlock *InvokeDest = 0;
1378 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1379 InvokeDest = getInvokeDest();
1380
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001381 llvm::CallSite CS;
John McCallbd309292010-07-06 01:34:17 +00001382 if (!InvokeDest) {
Jay Foad7d0479f2009-05-21 09:52:38 +00001383 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001384 } else {
1385 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +00001386 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +00001387 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001388 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001389 }
Chris Lattnere70a0072010-06-29 16:40:28 +00001390 if (callOrInvoke)
David Chisnallff5f88c2010-05-02 13:41:58 +00001391 *callOrInvoke = CS.getInstruction();
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001392
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001393 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001394 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001395
1396 // If the call doesn't return, finish the basic block and clear the
1397 // insertion point; this allows the rest of IRgen to discard
1398 // unreachable code.
1399 if (CS.doesNotReturn()) {
1400 Builder.CreateUnreachable();
1401 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001402
Mike Stump18bb9282009-05-16 07:57:57 +00001403 // FIXME: For now, emit a dummy basic block because expr emitters in
1404 // generally are not ready to handle emitting expressions at unreachable
1405 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001406 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001407
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001408 // Return a reasonable RValue.
1409 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001410 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001411
1412 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001413 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001414 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001415
1416 switch (RetAI.getKind()) {
Daniel Dunbar03816342010-08-21 02:24:36 +00001417 case ABIArgInfo::Indirect: {
1418 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001419 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001420 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001421 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001422 return RValue::getAggregate(Args[0]);
Daniel Dunbar03816342010-08-21 02:24:36 +00001423 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1424 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001425
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001426 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001427 // If we are ignoring an argument that had a result, make sure to
1428 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001429 return GetUndefRValue(RetTy);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001430
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001431 case ABIArgInfo::Extend:
1432 case ABIArgInfo::Direct: {
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001433 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1434 RetAI.getDirectOffset() == 0) {
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001435 if (RetTy->isAnyComplexType()) {
1436 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1437 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1438 return RValue::getComplex(std::make_pair(Real, Imag));
1439 }
1440 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1441 llvm::Value *DestPtr = ReturnValue.getValue();
1442 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001443
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001444 if (!DestPtr) {
1445 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1446 DestIsVolatile = false;
1447 }
Eli Friedmanaf9b3252011-05-17 21:08:01 +00001448 BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
Chris Lattnerfe34c1d2010-07-29 06:26:06 +00001449 return RValue::getAggregate(DestPtr);
1450 }
1451 return RValue::get(CI);
1452 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001453
Anders Carlsson17490832009-12-24 20:40:36 +00001454 llvm::Value *DestPtr = ReturnValue.getValue();
1455 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001456
Anders Carlsson17490832009-12-24 20:40:36 +00001457 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001458 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001459 DestIsVolatile = false;
1460 }
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001461
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001462 // If the value is offset in memory, apply the offset now.
1463 llvm::Value *StorePtr = DestPtr;
1464 if (unsigned Offs = RetAI.getDirectOffset()) {
1465 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1466 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001467 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner8a2f3c72010-07-30 04:02:24 +00001468 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1469 }
1470 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencerf5a1fbc2010-10-19 06:39:39 +00001471
Daniel Dunbar03816342010-08-21 02:24:36 +00001472 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001473 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001474 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001475 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001476 return RValue::getAggregate(DestPtr);
Daniel Dunbar03816342010-08-21 02:24:36 +00001477 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001478 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001479
Daniel Dunbard3674e62008-09-11 01:48:57 +00001480 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001481 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001482 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001483
1484 assert(0 && "Unhandled ABIArgInfo::Kind");
1485 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001486}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001487
1488/* VarArg handling */
1489
1490llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1491 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1492}