blob: d3a41d62a7749934f28670c6657f6522006cda94 [file] [log] [blame]
Daniel Dunbar0dbe2272008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGCall.h"
16#include "CodeGenFunction.h"
Daniel Dunbarb7688072008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000019#include "clang/AST/Decl.h"
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000021#include "clang/AST/DeclObjC.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000022#include "clang/CodeGen/CodeGenOptions.h"
Devang Pateld0646bd2008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbard14151d2009-03-02 04:32:35 +000024#include "llvm/Support/CallSite.h"
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbar9eb5c6d2009-02-03 01:05:53 +000026
27#include "ABIInfo.h"
28
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000034// FIXME: Use iterator and sidestep silly type array creation.
35
Mike Stump1eb44332009-09-09 15:08:12 +000036const
Douglas Gregor72564e72009-02-26 23:50:07 +000037CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbarbac7c252009-09-11 22:24:53 +000038 // FIXME: Set calling convention correctly, it needs to be associated with the
39 // type somehow.
Mike Stump1eb44332009-09-09 15:08:12 +000040 return getFunctionInfo(FTNP->getResultType(),
Daniel Dunbarbac7c252009-09-11 22:24:53 +000041 llvm::SmallVector<QualType, 16>(), 0);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000042}
43
Mike Stump1eb44332009-09-09 15:08:12 +000044const
Douglas Gregor72564e72009-02-26 23:50:07 +000045CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000046 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
Daniel Dunbarbac7c252009-09-11 22:24:53 +000050 // FIXME: Set calling convention correctly, it needs to be associated with the
51 // type somehow.
52 return getFunctionInfo(FTP->getResultType(), ArgTys, 0);
53}
54
55static unsigned getCallingConventionForDecl(const Decl *D) {
56 // Set the appropriate calling convention for the Function.
57 if (D->hasAttr<StdCallAttr>())
58 return llvm::CallingConv::X86_StdCall;
59
60 if (D->hasAttr<FastCallAttr>())
61 return llvm::CallingConv::X86_FastCall;
62
63 return llvm::CallingConv::C;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000064}
65
Anders Carlsson375c31c2009-10-03 19:43:08 +000066const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
67 const FunctionProtoType *FTP) {
68 llvm::SmallVector<QualType, 16> ArgTys;
69
70 // Add the 'this' pointer.
71 ArgTys.push_back(Context.getPointerType(Context.getTagDeclType(RD)));
72
73 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
74 ArgTys.push_back(FTP->getArgType(i));
75
76 // FIXME: Set calling convention correctly, it needs to be associated with the
77 // type somehow.
78 return getFunctionInfo(FTP->getResultType(), ArgTys, 0);
79}
80
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000081const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
82 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattner3eb67ca2009-05-12 20:27:19 +000083 // Add the 'this' pointer unless this is a static method.
84 if (MD->isInstance())
85 ArgTys.push_back(MD->getThisType(Context));
Mike Stump1eb44332009-09-09 15:08:12 +000086
John McCall183700f2009-09-21 23:43:11 +000087 const FunctionProtoType *FTP = MD->getType()->getAs<FunctionProtoType>();
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000088 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
89 ArgTys.push_back(FTP->getArgType(i));
Daniel Dunbarbac7c252009-09-11 22:24:53 +000090 return getFunctionInfo(FTP->getResultType(), ArgTys,
91 getCallingConventionForDecl(MD));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000092}
93
Anders Carlssonf6c56e22009-11-25 03:15:49 +000094const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
95 CXXCtorType Type) {
96 llvm::SmallVector<QualType, 16> ArgTys;
97
98 // Add the 'this' pointer.
99 ArgTys.push_back(D->getThisType(Context));
100
101 // Check if we need to add a VTT parameter (which has type void **).
102 if (Type == Ctor_Base && D->getParent()->getNumVBases() != 0)
103 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
104
105 const FunctionProtoType *FTP = D->getType()->getAs<FunctionProtoType>();
106 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
107 ArgTys.push_back(FTP->getArgType(i));
108 return getFunctionInfo(FTP->getResultType(), ArgTys,
109 getCallingConventionForDecl(D));
110}
111
112const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
113 CXXDtorType Type) {
114 llvm::SmallVector<QualType, 16> ArgTys;
115
116 // Add the 'this' pointer.
117 ArgTys.push_back(D->getThisType(Context));
118
119 // Check if we need to add a VTT parameter (which has type void **).
120 if (Type == Dtor_Base && D->getParent()->getNumVBases() != 0)
121 ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
122
123 const FunctionProtoType *FTP = D->getType()->getAs<FunctionProtoType>();
124 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
125 ArgTys.push_back(FTP->getArgType(i));
126 return getFunctionInfo(FTP->getResultType(), ArgTys,
127 getCallingConventionForDecl(D));
128}
129
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000130const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +0000131 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000132 if (MD->isInstance())
133 return getFunctionInfo(MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000135 unsigned CallingConvention = getCallingConventionForDecl(FD);
John McCall183700f2009-09-21 23:43:11 +0000136 const FunctionType *FTy = FD->getType()->getAs<FunctionType>();
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000137 if (const FunctionNoProtoType *FNTP = dyn_cast<FunctionNoProtoType>(FTy))
138 return getFunctionInfo(FNTP->getResultType(),
139 llvm::SmallVector<QualType, 16>(),
140 CallingConvention);
141
142 const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy);
143 llvm::SmallVector<QualType, 16> ArgTys;
144 // FIXME: Kill copy.
145 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
146 ArgTys.push_back(FPT->getArgType(i));
147 return getFunctionInfo(FPT->getResultType(), ArgTys, CallingConvention);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000148}
149
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000150const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
151 llvm::SmallVector<QualType, 16> ArgTys;
152 ArgTys.push_back(MD->getSelfDecl()->getType());
153 ArgTys.push_back(Context.getObjCSelType());
154 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +0000155 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000156 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000157 ArgTys.push_back((*i)->getType());
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000158 return getFunctionInfo(MD->getResultType(), ArgTys,
159 getCallingConventionForDecl(MD));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000160}
161
Mike Stump1eb44332009-09-09 15:08:12 +0000162const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000163 const CallArgList &Args,
164 unsigned CallingConvention){
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000165 // FIXME: Kill copy.
166 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000167 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar725ad312009-01-31 02:19:00 +0000168 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000169 ArgTys.push_back(i->second);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000170 return getFunctionInfo(ResTy, ArgTys, CallingConvention);
Daniel Dunbar725ad312009-01-31 02:19:00 +0000171}
172
Mike Stump1eb44332009-09-09 15:08:12 +0000173const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000174 const FunctionArgList &Args,
175 unsigned CallingConvention){
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000176 // FIXME: Kill copy.
177 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +0000178 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000179 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000180 ArgTys.push_back(i->second);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000181 return getFunctionInfo(ResTy, ArgTys, CallingConvention);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000182}
183
184const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000185 const llvm::SmallVector<QualType, 16> &ArgTys,
186 unsigned CallingConvention){
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000187 // Lookup or create unique function info.
188 llvm::FoldingSetNodeID ID;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000189 CGFunctionInfo::Profile(ID, CallingConvention, ResTy,
190 ArgTys.begin(), ArgTys.end());
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000191
192 void *InsertPos = 0;
193 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
194 if (FI)
195 return *FI;
196
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000197 // Construct the function info.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000198 FI = new CGFunctionInfo(CallingConvention, ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000199 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000200
201 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000202 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000203
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000204 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000205}
206
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000207CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
208 QualType ResTy,
209 const llvm::SmallVector<QualType, 16> &ArgTys)
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000210 : CallingConvention(_CallingConvention),
211 EffectiveCallingConvention(_CallingConvention)
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000212{
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000213 NumArgs = ArgTys.size();
214 Args = new ArgInfo[1 + NumArgs];
215 Args[0].type = ResTy;
216 for (unsigned i = 0; i < NumArgs; ++i)
217 Args[1 + i].type = ArgTys[i];
218}
219
220/***/
221
Mike Stump1eb44332009-09-09 15:08:12 +0000222void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar56273772008-09-17 00:51:38 +0000223 std::vector<const llvm::Type*> &ArgTys) {
224 const RecordType *RT = Ty->getAsStructureType();
225 assert(RT && "Can only expand structure types.");
226 const RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000227 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000228 "Cannot expand structure with flexible array.");
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000232 const FieldDecl *FD = *i;
Mike Stump1eb44332009-09-09 15:08:12 +0000233 assert(!FD->isBitField() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000234 "Cannot expand structure with bit-field members.");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Daniel Dunbar56273772008-09-17 00:51:38 +0000236 QualType FT = FD->getType();
237 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
238 GetExpandedTypes(FT, ArgTys);
239 } else {
240 ArgTys.push_back(ConvertType(FT));
241 }
242 }
243}
244
Mike Stump1eb44332009-09-09 15:08:12 +0000245llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +0000246CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
247 llvm::Function::arg_iterator AI) {
248 const RecordType *RT = Ty->getAsStructureType();
249 assert(RT && "Can only expand structure types.");
250
251 RecordDecl *RD = RT->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000252 assert(LV.isSimple() &&
253 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar56273772008-09-17 00:51:38 +0000254 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000255 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
256 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000257 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000258 QualType FT = FD->getType();
259
260 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000261 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000262 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
263 AI = ExpandTypeFromArgs(FT, LV, AI);
264 } else {
265 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
266 ++AI;
267 }
268 }
269
270 return AI;
271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273void
274CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar56273772008-09-17 00:51:38 +0000275 llvm::SmallVector<llvm::Value*, 16> &Args) {
276 const RecordType *RT = Ty->getAsStructureType();
277 assert(RT && "Can only expand structure types.");
278
279 RecordDecl *RD = RT->getDecl();
280 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
281 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000282 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
283 i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000284 FieldDecl *FD = *i;
Daniel Dunbar56273772008-09-17 00:51:38 +0000285 QualType FT = FD->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Daniel Dunbar56273772008-09-17 00:51:38 +0000287 // FIXME: What are the right qualifiers here?
Anders Carlssone6d2a532010-01-29 05:05:36 +0000288 LValue LV = EmitLValueForField(Addr, FD, 0);
Daniel Dunbar56273772008-09-17 00:51:38 +0000289 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
290 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
291 } else {
292 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump1eb44332009-09-09 15:08:12 +0000293 assert(RV.isScalar() &&
Daniel Dunbar56273772008-09-17 00:51:38 +0000294 "Unexpected non-scalar rvalue during struct expansion.");
295 Args.push_back(RV.getScalarVal());
296 }
297 }
298}
299
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000300/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
301/// a pointer to an object of type \arg Ty.
302///
303/// This safely handles the case when the src type is smaller than the
304/// destination type; in this situation the values of bits which not
305/// present in the src are undefined.
306static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
307 const llvm::Type *Ty,
308 CodeGenFunction &CGF) {
Mike Stump1eb44332009-09-09 15:08:12 +0000309 const llvm::Type *SrcTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000310 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000311 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
312 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000313
Daniel Dunbarb225be42009-02-03 05:59:18 +0000314 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000315 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000316 // Generally SrcSize is never greater than DstSize, since this means we are
317 // losing bits. However, this can happen in cases where the structure has
318 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000319 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000320 // FIXME: Assert that we aren't truncating non-padding bits when have access
321 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000322 llvm::Value *Casted =
323 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000324 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
325 // FIXME: Use better alignment / avoid requiring aligned load.
326 Load->setAlignment(1);
327 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000328 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000329 // Otherwise do coercion through memory. This is stupid, but
330 // simple.
331 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000332 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000333 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000334 llvm::StoreInst *Store =
Daniel Dunbar386621f2009-02-07 02:46:03 +0000335 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
336 // FIXME: Use better alignment / avoid requiring aligned store.
337 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000338 return CGF.Builder.CreateLoad(Tmp);
339 }
340}
341
342/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
343/// where the source and destination may have different types.
344///
345/// This safely handles the case when the src type is larger than the
346/// destination type; the upper bits of the src will be lost.
347static void CreateCoercedStore(llvm::Value *Src,
348 llvm::Value *DstPtr,
Anders Carlssond2490a92009-12-24 20:40:36 +0000349 bool DstIsVolatile,
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000350 CodeGenFunction &CGF) {
351 const llvm::Type *SrcTy = Src->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000352 const llvm::Type *DstTy =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000353 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
354
Duncan Sands9408c452009-05-09 07:08:47 +0000355 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
356 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000357
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000358 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000359 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000360 llvm::Value *Casted =
361 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000362 // FIXME: Use better alignment / avoid requiring aligned store.
Anders Carlssond2490a92009-12-24 20:40:36 +0000363 CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000364 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000365 // Otherwise do coercion through memory. This is stupid, but
366 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000367
368 // Generally SrcSize is never greater than DstSize, since this means we are
369 // losing bits. However, this can happen in cases where the structure has
370 // additional padding, for example due to a user specified alignment.
371 //
372 // FIXME: Assert that we aren't truncating non-padding bits when have access
373 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000374 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
375 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump1eb44332009-09-09 15:08:12 +0000376 llvm::Value *Casted =
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000377 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000378 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
379 // FIXME: Use better alignment / avoid requiring aligned load.
380 Load->setAlignment(1);
Anders Carlssond2490a92009-12-24 20:40:36 +0000381 CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000382 }
383}
384
Daniel Dunbar56273772008-09-17 00:51:38 +0000385/***/
386
Daniel Dunbar88b53962009-02-02 22:03:45 +0000387bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000388 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000389}
390
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000391const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000392CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000393 std::vector<const llvm::Type*> ArgTys;
394
395 const llvm::Type *ResultType = 0;
396
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000397 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000398 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000399 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000400 case ABIArgInfo::Expand:
401 assert(0 && "Invalid ABI kind for return argument");
402
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000403 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000404 case ABIArgInfo::Direct:
405 ResultType = ConvertType(RetTy);
406 break;
407
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000408 case ABIArgInfo::Indirect: {
409 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson0032b272009-08-13 21:57:51 +0000410 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000411 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000412 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
413 break;
414 }
415
Daniel Dunbar11434922009-01-26 21:26:08 +0000416 case ABIArgInfo::Ignore:
Owen Anderson0032b272009-08-13 21:57:51 +0000417 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar11434922009-01-26 21:26:08 +0000418 break;
419
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000420 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000421 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000422 break;
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
425 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000426 ie = FI.arg_end(); it != ie; ++it) {
427 const ABIArgInfo &AI = it->info;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000429 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000430 case ABIArgInfo::Ignore:
431 break;
432
Daniel Dunbar56273772008-09-17 00:51:38 +0000433 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000434 ArgTys.push_back(AI.getCoerceToType());
435 break;
436
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000437 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000438 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000439 const llvm::Type *LTy = ConvertTypeForMem(it->type);
440 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000441 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000442 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000443
444 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000445 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000446 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000447 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000449 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000450 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000451 break;
452 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000453 }
454
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000455 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000456}
457
Anders Carlssonecf282b2009-11-24 05:08:52 +0000458static bool HasIncompleteReturnTypeOrArgumentTypes(const FunctionProtoType *T) {
459 if (const TagType *TT = T->getResultType()->getAs<TagType>()) {
460 if (!TT->getDecl()->isDefinition())
461 return true;
462 }
463
464 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) {
465 if (const TagType *TT = T->getArgType(i)->getAs<TagType>()) {
466 if (!TT->getDecl()->isDefinition())
467 return true;
468 }
469 }
470
471 return false;
472}
473
474const llvm::Type *
475CodeGenTypes::GetFunctionTypeForVtable(const CXXMethodDecl *MD) {
476 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
477
478 if (!HasIncompleteReturnTypeOrArgumentTypes(FPT))
479 return GetFunctionType(getFunctionInfo(MD), FPT->isVariadic());
480
481 return llvm::OpaqueType::get(getLLVMContext());
482}
483
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000484void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000485 const Decl *TargetDecl,
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000486 AttributeListType &PAL,
487 unsigned &CallingConv) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000488 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000489 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000490
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000491 CallingConv = FI.getEffectiveCallingConvention();
492
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000493 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000494 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000495 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000496 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000497 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000498 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000499 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000500 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000501 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000502 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn76168e22009-08-09 20:07:29 +0000503 if (TargetDecl->hasAttr<MallocAttr>())
504 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000505 }
506
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000507 if (CodeGenOpts.OptimizeSize)
Daniel Dunbar7ab1c3e2009-10-27 19:48:08 +0000508 FuncAttrs |= llvm::Attribute::OptimizeForSize;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000509 if (CodeGenOpts.DisableRedZone)
Devang Patel24095da2009-06-04 23:32:02 +0000510 FuncAttrs |= llvm::Attribute::NoRedZone;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000511 if (CodeGenOpts.NoImplicitFloat)
Devang Patelacebb392009-06-05 22:05:48 +0000512 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000513
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000514 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000515 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000516 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000517 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000518 case ABIArgInfo::Extend:
519 if (RetTy->isSignedIntegerType()) {
520 RetAttrs |= llvm::Attribute::SExt;
521 } else if (RetTy->isUnsignedIntegerType()) {
522 RetAttrs |= llvm::Attribute::ZExt;
523 }
524 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000525 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000526 break;
527
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000528 case ABIArgInfo::Indirect:
Mike Stump1eb44332009-09-09 15:08:12 +0000529 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000530 llvm::Attribute::StructRet |
531 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000532 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000533 // sret disables readnone and readonly
534 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
535 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000536 break;
537
Daniel Dunbar11434922009-01-26 21:26:08 +0000538 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000539 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000540 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000541
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000542 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000543 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000544 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000545
Devang Patela2c69122008-09-26 22:53:57 +0000546 if (RetAttrs)
547 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000548
549 // FIXME: we need to honour command line settings also...
550 // FIXME: RegParm should be reduced in case of nested functions and/or global
551 // register variable.
552 signed RegParm = 0;
553 if (TargetDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000554 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000555 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000556 RegParm = RegParmAttr->getNumParams();
557
558 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000559 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000560 ie = FI.arg_end(); it != ie; ++it) {
561 QualType ParamType = it->type;
562 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000563 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000564
Nuno Lopes079b4952009-12-07 18:30:06 +0000565 if (ParamType.isRestrictQualified())
566 Attributes |= llvm::Attribute::NoAlias;
567
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000568 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000569 case ABIArgInfo::Coerce:
570 break;
571
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000572 case ABIArgInfo::Indirect:
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000573 if (AI.getIndirectByVal())
574 Attributes |= llvm::Attribute::ByVal;
575
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000576 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000577 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000578 // byval disables readnone and readonly.
579 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
580 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000581 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000582
583 case ABIArgInfo::Extend:
584 if (ParamType->isSignedIntegerType()) {
585 Attributes |= llvm::Attribute::SExt;
586 } else if (ParamType->isUnsignedIntegerType()) {
587 Attributes |= llvm::Attribute::ZExt;
588 }
589 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000590 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000591 if (RegParm > 0 &&
592 (ParamType->isIntegerType() || ParamType->isPointerType())) {
593 RegParm -=
594 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
595 if (RegParm >= 0)
596 Attributes |= llvm::Attribute::InReg;
597 }
598 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000599 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000600
Daniel Dunbar11434922009-01-26 21:26:08 +0000601 case ABIArgInfo::Ignore:
602 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000603 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000604
Daniel Dunbar56273772008-09-17 00:51:38 +0000605 case ABIArgInfo::Expand: {
Mike Stump1eb44332009-09-09 15:08:12 +0000606 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000607 // FIXME: This is rather inefficient. Do we ever actually need to do
608 // anything here? The result should be just reconstructed on the other
609 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000610 getTypes().GetExpandedTypes(ParamType, Tys);
611 Index += Tys.size();
612 continue;
613 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Devang Patel761d7f72008-09-25 21:02:23 +0000616 if (Attributes)
617 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000618 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000619 }
Devang Patela2c69122008-09-26 22:53:57 +0000620 if (FuncAttrs)
621 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000622}
623
Daniel Dunbar88b53962009-02-02 22:03:45 +0000624void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
625 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000626 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000627 // If this is an implicit-return-zero function, go ahead and
628 // initialize the return value. TODO: it might be nice to have
629 // a more general mechanism for this that didn't require synthesized
630 // return statements.
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000631 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCall0cfeb632009-07-28 01:00:58 +0000632 if (FD->hasImplicitReturnZero()) {
633 QualType RetTy = FD->getResultType().getUnqualifiedType();
634 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000635 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000636 Builder.CreateStore(Zero, ReturnValue);
637 }
638 }
639
Mike Stumpf5408fe2009-05-16 07:57:57 +0000640 // FIXME: We no longer need the types from FunctionArgList; lift up and
641 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000642
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000643 // Emit allocs for param decls. Give the LLVM Argument nodes names.
644 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000646 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000647 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000648 AI->setName("agg.result");
649 ++AI;
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000652 assert(FI.arg_size() == Args.size() &&
653 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000654 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000655 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000656 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000657 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000658 QualType Ty = info_it->type;
659 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000660
661 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000662 case ABIArgInfo::Indirect: {
663 llvm::Value* V = AI;
664 if (hasAggregateLLVMType(Ty)) {
665 // Do nothing, aggregates and complex variables are accessed by
666 // reference.
667 } else {
668 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000669 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000670 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
671 // This must be a promotion, for something like
672 // "void a(x) short x; {..."
673 V = EmitScalarConversion(V, Ty, Arg->getType());
674 }
675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676 EmitParmDecl(*Arg, V);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000677 break;
678 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000679
680 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000681 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000682 assert(AI != Fn->arg_end() && "Argument mismatch!");
683 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000684 if (hasAggregateLLVMType(Ty)) {
685 // Create a temporary alloca to hold the argument; the rest of
686 // codegen expects to access aggregates & complex values by
687 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000688 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000689 Builder.CreateStore(AI, V);
690 } else {
691 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
692 // This must be a promotion, for something like
693 // "void a(x) short x; {..."
694 V = EmitScalarConversion(V, Ty, Arg->getType());
695 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000696 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000697 EmitParmDecl(*Arg, V);
698 break;
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Daniel Dunbar56273772008-09-17 00:51:38 +0000701 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000702 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000703 // we need to create a temporary and reconstruct it from the
704 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000705 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000706 Arg->getName() + ".addr");
Daniel Dunbar56273772008-09-17 00:51:38 +0000707 // FIXME: What are the right qualifiers here?
Mike Stump1eb44332009-09-09 15:08:12 +0000708 llvm::Function::arg_iterator End =
John McCall0953e762009-09-24 19:53:00 +0000709 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp, Qualifiers()), AI);
Daniel Dunbar56273772008-09-17 00:51:38 +0000710 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000711
Daniel Dunbar56273772008-09-17 00:51:38 +0000712 // Name the arguments used in expansion and increment AI.
713 unsigned Index = 0;
714 for (; AI != End; ++AI, ++Index)
Daniel Dunbar259e9cc2009-10-19 01:21:05 +0000715 AI->setName(Arg->getName() + "." + llvm::Twine(Index));
Daniel Dunbar56273772008-09-17 00:51:38 +0000716 continue;
717 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000718
719 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000720 // Initialize the local variable appropriately.
Mike Stump1eb44332009-09-09 15:08:12 +0000721 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000722 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000723 } else {
724 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000727 // Skip increment, no matching LLVM parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000728 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000729
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000730 case ABIArgInfo::Coerce: {
731 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000732 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
733 // result in a new alloca anyway, so we could just store into that
734 // directly if we broke the abstraction down more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000735 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Anders Carlssond2490a92009-12-24 20:40:36 +0000736 CreateCoercedStore(AI, V, /*DestIsVolatile=*/false, *this);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000737 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000738 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000739 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000740 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
741 // This must be a promotion, for something like
742 // "void a(x) short x; {..."
743 V = EmitScalarConversion(V, Ty, Arg->getType());
744 }
745 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000746 EmitParmDecl(*Arg, V);
747 break;
748 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000749 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000750
751 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000752 }
753 assert(AI == Fn->arg_end() && "Argument mismatch!");
754}
755
Daniel Dunbar88b53962009-02-02 22:03:45 +0000756void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000757 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000758 llvm::Value *RV = 0;
759
760 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000761 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000762 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000763 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000764
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000765 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000766 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000767 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000768 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
769 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
770 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Eli Friedmanb17daf92009-12-04 02:43:40 +0000771 // Do nothing; aggregrates get evaluated directly into the destination.
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000772 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000773 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000774 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000775 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000776 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000777
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000778 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000779 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000780 // The internal return value temp always will have
781 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000782 RV = Builder.CreateLoad(ReturnValue);
783 break;
784
Daniel Dunbar11434922009-01-26 21:26:08 +0000785 case ABIArgInfo::Ignore:
786 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000788 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000789 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000790 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000791
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000792 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +0000793 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000794 }
795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000797 if (RV) {
798 Builder.CreateRet(RV);
799 } else {
800 Builder.CreateRetVoid();
801 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000802}
803
Anders Carlsson0139bb92009-04-08 20:47:54 +0000804RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000805 if (ArgType->isReferenceType())
806 return EmitReferenceBindingToExpr(E, ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson0139bb92009-04-08 20:47:54 +0000808 return EmitAnyExprToTemp(E);
809}
810
Daniel Dunbar88b53962009-02-02 22:03:45 +0000811RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump1eb44332009-09-09 15:08:12 +0000812 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000813 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000814 const CallArgList &CallArgs,
815 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000816 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000817 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000818
819 // Handle struct-return functions by passing a pointer to the
820 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000821 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000822 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000823
824
Chris Lattner5db7ae52009-06-13 00:26:38 +0000825 // If the call returns a temporary with struct return, create a temporary
Anders Carlssond2490a92009-12-24 20:40:36 +0000826 // alloca to hold the result, unless one is given to us.
827 if (CGM.ReturnTypeUsesSret(CallInfo)) {
828 llvm::Value *Value = ReturnValue.getValue();
829 if (!Value)
830 Value = CreateTempAlloca(ConvertTypeForMem(RetTy));
831 Args.push_back(Value);
832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000834 assert(CallInfo.arg_size() == CallArgs.size() &&
835 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000836 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000837 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000838 I != E; ++I, ++info_it) {
839 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000840 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000841
842 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000843 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000844 if (RV.isScalar() || RV.isComplex()) {
845 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000846 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000847 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000848 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000849 else
Mike Stump1eb44332009-09-09 15:08:12 +0000850 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000851 } else {
852 Args.push_back(RV.getAggregateAddr());
853 }
854 break;
855
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000856 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000857 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000858 if (RV.isScalar()) {
859 Args.push_back(RV.getScalarVal());
860 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000861 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
862 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
863 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
864 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000865 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000866 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000867 }
868 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Daniel Dunbar11434922009-01-26 21:26:08 +0000870 case ABIArgInfo::Ignore:
871 break;
872
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000873 case ABIArgInfo::Coerce: {
874 // FIXME: Avoid the conversion through memory if possible.
875 llvm::Value *SrcPtr;
876 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +0000877 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000878 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000879 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000880 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000881 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000882 } else
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000883 SrcPtr = RV.getAggregateAddr();
Mike Stump1eb44332009-09-09 15:08:12 +0000884 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000885 *this));
886 break;
887 }
888
Daniel Dunbar56273772008-09-17 00:51:38 +0000889 case ABIArgInfo::Expand:
890 ExpandTypeToArgs(I->second, RV, Args);
891 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000892 }
893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Chris Lattner5db7ae52009-06-13 00:26:38 +0000895 // If the callee is a bitcast of a function to a varargs pointer to function
896 // type, check to see if we can remove the bitcast. This handles some cases
897 // with unprototyped functions.
898 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
899 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
900 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
901 const llvm::FunctionType *CurFT =
902 cast<llvm::FunctionType>(CurPT->getElementType());
903 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattner5db7ae52009-06-13 00:26:38 +0000905 if (CE->getOpcode() == llvm::Instruction::BitCast &&
906 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000907 ActualFT->getNumParams() == CurFT->getNumParams() &&
908 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000909 bool ArgsMatch = true;
910 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
911 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
912 ArgsMatch = false;
913 break;
914 }
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Chris Lattner5db7ae52009-06-13 00:26:38 +0000916 // Strip the cast if we can get away with it. This is a nice cleanup,
917 // but also allows us to inline the function at -O0 if it is marked
918 // always_inline.
919 if (ArgsMatch)
920 Callee = CalleeF;
921 }
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000924
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000925 llvm::BasicBlock *InvokeDest = getInvokeDest();
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000926 unsigned CallingConv;
Devang Patel761d7f72008-09-25 21:02:23 +0000927 CodeGen::AttributeListType AttributeList;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000928 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000929 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
930 AttributeList.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Daniel Dunbard14151d2009-03-02 04:32:35 +0000932 llvm::CallSite CS;
933 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000934 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000935 } else {
936 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump1eb44332009-09-09 15:08:12 +0000937 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000938 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000939 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000940 }
941
Daniel Dunbard14151d2009-03-02 04:32:35 +0000942 CS.setAttributes(Attrs);
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000943 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
Daniel Dunbard14151d2009-03-02 04:32:35 +0000944
945 // If the call doesn't return, finish the basic block and clear the
946 // insertion point; this allows the rest of IRgen to discard
947 // unreachable code.
948 if (CS.doesNotReturn()) {
949 Builder.CreateUnreachable();
950 Builder.ClearInsertionPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Mike Stumpf5408fe2009-05-16 07:57:57 +0000952 // FIXME: For now, emit a dummy basic block because expr emitters in
953 // generally are not ready to handle emitting expressions at unreachable
954 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +0000955 EnsureInsertPoint();
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Daniel Dunbard14151d2009-03-02 04:32:35 +0000957 // Return a reasonable RValue.
958 return GetUndefRValue(RetTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000959 }
Daniel Dunbard14151d2009-03-02 04:32:35 +0000960
961 llvm::Instruction *CI = CS.getInstruction();
Benjamin Kramerffbb15e2009-10-05 13:47:21 +0000962 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000963 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000964
965 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000966 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000967 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000968 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +0000969 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +0000970 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +0000971 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000972
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000973 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000974 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000975 if (RetTy->isAnyComplexType()) {
976 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
977 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
978 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +0000979 }
980 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssond2490a92009-12-24 20:40:36 +0000981 llvm::Value *DestPtr = ReturnValue.getValue();
982 bool DestIsVolatile = ReturnValue.isVolatile();
983
984 if (!DestPtr) {
985 DestPtr = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
986 DestIsVolatile = false;
987 }
988 Builder.CreateStore(CI, DestPtr, DestIsVolatile);
989 return RValue::getAggregate(DestPtr);
Chris Lattner34030842009-03-22 00:32:22 +0000990 }
991 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000992
Daniel Dunbar11434922009-01-26 21:26:08 +0000993 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000994 // If we are ignoring an argument that had a result, make sure to
995 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +0000996 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +0000997
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000998 case ABIArgInfo::Coerce: {
Anders Carlssond2490a92009-12-24 20:40:36 +0000999 llvm::Value *DestPtr = ReturnValue.getValue();
1000 bool DestIsVolatile = ReturnValue.isVolatile();
1001
1002 if (!DestPtr) {
1003 DestPtr = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
1004 DestIsVolatile = false;
1005 }
1006
1007 CreateCoercedStore(CI, DestPtr, DestIsVolatile, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +00001008 if (RetTy->isAnyComplexType())
Anders Carlssond2490a92009-12-24 20:40:36 +00001009 return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
Chris Lattner34030842009-03-22 00:32:22 +00001010 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssond2490a92009-12-24 20:40:36 +00001011 return RValue::getAggregate(DestPtr);
1012 return RValue::get(EmitLoadOfScalar(DestPtr, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +00001013 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001014
Daniel Dunbar8951dbd2008-09-11 01:48:57 +00001015 case ABIArgInfo::Expand:
Mike Stump1eb44332009-09-09 15:08:12 +00001016 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001017 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +00001018
1019 assert(0 && "Unhandled ABIArgInfo::Kind");
1020 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001021}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +00001022
1023/* VarArg handling */
1024
1025llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1026 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1027}