blob: 1116053b52a72e314a70ba98a2f688fd5182588f [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;
Tilmann Scheller88d117c2011-03-02 19:36:23 +000039 case CC_Win64ThisCall: return llvm::CallingConv::Win64_ThisCall;
Dawn Perchik52fc3142010-09-03 01:29:35 +000040 // TODO: add support for CC_X86Pascal to llvm
John McCall04a67a62010-02-05 21:31:56 +000041 }
42}
43
John McCall0b0ef0a2010-02-24 07:14:12 +000044/// Derives the 'this' type for codegen purposes, i.e. ignoring method
45/// qualification.
46/// FIXME: address space qualification?
John McCallead608a2010-02-26 00:48:12 +000047static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
48 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
49 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000050}
51
John McCall0b0ef0a2010-02-24 07:14:12 +000052/// Returns the canonical formal type of the given C++ method.
John McCallead608a2010-02-26 00:48:12 +000053static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
54 return MD->getType()->getCanonicalTypeUnqualified()
55 .getAs<FunctionProtoType>();
John McCall0b0ef0a2010-02-24 07:14:12 +000056}
57
58/// Returns the "extra-canonicalized" return type, which discards
59/// qualifiers on the return type. Codegen doesn't care about them,
60/// and it makes ABI code a little easier to be able to assume that
61/// all parameter and return types are top-level unqualified.
John McCallead608a2010-02-26 00:48:12 +000062static CanQualType GetReturnType(QualType RetTy) {
63 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall0b0ef0a2010-02-24 07:14:12 +000064}
65
66const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000067CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
68 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000069 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
70 llvm::SmallVector<CanQualType, 16>(),
Chris Lattnerbcaedae2010-06-30 19:14:05 +000071 FTNP->getExtInfo(), IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000072}
73
74/// \param Args - contains any initial parameters besides those
75/// in the formal type
76static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCallead608a2010-02-26 00:48:12 +000077 llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +000078 CanQual<FunctionProtoType> FTP,
Tilmann Scheller88d117c2011-03-02 19:36:23 +000079 CallingConv CC,
Chris Lattnerbcaedae2010-06-30 19:14:05 +000080 bool IsRecursive = false) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000082 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000083 ArgTys.push_back(FTP->getArgType(i));
John McCallead608a2010-02-26 00:48:12 +000084 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
Tilmann Scheller88d117c2011-03-02 19:36:23 +000085
86 return CGT.getFunctionInfo(ResTy, ArgTys,
87 FTP->getExtInfo().withCallingConv(CC),
88 IsRecursive);
John McCall0b0ef0a2010-02-24 07:14:12 +000089}
90
91const CGFunctionInfo &
Chris Lattnerbcaedae2010-06-30 19:14:05 +000092CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
93 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +000094 llvm::SmallVector<CanQualType, 16> ArgTys;
Tilmann Scheller88d117c2011-03-02 19:36:23 +000095 return ::getFunctionInfo(*this, ArgTys, FTP, CC_Default, IsRecursive);
Daniel Dunbarbac7c252009-09-11 22:24:53 +000096}
97
John McCall04a67a62010-02-05 21:31:56 +000098static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000099 // Set the appropriate calling convention for the Function.
100 if (D->hasAttr<StdCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +0000101 return CC_X86StdCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000102
103 if (D->hasAttr<FastCallAttr>())
John McCall04a67a62010-02-05 21:31:56 +0000104 return CC_X86FastCall;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000105
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000106 if (D->hasAttr<ThisCallAttr>())
107 return CC_X86ThisCall;
108
Dawn Perchik52fc3142010-09-03 01:29:35 +0000109 if (D->hasAttr<PascalAttr>())
110 return CC_X86Pascal;
111
John McCall04a67a62010-02-05 21:31:56 +0000112 return CC_C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000113}
114
Anders Carlsson375c31c2009-10-03 19:43:08 +0000115const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
116 const FunctionProtoType *FTP) {
John McCallead608a2010-02-26 00:48:12 +0000117 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000118
Anders Carlsson375c31c2009-10-03 19:43:08 +0000119 // Add the 'this' pointer.
John McCall0b0ef0a2010-02-24 07:14:12 +0000120 ArgTys.push_back(GetThisType(Context, RD));
121
Tilmann Scheller88d117c2011-03-02 19:36:23 +0000122 CallingConv CC = Context.Target.isWin64() ? CC_Win64ThisCall : CC_Default;
123
John McCall0b0ef0a2010-02-24 07:14:12 +0000124 return ::getFunctionInfo(*this, ArgTys,
Tilmann Scheller88d117c2011-03-02 19:36:23 +0000125 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>(), CC);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000126}
127
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000128const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000129 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000130
John McCallfc400282010-09-03 01:26:39 +0000131 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
132 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
133
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000134 // Add the 'this' pointer unless this is a static method.
135 if (MD->isInstance())
John McCall0b0ef0a2010-02-24 07:14:12 +0000136 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Tilmann Scheller88d117c2011-03-02 19:36:23 +0000138 CallingConv CC = Context.Target.isWin64() ? CC_Win64ThisCall : CC_Default;
139
140 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD), CC);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000141}
142
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000143const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000144 CXXCtorType Type) {
John McCallead608a2010-02-26 00:48:12 +0000145 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall0b0ef0a2010-02-24 07:14:12 +0000146 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000147 CanQualType ResTy = Context.VoidTy;
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000148
John McCall4c40d982010-08-31 07:33:07 +0000149 TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
John McCall0b0ef0a2010-02-24 07:14:12 +0000150
John McCall4c40d982010-08-31 07:33:07 +0000151 CanQual<FunctionProtoType> FTP = GetFormalType(D);
152
153 // Add the formal parameters.
154 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
155 ArgTys.push_back(FTP->getArgType(i));
156
Tilmann Scheller88d117c2011-03-02 19:36:23 +0000157 CallingConv CC = Context.Target.isWin64() ? CC_Win64ThisCall : CC_Default;
158
159 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo().withCallingConv(CC));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000160}
161
162const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
163 CXXDtorType Type) {
John McCall4c40d982010-08-31 07:33:07 +0000164 llvm::SmallVector<CanQualType, 2> ArgTys;
John McCallead608a2010-02-26 00:48:12 +0000165 ArgTys.push_back(GetThisType(Context, D->getParent()));
John McCall4c40d982010-08-31 07:33:07 +0000166 CanQualType ResTy = Context.VoidTy;
John McCall0b0ef0a2010-02-24 07:14:12 +0000167
John McCall4c40d982010-08-31 07:33:07 +0000168 TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
169
170 CanQual<FunctionProtoType> FTP = GetFormalType(D);
171 assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
172
Tilmann Scheller88d117c2011-03-02 19:36:23 +0000173 CallingConv CC = Context.Target.isWin64() ? CC_Win64ThisCall : CC_Default;
174
175 return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo().withCallingConv(CC));
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000176}
177
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000178const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000179 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000180 if (MD->isInstance())
181 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
John McCallead608a2010-02-26 00:48:12 +0000183 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
184 assert(isa<FunctionType>(FTy));
John McCall0b0ef0a2010-02-24 07:14:12 +0000185 if (isa<FunctionNoProtoType>(FTy))
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000186 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
John McCallead608a2010-02-26 00:48:12 +0000187 assert(isa<FunctionProtoType>(FTy));
188 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000189}
190
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000191const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCallead608a2010-02-26 00:48:12 +0000192 llvm::SmallVector<CanQualType, 16> ArgTys;
193 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
194 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000195 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000196 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall0b0ef0a2010-02-24 07:14:12 +0000197 e = MD->param_end(); i != e; ++i) {
198 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
199 }
200 return getFunctionInfo(GetReturnType(MD->getResultType()),
201 ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000202 FunctionType::ExtInfo(
203 /*NoReturn*/ false,
Rafael Espindola425ef722010-03-30 22:15:11 +0000204 /*RegParm*/ 0,
Rafael Espindola264ba482010-03-30 20:24:48 +0000205 getCallingConventionForDecl(MD)));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000206}
207
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000208const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
209 // FIXME: Do we need to handle ObjCMethodDecl?
210 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000211
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000212 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
213 return getFunctionInfo(CD, GD.getCtorType());
214
215 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
216 return getFunctionInfo(DD, GD.getDtorType());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000217
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000218 return getFunctionInfo(FD);
219}
220
Mike Stump1eb44332009-09-09 15:08:12 +0000221const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000222 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000223 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000224 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000225 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000226 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000227 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000228 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000229 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000230}
231
Mike Stump1eb44332009-09-09 15:08:12 +0000232const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000233 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000234 const FunctionType::ExtInfo &Info) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000235 // FIXME: Kill copy.
John McCallead608a2010-02-26 00:48:12 +0000236 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000237 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000238 i != e; ++i)
John McCall0b0ef0a2010-02-24 07:14:12 +0000239 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindola264ba482010-03-30 20:24:48 +0000240 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000241}
242
John McCallead608a2010-02-26 00:48:12 +0000243const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
244 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000245 const FunctionType::ExtInfo &Info,
246 bool IsRecursive) {
John McCallead608a2010-02-26 00:48:12 +0000247#ifndef NDEBUG
248 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
249 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
250 assert(I->isCanonicalAsParam());
251#endif
252
Rafael Espindola425ef722010-03-30 22:15:11 +0000253 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCall04a67a62010-02-05 21:31:56 +0000254
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000255 // Lookup or create unique function info.
256 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +0000257 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000258 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000259
260 void *InsertPos = 0;
261 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
262 if (FI)
263 return *FI;
264
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000265 // Construct the function info.
Tilmann Scheller88d117c2011-03-02 19:36:23 +0000266 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(),
267 ResTy, ArgTys.data(), ArgTys.size());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000268 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000269
270 // Compute ABI information.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000271 getABIInfo().computeInfo(*FI);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000272
Chris Lattner800588f2010-07-29 06:26:06 +0000273 // Loop over all of the computed argument and return value info. If any of
274 // them are direct or extend without a specified coerce type, specify the
275 // default now.
276 ABIArgInfo &RetInfo = FI->getReturnInfo();
277 if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
278 RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000279
Chris Lattner800588f2010-07-29 06:26:06 +0000280 for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
281 I != E; ++I)
282 if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
283 I->info.setCoerceToType(ConvertTypeRecursive(I->type));
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000284
Chris Lattnera9fa8582010-07-01 06:20:47 +0000285 // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
286 // types, resolve them now. These pointers may point to this function, which
287 // we *just* filled in the FunctionInfo for.
Chris Lattneree5dcd02010-07-29 02:31:05 +0000288 if (!IsRecursive && !PointersToResolve.empty())
Chris Lattnera9fa8582010-07-01 06:20:47 +0000289 HandleLateResolvedPointers();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000290
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000291 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000292}
293
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000294CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
Chris Lattnerbb521142010-06-29 18:13:52 +0000295 bool _NoReturn, unsigned _RegParm,
John McCallead608a2010-02-26 00:48:12 +0000296 CanQualType ResTy,
Chris Lattnerbb521142010-06-29 18:13:52 +0000297 const CanQualType *ArgTys,
298 unsigned NumArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000299 : CallingConvention(_CallingConvention),
John McCall04a67a62010-02-05 21:31:56 +0000300 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola425ef722010-03-30 22:15:11 +0000301 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000302{
Chris Lattnerbb521142010-06-29 18:13:52 +0000303 NumArgs = NumArgTys;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000304
Chris Lattnerce700162010-06-28 23:44:11 +0000305 // FIXME: Coallocate with the CGFunctionInfo object.
Chris Lattnerbb521142010-06-29 18:13:52 +0000306 Args = new ArgInfo[1 + NumArgTys];
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000307 Args[0].type = ResTy;
Chris Lattnerbb521142010-06-29 18:13:52 +0000308 for (unsigned i = 0; i != NumArgTys; ++i)
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000309 Args[1 + i].type = ArgTys[i];
310}
311
312/***/
313
Mike Stump1eb44332009-09-09 15:08:12 +0000314void CodeGenTypes::GetExpandedTypes(QualType Ty,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000315 std::vector<const llvm::Type*> &ArgTys,
316 bool IsRecursive) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000317 const RecordType *RT = Ty->getAsStructureType();
318 assert(RT && "Can only expand structure types.");
319 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000320 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000321 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000323 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
324 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000325 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000326 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000327 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Daniel Dunbar56273772008-09-17 00:51:38 +0000329 QualType FT = FD->getType();
Chris Lattnerdeabde22010-07-28 18:24:28 +0000330 if (CodeGenFunction::hasAggregateLLVMType(FT))
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000331 GetExpandedTypes(FT, ArgTys, IsRecursive);
Chris Lattnerdeabde22010-07-28 18:24:28 +0000332 else
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000333 ArgTys.push_back(ConvertType(FT, IsRecursive));
Daniel Dunbar56273772008-09-17 00:51:38 +0000334 }
335}
336
Mike Stump1eb44332009-09-09 15:08:12 +0000337llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000338CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
339 llvm::Function::arg_iterator AI) {
340 const RecordType *RT = Ty->getAsStructureType();
341 assert(RT && "Can only expand structure types.");
342
343 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000344 assert(LV.isSimple() &&
345 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000346 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000347 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
348 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000349 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000350 QualType FT = FD->getType();
351
352 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000353 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000354 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
355 AI = ExpandTypeFromArgs(FT, LV, AI);
356 } else {
357 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
358 ++AI;
359 }
360 }
361
362 return AI;
363}
364
Mike Stump1eb44332009-09-09 15:08:12 +0000365void
366CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000367 llvm::SmallVector<llvm::Value*, 16> &Args) {
368 const RecordType *RT = Ty->getAsStructureType();
369 assert(RT && "Can only expand structure types.");
370
371 RecordDecl *RD = RT->getDecl();
372 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
373 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000374 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
375 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000376 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000377 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Daniel Dunbar56273772008-09-17 00:51:38 +0000379 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000380 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000381 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
382 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
383 } else {
384 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000385 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000386 "Unexpected non-scalar rvalue during struct expansion.");
387 Args.push_back(RV.getScalarVal());
388 }
389 }
390}
391
Chris Lattnere7bb7772010-06-27 06:04:18 +0000392/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner08dd2a02010-06-27 05:56:15 +0000393/// accessing some number of bytes out of it, try to gep into the struct to get
394/// at its inner goodness. Dive as deep as possible without entering an element
395/// with an in-memory size smaller than DstSize.
396static llvm::Value *
Chris Lattnere7bb7772010-06-27 06:04:18 +0000397EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
398 const llvm::StructType *SrcSTy,
399 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner08dd2a02010-06-27 05:56:15 +0000400 // We can't dive into a zero-element struct.
401 if (SrcSTy->getNumElements() == 0) return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000402
Chris Lattner08dd2a02010-06-27 05:56:15 +0000403 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000404
Chris Lattner08dd2a02010-06-27 05:56:15 +0000405 // If the first elt is at least as large as what we're looking for, or if the
406 // first element is the same size as the whole struct, we can enter it.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000407 uint64_t FirstEltSize =
Chris Lattner08dd2a02010-06-27 05:56:15 +0000408 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000409 if (FirstEltSize < DstSize &&
Chris Lattner08dd2a02010-06-27 05:56:15 +0000410 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
411 return SrcPtr;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000412
Chris Lattner08dd2a02010-06-27 05:56:15 +0000413 // GEP into the first element.
414 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000415
Chris Lattner08dd2a02010-06-27 05:56:15 +0000416 // If the first element is a struct, recurse.
417 const llvm::Type *SrcTy =
418 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
419 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattnere7bb7772010-06-27 06:04:18 +0000420 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000421
422 return SrcPtr;
423}
424
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000425/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
426/// are either integers or pointers. This does a truncation of the value if it
427/// is too large or a zero extension if it is too small.
428static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
429 const llvm::Type *Ty,
430 CodeGenFunction &CGF) {
431 if (Val->getType() == Ty)
432 return Val;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000433
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000434 if (isa<llvm::PointerType>(Val->getType())) {
435 // If this is Pointer->Pointer avoid conversion to and from int.
436 if (isa<llvm::PointerType>(Ty))
437 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000438
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000439 // Convert the pointer to an integer so we can play with its width.
Chris Lattner77b89b82010-06-27 07:15:29 +0000440 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000441 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000442
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000443 const llvm::Type *DestIntTy = Ty;
444 if (isa<llvm::PointerType>(DestIntTy))
Chris Lattner77b89b82010-06-27 07:15:29 +0000445 DestIntTy = CGF.IntPtrTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000446
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000447 if (Val->getType() != DestIntTy)
448 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000449
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000450 if (isa<llvm::PointerType>(Ty))
451 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
452 return Val;
453}
454
Chris Lattner08dd2a02010-06-27 05:56:15 +0000455
456
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000457/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
458/// a pointer to an object of type \arg Ty.
459///
460/// This safely handles the case when the src type is smaller than the
461/// destination type; in this situation the values of bits which not
462/// present in the src are undefined.
463static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
464 const llvm::Type *Ty,
465 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000466 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000467 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000468
Chris Lattner6ae00692010-06-28 22:51:39 +0000469 // If SrcTy and Ty are the same, just do a load.
470 if (SrcTy == Ty)
471 return CGF.Builder.CreateLoad(SrcPtr);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000472
Duncan Sands9408c452009-05-09 07:08:47 +0000473 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000474
Chris Lattner08dd2a02010-06-27 05:56:15 +0000475 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattnere7bb7772010-06-27 06:04:18 +0000476 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner08dd2a02010-06-27 05:56:15 +0000477 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
478 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000479
Chris Lattner08dd2a02010-06-27 05:56:15 +0000480 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000481
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000482 // If the source and destination are integer or pointer types, just do an
483 // extension or truncation to the desired type.
484 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
485 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
486 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
487 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
488 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000489
Daniel Dunbarb225be42009-02-03 05:59:18 +0000490 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000491 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000492 // Generally SrcSize is never greater than DstSize, since this means we are
493 // losing bits. However, this can happen in cases where the structure has
494 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000495 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000496 // FIXME: Assert that we aren't truncating non-padding bits when have access
497 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000498 llvm::Value *Casted =
499 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000500 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
501 // FIXME: Use better alignment / avoid requiring aligned load.
502 Load->setAlignment(1);
503 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000504 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000505
Chris Lattner35b21b82010-06-27 01:06:27 +0000506 // Otherwise do coercion through memory. This is stupid, but
507 // simple.
508 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
509 llvm::Value *Casted =
510 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
511 llvm::StoreInst *Store =
512 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
513 // FIXME: Use better alignment / avoid requiring aligned store.
514 Store->setAlignment(1);
515 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000516}
517
518/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
519/// where the source and destination may have different types.
520///
521/// This safely handles the case when the src type is larger than the
522/// destination type; the upper bits of the src will be lost.
523static void CreateCoercedStore(llvm::Value *Src,
524 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000525 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000526 CodeGenFunction &CGF) {
527 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000528 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000529 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
Chris Lattner6ae00692010-06-28 22:51:39 +0000530 if (SrcTy == DstTy) {
531 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
532 return;
533 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000534
Chris Lattner6ae00692010-06-28 22:51:39 +0000535 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000536
Chris Lattnere7bb7772010-06-27 06:04:18 +0000537 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
538 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
539 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
540 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000541
Chris Lattner6d11cdb2010-06-27 06:26:04 +0000542 // If the source and destination are integer or pointer types, just do an
543 // extension or truncation to the desired type.
544 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
545 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
546 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
547 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
548 return;
549 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000550
Duncan Sands9408c452009-05-09 07:08:47 +0000551 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000552
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000553 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000554 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000555 llvm::Value *Casted =
556 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000557 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000558 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000559 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000560 // Otherwise do coercion through memory. This is stupid, but
561 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000562
563 // Generally SrcSize is never greater than DstSize, since this means we are
564 // losing bits. However, this can happen in cases where the structure has
565 // additional padding, for example due to a user specified alignment.
566 //
567 // FIXME: Assert that we aren't truncating non-padding bits when have access
568 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000569 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
570 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000571 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000572 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000573 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
574 // FIXME: Use better alignment / avoid requiring aligned load.
575 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000576 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000577 }
578}
579
Daniel Dunbar56273772008-09-17 00:51:38 +0000580/***/
581
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000582bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000583 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000584}
585
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000586bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
587 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
588 switch (BT->getKind()) {
589 default:
590 return false;
591 case BuiltinType::Float:
592 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
593 case BuiltinType::Double:
594 return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
595 case BuiltinType::LongDouble:
596 return getContext().Target.useObjCFPRetForRealType(
597 TargetInfo::LongDouble);
598 }
599 }
600
601 return false;
602}
603
John McCallc0bf4622010-02-23 00:48:20 +0000604const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
605 const CGFunctionInfo &FI = getFunctionInfo(GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000606
John McCallc0bf4622010-02-23 00:48:20 +0000607 // For definition purposes, don't consider a K&R function variadic.
608 bool Variadic = false;
609 if (const FunctionProtoType *FPT =
610 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
611 Variadic = FPT->isVariadic();
612
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000613 return GetFunctionType(FI, Variadic, false);
John McCallc0bf4622010-02-23 00:48:20 +0000614}
615
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000616const llvm::FunctionType *
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000617CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
618 bool IsRecursive) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000619 std::vector<const llvm::Type*> ArgTys;
620
621 const llvm::Type *ResultType = 0;
622
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000623 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000624 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000625 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000626 case ABIArgInfo::Expand:
627 assert(0 && "Invalid ABI kind for return argument");
628
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000629 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000630 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000631 ResultType = RetAI.getCoerceToType();
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000632 break;
633
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000634 case ABIArgInfo::Indirect: {
635 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000636 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000637 const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000638 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
639 break;
640 }
641
Daniel Dunbar11434922009-01-26 21:26:08 +0000642 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000643 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000644 break;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
647 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000648 ie = FI.arg_end(); it != ie; ++it) {
649 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000651 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000652 case ABIArgInfo::Ignore:
653 break;
654
Chris Lattner800588f2010-07-29 06:26:06 +0000655 case ABIArgInfo::Indirect: {
656 // indirect arguments are always on the stack, which is addr space #0.
657 const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
658 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
659 break;
660 }
661
662 case ABIArgInfo::Extend:
Chris Lattner1ed72672010-07-29 06:44:09 +0000663 case ABIArgInfo::Direct: {
Chris Lattnerce700162010-06-28 23:44:11 +0000664 // If the coerce-to type is a first class aggregate, flatten it. Either
665 // way is semantically identical, but fast-isel and the optimizer
666 // generally likes scalar values better than FCAs.
667 const llvm::Type *ArgTy = AI.getCoerceToType();
668 if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
669 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
670 ArgTys.push_back(STy->getElementType(i));
671 } else {
672 ArgTys.push_back(ArgTy);
673 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000674 break;
Chris Lattner1ed72672010-07-29 06:44:09 +0000675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000677 case ABIArgInfo::Expand:
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000678 GetExpandedTypes(it->type, ArgTys, IsRecursive);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000679 break;
680 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000681 }
682
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000683 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000684}
685
John McCall4c40d982010-08-31 07:33:07 +0000686const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
687 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlssonecf282b2009-11-24 05:08:52 +0000688 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000689
John McCall4c40d982010-08-31 07:33:07 +0000690 if (!VerifyFuncTypeComplete(FPT)) {
691 const CGFunctionInfo *Info;
692 if (isa<CXXDestructorDecl>(MD))
693 Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
694 else
695 Info = &getFunctionInfo(MD);
696 return GetFunctionType(*Info, FPT->isVariadic(), false);
697 }
Anders Carlssonecf282b2009-11-24 05:08:52 +0000698
699 return llvm::OpaqueType::get(getLLVMContext());
700}
701
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000702void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000703 const Decl *TargetDecl,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000704 AttributeListType &PAL,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000705 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000706 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000707 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000708
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000709 CallingConv = FI.getEffectiveCallingConvention();
710
John McCall04a67a62010-02-05 21:31:56 +0000711 if (FI.isNoReturn())
712 FuncAttrs |= llvm::Attribute::NoReturn;
713
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000714 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000715 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000716 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000717 FuncAttrs |= llvm::Attribute::NoUnwind;
John McCall9c0c1f32010-07-08 06:48:12 +0000718 else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
719 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
720 if (FPT && FPT->hasEmptyExceptionSpec())
721 FuncAttrs |= llvm::Attribute::NoUnwind;
722 }
723
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000724 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000725 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000726 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000727 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000728 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000729 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000730 if (TargetDecl->hasAttr<MallocAttr>())
731 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000732 }
733
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000734 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000735 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000736 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000737 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000738 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000739 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000740
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000741 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000742 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000743 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000744 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000745 case ABIArgInfo::Extend:
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000746 if (RetTy->hasSignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000747 RetAttrs |= llvm::Attribute::SExt;
Chris Lattner2eb9cdd2010-07-28 23:46:15 +0000748 else if (RetTy->hasUnsignedIntegerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000749 RetAttrs |= llvm::Attribute::ZExt;
Chris Lattner800588f2010-07-29 06:26:06 +0000750 break;
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000751 case ABIArgInfo::Direct:
Chris Lattner800588f2010-07-29 06:26:06 +0000752 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000753 break;
754
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000755 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000756 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattnerfb97cf22010-04-20 05:44:43 +0000757 llvm::Attribute::StructRet));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000758 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000759 // sret disables readnone and readonly
760 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
761 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000762 break;
763
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000764 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000765 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000766 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000767
Devang Patela2c69122008-09-26 22:53:57 +0000768 if (RetAttrs)
769 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000770
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000771 // FIXME: RegParm should be reduced in case of global register variable.
Rafael Espindola425ef722010-03-30 22:15:11 +0000772 signed RegParm = FI.getRegParm();
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000773 if (!RegParm)
774 RegParm = CodeGenOpts.NumRegisterParameters;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000775
776 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000777 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000778 ie = FI.arg_end(); it != ie; ++it) {
779 QualType ParamType = it->type;
780 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000781 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000782
John McCalld8e10d22010-03-27 00:47:27 +0000783 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
784 // have the corresponding parameter variable. It doesn't make
Daniel Dunbar7f6890e2011-02-10 18:10:07 +0000785 // sense to do it here because parameters are so messed up.
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000786 switch (AI.getKind()) {
Chris Lattner800588f2010-07-29 06:26:06 +0000787 case ABIArgInfo::Extend:
788 if (ParamType->isSignedIntegerType())
789 Attributes |= llvm::Attribute::SExt;
790 else if (ParamType->isUnsignedIntegerType())
791 Attributes |= llvm::Attribute::ZExt;
792 // FALL THROUGH
793 case ABIArgInfo::Direct:
794 if (RegParm > 0 &&
795 (ParamType->isIntegerType() || ParamType->isPointerType())) {
796 RegParm -=
797 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
798 if (RegParm >= 0)
799 Attributes |= llvm::Attribute::InReg;
800 }
801 // FIXME: handle sseregparm someday...
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000802
Chris Lattnerce700162010-06-28 23:44:11 +0000803 if (const llvm::StructType *STy =
Chris Lattner800588f2010-07-29 06:26:06 +0000804 dyn_cast<llvm::StructType>(AI.getCoerceToType()))
805 Index += STy->getNumElements()-1; // 1 will be added below.
806 break;
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000807
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000808 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000809 if (AI.getIndirectByVal())
810 Attributes |= llvm::Attribute::ByVal;
811
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000812 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000813 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000814 // byval disables readnone and readonly.
815 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
816 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000817 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000818
Daniel Dunbar11434922009-01-26 21:26:08 +0000819 case ABIArgInfo::Ignore:
820 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000821 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000822
Daniel Dunbar56273772008-09-17 00:51:38 +0000823 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000824 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000825 // FIXME: This is rather inefficient. Do we ever actually need to do
826 // anything here? The result should be just reconstructed on the other
827 // side, so extension should be a non-issue.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000828 getTypes().GetExpandedTypes(ParamType, Tys, false);
Daniel Dunbar56273772008-09-17 00:51:38 +0000829 Index += Tys.size();
830 continue;
831 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Devang Patel761d7f72008-09-25 21:02:23 +0000834 if (Attributes)
835 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000836 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000837 }
Devang Patela2c69122008-09-26 22:53:57 +0000838 if (FuncAttrs)
839 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000840}
841
Daniel Dunbar88b53962009-02-02 22:03:45 +0000842void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
843 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000844 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000845 // If this is an implicit-return-zero function, go ahead and
846 // initialize the return value. TODO: it might be nice to have
847 // a more general mechanism for this that didn't require synthesized
848 // return statements.
Chris Lattner121b3fa2010-07-05 20:21:00 +0000849 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000850 if (FD->hasImplicitReturnZero()) {
851 QualType RetTy = FD->getResultType().getUnqualifiedType();
852 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000853 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000854 Builder.CreateStore(Zero, ReturnValue);
855 }
856 }
857
Mike Stumpf5408fe2009-05-16 07:57:57 +0000858 // FIXME: We no longer need the types from FunctionArgList; lift up and
859 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000860
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000861 // Emit allocs for param decls. Give the LLVM Argument nodes names.
862 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000864 // Name the struct return argument.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +0000865 if (CGM.ReturnTypeUsesSRet(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000866 AI->setName("agg.result");
867 ++AI;
868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000870 assert(FI.arg_size() == Args.size() &&
871 "Mismatch between function signature & arguments.");
Devang Patel810b07c2011-03-02 19:11:22 +0000872 unsigned ArgNo = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000873 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Devang Patel810b07c2011-03-02 19:11:22 +0000874 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
875 i != e; ++i, ++info_it, ++ArgNo) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000876 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000877 QualType Ty = info_it->type;
878 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000879
880 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000881 case ABIArgInfo::Indirect: {
Chris Lattnerce700162010-06-28 23:44:11 +0000882 llvm::Value *V = AI;
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000883
Daniel Dunbar1f745982009-02-05 09:16:39 +0000884 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000885 // Aggregates and complex variables are accessed by reference. All we
886 // need to do is realign the value, if requested
887 if (ArgI.getIndirectRealign()) {
888 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
889
890 // Copy from the incoming argument pointer to the temporary with the
891 // appropriate alignment.
892 //
893 // FIXME: We should have a common utility for generating an aggregate
894 // copy.
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000895 const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
Ken Dyckfe710082011-01-19 01:58:38 +0000896 CharUnits Size = getContext().getTypeSizeInChars(Ty);
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000897 Builder.CreateMemCpy(Builder.CreateBitCast(AlignedTemp, I8PtrTy),
898 Builder.CreateBitCast(V, I8PtrTy),
Ken Dyckfe710082011-01-19 01:58:38 +0000899 llvm::ConstantInt::get(IntPtrTy,
900 Size.getQuantity()),
Benjamin Kramer9f0c7cc2010-12-30 00:13:21 +0000901 ArgI.getIndirectAlign(),
902 false);
Daniel Dunbarcf3b6f22010-09-16 20:42:02 +0000903 V = AlignedTemp;
904 }
Daniel Dunbar1f745982009-02-05 09:16:39 +0000905 } else {
906 // Load scalar value from indirect argument.
Ken Dyckfe710082011-01-19 01:58:38 +0000907 CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
908 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000909 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
910 // This must be a promotion, for something like
911 // "void a(x) short x; {..."
912 V = EmitScalarConversion(V, Ty, Arg->getType());
913 }
914 }
Devang Patel810b07c2011-03-02 19:11:22 +0000915 EmitParmDecl(*Arg, V, ArgNo);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000916 break;
917 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000918
919 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000920 case ABIArgInfo::Direct: {
Chris Lattner800588f2010-07-29 06:26:06 +0000921 // If we have the trivial case, handle it with no muss and fuss.
922 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +0000923 ArgI.getCoerceToType() == ConvertType(Ty) &&
924 ArgI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +0000925 assert(AI != Fn->arg_end() && "Argument mismatch!");
926 llvm::Value *V = AI;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000927
John McCalld8e10d22010-03-27 00:47:27 +0000928 if (Arg->getType().isRestrictQualified())
929 AI->addAttr(llvm::Attribute::NoAlias);
930
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000931 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
932 // This must be a promotion, for something like
933 // "void a(x) short x; {..."
934 V = EmitScalarConversion(V, Ty, Arg->getType());
935 }
Devang Patel810b07c2011-03-02 19:11:22 +0000936 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +0000937 break;
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000938 }
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner121b3fa2010-07-05 20:21:00 +0000940 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000941
Chris Lattnerdeabde22010-07-28 18:24:28 +0000942 // The alignment we need to use is the max of the requested alignment for
943 // the argument plus the alignment required by our access code below.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000944 unsigned AlignmentToUse =
John McCalld16c2cf2011-02-08 08:22:06 +0000945 CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
Chris Lattnerdeabde22010-07-28 18:24:28 +0000946 AlignmentToUse = std::max(AlignmentToUse,
947 (unsigned)getContext().getDeclAlign(Arg).getQuantity());
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000948
Chris Lattnerdeabde22010-07-28 18:24:28 +0000949 Alloca->setAlignment(AlignmentToUse);
Chris Lattner121b3fa2010-07-05 20:21:00 +0000950 llvm::Value *V = Alloca;
Chris Lattner117e3f42010-07-30 04:02:24 +0000951 llvm::Value *Ptr = V; // Pointer to store into.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000952
Chris Lattner117e3f42010-07-30 04:02:24 +0000953 // If the value is offset in memory, apply the offset now.
954 if (unsigned Offs = ArgI.getDirectOffset()) {
955 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
956 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000957 Ptr = Builder.CreateBitCast(Ptr,
Chris Lattner117e3f42010-07-30 04:02:24 +0000958 llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
959 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000960
Chris Lattner309c59f2010-06-29 00:06:42 +0000961 // If the coerce-to type is a first class aggregate, we flatten it and
962 // pass the elements. Either way is semantically identical, but fast-isel
963 // and the optimizer generally likes scalar values better than FCAs.
964 if (const llvm::StructType *STy =
965 dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +0000966 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000967
Chris Lattner92826882010-07-05 20:41:41 +0000968 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
969 assert(AI != Fn->arg_end() && "Argument mismatch!");
970 AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
971 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
972 Builder.CreateStore(AI++, EltPtr);
Chris Lattner309c59f2010-06-29 00:06:42 +0000973 }
974 } else {
975 // Simple case, just do a coerced store of the argument into the alloca.
976 assert(AI != Fn->arg_end() && "Argument mismatch!");
Chris Lattner225e2862010-06-29 00:14:52 +0000977 AI->setName(Arg->getName() + ".coerce");
Chris Lattner117e3f42010-07-30 04:02:24 +0000978 CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
Chris Lattner309c59f2010-06-29 00:06:42 +0000979 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000980
981
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000982 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000983 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000984 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000985 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
986 // This must be a promotion, for something like
987 // "void a(x) short x; {..."
988 V = EmitScalarConversion(V, Ty, Arg->getType());
989 }
990 }
Devang Patel810b07c2011-03-02 19:11:22 +0000991 EmitParmDecl(*Arg, V, ArgNo);
Chris Lattnerce700162010-06-28 23:44:11 +0000992 continue; // Skip ++AI increment, already done.
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000993 }
Chris Lattner800588f2010-07-29 06:26:06 +0000994
995 case ABIArgInfo::Expand: {
996 // If this structure was expanded into multiple arguments then
997 // we need to create a temporary and reconstruct it from the
998 // arguments.
999 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Chris Lattner800588f2010-07-29 06:26:06 +00001000 llvm::Function::arg_iterator End =
Daniel Dunbar79c39282010-08-21 03:15:20 +00001001 ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
Devang Patel810b07c2011-03-02 19:11:22 +00001002 EmitParmDecl(*Arg, Temp, ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001003
1004 // Name the arguments used in expansion and increment AI.
1005 unsigned Index = 0;
1006 for (; AI != End; ++AI, ++Index)
1007 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1008 continue;
1009 }
1010
1011 case ABIArgInfo::Ignore:
1012 // Initialize the local variable appropriately.
1013 if (hasAggregateLLVMType(Ty))
Devang Patel810b07c2011-03-02 19:11:22 +00001014 EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001015 else
Devang Patel810b07c2011-03-02 19:11:22 +00001016 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1017 ArgNo);
Chris Lattner800588f2010-07-29 06:26:06 +00001018
1019 // Skip increment, no matching LLVM parameter.
1020 continue;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001021 }
Daniel Dunbar56273772008-09-17 00:51:38 +00001022
1023 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001024 }
1025 assert(AI == Fn->arg_end() && "Argument mismatch!");
1026}
1027
Chris Lattner35b21b82010-06-27 01:06:27 +00001028void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001029 // Functions with no result always return void.
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001030 if (ReturnValue == 0) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001031 Builder.CreateRetVoid();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001032 return;
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001033 }
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001034
Dan Gohman4751a532010-07-20 20:13:52 +00001035 llvm::DebugLoc RetDbgLoc;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001036 llvm::Value *RV = 0;
1037 QualType RetTy = FI.getReturnType();
1038 const ABIArgInfo &RetAI = FI.getReturnInfo();
1039
1040 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001041 case ABIArgInfo::Indirect: {
1042 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001043 if (RetTy->isAnyComplexType()) {
1044 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1045 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1046 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1047 // Do nothing; aggregrates get evaluated directly into the destination.
1048 } else {
1049 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001050 false, Alignment, RetTy);
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001051 }
1052 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001053 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001054
1055 case ABIArgInfo::Extend:
Chris Lattner800588f2010-07-29 06:26:06 +00001056 case ABIArgInfo::Direct:
Chris Lattner117e3f42010-07-30 04:02:24 +00001057 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1058 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001059 // The internal return value temp always will have pointer-to-return-type
1060 // type, just do a load.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001061
Chris Lattner800588f2010-07-29 06:26:06 +00001062 // If the instruction right before the insertion point is a store to the
1063 // return value, we can elide the load, zap the store, and usually zap the
1064 // alloca.
1065 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1066 llvm::StoreInst *SI = 0;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001067 if (InsertBB->empty() ||
Chris Lattner800588f2010-07-29 06:26:06 +00001068 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1069 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1070 RV = Builder.CreateLoad(ReturnValue);
1071 } else {
1072 // Get the stored value and nuke the now-dead store.
1073 RetDbgLoc = SI->getDebugLoc();
1074 RV = SI->getValueOperand();
1075 SI->eraseFromParent();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001076
Chris Lattner800588f2010-07-29 06:26:06 +00001077 // If that was the only use of the return value, nuke it as well now.
1078 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1079 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1080 ReturnValue = 0;
1081 }
Chris Lattner35b21b82010-06-27 01:06:27 +00001082 }
Chris Lattner800588f2010-07-29 06:26:06 +00001083 } else {
Chris Lattner117e3f42010-07-30 04:02:24 +00001084 llvm::Value *V = ReturnValue;
1085 // If the value is offset in memory, apply the offset now.
1086 if (unsigned Offs = RetAI.getDirectOffset()) {
1087 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1088 V = Builder.CreateConstGEP1_32(V, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001089 V = Builder.CreateBitCast(V,
Chris Lattner117e3f42010-07-30 04:02:24 +00001090 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1091 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001092
Chris Lattner117e3f42010-07-30 04:02:24 +00001093 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
Chris Lattner35b21b82010-06-27 01:06:27 +00001094 }
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001095 break;
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001096
Chris Lattner800588f2010-07-29 06:26:06 +00001097 case ABIArgInfo::Ignore:
Chris Lattnerc6e6dd22010-06-26 23:13:19 +00001098 break;
1099
1100 case ABIArgInfo::Expand:
1101 assert(0 && "Invalid ABI kind for return argument");
1102 }
1103
Daniel Dunbar21fcc8f2010-06-30 21:27:58 +00001104 llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
Devang Pateld3f265d2010-07-21 18:08:50 +00001105 if (!RetDbgLoc.isUnknown())
1106 Ret->setDebugLoc(RetDbgLoc);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001107}
1108
John McCall27360712010-05-26 22:34:26 +00001109RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
1110 // StartFunction converted the ABI-lowered parameter(s) into a
1111 // local alloca. We need to turn that into an r-value suitable
1112 // for EmitCall.
1113 llvm::Value *Local = GetAddrOfLocalVar(Param);
1114
1115 QualType ArgType = Param->getType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001116
John McCall27360712010-05-26 22:34:26 +00001117 // For the most part, we just need to load the alloca, except:
1118 // 1) aggregate r-values are actually pointers to temporaries, and
1119 // 2) references to aggregates are pointers directly to the aggregate.
1120 // I don't know why references to non-aggregates are different here.
1121 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
1122 if (hasAggregateLLVMType(RefType->getPointeeType()))
1123 return RValue::getAggregate(Local);
1124
1125 // Locals which are references to scalars are represented
1126 // with allocas holding the pointer.
1127 return RValue::get(Builder.CreateLoad(Local));
1128 }
1129
1130 if (ArgType->isAnyComplexType())
1131 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
1132
1133 if (hasAggregateLLVMType(ArgType))
1134 return RValue::getAggregate(Local);
1135
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001136 unsigned Alignment = getContext().getDeclAlign(Param).getQuantity();
1137 return RValue::get(EmitLoadOfScalar(Local, false, Alignment, ArgType));
John McCall27360712010-05-26 22:34:26 +00001138}
1139
Anders Carlsson0139bb92009-04-08 20:47:54 +00001140RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +00001141 if (ArgType->isReferenceType())
Anders Carlsson32f36ba2010-06-26 16:35:32 +00001142 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Anders Carlsson0139bb92009-04-08 20:47:54 +00001144 return EmitAnyExprToTemp(E);
1145}
1146
John McCallf1549f62010-07-06 01:34:17 +00001147/// Emits a call or invoke instruction to the given function, depending
1148/// on the current state of the EH stack.
1149llvm::CallSite
1150CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1151 llvm::Value * const *ArgBegin,
1152 llvm::Value * const *ArgEnd,
1153 const llvm::Twine &Name) {
1154 llvm::BasicBlock *InvokeDest = getInvokeDest();
1155 if (!InvokeDest)
1156 return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1157
1158 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1159 llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1160 ArgBegin, ArgEnd, Name);
1161 EmitBlock(ContBB);
1162 return Invoke;
1163}
1164
Daniel Dunbar88b53962009-02-02 22:03:45 +00001165RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001166 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00001167 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001168 const CallArgList &CallArgs,
David Chisnalldd5c98f2010-05-01 11:15:56 +00001169 const Decl *TargetDecl,
David Chisnall4b02afc2010-05-02 13:41:58 +00001170 llvm::Instruction **callOrInvoke) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001171 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001172 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001173
1174 // Handle struct-return functions by passing a pointer to the
1175 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001176 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001177 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00001178
1179
Chris Lattner5db7ae52009-06-13 00:26:38 +00001180 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +00001181 // alloca to hold the result, unless one is given to us.
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001182 if (CGM.ReturnTypeUsesSRet(CallInfo)) {
Anders Carlssond2490a92009-12-24 20:40:36 +00001183 llvm::Value *Value = ReturnValue.getValue();
1184 if (!Value)
Daniel Dunbar195337d2010-02-09 02:48:28 +00001185 Value = CreateMemTemp(RetTy);
Anders Carlssond2490a92009-12-24 20:40:36 +00001186 Args.push_back(Value);
1187 }
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +00001189 assert(CallInfo.arg_size() == CallArgs.size() &&
1190 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +00001191 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001192 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +00001193 I != E; ++I, ++info_it) {
1194 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001195 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +00001196
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001197 unsigned Alignment =
1198 getContext().getTypeAlignInChars(I->second).getQuantity();
Daniel Dunbar56273772008-09-17 00:51:38 +00001199 switch (ArgInfo.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001200 case ABIArgInfo::Indirect: {
Daniel Dunbar1f745982009-02-05 09:16:39 +00001201 if (RV.isScalar() || RV.isComplex()) {
1202 // Make a temporary alloca to pass the argument.
Daniel Dunbar195337d2010-02-09 02:48:28 +00001203 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar1f745982009-02-05 09:16:39 +00001204 if (RV.isScalar())
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001205 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1206 Alignment, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001207 else
Mike Stump1eb44332009-09-09 15:08:12 +00001208 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +00001209 } else {
1210 Args.push_back(RV.getAggregateAddr());
1211 }
1212 break;
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001213 }
Daniel Dunbar1f745982009-02-05 09:16:39 +00001214
Daniel Dunbar11434922009-01-26 21:26:08 +00001215 case ABIArgInfo::Ignore:
1216 break;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001217
Chris Lattner800588f2010-07-29 06:26:06 +00001218 case ABIArgInfo::Extend:
1219 case ABIArgInfo::Direct: {
1220 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
Chris Lattner117e3f42010-07-30 04:02:24 +00001221 ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1222 ArgInfo.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001223 if (RV.isScalar())
1224 Args.push_back(RV.getScalarVal());
1225 else
1226 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1227 break;
1228 }
Daniel Dunbar11434922009-01-26 21:26:08 +00001229
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001230 // FIXME: Avoid the conversion through memory if possible.
1231 llvm::Value *SrcPtr;
1232 if (RV.isScalar()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001233 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001234 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1235 I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001236 } else if (RV.isComplex()) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001237 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001238 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001239 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001240 SrcPtr = RV.getAggregateAddr();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001241
Chris Lattner117e3f42010-07-30 04:02:24 +00001242 // If the value is offset in memory, apply the offset now.
1243 if (unsigned Offs = ArgInfo.getDirectOffset()) {
1244 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1245 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001246 SrcPtr = Builder.CreateBitCast(SrcPtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001247 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1248
1249 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001250
Chris Lattnerce700162010-06-28 23:44:11 +00001251 // If the coerce-to type is a first class aggregate, we flatten it and
1252 // pass the elements. Either way is semantically identical, but fast-isel
1253 // and the optimizer generally likes scalar values better than FCAs.
1254 if (const llvm::StructType *STy =
Chris Lattner309c59f2010-06-29 00:06:42 +00001255 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
Chris Lattner92826882010-07-05 20:41:41 +00001256 SrcPtr = Builder.CreateBitCast(SrcPtr,
1257 llvm::PointerType::getUnqual(STy));
1258 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1259 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
Chris Lattnerdeabde22010-07-28 18:24:28 +00001260 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1261 // We don't know what we're loading from.
1262 LI->setAlignment(1);
1263 Args.push_back(LI);
Chris Lattner309c59f2010-06-29 00:06:42 +00001264 }
Chris Lattnerce700162010-06-28 23:44:11 +00001265 } else {
Chris Lattner309c59f2010-06-29 00:06:42 +00001266 // In the simple case, just pass the coerced loaded value.
1267 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1268 *this));
Chris Lattnerce700162010-06-28 23:44:11 +00001269 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001270
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +00001271 break;
1272 }
1273
Daniel Dunbar56273772008-09-17 00:51:38 +00001274 case ABIArgInfo::Expand:
1275 ExpandTypeToArgs(I->second, RV, Args);
1276 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001277 }
1278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Chris Lattner5db7ae52009-06-13 00:26:38 +00001280 // If the callee is a bitcast of a function to a varargs pointer to function
1281 // type, check to see if we can remove the bitcast. This handles some cases
1282 // with unprototyped functions.
1283 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1284 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1285 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1286 const llvm::FunctionType *CurFT =
1287 cast<llvm::FunctionType>(CurPT->getElementType());
1288 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Chris Lattner5db7ae52009-06-13 00:26:38 +00001290 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1291 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +00001292 ActualFT->getNumParams() == CurFT->getNumParams() &&
Fariborz Jahanianc0ddef22011-03-01 17:28:13 +00001293 ActualFT->getNumParams() == Args.size() &&
1294 (CurFT->isVarArg() || !ActualFT->isVarArg())) {
Chris Lattner5db7ae52009-06-13 00:26:38 +00001295 bool ArgsMatch = true;
1296 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1297 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1298 ArgsMatch = false;
1299 break;
1300 }
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Chris Lattner5db7ae52009-06-13 00:26:38 +00001302 // Strip the cast if we can get away with it. This is a nice cleanup,
1303 // but also allows us to inline the function at -O0 if it is marked
1304 // always_inline.
1305 if (ArgsMatch)
1306 Callee = CalleeF;
1307 }
1308 }
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001310
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001311 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +00001312 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001313 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001314 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1315 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001316
John McCallf1549f62010-07-06 01:34:17 +00001317 llvm::BasicBlock *InvokeDest = 0;
1318 if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1319 InvokeDest = getInvokeDest();
1320
Daniel Dunbard14151d2009-03-02 04:32:35 +00001321 llvm::CallSite CS;
John McCallf1549f62010-07-06 01:34:17 +00001322 if (!InvokeDest) {
Jay Foadbeaaccd2009-05-21 09:52:38 +00001323 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001324 } else {
1325 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +00001326 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001327 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001328 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001329 }
Chris Lattnerce933992010-06-29 16:40:28 +00001330 if (callOrInvoke)
David Chisnall4b02afc2010-05-02 13:41:58 +00001331 *callOrInvoke = CS.getInstruction();
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +00001332
Daniel Dunbard14151d2009-03-02 04:32:35 +00001333 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +00001334 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +00001335
1336 // If the call doesn't return, finish the basic block and clear the
1337 // insertion point; this allows the rest of IRgen to discard
1338 // unreachable code.
1339 if (CS.doesNotReturn()) {
1340 Builder.CreateUnreachable();
1341 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Mike Stumpf5408fe2009-05-16 07:57:57 +00001343 // FIXME: For now, emit a dummy basic block because expr emitters in
1344 // generally are not ready to handle emitting expressions at unreachable
1345 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +00001346 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Daniel Dunbard14151d2009-03-02 04:32:35 +00001348 // Return a reasonable RValue.
1349 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001350 }
Daniel Dunbard14151d2009-03-02 04:32:35 +00001351
1352 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +00001353 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001354 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001355
1356 switch (RetAI.getKind()) {
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001357 case ABIArgInfo::Indirect: {
1358 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001359 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +00001360 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +00001361 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +00001362 return RValue::getAggregate(Args[0]);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001363 return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1364 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001365
Daniel Dunbar11434922009-01-26 21:26:08 +00001366 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +00001367 // If we are ignoring an argument that had a result, make sure to
1368 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +00001369 return GetUndefRValue(RetTy);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001370
Chris Lattner800588f2010-07-29 06:26:06 +00001371 case ABIArgInfo::Extend:
1372 case ABIArgInfo::Direct: {
Chris Lattner117e3f42010-07-30 04:02:24 +00001373 if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1374 RetAI.getDirectOffset() == 0) {
Chris Lattner800588f2010-07-29 06:26:06 +00001375 if (RetTy->isAnyComplexType()) {
1376 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1377 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1378 return RValue::getComplex(std::make_pair(Real, Imag));
1379 }
1380 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1381 llvm::Value *DestPtr = ReturnValue.getValue();
1382 bool DestIsVolatile = ReturnValue.isVolatile();
Daniel Dunbar11434922009-01-26 21:26:08 +00001383
Chris Lattner800588f2010-07-29 06:26:06 +00001384 if (!DestPtr) {
1385 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1386 DestIsVolatile = false;
1387 }
1388 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1389 return RValue::getAggregate(DestPtr);
1390 }
1391 return RValue::get(CI);
1392 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001393
Anders Carlssond2490a92009-12-24 20:40:36 +00001394 llvm::Value *DestPtr = ReturnValue.getValue();
1395 bool DestIsVolatile = ReturnValue.isVolatile();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001396
Anders Carlssond2490a92009-12-24 20:40:36 +00001397 if (!DestPtr) {
Daniel Dunbar195337d2010-02-09 02:48:28 +00001398 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +00001399 DestIsVolatile = false;
1400 }
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001401
Chris Lattner117e3f42010-07-30 04:02:24 +00001402 // If the value is offset in memory, apply the offset now.
1403 llvm::Value *StorePtr = DestPtr;
1404 if (unsigned Offs = RetAI.getDirectOffset()) {
1405 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1406 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001407 StorePtr = Builder.CreateBitCast(StorePtr,
Chris Lattner117e3f42010-07-30 04:02:24 +00001408 llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1409 }
1410 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001411
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001412 unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
Anders Carlssonad3d6912008-11-25 22:21:48 +00001413 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001414 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001415 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001416 return RValue::getAggregate(DestPtr);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +00001417 return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001418 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001419
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001420 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001421 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001422 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001423
1424 assert(0 && "Unhandled ABIArgInfo::Kind");
1425 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001426}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001427
1428/* VarArg handling */
1429
1430llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1431 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1432}