blob: 7a1600f166c57d4ae56b1f8e8d611efa0944a5ae [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();
Tilmann Scheller9c6082f2011-03-02 21:36:49 +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;
Tilmann Scheller9c6082f2011-03-02 21:36:49 +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,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +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
Tilmann Scheller9c6082f2011-03-02 21:36:49 +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
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000148 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
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000162 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 McCalld26bc762011-03-09 04:27:21 +0000226 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
Rafael Espindola264ba482010-03-30 20:24:48 +0000227 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000228}
229
John McCalld26bc762011-03-09 04:27:21 +0000230const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
231 llvm::SmallVector<CanQualType, 1> args;
232 return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
233}
234
John McCallead608a2010-02-26 00:48:12 +0000235const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
236 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000237 const FunctionType::ExtInfo &Info,
238 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +0000239#ifndef NDEBUG
240 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
241 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
242 assert(I->isCanonicalAsParam());
243#endif
244
Rafael Espindola425ef722010-03-30 22:15:11 +0000245 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000246
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000247 // Lookup or create unique function info.
248 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000249 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000250 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000251
252 void *InsertPos = 0;
253 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
254 if (FI)
255 return *FI;
256
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000257 // Construct the function info.
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000258 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy,
259 ArgTys.data(), ArgTys.size());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000260 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000261
262 // Compute ABI information.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000263 getABIInfo().computeInfo(*FI);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000264
Chris Lattner800588f2010-07-29 06:26:06 +0000265 // Loop over all of the computed argument and return value info. If any of
266 // them are direct or extend without a specified coerce type, specify the
267 // default now.
268 ABIArgInfo &RetInfo = FI->getReturnInfo();
269 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
270 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000271
Chris Lattner800588f2010-07-29 06:26:06 +0000272 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
273 I != E; ++I)
274 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
275 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000276
Chris Lattnera9fa8582010-07-01 06:20:47 +0000277 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
278 // types, resolve them now. These pointers may point to this function, which
279 // we *just* filled in the FunctionInfo for.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000280 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattnera9fa8582010-07-01 06:20:47 +0000281 HandleLateResolvedPointers();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000282
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000283 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000284}
285
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000286CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattnerbb521142010-06-29 18:13:52 +0000287 bool _NoReturn, unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000288 CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000289 const CanQualType *ArgTys,
290 unsigned NumArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000291 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000292 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000293 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000294{
Chris Lattnerbb521142010-06-29 18:13:52 +0000295 NumArgs = NumArgTys;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000296
Chris Lattnerce700162010-06-28 23:44:11 +0000297 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattnerbb521142010-06-29 18:13:52 +0000298 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000299 Args[0].type = ResTy;
Chris Lattnerbb521142010-06-29 18:13:52 +0000300 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000301 Args[1 + i].type = ArgTys[i];
302}
303
304/***/
305
Mike Stump1eb44332009-09-09 15:08:12 +0000306void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000307 std::vector<const llvm::Type*> &ArgTys,
308 bool IsRecursive) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000309 const RecordType *RT = Ty->getAsStructureType();
310 assert(RT && "Can only expand structure types.");
311 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000312 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000313 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000315 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
316 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000317 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000318 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000319 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Daniel Dunbar56273772008-09-17 00:51:38 +0000321 QualType FT = FD->getType();
Chris Lattnerdeabde22010-07-28 18:24:28 +0000322 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000323 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000324 else
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000325 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar56273772008-09-17 00:51:38 +0000326 }
327}
328
Mike Stump1eb44332009-09-09 15:08:12 +0000329llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000330CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
331 llvm::Function::arg_iterator AI) {
332 const RecordType *RT = Ty->getAsStructureType();
333 assert(RT && "Can only expand structure types.");
334
335 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000336 assert(LV.isSimple() &&
337 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000338 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000339 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
340 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000341 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000342 QualType FT = FD->getType();
343
344 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000345 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000346 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
347 AI = ExpandTypeFromArgs(FT, LV, AI);
348 } else {
349 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
350 ++AI;
351 }
352 }
353
354 return AI;
355}
356
Mike Stump1eb44332009-09-09 15:08:12 +0000357void
358CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000359 llvm::SmallVector<llvm::Value*, 16> &Args) {
360 const RecordType *RT = Ty->getAsStructureType();
361 assert(RT && "Can only expand structure types.");
362
363 RecordDecl *RD = RT->getDecl();
364 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
365 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000366 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
367 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000368 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000369 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Daniel Dunbar56273772008-09-17 00:51:38 +0000371 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000372 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000373 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
374 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
375 } else {
376 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000377 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000378 "Unexpected non-scalar rvalue during struct expansion.");
379 Args.push_back(RV.getScalarVal());
380 }
381 }
382}
383
Chris Lattnere7bb7772010-06-27 06:04:18 +0000384/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000385/// accessing some number of bytes out of it, try to gep into the struct to get
386/// at its inner goodness. Dive as deep as possible without entering an element
387/// with an in-memory size smaller than DstSize.
388static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000389EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
390 const llvm::StructType *SrcSTy,
391 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000392 // We can't dive into a zero-element struct.
393 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000394
Chris Lattner08dd2a02010-06-27 05:56:15 +0000395 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000396
Chris Lattner08dd2a02010-06-27 05:56:15 +0000397 // If the first elt is at least as large as what we're looking for, or if the
398 // first element is the same size as the whole struct, we can enter it.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000399 uint64_t FirstEltSize =
Chris Lattner08dd2a02010-06-27 05:56:15 +0000400 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000401 if (FirstEltSize < DstSize &&
Chris Lattner08dd2a02010-06-27 05:56:15 +0000402 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
403 return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000404
Chris Lattner08dd2a02010-06-27 05:56:15 +0000405 // GEP into the first element.
406 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000407
Chris Lattner08dd2a02010-06-27 05:56:15 +0000408 // If the first element is a struct, recurse.
409 const llvm::Type *SrcTy =
410 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
411 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000412 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000413
414 return SrcPtr;
415}
416
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000417/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
418/// are either integers or pointers. This does a truncation of the value if it
419/// is too large or a zero extension if it is too small.
420static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
421 const llvm::Type *Ty,
422 CodeGenFunction &CGF) {
423 if (Val->getType() == Ty)
424 return Val;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000425
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000426 if (isa<llvm::PointerType>(Val->getType())) {
427 // If this is Pointer->Pointer avoid conversion to and from int.
428 if (isa<llvm::PointerType>(Ty))
429 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000430
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000431 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000432 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000433 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000434
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000435 const llvm::Type *DestIntTy = Ty;
436 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000437 DestIntTy = CGF.IntPtrTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000438
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000439 if (Val->getType() != DestIntTy)
440 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000441
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000442 if (isa<llvm::PointerType>(Ty))
443 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
444 return Val;
445}
446
Chris Lattner08dd2a02010-06-27 05:56:15 +0000447
448
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000449/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
450/// a pointer to an object of type \arg Ty.
451///
452/// This safely handles the case when the src type is smaller than the
453/// destination type; in this situation the values of bits which not
454/// present in the src are undefined.
455static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
456 const llvm::Type *Ty,
457 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000458 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000459 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000460
Chris Lattner6ae00692010-06-28 22:51:39 +0000461 // If SrcTy and Ty are the same, just do a load.
462 if (SrcTy == Ty)
463 return CGF.Builder.CreateLoad(SrcPtr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000464
Duncan Sands9408c452009-05-09 07:08:47 +0000465 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000466
Chris Lattner08dd2a02010-06-27 05:56:15 +0000467 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000468 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000469 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
470 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000471
Chris Lattner08dd2a02010-06-27 05:56:15 +0000472 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000473
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000474 // If the source and destination are integer or pointer types, just do an
475 // extension or truncation to the desired type.
476 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
477 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
478 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
479 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
480 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000481
Daniel Dunbarb225be42009-02-03 05:59:18 +0000482 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000483 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000484 // Generally SrcSize is never greater than DstSize, since this means we are
485 // losing bits. However, this can happen in cases where the structure has
486 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000487 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000488 // FIXME: Assert that we aren't truncating non-padding bits when have access
489 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000490 llvm::Value *Casted =
491 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000492 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
493 // FIXME: Use better alignment / avoid requiring aligned load.
494 Load->setAlignment(1);
495 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000496 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000497
Chris Lattner35b21b82010-06-27 01:06:27 +0000498 // Otherwise do coercion through memory. This is stupid, but
499 // simple.
500 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
501 llvm::Value *Casted =
502 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
503 llvm::StoreInst *Store =
504 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
505 // FIXME: Use better alignment / avoid requiring aligned store.
506 Store->setAlignment(1);
507 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000508}
509
510/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
511/// where the source and destination may have different types.
512///
513/// This safely handles the case when the src type is larger than the
514/// destination type; the upper bits of the src will be lost.
515static void CreateCoercedStore(llvm::Value *Src,
516 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000517 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000518 CodeGenFunction &CGF) {
519 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000520 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000521 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000522 if (SrcTy == DstTy) {
523 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
524 return;
525 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000526
Chris Lattner6ae00692010-06-28 22:51:39 +0000527 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000528
Chris Lattnere7bb7772010-06-27 06:04:18 +0000529 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
530 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
531 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
532 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000533
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000534 // If the source and destination are integer or pointer types, just do an
535 // extension or truncation to the desired type.
536 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
537 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
538 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
539 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
540 return;
541 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000542
Duncan Sands9408c452009-05-09 07:08:47 +0000543 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000544
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000545 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000546 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000547 llvm::Value *Casted =
548 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000549 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000550 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000551 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000552 // Otherwise do coercion through memory. This is stupid, but
553 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000554
555 // Generally SrcSize is never greater than DstSize, since this means we are
556 // losing bits. However, this can happen in cases where the structure has
557 // additional padding, for example due to a user specified alignment.
558 //
559 // FIXME: Assert that we aren't truncating non-padding bits when have access
560 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000561 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
562 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000563 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000564 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000565 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
566 // FIXME: Use better alignment / avoid requiring aligned load.
567 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000568 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000569 }
570}
571
Daniel Dunbar56273772008-09-17 00:51:38 +0000572/***/
573
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000574bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000575 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000576}
577
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000578bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
579 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
580 switch (BT->getKind()) {
581 default:
582 return false;
583 case BuiltinType::Float:
584 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
585 case BuiltinType::Double:
586 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
587 case BuiltinType::LongDouble:
588 return getContext().Target.useObjCFPRetForRealType(
589 TargetInfo::LongDouble);
590 }
591 }
592
593 return false;
594}
595
John McCallc0bf4622010-02-23 00:48:20 +0000596const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
597 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000598
John McCallc0bf4622010-02-23 00:48:20 +0000599 // For definition purposes, don't consider a K&R function variadic.
600 bool Variadic = false;
601 if (const FunctionProtoType *FPT =
602 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
603 Variadic = FPT->isVariadic();
604
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000605 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000606}
607
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000608const llvm::FunctionType *
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000609CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
610 bool IsRecursive) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000611 std::vector<const llvm::Type*> ArgTys;
612
613 const llvm::Type *ResultType = 0;
614
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000615 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000616 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000617 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000618 case ABIArgInfo::Expand:
619 assert(0 && "Invalid ABI kind for return argument");
620
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000621 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000622 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000623 ResultType = RetAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000624 break;
625
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000626 case ABIArgInfo::Indirect: {
627 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000628 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000629 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000630 unsigned AS = Context.getTargetAddressSpace(RetTy);
631 ArgTys.push_back(llvm::PointerType::get(STy, AS));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000632 break;
633 }
634
Daniel Dunbar11434922009-01-26 21:26:08 +0000635 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000636 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000637 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
640 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000641 ie = FI.arg_end(); it != ie; ++it) {
642 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000644 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000645 case ABIArgInfo::Ignore:
646 break;
647
Chris Lattner800588f2010-07-29 06:26:06 +0000648 case ABIArgInfo::Indirect: {
649 // indirect arguments are always on the stack, which is addr space #0.
650 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
651 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
652 break;
653 }
654
655 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000656 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000657 // If the coerce-to type is a first class aggregate, flatten it. Either
658 // way is semantically identical, but fast-isel and the optimizer
659 // generally likes scalar values better than FCAs.
660 const llvm::Type *ArgTy = AI.getCoerceToType();
661 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
662 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
663 ArgTys.push_back(STy->getElementType(i));
664 } else {
665 ArgTys.push_back(ArgTy);
666 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000667 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000670 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000671 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000672 break;
673 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000674 }
675
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000676 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000677}
678
John McCall4c40d982010-08-31 07:33:07 +0000679const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
680 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000681 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000682
John McCall4c40d982010-08-31 07:33:07 +0000683 if (!VerifyFuncTypeComplete(FPT)) {
684 const CGFunctionInfo *Info;
685 if (isa<CXXDestructorDecl>(MD))
686 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
687 else
688 Info = &getFunctionInfo(MD);
689 return GetFunctionType(*Info, FPT->isVariadic(), false);
690 }
Anders Carlssonecf282b2009-11-24 05:08:52 +0000691
692 return llvm::OpaqueType::get(getLLVMContext());
693}
694
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000695void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000696 const Decl *TargetDecl,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000697 AttributeListType &PAL,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000698 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000699 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000700 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000701
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000702 CallingConv = FI.getEffectiveCallingConvention();
703
John McCall04a67a62010-02-05 21:31:56 +0000704 if (FI.isNoReturn())
705 FuncAttrs |= llvm::Attribute::NoReturn;
706
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000707 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000708 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000709 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000710 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000711 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
712 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000713 if (FPT && FPT->isNothrow(getContext()))
John McCall9c0c1f32010-07-08 06:48:12 +0000714 FuncAttrs |= llvm::Attribute::NoUnwind;
715 }
716
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000717 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000718 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000719 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000720 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000721 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000722 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000723 if (TargetDecl->hasAttr<MallocAttr>())
724 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000725 }
726
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000727 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000728 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000729 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000730 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000731 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000732 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000733
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000734 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000735 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000736 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000737 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000738 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000739 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000740 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000741 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000742 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000743 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000744 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000745 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000746 break;
747
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000748 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000749 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000750 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000751 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000752 // sret disables readnone and readonly
753 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
754 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000755 break;
756
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000757 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000758 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000759 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000760
Devang Patela2c69122008-09-26 22:53:57 +0000761 if (RetAttrs)
762 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000763
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000764 // FIXME: RegParm should be reduced in case of global register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000765 signed RegParm = FI.getRegParm();
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000766 if (!RegParm)
767 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000768
769 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000770 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000771 ie = FI.arg_end(); it != ie; ++it) {
772 QualType ParamType = it->type;
773 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000774 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000775
John McCalld8e10d22010-03-27 00:47:27 +0000776 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
777 // have the corresponding parameter variable. It doesn't make
Daniel Dunbar7f6890e2011-02-10 18:10:07 +0000778 // sense to do it here because parameters are so messed up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000779 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000780 case ABIArgInfo::Extend:
781 if (ParamType->isSignedIntegerType())
782 Attributes |= llvm::Attribute::SExt;
783 else if (ParamType->isUnsignedIntegerType())
784 Attributes |= llvm::Attribute::ZExt;
785 // FALL THROUGH
786 case ABIArgInfo::Direct:
787 if (RegParm > 0 &&
788 (ParamType->isIntegerType() || ParamType->isPointerType())) {
789 RegParm -=
790 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
791 if (RegParm >= 0)
792 Attributes |= llvm::Attribute::InReg;
793 }
794 // FIXME: handle sseregparm someday...
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000795
Chris Lattnerce700162010-06-28 23:44:11 +0000796 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000797 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
798 Index += STy->getNumElements()-1; // 1 will be added below.
799 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000800
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000801 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000802 if (AI.getIndirectByVal())
803 Attributes |= llvm::Attribute::ByVal;
804
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000805 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000806 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000807 // byval disables readnone and readonly.
808 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
809 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000810 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000811
Daniel Dunbar11434922009-01-26 21:26:08 +0000812 case ABIArgInfo::Ignore:
813 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000814 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000815
Daniel Dunbar56273772008-09-17 00:51:38 +0000816 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000817 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000818 // FIXME: This is rather inefficient. Do we ever actually need to do
819 // anything here? The result should be just reconstructed on the other
820 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000821 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000822 Index += Tys.size();
823 continue;
824 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Devang Patel761d7f72008-09-25 21:02:23 +0000827 if (Attributes)
828 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000829 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000830 }
Devang Patela2c69122008-09-26 22:53:57 +0000831 if (FuncAttrs)
832 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000833}
834
John McCalld26bc762011-03-09 04:27:21 +0000835/// An argument came in as a promoted argument; demote it back to its
836/// declared type.
837static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
838 const VarDecl *var,
839 llvm::Value *value) {
840 const llvm::Type *varType = CGF.ConvertType(var->getType());
841
842 // This can happen with promotions that actually don't change the
843 // underlying type, like the enum promotions.
844 if (value->getType() == varType) return value;
845
846 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
847 && "unexpected promotion type");
848
849 if (isa<llvm::IntegerType>(varType))
850 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
851
852 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
853}
854
Daniel Dunbar88b53962009-02-02 22:03:45 +0000855void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
856 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000857 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000858 // If this is an implicit-return-zero function, go ahead and
859 // initialize the return value. TODO: it might be nice to have
860 // a more general mechanism for this that didn't require synthesized
861 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000862 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000863 if (FD->hasImplicitReturnZero()) {
864 QualType RetTy = FD->getResultType().getUnqualifiedType();
865 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000866 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000867 Builder.CreateStore(Zero, ReturnValue);
868 }
869 }
870
Mike Stumpf5408fe2009-05-16 07:57:57 +0000871 // FIXME: We no longer need the types from FunctionArgList; lift up and
872 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000873
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000874 // Emit allocs for param decls. Give the LLVM Argument nodes names.
875 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000877 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000878 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000879 AI->setName("agg.result");
880 ++AI;
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000883 assert(FI.arg_size() == Args.size() &&
884 "Mismatch between function signature & arguments.");
Devang Patel093ac462011-03-03 20:13:15 +0000885 unsigned ArgNo = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000886 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel093ac462011-03-03 20:13:15 +0000887 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
888 i != e; ++i, ++info_it, ++ArgNo) {
John McCalld26bc762011-03-09 04:27:21 +0000889 const VarDecl *Arg = *i;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000890 QualType Ty = info_it->type;
891 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000892
John McCalld26bc762011-03-09 04:27:21 +0000893 bool isPromoted =
894 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
895
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000896 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000897 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000898 llvm::Value *V = AI;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000899
Daniel Dunbar1f745982009-02-05 09:16:39 +0000900 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000901 // Aggregates and complex variables are accessed by reference. All we
902 // need to do is realign the value, if requested
903 if (ArgI.getIndirectRealign()) {
904 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
905
906 // Copy from the incoming argument pointer to the temporary with the
907 // appropriate alignment.
908 //
909 // FIXME: We should have a common utility for generating an aggregate
910 // copy.
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000911 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyckfe710082011-01-19 01:58:38 +0000912 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumic95a8fc2011-03-10 14:02:21 +0000913 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
914 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
915 Builder.CreateMemCpy(Dst,
916 Src,
Ken Dyckfe710082011-01-19 01:58:38 +0000917 llvm::ConstantInt::get(IntPtrTy,
918 Size.getQuantity()),
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000919 ArgI.getIndirectAlign(),
920 false);
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000921 V = AlignedTemp;
922 }
Daniel Dunbar1f745982009-02-05 09:16:39 +0000923 } else {
924 // Load scalar value from indirect argument.
Ken Dyckfe710082011-01-19 01:58:38 +0000925 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
926 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
John McCalld26bc762011-03-09 04:27:21 +0000927
928 if (isPromoted)
929 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000930 }
Devang Patel093ac462011-03-03 20:13:15 +0000931 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000932 break;
933 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000934
935 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000936 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000937 // If we have the trivial case, handle it with no muss and fuss.
938 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000939 ArgI.getCoerceToType() == ConvertType(Ty) &&
940 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000941 assert(AI != Fn->arg_end() && "Argument mismatch!");
942 llvm::Value *V = AI;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000943
John McCalld8e10d22010-03-27 00:47:27 +0000944 if (Arg->getType().isRestrictQualified())
945 AI->addAttr(llvm::Attribute::NoAlias);
946
John McCalld26bc762011-03-09 04:27:21 +0000947 if (isPromoted)
948 V = emitArgumentDemotion(*this, Arg, V);
949
Devang Patel093ac462011-03-03 20:13:15 +0000950 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +0000951 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000952 }
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Chris Lattner121b3fa2010-07-05 20:21:00 +0000954 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000955
Chris Lattnerdeabde22010-07-28 18:24:28 +0000956 // The alignment we need to use is the max of the requested alignment for
957 // the argument plus the alignment required by our access code below.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000958 unsigned AlignmentToUse =
John McCalld16c2cf2011-02-08 08:22:06 +0000959 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerdeabde22010-07-28 18:24:28 +0000960 AlignmentToUse = std::max(AlignmentToUse,
961 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000962
Chris Lattnerdeabde22010-07-28 18:24:28 +0000963 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000964 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000965 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000966
Chris Lattner117e3f42010-07-30 04:02:24 +0000967 // If the value is offset in memory, apply the offset now.
968 if (unsigned Offs = ArgI.getDirectOffset()) {
969 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
970 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000971 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner117e3f42010-07-30 04:02:24 +0000972 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
973 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000974
Chris Lattner309c59f2010-06-29 00:06:42 +0000975 // If the coerce-to type is a first class aggregate, we flatten it and
976 // pass the elements. Either way is semantically identical, but fast-isel
977 // and the optimizer generally likes scalar values better than FCAs.
978 if (const llvm::StructType *STy =
979 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000980 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000981
Chris Lattner92826882010-07-05 20:41:41 +0000982 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
983 assert(AI != Fn->arg_end() && "Argument mismatch!");
984 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
985 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
986 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000987 }
988 } else {
989 // Simple case, just do a coerced store of the argument into the alloca.
990 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000991 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +0000992 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +0000993 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000994
995
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000996 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000997 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000998 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
John McCalld26bc762011-03-09 04:27:21 +0000999 if (isPromoted)
1000 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001001 }
Devang Patel093ac462011-03-03 20:13:15 +00001002 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerce700162010-06-28 23:44:11 +00001003 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001004 }
Chris Lattner800588f2010-07-29 06:26:06 +00001005
1006 case ABIArgInfo::Expand: {
1007 // If this structure was expanded into multiple arguments then
1008 // we need to create a temporary and reconstruct it from the
1009 // arguments.
1010 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattner800588f2010-07-29 06:26:06 +00001011 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +00001012 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel093ac462011-03-03 20:13:15 +00001013 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001014
1015 // Name the arguments used in expansion and increment AI.
1016 unsigned Index = 0;
1017 for (; AI != End; ++AI, ++Index)
1018 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1019 continue;
1020 }
1021
1022 case ABIArgInfo::Ignore:
1023 // Initialize the local variable appropriately.
1024 if (hasAggregateLLVMType(Ty))
Devang Patel093ac462011-03-03 20:13:15 +00001025 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001026 else
Devang Patel093ac462011-03-03 20:13:15 +00001027 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1028 ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001029
1030 // Skip increment, no matching LLVM parameter.
1031 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001032 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001033
1034 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001035 }
1036 assert(AI == Fn->arg_end() && "Argument mismatch!");
1037}
1038
Chris Lattner35b21b82010-06-27 01:06:27 +00001039void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001040 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001041 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001042 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001043 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001044 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001045
Dan Gohman4751a532010-07-20 20:13:52 +00001046 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001047 llvm::Value *RV = 0;
1048 QualType RetTy = FI.getReturnType();
1049 const ABIArgInfo &RetAI = FI.getReturnInfo();
1050
1051 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001052 case ABIArgInfo::Indirect: {
1053 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001054 if (RetTy->isAnyComplexType()) {
1055 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1056 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1057 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1058 // Do nothing; aggregrates get evaluated directly into the destination.
1059 } else {
1060 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001061 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001062 }
1063 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001064 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001065
1066 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001067 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001068 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1069 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001070 // The internal return value temp always will have pointer-to-return-type
1071 // type, just do a load.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001072
Chris Lattner800588f2010-07-29 06:26:06 +00001073 // If the instruction right before the insertion point is a store to the
1074 // return value, we can elide the load, zap the store, and usually zap the
1075 // alloca.
1076 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1077 llvm::StoreInst *SI = 0;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001078 if (InsertBB->empty() ||
Chris Lattner800588f2010-07-29 06:26:06 +00001079 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1080 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1081 RV = Builder.CreateLoad(ReturnValue);
1082 } else {
1083 // Get the stored value and nuke the now-dead store.
1084 RetDbgLoc = SI->getDebugLoc();
1085 RV = SI->getValueOperand();
1086 SI->eraseFromParent();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001087
Chris Lattner800588f2010-07-29 06:26:06 +00001088 // If that was the only use of the return value, nuke it as well now.
1089 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1090 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1091 ReturnValue = 0;
1092 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001093 }
Chris Lattner800588f2010-07-29 06:26:06 +00001094 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001095 llvm::Value *V = ReturnValue;
1096 // If the value is offset in memory, apply the offset now.
1097 if (unsigned Offs = RetAI.getDirectOffset()) {
1098 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1099 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001100 V = Builder.CreateBitCast(V,
Chris Lattner117e3f42010-07-30 04:02:24 +00001101 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1102 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001103
Chris Lattner117e3f42010-07-30 04:02:24 +00001104 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001105 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001106 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001107
Chris Lattner800588f2010-07-29 06:26:06 +00001108 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001109 break;
1110
1111 case ABIArgInfo::Expand:
1112 assert(0 && "Invalid ABI kind for return argument");
1113 }
1114
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001115 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001116 if (!RetDbgLoc.isUnknown())
1117 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001118}
1119
John McCall413ebdb2011-03-11 20:59:21 +00001120void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1121 const VarDecl *param) {
John McCall27360712010-05-26 22:34:26 +00001122 // StartFunction converted the ABI-lowered parameter(s) into a
1123 // local alloca. We need to turn that into an r-value suitable
1124 // for EmitCall.
John McCall413ebdb2011-03-11 20:59:21 +00001125 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall27360712010-05-26 22:34:26 +00001126
John McCall413ebdb2011-03-11 20:59:21 +00001127 QualType type = param->getType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001128
John McCall27360712010-05-26 22:34:26 +00001129 // For the most part, we just need to load the alloca, except:
1130 // 1) aggregate r-values are actually pointers to temporaries, and
1131 // 2) references to aggregates are pointers directly to the aggregate.
1132 // I don't know why references to non-aggregates are different here.
John McCall413ebdb2011-03-11 20:59:21 +00001133 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1134 if (hasAggregateLLVMType(ref->getPointeeType()))
1135 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001136
1137 // Locals which are references to scalars are represented
1138 // with allocas holding the pointer.
John McCall413ebdb2011-03-11 20:59:21 +00001139 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall27360712010-05-26 22:34:26 +00001140 }
1141
John McCall413ebdb2011-03-11 20:59:21 +00001142 if (type->isAnyComplexType()) {
1143 ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1144 return args.add(RValue::getComplex(complex), type);
1145 }
John McCall27360712010-05-26 22:34:26 +00001146
John McCall413ebdb2011-03-11 20:59:21 +00001147 if (hasAggregateLLVMType(type))
1148 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001149
John McCall413ebdb2011-03-11 20:59:21 +00001150 unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1151 llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1152 return args.add(RValue::get(value), type);
John McCall27360712010-05-26 22:34:26 +00001153}
1154
John McCall413ebdb2011-03-11 20:59:21 +00001155void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1156 QualType type) {
1157 if (type->isReferenceType())
1158 return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1159 type);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
John McCall413ebdb2011-03-11 20:59:21 +00001161 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson0139bb92009-04-08 20:47:54 +00001162}
1163
John McCallf1549f62010-07-06 01:34:17 +00001164/// Emits a call or invoke instruction to the given function, depending
1165/// on the current state of the EH stack.
1166llvm::CallSite
1167CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1168 llvm::Value * const *ArgBegin,
1169 llvm::Value * const *ArgEnd,
1170 const llvm::Twine &Name) {
1171 llvm::BasicBlock *InvokeDest = getInvokeDest();
1172 if (!InvokeDest)
1173 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1174
1175 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1176 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1177 ArgBegin, ArgEnd, Name);
1178 EmitBlock(ContBB);
1179 return Invoke;
1180}
1181
Daniel Dunbar88b53962009-02-02 22:03:45 +00001182RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001183 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001184 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001185 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001186 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001187 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001188 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001189 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001190
1191 // Handle struct-return functions by passing a pointer to the
1192 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001193 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001194 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001195
1196
Chris Lattner5db7ae52009-06-13 00:26:38 +00001197 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001198 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001199 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001200 llvm::Value *Value = ReturnValue.getValue();
1201 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001202 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001203 Args.push_back(Value);
1204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001206 assert(CallInfo.arg_size() == CallArgs.size() &&
1207 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001208 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001209 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001210 I != E; ++I, ++info_it) {
1211 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001212 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001213
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001214 unsigned Alignment =
1215 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001216 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001217 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001218 if (RV.isScalar() || RV.isComplex()) {
1219 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001220 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001221 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001222 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1223 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001224 else
Mike Stump1eb44332009-09-09 15:08:12 +00001225 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001226 } else {
1227 Args.push_back(RV.getAggregateAddr());
1228 }
1229 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001230 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001231
Daniel Dunbar11434922009-01-26 21:26:08 +00001232 case ABIArgInfo::Ignore:
1233 break;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001234
Chris Lattner800588f2010-07-29 06:26:06 +00001235 case ABIArgInfo::Extend:
1236 case ABIArgInfo::Direct: {
1237 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001238 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1239 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001240 if (RV.isScalar())
1241 Args.push_back(RV.getScalarVal());
1242 else
1243 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1244 break;
1245 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001246
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001247 // FIXME: Avoid the conversion through memory if possible.
1248 llvm::Value *SrcPtr;
1249 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001250 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001251 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1252 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001253 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001254 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001255 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001256 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001257 SrcPtr = RV.getAggregateAddr();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001258
Chris Lattner117e3f42010-07-30 04:02:24 +00001259 // If the value is offset in memory, apply the offset now.
1260 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1261 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1262 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001263 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001264 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1265
1266 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001267
Chris Lattnerce700162010-06-28 23:44:11 +00001268 // If the coerce-to type is a first class aggregate, we flatten it and
1269 // pass the elements. Either way is semantically identical, but fast-isel
1270 // and the optimizer generally likes scalar values better than FCAs.
1271 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001272 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001273 SrcPtr = Builder.CreateBitCast(SrcPtr,
1274 llvm::PointerType::getUnqual(STy));
1275 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1276 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001277 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1278 // We don't know what we're loading from.
1279 LI->setAlignment(1);
1280 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001281 }
Chris Lattnerce700162010-06-28 23:44:11 +00001282 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001283 // In the simple case, just pass the coerced loaded value.
1284 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1285 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001286 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001287
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001288 break;
1289 }
1290
Daniel Dunbar56273772008-09-17 00:51:38 +00001291 case ABIArgInfo::Expand:
1292 ExpandTypeToArgs(I->second, RV, Args);
1293 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001294 }
1295 }
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Chris Lattner5db7ae52009-06-13 00:26:38 +00001297 // If the callee is a bitcast of a function to a varargs pointer to function
1298 // type, check to see if we can remove the bitcast. This handles some cases
1299 // with unprototyped functions.
1300 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1301 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1302 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1303 const llvm::FunctionType *CurFT =
1304 cast<llvm::FunctionType>(CurPT->getElementType());
1305 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Chris Lattner5db7ae52009-06-13 00:26:38 +00001307 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1308 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001309 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001310 ActualFT->getNumParams() == Args.size() &&
1311 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001312 bool ArgsMatch = true;
1313 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1314 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1315 ArgsMatch = false;
1316 break;
1317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattner5db7ae52009-06-13 00:26:38 +00001319 // Strip the cast if we can get away with it. This is a nice cleanup,
1320 // but also allows us to inline the function at -O0 if it is marked
1321 // always_inline.
1322 if (ArgsMatch)
1323 Callee = CalleeF;
1324 }
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001327
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001328 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001329 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001330 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001331 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1332 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001333
John McCallf1549f62010-07-06 01:34:17 +00001334 llvm::BasicBlock *InvokeDest = 0;
1335 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1336 InvokeDest = getInvokeDest();
1337
Daniel Dunbard14151d2009-03-02 04:32:35 +00001338 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001339 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001340 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001341 } else {
1342 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001343 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001344 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001345 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001346 }
Chris Lattnerce933992010-06-29 16:40:28 +00001347 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001348 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001349
Daniel Dunbard14151d2009-03-02 04:32:35 +00001350 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001351 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001352
1353 // If the call doesn't return, finish the basic block and clear the
1354 // insertion point; this allows the rest of IRgen to discard
1355 // unreachable code.
1356 if (CS.doesNotReturn()) {
1357 Builder.CreateUnreachable();
1358 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Mike Stumpf5408fe2009-05-16 07:57:57 +00001360 // FIXME: For now, emit a dummy basic block because expr emitters in
1361 // generally are not ready to handle emitting expressions at unreachable
1362 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001363 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Daniel Dunbard14151d2009-03-02 04:32:35 +00001365 // Return a reasonable RValue.
1366 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001367 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001368
1369 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001370 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001371 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001372
1373 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001374 case ABIArgInfo::Indirect: {
1375 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001376 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001377 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001378 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001379 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001380 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1381 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001382
Daniel Dunbar11434922009-01-26 21:26:08 +00001383 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001384 // If we are ignoring an argument that had a result, make sure to
1385 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001386 return GetUndefRValue(RetTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001387
Chris Lattner800588f2010-07-29 06:26:06 +00001388 case ABIArgInfo::Extend:
1389 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001390 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1391 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001392 if (RetTy->isAnyComplexType()) {
1393 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1394 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1395 return RValue::getComplex(std::make_pair(Real, Imag));
1396 }
1397 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1398 llvm::Value *DestPtr = ReturnValue.getValue();
1399 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001400
Chris Lattner800588f2010-07-29 06:26:06 +00001401 if (!DestPtr) {
1402 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1403 DestIsVolatile = false;
1404 }
1405 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1406 return RValue::getAggregate(DestPtr);
1407 }
1408 return RValue::get(CI);
1409 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001410
Anders Carlssond2490a92009-12-24 20:40:36 +00001411 llvm::Value *DestPtr = ReturnValue.getValue();
1412 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001413
Anders Carlssond2490a92009-12-24 20:40:36 +00001414 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001415 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001416 DestIsVolatile = false;
1417 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001418
Chris Lattner117e3f42010-07-30 04:02:24 +00001419 // If the value is offset in memory, apply the offset now.
1420 llvm::Value *StorePtr = DestPtr;
1421 if (unsigned Offs = RetAI.getDirectOffset()) {
1422 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1423 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001424 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001425 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1426 }
1427 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001428
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001429 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001430 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001431 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001432 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001433 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001434 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001435 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001436
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001437 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001438 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001439 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001440
1441 assert(0 && "Unhandled ABIArgInfo::Kind");
1442 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001443}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001444
1445/* VarArg handling */
1446
1447llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1448 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1449}