blob: 15ab6b080bef2cb5253f671f6b1fb9ddb84ff365 [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);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000630 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
631 break;
632 }
633
Daniel Dunbar11434922009-01-26 21:26:08 +0000634 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000635 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000636 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
639 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000640 ie = FI.arg_end(); it != ie; ++it) {
641 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000643 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000644 case ABIArgInfo::Ignore:
645 break;
646
Chris Lattner800588f2010-07-29 06:26:06 +0000647 case ABIArgInfo::Indirect: {
648 // indirect arguments are always on the stack, which is addr space #0.
649 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
650 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
651 break;
652 }
653
654 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000655 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000656 // If the coerce-to type is a first class aggregate, flatten it. Either
657 // way is semantically identical, but fast-isel and the optimizer
658 // generally likes scalar values better than FCAs.
659 const llvm::Type *ArgTy = AI.getCoerceToType();
660 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
661 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
662 ArgTys.push_back(STy->getElementType(i));
663 } else {
664 ArgTys.push_back(ArgTy);
665 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000666 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000669 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000670 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000671 break;
672 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000673 }
674
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000675 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000676}
677
John McCall4c40d982010-08-31 07:33:07 +0000678const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
679 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000680 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000681
John McCall4c40d982010-08-31 07:33:07 +0000682 if (!VerifyFuncTypeComplete(FPT)) {
683 const CGFunctionInfo *Info;
684 if (isa<CXXDestructorDecl>(MD))
685 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
686 else
687 Info = &getFunctionInfo(MD);
688 return GetFunctionType(*Info, FPT->isVariadic(), false);
689 }
Anders Carlssonecf282b2009-11-24 05:08:52 +0000690
691 return llvm::OpaqueType::get(getLLVMContext());
692}
693
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000694void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000695 const Decl *TargetDecl,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000696 AttributeListType &PAL,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000697 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000698 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000699 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000700
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000701 CallingConv = FI.getEffectiveCallingConvention();
702
John McCall04a67a62010-02-05 21:31:56 +0000703 if (FI.isNoReturn())
704 FuncAttrs |= llvm::Attribute::NoReturn;
705
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000706 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000707 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000708 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000709 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000710 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
711 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000712 if (FPT && FPT->isNothrow(getContext()))
John McCall9c0c1f32010-07-08 06:48:12 +0000713 FuncAttrs |= llvm::Attribute::NoUnwind;
714 }
715
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000716 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000717 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000718 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000719 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000720 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000721 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000722 if (TargetDecl->hasAttr<MallocAttr>())
723 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000724 }
725
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000726 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000727 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000728 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000729 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000730 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000731 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000732
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000733 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000734 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000735 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000736 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000737 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000738 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000739 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000740 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000741 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000742 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000743 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000744 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000745 break;
746
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000747 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000748 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000749 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000750 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000751 // sret disables readnone and readonly
752 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
753 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000754 break;
755
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000756 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000757 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000758 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000759
Devang Patela2c69122008-09-26 22:53:57 +0000760 if (RetAttrs)
761 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000762
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000763 // FIXME: RegParm should be reduced in case of global register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000764 signed RegParm = FI.getRegParm();
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000765 if (!RegParm)
766 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000767
768 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000769 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000770 ie = FI.arg_end(); it != ie; ++it) {
771 QualType ParamType = it->type;
772 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000773 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000774
John McCalld8e10d22010-03-27 00:47:27 +0000775 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
776 // have the corresponding parameter variable. It doesn't make
Daniel Dunbar7f6890e2011-02-10 18:10:07 +0000777 // sense to do it here because parameters are so messed up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000778 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000779 case ABIArgInfo::Extend:
780 if (ParamType->isSignedIntegerType())
781 Attributes |= llvm::Attribute::SExt;
782 else if (ParamType->isUnsignedIntegerType())
783 Attributes |= llvm::Attribute::ZExt;
784 // FALL THROUGH
785 case ABIArgInfo::Direct:
786 if (RegParm > 0 &&
787 (ParamType->isIntegerType() || ParamType->isPointerType())) {
788 RegParm -=
789 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
790 if (RegParm >= 0)
791 Attributes |= llvm::Attribute::InReg;
792 }
793 // FIXME: handle sseregparm someday...
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000794
Chris Lattnerce700162010-06-28 23:44:11 +0000795 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000796 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
797 Index += STy->getNumElements()-1; // 1 will be added below.
798 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000799
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000800 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000801 if (AI.getIndirectByVal())
802 Attributes |= llvm::Attribute::ByVal;
803
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000804 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000805 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000806 // byval disables readnone and readonly.
807 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
808 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000809 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000810
Daniel Dunbar11434922009-01-26 21:26:08 +0000811 case ABIArgInfo::Ignore:
812 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000813 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000814
Daniel Dunbar56273772008-09-17 00:51:38 +0000815 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000816 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000817 // FIXME: This is rather inefficient. Do we ever actually need to do
818 // anything here? The result should be just reconstructed on the other
819 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000820 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000821 Index += Tys.size();
822 continue;
823 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Devang Patel761d7f72008-09-25 21:02:23 +0000826 if (Attributes)
827 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000828 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000829 }
Devang Patela2c69122008-09-26 22:53:57 +0000830 if (FuncAttrs)
831 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000832}
833
John McCalld26bc762011-03-09 04:27:21 +0000834/// An argument came in as a promoted argument; demote it back to its
835/// declared type.
836static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
837 const VarDecl *var,
838 llvm::Value *value) {
839 const llvm::Type *varType = CGF.ConvertType(var->getType());
840
841 // This can happen with promotions that actually don't change the
842 // underlying type, like the enum promotions.
843 if (value->getType() == varType) return value;
844
845 assert((varType->isIntegerTy() || varType->isFloatingPointTy())
846 && "unexpected promotion type");
847
848 if (isa<llvm::IntegerType>(varType))
849 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
850
851 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
852}
853
Daniel Dunbar88b53962009-02-02 22:03:45 +0000854void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
855 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000856 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000857 // If this is an implicit-return-zero function, go ahead and
858 // initialize the return value. TODO: it might be nice to have
859 // a more general mechanism for this that didn't require synthesized
860 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000861 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000862 if (FD->hasImplicitReturnZero()) {
863 QualType RetTy = FD->getResultType().getUnqualifiedType();
864 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000865 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000866 Builder.CreateStore(Zero, ReturnValue);
867 }
868 }
869
Mike Stumpf5408fe2009-05-16 07:57:57 +0000870 // FIXME: We no longer need the types from FunctionArgList; lift up and
871 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000872
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000873 // Emit allocs for param decls. Give the LLVM Argument nodes names.
874 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000876 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000877 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000878 AI->setName("agg.result");
879 ++AI;
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000882 assert(FI.arg_size() == Args.size() &&
883 "Mismatch between function signature & arguments.");
Devang Patel093ac462011-03-03 20:13:15 +0000884 unsigned ArgNo = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000885 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel093ac462011-03-03 20:13:15 +0000886 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
887 i != e; ++i, ++info_it, ++ArgNo) {
John McCalld26bc762011-03-09 04:27:21 +0000888 const VarDecl *Arg = *i;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000889 QualType Ty = info_it->type;
890 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000891
John McCalld26bc762011-03-09 04:27:21 +0000892 bool isPromoted =
893 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
894
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000895 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000896 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000897 llvm::Value *V = AI;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000898
Daniel Dunbar1f745982009-02-05 09:16:39 +0000899 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000900 // Aggregates and complex variables are accessed by reference. All we
901 // need to do is realign the value, if requested
902 if (ArgI.getIndirectRealign()) {
903 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
904
905 // Copy from the incoming argument pointer to the temporary with the
906 // appropriate alignment.
907 //
908 // FIXME: We should have a common utility for generating an aggregate
909 // copy.
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000910 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyckfe710082011-01-19 01:58:38 +0000911 CharUnits Size = getContext().getTypeSizeInChars(Ty);
NAKAMURA Takumic95a8fc2011-03-10 14:02:21 +0000912 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
913 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
914 Builder.CreateMemCpy(Dst,
915 Src,
Ken Dyckfe710082011-01-19 01:58:38 +0000916 llvm::ConstantInt::get(IntPtrTy,
917 Size.getQuantity()),
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000918 ArgI.getIndirectAlign(),
919 false);
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000920 V = AlignedTemp;
921 }
Daniel Dunbar1f745982009-02-05 09:16:39 +0000922 } else {
923 // Load scalar value from indirect argument.
Ken Dyckfe710082011-01-19 01:58:38 +0000924 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
925 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
John McCalld26bc762011-03-09 04:27:21 +0000926
927 if (isPromoted)
928 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000929 }
Devang Patel093ac462011-03-03 20:13:15 +0000930 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000931 break;
932 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000933
934 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000935 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000936 // If we have the trivial case, handle it with no muss and fuss.
937 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000938 ArgI.getCoerceToType() == ConvertType(Ty) &&
939 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000940 assert(AI != Fn->arg_end() && "Argument mismatch!");
941 llvm::Value *V = AI;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000942
John McCalld8e10d22010-03-27 00:47:27 +0000943 if (Arg->getType().isRestrictQualified())
944 AI->addAttr(llvm::Attribute::NoAlias);
945
John McCalld26bc762011-03-09 04:27:21 +0000946 if (isPromoted)
947 V = emitArgumentDemotion(*this, Arg, V);
948
Devang Patel093ac462011-03-03 20:13:15 +0000949 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +0000950 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner121b3fa2010-07-05 20:21:00 +0000953 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000954
Chris Lattnerdeabde22010-07-28 18:24:28 +0000955 // The alignment we need to use is the max of the requested alignment for
956 // the argument plus the alignment required by our access code below.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000957 unsigned AlignmentToUse =
John McCalld16c2cf2011-02-08 08:22:06 +0000958 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerdeabde22010-07-28 18:24:28 +0000959 AlignmentToUse = std::max(AlignmentToUse,
960 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000961
Chris Lattnerdeabde22010-07-28 18:24:28 +0000962 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000963 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000964 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000965
Chris Lattner117e3f42010-07-30 04:02:24 +0000966 // If the value is offset in memory, apply the offset now.
967 if (unsigned Offs = ArgI.getDirectOffset()) {
968 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
969 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000970 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner117e3f42010-07-30 04:02:24 +0000971 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
972 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000973
Chris Lattner309c59f2010-06-29 00:06:42 +0000974 // If the coerce-to type is a first class aggregate, we flatten it and
975 // pass the elements. Either way is semantically identical, but fast-isel
976 // and the optimizer generally likes scalar values better than FCAs.
977 if (const llvm::StructType *STy =
978 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000979 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000980
Chris Lattner92826882010-07-05 20:41:41 +0000981 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
982 assert(AI != Fn->arg_end() && "Argument mismatch!");
983 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
984 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
985 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000986 }
987 } else {
988 // Simple case, just do a coerced store of the argument into the alloca.
989 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000990 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +0000991 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +0000992 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000993
994
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000995 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000996 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000997 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
John McCalld26bc762011-03-09 04:27:21 +0000998 if (isPromoted)
999 V = emitArgumentDemotion(*this, Arg, V);
Daniel Dunbar8b29a382009-02-04 07:22:24 +00001000 }
Devang Patel093ac462011-03-03 20:13:15 +00001001 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerce700162010-06-28 23:44:11 +00001002 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001003 }
Chris Lattner800588f2010-07-29 06:26:06 +00001004
1005 case ABIArgInfo::Expand: {
1006 // If this structure was expanded into multiple arguments then
1007 // we need to create a temporary and reconstruct it from the
1008 // arguments.
1009 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattner800588f2010-07-29 06:26:06 +00001010 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +00001011 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel093ac462011-03-03 20:13:15 +00001012 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001013
1014 // Name the arguments used in expansion and increment AI.
1015 unsigned Index = 0;
1016 for (; AI != End; ++AI, ++Index)
1017 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1018 continue;
1019 }
1020
1021 case ABIArgInfo::Ignore:
1022 // Initialize the local variable appropriately.
1023 if (hasAggregateLLVMType(Ty))
Devang Patel093ac462011-03-03 20:13:15 +00001024 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001025 else
Devang Patel093ac462011-03-03 20:13:15 +00001026 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1027 ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001028
1029 // Skip increment, no matching LLVM parameter.
1030 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001031 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001032
1033 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001034 }
1035 assert(AI == Fn->arg_end() && "Argument mismatch!");
1036}
1037
Chris Lattner35b21b82010-06-27 01:06:27 +00001038void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001039 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001040 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001041 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001042 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001043 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001044
Dan Gohman4751a532010-07-20 20:13:52 +00001045 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001046 llvm::Value *RV = 0;
1047 QualType RetTy = FI.getReturnType();
1048 const ABIArgInfo &RetAI = FI.getReturnInfo();
1049
1050 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001051 case ABIArgInfo::Indirect: {
1052 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001053 if (RetTy->isAnyComplexType()) {
1054 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1055 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1056 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1057 // Do nothing; aggregrates get evaluated directly into the destination.
1058 } else {
1059 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001060 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001061 }
1062 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001063 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001064
1065 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001066 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001067 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1068 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001069 // The internal return value temp always will have pointer-to-return-type
1070 // type, just do a load.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001071
Chris Lattner800588f2010-07-29 06:26:06 +00001072 // If the instruction right before the insertion point is a store to the
1073 // return value, we can elide the load, zap the store, and usually zap the
1074 // alloca.
1075 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1076 llvm::StoreInst *SI = 0;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001077 if (InsertBB->empty() ||
Chris Lattner800588f2010-07-29 06:26:06 +00001078 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1079 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1080 RV = Builder.CreateLoad(ReturnValue);
1081 } else {
1082 // Get the stored value and nuke the now-dead store.
1083 RetDbgLoc = SI->getDebugLoc();
1084 RV = SI->getValueOperand();
1085 SI->eraseFromParent();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001086
Chris Lattner800588f2010-07-29 06:26:06 +00001087 // If that was the only use of the return value, nuke it as well now.
1088 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1089 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1090 ReturnValue = 0;
1091 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001092 }
Chris Lattner800588f2010-07-29 06:26:06 +00001093 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001094 llvm::Value *V = ReturnValue;
1095 // If the value is offset in memory, apply the offset now.
1096 if (unsigned Offs = RetAI.getDirectOffset()) {
1097 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1098 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001099 V = Builder.CreateBitCast(V,
Chris Lattner117e3f42010-07-30 04:02:24 +00001100 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1101 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001102
Chris Lattner117e3f42010-07-30 04:02:24 +00001103 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001104 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001105 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001106
Chris Lattner800588f2010-07-29 06:26:06 +00001107 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001108 break;
1109
1110 case ABIArgInfo::Expand:
1111 assert(0 && "Invalid ABI kind for return argument");
1112 }
1113
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001114 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001115 if (!RetDbgLoc.isUnknown())
1116 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001117}
1118
John McCall413ebdb2011-03-11 20:59:21 +00001119void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1120 const VarDecl *param) {
John McCall27360712010-05-26 22:34:26 +00001121 // StartFunction converted the ABI-lowered parameter(s) into a
1122 // local alloca. We need to turn that into an r-value suitable
1123 // for EmitCall.
John McCall413ebdb2011-03-11 20:59:21 +00001124 llvm::Value *local = GetAddrOfLocalVar(param);
John McCall27360712010-05-26 22:34:26 +00001125
John McCall413ebdb2011-03-11 20:59:21 +00001126 QualType type = param->getType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001127
John McCall27360712010-05-26 22:34:26 +00001128 // For the most part, we just need to load the alloca, except:
1129 // 1) aggregate r-values are actually pointers to temporaries, and
1130 // 2) references to aggregates are pointers directly to the aggregate.
1131 // I don't know why references to non-aggregates are different here.
John McCall413ebdb2011-03-11 20:59:21 +00001132 if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1133 if (hasAggregateLLVMType(ref->getPointeeType()))
1134 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001135
1136 // Locals which are references to scalars are represented
1137 // with allocas holding the pointer.
John McCall413ebdb2011-03-11 20:59:21 +00001138 return args.add(RValue::get(Builder.CreateLoad(local)), type);
John McCall27360712010-05-26 22:34:26 +00001139 }
1140
John McCall413ebdb2011-03-11 20:59:21 +00001141 if (type->isAnyComplexType()) {
1142 ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1143 return args.add(RValue::getComplex(complex), type);
1144 }
John McCall27360712010-05-26 22:34:26 +00001145
John McCall413ebdb2011-03-11 20:59:21 +00001146 if (hasAggregateLLVMType(type))
1147 return args.add(RValue::getAggregate(local), type);
John McCall27360712010-05-26 22:34:26 +00001148
John McCall413ebdb2011-03-11 20:59:21 +00001149 unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1150 llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1151 return args.add(RValue::get(value), type);
John McCall27360712010-05-26 22:34:26 +00001152}
1153
John McCall413ebdb2011-03-11 20:59:21 +00001154void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1155 QualType type) {
1156 if (type->isReferenceType())
1157 return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1158 type);
Mike Stump1eb44332009-09-09 15:08:12 +00001159
John McCall413ebdb2011-03-11 20:59:21 +00001160 args.add(EmitAnyExprToTemp(E), type);
Anders Carlsson0139bb92009-04-08 20:47:54 +00001161}
1162
John McCallf1549f62010-07-06 01:34:17 +00001163/// Emits a call or invoke instruction to the given function, depending
1164/// on the current state of the EH stack.
1165llvm::CallSite
1166CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1167 llvm::Value * const *ArgBegin,
1168 llvm::Value * const *ArgEnd,
1169 const llvm::Twine &Name) {
1170 llvm::BasicBlock *InvokeDest = getInvokeDest();
1171 if (!InvokeDest)
1172 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1173
1174 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1175 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1176 ArgBegin, ArgEnd, Name);
1177 EmitBlock(ContBB);
1178 return Invoke;
1179}
1180
Daniel Dunbar88b53962009-02-02 22:03:45 +00001181RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001182 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001183 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001184 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001185 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001186 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001187 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001188 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001189
1190 // Handle struct-return functions by passing a pointer to the
1191 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001192 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001193 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001194
1195
Chris Lattner5db7ae52009-06-13 00:26:38 +00001196 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001197 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001198 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001199 llvm::Value *Value = ReturnValue.getValue();
1200 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001201 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001202 Args.push_back(Value);
1203 }
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001205 assert(CallInfo.arg_size() == CallArgs.size() &&
1206 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001207 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001208 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001209 I != E; ++I, ++info_it) {
1210 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001211 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001212
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001213 unsigned Alignment =
1214 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001215 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001216 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001217 if (RV.isScalar() || RV.isComplex()) {
1218 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001219 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001220 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001221 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1222 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001223 else
Mike Stump1eb44332009-09-09 15:08:12 +00001224 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001225 } else {
1226 Args.push_back(RV.getAggregateAddr());
1227 }
1228 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001229 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001230
Daniel Dunbar11434922009-01-26 21:26:08 +00001231 case ABIArgInfo::Ignore:
1232 break;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001233
Chris Lattner800588f2010-07-29 06:26:06 +00001234 case ABIArgInfo::Extend:
1235 case ABIArgInfo::Direct: {
1236 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001237 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1238 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001239 if (RV.isScalar())
1240 Args.push_back(RV.getScalarVal());
1241 else
1242 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1243 break;
1244 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001245
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001246 // FIXME: Avoid the conversion through memory if possible.
1247 llvm::Value *SrcPtr;
1248 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001249 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001250 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1251 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001252 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001253 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001254 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001255 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001256 SrcPtr = RV.getAggregateAddr();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001257
Chris Lattner117e3f42010-07-30 04:02:24 +00001258 // If the value is offset in memory, apply the offset now.
1259 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1260 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1261 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001262 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001263 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1264
1265 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001266
Chris Lattnerce700162010-06-28 23:44:11 +00001267 // If the coerce-to type is a first class aggregate, we flatten it and
1268 // pass the elements. Either way is semantically identical, but fast-isel
1269 // and the optimizer generally likes scalar values better than FCAs.
1270 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001271 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001272 SrcPtr = Builder.CreateBitCast(SrcPtr,
1273 llvm::PointerType::getUnqual(STy));
1274 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1275 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001276 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1277 // We don't know what we're loading from.
1278 LI->setAlignment(1);
1279 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001280 }
Chris Lattnerce700162010-06-28 23:44:11 +00001281 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001282 // In the simple case, just pass the coerced loaded value.
1283 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1284 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001285 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001286
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001287 break;
1288 }
1289
Daniel Dunbar56273772008-09-17 00:51:38 +00001290 case ABIArgInfo::Expand:
1291 ExpandTypeToArgs(I->second, RV, Args);
1292 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001293 }
1294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Chris Lattner5db7ae52009-06-13 00:26:38 +00001296 // If the callee is a bitcast of a function to a varargs pointer to function
1297 // type, check to see if we can remove the bitcast. This handles some cases
1298 // with unprototyped functions.
1299 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1300 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1301 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1302 const llvm::FunctionType *CurFT =
1303 cast<llvm::FunctionType>(CurPT->getElementType());
1304 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Chris Lattner5db7ae52009-06-13 00:26:38 +00001306 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1307 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001308 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001309 ActualFT->getNumParams() == Args.size() &&
1310 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001311 bool ArgsMatch = true;
1312 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1313 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1314 ArgsMatch = false;
1315 break;
1316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Chris Lattner5db7ae52009-06-13 00:26:38 +00001318 // Strip the cast if we can get away with it. This is a nice cleanup,
1319 // but also allows us to inline the function at -O0 if it is marked
1320 // always_inline.
1321 if (ArgsMatch)
1322 Callee = CalleeF;
1323 }
1324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001326
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001327 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001328 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001329 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001330 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1331 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001332
John McCallf1549f62010-07-06 01:34:17 +00001333 llvm::BasicBlock *InvokeDest = 0;
1334 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1335 InvokeDest = getInvokeDest();
1336
Daniel Dunbard14151d2009-03-02 04:32:35 +00001337 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001338 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001339 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001340 } else {
1341 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001342 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001343 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001344 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001345 }
Chris Lattnerce933992010-06-29 16:40:28 +00001346 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001347 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001348
Daniel Dunbard14151d2009-03-02 04:32:35 +00001349 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001350 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001351
1352 // If the call doesn't return, finish the basic block and clear the
1353 // insertion point; this allows the rest of IRgen to discard
1354 // unreachable code.
1355 if (CS.doesNotReturn()) {
1356 Builder.CreateUnreachable();
1357 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Mike Stumpf5408fe2009-05-16 07:57:57 +00001359 // FIXME: For now, emit a dummy basic block because expr emitters in
1360 // generally are not ready to handle emitting expressions at unreachable
1361 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001362 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Daniel Dunbard14151d2009-03-02 04:32:35 +00001364 // Return a reasonable RValue.
1365 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001366 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001367
1368 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001369 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001370 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001371
1372 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001373 case ABIArgInfo::Indirect: {
1374 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001375 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001376 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001377 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001378 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001379 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1380 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001381
Daniel Dunbar11434922009-01-26 21:26:08 +00001382 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001383 // If we are ignoring an argument that had a result, make sure to
1384 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001385 return GetUndefRValue(RetTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001386
Chris Lattner800588f2010-07-29 06:26:06 +00001387 case ABIArgInfo::Extend:
1388 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001389 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1390 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001391 if (RetTy->isAnyComplexType()) {
1392 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1393 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1394 return RValue::getComplex(std::make_pair(Real, Imag));
1395 }
1396 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1397 llvm::Value *DestPtr = ReturnValue.getValue();
1398 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001399
Chris Lattner800588f2010-07-29 06:26:06 +00001400 if (!DestPtr) {
1401 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1402 DestIsVolatile = false;
1403 }
1404 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1405 return RValue::getAggregate(DestPtr);
1406 }
1407 return RValue::get(CI);
1408 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001409
Anders Carlssond2490a92009-12-24 20:40:36 +00001410 llvm::Value *DestPtr = ReturnValue.getValue();
1411 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001412
Anders Carlssond2490a92009-12-24 20:40:36 +00001413 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001414 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001415 DestIsVolatile = false;
1416 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001417
Chris Lattner117e3f42010-07-30 04:02:24 +00001418 // If the value is offset in memory, apply the offset now.
1419 llvm::Value *StorePtr = DestPtr;
1420 if (unsigned Offs = RetAI.getDirectOffset()) {
1421 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1422 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001423 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001424 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1425 }
1426 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001427
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001428 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001429 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001430 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001431 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001432 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001433 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001434 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001435
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001436 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001437 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001438 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001439
1440 assert(0 && "Unhandled ABIArgInfo::Kind");
1441 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001442}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001443
1444/* VarArg handling */
1445
1446llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1447 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1448}