blob: b46e860b2eff5afa2ce73dbcbd9c8e1536f9c22d [file] [log] [blame]
Daniel Dunbara8f02052008-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 Dunbar3ef2e852008-09-10 00:41:16 +000017#include "CodeGenModule.h"
Daniel Dunbarf98eeff2008-10-13 17:02:26 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000019#include "clang/AST/Decl.h"
Anders Carlsson7a785352009-04-03 22:48:58 +000020#include "clang/AST/DeclCXX.h"
Daniel Dunbara8f02052008-09-08 21:33:45 +000021#include "clang/AST/DeclObjC.h"
Devang Patela3707f12009-06-04 23:32:02 +000022#include "clang/Frontend/CompileOptions.h"
Devang Patel98bfe502008-09-24 01:01:36 +000023#include "llvm/Attributes.h"
Daniel Dunbar90e43452009-03-02 04:32:35 +000024#include "llvm/Support/CallSite.h"
Daniel Dunbar708d8a82009-01-27 01:36:03 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbard283e632009-02-03 01:05:53 +000026
27#include "ABIInfo.h"
28
Daniel Dunbara8f02052008-09-08 21:33:45 +000029using namespace clang;
30using namespace CodeGen;
31
32/***/
33
Daniel Dunbara8f02052008-09-08 21:33:45 +000034// FIXME: Use iterator and sidestep silly type array creation.
35
Daniel Dunbar34bda882009-02-02 23:23:47 +000036const
Douglas Gregor4fa58902009-02-26 23:50:07 +000037CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000038 return getFunctionInfo(FTNP->getResultType(),
39 llvm::SmallVector<QualType, 16>());
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000040}
41
Daniel Dunbar34bda882009-02-02 23:23:47 +000042const
Douglas Gregor4fa58902009-02-26 23:50:07 +000043CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
Daniel Dunbar34bda882009-02-02 23:23:47 +000044 llvm::SmallVector<QualType, 16> ArgTys;
45 // FIXME: Kill copy.
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000046 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000047 ArgTys.push_back(FTP->getArgType(i));
48 return getFunctionInfo(FTP->getResultType(), ArgTys);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +000049}
50
Anders Carlsson7a785352009-04-03 22:48:58 +000051const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
52 llvm::SmallVector<QualType, 16> ArgTys;
Chris Lattnerd36c9d52009-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 Carlsson7a785352009-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 Dunbar34bda882009-02-02 23:23:47 +000063const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
Chris Lattnerd36c9d52009-05-12 20:27:19 +000064 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
Anders Carlsson7a785352009-04-03 22:48:58 +000065 if (MD->isInstance())
66 return getFunctionInfo(MD);
Anders Carlsson7a785352009-04-03 22:48:58 +000067
Daniel Dunbara8f02052008-09-08 21:33:45 +000068 const FunctionType *FTy = FD->getType()->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +000069 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
Daniel Dunbar34bda882009-02-02 23:23:47 +000070 return getFunctionInfo(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +000071 return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
Daniel Dunbara8f02052008-09-08 21:33:45 +000072}
73
Daniel Dunbar34bda882009-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 Lattner9408eb12009-02-20 06:23:21 +000079 for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
Daniel Dunbara8f02052008-09-08 21:33:45 +000080 e = MD->param_end(); i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000081 ArgTys.push_back((*i)->getType());
82 return getFunctionInfo(MD->getResultType(), ArgTys);
Daniel Dunbara8f02052008-09-08 21:33:45 +000083}
84
Daniel Dunbar34bda882009-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 Dunbarebbb8f32009-01-31 02:19:00 +000089 for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
90 i != e; ++i)
Daniel Dunbar34bda882009-02-02 23:23:47 +000091 ArgTys.push_back(i->second);
92 return getFunctionInfo(ResTy, ArgTys);
Daniel Dunbarebbb8f32009-01-31 02:19:00 +000093}
94
Daniel Dunbar34bda882009-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 Dunbar9fc15a82009-02-02 21:43:58 +000099 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
100 i != e; ++i)
Daniel Dunbar34bda882009-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 Dunbardcf19d12009-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 Dunbare92e0ab2009-02-03 05:31:23 +0000116 // Construct the function info.
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000117 FI = new CGFunctionInfo(ResTy, ArgTys);
Daniel Dunbarb944cc92009-02-05 00:00:23 +0000118 FunctionInfos.InsertNode(FI, InsertPos);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000119
120 // Compute ABI information.
Daniel Dunbar749e36b2009-02-03 06:51:18 +0000121 getABIInfo().computeInfo(*FI, getContext());
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000122
Daniel Dunbardcf19d12009-02-03 00:07:12 +0000123 return *FI;
Daniel Dunbar34bda882009-02-02 23:23:47 +0000124}
125
Daniel Dunbare92e0ab2009-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 Dunbar04d35782008-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
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000145 for (RecordDecl::field_iterator i = RD->field_begin(Context),
146 e = RD->field_end(Context); i != e; ++i) {
Daniel Dunbar04d35782008-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();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000170 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
171 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-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();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(getContext()),
198 e = RD->field_end(getContext()); i != e; ++i) {
Daniel Dunbar04d35782008-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 Dunbar84379912009-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 Sandsee6f6f82009-05-09 07:08:47 +0000226 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
227 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
Daniel Dunbar84379912009-02-02 19:06:38 +0000228
Daniel Dunbar77071992009-02-03 05:59:18 +0000229 // If load is legal, just bitcast the src pointer.
Daniel Dunbar93317922009-05-13 18:54:26 +0000230 if (SrcSize >= DstSize) {
Mike Stumpba2cb0e2009-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 Dunbar93317922009-05-13 18:54:26 +0000234 //
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000235 // FIXME: Assert that we aren't truncating non-padding bits when have access
236 // to that information.
Daniel Dunbar84379912009-02-02 19:06:38 +0000237 llvm::Value *Casted =
238 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
Daniel Dunbar3f062382009-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 Dunbar84379912009-02-02 19:06:38 +0000243 } else {
Daniel Dunbar84379912009-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 Dunbar3f062382009-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 Dunbar84379912009-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 Sandsee6f6f82009-05-09 07:08:47 +0000269 uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
270 uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
Daniel Dunbar84379912009-02-02 19:06:38 +0000271
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000272 // If store is legal, just bitcast the src pointer.
Daniel Dunbare563bae2009-06-05 07:58:54 +0000273 if (SrcSize <= DstSize) {
Daniel Dunbar84379912009-02-02 19:06:38 +0000274 llvm::Value *Casted =
275 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
Daniel Dunbar3f062382009-02-07 02:46:03 +0000276 // FIXME: Use better alignment / avoid requiring aligned store.
277 CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
Daniel Dunbar84379912009-02-02 19:06:38 +0000278 } else {
Daniel Dunbar84379912009-02-02 19:06:38 +0000279 // Otherwise do coercion through memory. This is stupid, but
280 // simple.
Daniel Dunbare563bae2009-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 Dunbar84379912009-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 Dunbar3f062382009-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 Dunbar84379912009-02-02 19:06:38 +0000296 }
297}
298
Daniel Dunbar04d35782008-09-17 00:51:38 +0000299/***/
300
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000301bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000302 return FI.getReturnInfo().isIndirect();
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000303}
304
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000305const llvm::FunctionType *
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000306CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000307 std::vector<const llvm::Type*> ArgTys;
308
309 const llvm::Type *ResultType = 0;
310
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000311 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +0000312 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar22e30052008-09-11 01:48:57 +0000313 switch (RetAI.getKind()) {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000314 case ABIArgInfo::Expand:
315 assert(0 && "Invalid ABI kind for return argument");
316
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000317 case ABIArgInfo::Direct:
318 ResultType = ConvertType(RetTy);
319 break;
320
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000321 case ABIArgInfo::Indirect: {
322 assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000323 ResultType = llvm::Type::VoidTy;
Daniel Dunbara9976a22008-09-10 07:00:50 +0000324 const llvm::Type *STy = ConvertType(RetTy);
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000325 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
326 break;
327 }
328
Daniel Dunbar1358b202009-01-26 21:26:08 +0000329 case ABIArgInfo::Ignore:
330 ResultType = llvm::Type::VoidTy;
331 break;
332
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000333 case ABIArgInfo::Coerce:
Daniel Dunbar73d66602008-09-10 07:04:09 +0000334 ResultType = RetAI.getCoerceToType();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000335 break;
336 }
337
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000338 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
339 ie = FI.arg_end(); it != ie; ++it) {
340 const ABIArgInfo &AI = it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000341
342 switch (AI.getKind()) {
Daniel Dunbar1358b202009-01-26 21:26:08 +0000343 case ABIArgInfo::Ignore:
344 break;
345
Daniel Dunbar04d35782008-09-17 00:51:38 +0000346 case ABIArgInfo::Coerce:
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000347 ArgTys.push_back(AI.getCoerceToType());
348 break;
349
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000350 case ABIArgInfo::Indirect: {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000351 // indirect arguments are always on the stack, which is addr space #0.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000352 const llvm::Type *LTy = ConvertTypeForMem(it->type);
353 ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000354 break;
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000355 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000356
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000357 case ABIArgInfo::Direct:
Daniel Dunbar6f56e452009-02-05 09:16:39 +0000358 ArgTys.push_back(ConvertType(it->type));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000359 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000360
361 case ABIArgInfo::Expand:
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000362 GetExpandedTypes(it->type, ArgTys);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000363 break;
364 }
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000365 }
366
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000367 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000368}
369
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000370void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000371 const Decl *TargetDecl,
Devang Patela85a9ef2008-09-25 21:02:23 +0000372 AttributeListType &PAL) {
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000373 unsigned FuncAttrs = 0;
Devang Patel2bb6eb82008-09-26 22:53:57 +0000374 unsigned RetAttrs = 0;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000375
Anton Korobeynikov2431e602009-04-04 00:49:24 +0000376 // FIXME: handle sseregparm someday...
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000377 if (TargetDecl) {
Daniel Dunbar78582862009-04-13 21:08:27 +0000378 if (TargetDecl->hasAttr<NoThrowAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000379 FuncAttrs |= llvm::Attribute::NoUnwind;
Daniel Dunbar78582862009-04-13 21:08:27 +0000380 if (TargetDecl->hasAttr<NoReturnAttr>())
Devang Patela85a9ef2008-09-25 21:02:23 +0000381 FuncAttrs |= llvm::Attribute::NoReturn;
Daniel Dunbar78582862009-04-13 21:08:27 +0000382 if (TargetDecl->hasAttr<ConstAttr>())
Anders Carlssondd6791c2008-10-05 23:32:53 +0000383 FuncAttrs |= llvm::Attribute::ReadNone;
Daniel Dunbar78582862009-04-13 21:08:27 +0000384 else if (TargetDecl->hasAttr<PureAttr>())
Daniel Dunbar521c3a32009-04-10 22:14:52 +0000385 FuncAttrs |= llvm::Attribute::ReadOnly;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000386 }
387
Devang Patela3707f12009-06-04 23:32:02 +0000388 if (CompileOpts.DisableRedZone)
389 FuncAttrs |= llvm::Attribute::NoRedZone;
Devang Patel62c2cca2009-06-05 22:05:48 +0000390 if (CompileOpts.NoImplicitFloat)
391 FuncAttrs |= llvm::Attribute::NoImplicitFloat;
Devang Patela3707f12009-06-04 23:32:02 +0000392
Daniel Dunbar0b37ca82009-02-02 23:43:58 +0000393 QualType RetTy = FI.getReturnType();
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000394 unsigned Index = 1;
Daniel Dunbar77071992009-02-03 05:59:18 +0000395 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000396 switch (RetAI.getKind()) {
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000397 case ABIArgInfo::Direct:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000398 if (RetTy->isPromotableIntegerType()) {
399 if (RetTy->isSignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000400 RetAttrs |= llvm::Attribute::SExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000401 } else if (RetTy->isUnsignedIntegerType()) {
Devang Patel2bb6eb82008-09-26 22:53:57 +0000402 RetAttrs |= llvm::Attribute::ZExt;
Daniel Dunbare126ab12008-09-10 02:41:04 +0000403 }
404 }
405 break;
406
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000407 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +0000408 PAL.push_back(llvm::AttributeWithIndex::get(Index,
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000409 llvm::Attribute::StructRet |
410 llvm::Attribute::NoAlias));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000411 ++Index;
Daniel Dunbar39ea2c12009-03-18 19:51:01 +0000412 // sret disables readnone and readonly
413 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
414 llvm::Attribute::ReadNone);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000415 break;
416
Daniel Dunbar1358b202009-01-26 21:26:08 +0000417 case ABIArgInfo::Ignore:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000418 case ABIArgInfo::Coerce:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000419 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000420
Daniel Dunbar22e30052008-09-11 01:48:57 +0000421 case ABIArgInfo::Expand:
422 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000423 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000424
Devang Patel2bb6eb82008-09-26 22:53:57 +0000425 if (RetAttrs)
426 PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
Anton Korobeynikov2431e602009-04-04 00:49:24 +0000427
428 // FIXME: we need to honour command line settings also...
429 // FIXME: RegParm should be reduced in case of nested functions and/or global
430 // register variable.
431 signed RegParm = 0;
432 if (TargetDecl)
433 if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>())
434 RegParm = RegParmAttr->getNumParams();
435
436 unsigned PointerWidth = getContext().Target.getPointerWidth(0);
Daniel Dunbare92e0ab2009-02-03 05:31:23 +0000437 for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
438 ie = FI.arg_end(); it != ie; ++it) {
439 QualType ParamType = it->type;
440 const ABIArgInfo &AI = it->info;
Devang Patela85a9ef2008-09-25 21:02:23 +0000441 unsigned Attributes = 0;
Anton Korobeynikov2431e602009-04-04 00:49:24 +0000442
Daniel Dunbar22e30052008-09-11 01:48:57 +0000443 switch (AI.getKind()) {
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000444 case ABIArgInfo::Coerce:
445 break;
446
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000447 case ABIArgInfo::Indirect:
Devang Patela85a9ef2008-09-25 21:02:23 +0000448 Attributes |= llvm::Attribute::ByVal;
Anton Korobeynikov2431e602009-04-04 00:49:24 +0000449 Attributes |=
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000450 llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
Daniel Dunbar39ea2c12009-03-18 19:51:01 +0000451 // byval disables readnone and readonly.
452 FuncAttrs &= ~(llvm::Attribute::ReadOnly |
453 llvm::Attribute::ReadNone);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000454 break;
455
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000456 case ABIArgInfo::Direct:
Daniel Dunbar22e30052008-09-11 01:48:57 +0000457 if (ParamType->isPromotableIntegerType()) {
458 if (ParamType->isSignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000459 Attributes |= llvm::Attribute::SExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000460 } else if (ParamType->isUnsignedIntegerType()) {
Devang Patela85a9ef2008-09-25 21:02:23 +0000461 Attributes |= llvm::Attribute::ZExt;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000462 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000463 }
Anton Korobeynikov2431e602009-04-04 00:49:24 +0000464 if (RegParm > 0 &&
465 (ParamType->isIntegerType() || ParamType->isPointerType())) {
466 RegParm -=
467 (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
468 if (RegParm >= 0)
469 Attributes |= llvm::Attribute::InReg;
470 }
471 // FIXME: handle sseregparm someday...
Daniel Dunbar22e30052008-09-11 01:48:57 +0000472 break;
Anton Korobeynikov2431e602009-04-04 00:49:24 +0000473
Daniel Dunbar1358b202009-01-26 21:26:08 +0000474 case ABIArgInfo::Ignore:
475 // Skip increment, no matching LLVM parameter.
476 continue;
477
Daniel Dunbar04d35782008-09-17 00:51:38 +0000478 case ABIArgInfo::Expand: {
479 std::vector<const llvm::Type*> Tys;
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000480 // FIXME: This is rather inefficient. Do we ever actually need to do
481 // anything here? The result should be just reconstructed on the other
482 // side, so extension should be a non-issue.
Daniel Dunbar04d35782008-09-17 00:51:38 +0000483 getTypes().GetExpandedTypes(ParamType, Tys);
484 Index += Tys.size();
485 continue;
486 }
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000487 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000488
Devang Patela85a9ef2008-09-25 21:02:23 +0000489 if (Attributes)
490 PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000491 ++Index;
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000492 }
Devang Patel2bb6eb82008-09-26 22:53:57 +0000493 if (FuncAttrs)
494 PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
Daniel Dunbarbccb0682008-09-10 00:32:18 +0000495}
496
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000497void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
498 llvm::Function *Fn,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000499 const FunctionArgList &Args) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000500 // FIXME: We no longer need the types from FunctionArgList; lift up and
501 // simplify.
Daniel Dunbar5b7ac652009-02-03 06:02:10 +0000502
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000503 // Emit allocs for param decls. Give the LLVM Argument nodes names.
504 llvm::Function::arg_iterator AI = Fn->arg_begin();
505
506 // Name the struct return argument.
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000507 if (CGM.ReturnTypeUsesSret(FI)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000508 AI->setName("agg.result");
509 ++AI;
510 }
Daniel Dunbar77071992009-02-03 05:59:18 +0000511
Daniel Dunbar14c884a2009-02-04 21:17:21 +0000512 assert(FI.arg_size() == Args.size() &&
513 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +0000514 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000515 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
Daniel Dunbar77071992009-02-03 05:59:18 +0000516 i != e; ++i, ++info_it) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000517 const VarDecl *Arg = i->first;
Daniel Dunbar77071992009-02-03 05:59:18 +0000518 QualType Ty = info_it->type;
519 const ABIArgInfo &ArgI = info_it->info;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000520
521 switch (ArgI.getKind()) {
Daniel Dunbar6f56e452009-02-05 09:16:39 +0000522 case ABIArgInfo::Indirect: {
523 llvm::Value* V = AI;
524 if (hasAggregateLLVMType(Ty)) {
525 // Do nothing, aggregates and complex variables are accessed by
526 // reference.
527 } else {
528 // Load scalar value from indirect argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000529 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar6f56e452009-02-05 09:16:39 +0000530 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
531 // This must be a promotion, for something like
532 // "void a(x) short x; {..."
533 V = EmitScalarConversion(V, Ty, Arg->getType());
534 }
535 }
536 EmitParmDecl(*Arg, V);
537 break;
538 }
539
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000540 case ABIArgInfo::Direct: {
Daniel Dunbar22e30052008-09-11 01:48:57 +0000541 assert(AI != Fn->arg_end() && "Argument mismatch!");
542 llvm::Value* V = AI;
Daniel Dunbarcc811502009-02-05 11:13:54 +0000543 if (hasAggregateLLVMType(Ty)) {
544 // Create a temporary alloca to hold the argument; the rest of
545 // codegen expects to access aggregates & complex values by
546 // reference.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000547 V = CreateTempAlloca(ConvertTypeForMem(Ty));
Daniel Dunbarcc811502009-02-05 11:13:54 +0000548 Builder.CreateStore(AI, V);
549 } else {
550 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
551 // This must be a promotion, for something like
552 // "void a(x) short x; {..."
553 V = EmitScalarConversion(V, Ty, Arg->getType());
554 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000555 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000556 EmitParmDecl(*Arg, V);
557 break;
558 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000559
560 case ABIArgInfo::Expand: {
Daniel Dunbar77071992009-02-03 05:59:18 +0000561 // If this structure was expanded into multiple arguments then
Daniel Dunbar04d35782008-09-17 00:51:38 +0000562 // we need to create a temporary and reconstruct it from the
563 // arguments.
Chris Lattner6c5ec622008-11-24 04:00:27 +0000564 std::string Name = Arg->getNameAsString();
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000565 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
Daniel Dunbar04d35782008-09-17 00:51:38 +0000566 (Name + ".addr").c_str());
567 // FIXME: What are the right qualifiers here?
568 llvm::Function::arg_iterator End =
569 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
570 EmitParmDecl(*Arg, Temp);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000571
Daniel Dunbar04d35782008-09-17 00:51:38 +0000572 // Name the arguments used in expansion and increment AI.
573 unsigned Index = 0;
574 for (; AI != End; ++AI, ++Index)
575 AI->setName(Name + "." + llvm::utostr(Index));
576 continue;
577 }
Daniel Dunbar1358b202009-01-26 21:26:08 +0000578
579 case ABIArgInfo::Ignore:
Daniel Dunbar94b4fec2009-02-10 00:06:49 +0000580 // Initialize the local variable appropriately.
581 if (hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000582 EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
Daniel Dunbar94b4fec2009-02-10 00:06:49 +0000583 } else {
584 EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
585 }
586
Daniel Dunbar015bc8e2009-02-03 20:00:13 +0000587 // Skip increment, no matching LLVM parameter.
588 continue;
Daniel Dunbar1358b202009-01-26 21:26:08 +0000589
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000590 case ABIArgInfo::Coerce: {
591 assert(AI != Fn->arg_end() && "Argument mismatch!");
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000592 // FIXME: This is very wasteful; EmitParmDecl is just going to drop the
593 // result in a new alloca anyway, so we could just store into that
594 // directly if we broke the abstraction down more.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000595 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000596 CreateCoercedStore(AI, V, *this);
597 // Match to what EmitParmDecl is expecting for this type.
Daniel Dunbar99473cd2009-02-04 07:22:24 +0000598 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000599 V = EmitLoadOfScalar(V, false, Ty);
Daniel Dunbar99473cd2009-02-04 07:22:24 +0000600 if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
601 // This must be a promotion, for something like
602 // "void a(x) short x; {..."
603 V = EmitScalarConversion(V, Ty, Arg->getType());
604 }
605 }
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000606 EmitParmDecl(*Arg, V);
607 break;
608 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000609 }
Daniel Dunbar04d35782008-09-17 00:51:38 +0000610
611 ++AI;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000612 }
613 assert(AI == Fn->arg_end() && "Argument mismatch!");
614}
615
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000616void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000617 llvm::Value *ReturnValue) {
Daniel Dunbare126ab12008-09-10 02:41:04 +0000618 llvm::Value *RV = 0;
619
620 // Functions with no result always return void.
621 if (ReturnValue) {
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000622 QualType RetTy = FI.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +0000623 const ABIArgInfo &RetAI = FI.getReturnInfo();
Daniel Dunbare126ab12008-09-10 02:41:04 +0000624
625 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000626 case ABIArgInfo::Indirect:
Daniel Dunbar17d35372008-12-18 04:52:14 +0000627 if (RetTy->isAnyComplexType()) {
Daniel Dunbar17d35372008-12-18 04:52:14 +0000628 ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
629 StoreComplexToAddr(RT, CurFn->arg_begin(), false);
630 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
631 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
632 } else {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000633 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
Anders Carlsson05dfa992009-05-19 18:50:41 +0000634 false, RetTy);
Daniel Dunbar17d35372008-12-18 04:52:14 +0000635 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000636 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000637
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000638 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +0000639 // The internal return value temp always will have
640 // pointer-to-return-type type.
Daniel Dunbare126ab12008-09-10 02:41:04 +0000641 RV = Builder.CreateLoad(ReturnValue);
642 break;
643
Daniel Dunbar1358b202009-01-26 21:26:08 +0000644 case ABIArgInfo::Ignore:
645 break;
646
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000647 case ABIArgInfo::Coerce:
Daniel Dunbar708d8a82009-01-27 01:36:03 +0000648 RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
Daniel Dunbar22e30052008-09-11 01:48:57 +0000649 break;
Daniel Dunbar22e30052008-09-11 01:48:57 +0000650
Daniel Dunbar22e30052008-09-11 01:48:57 +0000651 case ABIArgInfo::Expand:
652 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000653 }
654 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000655
656 if (RV) {
657 Builder.CreateRet(RV);
658 } else {
659 Builder.CreateRetVoid();
660 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000661}
662
Anders Carlssond927fa72009-04-08 20:47:54 +0000663RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) {
Anders Carlssonde55abc2009-05-20 00:24:07 +0000664 if (ArgType->isReferenceType())
665 return EmitReferenceBindingToExpr(E, ArgType);
666
Anders Carlssond927fa72009-04-08 20:47:54 +0000667 return EmitAnyExprToTemp(E);
668}
669
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000670RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
671 llvm::Value *Callee,
Daniel Dunbar191eb9e2009-02-20 18:06:48 +0000672 const CallArgList &CallArgs,
673 const Decl *TargetDecl) {
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000674 // FIXME: We no longer need the types from CallArgs; lift up and simplify.
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000675 llvm::SmallVector<llvm::Value*, 16> Args;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000676
677 // Handle struct-return functions by passing a pointer to the
678 // location that we would like to return into.
Daniel Dunbar9fc15a82009-02-02 21:43:58 +0000679 QualType RetTy = CallInfo.getReturnType();
Daniel Dunbar77071992009-02-03 05:59:18 +0000680 const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
Daniel Dunbar32cae462009-02-05 09:24:53 +0000681 if (CGM.ReturnTypeUsesSret(CallInfo)) {
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000682 // Create a temporary alloca to hold the result of the call. :(
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000683 Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000684 }
685
Daniel Dunbar14c884a2009-02-04 21:17:21 +0000686 assert(CallInfo.arg_size() == CallArgs.size() &&
687 "Mismatch between function signature & arguments.");
Daniel Dunbar77071992009-02-03 05:59:18 +0000688 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000689 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
Daniel Dunbar77071992009-02-03 05:59:18 +0000690 I != E; ++I, ++info_it) {
691 const ABIArgInfo &ArgInfo = info_it->info;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000692 RValue RV = I->first;
Daniel Dunbar04d35782008-09-17 00:51:38 +0000693
694 switch (ArgInfo.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000695 case ABIArgInfo::Indirect:
Daniel Dunbar6f56e452009-02-05 09:16:39 +0000696 if (RV.isScalar() || RV.isComplex()) {
697 // Make a temporary alloca to pass the argument.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000698 Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
Daniel Dunbar6f56e452009-02-05 09:16:39 +0000699 if (RV.isScalar())
Anders Carlsson05dfa992009-05-19 18:50:41 +0000700 EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second);
Daniel Dunbar6f56e452009-02-05 09:16:39 +0000701 else
702 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
703 } else {
704 Args.push_back(RV.getAggregateAddr());
705 }
706 break;
707
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000708 case ABIArgInfo::Direct:
Daniel Dunbar04d35782008-09-17 00:51:38 +0000709 if (RV.isScalar()) {
710 Args.push_back(RV.getScalarVal());
711 } else if (RV.isComplex()) {
Daniel Dunbarcc811502009-02-05 11:13:54 +0000712 llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
713 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
714 Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
715 Args.push_back(Tmp);
Daniel Dunbar04d35782008-09-17 00:51:38 +0000716 } else {
Daniel Dunbarcc811502009-02-05 11:13:54 +0000717 Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
Daniel Dunbar04d35782008-09-17 00:51:38 +0000718 }
719 break;
720
Daniel Dunbar1358b202009-01-26 21:26:08 +0000721 case ABIArgInfo::Ignore:
722 break;
723
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000724 case ABIArgInfo::Coerce: {
725 // FIXME: Avoid the conversion through memory if possible.
726 llvm::Value *SrcPtr;
727 if (RV.isScalar()) {
Daniel Dunbar4ce351b2009-02-03 23:04:57 +0000728 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Anders Carlsson05dfa992009-05-19 18:50:41 +0000729 EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second);
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000730 } else if (RV.isComplex()) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000731 SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000732 StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
733 } else
734 SrcPtr = RV.getAggregateAddr();
735 Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
736 *this));
737 break;
738 }
739
Daniel Dunbar04d35782008-09-17 00:51:38 +0000740 case ABIArgInfo::Expand:
741 ExpandTypeToArgs(I->second, RV, Args);
742 break;
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000743 }
744 }
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000745
Daniel Dunbar0a067402009-02-23 17:26:39 +0000746 llvm::BasicBlock *InvokeDest = getInvokeDest();
Devang Patela85a9ef2008-09-25 21:02:23 +0000747 CodeGen::AttributeListType AttributeList;
Daniel Dunbar191eb9e2009-02-20 18:06:48 +0000748 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
Daniel Dunbar0a067402009-02-23 17:26:39 +0000749 llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
750 AttributeList.end());
Daniel Dunbarebbb8f32009-01-31 02:19:00 +0000751
Daniel Dunbar90e43452009-03-02 04:32:35 +0000752 llvm::CallSite CS;
753 if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
Jay Foad9e6bef42009-05-21 09:52:38 +0000754 CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +0000755 } else {
756 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Daniel Dunbar90e43452009-03-02 04:32:35 +0000757 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
Jay Foad9e6bef42009-05-21 09:52:38 +0000758 Args.data(), Args.data()+Args.size());
Daniel Dunbar0a067402009-02-23 17:26:39 +0000759 EmitBlock(Cont);
Daniel Dunbaraf438dc2009-02-20 18:54:31 +0000760 }
761
Daniel Dunbar90e43452009-03-02 04:32:35 +0000762 CS.setAttributes(Attrs);
Edwin Törökafed9682009-05-22 07:25:06 +0000763 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
Daniel Dunbar90e43452009-03-02 04:32:35 +0000764 CS.setCallingConv(F->getCallingConv());
765
766 // If the call doesn't return, finish the basic block and clear the
767 // insertion point; this allows the rest of IRgen to discard
768 // unreachable code.
769 if (CS.doesNotReturn()) {
770 Builder.CreateUnreachable();
771 Builder.ClearInsertionPoint();
772
Mike Stumpba2cb0e2009-05-16 07:57:57 +0000773 // FIXME: For now, emit a dummy basic block because expr emitters in
774 // generally are not ready to handle emitting expressions at unreachable
775 // points.
Daniel Dunbar90e43452009-03-02 04:32:35 +0000776 EnsureInsertPoint();
777
778 // Return a reasonable RValue.
779 return GetUndefRValue(RetTy);
780 }
781
782 llvm::Instruction *CI = CS.getInstruction();
Chris Lattner28466632009-03-22 00:32:22 +0000783 if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy)
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000784 CI->setName("call");
Daniel Dunbare126ab12008-09-10 02:41:04 +0000785
786 switch (RetAI.getKind()) {
Daniel Dunbar88dde9b2009-02-05 08:00:50 +0000787 case ABIArgInfo::Indirect:
Daniel Dunbare126ab12008-09-10 02:41:04 +0000788 if (RetTy->isAnyComplexType())
Daniel Dunbar04d35782008-09-17 00:51:38 +0000789 return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
Chris Lattner28466632009-03-22 00:32:22 +0000790 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Daniel Dunbar04d35782008-09-17 00:51:38 +0000791 return RValue::getAggregate(Args[0]);
Chris Lattner28466632009-03-22 00:32:22 +0000792 return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
Daniel Dunbar22e30052008-09-11 01:48:57 +0000793
Daniel Dunbarb1a60c02009-02-03 06:17:37 +0000794 case ABIArgInfo::Direct:
Daniel Dunbarcc811502009-02-05 11:13:54 +0000795 if (RetTy->isAnyComplexType()) {
796 llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
797 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
798 return RValue::getComplex(std::make_pair(Real, Imag));
Chris Lattner28466632009-03-22 00:32:22 +0000799 }
800 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000801 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
Daniel Dunbarcc811502009-02-05 11:13:54 +0000802 Builder.CreateStore(CI, V);
803 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +0000804 }
805 return RValue::get(CI);
Daniel Dunbare126ab12008-09-10 02:41:04 +0000806
Daniel Dunbar1358b202009-01-26 21:26:08 +0000807 case ABIArgInfo::Ignore:
Daniel Dunbareec02622009-02-03 06:30:17 +0000808 // If we are ignoring an argument that had a result, make sure to
809 // construct the appropriate return value for our caller.
Daniel Dunbar900c85a2009-02-05 07:09:07 +0000810 return GetUndefRValue(RetTy);
Daniel Dunbar1358b202009-01-26 21:26:08 +0000811
Daniel Dunbar73d66602008-09-10 07:04:09 +0000812 case ABIArgInfo::Coerce: {
Daniel Dunbar33fa5812009-02-03 19:12:28 +0000813 // FIXME: Avoid the conversion through memory if possible.
Daniel Dunbar8559b5d2009-02-10 01:51:39 +0000814 llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
Daniel Dunbar708d8a82009-01-27 01:36:03 +0000815 CreateCoercedStore(CI, V, *this);
Anders Carlssonfccf7472008-11-25 22:21:48 +0000816 if (RetTy->isAnyComplexType())
817 return RValue::getComplex(LoadComplexFromAddr(V, false));
Chris Lattner28466632009-03-22 00:32:22 +0000818 if (CodeGenFunction::hasAggregateLLVMType(RetTy))
Anders Carlssonfccf7472008-11-25 22:21:48 +0000819 return RValue::getAggregate(V);
Chris Lattner28466632009-03-22 00:32:22 +0000820 return RValue::get(EmitLoadOfScalar(V, false, RetTy));
Daniel Dunbar73d66602008-09-10 07:04:09 +0000821 }
Daniel Dunbar22e30052008-09-11 01:48:57 +0000822
Daniel Dunbar22e30052008-09-11 01:48:57 +0000823 case ABIArgInfo::Expand:
824 assert(0 && "Invalid ABI kind for return argument");
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000825 }
Daniel Dunbare126ab12008-09-10 02:41:04 +0000826
827 assert(0 && "Unhandled ABIArgInfo::Kind");
828 return RValue::get(0);
Daniel Dunbarfc1a9c42008-09-09 23:27:19 +0000829}
Daniel Dunbar7fbcf9c2009-02-10 20:44:09 +0000830
831/* VarArg handling */
832
833llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
834 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
835}