blob: dfe9049add1197c9650436f67ad13daaa1af6602 [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)
John McCall0b0ef0a2010-02-24 07:14:12 +0000221 ArgTys.push_back(Context.getCanonicalParamType(i->second));
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
Mike Stump1eb44332009-09-09 15:08:12 +0000312void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000313 std::vector<const llvm::Type*> &ArgTys,
314 bool IsRecursive) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000315 const RecordType *RT = Ty->getAsStructureType();
316 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
Daniel Dunbar56273772008-09-17 00:51:38 +0000327 QualType FT = FD->getType();
Chris Lattnerdeabde22010-07-28 18:24:28 +0000328 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000329 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000330 else
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000331 ArgTys.push_back(ConvertType(FT, 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
516/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
517/// where the source and destination may have different types.
518///
519/// This safely handles the case when the src type is larger than the
520/// destination type; the upper bits of the src will be lost.
521static void CreateCoercedStore(llvm::Value *Src,
522 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000523 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000524 CodeGenFunction &CGF) {
525 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000526 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000527 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000528 if (SrcTy == DstTy) {
529 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
530 return;
531 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000532
Chris Lattner6ae00692010-06-28 22:51:39 +0000533 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000534
Chris Lattnere7bb7772010-06-27 06:04:18 +0000535 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
536 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
537 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
538 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000539
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000540 // If the source and destination are integer or pointer types, just do an
541 // extension or truncation to the desired type.
542 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
543 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
544 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
545 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
546 return;
547 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000548
Duncan Sands9408c452009-05-09 07:08:47 +0000549 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000550
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000551 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000552 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000553 llvm::Value *Casted =
554 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000555 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000556 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000557 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000558 // Otherwise do coercion through memory. This is stupid, but
559 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000560
561 // Generally SrcSize is never greater than DstSize, since this means we are
562 // losing bits. However, this can happen in cases where the structure has
563 // additional padding, for example due to a user specified alignment.
564 //
565 // FIXME: Assert that we aren't truncating non-padding bits when have access
566 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000567 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
568 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000569 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000570 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000571 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
572 // FIXME: Use better alignment / avoid requiring aligned load.
573 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000574 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000575 }
576}
577
Daniel Dunbar56273772008-09-17 00:51:38 +0000578/***/
579
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000580bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000581 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000582}
583
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000584bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
585 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
586 switch (BT->getKind()) {
587 default:
588 return false;
589 case BuiltinType::Float:
590 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
591 case BuiltinType::Double:
592 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
593 case BuiltinType::LongDouble:
594 return getContext().Target.useObjCFPRetForRealType(
595 TargetInfo::LongDouble);
596 }
597 }
598
599 return false;
600}
601
John McCallc0bf4622010-02-23 00:48:20 +0000602const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
603 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000604
John McCallc0bf4622010-02-23 00:48:20 +0000605 // For definition purposes, don't consider a K&R function variadic.
606 bool Variadic = false;
607 if (const FunctionProtoType *FPT =
608 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
609 Variadic = FPT->isVariadic();
610
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000611 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000612}
613
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000614const llvm::FunctionType *
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000615CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
616 bool IsRecursive) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000617 std::vector<const llvm::Type*> ArgTys;
618
619 const llvm::Type *ResultType = 0;
620
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000621 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000622 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000623 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000624 case ABIArgInfo::Expand:
625 assert(0 && "Invalid ABI kind for return argument");
626
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000627 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000628 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000629 ResultType = RetAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000630 break;
631
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000632 case ABIArgInfo::Indirect: {
633 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000634 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000635 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000636 unsigned AS = Context.getTargetAddressSpace(RetTy);
637 ArgTys.push_back(llvm::PointerType::get(STy, AS));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000638 break;
639 }
640
Daniel Dunbar11434922009-01-26 21:26:08 +0000641 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000642 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000643 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
646 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000647 ie = FI.arg_end(); it != ie; ++it) {
648 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000650 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000651 case ABIArgInfo::Ignore:
652 break;
653
Chris Lattner800588f2010-07-29 06:26:06 +0000654 case ABIArgInfo::Indirect: {
655 // indirect arguments are always on the stack, which is addr space #0.
656 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
657 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
658 break;
659 }
660
661 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000662 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000663 // If the coerce-to type is a first class aggregate, flatten it. Either
664 // way is semantically identical, but fast-isel and the optimizer
665 // generally likes scalar values better than FCAs.
666 const llvm::Type *ArgTy = AI.getCoerceToType();
667 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
668 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
669 ArgTys.push_back(STy->getElementType(i));
670 } else {
671 ArgTys.push_back(ArgTy);
672 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000673 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000676 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000677 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000678 break;
679 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000680 }
681
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000682 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000683}
684
John McCall4c40d982010-08-31 07:33:07 +0000685const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
686 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000687 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000688
John McCall4c40d982010-08-31 07:33:07 +0000689 if (!VerifyFuncTypeComplete(FPT)) {
690 const CGFunctionInfo *Info;
691 if (isa<CXXDestructorDecl>(MD))
692 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
693 else
694 Info = &getFunctionInfo(MD);
695 return GetFunctionType(*Info, FPT->isVariadic(), false);
696 }
Anders Carlssonecf282b2009-11-24 05:08:52 +0000697
698 return llvm::OpaqueType::get(getLLVMContext());
699}
700
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000701void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000702 const Decl *TargetDecl,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000703 AttributeListType &PAL,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000704 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000705 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000706 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000707
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000708 CallingConv = FI.getEffectiveCallingConvention();
709
John McCall04a67a62010-02-05 21:31:56 +0000710 if (FI.isNoReturn())
711 FuncAttrs |= llvm::Attribute::NoReturn;
712
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000713 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000714 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000715 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000716 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000717 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
718 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000719 if (FPT && FPT->isNothrow(getContext()))
John McCall9c0c1f32010-07-08 06:48:12 +0000720 FuncAttrs |= llvm::Attribute::NoUnwind;
721 }
722
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000723 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000724 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000725 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000726 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000727 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000728 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000729 if (TargetDecl->hasAttr<MallocAttr>())
730 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000731 }
732
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000733 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000734 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000735 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000736 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000737 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000738 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000739
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000740 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000741 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000742 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000743 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000744 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000745 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000746 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000747 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000748 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000749 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000750 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000751 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000752 break;
753
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000754 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000755 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000756 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000757 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000758 // sret disables readnone and readonly
759 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
760 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000761 break;
762
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000763 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000764 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000765 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000766
Devang Patela2c69122008-09-26 22:53:57 +0000767 if (RetAttrs)
768 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000769
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000770 // FIXME: RegParm should be reduced in case of global register variable.
Eli Friedmana49218e2011-04-09 08:18:08 +0000771 signed RegParm;
772 if (FI.getHasRegParm())
773 RegParm = FI.getRegParm();
774 else
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000775 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000776
777 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000778 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000779 ie = FI.arg_end(); it != ie; ++it) {
780 QualType ParamType = it->type;
781 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000782 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000783
John McCalld8e10d22010-03-27 00:47:27 +0000784 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
785 // have the corresponding parameter variable. It doesn't make
Daniel Dunbar7f6890e2011-02-10 18:10:07 +0000786 // sense to do it here because parameters are so messed up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000787 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000788 case ABIArgInfo::Extend:
789 if (ParamType->isSignedIntegerType())
790 Attributes |= llvm::Attribute::SExt;
791 else if (ParamType->isUnsignedIntegerType())
792 Attributes |= llvm::Attribute::ZExt;
793 // FALL THROUGH
794 case ABIArgInfo::Direct:
795 if (RegParm > 0 &&
796 (ParamType->isIntegerType() || ParamType->isPointerType())) {
797 RegParm -=
798 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
799 if (RegParm >= 0)
800 Attributes |= llvm::Attribute::InReg;
801 }
802 // FIXME: handle sseregparm someday...
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000803
Chris Lattnerce700162010-06-28 23:44:11 +0000804 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000805 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
806 Index += STy->getNumElements()-1; // 1 will be added below.
807 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000808
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000809 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000810 if (AI.getIndirectByVal())
811 Attributes |= llvm::Attribute::ByVal;
812
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000813 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000814 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000815 // byval disables readnone and readonly.
816 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
817 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000818 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000819
Daniel Dunbar11434922009-01-26 21:26:08 +0000820 case ABIArgInfo::Ignore:
821 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000822 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000823
Daniel Dunbar56273772008-09-17 00:51:38 +0000824 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000825 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000826 // FIXME: This is rather inefficient. Do we ever actually need to do
827 // anything here? The result should be just reconstructed on the other
828 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000829 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000830 Index += Tys.size();
831 continue;
832 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Devang Patel761d7f72008-09-25 21:02:23 +0000835 if (Attributes)
836 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000837 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000838 }
Devang Patela2c69122008-09-26 22:53:57 +0000839 if (FuncAttrs)
840 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000841}
842
John McCalld26bc762011-03-09 04:27:21 +0000843/// An argument came in as a promoted argument; demote it back to its
844/// declared type.
845static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
846 const VarDecl *var,
847 llvm::Value *value) {
848 const llvm::Type *varType = CGF.ConvertType(var->getType());
849
850 // This can happen with promotions that actually don't change the
851 // underlying type, like the enum promotions.
852 if (value->getType() == varType) return value;
853
854 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
855 && "unexpected promotion type");
856
857 if (isa<llvm::IntegerType>(varType))
858 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
859
860 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
861}
862
Daniel Dunbar88b53962009-02-02 22:03:45 +0000863void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
864 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000865 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000866 // If this is an implicit-return-zero function, go ahead and
867 // initialize the return value. TODO: it might be nice to have
868 // a more general mechanism for this that didn't require synthesized
869 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000870 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000871 if (FD->hasImplicitReturnZero()) {
872 QualType RetTy = FD->getResultType().getUnqualifiedType();
873 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000874 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000875 Builder.CreateStore(Zero, ReturnValue);
876 }
877 }
878
Mike Stumpf5408fe2009-05-16 07:57:57 +0000879 // FIXME: We no longer need the types from FunctionArgList; lift up and
880 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000881
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000882 // Emit allocs for param decls. Give the LLVM Argument nodes names.
883 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000885 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000886 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000887 AI->setName("agg.result");
888 ++AI;
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000891 assert(FI.arg_size() == Args.size() &&
892 "Mismatch between function signature & arguments.");
Devang Patel093ac462011-03-03 20:13:15 +0000893 unsigned ArgNo = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000894 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel093ac462011-03-03 20:13:15 +0000895 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
896 i != e; ++i, ++info_it, ++ArgNo) {
John McCalld26bc762011-03-09 04:27:21 +0000897 const VarDecl *Arg = *i;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000898 QualType Ty = info_it->type;
899 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000900
John McCalld26bc762011-03-09 04:27:21 +0000901 bool isPromoted =
902 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
903
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000904 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000905 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000906 llvm::Value *V = AI;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000907
Daniel Dunbar1f745982009-02-05 09:16:39 +0000908 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000909 // Aggregates and complex variables are accessed by reference. All we
910 // need to do is realign the value, if requested
911 if (ArgI.getIndirectRealign()) {
912 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
913
914 // Copy from the incoming argument pointer to the temporary with the
915 // appropriate alignment.
916 //
917 // FIXME: We should have a common utility for generating an aggregate
918 // copy.
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000919 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyckfe710082011-01-19 01:58:38 +0000920 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumic95a8fc2011-03-10 14:02:21 +0000921 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
922 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
923 Builder.CreateMemCpy(Dst,
924 Src,
Ken Dyckfe710082011-01-19 01:58:38 +0000925 llvm::ConstantInt::get(IntPtrTy,
926 Size.getQuantity()),
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000927 ArgI.getIndirectAlign(),
928 false);
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000929 V = AlignedTemp;
930 }
Daniel Dunbar1f745982009-02-05 09:16:39 +0000931 } else {
932 // Load scalar value from indirect argument.
Ken Dyckfe710082011-01-19 01:58:38 +0000933 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
934 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
John McCalld26bc762011-03-09 04:27:21 +0000935
936 if (isPromoted)
937 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000938 }
Devang Patel093ac462011-03-03 20:13:15 +0000939 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000940 break;
941 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000942
943 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000944 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000945 // If we have the trivial case, handle it with no muss and fuss.
946 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000947 ArgI.getCoerceToType() == ConvertType(Ty) &&
948 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000949 assert(AI != Fn->arg_end() && "Argument mismatch!");
950 llvm::Value *V = AI;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000951
John McCalld8e10d22010-03-27 00:47:27 +0000952 if (Arg->getType().isRestrictQualified())
953 AI->addAttr(llvm::Attribute::NoAlias);
954
John McCalld26bc762011-03-09 04:27:21 +0000955 if (isPromoted)
956 V = emitArgumentDemotion(*this, Arg, V);
957
Devang Patel093ac462011-03-03 20:13:15 +0000958 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +0000959 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattner121b3fa2010-07-05 20:21:00 +0000962 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000963
Chris Lattnerdeabde22010-07-28 18:24:28 +0000964 // The alignment we need to use is the max of the requested alignment for
965 // the argument plus the alignment required by our access code below.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000966 unsigned AlignmentToUse =
John McCalld16c2cf2011-02-08 08:22:06 +0000967 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerdeabde22010-07-28 18:24:28 +0000968 AlignmentToUse = std::max(AlignmentToUse,
969 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000970
Chris Lattnerdeabde22010-07-28 18:24:28 +0000971 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000972 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000973 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000974
Chris Lattner117e3f42010-07-30 04:02:24 +0000975 // If the value is offset in memory, apply the offset now.
976 if (unsigned Offs = ArgI.getDirectOffset()) {
977 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
978 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000979 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner117e3f42010-07-30 04:02:24 +0000980 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
981 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000982
Chris Lattner309c59f2010-06-29 00:06:42 +0000983 // If the coerce-to type is a first class aggregate, we flatten it and
984 // pass the elements. Either way is semantically identical, but fast-isel
985 // and the optimizer generally likes scalar values better than FCAs.
986 if (const llvm::StructType *STy =
987 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000988 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000989
Chris Lattner92826882010-07-05 20:41:41 +0000990 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
991 assert(AI != Fn->arg_end() && "Argument mismatch!");
992 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
993 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
994 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000995 }
996 } else {
997 // Simple case, just do a coerced store of the argument into the alloca.
998 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000999 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +00001000 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +00001001 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001002
1003
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001004 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001005 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001006 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
John McCalld26bc762011-03-09 04:27:21 +00001007 if (isPromoted)
1008 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001009 }
Devang Patel093ac462011-03-03 20:13:15 +00001010 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerce700162010-06-28 23:44:11 +00001011 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001012 }
Chris Lattner800588f2010-07-29 06:26:06 +00001013
1014 case ABIArgInfo::Expand: {
1015 // If this structure was expanded into multiple arguments then
1016 // we need to create a temporary and reconstruct it from the
1017 // arguments.
1018 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattner800588f2010-07-29 06:26:06 +00001019 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +00001020 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel093ac462011-03-03 20:13:15 +00001021 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001022
1023 // Name the arguments used in expansion and increment AI.
1024 unsigned Index = 0;
1025 for (; AI != End; ++AI, ++Index)
1026 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1027 continue;
1028 }
1029
1030 case ABIArgInfo::Ignore:
1031 // Initialize the local variable appropriately.
1032 if (hasAggregateLLVMType(Ty))
Devang Patel093ac462011-03-03 20:13:15 +00001033 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001034 else
Devang Patel093ac462011-03-03 20:13:15 +00001035 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1036 ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001037
1038 // Skip increment, no matching LLVM parameter.
1039 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001040 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001041
1042 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001043 }
1044 assert(AI == Fn->arg_end() && "Argument mismatch!");
1045}
1046
Chris Lattner35b21b82010-06-27 01:06:27 +00001047void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001048 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001049 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001050 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001051 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001052 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001053
Dan Gohman4751a532010-07-20 20:13:52 +00001054 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001055 llvm::Value *RV = 0;
1056 QualType RetTy = FI.getReturnType();
1057 const ABIArgInfo &RetAI = FI.getReturnInfo();
1058
1059 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001060 case ABIArgInfo::Indirect: {
1061 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001062 if (RetTy->isAnyComplexType()) {
1063 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1064 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1065 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1066 // Do nothing; aggregrates get evaluated directly into the destination.
1067 } else {
1068 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001069 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001070 }
1071 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001072 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001073
1074 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001075 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001076 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1077 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001078 // The internal return value temp always will have pointer-to-return-type
1079 // type, just do a load.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001080
Chris Lattner800588f2010-07-29 06:26:06 +00001081 // If the instruction right before the insertion point is a store to the
1082 // return value, we can elide the load, zap the store, and usually zap the
1083 // alloca.
1084 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1085 llvm::StoreInst *SI = 0;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001086 if (InsertBB->empty() ||
Chris Lattner800588f2010-07-29 06:26:06 +00001087 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1088 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1089 RV = Builder.CreateLoad(ReturnValue);
1090 } else {
1091 // Get the stored value and nuke the now-dead store.
1092 RetDbgLoc = SI->getDebugLoc();
1093 RV = SI->getValueOperand();
1094 SI->eraseFromParent();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001095
Chris Lattner800588f2010-07-29 06:26:06 +00001096 // If that was the only use of the return value, nuke it as well now.
1097 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1098 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1099 ReturnValue = 0;
1100 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001101 }
Chris Lattner800588f2010-07-29 06:26:06 +00001102 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001103 llvm::Value *V = ReturnValue;
1104 // If the value is offset in memory, apply the offset now.
1105 if (unsigned Offs = RetAI.getDirectOffset()) {
1106 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1107 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001108 V = Builder.CreateBitCast(V,
Chris Lattner117e3f42010-07-30 04:02:24 +00001109 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1110 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001111
Chris Lattner117e3f42010-07-30 04:02:24 +00001112 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001113 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001114 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001115
Chris Lattner800588f2010-07-29 06:26:06 +00001116 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001117 break;
1118
1119 case ABIArgInfo::Expand:
1120 assert(0 && "Invalid ABI kind for return argument");
1121 }
1122
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001123 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001124 if (!RetDbgLoc.isUnknown())
1125 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001126}
1127
John McCall413ebdb2011-03-11 20:59:21 +00001128void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1129 const VarDecl *param) {
John McCall27360712010-05-26 22:34:26 +00001130 // StartFunction converted the ABI-lowered parameter(s) into a
1131 // local alloca. We need to turn that into an r-value suitable
1132 // for EmitCall.
John McCall413ebdb2011-03-11 20:59:21 +00001133 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall27360712010-05-26 22:34:26 +00001134
John McCall413ebdb2011-03-11 20:59:21 +00001135 QualType type = param->getType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001136
John McCall27360712010-05-26 22:34:26 +00001137 // For the most part, we just need to load the alloca, except:
1138 // 1) aggregate r-values are actually pointers to temporaries, and
1139 // 2) references to aggregates are pointers directly to the aggregate.
1140 // I don't know why references to non-aggregates are different here.
John McCall413ebdb2011-03-11 20:59:21 +00001141 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1142 if (hasAggregateLLVMType(ref->getPointeeType()))
1143 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001144
1145 // Locals which are references to scalars are represented
1146 // with allocas holding the pointer.
John McCall413ebdb2011-03-11 20:59:21 +00001147 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall27360712010-05-26 22:34:26 +00001148 }
1149
John McCall413ebdb2011-03-11 20:59:21 +00001150 if (type->isAnyComplexType()) {
1151 ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1152 return args.add(RValue::getComplex(complex), type);
1153 }
John McCall27360712010-05-26 22:34:26 +00001154
John McCall413ebdb2011-03-11 20:59:21 +00001155 if (hasAggregateLLVMType(type))
1156 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001157
John McCall413ebdb2011-03-11 20:59:21 +00001158 unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1159 llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1160 return args.add(RValue::get(value), type);
John McCall27360712010-05-26 22:34:26 +00001161}
1162
John McCall413ebdb2011-03-11 20:59:21 +00001163void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1164 QualType type) {
1165 if (type->isReferenceType())
1166 return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1167 type);
Mike Stump1eb44332009-09-09 15:08:12 +00001168
John McCall413ebdb2011-03-11 20:59:21 +00001169 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson0139bb92009-04-08 20:47:54 +00001170}
1171
John McCallf1549f62010-07-06 01:34:17 +00001172/// Emits a call or invoke instruction to the given function, depending
1173/// on the current state of the EH stack.
1174llvm::CallSite
1175CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1176 llvm::Value * const *ArgBegin,
1177 llvm::Value * const *ArgEnd,
1178 const llvm::Twine &Name) {
1179 llvm::BasicBlock *InvokeDest = getInvokeDest();
1180 if (!InvokeDest)
1181 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1182
1183 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1184 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1185 ArgBegin, ArgEnd, Name);
1186 EmitBlock(ContBB);
1187 return Invoke;
1188}
1189
Daniel Dunbar88b53962009-02-02 22:03:45 +00001190RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001191 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001192 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001193 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001194 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001195 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001196 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001197 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001198
1199 // Handle struct-return functions by passing a pointer to the
1200 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001201 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001202 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001203
1204
Chris Lattner5db7ae52009-06-13 00:26:38 +00001205 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001206 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001207 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001208 llvm::Value *Value = ReturnValue.getValue();
1209 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001210 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001211 Args.push_back(Value);
1212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001214 assert(CallInfo.arg_size() == CallArgs.size() &&
1215 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001216 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001217 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001218 I != E; ++I, ++info_it) {
1219 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001220 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001221
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001222 unsigned Alignment =
1223 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001224 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001225 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001226 if (RV.isScalar() || RV.isComplex()) {
1227 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001228 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001229 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001230 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1231 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001232 else
Mike Stump1eb44332009-09-09 15:08:12 +00001233 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001234 } else {
1235 Args.push_back(RV.getAggregateAddr());
1236 }
1237 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001238 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001239
Daniel Dunbar11434922009-01-26 21:26:08 +00001240 case ABIArgInfo::Ignore:
1241 break;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001242
Chris Lattner800588f2010-07-29 06:26:06 +00001243 case ABIArgInfo::Extend:
1244 case ABIArgInfo::Direct: {
1245 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001246 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1247 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001248 if (RV.isScalar())
1249 Args.push_back(RV.getScalarVal());
1250 else
1251 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1252 break;
1253 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001254
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001255 // FIXME: Avoid the conversion through memory if possible.
1256 llvm::Value *SrcPtr;
1257 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001258 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001259 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1260 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001261 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001262 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001263 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001264 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001265 SrcPtr = RV.getAggregateAddr();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001266
Chris Lattner117e3f42010-07-30 04:02:24 +00001267 // If the value is offset in memory, apply the offset now.
1268 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1269 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1270 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001271 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001272 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1273
1274 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001275
Chris Lattnerce700162010-06-28 23:44:11 +00001276 // If the coerce-to type is a first class aggregate, we flatten it and
1277 // pass the elements. Either way is semantically identical, but fast-isel
1278 // and the optimizer generally likes scalar values better than FCAs.
1279 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001280 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001281 SrcPtr = Builder.CreateBitCast(SrcPtr,
1282 llvm::PointerType::getUnqual(STy));
1283 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1284 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001285 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1286 // We don't know what we're loading from.
1287 LI->setAlignment(1);
1288 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001289 }
Chris Lattnerce700162010-06-28 23:44:11 +00001290 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001291 // In the simple case, just pass the coerced loaded value.
1292 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1293 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001294 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001295
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001296 break;
1297 }
1298
Daniel Dunbar56273772008-09-17 00:51:38 +00001299 case ABIArgInfo::Expand:
1300 ExpandTypeToArgs(I->second, RV, Args);
1301 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001302 }
1303 }
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Chris Lattner5db7ae52009-06-13 00:26:38 +00001305 // If the callee is a bitcast of a function to a varargs pointer to function
1306 // type, check to see if we can remove the bitcast. This handles some cases
1307 // with unprototyped functions.
1308 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1309 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1310 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1311 const llvm::FunctionType *CurFT =
1312 cast<llvm::FunctionType>(CurPT->getElementType());
1313 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Chris Lattner5db7ae52009-06-13 00:26:38 +00001315 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1316 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001317 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001318 ActualFT->getNumParams() == Args.size() &&
1319 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001320 bool ArgsMatch = true;
1321 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1322 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1323 ArgsMatch = false;
1324 break;
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chris Lattner5db7ae52009-06-13 00:26:38 +00001327 // Strip the cast if we can get away with it. This is a nice cleanup,
1328 // but also allows us to inline the function at -O0 if it is marked
1329 // always_inline.
1330 if (ArgsMatch)
1331 Callee = CalleeF;
1332 }
1333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001335
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001336 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001337 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001338 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001339 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1340 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001341
John McCallf1549f62010-07-06 01:34:17 +00001342 llvm::BasicBlock *InvokeDest = 0;
1343 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1344 InvokeDest = getInvokeDest();
1345
Daniel Dunbard14151d2009-03-02 04:32:35 +00001346 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001347 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001348 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001349 } else {
1350 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001351 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001352 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001353 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001354 }
Chris Lattnerce933992010-06-29 16:40:28 +00001355 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001356 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001357
Daniel Dunbard14151d2009-03-02 04:32:35 +00001358 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001359 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001360
1361 // If the call doesn't return, finish the basic block and clear the
1362 // insertion point; this allows the rest of IRgen to discard
1363 // unreachable code.
1364 if (CS.doesNotReturn()) {
1365 Builder.CreateUnreachable();
1366 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Mike Stumpf5408fe2009-05-16 07:57:57 +00001368 // FIXME: For now, emit a dummy basic block because expr emitters in
1369 // generally are not ready to handle emitting expressions at unreachable
1370 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001371 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Daniel Dunbard14151d2009-03-02 04:32:35 +00001373 // Return a reasonable RValue.
1374 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001375 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001376
1377 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001378 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001379 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001380
1381 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001382 case ABIArgInfo::Indirect: {
1383 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001384 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001385 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001386 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001387 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001388 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1389 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001390
Daniel Dunbar11434922009-01-26 21:26:08 +00001391 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001392 // If we are ignoring an argument that had a result, make sure to
1393 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001394 return GetUndefRValue(RetTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001395
Chris Lattner800588f2010-07-29 06:26:06 +00001396 case ABIArgInfo::Extend:
1397 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001398 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1399 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001400 if (RetTy->isAnyComplexType()) {
1401 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1402 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1403 return RValue::getComplex(std::make_pair(Real, Imag));
1404 }
1405 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1406 llvm::Value *DestPtr = ReturnValue.getValue();
1407 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001408
Chris Lattner800588f2010-07-29 06:26:06 +00001409 if (!DestPtr) {
1410 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1411 DestIsVolatile = false;
1412 }
1413 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1414 return RValue::getAggregate(DestPtr);
1415 }
1416 return RValue::get(CI);
1417 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001418
Anders Carlssond2490a92009-12-24 20:40:36 +00001419 llvm::Value *DestPtr = ReturnValue.getValue();
1420 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001421
Anders Carlssond2490a92009-12-24 20:40:36 +00001422 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001423 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001424 DestIsVolatile = false;
1425 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001426
Chris Lattner117e3f42010-07-30 04:02:24 +00001427 // If the value is offset in memory, apply the offset now.
1428 llvm::Value *StorePtr = DestPtr;
1429 if (unsigned Offs = RetAI.getDirectOffset()) {
1430 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1431 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001432 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001433 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1434 }
1435 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001436
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001437 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001438 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001439 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001440 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001441 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001442 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001443 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001444
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001445 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001446 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001447 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001448
1449 assert(0 && "Unhandled ABIArgInfo::Kind");
1450 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001451}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001452
1453/* VarArg handling */
1454
1455llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1456 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1457}