blob: 6151fa2ed4cd9736193cd82cacb93cb6e7fdec2c [file] [log] [blame]
Daniel Dunbar3d7c90b2008-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"
16#include "CodeGenFunction.h"
Daniel Dunbarc68897d2008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbard9eff3d2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000019#include "clang/AST/Decl.h"
Anders Carlssonb15b55c2009-04-03 22:48:58 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000021#include "clang/AST/DeclObjC.h"
Chandler Carruth85098242010-06-15 23:19:56 +000022#include "clang/Frontend/CodeGenOptions.h"
Devang Patel3e1f51b2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbarb960b7b2009-03-02 04:32:35 +000024#include "llvm/Support/CallSite.h"
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbar6d6b0d32009-02-03 01:05:53 +000026
27#include "ABIInfo.h"
28
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000034// FIXME: Use iterator and sidestep silly type array creation.
35
John McCallab26cfa2010-02-05 21:31:56 +000036static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
37 switch (CC) {
38 default: return llvm::CallingConv::C;
39 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
40 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
Douglas Gregora941dca2010-05-18 16:57:00 +000041 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
John McCallab26cfa2010-02-05 21:31:56 +000042 }
43}
44
John McCall8ee376f2010-02-24 07:14:12 +000045/// Derives the 'this' type for codegen purposes, i.e. ignoring method
46/// qualification.
47/// FIXME: address space qualification?
John McCall2da83a32010-02-26 00:48:12 +000048static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
49 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
50 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000051}
52
John McCall8ee376f2010-02-24 07:14:12 +000053/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000054static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
55 return MD->getType()->getCanonicalTypeUnqualified()
56 .getAs<FunctionProtoType>();
John McCall8ee376f2010-02-24 07:14:12 +000057}
58
59/// Returns the "extra-canonicalized" return type, which discards
60/// qualifiers on the return type. Codegen doesn't care about them,
61/// and it makes ABI code a little easier to be able to assume that
62/// all parameter and return types are top-level unqualified.
John McCall2da83a32010-02-26 00:48:12 +000063static CanQualType GetReturnType(QualType RetTy) {
64 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000065}
66
67const CGFunctionInfo &
John McCall2da83a32010-02-26 00:48:12 +000068CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
69 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
70 llvm::SmallVector<CanQualType, 16>(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +000071 FTNP->getExtInfo());
John McCall8ee376f2010-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 McCall2da83a32010-02-26 00:48:12 +000077 llvm::SmallVectorImpl<CanQualType> &ArgTys,
78 CanQual<FunctionProtoType> FTP) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000079 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000080 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000081 ArgTys.push_back(FTP->getArgType(i));
John McCall2da83a32010-02-26 00:48:12 +000082 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
83 return CGT.getFunctionInfo(ResTy, ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +000084 FTP->getExtInfo());
John McCall8ee376f2010-02-24 07:14:12 +000085}
86
87const CGFunctionInfo &
John McCall2da83a32010-02-26 00:48:12 +000088CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
89 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +000090 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbar7feafc72009-09-11 22:24:53 +000091}
92
John McCallab26cfa2010-02-05 21:31:56 +000093static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000094 // Set the appropriate calling convention for the Function.
95 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000096 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000097
98 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000099 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000100
Douglas Gregora941dca2010-05-18 16:57:00 +0000101 if (D->hasAttr<ThisCallAttr>())
102 return CC_X86ThisCall;
103
John McCallab26cfa2010-02-05 21:31:56 +0000104 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000105}
106
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000107const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
108 const FunctionProtoType *FTP) {
John McCall2da83a32010-02-26 00:48:12 +0000109 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000110
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000111 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000112 ArgTys.push_back(GetThisType(Context, RD));
113
114 return ::getFunctionInfo(*this, ArgTys,
John McCall2da83a32010-02-26 00:48:12 +0000115 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000116}
117
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000118const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000119 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000120
Chris Lattnerbea5b622009-05-12 20:27:19 +0000121 // Add the 'this' pointer unless this is a static method.
122 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000123 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000124
John McCall8ee376f2010-02-24 07:14:12 +0000125 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000126}
127
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000128const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
129 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000130 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000131
132 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000133 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000134
135 // Check if we need to add a VTT parameter (which has type void **).
136 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
137 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall8ee376f2010-02-24 07:14:12 +0000138
139 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000140}
141
142const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
143 CXXDtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000144 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000145
146 // Add the 'this' pointer.
John McCall2da83a32010-02-26 00:48:12 +0000147 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000148
149 // Check if we need to add a VTT parameter (which has type void **).
150 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
151 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall8ee376f2010-02-24 07:14:12 +0000152
153 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000154}
155
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000156const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000157 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000158 if (MD->isInstance())
159 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000160
John McCall2da83a32010-02-26 00:48:12 +0000161 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
162 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000163 if (isa<FunctionNoProtoType>(FTy))
John McCall2da83a32010-02-26 00:48:12 +0000164 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
165 assert(isa<FunctionProtoType>(FTy));
166 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000167}
168
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000169const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000170 llvm::SmallVector<CanQualType, 16> ArgTys;
171 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
172 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000173 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000174 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000175 e = MD->param_end(); i != e; ++i) {
176 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
177 }
178 return getFunctionInfo(GetReturnType(MD->getResultType()),
179 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000180 FunctionType::ExtInfo(
181 /*NoReturn*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000182 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000183 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000184}
185
Anders Carlsson6710c532010-02-06 02:44:09 +0000186const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
187 // FIXME: Do we need to handle ObjCMethodDecl?
188 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
189
190 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
191 return getFunctionInfo(CD, GD.getCtorType());
192
193 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
194 return getFunctionInfo(DD, GD.getDtorType());
195
196 return getFunctionInfo(FD);
197}
198
Mike Stump11289f42009-09-09 15:08:12 +0000199const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000200 const CallArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000201 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000202 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000203 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000204 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000205 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000206 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000207 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000208}
209
Mike Stump11289f42009-09-09 15:08:12 +0000210const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000211 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000212 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000213 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000214 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000215 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000216 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000217 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000218 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000219}
220
John McCall2da83a32010-02-26 00:48:12 +0000221const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
222 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000223 const FunctionType::ExtInfo &Info) {
John McCall2da83a32010-02-26 00:48:12 +0000224#ifndef NDEBUG
225 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
226 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
227 assert(I->isCanonicalAsParam());
228#endif
229
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000230 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000231
Daniel Dunbare0be8292009-02-03 00:07:12 +0000232 // Lookup or create unique function info.
233 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000234 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000235 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000236
237 void *InsertPos = 0;
238 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
239 if (FI)
240 return *FI;
241
Daniel Dunbar313321e2009-02-03 05:31:23 +0000242 // Construct the function info.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000243 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy, ArgTys);
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000244 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000245
246 // Compute ABI information.
Owen Anderson170229f2009-07-14 23:10:40 +0000247 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar313321e2009-02-03 05:31:23 +0000248
Daniel Dunbare0be8292009-02-03 00:07:12 +0000249 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000250}
251
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000252CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCallab26cfa2010-02-05 21:31:56 +0000253 bool _NoReturn,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000254 unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000255 CanQualType ResTy,
256 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000257 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000258 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000259 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000260{
Daniel Dunbar313321e2009-02-03 05:31:23 +0000261 NumArgs = ArgTys.size();
262 Args = new ArgInfo[1 + NumArgs];
263 Args[0].type = ResTy;
264 for (unsigned i = 0; i < NumArgs; ++i)
265 Args[1 + i].type = ArgTys[i];
266}
267
268/***/
269
Mike Stump11289f42009-09-09 15:08:12 +0000270void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000271 std::vector<const llvm::Type*> &ArgTys) {
272 const RecordType *RT = Ty->getAsStructureType();
273 assert(RT && "Can only expand structure types.");
274 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000275 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000276 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000277
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000278 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
279 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000280 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000281 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000282 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000283
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000284 QualType FT = FD->getType();
285 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
286 GetExpandedTypes(FT, ArgTys);
287 } else {
288 ArgTys.push_back(ConvertType(FT));
289 }
290 }
291}
292
Mike Stump11289f42009-09-09 15:08:12 +0000293llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000294CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
295 llvm::Function::arg_iterator AI) {
296 const RecordType *RT = Ty->getAsStructureType();
297 assert(RT && "Can only expand structure types.");
298
299 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000300 assert(LV.isSimple() &&
301 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000302 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000303 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
304 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000305 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000306 QualType FT = FD->getType();
307
308 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000309 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000310 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
311 AI = ExpandTypeFromArgs(FT, LV, AI);
312 } else {
313 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
314 ++AI;
315 }
316 }
317
318 return AI;
319}
320
Mike Stump11289f42009-09-09 15:08:12 +0000321void
322CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000323 llvm::SmallVector<llvm::Value*, 16> &Args) {
324 const RecordType *RT = Ty->getAsStructureType();
325 assert(RT && "Can only expand structure types.");
326
327 RecordDecl *RD = RT->getDecl();
328 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
329 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000330 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
331 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000332 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000333 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000334
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000335 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000336 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000337 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
338 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
339 } else {
340 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000341 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000342 "Unexpected non-scalar rvalue during struct expansion.");
343 Args.push_back(RV.getScalarVal());
344 }
345 }
346}
347
Chris Lattner895c52b2010-06-27 06:04:18 +0000348/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
Chris Lattner1cd66982010-06-27 05:56:15 +0000349/// accessing some number of bytes out of it, try to gep into the struct to get
350/// at its inner goodness. Dive as deep as possible without entering an element
351/// with an in-memory size smaller than DstSize.
352static llvm::Value *
Chris Lattner895c52b2010-06-27 06:04:18 +0000353EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
354 const llvm::StructType *SrcSTy,
355 uint64_t DstSize, CodeGenFunction &CGF) {
Chris Lattner1cd66982010-06-27 05:56:15 +0000356 // We can't dive into a zero-element struct.
357 if (SrcSTy->getNumElements() == 0) return SrcPtr;
358
359 const llvm::Type *FirstElt = SrcSTy->getElementType(0);
360
361 // If the first elt is at least as large as what we're looking for, or if the
362 // first element is the same size as the whole struct, we can enter it.
363 uint64_t FirstEltSize =
364 CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
365 if (FirstEltSize < DstSize &&
366 FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
367 return SrcPtr;
368
369 // GEP into the first element.
370 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
371
372 // If the first element is a struct, recurse.
373 const llvm::Type *SrcTy =
374 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
375 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
Chris Lattner895c52b2010-06-27 06:04:18 +0000376 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000377
378 return SrcPtr;
379}
380
Chris Lattner055097f2010-06-27 06:26:04 +0000381/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
382/// are either integers or pointers. This does a truncation of the value if it
383/// is too large or a zero extension if it is too small.
384static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
385 const llvm::Type *Ty,
386 CodeGenFunction &CGF) {
387 if (Val->getType() == Ty)
388 return Val;
389
390 if (isa<llvm::PointerType>(Val->getType())) {
391 // If this is Pointer->Pointer avoid conversion to and from int.
392 if (isa<llvm::PointerType>(Ty))
393 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
394
395 // Convert the pointer to an integer so we can play with its width.
396 const llvm::Type *IntPtrTy = llvm::IntegerType::get(Ty->getContext(),
397 CGF.LLVMPointerWidth);
398 Val = CGF.Builder.CreatePtrToInt(Val, IntPtrTy, "coerce.val.pi");
399 }
400
401 const llvm::Type *DestIntTy = Ty;
402 if (isa<llvm::PointerType>(DestIntTy))
403 DestIntTy = llvm::IntegerType::get(Ty->getContext(), CGF.LLVMPointerWidth);
404
405 if (Val->getType() != DestIntTy)
406 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
407
408 if (isa<llvm::PointerType>(Ty))
409 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
410 return Val;
411}
412
Chris Lattner1cd66982010-06-27 05:56:15 +0000413
414
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000415/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
416/// a pointer to an object of type \arg Ty.
417///
418/// This safely handles the case when the src type is smaller than the
419/// destination type; in this situation the values of bits which not
420/// present in the src are undefined.
421static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
422 const llvm::Type *Ty,
423 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000424 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000425 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000426 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Chris Lattner1cd66982010-06-27 05:56:15 +0000427
428 if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
Chris Lattner895c52b2010-06-27 06:04:18 +0000429 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
Chris Lattner1cd66982010-06-27 05:56:15 +0000430 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
431 }
432
433 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000434
Chris Lattner055097f2010-06-27 06:26:04 +0000435 // If the source and destination are integer or pointer types, just do an
436 // extension or truncation to the desired type.
437 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
438 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
439 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
440 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
441 }
442
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000443 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000444 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000445 // Generally SrcSize is never greater than DstSize, since this means we are
446 // losing bits. However, this can happen in cases where the structure has
447 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000448 //
Mike Stump18bb9282009-05-16 07:57:57 +0000449 // FIXME: Assert that we aren't truncating non-padding bits when have access
450 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000451 llvm::Value *Casted =
452 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000453 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
454 // FIXME: Use better alignment / avoid requiring aligned load.
455 Load->setAlignment(1);
456 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000457 }
Chris Lattner3fcc7902010-06-27 01:06:27 +0000458
459 // Otherwise do coercion through memory. This is stupid, but
460 // simple.
461 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
462 llvm::Value *Casted =
463 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
464 llvm::StoreInst *Store =
465 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
466 // FIXME: Use better alignment / avoid requiring aligned store.
467 Store->setAlignment(1);
468 return CGF.Builder.CreateLoad(Tmp);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000469}
470
471/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
472/// where the source and destination may have different types.
473///
474/// This safely handles the case when the src type is larger than the
475/// destination type; the upper bits of the src will be lost.
476static void CreateCoercedStore(llvm::Value *Src,
477 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000478 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000479 CodeGenFunction &CGF) {
480 const llvm::Type *SrcTy = Src->getType();
Chris Lattner895c52b2010-06-27 06:04:18 +0000481 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
482
Mike Stump11289f42009-09-09 15:08:12 +0000483 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000484 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
485
Chris Lattner895c52b2010-06-27 06:04:18 +0000486 if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
487 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
488 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
489 }
490
Chris Lattner055097f2010-06-27 06:26:04 +0000491 // If the source and destination are integer or pointer types, just do an
492 // extension or truncation to the desired type.
493 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
494 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
495 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
496 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
497 return;
498 }
499
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000500 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000501
Daniel Dunbar313321e2009-02-03 05:31:23 +0000502 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000503 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000504 llvm::Value *Casted =
505 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000506 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlsson17490832009-12-24 20:40:36 +0000507 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000508 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000509 // Otherwise do coercion through memory. This is stupid, but
510 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000511
512 // Generally SrcSize is never greater than DstSize, since this means we are
513 // losing bits. However, this can happen in cases where the structure has
514 // additional padding, for example due to a user specified alignment.
515 //
516 // FIXME: Assert that we aren't truncating non-padding bits when have access
517 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000518 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
519 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000520 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000521 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000522 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
523 // FIXME: Use better alignment / avoid requiring aligned load.
524 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000525 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000526 }
527}
528
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000529/***/
530
Daniel Dunbard931a872009-02-02 22:03:45 +0000531bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000532 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000533}
534
John McCallf8ff7b92010-02-23 00:48:20 +0000535const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
536 const CGFunctionInfo &FI = getFunctionInfo(GD);
537
538 // For definition purposes, don't consider a K&R function variadic.
539 bool Variadic = false;
540 if (const FunctionProtoType *FPT =
541 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
542 Variadic = FPT->isVariadic();
543
544 return GetFunctionType(FI, Variadic);
545}
546
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000547const llvm::FunctionType *
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000548CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000549 std::vector<const llvm::Type*> ArgTys;
550
551 const llvm::Type *ResultType = 0;
552
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000553 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000554 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbard3674e62008-09-11 01:48:57 +0000555 switch (RetAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000556 case ABIArgInfo::Expand:
557 assert(0 && "Invalid ABI kind for return argument");
558
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000559 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000560 case ABIArgInfo::Direct:
561 ResultType = ConvertType(RetTy);
562 break;
563
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000564 case ABIArgInfo::Indirect: {
565 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson41a75022009-08-13 21:57:51 +0000566 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000567 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000568 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
569 break;
570 }
571
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000572 case ABIArgInfo::Ignore:
Owen Anderson41a75022009-08-13 21:57:51 +0000573 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000574 break;
575
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000576 case ABIArgInfo::Coerce:
Daniel Dunbar573884e2008-09-10 07:04:09 +0000577 ResultType = RetAI.getCoerceToType();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000578 break;
579 }
Mike Stump11289f42009-09-09 15:08:12 +0000580
581 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000582 ie = FI.arg_end(); it != ie; ++it) {
583 const ABIArgInfo &AI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Daniel Dunbard3674e62008-09-11 01:48:57 +0000585 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000586 case ABIArgInfo::Ignore:
587 break;
588
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000589 case ABIArgInfo::Coerce:
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000590 ArgTys.push_back(AI.getCoerceToType());
591 break;
592
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000593 case ABIArgInfo::Indirect: {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000594 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000595 const llvm::Type *LTy = ConvertTypeForMem(it->type);
596 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000597 break;
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000598 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000599
600 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000601 case ABIArgInfo::Direct:
Daniel Dunbar747865a2009-02-05 09:16:39 +0000602 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000603 break;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Daniel Dunbard3674e62008-09-11 01:48:57 +0000605 case ABIArgInfo::Expand:
Daniel Dunbar313321e2009-02-03 05:31:23 +0000606 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000607 break;
608 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000609 }
610
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000611 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000612}
613
Anders Carlsson64457732009-11-24 05:08:52 +0000614const llvm::Type *
Anders Carlsson11e51402010-04-17 20:15:18 +0000615CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlsson64457732009-11-24 05:08:52 +0000616 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
617
Eli Friedmanc8731be2010-05-30 06:03:20 +0000618 if (!VerifyFuncTypeComplete(FPT))
Anders Carlsson64457732009-11-24 05:08:52 +0000619 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
620
621 return llvm::OpaqueType::get(getLLVMContext());
622}
623
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000624void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000625 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000626 AttributeListType &PAL,
627 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000628 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000629 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000630
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000631 CallingConv = FI.getEffectiveCallingConvention();
632
John McCallab26cfa2010-02-05 21:31:56 +0000633 if (FI.isNoReturn())
634 FuncAttrs |= llvm::Attribute::NoReturn;
635
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000636 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000637 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000638 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000639 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000640 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000641 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000642 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000643 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000644 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000645 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000646 if (TargetDecl->hasAttr<MallocAttr>())
647 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000648 }
649
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000650 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000651 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000652 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000653 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000654 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000655 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000656
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000657 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000658 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000659 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000660 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000661 case ABIArgInfo::Extend:
662 if (RetTy->isSignedIntegerType()) {
663 RetAttrs |= llvm::Attribute::SExt;
664 } else if (RetTy->isUnsignedIntegerType()) {
665 RetAttrs |= llvm::Attribute::ZExt;
666 }
667 // FALLTHROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000668 case ABIArgInfo::Direct:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000669 break;
670
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000671 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000672 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000673 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000674 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000675 // sret disables readnone and readonly
676 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
677 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000678 break;
679
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000680 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000681 case ABIArgInfo::Coerce:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000682 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000683
Daniel Dunbard3674e62008-09-11 01:48:57 +0000684 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000685 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000686 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000687
Devang Patel597e7082008-09-26 22:53:57 +0000688 if (RetAttrs)
689 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000690
691 // FIXME: we need to honour command line settings also...
692 // FIXME: RegParm should be reduced in case of nested functions and/or global
693 // register variable.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000694 signed RegParm = FI.getRegParm();
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000695
696 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000697 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000698 ie = FI.arg_end(); it != ie; ++it) {
699 QualType ParamType = it->type;
700 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000701 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000702
John McCall39ec71f2010-03-27 00:47:27 +0000703 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
704 // have the corresponding parameter variable. It doesn't make
705 // sense to do it here because parameters are so fucked up.
Nuno Lopes72513272009-12-07 18:30:06 +0000706
Daniel Dunbard3674e62008-09-11 01:48:57 +0000707 switch (AI.getKind()) {
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000708 case ABIArgInfo::Coerce:
709 break;
710
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000711 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000712 if (AI.getIndirectByVal())
713 Attributes |= llvm::Attribute::ByVal;
714
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000715 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000716 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000717 // byval disables readnone and readonly.
718 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
719 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000720 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000721
722 case ABIArgInfo::Extend:
723 if (ParamType->isSignedIntegerType()) {
724 Attributes |= llvm::Attribute::SExt;
725 } else if (ParamType->isUnsignedIntegerType()) {
726 Attributes |= llvm::Attribute::ZExt;
727 }
728 // FALLS THROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000729 case ABIArgInfo::Direct:
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000730 if (RegParm > 0 &&
731 (ParamType->isIntegerType() || ParamType->isPointerType())) {
732 RegParm -=
733 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
734 if (RegParm >= 0)
735 Attributes |= llvm::Attribute::InReg;
736 }
737 // FIXME: handle sseregparm someday...
Daniel Dunbard3674e62008-09-11 01:48:57 +0000738 break;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000739
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000740 case ABIArgInfo::Ignore:
741 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000742 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000743
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000744 case ABIArgInfo::Expand: {
Mike Stump11289f42009-09-09 15:08:12 +0000745 std::vector<const llvm::Type*> Tys;
Mike Stump18bb9282009-05-16 07:57:57 +0000746 // FIXME: This is rather inefficient. Do we ever actually need to do
747 // anything here? The result should be just reconstructed on the other
748 // side, so extension should be a non-issue.
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000749 getTypes().GetExpandedTypes(ParamType, Tys);
750 Index += Tys.size();
751 continue;
752 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Devang Patel322300d2008-09-25 21:02:23 +0000755 if (Attributes)
756 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000757 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000758 }
Devang Patel597e7082008-09-26 22:53:57 +0000759 if (FuncAttrs)
760 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000761}
762
Daniel Dunbard931a872009-02-02 22:03:45 +0000763void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
764 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000765 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000766 // If this is an implicit-return-zero function, go ahead and
767 // initialize the return value. TODO: it might be nice to have
768 // a more general mechanism for this that didn't require synthesized
769 // return statements.
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000770 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000771 if (FD->hasImplicitReturnZero()) {
772 QualType RetTy = FD->getResultType().getUnqualifiedType();
773 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000774 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000775 Builder.CreateStore(Zero, ReturnValue);
776 }
777 }
778
Mike Stump18bb9282009-05-16 07:57:57 +0000779 // FIXME: We no longer need the types from FunctionArgList; lift up and
780 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000781
Daniel Dunbar613855c2008-09-09 23:27:19 +0000782 // Emit allocs for param decls. Give the LLVM Argument nodes names.
783 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000784
Daniel Dunbar613855c2008-09-09 23:27:19 +0000785 // Name the struct return argument.
Daniel Dunbard931a872009-02-02 22:03:45 +0000786 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000787 AI->setName("agg.result");
788 ++AI;
789 }
Mike Stump11289f42009-09-09 15:08:12 +0000790
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000791 assert(FI.arg_size() == Args.size() &&
792 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000793 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000794 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000795 i != e; ++i, ++info_it) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000796 const VarDecl *Arg = i->first;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000797 QualType Ty = info_it->type;
798 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000799
800 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000801 case ABIArgInfo::Indirect: {
802 llvm::Value* V = AI;
803 if (hasAggregateLLVMType(Ty)) {
804 // Do nothing, aggregates and complex variables are accessed by
805 // reference.
806 } else {
807 // Load scalar value from indirect argument.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000808 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000809 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
810 // This must be a promotion, for something like
811 // "void a(x) short x; {..."
812 V = EmitScalarConversion(V, Ty, Arg->getType());
813 }
814 }
Mike Stump11289f42009-09-09 15:08:12 +0000815 EmitParmDecl(*Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000816 break;
817 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000818
819 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000820 case ABIArgInfo::Direct: {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000821 assert(AI != Fn->arg_end() && "Argument mismatch!");
822 llvm::Value* V = AI;
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000823 if (hasAggregateLLVMType(Ty)) {
824 // Create a temporary alloca to hold the argument; the rest of
825 // codegen expects to access aggregates & complex values by
826 // reference.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000827 V = CreateMemTemp(Ty);
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000828 Builder.CreateStore(AI, V);
829 } else {
John McCall39ec71f2010-03-27 00:47:27 +0000830 if (Arg->getType().isRestrictQualified())
831 AI->addAttr(llvm::Attribute::NoAlias);
832
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000833 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
834 // This must be a promotion, for something like
835 // "void a(x) short x; {..."
836 V = EmitScalarConversion(V, Ty, Arg->getType());
837 }
Daniel Dunbar613855c2008-09-09 23:27:19 +0000838 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000839 EmitParmDecl(*Arg, V);
840 break;
841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000843 case ABIArgInfo::Expand: {
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000844 // If this structure was expanded into multiple arguments then
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000845 // we need to create a temporary and reconstruct it from the
846 // arguments.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000847 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000848 // FIXME: What are the right qualifiers here?
Mike Stump11289f42009-09-09 15:08:12 +0000849 llvm::Function::arg_iterator End =
John McCall8ccfcb52009-09-24 19:53:00 +0000850 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000851 EmitParmDecl(*Arg, Temp);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000852
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000853 // Name the arguments used in expansion and increment AI.
854 unsigned Index = 0;
855 for (; AI != End; ++AI, ++Index)
Daniel Dunbarb5aacc22009-10-19 01:21:05 +0000856 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000857 continue;
858 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000859
860 case ABIArgInfo::Ignore:
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000861 // Initialize the local variable appropriately.
Mike Stump11289f42009-09-09 15:08:12 +0000862 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbara7566f12010-02-09 02:48:28 +0000863 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000864 } else {
865 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
866 }
Mike Stump11289f42009-09-09 15:08:12 +0000867
Daniel Dunbarfc7c7612009-02-03 20:00:13 +0000868 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000869 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000870
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000871 case ABIArgInfo::Coerce: {
872 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stump18bb9282009-05-16 07:57:57 +0000873 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
874 // result in a new alloca anyway, so we could just store into that
875 // directly if we broke the abstraction down more.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000876 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +0000877 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000878 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000879 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000880 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000881 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
882 // This must be a promotion, for something like
883 // "void a(x) short x; {..."
884 V = EmitScalarConversion(V, Ty, Arg->getType());
885 }
886 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000887 EmitParmDecl(*Arg, V);
888 break;
889 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000890 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000891
892 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000893 }
894 assert(AI == Fn->arg_end() && "Argument mismatch!");
895}
896
Chris Lattner3fcc7902010-06-27 01:06:27 +0000897void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000898 // Functions with no result always return void.
Chris Lattner726b3d02010-06-26 23:13:19 +0000899 if (ReturnValue == 0) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000900 Builder.CreateRetVoid();
Chris Lattner726b3d02010-06-26 23:13:19 +0000901 return;
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000902 }
Chris Lattner726b3d02010-06-26 23:13:19 +0000903
904 llvm::Value *RV = 0;
905 QualType RetTy = FI.getReturnType();
906 const ABIArgInfo &RetAI = FI.getReturnInfo();
907
908 switch (RetAI.getKind()) {
909 case ABIArgInfo::Indirect:
910 if (RetTy->isAnyComplexType()) {
911 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
912 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
913 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
914 // Do nothing; aggregrates get evaluated directly into the destination.
915 } else {
916 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
917 false, RetTy);
918 }
919 break;
920
921 case ABIArgInfo::Extend:
Chris Lattner3fcc7902010-06-27 01:06:27 +0000922 case ABIArgInfo::Direct: {
923 // The internal return value temp always will have pointer-to-return-type
924 // type, just do a load.
925
926 // If the instruction right before the insertion point is a store to the
927 // return value, we can elide the load, zap the store, and usually zap the
928 // alloca.
929 llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
930 llvm::StoreInst *SI = 0;
931 if (InsertBB->empty() ||
932 !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
933 SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
934 RV = Builder.CreateLoad(ReturnValue);
935 } else {
936 // Get the stored value and nuke the now-dead store.
937 RV = SI->getValueOperand();
938 SI->eraseFromParent();
939
940 // If that was the only use of the return value, nuke it as well now.
941 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
942 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
943 ReturnValue = 0;
944 }
945 }
Chris Lattner726b3d02010-06-26 23:13:19 +0000946 break;
Chris Lattner3fcc7902010-06-27 01:06:27 +0000947 }
Chris Lattner726b3d02010-06-26 23:13:19 +0000948 case ABIArgInfo::Ignore:
949 break;
950
951 case ABIArgInfo::Coerce:
952 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
953 break;
954
955 case ABIArgInfo::Expand:
956 assert(0 && "Invalid ABI kind for return argument");
957 }
958
959 if (RV)
960 Builder.CreateRet(RV);
961 else
962 Builder.CreateRetVoid();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000963}
964
John McCall23f66262010-05-26 22:34:26 +0000965RValue CodeGenFunction::EmitDelegateCallArg(const VarDecl *Param) {
966 // StartFunction converted the ABI-lowered parameter(s) into a
967 // local alloca. We need to turn that into an r-value suitable
968 // for EmitCall.
969 llvm::Value *Local = GetAddrOfLocalVar(Param);
970
971 QualType ArgType = Param->getType();
972
973 // For the most part, we just need to load the alloca, except:
974 // 1) aggregate r-values are actually pointers to temporaries, and
975 // 2) references to aggregates are pointers directly to the aggregate.
976 // I don't know why references to non-aggregates are different here.
977 if (const ReferenceType *RefType = ArgType->getAs<ReferenceType>()) {
978 if (hasAggregateLLVMType(RefType->getPointeeType()))
979 return RValue::getAggregate(Local);
980
981 // Locals which are references to scalars are represented
982 // with allocas holding the pointer.
983 return RValue::get(Builder.CreateLoad(Local));
984 }
985
986 if (ArgType->isAnyComplexType())
987 return RValue::getComplex(LoadComplexFromAddr(Local, /*volatile*/ false));
988
989 if (hasAggregateLLVMType(ArgType))
990 return RValue::getAggregate(Local);
991
992 return RValue::get(EmitLoadOfScalar(Local, false, ArgType));
993}
994
Anders Carlsson60ce3fe2009-04-08 20:47:54 +0000995RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000996 if (ArgType->isReferenceType())
Anders Carlsson04775f82010-06-26 16:35:32 +0000997 return EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Mike Stump11289f42009-09-09 15:08:12 +0000998
Anders Carlsson60ce3fe2009-04-08 20:47:54 +0000999 return EmitAnyExprToTemp(E);
1000}
1001
Daniel Dunbard931a872009-02-02 22:03:45 +00001002RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +00001003 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +00001004 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001005 const CallArgList &CallArgs,
David Chisnall9eecafa2010-05-01 11:15:56 +00001006 const Decl *TargetDecl,
David Chisnallff5f88c2010-05-02 13:41:58 +00001007 llvm::Instruction **callOrInvoke) {
Mike Stump18bb9282009-05-16 07:57:57 +00001008 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +00001009 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001010
1011 // Handle struct-return functions by passing a pointer to the
1012 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +00001013 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001014 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +00001015
1016
Chris Lattner4ca97c32009-06-13 00:26:38 +00001017 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +00001018 // alloca to hold the result, unless one is given to us.
1019 if (CGM.ReturnTypeUsesSret(CallInfo)) {
1020 llvm::Value *Value = ReturnValue.getValue();
1021 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +00001022 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +00001023 Args.push_back(Value);
1024 }
Mike Stump11289f42009-09-09 15:08:12 +00001025
Daniel Dunbara45bdbb2009-02-04 21:17:21 +00001026 assert(CallInfo.arg_size() == CallArgs.size() &&
1027 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001028 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001029 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +00001030 I != E; ++I, ++info_it) {
1031 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001032 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001033
1034 switch (ArgInfo.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001035 case ABIArgInfo::Indirect:
Daniel Dunbar747865a2009-02-05 09:16:39 +00001036 if (RV.isScalar() || RV.isComplex()) {
1037 // Make a temporary alloca to pass the argument.
Daniel Dunbara7566f12010-02-09 02:48:28 +00001038 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar747865a2009-02-05 09:16:39 +00001039 if (RV.isScalar())
Anders Carlsson83709642009-05-19 18:50:41 +00001040 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001041 else
Mike Stump11289f42009-09-09 15:08:12 +00001042 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +00001043 } else {
1044 Args.push_back(RV.getAggregateAddr());
1045 }
1046 break;
1047
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001048 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001049 case ABIArgInfo::Direct:
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001050 if (RV.isScalar()) {
1051 Args.push_back(RV.getScalarVal());
1052 } else if (RV.isComplex()) {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001053 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1054 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1055 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1056 Args.push_back(Tmp);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001057 } else {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001058 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001059 }
1060 break;
Mike Stump11289f42009-09-09 15:08:12 +00001061
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001062 case ABIArgInfo::Ignore:
1063 break;
1064
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001065 case ABIArgInfo::Coerce: {
1066 // FIXME: Avoid the conversion through memory if possible.
1067 llvm::Value *SrcPtr;
1068 if (RV.isScalar()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001069 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlsson83709642009-05-19 18:50:41 +00001070 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001071 } else if (RV.isComplex()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001072 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001073 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +00001074 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001075 SrcPtr = RV.getAggregateAddr();
Mike Stump11289f42009-09-09 15:08:12 +00001076 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar2f219b02009-02-03 19:12:28 +00001077 *this));
1078 break;
1079 }
1080
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001081 case ABIArgInfo::Expand:
1082 ExpandTypeToArgs(I->second, RV, Args);
1083 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +00001084 }
1085 }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattner4ca97c32009-06-13 00:26:38 +00001087 // If the callee is a bitcast of a function to a varargs pointer to function
1088 // type, check to see if we can remove the bitcast. This handles some cases
1089 // with unprototyped functions.
1090 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1091 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1092 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1093 const llvm::FunctionType *CurFT =
1094 cast<llvm::FunctionType>(CurPT->getElementType());
1095 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +00001096
Chris Lattner4ca97c32009-06-13 00:26:38 +00001097 if (CE->getOpcode() == llvm::Instruction::BitCast &&
1098 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +00001099 ActualFT->getNumParams() == CurFT->getNumParams() &&
1100 ActualFT->getNumParams() == Args.size()) {
Chris Lattner4ca97c32009-06-13 00:26:38 +00001101 bool ArgsMatch = true;
1102 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1103 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1104 ArgsMatch = false;
1105 break;
1106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Chris Lattner4ca97c32009-06-13 00:26:38 +00001108 // Strip the cast if we can get away with it. This is a nice cleanup,
1109 // but also allows us to inline the function at -O0 if it is marked
1110 // always_inline.
1111 if (ArgsMatch)
1112 Callee = CalleeF;
1113 }
1114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Daniel Dunbar613855c2008-09-09 23:27:19 +00001116
Daniel Dunbar12347492009-02-23 17:26:39 +00001117 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001118 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +00001119 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001120 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +00001121 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1122 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +00001123
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001124 llvm::CallSite CS;
1125 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foad7d0479f2009-05-21 09:52:38 +00001126 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001127 } else {
1128 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +00001129 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +00001130 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +00001131 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001132 }
David Chisnallff5f88c2010-05-02 13:41:58 +00001133 if (callOrInvoke) {
1134 *callOrInvoke = CS.getInstruction();
David Chisnall9eecafa2010-05-01 11:15:56 +00001135 }
Daniel Dunbar5006f4a2009-02-20 18:54:31 +00001136
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001137 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001138 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001139
1140 // If the call doesn't return, finish the basic block and clear the
1141 // insertion point; this allows the rest of IRgen to discard
1142 // unreachable code.
1143 if (CS.doesNotReturn()) {
1144 Builder.CreateUnreachable();
1145 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001146
Mike Stump18bb9282009-05-16 07:57:57 +00001147 // FIXME: For now, emit a dummy basic block because expr emitters in
1148 // generally are not ready to handle emitting expressions at unreachable
1149 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001150 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001151
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001152 // Return a reasonable RValue.
1153 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001154 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001155
1156 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001157 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001158 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001159
1160 switch (RetAI.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001161 case ABIArgInfo::Indirect:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001162 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001163 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001164 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001165 return RValue::getAggregate(Args[0]);
Chris Lattnere09ad902009-03-22 00:32:22 +00001166 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +00001167
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001168 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001169 case ABIArgInfo::Direct:
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001170 if (RetTy->isAnyComplexType()) {
1171 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1172 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1173 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattnere09ad902009-03-22 00:32:22 +00001174 }
1175 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001176 llvm::Value *DestPtr = ReturnValue.getValue();
1177 bool DestIsVolatile = ReturnValue.isVolatile();
1178
1179 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001180 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlsson17490832009-12-24 20:40:36 +00001181 DestIsVolatile = false;
1182 }
1183 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1184 return RValue::getAggregate(DestPtr);
Chris Lattnere09ad902009-03-22 00:32:22 +00001185 }
1186 return RValue::get(CI);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001187
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001188 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001189 // If we are ignoring an argument that had a result, make sure to
1190 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001191 return GetUndefRValue(RetTy);
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001192
Daniel Dunbar573884e2008-09-10 07:04:09 +00001193 case ABIArgInfo::Coerce: {
Anders Carlsson17490832009-12-24 20:40:36 +00001194 llvm::Value *DestPtr = ReturnValue.getValue();
1195 bool DestIsVolatile = ReturnValue.isVolatile();
1196
1197 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001198 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001199 DestIsVolatile = false;
1200 }
1201
1202 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001203 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001204 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001205 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001206 return RValue::getAggregate(DestPtr);
1207 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001208 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001209
Daniel Dunbard3674e62008-09-11 01:48:57 +00001210 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001211 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001212 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001213
1214 assert(0 && "Unhandled ABIArgInfo::Kind");
1215 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001216}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001217
1218/* VarArg handling */
1219
1220llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1221 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1222}