blob: be30fcc5ca9fc675bbb11d4d9ad07ade831c42b6 [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"
Devang Patel24095da2009-06-04 23:32:02 +000022#include "clang/Frontend/CompileOptions.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
Daniel Dunbar541b63b2009-02-02 23:23:47 +000036const
Douglas Gregor72564e72009-02-26 23:50:07 +000037CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000038 return getFunctionInfo(FTNP->getResultType(),
39 llvm::SmallVector<QualType, 16>());
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000040}
41
Daniel Dunbar541b63b2009-02-02 23:23:47 +000042const
Douglas Gregor72564e72009-02-26 23:50:07 +000043CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar541b63b2009-02-02 23:23:47 +000044 llvm::SmallVector<QualType, 16> ArgTys;
45 // FIXME: Kill copy.
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000046 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000047 ArgTys.push_back(FTP->getArgType(i));
48 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000049}
50
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000051const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
52 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattner3eb67ca2009-05-12 20:27:19 +000053 // Add the 'this' pointer unless this is a static method.
54 if (MD->isInstance())
55 ArgTys.push_back(MD->getThisType(Context));
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000056
57 const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType();
58 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
59 ArgTys.push_back(FTP->getArgType(i));
60 return getFunctionInfo(FTP->getResultType(), ArgTys);
61}
62
Daniel Dunbar541b63b2009-02-02 23:23:47 +000063const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattner3eb67ca2009-05-12 20:27:19 +000064 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000065 if (MD->isInstance())
66 return getFunctionInfo(MD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000067
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000068 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +000069 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar541b63b2009-02-02 23:23:47 +000070 return getFunctionInfo(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +000071 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000072}
73
Daniel Dunbar541b63b2009-02-02 23:23:47 +000074const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
75 llvm::SmallVector<QualType, 16> ArgTys;
76 ArgTys.push_back(MD->getSelfDecl()->getType());
77 ArgTys.push_back(Context.getObjCSelType());
78 // FIXME: Kill copy?
Chris Lattner20732162009-02-20 06:23:21 +000079 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000080 e = MD->param_end(); i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000081 ArgTys.push_back((*i)->getType());
82 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000083}
84
Daniel Dunbar541b63b2009-02-02 23:23:47 +000085const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
86 const CallArgList &Args) {
87 // FIXME: Kill copy.
88 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbar725ad312009-01-31 02:19:00 +000089 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
90 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +000091 ArgTys.push_back(i->second);
92 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbar725ad312009-01-31 02:19:00 +000093}
94
Daniel Dunbar541b63b2009-02-02 23:23:47 +000095const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
96 const FunctionArgList &Args) {
97 // FIXME: Kill copy.
98 llvm::SmallVector<QualType, 16> ArgTys;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000099 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
100 i != e; ++i)
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000101 ArgTys.push_back(i->second);
102 return getFunctionInfo(ResTy, ArgTys);
103}
104
105const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
106 const llvm::SmallVector<QualType, 16> &ArgTys) {
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000107 // Lookup or create unique function info.
108 llvm::FoldingSetNodeID ID;
109 CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
110
111 void *InsertPos = 0;
112 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
113 if (FI)
114 return *FI;
115
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000116 // Construct the function info.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000117 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000118 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000119
120 // Compute ABI information.
Owen Andersona1cf15f2009-07-14 23:10:40 +0000121 getABIInfo().computeInfo(*FI, getContext(), TheModule.getContext());
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000122
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000123 return *FI;
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000124}
125
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000126CGFunctionInfo::CGFunctionInfo(QualType ResTy,
127 const llvm::SmallVector<QualType, 16> &ArgTys) {
128 NumArgs = ArgTys.size();
129 Args = new ArgInfo[1 + NumArgs];
130 Args[0].type = ResTy;
131 for (unsigned i = 0; i < NumArgs; ++i)
132 Args[1 + i].type = ArgTys[i];
133}
134
135/***/
136
Daniel Dunbar56273772008-09-17 00:51:38 +0000137void CodeGenTypes::GetExpandedTypes(QualType Ty,
138 std::vector<const llvm::Type*> &ArgTys) {
139 const RecordType *RT = Ty->getAsStructureType();
140 assert(RT && "Can only expand structure types.");
141 const RecordDecl *RD = RT->getDecl();
142 assert(!RD->hasFlexibleArrayMember() &&
143 "Cannot expand structure with flexible array.");
144
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000145 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
146 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000147 const FieldDecl *FD = *i;
148 assert(!FD->isBitField() &&
149 "Cannot expand structure with bit-field members.");
150
151 QualType FT = FD->getType();
152 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
153 GetExpandedTypes(FT, ArgTys);
154 } else {
155 ArgTys.push_back(ConvertType(FT));
156 }
157 }
158}
159
160llvm::Function::arg_iterator
161CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
162 llvm::Function::arg_iterator AI) {
163 const RecordType *RT = Ty->getAsStructureType();
164 assert(RT && "Can only expand structure types.");
165
166 RecordDecl *RD = RT->getDecl();
167 assert(LV.isSimple() &&
168 "Unexpected non-simple lvalue during struct expansion.");
169 llvm::Value *Addr = LV.getAddress();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000170 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
171 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000172 FieldDecl *FD = *i;
173 QualType FT = FD->getType();
174
175 // FIXME: What are the right qualifiers here?
176 LValue LV = EmitLValueForField(Addr, FD, false, 0);
177 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
178 AI = ExpandTypeFromArgs(FT, LV, AI);
179 } else {
180 EmitStoreThroughLValue(RValue::get(AI), LV, FT);
181 ++AI;
182 }
183 }
184
185 return AI;
186}
187
188void
189CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
190 llvm::SmallVector<llvm::Value*, 16> &Args) {
191 const RecordType *RT = Ty->getAsStructureType();
192 assert(RT && "Can only expand structure types.");
193
194 RecordDecl *RD = RT->getDecl();
195 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
196 llvm::Value *Addr = RV.getAggregateAddr();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
198 i != e; ++i) {
Daniel Dunbar56273772008-09-17 00:51:38 +0000199 FieldDecl *FD = *i;
200 QualType FT = FD->getType();
201
202 // FIXME: What are the right qualifiers here?
203 LValue LV = EmitLValueForField(Addr, FD, false, 0);
204 if (CodeGenFunction::hasAggregateLLVMType(FT)) {
205 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
206 } else {
207 RValue RV = EmitLoadOfLValue(LV, FT);
208 assert(RV.isScalar() &&
209 "Unexpected non-scalar rvalue during struct expansion.");
210 Args.push_back(RV.getScalarVal());
211 }
212 }
213}
214
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000215/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
216/// a pointer to an object of type \arg Ty.
217///
218/// This safely handles the case when the src type is smaller than the
219/// destination type; in this situation the values of bits which not
220/// present in the src are undefined.
221static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
222 const llvm::Type *Ty,
223 CodeGenFunction &CGF) {
224 const llvm::Type *SrcTy =
225 cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
Duncan Sands9408c452009-05-09 07:08:47 +0000226 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
227 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000228
Daniel Dunbarb225be42009-02-03 05:59:18 +0000229 // If load is legal, just bitcast the src pointer.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000230 if (SrcSize >= DstSize) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000231 // Generally SrcSize is never greater than DstSize, since this means we are
232 // losing bits. However, this can happen in cases where the structure has
233 // additional padding, for example due to a user specified alignment.
Daniel Dunbar7ef455b2009-05-13 18:54:26 +0000234 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000235 // FIXME: Assert that we aren't truncating non-padding bits when have access
236 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000237 llvm::Value *Casted =
238 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000239 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
240 // FIXME: Use better alignment / avoid requiring aligned load.
241 Load->setAlignment(1);
242 return Load;
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000243 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000244 // Otherwise do coercion through memory. This is stupid, but
245 // simple.
246 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
247 llvm::Value *Casted =
248 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000249 llvm::StoreInst *Store =
250 CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
251 // FIXME: Use better alignment / avoid requiring aligned store.
252 Store->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000253 return CGF.Builder.CreateLoad(Tmp);
254 }
255}
256
257/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
258/// where the source and destination may have different types.
259///
260/// This safely handles the case when the src type is larger than the
261/// destination type; the upper bits of the src will be lost.
262static void CreateCoercedStore(llvm::Value *Src,
263 llvm::Value *DstPtr,
264 CodeGenFunction &CGF) {
265 const llvm::Type *SrcTy = Src->getType();
266 const llvm::Type *DstTy =
267 cast<llvm::PointerType>(DstPtr->getType())->getElementType();
268
Duncan Sands9408c452009-05-09 07:08:47 +0000269 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
270 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000271
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000272 // If store is legal, just bitcast the src pointer.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000273 if (SrcSize <= DstSize) {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000274 llvm::Value *Casted =
275 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000276 // FIXME: Use better alignment / avoid requiring aligned store.
277 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000278 } else {
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000279 // Otherwise do coercion through memory. This is stupid, but
280 // simple.
Daniel Dunbarfdf49862009-06-05 07:58:54 +0000281
282 // Generally SrcSize is never greater than DstSize, since this means we are
283 // losing bits. However, this can happen in cases where the structure has
284 // additional padding, for example due to a user specified alignment.
285 //
286 // FIXME: Assert that we aren't truncating non-padding bits when have access
287 // to that information.
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000288 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
289 CGF.Builder.CreateStore(Src, Tmp);
290 llvm::Value *Casted =
291 CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
Daniel Dunbar386621f2009-02-07 02:46:03 +0000292 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
293 // FIXME: Use better alignment / avoid requiring aligned load.
294 Load->setAlignment(1);
295 CGF.Builder.CreateStore(Load, DstPtr);
Daniel Dunbar275e10d2009-02-02 19:06:38 +0000296 }
297}
298
Daniel Dunbar56273772008-09-17 00:51:38 +0000299/***/
300
Daniel Dunbar88b53962009-02-02 22:03:45 +0000301bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000302 return FI.getReturnInfo().isIndirect();
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000303}
304
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000305const llvm::FunctionType *
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000306CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000307 std::vector<const llvm::Type*> ArgTys;
308
309 const llvm::Type *ResultType = 0;
310
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000311 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000312 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000313 switch (RetAI.getKind()) {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000314 case ABIArgInfo::Expand:
315 assert(0 && "Invalid ABI kind for return argument");
316
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000317 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000318 case ABIArgInfo::Direct:
319 ResultType = ConvertType(RetTy);
320 break;
321
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000322 case ABIArgInfo::Indirect: {
323 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000324 ResultType = llvm::Type::VoidTy;
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000325 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000326 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
327 break;
328 }
329
Daniel Dunbar11434922009-01-26 21:26:08 +0000330 case ABIArgInfo::Ignore:
331 ResultType = llvm::Type::VoidTy;
332 break;
333
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000334 case ABIArgInfo::Coerce:
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000335 ResultType = RetAI.getCoerceToType();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000336 break;
337 }
338
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000339 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
340 ie = FI.arg_end(); it != ie; ++it) {
341 const ABIArgInfo &AI = it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000342
343 switch (AI.getKind()) {
Daniel Dunbar11434922009-01-26 21:26:08 +0000344 case ABIArgInfo::Ignore:
345 break;
346
Daniel Dunbar56273772008-09-17 00:51:38 +0000347 case ABIArgInfo::Coerce:
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000348 ArgTys.push_back(AI.getCoerceToType());
349 break;
350
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000351 case ABIArgInfo::Indirect: {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000352 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000353 const llvm::Type *LTy = ConvertTypeForMem(it->type);
354 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000355 break;
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000356 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000357
358 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000359 case ABIArgInfo::Direct:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000360 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000361 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000362
363 case ABIArgInfo::Expand:
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000364 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000365 break;
366 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000367 }
368
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000369 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar3913f182008-09-09 23:48:28 +0000370}
371
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000372void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar88b53962009-02-02 22:03:45 +0000373 const Decl *TargetDecl,
Devang Patel761d7f72008-09-25 21:02:23 +0000374 AttributeListType &PAL) {
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000375 unsigned FuncAttrs = 0;
Devang Patela2c69122008-09-26 22:53:57 +0000376 unsigned RetAttrs = 0;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000377
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000378 // FIXME: handle sseregparm someday...
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000379 if (TargetDecl) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000380 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000381 FuncAttrs |= llvm::Attribute::NoUnwind;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000382 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patel761d7f72008-09-25 21:02:23 +0000383 FuncAttrs |= llvm::Attribute::NoReturn;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000384 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlsson232eb7d2008-10-05 23:32:53 +0000385 FuncAttrs |= llvm::Attribute::ReadNone;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000386 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar64c2e072009-04-10 22:14:52 +0000387 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000388 }
389
Devang Patel24095da2009-06-04 23:32:02 +0000390 if (CompileOpts.DisableRedZone)
391 FuncAttrs |= llvm::Attribute::NoRedZone;
Devang Patelacebb392009-06-05 22:05:48 +0000392 if (CompileOpts.NoImplicitFloat)
393 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patel24095da2009-06-04 23:32:02 +0000394
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000395 if (Features.getStackProtectorMode() == LangOptions::SSPOn)
Bill Wendling45483f72009-06-28 07:36:13 +0000396 FuncAttrs |= llvm::Attribute::StackProtect;
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000397 else if (Features.getStackProtectorMode() == LangOptions::SSPReq)
Bill Wendling45483f72009-06-28 07:36:13 +0000398 FuncAttrs |= llvm::Attribute::StackProtectReq;
399
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000400 QualType RetTy = FI.getReturnType();
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000401 unsigned Index = 1;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000402 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000403 switch (RetAI.getKind()) {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000404 case ABIArgInfo::Extend:
405 if (RetTy->isSignedIntegerType()) {
406 RetAttrs |= llvm::Attribute::SExt;
407 } else if (RetTy->isUnsignedIntegerType()) {
408 RetAttrs |= llvm::Attribute::ZExt;
409 }
410 // FALLTHROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000411 case ABIArgInfo::Direct:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000412 break;
413
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000414 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +0000415 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbar725ad312009-01-31 02:19:00 +0000416 llvm::Attribute::StructRet |
417 llvm::Attribute::NoAlias));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000418 ++Index;
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000419 // sret disables readnone and readonly
420 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
421 llvm::Attribute::ReadNone);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000422 break;
423
Daniel Dunbar11434922009-01-26 21:26:08 +0000424 case ABIArgInfo::Ignore:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000425 case ABIArgInfo::Coerce:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000426 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000427
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000428 case ABIArgInfo::Expand:
429 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000430 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000431
Devang Patela2c69122008-09-26 22:53:57 +0000432 if (RetAttrs)
433 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000434
435 // FIXME: we need to honour command line settings also...
436 // FIXME: RegParm should be reduced in case of nested functions and/or global
437 // register variable.
438 signed RegParm = 0;
439 if (TargetDecl)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000440 if (const RegparmAttr *RegParmAttr
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000441 = TargetDecl->getAttr<RegparmAttr>())
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000442 RegParm = RegParmAttr->getNumParams();
443
444 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000445 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
446 ie = FI.arg_end(); it != ie; ++it) {
447 QualType ParamType = it->type;
448 const ABIArgInfo &AI = it->info;
Devang Patel761d7f72008-09-25 21:02:23 +0000449 unsigned Attributes = 0;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000450
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000451 switch (AI.getKind()) {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000452 case ABIArgInfo::Coerce:
453 break;
454
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000455 case ABIArgInfo::Indirect:
Devang Patel761d7f72008-09-25 21:02:23 +0000456 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000457 Attributes |=
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000458 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar0ac86f02009-03-18 19:51:01 +0000459 // byval disables readnone and readonly.
460 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
461 llvm::Attribute::ReadNone);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000462 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000463
464 case ABIArgInfo::Extend:
465 if (ParamType->isSignedIntegerType()) {
466 Attributes |= llvm::Attribute::SExt;
467 } else if (ParamType->isUnsignedIntegerType()) {
468 Attributes |= llvm::Attribute::ZExt;
469 }
470 // FALLS THROUGH
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000471 case ABIArgInfo::Direct:
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000472 if (RegParm > 0 &&
473 (ParamType->isIntegerType() || ParamType->isPointerType())) {
474 RegParm -=
475 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
476 if (RegParm >= 0)
477 Attributes |= llvm::Attribute::InReg;
478 }
479 // FIXME: handle sseregparm someday...
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000480 break;
Anton Korobeynikov1102f422009-04-04 00:49:24 +0000481
Daniel Dunbar11434922009-01-26 21:26:08 +0000482 case ABIArgInfo::Ignore:
483 // Skip increment, no matching LLVM parameter.
484 continue;
485
Daniel Dunbar56273772008-09-17 00:51:38 +0000486 case ABIArgInfo::Expand: {
487 std::vector<const llvm::Type*> Tys;
Mike Stumpf5408fe2009-05-16 07:57:57 +0000488 // FIXME: This is rather inefficient. Do we ever actually need to do
489 // anything here? The result should be just reconstructed on the other
490 // side, so extension should be a non-issue.
Daniel Dunbar56273772008-09-17 00:51:38 +0000491 getTypes().GetExpandedTypes(ParamType, Tys);
492 Index += Tys.size();
493 continue;
494 }
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000495 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000496
Devang Patel761d7f72008-09-25 21:02:23 +0000497 if (Attributes)
498 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar56273772008-09-17 00:51:38 +0000499 ++Index;
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000500 }
Devang Patela2c69122008-09-26 22:53:57 +0000501 if (FuncAttrs)
502 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbar5323a4b2008-09-10 00:32:18 +0000503}
504
Daniel Dunbar88b53962009-02-02 22:03:45 +0000505void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
506 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000507 const FunctionArgList &Args) {
John McCall0cfeb632009-07-28 01:00:58 +0000508 // If this is an implicit-return-zero function, go ahead and
509 // initialize the return value. TODO: it might be nice to have
510 // a more general mechanism for this that didn't require synthesized
511 // return statements.
512 if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
513 if (FD->hasImplicitReturnZero()) {
514 QualType RetTy = FD->getResultType().getUnqualifiedType();
515 const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000516 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
John McCall0cfeb632009-07-28 01:00:58 +0000517 Builder.CreateStore(Zero, ReturnValue);
518 }
519 }
520
Mike Stumpf5408fe2009-05-16 07:57:57 +0000521 // FIXME: We no longer need the types from FunctionArgList; lift up and
522 // simplify.
Daniel Dunbar5251afa2009-02-03 06:02:10 +0000523
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000524 // Emit allocs for param decls. Give the LLVM Argument nodes names.
525 llvm::Function::arg_iterator AI = Fn->arg_begin();
526
527 // Name the struct return argument.
Daniel Dunbar88b53962009-02-02 22:03:45 +0000528 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000529 AI->setName("agg.result");
530 ++AI;
531 }
Daniel Dunbarb225be42009-02-03 05:59:18 +0000532
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000533 assert(FI.arg_size() == Args.size() &&
534 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000535 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000536 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000537 i != e; ++i, ++info_it) {
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000538 const VarDecl *Arg = i->first;
Daniel Dunbarb225be42009-02-03 05:59:18 +0000539 QualType Ty = info_it->type;
540 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000541
542 switch (ArgI.getKind()) {
Daniel Dunbar1f745982009-02-05 09:16:39 +0000543 case ABIArgInfo::Indirect: {
544 llvm::Value* V = AI;
545 if (hasAggregateLLVMType(Ty)) {
546 // Do nothing, aggregates and complex variables are accessed by
547 // reference.
548 } else {
549 // Load scalar value from indirect argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000550 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000551 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
552 // This must be a promotion, for something like
553 // "void a(x) short x; {..."
554 V = EmitScalarConversion(V, Ty, Arg->getType());
555 }
556 }
557 EmitParmDecl(*Arg, V);
558 break;
559 }
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000560
561 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000562 case ABIArgInfo::Direct: {
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000563 assert(AI != Fn->arg_end() && "Argument mismatch!");
564 llvm::Value* V = AI;
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000565 if (hasAggregateLLVMType(Ty)) {
566 // Create a temporary alloca to hold the argument; the rest of
567 // codegen expects to access aggregates & complex values by
568 // reference.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000569 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000570 Builder.CreateStore(AI, V);
571 } else {
572 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
573 // This must be a promotion, for something like
574 // "void a(x) short x; {..."
575 V = EmitScalarConversion(V, Ty, Arg->getType());
576 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000577 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000578 EmitParmDecl(*Arg, V);
579 break;
580 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000581
582 case ABIArgInfo::Expand: {
Daniel Dunbarb225be42009-02-03 05:59:18 +0000583 // If this structure was expanded into multiple arguments then
Daniel Dunbar56273772008-09-17 00:51:38 +0000584 // we need to create a temporary and reconstruct it from the
585 // arguments.
Chris Lattner39f34e92008-11-24 04:00:27 +0000586 std::string Name = Arg->getNameAsString();
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000587 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar56273772008-09-17 00:51:38 +0000588 (Name + ".addr").c_str());
589 // FIXME: What are the right qualifiers here?
590 llvm::Function::arg_iterator End =
591 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
592 EmitParmDecl(*Arg, Temp);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000593
Daniel Dunbar56273772008-09-17 00:51:38 +0000594 // Name the arguments used in expansion and increment AI.
595 unsigned Index = 0;
596 for (; AI != End; ++AI, ++Index)
597 AI->setName(Name + "." + llvm::utostr(Index));
598 continue;
599 }
Daniel Dunbar11434922009-01-26 21:26:08 +0000600
601 case ABIArgInfo::Ignore:
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000602 // Initialize the local variable appropriately.
603 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000604 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar8b979d92009-02-10 00:06:49 +0000605 } else {
606 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
607 }
608
Daniel Dunbar59e5a0e2009-02-03 20:00:13 +0000609 // Skip increment, no matching LLVM parameter.
610 continue;
Daniel Dunbar11434922009-01-26 21:26:08 +0000611
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000612 case ABIArgInfo::Coerce: {
613 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpf5408fe2009-05-16 07:57:57 +0000614 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
615 // result in a new alloca anyway, so we could just store into that
616 // directly if we broke the abstraction down more.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000617 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000618 CreateCoercedStore(AI, V, *this);
619 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000620 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000621 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar8b29a382009-02-04 07:22:24 +0000622 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
623 // This must be a promotion, for something like
624 // "void a(x) short x; {..."
625 V = EmitScalarConversion(V, Ty, Arg->getType());
626 }
627 }
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000628 EmitParmDecl(*Arg, V);
629 break;
630 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000631 }
Daniel Dunbar56273772008-09-17 00:51:38 +0000632
633 ++AI;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000634 }
635 assert(AI == Fn->arg_end() && "Argument mismatch!");
636}
637
Daniel Dunbar88b53962009-02-02 22:03:45 +0000638void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000639 llvm::Value *ReturnValue) {
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000640 llvm::Value *RV = 0;
641
642 // Functions with no result always return void.
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000643 if (ReturnValue) {
Daniel Dunbar88b53962009-02-02 22:03:45 +0000644 QualType RetTy = FI.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000645 const ABIArgInfo &RetAI = FI.getReturnInfo();
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000646
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000647 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000648 case ABIArgInfo::Indirect:
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000649 if (RetTy->isAnyComplexType()) {
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000650 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
651 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
652 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
653 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
654 } else {
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000655 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000656 false, RetTy);
Daniel Dunbar3aea8ca2008-12-18 04:52:14 +0000657 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000658 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000659
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000660 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000661 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000662 // The internal return value temp always will have
663 // pointer-to-return-type type.
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000664 RV = Builder.CreateLoad(ReturnValue);
665 break;
666
Daniel Dunbar11434922009-01-26 21:26:08 +0000667 case ABIArgInfo::Ignore:
668 break;
669
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000670 case ABIArgInfo::Coerce:
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000671 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000672 break;
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000673
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000674 case ABIArgInfo::Expand:
675 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000676 }
677 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000678
679 if (RV) {
680 Builder.CreateRet(RV);
681 } else {
682 Builder.CreateRetVoid();
683 }
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000684}
685
Anders Carlsson0139bb92009-04-08 20:47:54 +0000686RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlsson4029ca72009-05-20 00:24:07 +0000687 if (ArgType->isReferenceType())
688 return EmitReferenceBindingToExpr(E, ArgType);
689
Anders Carlsson0139bb92009-04-08 20:47:54 +0000690 return EmitAnyExprToTemp(E);
691}
692
Daniel Dunbar88b53962009-02-02 22:03:45 +0000693RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
694 llvm::Value *Callee,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000695 const CallArgList &CallArgs,
696 const Decl *TargetDecl) {
Mike Stumpf5408fe2009-05-16 07:57:57 +0000697 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000698 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000699
700 // Handle struct-return functions by passing a pointer to the
701 // location that we would like to return into.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000702 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000703 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Chris Lattner5db7ae52009-06-13 00:26:38 +0000704
705
706 // If the call returns a temporary with struct return, create a temporary
707 // alloca to hold the result.
708 if (CGM.ReturnTypeUsesSret(CallInfo))
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000709 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000710
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000711 assert(CallInfo.arg_size() == CallArgs.size() &&
712 "Mismatch between function signature & arguments.");
Daniel Dunbarb225be42009-02-03 05:59:18 +0000713 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000714 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbarb225be42009-02-03 05:59:18 +0000715 I != E; ++I, ++info_it) {
716 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000717 RValue RV = I->first;
Daniel Dunbar56273772008-09-17 00:51:38 +0000718
719 switch (ArgInfo.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000720 case ABIArgInfo::Indirect:
Daniel Dunbar1f745982009-02-05 09:16:39 +0000721 if (RV.isScalar() || RV.isComplex()) {
722 // Make a temporary alloca to pass the argument.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000723 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar1f745982009-02-05 09:16:39 +0000724 if (RV.isScalar())
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000725 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar1f745982009-02-05 09:16:39 +0000726 else
727 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
728 } else {
729 Args.push_back(RV.getAggregateAddr());
730 }
731 break;
732
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000733 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000734 case ABIArgInfo::Direct:
Daniel Dunbar56273772008-09-17 00:51:38 +0000735 if (RV.isScalar()) {
736 Args.push_back(RV.getScalarVal());
737 } else if (RV.isComplex()) {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000738 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
739 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
740 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
741 Args.push_back(Tmp);
Daniel Dunbar56273772008-09-17 00:51:38 +0000742 } else {
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000743 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar56273772008-09-17 00:51:38 +0000744 }
745 break;
746
Daniel Dunbar11434922009-01-26 21:26:08 +0000747 case ABIArgInfo::Ignore:
748 break;
749
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000750 case ABIArgInfo::Coerce: {
751 // FIXME: Avoid the conversion through memory if possible.
752 llvm::Value *SrcPtr;
753 if (RV.isScalar()) {
Daniel Dunbar5a1be6e2009-02-03 23:04:57 +0000754 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000755 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000756 } else if (RV.isComplex()) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000757 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000758 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
759 } else
760 SrcPtr = RV.getAggregateAddr();
761 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
762 *this));
763 break;
764 }
765
Daniel Dunbar56273772008-09-17 00:51:38 +0000766 case ABIArgInfo::Expand:
767 ExpandTypeToArgs(I->second, RV, Args);
768 break;
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000769 }
770 }
Chris Lattner5db7ae52009-06-13 00:26:38 +0000771
772 // If the callee is a bitcast of a function to a varargs pointer to function
773 // type, check to see if we can remove the bitcast. This handles some cases
774 // with unprototyped functions.
775 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
776 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
777 const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
778 const llvm::FunctionType *CurFT =
779 cast<llvm::FunctionType>(CurPT->getElementType());
780 const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
781
782 if (CE->getOpcode() == llvm::Instruction::BitCast &&
783 ActualFT->getReturnType() == CurFT->getReturnType() &&
Chris Lattnerd6bebbf2009-06-23 01:38:41 +0000784 ActualFT->getNumParams() == CurFT->getNumParams() &&
785 ActualFT->getNumParams() == Args.size()) {
Chris Lattner5db7ae52009-06-13 00:26:38 +0000786 bool ArgsMatch = true;
787 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
788 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
789 ArgsMatch = false;
790 break;
791 }
792
793 // Strip the cast if we can get away with it. This is a nice cleanup,
794 // but also allows us to inline the function at -O0 if it is marked
795 // always_inline.
796 if (ArgsMatch)
797 Callee = CalleeF;
798 }
799 }
800
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000801
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000802 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patel761d7f72008-09-25 21:02:23 +0000803 CodeGen::AttributeListType AttributeList;
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +0000804 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000805 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
806 AttributeList.end());
Daniel Dunbar725ad312009-01-31 02:19:00 +0000807
Daniel Dunbard14151d2009-03-02 04:32:35 +0000808 llvm::CallSite CS;
809 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foadbeaaccd2009-05-21 09:52:38 +0000810 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000811 } else {
812 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbard14151d2009-03-02 04:32:35 +0000813 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000814 Args.data(), Args.data()+Args.size());
Daniel Dunbar9834ffb2009-02-23 17:26:39 +0000815 EmitBlock(Cont);
Daniel Dunbarf4fe0f02009-02-20 18:54:31 +0000816 }
817
Daniel Dunbard14151d2009-03-02 04:32:35 +0000818 CS.setAttributes(Attrs);
Chris Lattner5db7ae52009-06-13 00:26:38 +0000819 if (const llvm::Function *F =
820 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
Daniel Dunbard14151d2009-03-02 04:32:35 +0000821 CS.setCallingConv(F->getCallingConv());
822
823 // If the call doesn't return, finish the basic block and clear the
824 // insertion point; this allows the rest of IRgen to discard
825 // unreachable code.
826 if (CS.doesNotReturn()) {
827 Builder.CreateUnreachable();
828 Builder.ClearInsertionPoint();
829
Mike Stumpf5408fe2009-05-16 07:57:57 +0000830 // FIXME: For now, emit a dummy basic block because expr emitters in
831 // generally are not ready to handle emitting expressions at unreachable
832 // points.
Daniel Dunbard14151d2009-03-02 04:32:35 +0000833 EnsureInsertPoint();
834
835 // Return a reasonable RValue.
836 return GetUndefRValue(RetTy);
837 }
838
839 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner34030842009-03-22 00:32:22 +0000840 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000841 CI->setName("call");
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000842
843 switch (RetAI.getKind()) {
Daniel Dunbar11e383a2009-02-05 08:00:50 +0000844 case ABIArgInfo::Indirect:
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000845 if (RetTy->isAnyComplexType())
Daniel Dunbar56273772008-09-17 00:51:38 +0000846 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner34030842009-03-22 00:32:22 +0000847 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar56273772008-09-17 00:51:38 +0000848 return RValue::getAggregate(Args[0]);
Chris Lattner34030842009-03-22 00:32:22 +0000849 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000850
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000851 case ABIArgInfo::Extend:
Daniel Dunbar46327aa2009-02-03 06:17:37 +0000852 case ABIArgInfo::Direct:
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000853 if (RetTy->isAnyComplexType()) {
854 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
855 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
856 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner34030842009-03-22 00:32:22 +0000857 }
858 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000859 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbar2fbf2f52009-02-05 11:13:54 +0000860 Builder.CreateStore(CI, V);
861 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +0000862 }
863 return RValue::get(CI);
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000864
Daniel Dunbar11434922009-01-26 21:26:08 +0000865 case ABIArgInfo::Ignore:
Daniel Dunbar0bcc5212009-02-03 06:30:17 +0000866 // If we are ignoring an argument that had a result, make sure to
867 // construct the appropriate return value for our caller.
Daniel Dunbar13e81732009-02-05 07:09:07 +0000868 return GetUndefRValue(RetTy);
Daniel Dunbar11434922009-01-26 21:26:08 +0000869
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000870 case ABIArgInfo::Coerce: {
Daniel Dunbar89c9d8e2009-02-03 19:12:28 +0000871 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbaradc8bdd2009-02-10 01:51:39 +0000872 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar54d1ccb2009-01-27 01:36:03 +0000873 CreateCoercedStore(CI, V, *this);
Anders Carlssonad3d6912008-11-25 22:21:48 +0000874 if (RetTy->isAnyComplexType())
875 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner34030842009-03-22 00:32:22 +0000876 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonad3d6912008-11-25 22:21:48 +0000877 return RValue::getAggregate(V);
Chris Lattner34030842009-03-22 00:32:22 +0000878 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar639ffe42008-09-10 07:04:09 +0000879 }
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000880
Daniel Dunbar8951dbd2008-09-11 01:48:57 +0000881 case ABIArgInfo::Expand:
882 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000883 }
Daniel Dunbar2c8e0f32008-09-10 02:41:04 +0000884
885 assert(0 && "Unhandled ABIArgInfo::Kind");
886 return RValue::get(0);
Daniel Dunbar17b708d2008-09-09 23:27:19 +0000887}
Daniel Dunbarb4094ea2009-02-10 20:44:09 +0000888
889/* VarArg handling */
890
891llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
892 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
893}