blob: fba89a76fc65df9482f1a020c3a7cc128a672dad [file] [log] [blame]
Daniel Dunbar0dbe2272008-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 McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Chris Lattnerce933992010-06-29 16:40:28 +000017#include "ABIInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000018#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000019#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000020#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000021#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000022#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000023#include "clang/AST/DeclObjC.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000024#include "clang/Frontend/CodeGenOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000025#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000026#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000027#include "llvm/Target/TargetData.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000028using namespace clang;
29using namespace CodeGen;
30
31/***/
32
John McCall04a67a62010-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 Gregorf813a2c2010-05-18 16:57:00 +000038 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
Anton Korobeynikov414d8962011-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 Perchik52fc3142010-09-03 01:29:35 +000041 // TODO: add support for CC_X86Pascal to llvm
John McCall04a67a62010-02-05 21:31:56 +000042 }
43}
44
John McCall0b0ef0a2010-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 McCallead608a2010-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 Dunbar45c25ba2008-09-10 04:01:49 +000051}
52
John McCall0b0ef0a2010-02-24 07:14:12 +000053/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000054static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
55 return MD->getType()->getCanonicalTypeUnqualified()
56 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-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 McCallead608a2010-02-26 00:48:12 +000063static CanQualType GetReturnType(QualType RetTy) {
64 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000065}
66
67const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000068CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
69 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000070 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
71 llvm::SmallVector<CanQualType, 16>(),
Chris Lattnerbcaedae2010-06-30 19:14:05 +000072 FTNP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-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 McCallead608a2010-02-26 00:48:12 +000078 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +000079 CanQual<FunctionProtoType> FTP,
80 bool IsRecursive = false) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000082 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000083 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000084 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Tilmann Scheller9c6082f2011-03-02 21:36:49 +000085 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000086}
87
88const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000089CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
90 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000091 llvm::SmallVector<CanQualType, 16> ArgTys;
Tilmann Scheller9c6082f2011-03-02 21:36:49 +000092 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000093}
94
John McCall04a67a62010-02-05 21:31:56 +000095static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000096 // Set the appropriate calling convention for the Function.
97 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000098 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000099
100 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +0000101 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000102
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000103 if (D->hasAttr<ThisCallAttr>())
104 return CC_X86ThisCall;
105
Dawn Perchik52fc3142010-09-03 01:29:35 +0000106 if (D->hasAttr<PascalAttr>())
107 return CC_X86Pascal;
108
Anton Korobeynikov414d8962011-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 McCall04a67a62010-02-05 21:31:56 +0000112 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000113}
114
Anders Carlsson375c31c2009-10-03 19:43:08 +0000115const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
116 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000117 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000118
Anders Carlsson375c31c2009-10-03 19:43:08 +0000119 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000120 ArgTys.push_back(GetThisType(Context, RD));
121
122 return ::getFunctionInfo(*this, ArgTys,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000123 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000124}
125
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000126const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000127 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000128
John McCallfc400282010-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 Lattner3eb67ca2009-05-12 20:27:19 +0000132 // Add the 'this' pointer unless this is a static method.
133 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000134 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000136 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000137}
138
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000139const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000140 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000141 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000142 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000143 CanQualType ResTy = Context.VoidTy;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000144
John McCall4c40d982010-08-31 07:33:07 +0000145 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall0b0ef0a2010-02-24 07:14:12 +0000146
John McCall4c40d982010-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 Scheller9c6082f2011-03-02 21:36:49 +0000153 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000154}
155
156const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
157 CXXDtorType Type) {
John McCall4c40d982010-08-31 07:33:07 +0000158 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCallead608a2010-02-26 00:48:12 +0000159 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000160 CanQualType ResTy = Context.VoidTy;
John McCall0b0ef0a2010-02-24 07:14:12 +0000161
John McCall4c40d982010-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 Scheller9c6082f2011-03-02 21:36:49 +0000167 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000168}
169
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000170const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000171 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000172 if (MD->isInstance())
173 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000174
John McCallead608a2010-02-26 00:48:12 +0000175 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
176 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000177 if (isa<FunctionNoProtoType>(FTy))
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000178 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
John McCallead608a2010-02-26 00:48:12 +0000179 assert(isa<FunctionProtoType>(FTy));
180 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000181}
182
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000183const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-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 Dunbar541b63b2009-02-02 23:23:47 +0000187 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000188 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-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 Espindola264ba482010-03-30 20:24:48 +0000194 FunctionType::ExtInfo(
195 /*NoReturn*/ false,
Eli Friedmana49218e2011-04-09 08:18:08 +0000196 /*HasRegParm*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000197 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000198 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000199}
200
Anders Carlssonb2bcf1c2010-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. Spencer9cac4942010-10-19 06:39:39 +0000204
Anders Carlssonb2bcf1c2010-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. Spencer9cac4942010-10-19 06:39:39 +0000210
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000211 return getFunctionInfo(FD);
212}
213
Mike Stump1eb44332009-09-09 15:08:12 +0000214const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000215 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000216 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000217 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000218 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000219 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000220 i != e; ++i)
Eli Friedmanc6d07822011-05-02 18:05:27 +0000221 ArgTys.push_back(Context.getCanonicalParamType(i->Ty));
Rafael Espindola264ba482010-03-30 20:24:48 +0000222 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000223}
224
Mike Stump1eb44332009-09-09 15:08:12 +0000225const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000226 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000227 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000228 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000229 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000230 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000231 i != e; ++i)
John McCalld26bc762011-03-09 04:27:21 +0000232 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
Rafael Espindola264ba482010-03-30 20:24:48 +0000233 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000234}
235
John McCalld26bc762011-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 McCallead608a2010-02-26 00:48:12 +0000241const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
242 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000243 const FunctionType::ExtInfo &Info,
244 bool IsRecursive) {
John McCallead608a2010-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 Espindola425ef722010-03-30 22:15:11 +0000251 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000252
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000253 // Lookup or create unique function info.
254 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000255 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000256 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-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 Dunbar88c2fa92009-02-03 05:31:23 +0000263 // Construct the function info.
Eli Friedmana49218e2011-04-09 08:18:08 +0000264 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getHasRegParm(), Info.getRegParm(), ResTy,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000265 ArgTys.data(), ArgTys.size());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000266 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000267
268 // Compute ABI information.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000269 getABIInfo().computeInfo(*FI);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000270
Chris Lattner800588f2010-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. Spencer9cac4942010-10-19 06:39:39 +0000277
Chris Lattner800588f2010-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 Dunbar88c2fa92009-02-03 05:31:23 +0000282
Chris Lattnera9fa8582010-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 Lattneree5dcd02010-07-29 02:31:05 +0000286 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattnera9fa8582010-07-01 06:20:47 +0000287 HandleLateResolvedPointers();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000288
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000289 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000290}
291
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000292CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Eli Friedmana49218e2011-04-09 08:18:08 +0000293 bool _NoReturn, bool _HasRegParm, unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000294 CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000295 const CanQualType *ArgTys,
296 unsigned NumArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000297 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000298 EffectiveCallingConvention(_CallingConvention),
Eli Friedmana49218e2011-04-09 08:18:08 +0000299 NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000300{
Chris Lattnerbb521142010-06-29 18:13:52 +0000301 NumArgs = NumArgTys;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000302
Chris Lattnerce700162010-06-28 23:44:11 +0000303 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattnerbb521142010-06-29 18:13:52 +0000304 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000305 Args[0].type = ResTy;
Chris Lattnerbb521142010-06-29 18:13:52 +0000306 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000307 Args[1 + i].type = ArgTys[i];
308}
309
310/***/
311
John McCall42e06112011-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 Dunbar56273772008-09-17 00:51:38 +0000316 assert(RT && "Can only expand structure types.");
317 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000318 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000319 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000321 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
322 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000323 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000324 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000325 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000326
John McCall42e06112011-05-15 02:19:42 +0000327 QualType fieldType = FD->getType();
328 if (fieldType->isRecordType())
329 GetExpandedTypes(fieldType, expandedTypes, isRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000330 else
John McCall42e06112011-05-15 02:19:42 +0000331 expandedTypes.push_back(ConvertType(fieldType, isRecursive));
Daniel Dunbar56273772008-09-17 00:51:38 +0000332 }
333}
334
Mike Stump1eb44332009-09-09 15:08:12 +0000335llvm::Function::arg_iterator
Daniel Dunbar56273772008-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 Stump1eb44332009-09-09 15:08:12 +0000342 assert(LV.isSimple() &&
343 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000344 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000345 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
346 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000347 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000348 QualType FT = FD->getType();
349
350 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000351 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-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 Stump1eb44332009-09-09 15:08:12 +0000363void
364CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-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 Kyrtzidis17945a02009-06-30 02:36:12 +0000372 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
373 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000374 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000375 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Daniel Dunbar56273772008-09-17 00:51:38 +0000377 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000378 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-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 Stump1eb44332009-09-09 15:08:12 +0000383 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000384 "Unexpected non-scalar rvalue during struct expansion.");
385 Args.push_back(RV.getScalarVal());
386 }
387 }
388}
389
Chris Lattnere7bb7772010-06-27 06:04:18 +0000390/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-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 Lattnere7bb7772010-06-27 06:04:18 +0000395EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
396 const llvm::StructType *SrcSTy,
397 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000398 // We can't dive into a zero-element struct.
399 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000400
Chris Lattner08dd2a02010-06-27 05:56:15 +0000401 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000402
Chris Lattner08dd2a02010-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. Spencer9cac4942010-10-19 06:39:39 +0000405 uint64_t FirstEltSize =
Chris Lattner08dd2a02010-06-27 05:56:15 +0000406 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000407 if (FirstEltSize < DstSize &&
Chris Lattner08dd2a02010-06-27 05:56:15 +0000408 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
409 return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000410
Chris Lattner08dd2a02010-06-27 05:56:15 +0000411 // GEP into the first element.
412 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000413
Chris Lattner08dd2a02010-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 Lattnere7bb7772010-06-27 06:04:18 +0000418 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000419
420 return SrcPtr;
421}
422
Chris Lattner6d11cdb2010-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. Spencer9cac4942010-10-19 06:39:39 +0000431
Chris Lattner6d11cdb2010-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. Spencer9cac4942010-10-19 06:39:39 +0000436
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000437 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000438 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000439 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000440
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000441 const llvm::Type *DestIntTy = Ty;
442 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000443 DestIntTy = CGF.IntPtrTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000444
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000445 if (Val->getType() != DestIntTy)
446 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000447
Chris Lattner6d11cdb2010-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 Lattner08dd2a02010-06-27 05:56:15 +0000453
454
Daniel Dunbar275e10d2009-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 Stump1eb44332009-09-09 15:08:12 +0000464 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000465 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000466
Chris Lattner6ae00692010-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. Spencer9cac4942010-10-19 06:39:39 +0000470
Duncan Sands9408c452009-05-09 07:08:47 +0000471 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000472
Chris Lattner08dd2a02010-06-27 05:56:15 +0000473 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000474 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000475 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
476 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000477
Chris Lattner08dd2a02010-06-27 05:56:15 +0000478 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000479
Chris Lattner6d11cdb2010-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. Spencer9cac4942010-10-19 06:39:39 +0000487
Daniel Dunbarb225be42009-02-03 05:59:18 +0000488 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000489 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-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 Dunbar7ef455b2009-05-13 18:54:26 +0000493 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000494 // FIXME: Assert that we aren't truncating non-padding bits when have access
495 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000496 llvm::Value *Casted =
497 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-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 Dunbar275e10d2009-02-02 19:06:38 +0000502 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000503
Chris Lattner35b21b82010-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 Dunbar275e10d2009-02-02 19:06:38 +0000514}
515
Eli Friedmanbadea572011-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 Dunbar275e10d2009-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 Carlssond2490a92009-12-24 20:40:36 +0000546 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000547 CodeGenFunction &CGF) {
548 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000549 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000550 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000551 if (SrcTy == DstTy) {
552 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
553 return;
554 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000555
Chris Lattner6ae00692010-06-28 22:51:39 +0000556 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000557
Chris Lattnere7bb7772010-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. Spencer9cac4942010-10-19 06:39:39 +0000562
Chris Lattner6d11cdb2010-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. Spencer9cac4942010-10-19 06:39:39 +0000571
Duncan Sands9408c452009-05-09 07:08:47 +0000572 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000573
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000574 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000575 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000576 llvm::Value *Casted =
577 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000578 // FIXME: Use better alignment / avoid requiring aligned store.
Eli Friedmanbadea572011-05-17 21:08:01 +0000579 BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000580 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000581 // Otherwise do coercion through memory. This is stupid, but
582 // simple.
Daniel Dunbarfdf49862009-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 Dunbar275e10d2009-02-02 19:06:38 +0000590 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
591 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000592 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000593 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-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 Carlssond2490a92009-12-24 20:40:36 +0000597 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000598 }
599}
600
Daniel Dunbar56273772008-09-17 00:51:38 +0000601/***/
602
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000603bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000604 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000605}
606
Daniel Dunbardacf9dd2010-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 McCallc0bf4622010-02-23 00:48:20 +0000625const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
626 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000627
John McCallc0bf4622010-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 Lattnerbcaedae2010-06-30 19:14:05 +0000634 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000635}
636
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000637const llvm::FunctionType *
John McCall42e06112011-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 Dunbar45c25ba2008-09-10 04:01:49 +0000642
John McCall42e06112011-05-15 02:19:42 +0000643 const ABIArgInfo &retAI = FI.getReturnInfo();
644 switch (retAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000645 case ABIArgInfo::Expand:
John McCall42e06112011-05-15 02:19:42 +0000646 llvm_unreachable("Invalid ABI kind for return argument");
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000647
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000648 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000649 case ABIArgInfo::Direct:
John McCall42e06112011-05-15 02:19:42 +0000650 resultType = retAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000651 break;
652
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000653 case ABIArgInfo::Indirect: {
John McCall42e06112011-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 Dunbar45c25ba2008-09-10 04:01:49 +0000661 break;
662 }
663
Daniel Dunbar11434922009-01-26 21:26:08 +0000664 case ABIArgInfo::Ignore:
John McCall42e06112011-05-15 02:19:42 +0000665 resultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000666 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
669 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000670 ie = FI.arg_end(); it != ie; ++it) {
John McCall42e06112011-05-15 02:19:42 +0000671 const ABIArgInfo &argAI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
John McCall42e06112011-05-15 02:19:42 +0000673 switch (argAI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000674 case ABIArgInfo::Ignore:
675 break;
676
Chris Lattner800588f2010-07-29 06:26:06 +0000677 case ABIArgInfo::Indirect: {
678 // indirect arguments are always on the stack, which is addr space #0.
John McCall42e06112011-05-15 02:19:42 +0000679 const llvm::Type *LTy = ConvertTypeForMem(it->type, isRecursive);
680 argTypes.push_back(LTy->getPointerTo());
Chris Lattner800588f2010-07-29 06:26:06 +0000681 break;
682 }
683
684 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000685 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-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 McCall42e06112011-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 Lattnerce700162010-06-28 23:44:11 +0000693 } else {
John McCall42e06112011-05-15 02:19:42 +0000694 argTypes.push_back(argType);
Chris Lattnerce700162010-06-28 23:44:11 +0000695 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000696 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000699 case ABIArgInfo::Expand:
John McCall42e06112011-05-15 02:19:42 +0000700 GetExpandedTypes(it->type, argTypes, isRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000701 break;
702 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000703 }
704
John McCall42e06112011-05-15 02:19:42 +0000705 return llvm::FunctionType::get(resultType, argTypes, isVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000706}
707
John McCall4c40d982010-08-31 07:33:07 +0000708const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
709 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000710 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000711
John McCall4c40d982010-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 Carlssonecf282b2009-11-24 05:08:52 +0000720
721 return llvm::OpaqueType::get(getLLVMContext());
722}
723
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000724void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000725 const Decl *TargetDecl,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000726 AttributeListType &PAL,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000727 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000728 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000729 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000730
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000731 CallingConv = FI.getEffectiveCallingConvention();
732
John McCall04a67a62010-02-05 21:31:56 +0000733 if (FI.isNoReturn())
734 FuncAttrs |= llvm::Attribute::NoReturn;
735
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000736 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000737 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000738 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000739 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000740 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
741 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000742 if (FPT && FPT->isNothrow(getContext()))
John McCall9c0c1f32010-07-08 06:48:12 +0000743 FuncAttrs |= llvm::Attribute::NoUnwind;
744 }
745
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000746 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000747 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000748 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000749 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000750 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000751 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000752 if (TargetDecl->hasAttr<MallocAttr>())
753 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000754 }
755
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000756 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000757 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000758 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000759 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000760 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000761 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000762
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000763 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000764 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000765 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000766 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000767 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000768 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000769 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000770 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000771 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000772 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000773 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000774 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000775 break;
776
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000777 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000778 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000779 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000780 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000781 // sret disables readnone and readonly
782 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
783 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000784 break;
785
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000786 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000787 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000788 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000789
Devang Patela2c69122008-09-26 22:53:57 +0000790 if (RetAttrs)
791 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000792
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000793 // FIXME: RegParm should be reduced in case of global register variable.
Eli Friedmana49218e2011-04-09 08:18:08 +0000794 signed RegParm;
795 if (FI.getHasRegParm())
796 RegParm = FI.getRegParm();
797 else
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000798 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000799
800 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000801 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-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 Patel761d7f72008-09-25 21:02:23 +0000805 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000806
John McCalld8e10d22010-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 Dunbar7f6890e2011-02-10 18:10:07 +0000809 // sense to do it here because parameters are so messed up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000810 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000811 case ABIArgInfo::Extend:
812 if (ParamType->isSignedIntegerType())
813 Attributes |= llvm::Attribute::SExt;
814 else if (ParamType->isUnsignedIntegerType())
815 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. Spencer9cac4942010-10-19 06:39:39 +0000826
Chris Lattnerce700162010-06-28 23:44:11 +0000827 if (const llvm::StructType *STy =
Chris Lattner800588f2010-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 Dunbar89c9d8e2009-02-03 19:12:28 +0000831
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000832 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000833 if (AI.getIndirectByVal())
834 Attributes |= llvm::Attribute::ByVal;
835
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000836 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000837 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000838 // byval disables readnone and readonly.
839 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
840 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000841 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000842
Daniel Dunbar11434922009-01-26 21:26:08 +0000843 case ABIArgInfo::Ignore:
844 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000845 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000846
Daniel Dunbar56273772008-09-17 00:51:38 +0000847 case ABIArgInfo::Expand: {
John McCall42e06112011-05-15 02:19:42 +0000848 llvm::SmallVector<const llvm::Type*, 8> types;
Mike Stumpf5408fe2009-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 McCall42e06112011-05-15 02:19:42 +0000852 getTypes().GetExpandedTypes(ParamType, types, false);
853 Index += types.size();
Daniel Dunbar56273772008-09-17 00:51:38 +0000854 continue;
855 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Devang Patel761d7f72008-09-25 21:02:23 +0000858 if (Attributes)
859 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000860 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000861 }
Devang Patela2c69122008-09-26 22:53:57 +0000862 if (FuncAttrs)
863 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000864}
865
John McCalld26bc762011-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 Dunbar88b53962009-02-02 22:03:45 +0000886void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
887 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000888 const FunctionArgList &Args) {
John McCall0cfeb632009-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 Lattner121b3fa2010-07-05 20:21:00 +0000893 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-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 Andersonc9c88b42009-07-31 20:28:54 +0000897 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000898 Builder.CreateStore(Zero, ReturnValue);
899 }
900 }
901
Mike Stumpf5408fe2009-05-16 07:57:57 +0000902 // FIXME: We no longer need the types from FunctionArgList; lift up and
903 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000904
Daniel Dunbar17b708d2008-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 Stump1eb44332009-09-09 15:08:12 +0000907
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000908 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000909 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000910 AI->setName("agg.result");
911 ++AI;
912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000914 assert(FI.arg_size() == Args.size() &&
915 "Mismatch between function signature & arguments.");
Devang Patel093ac462011-03-03 20:13:15 +0000916 unsigned ArgNo = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000917 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel093ac462011-03-03 20:13:15 +0000918 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
919 i != e; ++i, ++info_it, ++ArgNo) {
John McCalld26bc762011-03-09 04:27:21 +0000920 const VarDecl *Arg = *i;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000921 QualType Ty = info_it->type;
922 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000923
John McCalld26bc762011-03-09 04:27:21 +0000924 bool isPromoted =
925 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
926
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000927 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000928 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000929 llvm::Value *V = AI;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000930
Daniel Dunbar1f745982009-02-05 09:16:39 +0000931 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbarcf3b6f22010-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 Kramer9f0c7cc2010-12-30 00:13:21 +0000942 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyckfe710082011-01-19 01:58:38 +0000943 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumic95a8fc2011-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 Dyckfe710082011-01-19 01:58:38 +0000948 llvm::ConstantInt::get(IntPtrTy,
949 Size.getQuantity()),
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000950 ArgI.getIndirectAlign(),
951 false);
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000952 V = AlignedTemp;
953 }
Daniel Dunbar1f745982009-02-05 09:16:39 +0000954 } else {
955 // Load scalar value from indirect argument.
Ken Dyckfe710082011-01-19 01:58:38 +0000956 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
957 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
John McCalld26bc762011-03-09 04:27:21 +0000958
959 if (isPromoted)
960 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000961 }
Devang Patel093ac462011-03-03 20:13:15 +0000962 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000963 break;
964 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000965
966 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000967 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-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 Lattner117e3f42010-07-30 04:02:24 +0000970 ArgI.getCoerceToType() == ConvertType(Ty) &&
971 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000972 assert(AI != Fn->arg_end() && "Argument mismatch!");
973 llvm::Value *V = AI;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000974
John McCalld8e10d22010-03-27 00:47:27 +0000975 if (Arg->getType().isRestrictQualified())
976 AI->addAttr(llvm::Attribute::NoAlias);
977
John McCalld26bc762011-03-09 04:27:21 +0000978 if (isPromoted)
979 V = emitArgumentDemotion(*this, Arg, V);
980
Devang Patel093ac462011-03-03 20:13:15 +0000981 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +0000982 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattner121b3fa2010-07-05 20:21:00 +0000985 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000986
Chris Lattnerdeabde22010-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. Spencer9cac4942010-10-19 06:39:39 +0000989 unsigned AlignmentToUse =
John McCalld16c2cf2011-02-08 08:22:06 +0000990 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerdeabde22010-07-28 18:24:28 +0000991 AlignmentToUse = std::max(AlignmentToUse,
992 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000993
Chris Lattnerdeabde22010-07-28 18:24:28 +0000994 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000995 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000996 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000997
Chris Lattner117e3f42010-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. Spencer9cac4942010-10-19 06:39:39 +00001002 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001003 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1004 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001005
Chris Lattner309c59f2010-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 Lattner92826882010-07-05 20:41:41 +00001011 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001012
Chris Lattner92826882010-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 Lattner309c59f2010-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 Lattner225e2862010-06-29 00:14:52 +00001022 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +00001023 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +00001024 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001025
1026
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001027 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001028 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001029 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
John McCalld26bc762011-03-09 04:27:21 +00001030 if (isPromoted)
1031 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001032 }
Devang Patel093ac462011-03-03 20:13:15 +00001033 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerce700162010-06-28 23:44:11 +00001034 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001035 }
Chris Lattner800588f2010-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 Lattner800588f2010-07-29 06:26:06 +00001042 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +00001043 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel093ac462011-03-03 20:13:15 +00001044 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattner800588f2010-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 Patel093ac462011-03-03 20:13:15 +00001056 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001057 else
Devang Patel093ac462011-03-03 20:13:15 +00001058 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1059 ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001060
1061 // Skip increment, no matching LLVM parameter.
1062 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001063 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001064
1065 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001066 }
1067 assert(AI == Fn->arg_end() && "Argument mismatch!");
1068}
1069
Chris Lattner35b21b82010-06-27 01:06:27 +00001070void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001071 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001072 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001073 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001074 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001075 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001076
Dan Gohman4751a532010-07-20 20:13:52 +00001077 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-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 Dunbar91a16fa2010-08-21 02:24:36 +00001083 case ABIArgInfo::Indirect: {
1084 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-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 Dunbar91a16fa2010-08-21 02:24:36 +00001092 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001093 }
1094 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001095 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001096
1097 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001098 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001099 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1100 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-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. Spencer9cac4942010-10-19 06:39:39 +00001103
Chris Lattner800588f2010-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. Spencer9cac4942010-10-19 06:39:39 +00001109 if (InsertBB->empty() ||
Chris Lattner800588f2010-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. Spencer9cac4942010-10-19 06:39:39 +00001118
Chris Lattner800588f2010-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 Lattner35b21b82010-06-27 01:06:27 +00001124 }
Chris Lattner800588f2010-07-29 06:26:06 +00001125 } else {
Chris Lattner117e3f42010-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. Spencer9cac4942010-10-19 06:39:39 +00001131 V = Builder.CreateBitCast(V,
Chris Lattner117e3f42010-07-30 04:02:24 +00001132 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1133 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001134
Chris Lattner117e3f42010-07-30 04:02:24 +00001135 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001136 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001137 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001138
Chris Lattner800588f2010-07-29 06:26:06 +00001139 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001140 break;
1141
1142 case ABIArgInfo::Expand:
1143 assert(0 && "Invalid ABI kind for return argument");
1144 }
1145
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001146 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001147 if (!RetDbgLoc.isUnknown())
1148 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001149}
1150
John McCall413ebdb2011-03-11 20:59:21 +00001151void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1152 const VarDecl *param) {
John McCall27360712010-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 McCall413ebdb2011-03-11 20:59:21 +00001156 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall27360712010-05-26 22:34:26 +00001157
John McCall413ebdb2011-03-11 20:59:21 +00001158 QualType type = param->getType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001159
John McCall27360712010-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 McCall413ebdb2011-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 McCall27360712010-05-26 22:34:26 +00001167
1168 // Locals which are references to scalars are represented
1169 // with allocas holding the pointer.
John McCall413ebdb2011-03-11 20:59:21 +00001170 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall27360712010-05-26 22:34:26 +00001171 }
1172
John McCall413ebdb2011-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 McCall27360712010-05-26 22:34:26 +00001177
John McCall413ebdb2011-03-11 20:59:21 +00001178 if (hasAggregateLLVMType(type))
1179 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001180
John McCall413ebdb2011-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 McCall27360712010-05-26 22:34:26 +00001184}
1185
John McCall413ebdb2011-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 Stump1eb44332009-09-09 15:08:12 +00001191
John McCall413ebdb2011-03-11 20:59:21 +00001192 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson0139bb92009-04-08 20:47:54 +00001193}
1194
John McCallf1549f62010-07-06 01:34:17 +00001195/// Emits a call or invoke instruction to the given function, depending
1196/// on the current state of the EH stack.
1197llvm::CallSite
1198CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1199 llvm::Value * const *ArgBegin,
1200 llvm::Value * const *ArgEnd,
1201 const llvm::Twine &Name) {
1202 llvm::BasicBlock *InvokeDest = getInvokeDest();
1203 if (!InvokeDest)
1204 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1205
1206 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1207 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1208 ArgBegin, ArgEnd, Name);
1209 EmitBlock(ContBB);
1210 return Invoke;
1211}
1212
Daniel Dunbar88b53962009-02-02 22:03:45 +00001213RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001214 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001215 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001216 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001217 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001218 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001219 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001220 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001221
1222 // Handle struct-return functions by passing a pointer to the
1223 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001224 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001225 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001226
1227
Chris Lattner5db7ae52009-06-13 00:26:38 +00001228 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001229 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001230 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001231 llvm::Value *Value = ReturnValue.getValue();
1232 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001233 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001234 Args.push_back(Value);
1235 }
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001237 assert(CallInfo.arg_size() == CallArgs.size() &&
1238 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001239 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001240 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001241 I != E; ++I, ++info_it) {
1242 const ABIArgInfo &ArgInfo = info_it->info;
Eli Friedmanc6d07822011-05-02 18:05:27 +00001243 RValue RV = I->RV;
Daniel Dunbar56273772008-09-17 00:51:38 +00001244
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001245 unsigned Alignment =
Eli Friedmanc6d07822011-05-02 18:05:27 +00001246 getContext().getTypeAlignInChars(I->Ty).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001247 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001248 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001249 if (RV.isScalar() || RV.isComplex()) {
1250 // Make a temporary alloca to pass the argument.
Eli Friedmanc6d07822011-05-02 18:05:27 +00001251 Args.push_back(CreateMemTemp(I->Ty));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001252 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001253 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
Eli Friedmanc6d07822011-05-02 18:05:27 +00001254 Alignment, I->Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001255 else
Mike Stump1eb44332009-09-09 15:08:12 +00001256 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001257 } else {
1258 Args.push_back(RV.getAggregateAddr());
1259 }
1260 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001261 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001262
Daniel Dunbar11434922009-01-26 21:26:08 +00001263 case ABIArgInfo::Ignore:
1264 break;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001265
Chris Lattner800588f2010-07-29 06:26:06 +00001266 case ABIArgInfo::Extend:
1267 case ABIArgInfo::Direct: {
1268 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001269 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1270 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001271 if (RV.isScalar())
1272 Args.push_back(RV.getScalarVal());
1273 else
1274 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1275 break;
1276 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001277
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001278 // FIXME: Avoid the conversion through memory if possible.
1279 llvm::Value *SrcPtr;
1280 if (RV.isScalar()) {
Eli Friedmanc6d07822011-05-02 18:05:27 +00001281 SrcPtr = CreateMemTemp(I->Ty, "coerce");
1282 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment, I->Ty);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001283 } else if (RV.isComplex()) {
Eli Friedmanc6d07822011-05-02 18:05:27 +00001284 SrcPtr = CreateMemTemp(I->Ty, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001285 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001286 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001287 SrcPtr = RV.getAggregateAddr();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001288
Chris Lattner117e3f42010-07-30 04:02:24 +00001289 // If the value is offset in memory, apply the offset now.
1290 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1291 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1292 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001293 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001294 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1295
1296 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001297
Chris Lattnerce700162010-06-28 23:44:11 +00001298 // If the coerce-to type is a first class aggregate, we flatten it and
1299 // pass the elements. Either way is semantically identical, but fast-isel
1300 // and the optimizer generally likes scalar values better than FCAs.
1301 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001302 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001303 SrcPtr = Builder.CreateBitCast(SrcPtr,
1304 llvm::PointerType::getUnqual(STy));
1305 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1306 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001307 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1308 // We don't know what we're loading from.
1309 LI->setAlignment(1);
1310 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001311 }
Chris Lattnerce700162010-06-28 23:44:11 +00001312 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001313 // In the simple case, just pass the coerced loaded value.
1314 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1315 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001316 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001317
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001318 break;
1319 }
1320
Daniel Dunbar56273772008-09-17 00:51:38 +00001321 case ABIArgInfo::Expand:
Eli Friedmanc6d07822011-05-02 18:05:27 +00001322 ExpandTypeToArgs(I->Ty, RV, Args);
Daniel Dunbar56273772008-09-17 00:51:38 +00001323 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001324 }
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chris Lattner5db7ae52009-06-13 00:26:38 +00001327 // If the callee is a bitcast of a function to a varargs pointer to function
1328 // type, check to see if we can remove the bitcast. This handles some cases
1329 // with unprototyped functions.
1330 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1331 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1332 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1333 const llvm::FunctionType *CurFT =
1334 cast<llvm::FunctionType>(CurPT->getElementType());
1335 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Chris Lattner5db7ae52009-06-13 00:26:38 +00001337 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1338 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001339 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001340 ActualFT->getNumParams() == Args.size() &&
1341 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001342 bool ArgsMatch = true;
1343 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1344 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1345 ArgsMatch = false;
1346 break;
1347 }
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Chris Lattner5db7ae52009-06-13 00:26:38 +00001349 // Strip the cast if we can get away with it. This is a nice cleanup,
1350 // but also allows us to inline the function at -O0 if it is marked
1351 // always_inline.
1352 if (ArgsMatch)
1353 Callee = CalleeF;
1354 }
1355 }
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001357
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001358 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001359 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001360 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001361 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1362 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001363
John McCallf1549f62010-07-06 01:34:17 +00001364 llvm::BasicBlock *InvokeDest = 0;
1365 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1366 InvokeDest = getInvokeDest();
1367
Daniel Dunbard14151d2009-03-02 04:32:35 +00001368 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001369 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001370 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001371 } else {
1372 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001373 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001374 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001375 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001376 }
Chris Lattnerce933992010-06-29 16:40:28 +00001377 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001378 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001379
Daniel Dunbard14151d2009-03-02 04:32:35 +00001380 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001381 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001382
1383 // If the call doesn't return, finish the basic block and clear the
1384 // insertion point; this allows the rest of IRgen to discard
1385 // unreachable code.
1386 if (CS.doesNotReturn()) {
1387 Builder.CreateUnreachable();
1388 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Mike Stumpf5408fe2009-05-16 07:57:57 +00001390 // FIXME: For now, emit a dummy basic block because expr emitters in
1391 // generally are not ready to handle emitting expressions at unreachable
1392 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001393 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Daniel Dunbard14151d2009-03-02 04:32:35 +00001395 // Return a reasonable RValue.
1396 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001397 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001398
1399 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001400 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001401 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001402
1403 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001404 case ABIArgInfo::Indirect: {
1405 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001406 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001407 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001408 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001409 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001410 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1411 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001412
Daniel Dunbar11434922009-01-26 21:26:08 +00001413 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001414 // If we are ignoring an argument that had a result, make sure to
1415 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001416 return GetUndefRValue(RetTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001417
Chris Lattner800588f2010-07-29 06:26:06 +00001418 case ABIArgInfo::Extend:
1419 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001420 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1421 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001422 if (RetTy->isAnyComplexType()) {
1423 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1424 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1425 return RValue::getComplex(std::make_pair(Real, Imag));
1426 }
1427 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1428 llvm::Value *DestPtr = ReturnValue.getValue();
1429 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001430
Chris Lattner800588f2010-07-29 06:26:06 +00001431 if (!DestPtr) {
1432 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1433 DestIsVolatile = false;
1434 }
Eli Friedmanbadea572011-05-17 21:08:01 +00001435 BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
Chris Lattner800588f2010-07-29 06:26:06 +00001436 return RValue::getAggregate(DestPtr);
1437 }
1438 return RValue::get(CI);
1439 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001440
Anders Carlssond2490a92009-12-24 20:40:36 +00001441 llvm::Value *DestPtr = ReturnValue.getValue();
1442 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001443
Anders Carlssond2490a92009-12-24 20:40:36 +00001444 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001445 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001446 DestIsVolatile = false;
1447 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001448
Chris Lattner117e3f42010-07-30 04:02:24 +00001449 // If the value is offset in memory, apply the offset now.
1450 llvm::Value *StorePtr = DestPtr;
1451 if (unsigned Offs = RetAI.getDirectOffset()) {
1452 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1453 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001454 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001455 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1456 }
1457 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001458
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001459 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001460 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001461 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001462 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001463 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001464 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001465 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001466
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001467 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001468 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001469 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001470
1471 assert(0 && "Unhandled ABIArgInfo::Kind");
1472 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001473}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001474
1475/* VarArg handling */
1476
1477llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1478 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1479}