blob: ae84b6196dfaf802a850327b328b5353c59ef408 [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;
Dawn Perchik52fc3142010-09-03 01:29:35 +000039 // TODO: add support for CC_X86Pascal to llvm
John McCall04a67a62010-02-05 21:31:56 +000040 }
41}
42
John McCall0b0ef0a2010-02-24 07:14:12 +000043/// Derives the 'this' type for codegen purposes, i.e. ignoring method
44/// qualification.
45/// FIXME: address space qualification?
John McCallead608a2010-02-26 00:48:12 +000046static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
47 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
48 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049}
50
John McCall0b0ef0a2010-02-24 07:14:12 +000051/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000052static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
53 return MD->getType()->getCanonicalTypeUnqualified()
54 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-02-24 07:14:12 +000055}
56
57/// Returns the "extra-canonicalized" return type, which discards
58/// qualifiers on the return type. Codegen doesn't care about them,
59/// and it makes ABI code a little easier to be able to assume that
60/// all parameter and return types are top-level unqualified.
John McCallead608a2010-02-26 00:48:12 +000061static CanQualType GetReturnType(QualType RetTy) {
62 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000063}
64
65const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000066CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
67 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000068 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
69 llvm::SmallVector<CanQualType, 16>(),
Chris Lattnerbcaedae2010-06-30 19:14:05 +000070 FTNP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000071}
72
73/// \param Args - contains any initial parameters besides those
74/// in the formal type
75static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000076 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +000077 CanQual<FunctionProtoType> FTP,
78 bool IsRecursive = false) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000079 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000080 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000082 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Chris Lattnerbcaedae2010-06-30 19:14:05 +000083 return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000084}
85
86const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000087CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
88 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000089 llvm::SmallVector<CanQualType, 16> ArgTys;
Chris Lattnerbcaedae2010-06-30 19:14:05 +000090 return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000091}
92
John McCall04a67a62010-02-05 21:31:56 +000093static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000094 // Set the appropriate calling convention for the Function.
95 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000096 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +000097
98 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +000099 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000100
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000101 if (D->hasAttr<ThisCallAttr>())
102 return CC_X86ThisCall;
103
Dawn Perchik52fc3142010-09-03 01:29:35 +0000104 if (D->hasAttr<PascalAttr>())
105 return CC_X86Pascal;
106
John McCall04a67a62010-02-05 21:31:56 +0000107 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000108}
109
Anders Carlsson375c31c2009-10-03 19:43:08 +0000110const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
111 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000112 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000113
Anders Carlsson375c31c2009-10-03 19:43:08 +0000114 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000115 ArgTys.push_back(GetThisType(Context, RD));
116
117 return ::getFunctionInfo(*this, ArgTys,
John McCallead608a2010-02-26 00:48:12 +0000118 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000119}
120
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000121const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000122 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000123
John McCallfc400282010-09-03 01:26:39 +0000124 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
125 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
126
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000127 // Add the 'this' pointer unless this is a static method.
128 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000129 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000130
John McCall0b0ef0a2010-02-24 07:14:12 +0000131 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000132}
133
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000134const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000135 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000136 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000137 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000138 CanQualType ResTy = Context.VoidTy;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000139
John McCall4c40d982010-08-31 07:33:07 +0000140 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall0b0ef0a2010-02-24 07:14:12 +0000141
John McCall4c40d982010-08-31 07:33:07 +0000142 CanQual<FunctionProtoType> FTP = GetFormalType(D);
143
144 // Add the formal parameters.
145 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
146 ArgTys.push_back(FTP->getArgType(i));
147
148 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000149}
150
151const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
152 CXXDtorType Type) {
John McCall4c40d982010-08-31 07:33:07 +0000153 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCallead608a2010-02-26 00:48:12 +0000154 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000155 CanQualType ResTy = Context.VoidTy;
John McCall0b0ef0a2010-02-24 07:14:12 +0000156
John McCall4c40d982010-08-31 07:33:07 +0000157 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
158
159 CanQual<FunctionProtoType> FTP = GetFormalType(D);
160 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
161
162 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000163}
164
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000165const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000166 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000167 if (MD->isInstance())
168 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
John McCallead608a2010-02-26 00:48:12 +0000170 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
171 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000172 if (isa<FunctionNoProtoType>(FTy))
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000173 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
John McCallead608a2010-02-26 00:48:12 +0000174 assert(isa<FunctionProtoType>(FTy));
175 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000176}
177
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000178const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000179 llvm::SmallVector<CanQualType, 16> ArgTys;
180 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
181 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000182 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000183 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000184 e = MD->param_end(); i != e; ++i) {
185 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
186 }
187 return getFunctionInfo(GetReturnType(MD->getResultType()),
188 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000189 FunctionType::ExtInfo(
190 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000191 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000192 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000193}
194
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000195const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
196 // FIXME: Do we need to handle ObjCMethodDecl?
197 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000198
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000199 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
200 return getFunctionInfo(CD, GD.getCtorType());
201
202 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
203 return getFunctionInfo(DD, GD.getDtorType());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000204
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000205 return getFunctionInfo(FD);
206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000209 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000210 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000211 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000212 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000213 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000214 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000215 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000216 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000217}
218
Mike Stump1eb44332009-09-09 15:08:12 +0000219const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000220 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000221 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000222 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000223 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000224 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000225 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000226 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000227 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000228}
229
John McCallead608a2010-02-26 00:48:12 +0000230const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
231 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000232 const FunctionType::ExtInfo &Info,
233 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +0000234#ifndef NDEBUG
235 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
236 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
237 assert(I->isCanonicalAsParam());
238#endif
239
Rafael Espindola425ef722010-03-30 22:15:11 +0000240 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000241
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000242 // Lookup or create unique function info.
243 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000244 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000245 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000246
247 void *InsertPos = 0;
248 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
249 if (FI)
250 return *FI;
251
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000252 // Construct the function info.
Chris Lattnerce700162010-06-28 23:44:11 +0000253 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000254 ArgTys.data(), ArgTys.size());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000255 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000256
257 // Compute ABI information.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000258 getABIInfo().computeInfo(*FI);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000259
Chris Lattner800588f2010-07-29 06:26:06 +0000260 // Loop over all of the computed argument and return value info. If any of
261 // them are direct or extend without a specified coerce type, specify the
262 // default now.
263 ABIArgInfo &RetInfo = FI->getReturnInfo();
264 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
265 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000266
Chris Lattner800588f2010-07-29 06:26:06 +0000267 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
268 I != E; ++I)
269 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
270 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000271
Chris Lattnera9fa8582010-07-01 06:20:47 +0000272 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
273 // types, resolve them now. These pointers may point to this function, which
274 // we *just* filled in the FunctionInfo for.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000275 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattnera9fa8582010-07-01 06:20:47 +0000276 HandleLateResolvedPointers();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000277
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000278 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000279}
280
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000281CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattnerbb521142010-06-29 18:13:52 +0000282 bool _NoReturn, unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000283 CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000284 const CanQualType *ArgTys,
285 unsigned NumArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000286 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000287 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000288 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000289{
Chris Lattnerbb521142010-06-29 18:13:52 +0000290 NumArgs = NumArgTys;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000291
Chris Lattnerce700162010-06-28 23:44:11 +0000292 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattnerbb521142010-06-29 18:13:52 +0000293 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000294 Args[0].type = ResTy;
Chris Lattnerbb521142010-06-29 18:13:52 +0000295 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000296 Args[1 + i].type = ArgTys[i];
297}
298
299/***/
300
Mike Stump1eb44332009-09-09 15:08:12 +0000301void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000302 std::vector<const llvm::Type*> &ArgTys,
303 bool IsRecursive) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000304 const RecordType *RT = Ty->getAsStructureType();
305 assert(RT && "Can only expand structure types.");
306 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000307 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000308 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000310 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
311 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000312 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000313 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000314 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Daniel Dunbar56273772008-09-17 00:51:38 +0000316 QualType FT = FD->getType();
Chris Lattnerdeabde22010-07-28 18:24:28 +0000317 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000318 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000319 else
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000320 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar56273772008-09-17 00:51:38 +0000321 }
322}
323
Mike Stump1eb44332009-09-09 15:08:12 +0000324llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000325CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
326 llvm::Function::arg_iterator AI) {
327 const RecordType *RT = Ty->getAsStructureType();
328 assert(RT && "Can only expand structure types.");
329
330 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000331 assert(LV.isSimple() &&
332 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000333 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000334 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
335 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000336 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000337 QualType FT = FD->getType();
338
339 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000340 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000341 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
342 AI = ExpandTypeFromArgs(FT, LV, AI);
343 } else {
344 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
345 ++AI;
346 }
347 }
348
349 return AI;
350}
351
Mike Stump1eb44332009-09-09 15:08:12 +0000352void
353CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000354 llvm::SmallVector<llvm::Value*, 16> &Args) {
355 const RecordType *RT = Ty->getAsStructureType();
356 assert(RT && "Can only expand structure types.");
357
358 RecordDecl *RD = RT->getDecl();
359 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
360 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000361 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
362 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000364 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Daniel Dunbar56273772008-09-17 00:51:38 +0000366 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000367 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000368 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
369 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
370 } else {
371 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000372 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000373 "Unexpected non-scalar rvalue during struct expansion.");
374 Args.push_back(RV.getScalarVal());
375 }
376 }
377}
378
Chris Lattnere7bb7772010-06-27 06:04:18 +0000379/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000380/// accessing some number of bytes out of it, try to gep into the struct to get
381/// at its inner goodness. Dive as deep as possible without entering an element
382/// with an in-memory size smaller than DstSize.
383static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000384EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
385 const llvm::StructType *SrcSTy,
386 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000387 // We can't dive into a zero-element struct.
388 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000389
Chris Lattner08dd2a02010-06-27 05:56:15 +0000390 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000391
Chris Lattner08dd2a02010-06-27 05:56:15 +0000392 // If the first elt is at least as large as what we're looking for, or if the
393 // first element is the same size as the whole struct, we can enter it.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000394 uint64_t FirstEltSize =
Chris Lattner08dd2a02010-06-27 05:56:15 +0000395 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000396 if (FirstEltSize < DstSize &&
Chris Lattner08dd2a02010-06-27 05:56:15 +0000397 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
398 return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000399
Chris Lattner08dd2a02010-06-27 05:56:15 +0000400 // GEP into the first element.
401 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000402
Chris Lattner08dd2a02010-06-27 05:56:15 +0000403 // If the first element is a struct, recurse.
404 const llvm::Type *SrcTy =
405 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
406 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000407 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000408
409 return SrcPtr;
410}
411
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000412/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
413/// are either integers or pointers. This does a truncation of the value if it
414/// is too large or a zero extension if it is too small.
415static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
416 const llvm::Type *Ty,
417 CodeGenFunction &CGF) {
418 if (Val->getType() == Ty)
419 return Val;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000420
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000421 if (isa<llvm::PointerType>(Val->getType())) {
422 // If this is Pointer->Pointer avoid conversion to and from int.
423 if (isa<llvm::PointerType>(Ty))
424 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000425
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000426 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000427 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000428 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000429
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000430 const llvm::Type *DestIntTy = Ty;
431 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000432 DestIntTy = CGF.IntPtrTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000433
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000434 if (Val->getType() != DestIntTy)
435 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000436
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000437 if (isa<llvm::PointerType>(Ty))
438 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
439 return Val;
440}
441
Chris Lattner08dd2a02010-06-27 05:56:15 +0000442
443
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000444/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
445/// a pointer to an object of type \arg Ty.
446///
447/// This safely handles the case when the src type is smaller than the
448/// destination type; in this situation the values of bits which not
449/// present in the src are undefined.
450static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
451 const llvm::Type *Ty,
452 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000453 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000454 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000455
Chris Lattner6ae00692010-06-28 22:51:39 +0000456 // If SrcTy and Ty are the same, just do a load.
457 if (SrcTy == Ty)
458 return CGF.Builder.CreateLoad(SrcPtr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000459
Duncan Sands9408c452009-05-09 07:08:47 +0000460 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000461
Chris Lattner08dd2a02010-06-27 05:56:15 +0000462 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000463 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000464 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
465 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000466
Chris Lattner08dd2a02010-06-27 05:56:15 +0000467 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000468
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000469 // If the source and destination are integer or pointer types, just do an
470 // extension or truncation to the desired type.
471 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
472 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
473 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
474 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
475 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000476
Daniel Dunbarb225be42009-02-03 05:59:18 +0000477 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000478 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000479 // Generally SrcSize is never greater than DstSize, since this means we are
480 // losing bits. However, this can happen in cases where the structure has
481 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000482 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000483 // FIXME: Assert that we aren't truncating non-padding bits when have access
484 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000485 llvm::Value *Casted =
486 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000487 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
488 // FIXME: Use better alignment / avoid requiring aligned load.
489 Load->setAlignment(1);
490 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000491 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000492
Chris Lattner35b21b82010-06-27 01:06:27 +0000493 // Otherwise do coercion through memory. This is stupid, but
494 // simple.
495 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
496 llvm::Value *Casted =
497 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
498 llvm::StoreInst *Store =
499 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
500 // FIXME: Use better alignment / avoid requiring aligned store.
501 Store->setAlignment(1);
502 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000503}
504
505/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
506/// where the source and destination may have different types.
507///
508/// This safely handles the case when the src type is larger than the
509/// destination type; the upper bits of the src will be lost.
510static void CreateCoercedStore(llvm::Value *Src,
511 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000512 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000513 CodeGenFunction &CGF) {
514 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000515 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000516 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000517 if (SrcTy == DstTy) {
518 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
519 return;
520 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000521
Chris Lattner6ae00692010-06-28 22:51:39 +0000522 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000523
Chris Lattnere7bb7772010-06-27 06:04:18 +0000524 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
525 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
526 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
527 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000528
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000529 // If the source and destination are integer or pointer types, just do an
530 // extension or truncation to the desired type.
531 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
532 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
533 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
534 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
535 return;
536 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000537
Duncan Sands9408c452009-05-09 07:08:47 +0000538 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000539
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000540 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000541 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000542 llvm::Value *Casted =
543 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000544 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000545 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000546 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000547 // Otherwise do coercion through memory. This is stupid, but
548 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000549
550 // Generally SrcSize is never greater than DstSize, since this means we are
551 // losing bits. However, this can happen in cases where the structure has
552 // additional padding, for example due to a user specified alignment.
553 //
554 // FIXME: Assert that we aren't truncating non-padding bits when have access
555 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000556 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
557 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000558 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000559 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000560 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
561 // FIXME: Use better alignment / avoid requiring aligned load.
562 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000563 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000564 }
565}
566
Daniel Dunbar56273772008-09-17 00:51:38 +0000567/***/
568
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000569bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000570 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000571}
572
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000573bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
574 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
575 switch (BT->getKind()) {
576 default:
577 return false;
578 case BuiltinType::Float:
579 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
580 case BuiltinType::Double:
581 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
582 case BuiltinType::LongDouble:
583 return getContext().Target.useObjCFPRetForRealType(
584 TargetInfo::LongDouble);
585 }
586 }
587
588 return false;
589}
590
John McCallc0bf4622010-02-23 00:48:20 +0000591const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
592 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000593
John McCallc0bf4622010-02-23 00:48:20 +0000594 // For definition purposes, don't consider a K&R function variadic.
595 bool Variadic = false;
596 if (const FunctionProtoType *FPT =
597 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
598 Variadic = FPT->isVariadic();
599
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000600 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000601}
602
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000603const llvm::FunctionType *
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000604CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
605 bool IsRecursive) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000606 std::vector<const llvm::Type*> ArgTys;
607
608 const llvm::Type *ResultType = 0;
609
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000610 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000611 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000612 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000613 case ABIArgInfo::Expand:
614 assert(0 && "Invalid ABI kind for return argument");
615
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000616 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000617 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000618 ResultType = RetAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000619 break;
620
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000621 case ABIArgInfo::Indirect: {
622 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000623 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000624 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000625 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
626 break;
627 }
628
Daniel Dunbar11434922009-01-26 21:26:08 +0000629 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000630 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000631 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000635 ie = FI.arg_end(); it != ie; ++it) {
636 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000638 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000639 case ABIArgInfo::Ignore:
640 break;
641
Chris Lattner800588f2010-07-29 06:26:06 +0000642 case ABIArgInfo::Indirect: {
643 // indirect arguments are always on the stack, which is addr space #0.
644 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
645 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
646 break;
647 }
648
649 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000650 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000651 // If the coerce-to type is a first class aggregate, flatten it. Either
652 // way is semantically identical, but fast-isel and the optimizer
653 // generally likes scalar values better than FCAs.
654 const llvm::Type *ArgTy = AI.getCoerceToType();
655 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
656 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
657 ArgTys.push_back(STy->getElementType(i));
658 } else {
659 ArgTys.push_back(ArgTy);
660 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000661 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000664 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000665 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000666 break;
667 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000668 }
669
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000670 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000671}
672
John McCall4c40d982010-08-31 07:33:07 +0000673const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
674 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000675 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000676
John McCall4c40d982010-08-31 07:33:07 +0000677 if (!VerifyFuncTypeComplete(FPT)) {
678 const CGFunctionInfo *Info;
679 if (isa<CXXDestructorDecl>(MD))
680 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
681 else
682 Info = &getFunctionInfo(MD);
683 return GetFunctionType(*Info, FPT->isVariadic(), false);
684 }
Anders Carlssonecf282b2009-11-24 05:08:52 +0000685
686 return llvm::OpaqueType::get(getLLVMContext());
687}
688
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000689void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000690 const Decl *TargetDecl,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000691 AttributeListType &PAL,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000692 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000693 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000694 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000695
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000696 CallingConv = FI.getEffectiveCallingConvention();
697
John McCall04a67a62010-02-05 21:31:56 +0000698 if (FI.isNoReturn())
699 FuncAttrs |= llvm::Attribute::NoReturn;
700
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000701 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000702 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000703 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000704 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000705 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
706 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
707 if (FPT && FPT->hasEmptyExceptionSpec())
708 FuncAttrs |= llvm::Attribute::NoUnwind;
709 }
710
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000711 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000712 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000713 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000714 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000715 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000716 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000717 if (TargetDecl->hasAttr<MallocAttr>())
718 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000719 }
720
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000721 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000722 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000723 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000724 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000725 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000726 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000727
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000728 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000729 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000730 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000731 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000732 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000733 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000734 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000735 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000736 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000737 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000738 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000739 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000740 break;
741
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000742 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000743 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000744 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000745 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000746 // sret disables readnone and readonly
747 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
748 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000749 break;
750
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000751 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000752 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000753 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000754
Devang Patela2c69122008-09-26 22:53:57 +0000755 if (RetAttrs)
756 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000757
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000758 // FIXME: RegParm should be reduced in case of global register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000759 signed RegParm = FI.getRegParm();
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000760 if (!RegParm)
761 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000762
763 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000764 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000765 ie = FI.arg_end(); it != ie; ++it) {
766 QualType ParamType = it->type;
767 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000768 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000769
John McCalld8e10d22010-03-27 00:47:27 +0000770 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
771 // have the corresponding parameter variable. It doesn't make
Daniel Dunbar7f6890e2011-02-10 18:10:07 +0000772 // sense to do it here because parameters are so messed up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000773 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000774 case ABIArgInfo::Extend:
775 if (ParamType->isSignedIntegerType())
776 Attributes |= llvm::Attribute::SExt;
777 else if (ParamType->isUnsignedIntegerType())
778 Attributes |= llvm::Attribute::ZExt;
779 // FALL THROUGH
780 case ABIArgInfo::Direct:
781 if (RegParm > 0 &&
782 (ParamType->isIntegerType() || ParamType->isPointerType())) {
783 RegParm -=
784 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
785 if (RegParm >= 0)
786 Attributes |= llvm::Attribute::InReg;
787 }
788 // FIXME: handle sseregparm someday...
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000789
Chris Lattnerce700162010-06-28 23:44:11 +0000790 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000791 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
792 Index += STy->getNumElements()-1; // 1 will be added below.
793 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000794
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000795 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000796 if (AI.getIndirectByVal())
797 Attributes |= llvm::Attribute::ByVal;
798
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000799 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000800 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000801 // byval disables readnone and readonly.
802 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
803 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000804 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000805
Daniel Dunbar11434922009-01-26 21:26:08 +0000806 case ABIArgInfo::Ignore:
807 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000808 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000809
Daniel Dunbar56273772008-09-17 00:51:38 +0000810 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000811 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000812 // FIXME: This is rather inefficient. Do we ever actually need to do
813 // anything here? The result should be just reconstructed on the other
814 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000815 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000816 Index += Tys.size();
817 continue;
818 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Devang Patel761d7f72008-09-25 21:02:23 +0000821 if (Attributes)
822 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000823 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000824 }
Devang Patela2c69122008-09-26 22:53:57 +0000825 if (FuncAttrs)
826 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000827}
828
Daniel Dunbar88b53962009-02-02 22:03:45 +0000829void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
830 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000831 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000832 // If this is an implicit-return-zero function, go ahead and
833 // initialize the return value. TODO: it might be nice to have
834 // a more general mechanism for this that didn't require synthesized
835 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000836 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000837 if (FD->hasImplicitReturnZero()) {
838 QualType RetTy = FD->getResultType().getUnqualifiedType();
839 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000840 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000841 Builder.CreateStore(Zero, ReturnValue);
842 }
843 }
844
Mike Stumpf5408fe2009-05-16 07:57:57 +0000845 // FIXME: We no longer need the types from FunctionArgList; lift up and
846 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000847
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000848 // Emit allocs for param decls. Give the LLVM Argument nodes names.
849 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000851 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000852 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000853 AI->setName("agg.result");
854 ++AI;
855 }
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000857 assert(FI.arg_size() == Args.size() &&
858 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000859 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000860 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000861 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000862 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000863 QualType Ty = info_it->type;
864 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000865
866 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000867 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000868 llvm::Value *V = AI;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000869
Daniel Dunbar1f745982009-02-05 09:16:39 +0000870 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000871 // Aggregates and complex variables are accessed by reference. All we
872 // need to do is realign the value, if requested
873 if (ArgI.getIndirectRealign()) {
874 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
875
876 // Copy from the incoming argument pointer to the temporary with the
877 // appropriate alignment.
878 //
879 // FIXME: We should have a common utility for generating an aggregate
880 // copy.
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000881 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyckfe710082011-01-19 01:58:38 +0000882 CharUnits Size = getContext().getTypeSizeInChars(Ty);
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000883 Builder.CreateMemCpy(Builder.CreateBitCast(AlignedTemp, I8PtrTy),
884 Builder.CreateBitCast(V, I8PtrTy),
Ken Dyckfe710082011-01-19 01:58:38 +0000885 llvm::ConstantInt::get(IntPtrTy,
886 Size.getQuantity()),
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000887 ArgI.getIndirectAlign(),
888 false);
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000889 V = AlignedTemp;
890 }
Daniel Dunbar1f745982009-02-05 09:16:39 +0000891 } else {
892 // Load scalar value from indirect argument.
Ken Dyckfe710082011-01-19 01:58:38 +0000893 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
894 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000895 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
896 // This must be a promotion, for something like
897 // "void a(x) short x; {..."
898 V = EmitScalarConversion(V, Ty, Arg->getType());
899 }
900 }
Devang Patel34753802011-02-16 01:11:51 +0000901 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000902 break;
903 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000904
905 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000906 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000907 // If we have the trivial case, handle it with no muss and fuss.
908 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000909 ArgI.getCoerceToType() == ConvertType(Ty) &&
910 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000911 assert(AI != Fn->arg_end() && "Argument mismatch!");
912 llvm::Value *V = AI;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000913
John McCalld8e10d22010-03-27 00:47:27 +0000914 if (Arg->getType().isRestrictQualified())
915 AI->addAttr(llvm::Attribute::NoAlias);
916
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000917 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
918 // This must be a promotion, for something like
919 // "void a(x) short x; {..."
920 V = EmitScalarConversion(V, Ty, Arg->getType());
921 }
Chris Lattner800588f2010-07-29 06:26:06 +0000922 EmitParmDecl(*Arg, V);
923 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattner121b3fa2010-07-05 20:21:00 +0000926 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000927
Chris Lattnerdeabde22010-07-28 18:24:28 +0000928 // The alignment we need to use is the max of the requested alignment for
929 // the argument plus the alignment required by our access code below.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000930 unsigned AlignmentToUse =
John McCalld16c2cf2011-02-08 08:22:06 +0000931 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerdeabde22010-07-28 18:24:28 +0000932 AlignmentToUse = std::max(AlignmentToUse,
933 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000934
Chris Lattnerdeabde22010-07-28 18:24:28 +0000935 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000936 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000937 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000938
Chris Lattner117e3f42010-07-30 04:02:24 +0000939 // If the value is offset in memory, apply the offset now.
940 if (unsigned Offs = ArgI.getDirectOffset()) {
941 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
942 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000943 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner117e3f42010-07-30 04:02:24 +0000944 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
945 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000946
Chris Lattner309c59f2010-06-29 00:06:42 +0000947 // If the coerce-to type is a first class aggregate, we flatten it and
948 // pass the elements. Either way is semantically identical, but fast-isel
949 // and the optimizer generally likes scalar values better than FCAs.
950 if (const llvm::StructType *STy =
951 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000952 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000953
Chris Lattner92826882010-07-05 20:41:41 +0000954 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
955 assert(AI != Fn->arg_end() && "Argument mismatch!");
956 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
957 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
958 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000959 }
960 } else {
961 // Simple case, just do a coerced store of the argument into the alloca.
962 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000963 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +0000964 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +0000965 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000966
967
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000968 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000969 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000970 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000971 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
972 // This must be a promotion, for something like
973 // "void a(x) short x; {..."
974 V = EmitScalarConversion(V, Ty, Arg->getType());
975 }
976 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000977 EmitParmDecl(*Arg, V);
Chris Lattnerce700162010-06-28 23:44:11 +0000978 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000979 }
Chris Lattner800588f2010-07-29 06:26:06 +0000980
981 case ABIArgInfo::Expand: {
982 // If this structure was expanded into multiple arguments then
983 // we need to create a temporary and reconstruct it from the
984 // arguments.
985 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattner800588f2010-07-29 06:26:06 +0000986 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +0000987 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Chris Lattner800588f2010-07-29 06:26:06 +0000988 EmitParmDecl(*Arg, Temp);
989
990 // Name the arguments used in expansion and increment AI.
991 unsigned Index = 0;
992 for (; AI != End; ++AI, ++Index)
993 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
994 continue;
995 }
996
997 case ABIArgInfo::Ignore:
998 // Initialize the local variable appropriately.
999 if (hasAggregateLLVMType(Ty))
1000 EmitParmDecl(*Arg, CreateMemTemp(Ty));
1001 else
1002 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1003
1004 // Skip increment, no matching LLVM parameter.
1005 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001006 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001007
1008 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001009 }
1010 assert(AI == Fn->arg_end() && "Argument mismatch!");
1011}
1012
Chris Lattner35b21b82010-06-27 01:06:27 +00001013void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001014 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001015 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001016 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001017 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001018 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001019
Dan Gohman4751a532010-07-20 20:13:52 +00001020 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001021 llvm::Value *RV = 0;
1022 QualType RetTy = FI.getReturnType();
1023 const ABIArgInfo &RetAI = FI.getReturnInfo();
1024
1025 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001026 case ABIArgInfo::Indirect: {
1027 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001028 if (RetTy->isAnyComplexType()) {
1029 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1030 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1031 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1032 // Do nothing; aggregrates get evaluated directly into the destination.
1033 } else {
1034 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001035 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001036 }
1037 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001038 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001039
1040 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001041 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001042 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1043 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001044 // The internal return value temp always will have pointer-to-return-type
1045 // type, just do a load.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001046
Chris Lattner800588f2010-07-29 06:26:06 +00001047 // If the instruction right before the insertion point is a store to the
1048 // return value, we can elide the load, zap the store, and usually zap the
1049 // alloca.
1050 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1051 llvm::StoreInst *SI = 0;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001052 if (InsertBB->empty() ||
Chris Lattner800588f2010-07-29 06:26:06 +00001053 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1054 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1055 RV = Builder.CreateLoad(ReturnValue);
1056 } else {
1057 // Get the stored value and nuke the now-dead store.
1058 RetDbgLoc = SI->getDebugLoc();
1059 RV = SI->getValueOperand();
1060 SI->eraseFromParent();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001061
Chris Lattner800588f2010-07-29 06:26:06 +00001062 // If that was the only use of the return value, nuke it as well now.
1063 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1064 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1065 ReturnValue = 0;
1066 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001067 }
Chris Lattner800588f2010-07-29 06:26:06 +00001068 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001069 llvm::Value *V = ReturnValue;
1070 // If the value is offset in memory, apply the offset now.
1071 if (unsigned Offs = RetAI.getDirectOffset()) {
1072 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1073 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001074 V = Builder.CreateBitCast(V,
Chris Lattner117e3f42010-07-30 04:02:24 +00001075 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1076 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001077
Chris Lattner117e3f42010-07-30 04:02:24 +00001078 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001079 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001080 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001081
Chris Lattner800588f2010-07-29 06:26:06 +00001082 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001083 break;
1084
1085 case ABIArgInfo::Expand:
1086 assert(0 && "Invalid ABI kind for return argument");
1087 }
1088
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001089 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001090 if (!RetDbgLoc.isUnknown())
1091 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001092}
1093
John McCall27360712010-05-26 22:34:26 +00001094RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1095 // StartFunction converted the ABI-lowered parameter(s) into a
1096 // local alloca. We need to turn that into an r-value suitable
1097 // for EmitCall.
1098 llvm::Value *Local = GetAddrOfLocalVar(Param);
1099
1100 QualType ArgType = Param->getType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001101
John McCall27360712010-05-26 22:34:26 +00001102 // For the most part, we just need to load the alloca, except:
1103 // 1) aggregate r-values are actually pointers to temporaries, and
1104 // 2) references to aggregates are pointers directly to the aggregate.
1105 // I don't know why references to non-aggregates are different here.
1106 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1107 if (hasAggregateLLVMType(RefType->getPointeeType()))
1108 return RValue::getAggregate(Local);
1109
1110 // Locals which are references to scalars are represented
1111 // with allocas holding the pointer.
1112 return RValue::get(Builder.CreateLoad(Local));
1113 }
1114
1115 if (ArgType->isAnyComplexType())
1116 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1117
1118 if (hasAggregateLLVMType(ArgType))
1119 return RValue::getAggregate(Local);
1120
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001121 unsigned Alignment = getContext().getDeclAlign(Param).getQuantity();
1122 return RValue::get(EmitLoadOfScalar(Local, false, Alignment, ArgType));
John McCall27360712010-05-26 22:34:26 +00001123}
1124
Anders Carlsson0139bb92009-04-08 20:47:54 +00001125RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00001126 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +00001127 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Anders Carlsson0139bb92009-04-08 20:47:54 +00001129 return EmitAnyExprToTemp(E);
1130}
1131
John McCallf1549f62010-07-06 01:34:17 +00001132/// Emits a call or invoke instruction to the given function, depending
1133/// on the current state of the EH stack.
1134llvm::CallSite
1135CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1136 llvm::Value * const *ArgBegin,
1137 llvm::Value * const *ArgEnd,
1138 const llvm::Twine &Name) {
1139 llvm::BasicBlock *InvokeDest = getInvokeDest();
1140 if (!InvokeDest)
1141 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1142
1143 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1144 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1145 ArgBegin, ArgEnd, Name);
1146 EmitBlock(ContBB);
1147 return Invoke;
1148}
1149
Daniel Dunbar88b53962009-02-02 22:03:45 +00001150RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001151 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001152 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001153 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001154 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001155 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001156 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001157 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001158
1159 // Handle struct-return functions by passing a pointer to the
1160 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001161 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001162 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001163
1164
Chris Lattner5db7ae52009-06-13 00:26:38 +00001165 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001166 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001167 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001168 llvm::Value *Value = ReturnValue.getValue();
1169 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001170 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001171 Args.push_back(Value);
1172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001174 assert(CallInfo.arg_size() == CallArgs.size() &&
1175 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001176 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001177 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001178 I != E; ++I, ++info_it) {
1179 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001180 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001181
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001182 unsigned Alignment =
1183 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001184 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001185 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001186 if (RV.isScalar() || RV.isComplex()) {
1187 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001188 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001189 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001190 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1191 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001192 else
Mike Stump1eb44332009-09-09 15:08:12 +00001193 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001194 } else {
1195 Args.push_back(RV.getAggregateAddr());
1196 }
1197 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001198 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001199
Daniel Dunbar11434922009-01-26 21:26:08 +00001200 case ABIArgInfo::Ignore:
1201 break;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001202
Chris Lattner800588f2010-07-29 06:26:06 +00001203 case ABIArgInfo::Extend:
1204 case ABIArgInfo::Direct: {
1205 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001206 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1207 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001208 if (RV.isScalar())
1209 Args.push_back(RV.getScalarVal());
1210 else
1211 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1212 break;
1213 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001214
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001215 // FIXME: Avoid the conversion through memory if possible.
1216 llvm::Value *SrcPtr;
1217 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001218 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001219 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1220 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001221 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001222 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001223 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001224 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001225 SrcPtr = RV.getAggregateAddr();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001226
Chris Lattner117e3f42010-07-30 04:02:24 +00001227 // If the value is offset in memory, apply the offset now.
1228 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1229 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1230 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001231 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001232 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1233
1234 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001235
Chris Lattnerce700162010-06-28 23:44:11 +00001236 // If the coerce-to type is a first class aggregate, we flatten it and
1237 // pass the elements. Either way is semantically identical, but fast-isel
1238 // and the optimizer generally likes scalar values better than FCAs.
1239 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001240 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001241 SrcPtr = Builder.CreateBitCast(SrcPtr,
1242 llvm::PointerType::getUnqual(STy));
1243 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1244 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001245 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1246 // We don't know what we're loading from.
1247 LI->setAlignment(1);
1248 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001249 }
Chris Lattnerce700162010-06-28 23:44:11 +00001250 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001251 // In the simple case, just pass the coerced loaded value.
1252 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1253 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001254 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001255
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001256 break;
1257 }
1258
Daniel Dunbar56273772008-09-17 00:51:38 +00001259 case ABIArgInfo::Expand:
1260 ExpandTypeToArgs(I->second, RV, Args);
1261 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001262 }
1263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Chris Lattner5db7ae52009-06-13 00:26:38 +00001265 // If the callee is a bitcast of a function to a varargs pointer to function
1266 // type, check to see if we can remove the bitcast. This handles some cases
1267 // with unprototyped functions.
1268 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1269 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1270 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1271 const llvm::FunctionType *CurFT =
1272 cast<llvm::FunctionType>(CurPT->getElementType());
1273 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Chris Lattner5db7ae52009-06-13 00:26:38 +00001275 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1276 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001277 ActualFT->getNumParams() == CurFT->getNumParams() &&
1278 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001279 bool ArgsMatch = true;
1280 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1281 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1282 ArgsMatch = false;
1283 break;
1284 }
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Chris Lattner5db7ae52009-06-13 00:26:38 +00001286 // Strip the cast if we can get away with it. This is a nice cleanup,
1287 // but also allows us to inline the function at -O0 if it is marked
1288 // always_inline.
1289 if (ArgsMatch)
1290 Callee = CalleeF;
1291 }
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001294
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001295 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001296 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001297 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001298 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1299 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001300
John McCallf1549f62010-07-06 01:34:17 +00001301 llvm::BasicBlock *InvokeDest = 0;
1302 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1303 InvokeDest = getInvokeDest();
1304
Daniel Dunbard14151d2009-03-02 04:32:35 +00001305 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001306 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001307 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001308 } else {
1309 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001310 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001311 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001312 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001313 }
Chris Lattnerce933992010-06-29 16:40:28 +00001314 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001315 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001316
Daniel Dunbard14151d2009-03-02 04:32:35 +00001317 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001318 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001319
1320 // If the call doesn't return, finish the basic block and clear the
1321 // insertion point; this allows the rest of IRgen to discard
1322 // unreachable code.
1323 if (CS.doesNotReturn()) {
1324 Builder.CreateUnreachable();
1325 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Mike Stumpf5408fe2009-05-16 07:57:57 +00001327 // FIXME: For now, emit a dummy basic block because expr emitters in
1328 // generally are not ready to handle emitting expressions at unreachable
1329 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001330 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Daniel Dunbard14151d2009-03-02 04:32:35 +00001332 // Return a reasonable RValue.
1333 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001334 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001335
1336 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001337 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001338 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001339
1340 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001341 case ABIArgInfo::Indirect: {
1342 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001343 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001344 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001345 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001346 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001347 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1348 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001349
Daniel Dunbar11434922009-01-26 21:26:08 +00001350 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001351 // If we are ignoring an argument that had a result, make sure to
1352 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001353 return GetUndefRValue(RetTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001354
Chris Lattner800588f2010-07-29 06:26:06 +00001355 case ABIArgInfo::Extend:
1356 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001357 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1358 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001359 if (RetTy->isAnyComplexType()) {
1360 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1361 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1362 return RValue::getComplex(std::make_pair(Real, Imag));
1363 }
1364 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1365 llvm::Value *DestPtr = ReturnValue.getValue();
1366 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001367
Chris Lattner800588f2010-07-29 06:26:06 +00001368 if (!DestPtr) {
1369 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1370 DestIsVolatile = false;
1371 }
1372 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1373 return RValue::getAggregate(DestPtr);
1374 }
1375 return RValue::get(CI);
1376 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001377
Anders Carlssond2490a92009-12-24 20:40:36 +00001378 llvm::Value *DestPtr = ReturnValue.getValue();
1379 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001380
Anders Carlssond2490a92009-12-24 20:40:36 +00001381 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001382 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001383 DestIsVolatile = false;
1384 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001385
Chris Lattner117e3f42010-07-30 04:02:24 +00001386 // If the value is offset in memory, apply the offset now.
1387 llvm::Value *StorePtr = DestPtr;
1388 if (unsigned Offs = RetAI.getDirectOffset()) {
1389 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1390 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001391 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001392 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1393 }
1394 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001395
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001396 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001397 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001398 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001399 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001400 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001401 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001402 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001403
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001404 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001405 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001406 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001407
1408 assert(0 && "Unhandled ABIArgInfo::Kind");
1409 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001410}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001411
1412/* VarArg handling */
1413
1414llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1415 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1416}