blob: 8b5c3a0f6c46de0d6d20d8b62b8aceabff6b8c2d [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 Carruthbc55fe22009-11-12 17:24:48 +000022#include "clang/CodeGen/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;
41 }
42}
43
John McCall8ee376f2010-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 McCall2da83a32010-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 Dunbar7a95ca32008-09-10 04:01:49 +000050}
51
John McCall8ee376f2010-02-24 07:14:12 +000052/// Returns the canonical formal type of the given C++ method.
John McCall2da83a32010-02-26 00:48:12 +000053static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
54 return MD->getType()->getCanonicalTypeUnqualified()
55 .getAs<FunctionProtoType>();
John McCall8ee376f2010-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 McCall2da83a32010-02-26 00:48:12 +000062static CanQualType GetReturnType(QualType RetTy) {
63 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
John McCall8ee376f2010-02-24 07:14:12 +000064}
65
66const CGFunctionInfo &
John McCall2da83a32010-02-26 00:48:12 +000067CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
68 return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
69 llvm::SmallVector<CanQualType, 16>(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +000070 FTNP->getExtInfo());
John McCall8ee376f2010-02-24 07:14:12 +000071}
72
73/// \param Args - contains any initial parameters besides those
74/// in the formal type
75static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
John McCall2da83a32010-02-26 00:48:12 +000076 llvm::SmallVectorImpl<CanQualType> &ArgTys,
77 CanQual<FunctionProtoType> FTP) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000078 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000079 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000080 ArgTys.push_back(FTP->getArgType(i));
John McCall2da83a32010-02-26 00:48:12 +000081 CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
82 return CGT.getFunctionInfo(ResTy, ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +000083 FTP->getExtInfo());
John McCall8ee376f2010-02-24 07:14:12 +000084}
85
86const CGFunctionInfo &
John McCall2da83a32010-02-26 00:48:12 +000087CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
88 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +000089 return ::getFunctionInfo(*this, ArgTys, FTP);
Daniel Dunbar7feafc72009-09-11 22:24:53 +000090}
91
John McCallab26cfa2010-02-05 21:31:56 +000092static CallingConv getCallingConventionForDecl(const Decl *D) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000093 // Set the appropriate calling convention for the Function.
94 if (D->hasAttr<StdCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000095 return CC_X86StdCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000096
97 if (D->hasAttr<FastCallAttr>())
John McCallab26cfa2010-02-05 21:31:56 +000098 return CC_X86FastCall;
Daniel Dunbar7feafc72009-09-11 22:24:53 +000099
John McCallab26cfa2010-02-05 21:31:56 +0000100 return CC_C;
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000101}
102
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000103const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
104 const FunctionProtoType *FTP) {
John McCall2da83a32010-02-26 00:48:12 +0000105 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000106
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000107 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000108 ArgTys.push_back(GetThisType(Context, RD));
109
110 return ::getFunctionInfo(*this, ArgTys,
John McCall2da83a32010-02-26 00:48:12 +0000111 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
Anders Carlsson2ee3c012009-10-03 19:43:08 +0000112}
113
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000114const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000115 llvm::SmallVector<CanQualType, 16> ArgTys;
John McCall8ee376f2010-02-24 07:14:12 +0000116
Chris Lattnerbea5b622009-05-12 20:27:19 +0000117 // Add the 'this' pointer unless this is a static method.
118 if (MD->isInstance())
John McCall8ee376f2010-02-24 07:14:12 +0000119 ArgTys.push_back(GetThisType(Context, MD->getParent()));
Mike Stump11289f42009-09-09 15:08:12 +0000120
John McCall8ee376f2010-02-24 07:14:12 +0000121 return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000122}
123
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000124const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
125 CXXCtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000126 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000127
128 // Add the 'this' pointer.
John McCall8ee376f2010-02-24 07:14:12 +0000129 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000130
131 // Check if we need to add a VTT parameter (which has type void **).
132 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
133 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall8ee376f2010-02-24 07:14:12 +0000134
135 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000136}
137
138const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
139 CXXDtorType Type) {
John McCall2da83a32010-02-26 00:48:12 +0000140 llvm::SmallVector<CanQualType, 16> ArgTys;
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000141
142 // Add the 'this' pointer.
John McCall2da83a32010-02-26 00:48:12 +0000143 ArgTys.push_back(GetThisType(Context, D->getParent()));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000144
145 // Check if we need to add a VTT parameter (which has type void **).
146 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
147 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
John McCall8ee376f2010-02-24 07:14:12 +0000148
149 return ::getFunctionInfo(*this, ArgTys, GetFormalType(D));
Anders Carlsson82ba57c2009-11-25 03:15:49 +0000150}
151
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000152const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +0000153 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +0000154 if (MD->isInstance())
155 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +0000156
John McCall2da83a32010-02-26 00:48:12 +0000157 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
158 assert(isa<FunctionType>(FTy));
John McCall8ee376f2010-02-24 07:14:12 +0000159 if (isa<FunctionNoProtoType>(FTy))
John McCall2da83a32010-02-26 00:48:12 +0000160 return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
161 assert(isa<FunctionProtoType>(FTy));
162 return getFunctionInfo(FTy.getAs<FunctionProtoType>());
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000163}
164
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000165const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
John McCall2da83a32010-02-26 00:48:12 +0000166 llvm::SmallVector<CanQualType, 16> ArgTys;
167 ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
168 ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000169 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000170 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
John McCall8ee376f2010-02-24 07:14:12 +0000171 e = MD->param_end(); i != e; ++i) {
172 ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
173 }
174 return getFunctionInfo(GetReturnType(MD->getResultType()),
175 ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000176 FunctionType::ExtInfo(
177 /*NoReturn*/ false,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000178 /*RegParm*/ 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000179 getCallingConventionForDecl(MD)));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000180}
181
Anders Carlsson6710c532010-02-06 02:44:09 +0000182const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
183 // FIXME: Do we need to handle ObjCMethodDecl?
184 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
185
186 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
187 return getFunctionInfo(CD, GD.getCtorType());
188
189 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
190 return getFunctionInfo(DD, GD.getDtorType());
191
192 return getFunctionInfo(FD);
193}
194
Mike Stump11289f42009-09-09 15:08:12 +0000195const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000196 const CallArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000197 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000198 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000199 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000200 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000201 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000202 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000203 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000204}
205
Mike Stump11289f42009-09-09 15:08:12 +0000206const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000207 const FunctionArgList &Args,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000208 const FunctionType::ExtInfo &Info) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000209 // FIXME: Kill copy.
John McCall2da83a32010-02-26 00:48:12 +0000210 llvm::SmallVector<CanQualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000211 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000212 i != e; ++i)
John McCall8ee376f2010-02-24 07:14:12 +0000213 ArgTys.push_back(Context.getCanonicalParamType(i->second));
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000214 return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000215}
216
John McCall2da83a32010-02-26 00:48:12 +0000217const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
218 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000219 const FunctionType::ExtInfo &Info) {
John McCall2da83a32010-02-26 00:48:12 +0000220#ifndef NDEBUG
221 for (llvm::SmallVectorImpl<CanQualType>::const_iterator
222 I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
223 assert(I->isCanonicalAsParam());
224#endif
225
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000226 unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
John McCallab26cfa2010-02-05 21:31:56 +0000227
Daniel Dunbare0be8292009-02-03 00:07:12 +0000228 // Lookup or create unique function info.
229 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000230 CGFunctionInfo::Profile(ID, Info, ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000231 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000232
233 void *InsertPos = 0;
234 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
235 if (FI)
236 return *FI;
237
Daniel Dunbar313321e2009-02-03 05:31:23 +0000238 // Construct the function info.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000239 FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getRegParm(), ResTy, ArgTys);
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000240 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000241
242 // Compute ABI information.
Owen Anderson170229f2009-07-14 23:10:40 +0000243 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar313321e2009-02-03 05:31:23 +0000244
Daniel Dunbare0be8292009-02-03 00:07:12 +0000245 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000246}
247
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000248CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
John McCallab26cfa2010-02-05 21:31:56 +0000249 bool _NoReturn,
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000250 unsigned _RegParm,
John McCall2da83a32010-02-26 00:48:12 +0000251 CanQualType ResTy,
252 const llvm::SmallVectorImpl<CanQualType> &ArgTys)
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000253 : CallingConvention(_CallingConvention),
John McCallab26cfa2010-02-05 21:31:56 +0000254 EffectiveCallingConvention(_CallingConvention),
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000255 NoReturn(_NoReturn), RegParm(_RegParm)
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000256{
Daniel Dunbar313321e2009-02-03 05:31:23 +0000257 NumArgs = ArgTys.size();
258 Args = new ArgInfo[1 + NumArgs];
259 Args[0].type = ResTy;
260 for (unsigned i = 0; i < NumArgs; ++i)
261 Args[1 + i].type = ArgTys[i];
262}
263
264/***/
265
Mike Stump11289f42009-09-09 15:08:12 +0000266void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000267 std::vector<const llvm::Type*> &ArgTys) {
268 const RecordType *RT = Ty->getAsStructureType();
269 assert(RT && "Can only expand structure types.");
270 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000271 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000272 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000273
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000274 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
275 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000276 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000277 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000278 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000279
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000280 QualType FT = FD->getType();
281 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
282 GetExpandedTypes(FT, ArgTys);
283 } else {
284 ArgTys.push_back(ConvertType(FT));
285 }
286 }
287}
288
Mike Stump11289f42009-09-09 15:08:12 +0000289llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000290CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
291 llvm::Function::arg_iterator AI) {
292 const RecordType *RT = Ty->getAsStructureType();
293 assert(RT && "Can only expand structure types.");
294
295 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000296 assert(LV.isSimple() &&
297 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000298 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000299 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
300 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000301 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000302 QualType FT = FD->getType();
303
304 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000305 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000306 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
307 AI = ExpandTypeFromArgs(FT, LV, AI);
308 } else {
309 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
310 ++AI;
311 }
312 }
313
314 return AI;
315}
316
Mike Stump11289f42009-09-09 15:08:12 +0000317void
318CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000319 llvm::SmallVector<llvm::Value*, 16> &Args) {
320 const RecordType *RT = Ty->getAsStructureType();
321 assert(RT && "Can only expand structure types.");
322
323 RecordDecl *RD = RT->getDecl();
324 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
325 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000326 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
327 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000328 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000329 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000330
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000331 // FIXME: What are the right qualifiers here?
Anders Carlsson5d8645b2010-01-29 05:05:36 +0000332 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000333 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
334 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
335 } else {
336 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000337 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000338 "Unexpected non-scalar rvalue during struct expansion.");
339 Args.push_back(RV.getScalarVal());
340 }
341 }
342}
343
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000344/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
345/// a pointer to an object of type \arg Ty.
346///
347/// This safely handles the case when the src type is smaller than the
348/// destination type; in this situation the values of bits which not
349/// present in the src are undefined.
350static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
351 const llvm::Type *Ty,
352 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000353 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000354 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000355 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
356 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000357
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000358 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000359 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000360 // Generally SrcSize is never greater than DstSize, since this means we are
361 // losing bits. However, this can happen in cases where the structure has
362 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000363 //
Mike Stump18bb9282009-05-16 07:57:57 +0000364 // FIXME: Assert that we aren't truncating non-padding bits when have access
365 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000366 llvm::Value *Casted =
367 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000368 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
369 // FIXME: Use better alignment / avoid requiring aligned load.
370 Load->setAlignment(1);
371 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000372 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000373 // Otherwise do coercion through memory. This is stupid, but
374 // simple.
375 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000376 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000377 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump11289f42009-09-09 15:08:12 +0000378 llvm::StoreInst *Store =
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000379 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
380 // FIXME: Use better alignment / avoid requiring aligned store.
381 Store->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000382 return CGF.Builder.CreateLoad(Tmp);
383 }
384}
385
386/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
387/// where the source and destination may have different types.
388///
389/// This safely handles the case when the src type is larger than the
390/// destination type; the upper bits of the src will be lost.
391static void CreateCoercedStore(llvm::Value *Src,
392 llvm::Value *DstPtr,
Anders Carlsson17490832009-12-24 20:40:36 +0000393 bool DstIsVolatile,
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000394 CodeGenFunction &CGF) {
395 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000396 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000397 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
398
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000399 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
400 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000401
Daniel Dunbar313321e2009-02-03 05:31:23 +0000402 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000403 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000404 llvm::Value *Casted =
405 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000406 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlsson17490832009-12-24 20:40:36 +0000407 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000408 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000409 // Otherwise do coercion through memory. This is stupid, but
410 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000411
412 // Generally SrcSize is never greater than DstSize, since this means we are
413 // losing bits. However, this can happen in cases where the structure has
414 // additional padding, for example due to a user specified alignment.
415 //
416 // FIXME: Assert that we aren't truncating non-padding bits when have access
417 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000418 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
419 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000420 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000421 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000422 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
423 // FIXME: Use better alignment / avoid requiring aligned load.
424 Load->setAlignment(1);
Anders Carlsson17490832009-12-24 20:40:36 +0000425 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000426 }
427}
428
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000429/***/
430
Daniel Dunbard931a872009-02-02 22:03:45 +0000431bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000432 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000433}
434
John McCallf8ff7b92010-02-23 00:48:20 +0000435const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
436 const CGFunctionInfo &FI = getFunctionInfo(GD);
437
438 // For definition purposes, don't consider a K&R function variadic.
439 bool Variadic = false;
440 if (const FunctionProtoType *FPT =
441 cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
442 Variadic = FPT->isVariadic();
443
444 return GetFunctionType(FI, Variadic);
445}
446
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000447const llvm::FunctionType *
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000448CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000449 std::vector<const llvm::Type*> ArgTys;
450
451 const llvm::Type *ResultType = 0;
452
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000453 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000454 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbard3674e62008-09-11 01:48:57 +0000455 switch (RetAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000456 case ABIArgInfo::Expand:
457 assert(0 && "Invalid ABI kind for return argument");
458
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000459 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000460 case ABIArgInfo::Direct:
461 ResultType = ConvertType(RetTy);
462 break;
463
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000464 case ABIArgInfo::Indirect: {
465 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson41a75022009-08-13 21:57:51 +0000466 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000467 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000468 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
469 break;
470 }
471
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000472 case ABIArgInfo::Ignore:
Owen Anderson41a75022009-08-13 21:57:51 +0000473 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000474 break;
475
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000476 case ABIArgInfo::Coerce:
Daniel Dunbar573884e2008-09-10 07:04:09 +0000477 ResultType = RetAI.getCoerceToType();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000478 break;
479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
481 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000482 ie = FI.arg_end(); it != ie; ++it) {
483 const ABIArgInfo &AI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Daniel Dunbard3674e62008-09-11 01:48:57 +0000485 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000486 case ABIArgInfo::Ignore:
487 break;
488
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000489 case ABIArgInfo::Coerce:
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000490 ArgTys.push_back(AI.getCoerceToType());
491 break;
492
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000493 case ABIArgInfo::Indirect: {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000494 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000495 const llvm::Type *LTy = ConvertTypeForMem(it->type);
496 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000497 break;
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000498 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000499
500 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000501 case ABIArgInfo::Direct:
Daniel Dunbar747865a2009-02-05 09:16:39 +0000502 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000503 break;
Mike Stump11289f42009-09-09 15:08:12 +0000504
Daniel Dunbard3674e62008-09-11 01:48:57 +0000505 case ABIArgInfo::Expand:
Daniel Dunbar313321e2009-02-03 05:31:23 +0000506 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000507 break;
508 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000509 }
510
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000511 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000512}
513
Anders Carlsson64457732009-11-24 05:08:52 +0000514static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
515 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
516 if (!TT->getDecl()->isDefinition())
517 return true;
518 }
519
520 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
521 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
522 if (!TT->getDecl()->isDefinition())
523 return true;
524 }
525 }
526
527 return false;
528}
529
530const llvm::Type *
Anders Carlsson11e51402010-04-17 20:15:18 +0000531CodeGenTypes::GetFunctionTypeForVTable(const CXXMethodDecl *MD) {
Anders Carlsson64457732009-11-24 05:08:52 +0000532 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
533
534 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
535 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
536
537 return llvm::OpaqueType::get(getLLVMContext());
538}
539
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000540void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000541 const Decl *TargetDecl,
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000542 AttributeListType &PAL,
543 unsigned &CallingConv) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000544 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000545 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000546
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000547 CallingConv = FI.getEffectiveCallingConvention();
548
John McCallab26cfa2010-02-05 21:31:56 +0000549 if (FI.isNoReturn())
550 FuncAttrs |= llvm::Attribute::NoReturn;
551
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000552 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000553 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000554 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000555 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000556 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000557 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000558 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000559 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000560 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000561 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000562 if (TargetDecl->hasAttr<MallocAttr>())
563 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000564 }
565
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000566 if (CodeGenOpts.OptimizeSize)
Daniel Dunbarc369d732009-10-27 19:48:08 +0000567 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000568 if (CodeGenOpts.DisableRedZone)
Devang Patel6e467b12009-06-04 23:32:02 +0000569 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruthbc55fe22009-11-12 17:24:48 +0000570 if (CodeGenOpts.NoImplicitFloat)
Devang Patel9e243862009-06-05 22:05:48 +0000571 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000572
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000573 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000574 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000575 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000576 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000577 case ABIArgInfo::Extend:
578 if (RetTy->isSignedIntegerType()) {
579 RetAttrs |= llvm::Attribute::SExt;
580 } else if (RetTy->isUnsignedIntegerType()) {
581 RetAttrs |= llvm::Attribute::ZExt;
582 }
583 // FALLTHROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000584 case ABIArgInfo::Direct:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000585 break;
586
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000587 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000588 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Chris Lattner9cffdf12010-04-20 05:44:43 +0000589 llvm::Attribute::StructRet));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000590 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000591 // sret disables readnone and readonly
592 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
593 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000594 break;
595
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000596 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000597 case ABIArgInfo::Coerce:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000598 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000599
Daniel Dunbard3674e62008-09-11 01:48:57 +0000600 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000601 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000602 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000603
Devang Patel597e7082008-09-26 22:53:57 +0000604 if (RetAttrs)
605 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000606
607 // FIXME: we need to honour command line settings also...
608 // FIXME: RegParm should be reduced in case of nested functions and/or global
609 // register variable.
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000610 signed RegParm = FI.getRegParm();
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000611
612 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000613 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000614 ie = FI.arg_end(); it != ie; ++it) {
615 QualType ParamType = it->type;
616 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000617 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000618
John McCall39ec71f2010-03-27 00:47:27 +0000619 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
620 // have the corresponding parameter variable. It doesn't make
621 // sense to do it here because parameters are so fucked up.
Nuno Lopes72513272009-12-07 18:30:06 +0000622
Daniel Dunbard3674e62008-09-11 01:48:57 +0000623 switch (AI.getKind()) {
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000624 case ABIArgInfo::Coerce:
625 break;
626
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000627 case ABIArgInfo::Indirect:
Anders Carlsson20759ad2009-09-16 15:53:40 +0000628 if (AI.getIndirectByVal())
629 Attributes |= llvm::Attribute::ByVal;
630
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000631 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000632 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000633 // byval disables readnone and readonly.
634 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
635 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000636 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000637
638 case ABIArgInfo::Extend:
639 if (ParamType->isSignedIntegerType()) {
640 Attributes |= llvm::Attribute::SExt;
641 } else if (ParamType->isUnsignedIntegerType()) {
642 Attributes |= llvm::Attribute::ZExt;
643 }
644 // FALLS THROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000645 case ABIArgInfo::Direct:
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000646 if (RegParm > 0 &&
647 (ParamType->isIntegerType() || ParamType->isPointerType())) {
648 RegParm -=
649 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
650 if (RegParm >= 0)
651 Attributes |= llvm::Attribute::InReg;
652 }
653 // FIXME: handle sseregparm someday...
Daniel Dunbard3674e62008-09-11 01:48:57 +0000654 break;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000655
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000656 case ABIArgInfo::Ignore:
657 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000658 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000659
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000660 case ABIArgInfo::Expand: {
Mike Stump11289f42009-09-09 15:08:12 +0000661 std::vector<const llvm::Type*> Tys;
Mike Stump18bb9282009-05-16 07:57:57 +0000662 // FIXME: This is rather inefficient. Do we ever actually need to do
663 // anything here? The result should be just reconstructed on the other
664 // side, so extension should be a non-issue.
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000665 getTypes().GetExpandedTypes(ParamType, Tys);
666 Index += Tys.size();
667 continue;
668 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000669 }
Mike Stump11289f42009-09-09 15:08:12 +0000670
Devang Patel322300d2008-09-25 21:02:23 +0000671 if (Attributes)
672 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000673 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000674 }
Devang Patel597e7082008-09-26 22:53:57 +0000675 if (FuncAttrs)
676 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000677}
678
Daniel Dunbard931a872009-02-02 22:03:45 +0000679void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
680 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000681 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000682 // If this is an implicit-return-zero function, go ahead and
683 // initialize the return value. TODO: it might be nice to have
684 // a more general mechanism for this that didn't require synthesized
685 // return statements.
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000686 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000687 if (FD->hasImplicitReturnZero()) {
688 QualType RetTy = FD->getResultType().getUnqualifiedType();
689 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000690 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000691 Builder.CreateStore(Zero, ReturnValue);
692 }
693 }
694
Mike Stump18bb9282009-05-16 07:57:57 +0000695 // FIXME: We no longer need the types from FunctionArgList; lift up and
696 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000697
Daniel Dunbar613855c2008-09-09 23:27:19 +0000698 // Emit allocs for param decls. Give the LLVM Argument nodes names.
699 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000700
Daniel Dunbar613855c2008-09-09 23:27:19 +0000701 // Name the struct return argument.
Daniel Dunbard931a872009-02-02 22:03:45 +0000702 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000703 AI->setName("agg.result");
704 ++AI;
705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000707 assert(FI.arg_size() == Args.size() &&
708 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000709 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000710 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000711 i != e; ++i, ++info_it) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000712 const VarDecl *Arg = i->first;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000713 QualType Ty = info_it->type;
714 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000715
716 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000717 case ABIArgInfo::Indirect: {
718 llvm::Value* V = AI;
719 if (hasAggregateLLVMType(Ty)) {
720 // Do nothing, aggregates and complex variables are accessed by
721 // reference.
722 } else {
723 // Load scalar value from indirect argument.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000724 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000725 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
726 // This must be a promotion, for something like
727 // "void a(x) short x; {..."
728 V = EmitScalarConversion(V, Ty, Arg->getType());
729 }
730 }
Mike Stump11289f42009-09-09 15:08:12 +0000731 EmitParmDecl(*Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000732 break;
733 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000734
735 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000736 case ABIArgInfo::Direct: {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000737 assert(AI != Fn->arg_end() && "Argument mismatch!");
738 llvm::Value* V = AI;
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000739 if (hasAggregateLLVMType(Ty)) {
740 // Create a temporary alloca to hold the argument; the rest of
741 // codegen expects to access aggregates & complex values by
742 // reference.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000743 V = CreateMemTemp(Ty);
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000744 Builder.CreateStore(AI, V);
745 } else {
John McCall39ec71f2010-03-27 00:47:27 +0000746 if (Arg->getType().isRestrictQualified())
747 AI->addAttr(llvm::Attribute::NoAlias);
748
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000749 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
750 // This must be a promotion, for something like
751 // "void a(x) short x; {..."
752 V = EmitScalarConversion(V, Ty, Arg->getType());
753 }
Daniel Dunbar613855c2008-09-09 23:27:19 +0000754 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000755 EmitParmDecl(*Arg, V);
756 break;
757 }
Mike Stump11289f42009-09-09 15:08:12 +0000758
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000759 case ABIArgInfo::Expand: {
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000760 // If this structure was expanded into multiple arguments then
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000761 // we need to create a temporary and reconstruct it from the
762 // arguments.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000763 llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000764 // FIXME: What are the right qualifiers here?
Mike Stump11289f42009-09-09 15:08:12 +0000765 llvm::Function::arg_iterator End =
John McCall8ccfcb52009-09-24 19:53:00 +0000766 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000767 EmitParmDecl(*Arg, Temp);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000768
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000769 // Name the arguments used in expansion and increment AI.
770 unsigned Index = 0;
771 for (; AI != End; ++AI, ++Index)
Daniel Dunbarb5aacc22009-10-19 01:21:05 +0000772 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000773 continue;
774 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000775
776 case ABIArgInfo::Ignore:
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000777 // Initialize the local variable appropriately.
Mike Stump11289f42009-09-09 15:08:12 +0000778 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbara7566f12010-02-09 02:48:28 +0000779 EmitParmDecl(*Arg, CreateMemTemp(Ty));
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000780 } else {
781 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Daniel Dunbarfc7c7612009-02-03 20:00:13 +0000784 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000785 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000786
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000787 case ABIArgInfo::Coerce: {
788 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stump18bb9282009-05-16 07:57:57 +0000789 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
790 // result in a new alloca anyway, so we could just store into that
791 // directly if we broke the abstraction down more.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000792 llvm::Value *V = CreateMemTemp(Ty, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +0000793 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000794 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000795 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000796 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000797 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
798 // This must be a promotion, for something like
799 // "void a(x) short x; {..."
800 V = EmitScalarConversion(V, Ty, Arg->getType());
801 }
802 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000803 EmitParmDecl(*Arg, V);
804 break;
805 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000806 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000807
808 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000809 }
810 assert(AI == Fn->arg_end() && "Argument mismatch!");
811}
812
Daniel Dunbard931a872009-02-02 22:03:45 +0000813void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000814 llvm::Value *ReturnValue) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000815 llvm::Value *RV = 0;
816
817 // Functions with no result always return void.
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000818 if (ReturnValue) {
Daniel Dunbard931a872009-02-02 22:03:45 +0000819 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000820 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000821
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000822 switch (RetAI.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000823 case ABIArgInfo::Indirect:
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000824 if (RetTy->isAnyComplexType()) {
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000825 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
826 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
827 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedman4b1942c2009-12-04 02:43:40 +0000828 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000829 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000830 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlsson83709642009-05-19 18:50:41 +0000831 false, RetTy);
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000832 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000833 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000834
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000835 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000836 case ABIArgInfo::Direct:
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000837 // The internal return value temp always will have
838 // pointer-to-return-type type.
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000839 RV = Builder.CreateLoad(ReturnValue);
840 break;
841
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000842 case ABIArgInfo::Ignore:
843 break;
Mike Stump11289f42009-09-09 15:08:12 +0000844
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000845 case ABIArgInfo::Coerce:
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +0000846 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000847 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000848
Daniel Dunbard3674e62008-09-11 01:48:57 +0000849 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000850 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +0000851 }
852 }
Mike Stump11289f42009-09-09 15:08:12 +0000853
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000854 if (RV) {
855 Builder.CreateRet(RV);
856 } else {
857 Builder.CreateRetVoid();
858 }
Daniel Dunbar613855c2008-09-09 23:27:19 +0000859}
860
Anders Carlsson60ce3fe2009-04-08 20:47:54 +0000861RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000862 if (ArgType->isReferenceType())
Anders Carlsson3b227bd2010-02-03 16:38:03 +0000863 return EmitReferenceBindingToExpr(E);
Mike Stump11289f42009-09-09 15:08:12 +0000864
Anders Carlsson60ce3fe2009-04-08 20:47:54 +0000865 return EmitAnyExprToTemp(E);
866}
867
Daniel Dunbard931a872009-02-02 22:03:45 +0000868RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000869 llvm::Value *Callee,
Anders Carlsson61a401c2009-12-24 19:25:24 +0000870 ReturnValueSlot ReturnValue,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +0000871 const CallArgList &CallArgs,
872 const Decl *TargetDecl) {
Mike Stump18bb9282009-05-16 07:57:57 +0000873 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +0000874 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000875
876 // Handle struct-return functions by passing a pointer to the
877 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000878 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000879 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000880
881
Chris Lattner4ca97c32009-06-13 00:26:38 +0000882 // If the call returns a temporary with struct return, create a temporary
Anders Carlsson17490832009-12-24 20:40:36 +0000883 // alloca to hold the result, unless one is given to us.
884 if (CGM.ReturnTypeUsesSret(CallInfo)) {
885 llvm::Value *Value = ReturnValue.getValue();
886 if (!Value)
Daniel Dunbara7566f12010-02-09 02:48:28 +0000887 Value = CreateMemTemp(RetTy);
Anders Carlsson17490832009-12-24 20:40:36 +0000888 Args.push_back(Value);
889 }
Mike Stump11289f42009-09-09 15:08:12 +0000890
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000891 assert(CallInfo.arg_size() == CallArgs.size() &&
892 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000893 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000894 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000895 I != E; ++I, ++info_it) {
896 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000897 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000898
899 switch (ArgInfo.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000900 case ABIArgInfo::Indirect:
Daniel Dunbar747865a2009-02-05 09:16:39 +0000901 if (RV.isScalar() || RV.isComplex()) {
902 // Make a temporary alloca to pass the argument.
Daniel Dunbara7566f12010-02-09 02:48:28 +0000903 Args.push_back(CreateMemTemp(I->second));
Daniel Dunbar747865a2009-02-05 09:16:39 +0000904 if (RV.isScalar())
Anders Carlsson83709642009-05-19 18:50:41 +0000905 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000906 else
Mike Stump11289f42009-09-09 15:08:12 +0000907 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000908 } else {
909 Args.push_back(RV.getAggregateAddr());
910 }
911 break;
912
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000913 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000914 case ABIArgInfo::Direct:
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000915 if (RV.isScalar()) {
916 Args.push_back(RV.getScalarVal());
917 } else if (RV.isComplex()) {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000918 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
919 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
920 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
921 Args.push_back(Tmp);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000922 } else {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000923 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000924 }
925 break;
Mike Stump11289f42009-09-09 15:08:12 +0000926
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000927 case ABIArgInfo::Ignore:
928 break;
929
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000930 case ABIArgInfo::Coerce: {
931 // FIXME: Avoid the conversion through memory if possible.
932 llvm::Value *SrcPtr;
933 if (RV.isScalar()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +0000934 SrcPtr = CreateMemTemp(I->second, "coerce");
Anders Carlsson83709642009-05-19 18:50:41 +0000935 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000936 } else if (RV.isComplex()) {
Daniel Dunbara7566f12010-02-09 02:48:28 +0000937 SrcPtr = CreateMemTemp(I->second, "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000938 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000939 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000940 SrcPtr = RV.getAggregateAddr();
Mike Stump11289f42009-09-09 15:08:12 +0000941 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000942 *this));
943 break;
944 }
945
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000946 case ABIArgInfo::Expand:
947 ExpandTypeToArgs(I->second, RV, Args);
948 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000949 }
950 }
Mike Stump11289f42009-09-09 15:08:12 +0000951
Chris Lattner4ca97c32009-06-13 00:26:38 +0000952 // If the callee is a bitcast of a function to a varargs pointer to function
953 // type, check to see if we can remove the bitcast. This handles some cases
954 // with unprototyped functions.
955 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
956 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
957 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
958 const llvm::FunctionType *CurFT =
959 cast<llvm::FunctionType>(CurPT->getElementType());
960 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +0000961
Chris Lattner4ca97c32009-06-13 00:26:38 +0000962 if (CE->getOpcode() == llvm::Instruction::BitCast &&
963 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +0000964 ActualFT->getNumParams() == CurFT->getNumParams() &&
965 ActualFT->getNumParams() == Args.size()) {
Chris Lattner4ca97c32009-06-13 00:26:38 +0000966 bool ArgsMatch = true;
967 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
968 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
969 ArgsMatch = false;
970 break;
971 }
Mike Stump11289f42009-09-09 15:08:12 +0000972
Chris Lattner4ca97c32009-06-13 00:26:38 +0000973 // Strip the cast if we can get away with it. This is a nice cleanup,
974 // but also allows us to inline the function at -O0 if it is marked
975 // always_inline.
976 if (ArgsMatch)
977 Callee = CalleeF;
978 }
979 }
Mike Stump11289f42009-09-09 15:08:12 +0000980
Daniel Dunbar613855c2008-09-09 23:27:19 +0000981
Daniel Dunbar12347492009-02-23 17:26:39 +0000982 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000983 unsigned CallingConv;
Devang Patel322300d2008-09-25 21:02:23 +0000984 CodeGen::AttributeListType AttributeList;
Daniel Dunbar0ef34792009-09-12 00:59:20 +0000985 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar12347492009-02-23 17:26:39 +0000986 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
987 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +0000988
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000989 llvm::CallSite CS;
990 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foad7d0479f2009-05-21 09:52:38 +0000991 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +0000992 } else {
993 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +0000994 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +0000995 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +0000996 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +0000997 }
998
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000999 CS.setAttributes(Attrs);
Daniel Dunbar0ef34792009-09-12 00:59:20 +00001000 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001001
1002 // If the call doesn't return, finish the basic block and clear the
1003 // insertion point; this allows the rest of IRgen to discard
1004 // unreachable code.
1005 if (CS.doesNotReturn()) {
1006 Builder.CreateUnreachable();
1007 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001008
Mike Stump18bb9282009-05-16 07:57:57 +00001009 // FIXME: For now, emit a dummy basic block because expr emitters in
1010 // generally are not ready to handle emitting expressions at unreachable
1011 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001012 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +00001013
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001014 // Return a reasonable RValue.
1015 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001016 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +00001017
1018 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerdde0fee2009-10-05 13:47:21 +00001019 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar613855c2008-09-09 23:27:19 +00001020 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001021
1022 switch (RetAI.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +00001023 case ABIArgInfo::Indirect:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001024 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001025 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001026 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +00001027 return RValue::getAggregate(Args[0]);
Chris Lattnere09ad902009-03-22 00:32:22 +00001028 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +00001029
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001030 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +00001031 case ABIArgInfo::Direct:
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +00001032 if (RetTy->isAnyComplexType()) {
1033 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1034 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1035 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattnere09ad902009-03-22 00:32:22 +00001036 }
1037 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson17490832009-12-24 20:40:36 +00001038 llvm::Value *DestPtr = ReturnValue.getValue();
1039 bool DestIsVolatile = ReturnValue.isVolatile();
1040
1041 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001042 DestPtr = CreateMemTemp(RetTy, "agg.tmp");
Anders Carlsson17490832009-12-24 20:40:36 +00001043 DestIsVolatile = false;
1044 }
1045 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1046 return RValue::getAggregate(DestPtr);
Chris Lattnere09ad902009-03-22 00:32:22 +00001047 }
1048 return RValue::get(CI);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001049
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001050 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +00001051 // If we are ignoring an argument that had a result, make sure to
1052 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +00001053 return GetUndefRValue(RetTy);
Daniel Dunbar94a6f252009-01-26 21:26:08 +00001054
Daniel Dunbar573884e2008-09-10 07:04:09 +00001055 case ABIArgInfo::Coerce: {
Anders Carlsson17490832009-12-24 20:40:36 +00001056 llvm::Value *DestPtr = ReturnValue.getValue();
1057 bool DestIsVolatile = ReturnValue.isVolatile();
1058
1059 if (!DestPtr) {
Daniel Dunbara7566f12010-02-09 02:48:28 +00001060 DestPtr = CreateMemTemp(RetTy, "coerce");
Anders Carlsson17490832009-12-24 20:40:36 +00001061 DestIsVolatile = false;
1062 }
1063
1064 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlsson32ef8ce2008-11-25 22:21:48 +00001065 if (RetTy->isAnyComplexType())
Anders Carlsson17490832009-12-24 20:40:36 +00001066 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattnere09ad902009-03-22 00:32:22 +00001067 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson17490832009-12-24 20:40:36 +00001068 return RValue::getAggregate(DestPtr);
1069 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +00001070 }
Daniel Dunbard3674e62008-09-11 01:48:57 +00001071
Daniel Dunbard3674e62008-09-11 01:48:57 +00001072 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +00001073 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +00001074 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +00001075
1076 assert(0 && "Unhandled ABIArgInfo::Kind");
1077 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +00001078}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +00001079
1080/* VarArg handling */
1081
1082llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1083 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1084}