blob: fcaa49e129eb38fe6038f30a0d9499efcf11612f [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"
Devang Patel6e467b12009-06-04 23:32:02 +000022#include "clang/Frontend/CompileOptions.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
Mike Stump11289f42009-09-09 15:08:12 +000036const
Douglas Gregordeaad8c2009-02-26 23:50:07 +000037CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar7feafc72009-09-11 22:24:53 +000038 // FIXME: Set calling convention correctly, it needs to be associated with the
39 // type somehow.
Mike Stump11289f42009-09-09 15:08:12 +000040 return getFunctionInfo(FTNP->getResultType(),
Daniel Dunbar7feafc72009-09-11 22:24:53 +000041 llvm::SmallVector<QualType, 16>(), 0);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000042}
43
Mike Stump11289f42009-09-09 15:08:12 +000044const
Douglas Gregordeaad8c2009-02-26 23:50:07 +000045CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000046 llvm::SmallVector<QualType, 16> ArgTys;
47 // FIXME: Kill copy.
Daniel Dunbar7a95ca32008-09-10 04:01:49 +000048 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000049 ArgTys.push_back(FTP->getArgType(i));
Daniel Dunbar7feafc72009-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 Dunbar7a95ca32008-09-10 04:01:49 +000064}
65
Anders Carlssonb15b55c2009-04-03 22:48:58 +000066const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
67 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattnerbea5b622009-05-12 20:27:19 +000068 // Add the 'this' pointer unless this is a static method.
69 if (MD->isInstance())
70 ArgTys.push_back(MD->getThisType(Context));
Mike Stump11289f42009-09-09 15:08:12 +000071
Anders Carlssonb15b55c2009-04-03 22:48:58 +000072 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
73 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
74 ArgTys.push_back(FTP->getArgType(i));
Daniel Dunbar7feafc72009-09-11 22:24:53 +000075 return getFunctionInfo(FTP->getResultType(), ArgTys,
76 getCallingConventionForDecl(MD));
Anders Carlssonb15b55c2009-04-03 22:48:58 +000077}
78
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000079const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerbea5b622009-05-12 20:27:19 +000080 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonb15b55c2009-04-03 22:48:58 +000081 if (MD->isInstance())
82 return getFunctionInfo(MD);
Mike Stump11289f42009-09-09 15:08:12 +000083
Daniel Dunbar7feafc72009-09-11 22:24:53 +000084 unsigned CallingConvention = getCallingConventionForDecl(FD);
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000085 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Daniel Dunbar7feafc72009-09-11 22:24:53 +000086 if (const FunctionNoProtoType *FNTP = dyn_cast<FunctionNoProtoType>(FTy))
87 return getFunctionInfo(FNTP->getResultType(),
88 llvm::SmallVector<QualType, 16>(),
89 CallingConvention);
90
91 const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy);
92 llvm::SmallVector<QualType, 16> ArgTys;
93 // FIXME: Kill copy.
94 for (unsigned i = 0, e = FPT->getNumArgs(); i != e; ++i)
95 ArgTys.push_back(FPT->getArgType(i));
96 return getFunctionInfo(FPT->getResultType(), ArgTys, CallingConvention);
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000097}
98
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +000099const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
100 llvm::SmallVector<QualType, 16> ArgTys;
101 ArgTys.push_back(MD->getSelfDecl()->getType());
102 ArgTys.push_back(Context.getObjCSelType());
103 // FIXME: Kill copy?
Chris Lattner90669d02009-02-20 06:23:21 +0000104 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000105 e = MD->param_end(); i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000106 ArgTys.push_back((*i)->getType());
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000107 return getFunctionInfo(MD->getResultType(), ArgTys,
108 getCallingConventionForDecl(MD));
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000112 const CallArgList &Args,
113 unsigned CallingConvention){
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000114 // FIXME: Kill copy.
115 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000116 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000117 i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000118 ArgTys.push_back(i->second);
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000119 return getFunctionInfo(ResTy, ArgTys, CallingConvention);
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000120}
121
Mike Stump11289f42009-09-09 15:08:12 +0000122const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000123 const FunctionArgList &Args,
124 unsigned CallingConvention){
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000125 // FIXME: Kill copy.
126 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump11289f42009-09-09 15:08:12 +0000127 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000128 i != e; ++i)
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000129 ArgTys.push_back(i->second);
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000130 return getFunctionInfo(ResTy, ArgTys, CallingConvention);
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000131}
132
133const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000134 const llvm::SmallVector<QualType, 16> &ArgTys,
135 unsigned CallingConvention){
Daniel Dunbare0be8292009-02-03 00:07:12 +0000136 // Lookup or create unique function info.
137 llvm::FoldingSetNodeID ID;
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000138 CGFunctionInfo::Profile(ID, CallingConvention, ResTy,
139 ArgTys.begin(), ArgTys.end());
Daniel Dunbare0be8292009-02-03 00:07:12 +0000140
141 void *InsertPos = 0;
142 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
143 if (FI)
144 return *FI;
145
Daniel Dunbar313321e2009-02-03 05:31:23 +0000146 // Construct the function info.
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000147 FI = new CGFunctionInfo(CallingConvention, ResTy, ArgTys);
Daniel Dunbarfff09f32009-02-05 00:00:23 +0000148 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar313321e2009-02-03 05:31:23 +0000149
150 // Compute ABI information.
Owen Anderson170229f2009-07-14 23:10:40 +0000151 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar313321e2009-02-03 05:31:23 +0000152
Daniel Dunbare0be8292009-02-03 00:07:12 +0000153 return *FI;
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +0000154}
155
Daniel Dunbar7feafc72009-09-11 22:24:53 +0000156CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
157 QualType ResTy,
158 const llvm::SmallVector<QualType, 16> &ArgTys)
159 : CallingConvention(_CallingConvention)
160{
Daniel Dunbar313321e2009-02-03 05:31:23 +0000161 NumArgs = ArgTys.size();
162 Args = new ArgInfo[1 + NumArgs];
163 Args[0].type = ResTy;
164 for (unsigned i = 0; i < NumArgs; ++i)
165 Args[1 + i].type = ArgTys[i];
166}
167
168/***/
169
Mike Stump11289f42009-09-09 15:08:12 +0000170void CodeGenTypes::GetExpandedTypes(QualType Ty,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000171 std::vector<const llvm::Type*> &ArgTys) {
172 const RecordType *RT = Ty->getAsStructureType();
173 assert(RT && "Can only expand structure types.");
174 const RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000175 assert(!RD->hasFlexibleArrayMember() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000176 "Cannot expand structure with flexible array.");
Mike Stump11289f42009-09-09 15:08:12 +0000177
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000178 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
179 i != e; ++i) {
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000180 const FieldDecl *FD = *i;
Mike Stump11289f42009-09-09 15:08:12 +0000181 assert(!FD->isBitField() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000182 "Cannot expand structure with bit-field members.");
Mike Stump11289f42009-09-09 15:08:12 +0000183
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000184 QualType FT = FD->getType();
185 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
186 GetExpandedTypes(FT, ArgTys);
187 } else {
188 ArgTys.push_back(ConvertType(FT));
189 }
190 }
191}
192
Mike Stump11289f42009-09-09 15:08:12 +0000193llvm::Function::arg_iterator
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000194CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
195 llvm::Function::arg_iterator AI) {
196 const RecordType *RT = Ty->getAsStructureType();
197 assert(RT && "Can only expand structure types.");
198
199 RecordDecl *RD = RT->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000200 assert(LV.isSimple() &&
201 "Unexpected non-simple lvalue during struct expansion.");
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000202 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000203 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
204 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000205 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000206 QualType FT = FD->getType();
207
208 // FIXME: What are the right qualifiers here?
209 LValue LV = EmitLValueForField(Addr, FD, false, 0);
210 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
211 AI = ExpandTypeFromArgs(FT, LV, AI);
212 } else {
213 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
214 ++AI;
215 }
216 }
217
218 return AI;
219}
220
Mike Stump11289f42009-09-09 15:08:12 +0000221void
222CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000223 llvm::SmallVector<llvm::Value*, 16> &Args) {
224 const RecordType *RT = Ty->getAsStructureType();
225 assert(RT && "Can only expand structure types.");
226
227 RecordDecl *RD = RT->getDecl();
228 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
229 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
231 i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000232 FieldDecl *FD = *i;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000233 QualType FT = FD->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000234
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000235 // FIXME: What are the right qualifiers here?
236 LValue LV = EmitLValueForField(Addr, FD, false, 0);
237 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
238 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
239 } else {
240 RValue RV = EmitLoadOfLValue(LV, FT);
Mike Stump11289f42009-09-09 15:08:12 +0000241 assert(RV.isScalar() &&
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000242 "Unexpected non-scalar rvalue during struct expansion.");
243 Args.push_back(RV.getScalarVal());
244 }
245 }
246}
247
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000248/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
249/// a pointer to an object of type \arg Ty.
250///
251/// This safely handles the case when the src type is smaller than the
252/// destination type; in this situation the values of bits which not
253/// present in the src are undefined.
254static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
255 const llvm::Type *Ty,
256 CodeGenFunction &CGF) {
Mike Stump11289f42009-09-09 15:08:12 +0000257 const llvm::Type *SrcTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000258 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000259 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
260 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000261
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000262 // If load is legal, just bitcast the src pointer.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000263 if (SrcSize >= DstSize) {
Mike Stump18bb9282009-05-16 07:57:57 +0000264 // Generally SrcSize is never greater than DstSize, since this means we are
265 // losing bits. However, this can happen in cases where the structure has
266 // additional padding, for example due to a user specified alignment.
Daniel Dunbarffdb8432009-05-13 18:54:26 +0000267 //
Mike Stump18bb9282009-05-16 07:57:57 +0000268 // FIXME: Assert that we aren't truncating non-padding bits when have access
269 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000270 llvm::Value *Casted =
271 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000272 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
273 // FIXME: Use better alignment / avoid requiring aligned load.
274 Load->setAlignment(1);
275 return Load;
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000276 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000277 // Otherwise do coercion through memory. This is stupid, but
278 // simple.
279 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000280 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000281 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Mike Stump11289f42009-09-09 15:08:12 +0000282 llvm::StoreInst *Store =
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000283 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
284 // FIXME: Use better alignment / avoid requiring aligned store.
285 Store->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000286 return CGF.Builder.CreateLoad(Tmp);
287 }
288}
289
290/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
291/// where the source and destination may have different types.
292///
293/// This safely handles the case when the src type is larger than the
294/// destination type; the upper bits of the src will be lost.
295static void CreateCoercedStore(llvm::Value *Src,
296 llvm::Value *DstPtr,
297 CodeGenFunction &CGF) {
298 const llvm::Type *SrcTy = Src->getType();
Mike Stump11289f42009-09-09 15:08:12 +0000299 const llvm::Type *DstTy =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000300 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
301
Duncan Sandsc76fe8b2009-05-09 07:08:47 +0000302 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
303 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000304
Daniel Dunbar313321e2009-02-03 05:31:23 +0000305 // If store is legal, just bitcast the src pointer.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000306 if (SrcSize <= DstSize) {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000307 llvm::Value *Casted =
308 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000309 // FIXME: Use better alignment / avoid requiring aligned store.
310 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000311 } else {
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000312 // Otherwise do coercion through memory. This is stupid, but
313 // simple.
Daniel Dunbar4be99ff2009-06-05 07:58:54 +0000314
315 // Generally SrcSize is never greater than DstSize, since this means we are
316 // losing bits. However, this can happen in cases where the structure has
317 // additional padding, for example due to a user specified alignment.
318 //
319 // FIXME: Assert that we aren't truncating non-padding bits when have access
320 // to that information.
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000321 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
322 CGF.Builder.CreateStore(Src, Tmp);
Mike Stump11289f42009-09-09 15:08:12 +0000323 llvm::Value *Casted =
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000324 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbaree9e4c22009-02-07 02:46:03 +0000325 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
326 // FIXME: Use better alignment / avoid requiring aligned load.
327 Load->setAlignment(1);
328 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbarf5589ac2009-02-02 19:06:38 +0000329 }
330}
331
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000332/***/
333
Daniel Dunbard931a872009-02-02 22:03:45 +0000334bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000335 return FI.getReturnInfo().isIndirect();
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000336}
337
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000338const llvm::FunctionType *
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000339CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000340 std::vector<const llvm::Type*> ArgTys;
341
342 const llvm::Type *ResultType = 0;
343
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000344 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000345 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbard3674e62008-09-11 01:48:57 +0000346 switch (RetAI.getKind()) {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000347 case ABIArgInfo::Expand:
348 assert(0 && "Invalid ABI kind for return argument");
349
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000350 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000351 case ABIArgInfo::Direct:
352 ResultType = ConvertType(RetTy);
353 break;
354
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000355 case ABIArgInfo::Indirect: {
356 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Owen Anderson41a75022009-08-13 21:57:51 +0000357 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbarb8b47592008-09-10 07:00:50 +0000358 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000359 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
360 break;
361 }
362
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000363 case ABIArgInfo::Ignore:
Owen Anderson41a75022009-08-13 21:57:51 +0000364 ResultType = llvm::Type::getVoidTy(getLLVMContext());
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000365 break;
366
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000367 case ABIArgInfo::Coerce:
Daniel Dunbar573884e2008-09-10 07:04:09 +0000368 ResultType = RetAI.getCoerceToType();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000369 break;
370 }
Mike Stump11289f42009-09-09 15:08:12 +0000371
372 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000373 ie = FI.arg_end(); it != ie; ++it) {
374 const ABIArgInfo &AI = it->info;
Mike Stump11289f42009-09-09 15:08:12 +0000375
Daniel Dunbard3674e62008-09-11 01:48:57 +0000376 switch (AI.getKind()) {
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000377 case ABIArgInfo::Ignore:
378 break;
379
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000380 case ABIArgInfo::Coerce:
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000381 ArgTys.push_back(AI.getCoerceToType());
382 break;
383
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000384 case ABIArgInfo::Indirect: {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000385 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000386 const llvm::Type *LTy = ConvertTypeForMem(it->type);
387 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000388 break;
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000389 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000390
391 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000392 case ABIArgInfo::Direct:
Daniel Dunbar747865a2009-02-05 09:16:39 +0000393 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000394 break;
Mike Stump11289f42009-09-09 15:08:12 +0000395
Daniel Dunbard3674e62008-09-11 01:48:57 +0000396 case ABIArgInfo::Expand:
Daniel Dunbar313321e2009-02-03 05:31:23 +0000397 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000398 break;
399 }
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000400 }
401
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000402 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar81cf67f2008-09-09 23:48:28 +0000403}
404
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000405void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbard931a872009-02-02 22:03:45 +0000406 const Decl *TargetDecl,
Devang Patel322300d2008-09-25 21:02:23 +0000407 AttributeListType &PAL) {
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000408 unsigned FuncAttrs = 0;
Devang Patel597e7082008-09-26 22:53:57 +0000409 unsigned RetAttrs = 0;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000410
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000411 // FIXME: handle sseregparm someday...
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000412 if (TargetDecl) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000413 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000414 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000415 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel322300d2008-09-25 21:02:23 +0000416 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000417 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssonb8316282008-10-05 23:32:53 +0000418 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000419 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar8c920c92009-04-10 22:14:52 +0000420 FuncAttrs |= llvm::Attribute::ReadOnly;
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000421 if (TargetDecl->hasAttr<MallocAttr>())
422 RetAttrs |= llvm::Attribute::NoAlias;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000423 }
424
Devang Patel6e467b12009-06-04 23:32:02 +0000425 if (CompileOpts.DisableRedZone)
426 FuncAttrs |= llvm::Attribute::NoRedZone;
Devang Patel9e243862009-06-05 22:05:48 +0000427 if (CompileOpts.NoImplicitFloat)
428 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel6e467b12009-06-04 23:32:02 +0000429
Bill Wendling18351072009-06-28 23:01:01 +0000430 if (Features.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000431 FuncAttrs |= llvm::Attribute::StackProtect;
Bill Wendling18351072009-06-28 23:01:01 +0000432 else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendlingd63bbad2009-06-28 07:36:13 +0000433 FuncAttrs |= llvm::Attribute::StackProtectReq;
434
Daniel Dunbar3668cb22009-02-02 23:43:58 +0000435 QualType RetTy = FI.getReturnType();
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000436 unsigned Index = 1;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000437 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +0000438 switch (RetAI.getKind()) {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000439 case ABIArgInfo::Extend:
440 if (RetTy->isSignedIntegerType()) {
441 RetAttrs |= llvm::Attribute::SExt;
442 } else if (RetTy->isUnsignedIntegerType()) {
443 RetAttrs |= llvm::Attribute::ZExt;
444 }
445 // FALLTHROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000446 case ABIArgInfo::Direct:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000447 break;
448
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000449 case ABIArgInfo::Indirect:
Mike Stump11289f42009-09-09 15:08:12 +0000450 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar3cd20632009-01-31 02:19:00 +0000451 llvm::Attribute::StructRet |
452 llvm::Attribute::NoAlias));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000453 ++Index;
Daniel Dunbarc2304432009-03-18 19:51:01 +0000454 // sret disables readnone and readonly
455 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
456 llvm::Attribute::ReadNone);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000457 break;
458
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000459 case ABIArgInfo::Ignore:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000460 case ABIArgInfo::Coerce:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000461 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000462
Daniel Dunbard3674e62008-09-11 01:48:57 +0000463 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000464 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000465 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000466
Devang Patel597e7082008-09-26 22:53:57 +0000467 if (RetAttrs)
468 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000469
470 // FIXME: we need to honour command line settings also...
471 // FIXME: RegParm should be reduced in case of nested functions and/or global
472 // register variable.
473 signed RegParm = 0;
474 if (TargetDecl)
Mike Stump11289f42009-09-09 15:08:12 +0000475 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000476 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000477 RegParm = RegParmAttr->getNumParams();
478
479 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Mike Stump11289f42009-09-09 15:08:12 +0000480 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
Daniel Dunbar313321e2009-02-03 05:31:23 +0000481 ie = FI.arg_end(); it != ie; ++it) {
482 QualType ParamType = it->type;
483 const ABIArgInfo &AI = it->info;
Devang Patel322300d2008-09-25 21:02:23 +0000484 unsigned Attributes = 0;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000485
Daniel Dunbard3674e62008-09-11 01:48:57 +0000486 switch (AI.getKind()) {
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000487 case ABIArgInfo::Coerce:
488 break;
489
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000490 case ABIArgInfo::Indirect:
Devang Patel322300d2008-09-25 21:02:23 +0000491 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000492 Attributes |=
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000493 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbarc2304432009-03-18 19:51:01 +0000494 // byval disables readnone and readonly.
495 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
496 llvm::Attribute::ReadNone);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000497 break;
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000498
499 case ABIArgInfo::Extend:
500 if (ParamType->isSignedIntegerType()) {
501 Attributes |= llvm::Attribute::SExt;
502 } else if (ParamType->isUnsignedIntegerType()) {
503 Attributes |= llvm::Attribute::ZExt;
504 }
505 // FALLS THROUGH
Daniel Dunbar67dace892009-02-03 06:17:37 +0000506 case ABIArgInfo::Direct:
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000507 if (RegParm > 0 &&
508 (ParamType->isIntegerType() || ParamType->isPointerType())) {
509 RegParm -=
510 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
511 if (RegParm >= 0)
512 Attributes |= llvm::Attribute::InReg;
513 }
514 // FIXME: handle sseregparm someday...
Daniel Dunbard3674e62008-09-11 01:48:57 +0000515 break;
Anton Korobeynikovc8478242009-04-04 00:49:24 +0000516
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000517 case ABIArgInfo::Ignore:
518 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000519 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000520
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000521 case ABIArgInfo::Expand: {
Mike Stump11289f42009-09-09 15:08:12 +0000522 std::vector<const llvm::Type*> Tys;
Mike Stump18bb9282009-05-16 07:57:57 +0000523 // FIXME: This is rather inefficient. Do we ever actually need to do
524 // anything here? The result should be just reconstructed on the other
525 // side, so extension should be a non-issue.
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000526 getTypes().GetExpandedTypes(ParamType, Tys);
527 Index += Tys.size();
528 continue;
529 }
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Devang Patel322300d2008-09-25 21:02:23 +0000532 if (Attributes)
533 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000534 ++Index;
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000535 }
Devang Patel597e7082008-09-26 22:53:57 +0000536 if (FuncAttrs)
537 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar76c8eb72008-09-10 00:32:18 +0000538}
539
Daniel Dunbard931a872009-02-02 22:03:45 +0000540void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
541 llvm::Function *Fn,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000542 const FunctionArgList &Args) {
John McCallcaa19452009-07-28 01:00:58 +0000543 // If this is an implicit-return-zero function, go ahead and
544 // initialize the return value. TODO: it might be nice to have
545 // a more general mechanism for this that didn't require synthesized
546 // return statements.
Anders Carlssonb8be93f2009-08-08 23:24:23 +0000547 if (const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
John McCallcaa19452009-07-28 01:00:58 +0000548 if (FD->hasImplicitReturnZero()) {
549 QualType RetTy = FD->getResultType().getUnqualifiedType();
550 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Anderson0b75f232009-07-31 20:28:54 +0000551 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCallcaa19452009-07-28 01:00:58 +0000552 Builder.CreateStore(Zero, ReturnValue);
553 }
554 }
555
Mike Stump18bb9282009-05-16 07:57:57 +0000556 // FIXME: We no longer need the types from FunctionArgList; lift up and
557 // simplify.
Daniel Dunbar5a0acdc92009-02-03 06:02:10 +0000558
Daniel Dunbar613855c2008-09-09 23:27:19 +0000559 // Emit allocs for param decls. Give the LLVM Argument nodes names.
560 llvm::Function::arg_iterator AI = Fn->arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000561
Daniel Dunbar613855c2008-09-09 23:27:19 +0000562 // Name the struct return argument.
Daniel Dunbard931a872009-02-02 22:03:45 +0000563 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000564 AI->setName("agg.result");
565 ++AI;
566 }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000568 assert(FI.arg_size() == Args.size() &&
569 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000570 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar613855c2008-09-09 23:27:19 +0000571 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000572 i != e; ++i, ++info_it) {
Daniel Dunbar613855c2008-09-09 23:27:19 +0000573 const VarDecl *Arg = i->first;
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000574 QualType Ty = info_it->type;
575 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000576
577 switch (ArgI.getKind()) {
Daniel Dunbar747865a2009-02-05 09:16:39 +0000578 case ABIArgInfo::Indirect: {
579 llvm::Value* V = AI;
580 if (hasAggregateLLVMType(Ty)) {
581 // Do nothing, aggregates and complex variables are accessed by
582 // reference.
583 } else {
584 // Load scalar value from indirect argument.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000585 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000586 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
587 // This must be a promotion, for something like
588 // "void a(x) short x; {..."
589 V = EmitScalarConversion(V, Ty, Arg->getType());
590 }
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592 EmitParmDecl(*Arg, V);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000593 break;
594 }
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000595
596 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000597 case ABIArgInfo::Direct: {
Daniel Dunbard3674e62008-09-11 01:48:57 +0000598 assert(AI != Fn->arg_end() && "Argument mismatch!");
599 llvm::Value* V = AI;
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000600 if (hasAggregateLLVMType(Ty)) {
601 // Create a temporary alloca to hold the argument; the rest of
602 // codegen expects to access aggregates & complex values by
603 // reference.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000604 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000605 Builder.CreateStore(AI, V);
606 } else {
607 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
608 // This must be a promotion, for something like
609 // "void a(x) short x; {..."
610 V = EmitScalarConversion(V, Ty, Arg->getType());
611 }
Daniel Dunbar613855c2008-09-09 23:27:19 +0000612 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000613 EmitParmDecl(*Arg, V);
614 break;
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000617 case ABIArgInfo::Expand: {
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000618 // If this structure was expanded into multiple arguments then
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000619 // we need to create a temporary and reconstruct it from the
620 // arguments.
Chris Lattner1cbaacc2008-11-24 04:00:27 +0000621 std::string Name = Arg->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +0000622 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000623 (Name + ".addr").c_str());
624 // FIXME: What are the right qualifiers here?
Mike Stump11289f42009-09-09 15:08:12 +0000625 llvm::Function::arg_iterator End =
626 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000627 EmitParmDecl(*Arg, Temp);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000628
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000629 // Name the arguments used in expansion and increment AI.
630 unsigned Index = 0;
631 for (; AI != End; ++AI, ++Index)
Daniel Dunbar4074b9312009-08-02 01:43:57 +0000632 AI->setName(Name + "." + llvm::Twine(Index));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000633 continue;
634 }
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000635
636 case ABIArgInfo::Ignore:
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000637 // Initialize the local variable appropriately.
Mike Stump11289f42009-09-09 15:08:12 +0000638 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000639 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbard5f1f552009-02-10 00:06:49 +0000640 } else {
641 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
642 }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Daniel Dunbarfc7c7612009-02-03 20:00:13 +0000644 // Skip increment, no matching LLVM parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000645 continue;
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000646
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000647 case ABIArgInfo::Coerce: {
648 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stump18bb9282009-05-16 07:57:57 +0000649 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
650 // result in a new alloca anyway, so we could just store into that
651 // directly if we broke the abstraction down more.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000652 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000653 CreateCoercedStore(AI, V, *this);
654 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000655 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000656 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6e3b7df2009-02-04 07:22:24 +0000657 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
658 // This must be a promotion, for something like
659 // "void a(x) short x; {..."
660 V = EmitScalarConversion(V, Ty, Arg->getType());
661 }
662 }
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000663 EmitParmDecl(*Arg, V);
664 break;
665 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000666 }
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000667
668 ++AI;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000669 }
670 assert(AI == Fn->arg_end() && "Argument mismatch!");
671}
672
Daniel Dunbard931a872009-02-02 22:03:45 +0000673void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar613855c2008-09-09 23:27:19 +0000674 llvm::Value *ReturnValue) {
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000675 llvm::Value *RV = 0;
676
677 // Functions with no result always return void.
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000678 if (ReturnValue) {
Daniel Dunbard931a872009-02-02 22:03:45 +0000679 QualType RetTy = FI.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000680 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000681
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000682 switch (RetAI.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000683 case ABIArgInfo::Indirect:
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000684 if (RetTy->isAnyComplexType()) {
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000685 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
686 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
687 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
688 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
689 } else {
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000690 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlsson83709642009-05-19 18:50:41 +0000691 false, RetTy);
Daniel Dunbar9ae0afd2008-12-18 04:52:14 +0000692 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000693 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000694
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000695 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000696 case ABIArgInfo::Direct:
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000697 // The internal return value temp always will have
698 // pointer-to-return-type type.
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000699 RV = Builder.CreateLoad(ReturnValue);
700 break;
701
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000702 case ABIArgInfo::Ignore:
703 break;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000705 case ABIArgInfo::Coerce:
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +0000706 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbard3674e62008-09-11 01:48:57 +0000707 break;
Daniel Dunbard3674e62008-09-11 01:48:57 +0000708
Daniel Dunbard3674e62008-09-11 01:48:57 +0000709 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000710 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +0000711 }
712 }
Mike Stump11289f42009-09-09 15:08:12 +0000713
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000714 if (RV) {
715 Builder.CreateRet(RV);
716 } else {
717 Builder.CreateRetVoid();
718 }
Daniel Dunbar613855c2008-09-09 23:27:19 +0000719}
720
Anders Carlsson60ce3fe2009-04-08 20:47:54 +0000721RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000722 if (ArgType->isReferenceType())
723 return EmitReferenceBindingToExpr(E, ArgType);
Mike Stump11289f42009-09-09 15:08:12 +0000724
Anders Carlsson60ce3fe2009-04-08 20:47:54 +0000725 return EmitAnyExprToTemp(E);
726}
727
Daniel Dunbard931a872009-02-02 22:03:45 +0000728RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Mike Stump11289f42009-09-09 15:08:12 +0000729 llvm::Value *Callee,
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +0000730 const CallArgList &CallArgs,
731 const Decl *TargetDecl) {
Mike Stump18bb9282009-05-16 07:57:57 +0000732 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar613855c2008-09-09 23:27:19 +0000733 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000734
735 // Handle struct-return functions by passing a pointer to the
736 // location that we would like to return into.
Daniel Dunbar7633cbf2009-02-02 21:43:58 +0000737 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000738 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000739
740
Chris Lattner4ca97c32009-06-13 00:26:38 +0000741 // If the call returns a temporary with struct return, create a temporary
742 // alloca to hold the result.
743 if (CGM.ReturnTypeUsesSret(CallInfo))
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000744 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Mike Stump11289f42009-09-09 15:08:12 +0000745
Daniel Dunbara45bdbb2009-02-04 21:17:21 +0000746 assert(CallInfo.arg_size() == CallArgs.size() &&
747 "Mismatch between function signature & arguments.");
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000748 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000749 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb52d0772009-02-03 05:59:18 +0000750 I != E; ++I, ++info_it) {
751 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000752 RValue RV = I->first;
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000753
754 switch (ArgInfo.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000755 case ABIArgInfo::Indirect:
Daniel Dunbar747865a2009-02-05 09:16:39 +0000756 if (RV.isScalar() || RV.isComplex()) {
757 // Make a temporary alloca to pass the argument.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000758 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar747865a2009-02-05 09:16:39 +0000759 if (RV.isScalar())
Anders Carlsson83709642009-05-19 18:50:41 +0000760 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000761 else
Mike Stump11289f42009-09-09 15:08:12 +0000762 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
Daniel Dunbar747865a2009-02-05 09:16:39 +0000763 } else {
764 Args.push_back(RV.getAggregateAddr());
765 }
766 break;
767
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000768 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000769 case ABIArgInfo::Direct:
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000770 if (RV.isScalar()) {
771 Args.push_back(RV.getScalarVal());
772 } else if (RV.isComplex()) {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000773 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
774 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
775 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
776 Args.push_back(Tmp);
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000777 } else {
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000778 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000779 }
780 break;
Mike Stump11289f42009-09-09 15:08:12 +0000781
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000782 case ABIArgInfo::Ignore:
783 break;
784
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000785 case ABIArgInfo::Coerce: {
786 // FIXME: Avoid the conversion through memory if possible.
787 llvm::Value *SrcPtr;
788 if (RV.isScalar()) {
Daniel Dunbara3346112009-02-03 23:04:57 +0000789 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlsson83709642009-05-19 18:50:41 +0000790 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000791 } else if (RV.isComplex()) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000792 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000793 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
Mike Stump11289f42009-09-09 15:08:12 +0000794 } else
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000795 SrcPtr = RV.getAggregateAddr();
Mike Stump11289f42009-09-09 15:08:12 +0000796 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000797 *this));
798 break;
799 }
800
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000801 case ABIArgInfo::Expand:
802 ExpandTypeToArgs(I->second, RV, Args);
803 break;
Daniel Dunbar613855c2008-09-09 23:27:19 +0000804 }
805 }
Mike Stump11289f42009-09-09 15:08:12 +0000806
Chris Lattner4ca97c32009-06-13 00:26:38 +0000807 // If the callee is a bitcast of a function to a varargs pointer to function
808 // type, check to see if we can remove the bitcast. This handles some cases
809 // with unprototyped functions.
810 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
811 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
812 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
813 const llvm::FunctionType *CurFT =
814 cast<llvm::FunctionType>(CurPT->getElementType());
815 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
Mike Stump11289f42009-09-09 15:08:12 +0000816
Chris Lattner4ca97c32009-06-13 00:26:38 +0000817 if (CE->getOpcode() == llvm::Instruction::BitCast &&
818 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattner4c8da962009-06-23 01:38:41 +0000819 ActualFT->getNumParams() == CurFT->getNumParams() &&
820 ActualFT->getNumParams() == Args.size()) {
Chris Lattner4ca97c32009-06-13 00:26:38 +0000821 bool ArgsMatch = true;
822 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
823 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
824 ArgsMatch = false;
825 break;
826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner4ca97c32009-06-13 00:26:38 +0000828 // Strip the cast if we can get away with it. This is a nice cleanup,
829 // but also allows us to inline the function at -O0 if it is marked
830 // always_inline.
831 if (ArgsMatch)
832 Callee = CalleeF;
833 }
834 }
Mike Stump11289f42009-09-09 15:08:12 +0000835
Daniel Dunbar613855c2008-09-09 23:27:19 +0000836
Daniel Dunbar12347492009-02-23 17:26:39 +0000837 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel322300d2008-09-25 21:02:23 +0000838 CodeGen::AttributeListType AttributeList;
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +0000839 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar12347492009-02-23 17:26:39 +0000840 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
841 AttributeList.end());
Mike Stump11289f42009-09-09 15:08:12 +0000842
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000843 llvm::CallSite CS;
844 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foad7d0479f2009-05-21 09:52:38 +0000845 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +0000846 } else {
847 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump11289f42009-09-09 15:08:12 +0000848 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad7d0479f2009-05-21 09:52:38 +0000849 Args.data(), Args.data()+Args.size());
Daniel Dunbar12347492009-02-23 17:26:39 +0000850 EmitBlock(Cont);
Daniel Dunbar5006f4a2009-02-20 18:54:31 +0000851 }
852
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000853 CS.setAttributes(Attrs);
Daniel Dunbarbbaeca42009-09-11 22:25:00 +0000854 llvm::CallingConv::ID CC =
855 static_cast<llvm::CallingConv::ID>(CallInfo.getCallingConvention());
856 CS.setCallingConv(CC);
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000857
858 // If the call doesn't return, finish the basic block and clear the
859 // insertion point; this allows the rest of IRgen to discard
860 // unreachable code.
861 if (CS.doesNotReturn()) {
862 Builder.CreateUnreachable();
863 Builder.ClearInsertionPoint();
Mike Stump11289f42009-09-09 15:08:12 +0000864
Mike Stump18bb9282009-05-16 07:57:57 +0000865 // FIXME: For now, emit a dummy basic block because expr emitters in
866 // generally are not ready to handle emitting expressions at unreachable
867 // points.
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000868 EnsureInsertPoint();
Mike Stump11289f42009-09-09 15:08:12 +0000869
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000870 // Return a reasonable RValue.
871 return GetUndefRValue(RetTy);
Mike Stump11289f42009-09-09 15:08:12 +0000872 }
Daniel Dunbarb960b7b2009-03-02 04:32:35 +0000873
874 llvm::Instruction *CI = CS.getInstruction();
Owen Anderson41a75022009-08-13 21:57:51 +0000875 if (Builder.isNamePreserving() &&
876 CI->getType() != llvm::Type::getVoidTy(VMContext))
Daniel Dunbar613855c2008-09-09 23:27:19 +0000877 CI->setName("call");
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000878
879 switch (RetAI.getKind()) {
Daniel Dunbarb8b1c672009-02-05 08:00:50 +0000880 case ABIArgInfo::Indirect:
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000881 if (RetTy->isAnyComplexType())
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000882 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattnere09ad902009-03-22 00:32:22 +0000883 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar8fc81b02008-09-17 00:51:38 +0000884 return RValue::getAggregate(Args[0]);
Chris Lattnere09ad902009-03-22 00:32:22 +0000885 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbard3674e62008-09-11 01:48:57 +0000886
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000887 case ABIArgInfo::Extend:
Daniel Dunbar67dace892009-02-03 06:17:37 +0000888 case ABIArgInfo::Direct:
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000889 if (RetTy->isAnyComplexType()) {
890 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
891 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
892 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattnere09ad902009-03-22 00:32:22 +0000893 }
894 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000895 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar5d3dbd62009-02-05 11:13:54 +0000896 Builder.CreateStore(CI, V);
897 return RValue::getAggregate(V);
Chris Lattnere09ad902009-03-22 00:32:22 +0000898 }
899 return RValue::get(CI);
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000900
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000901 case ABIArgInfo::Ignore:
Daniel Dunbar01362822009-02-03 06:30:17 +0000902 // If we are ignoring an argument that had a result, make sure to
903 // construct the appropriate return value for our caller.
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000904 return GetUndefRValue(RetTy);
Daniel Dunbar94a6f252009-01-26 21:26:08 +0000905
Daniel Dunbar573884e2008-09-10 07:04:09 +0000906 case ABIArgInfo::Coerce: {
Daniel Dunbar2f219b02009-02-03 19:12:28 +0000907 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar9bfb4de2009-02-10 01:51:39 +0000908 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar0f4aa3c2009-01-27 01:36:03 +0000909 CreateCoercedStore(CI, V, *this);
Anders Carlsson32ef8ce2008-11-25 22:21:48 +0000910 if (RetTy->isAnyComplexType())
911 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattnere09ad902009-03-22 00:32:22 +0000912 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlsson32ef8ce2008-11-25 22:21:48 +0000913 return RValue::getAggregate(V);
Chris Lattnere09ad902009-03-22 00:32:22 +0000914 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar573884e2008-09-10 07:04:09 +0000915 }
Daniel Dunbard3674e62008-09-11 01:48:57 +0000916
Daniel Dunbard3674e62008-09-11 01:48:57 +0000917 case ABIArgInfo::Expand:
Mike Stump11289f42009-09-09 15:08:12 +0000918 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar613855c2008-09-09 23:27:19 +0000919 }
Daniel Dunbara72d4ae2008-09-10 02:41:04 +0000920
921 assert(0 && "Unhandled ABIArgInfo::Kind");
922 return RValue::get(0);
Daniel Dunbar613855c2008-09-09 23:27:19 +0000923}
Daniel Dunbar2d0746f2009-02-10 20:44:09 +0000924
925/* VarArg handling */
926
927llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
928 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
929}