CGCall.cpp revision 21ca1fdb25c2bb98721e569aacd10e8b684dd51a
1//===--- CGCall.cpp - 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 "CGCXXABI.h"
17#include "ABIInfo.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "clang/Basic/TargetInfo.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/Frontend/CodeGenOptions.h"
25#include "llvm/Attributes.h"
26#include "llvm/Support/CallSite.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/InlineAsm.h"
29#include "llvm/Transforms/Utils/Local.h"
30using namespace clang;
31using namespace CodeGen;
32
33/***/
34
35static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
36  switch (CC) {
37  default: return llvm::CallingConv::C;
38  case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
39  case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
40  case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
41  case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
42  case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
43  // TODO: add support for CC_X86Pascal to llvm
44  }
45}
46
47/// Derives the 'this' type for codegen purposes, i.e. ignoring method
48/// qualification.
49/// FIXME: address space qualification?
50static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
51  QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
52  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
53}
54
55/// Returns the canonical formal type of the given C++ method.
56static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
57  return MD->getType()->getCanonicalTypeUnqualified()
58           .getAs<FunctionProtoType>();
59}
60
61/// Returns the "extra-canonicalized" return type, which discards
62/// qualifiers on the return type.  Codegen doesn't care about them,
63/// and it makes ABI code a little easier to be able to assume that
64/// all parameter and return types are top-level unqualified.
65static CanQualType GetReturnType(QualType RetTy) {
66  return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
67}
68
69const CGFunctionInfo &
70CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
71  return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
72                         llvm::SmallVector<CanQualType, 16>(),
73                         FTNP->getExtInfo());
74}
75
76/// \param Args - contains any initial parameters besides those
77///   in the formal type
78static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
79                                  llvm::SmallVectorImpl<CanQualType> &ArgTys,
80                                             CanQual<FunctionProtoType> FTP) {
81  // FIXME: Kill copy.
82  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
83    ArgTys.push_back(FTP->getArgType(i));
84  CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
85  return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
86}
87
88const CGFunctionInfo &
89CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
90  llvm::SmallVector<CanQualType, 16> ArgTys;
91  return ::getFunctionInfo(*this, ArgTys, FTP);
92}
93
94static CallingConv getCallingConventionForDecl(const Decl *D) {
95  // Set the appropriate calling convention for the Function.
96  if (D->hasAttr<StdCallAttr>())
97    return CC_X86StdCall;
98
99  if (D->hasAttr<FastCallAttr>())
100    return CC_X86FastCall;
101
102  if (D->hasAttr<ThisCallAttr>())
103    return CC_X86ThisCall;
104
105  if (D->hasAttr<PascalAttr>())
106    return CC_X86Pascal;
107
108  if (PcsAttr *PCS = D->getAttr<PcsAttr>())
109    return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
110
111  return CC_C;
112}
113
114const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
115                                                 const FunctionProtoType *FTP) {
116  llvm::SmallVector<CanQualType, 16> ArgTys;
117
118  // Add the 'this' pointer.
119  ArgTys.push_back(GetThisType(Context, RD));
120
121  return ::getFunctionInfo(*this, ArgTys,
122              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
123}
124
125const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
126  llvm::SmallVector<CanQualType, 16> ArgTys;
127
128  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
129  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
130
131  // Add the 'this' pointer unless this is a static method.
132  if (MD->isInstance())
133    ArgTys.push_back(GetThisType(Context, MD->getParent()));
134
135  return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
136}
137
138const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
139                                                    CXXCtorType Type) {
140  llvm::SmallVector<CanQualType, 16> ArgTys;
141  ArgTys.push_back(GetThisType(Context, D->getParent()));
142  CanQualType ResTy = Context.VoidTy;
143
144  TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
145
146  CanQual<FunctionProtoType> FTP = GetFormalType(D);
147
148  // Add the formal parameters.
149  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
150    ArgTys.push_back(FTP->getArgType(i));
151
152  return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
153}
154
155const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
156                                                    CXXDtorType Type) {
157  llvm::SmallVector<CanQualType, 2> ArgTys;
158  ArgTys.push_back(GetThisType(Context, D->getParent()));
159  CanQualType ResTy = Context.VoidTy;
160
161  TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
162
163  CanQual<FunctionProtoType> FTP = GetFormalType(D);
164  assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
165
166  return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
167}
168
169const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
170  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
171    if (MD->isInstance())
172      return getFunctionInfo(MD);
173
174  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
175  assert(isa<FunctionType>(FTy));
176  if (isa<FunctionNoProtoType>(FTy))
177    return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
178  assert(isa<FunctionProtoType>(FTy));
179  return getFunctionInfo(FTy.getAs<FunctionProtoType>());
180}
181
182const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
183  llvm::SmallVector<CanQualType, 16> ArgTys;
184  ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
185  ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
186  // FIXME: Kill copy?
187  for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
188         e = MD->param_end(); i != e; ++i) {
189    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
190  }
191
192  FunctionType::ExtInfo einfo;
193  einfo = einfo.withCallingConv(getCallingConventionForDecl(MD));
194
195  if (getContext().getLangOptions().ObjCAutoRefCount &&
196      MD->hasAttr<NSReturnsRetainedAttr>())
197    einfo = einfo.withProducesResult(true);
198
199  return getFunctionInfo(GetReturnType(MD->getResultType()), ArgTys, einfo);
200}
201
202const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
203  // FIXME: Do we need to handle ObjCMethodDecl?
204  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
205
206  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
207    return getFunctionInfo(CD, GD.getCtorType());
208
209  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
210    return getFunctionInfo(DD, GD.getDtorType());
211
212  return getFunctionInfo(FD);
213}
214
215const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
216                                                    const CallArgList &Args,
217                                            const FunctionType::ExtInfo &Info) {
218  // FIXME: Kill copy.
219  llvm::SmallVector<CanQualType, 16> ArgTys;
220  for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
221       i != e; ++i)
222    ArgTys.push_back(Context.getCanonicalParamType(i->Ty));
223  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
224}
225
226const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
227                                                    const FunctionArgList &Args,
228                                            const FunctionType::ExtInfo &Info) {
229  // FIXME: Kill copy.
230  llvm::SmallVector<CanQualType, 16> ArgTys;
231  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
232       i != e; ++i)
233    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
234  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
235}
236
237const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
238  llvm::SmallVector<CanQualType, 1> args;
239  return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
240}
241
242const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
243                           const llvm::SmallVectorImpl<CanQualType> &ArgTys,
244                                            const FunctionType::ExtInfo &Info) {
245#ifndef NDEBUG
246  for (llvm::SmallVectorImpl<CanQualType>::const_iterator
247         I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
248    assert(I->isCanonicalAsParam());
249#endif
250
251  unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
252
253  // Lookup or create unique function info.
254  llvm::FoldingSetNodeID ID;
255  CGFunctionInfo::Profile(ID, Info, ResTy, ArgTys.begin(), ArgTys.end());
256
257  void *InsertPos = 0;
258  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
259  if (FI)
260    return *FI;
261
262  // Construct the function info.
263  FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getProducesResult(),
264                          Info.getHasRegParm(), Info.getRegParm(), ResTy,
265                          ArgTys.data(), ArgTys.size());
266  FunctionInfos.InsertNode(FI, InsertPos);
267
268  // Compute ABI information.
269  getABIInfo().computeInfo(*FI);
270
271  // Loop over all of the computed argument and return value info.  If any of
272  // them are direct or extend without a specified coerce type, specify the
273  // default now.
274  ABIArgInfo &RetInfo = FI->getReturnInfo();
275  if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
276    RetInfo.setCoerceToType(ConvertType(FI->getReturnType()));
277
278  for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
279       I != E; ++I)
280    if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
281      I->info.setCoerceToType(ConvertType(I->type));
282
283  return *FI;
284}
285
286CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
287                               bool _NoReturn, bool returnsRetained,
288                               bool _HasRegParm, unsigned _RegParm,
289                               CanQualType ResTy,
290                               const CanQualType *ArgTys,
291                               unsigned NumArgTys)
292  : CallingConvention(_CallingConvention),
293    EffectiveCallingConvention(_CallingConvention),
294    NoReturn(_NoReturn), ReturnsRetained(returnsRetained),
295    HasRegParm(_HasRegParm), RegParm(_RegParm)
296{
297  NumArgs = NumArgTys;
298
299  // FIXME: Coallocate with the CGFunctionInfo object.
300  Args = new ArgInfo[1 + NumArgTys];
301  Args[0].type = ResTy;
302  for (unsigned i = 0; i != NumArgTys; ++i)
303    Args[1 + i].type = ArgTys[i];
304}
305
306/***/
307
308void CodeGenTypes::GetExpandedTypes(QualType type,
309                     llvm::SmallVectorImpl<llvm::Type*> &expandedTypes) {
310  const RecordType *RT = type->getAsStructureType();
311  assert(RT && "Can only expand structure types.");
312  const RecordDecl *RD = RT->getDecl();
313  assert(!RD->hasFlexibleArrayMember() &&
314         "Cannot expand structure with flexible array.");
315
316  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
317         i != e; ++i) {
318    const FieldDecl *FD = *i;
319    assert(!FD->isBitField() &&
320           "Cannot expand structure with bit-field members.");
321
322    QualType fieldType = FD->getType();
323    if (fieldType->isRecordType())
324      GetExpandedTypes(fieldType, expandedTypes);
325    else
326      expandedTypes.push_back(ConvertType(fieldType));
327  }
328}
329
330llvm::Function::arg_iterator
331CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
332                                    llvm::Function::arg_iterator AI) {
333  const RecordType *RT = Ty->getAsStructureType();
334  assert(RT && "Can only expand structure types.");
335
336  RecordDecl *RD = RT->getDecl();
337  assert(LV.isSimple() &&
338         "Unexpected non-simple lvalue during struct expansion.");
339  llvm::Value *Addr = LV.getAddress();
340  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
341         i != e; ++i) {
342    FieldDecl *FD = *i;
343    QualType FT = FD->getType();
344
345    // FIXME: What are the right qualifiers here?
346    LValue LV = EmitLValueForField(Addr, FD, 0);
347    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
348      AI = ExpandTypeFromArgs(FT, LV, AI);
349    } else {
350      EmitStoreThroughLValue(RValue::get(AI), LV);
351      ++AI;
352    }
353  }
354
355  return AI;
356}
357
358void
359CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
360                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
361  const RecordType *RT = Ty->getAsStructureType();
362  assert(RT && "Can only expand structure types.");
363
364  RecordDecl *RD = RT->getDecl();
365  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
366  llvm::Value *Addr = RV.getAggregateAddr();
367  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
368         i != e; ++i) {
369    FieldDecl *FD = *i;
370    QualType FT = FD->getType();
371
372    // FIXME: What are the right qualifiers here?
373    LValue LV = EmitLValueForField(Addr, FD, 0);
374    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
375      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
376    } else {
377      RValue RV = EmitLoadOfLValue(LV);
378      assert(RV.isScalar() &&
379             "Unexpected non-scalar rvalue during struct expansion.");
380      Args.push_back(RV.getScalarVal());
381    }
382  }
383}
384
385/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
386/// accessing some number of bytes out of it, try to gep into the struct to get
387/// at its inner goodness.  Dive as deep as possible without entering an element
388/// with an in-memory size smaller than DstSize.
389static llvm::Value *
390EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
391                                   const llvm::StructType *SrcSTy,
392                                   uint64_t DstSize, CodeGenFunction &CGF) {
393  // We can't dive into a zero-element struct.
394  if (SrcSTy->getNumElements() == 0) return SrcPtr;
395
396  const llvm::Type *FirstElt = SrcSTy->getElementType(0);
397
398  // If the first elt is at least as large as what we're looking for, or if the
399  // first element is the same size as the whole struct, we can enter it.
400  uint64_t FirstEltSize =
401    CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
402  if (FirstEltSize < DstSize &&
403      FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
404    return SrcPtr;
405
406  // GEP into the first element.
407  SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
408
409  // If the first element is a struct, recurse.
410  const llvm::Type *SrcTy =
411    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
412  if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
413    return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
414
415  return SrcPtr;
416}
417
418/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
419/// are either integers or pointers.  This does a truncation of the value if it
420/// is too large or a zero extension if it is too small.
421static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
422                                             const llvm::Type *Ty,
423                                             CodeGenFunction &CGF) {
424  if (Val->getType() == Ty)
425    return Val;
426
427  if (isa<llvm::PointerType>(Val->getType())) {
428    // If this is Pointer->Pointer avoid conversion to and from int.
429    if (isa<llvm::PointerType>(Ty))
430      return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
431
432    // Convert the pointer to an integer so we can play with its width.
433    Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
434  }
435
436  const llvm::Type *DestIntTy = Ty;
437  if (isa<llvm::PointerType>(DestIntTy))
438    DestIntTy = CGF.IntPtrTy;
439
440  if (Val->getType() != DestIntTy)
441    Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
442
443  if (isa<llvm::PointerType>(Ty))
444    Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
445  return Val;
446}
447
448
449
450/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
451/// a pointer to an object of type \arg Ty.
452///
453/// This safely handles the case when the src type is smaller than the
454/// destination type; in this situation the values of bits which not
455/// present in the src are undefined.
456static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
457                                      const llvm::Type *Ty,
458                                      CodeGenFunction &CGF) {
459  const llvm::Type *SrcTy =
460    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
461
462  // If SrcTy and Ty are the same, just do a load.
463  if (SrcTy == Ty)
464    return CGF.Builder.CreateLoad(SrcPtr);
465
466  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
467
468  if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
469    SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
470    SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
471  }
472
473  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
474
475  // If the source and destination are integer or pointer types, just do an
476  // extension or truncation to the desired type.
477  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
478      (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
479    llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
480    return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
481  }
482
483  // If load is legal, just bitcast the src pointer.
484  if (SrcSize >= DstSize) {
485    // Generally SrcSize is never greater than DstSize, since this means we are
486    // losing bits. However, this can happen in cases where the structure has
487    // additional padding, for example due to a user specified alignment.
488    //
489    // FIXME: Assert that we aren't truncating non-padding bits when have access
490    // to that information.
491    llvm::Value *Casted =
492      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
493    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
494    // FIXME: Use better alignment / avoid requiring aligned load.
495    Load->setAlignment(1);
496    return Load;
497  }
498
499  // Otherwise do coercion through memory. This is stupid, but
500  // simple.
501  llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
502  llvm::Value *Casted =
503    CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
504  llvm::StoreInst *Store =
505    CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
506  // FIXME: Use better alignment / avoid requiring aligned store.
507  Store->setAlignment(1);
508  return CGF.Builder.CreateLoad(Tmp);
509}
510
511// Function to store a first-class aggregate into memory.  We prefer to
512// store the elements rather than the aggregate to be more friendly to
513// fast-isel.
514// FIXME: Do we need to recurse here?
515static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
516                          llvm::Value *DestPtr, bool DestIsVolatile,
517                          bool LowAlignment) {
518  // Prefer scalar stores to first-class aggregate stores.
519  if (const llvm::StructType *STy =
520        dyn_cast<llvm::StructType>(Val->getType())) {
521    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
522      llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
523      llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
524      llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
525                                                    DestIsVolatile);
526      if (LowAlignment)
527        SI->setAlignment(1);
528    }
529  } else {
530    CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
531  }
532}
533
534/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
535/// where the source and destination may have different types.
536///
537/// This safely handles the case when the src type is larger than the
538/// destination type; the upper bits of the src will be lost.
539static void CreateCoercedStore(llvm::Value *Src,
540                               llvm::Value *DstPtr,
541                               bool DstIsVolatile,
542                               CodeGenFunction &CGF) {
543  const llvm::Type *SrcTy = Src->getType();
544  const llvm::Type *DstTy =
545    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
546  if (SrcTy == DstTy) {
547    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
548    return;
549  }
550
551  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
552
553  if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
554    DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
555    DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
556  }
557
558  // If the source and destination are integer or pointer types, just do an
559  // extension or truncation to the desired type.
560  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
561      (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
562    Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
563    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
564    return;
565  }
566
567  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
568
569  // If store is legal, just bitcast the src pointer.
570  if (SrcSize <= DstSize) {
571    llvm::Value *Casted =
572      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
573    // FIXME: Use better alignment / avoid requiring aligned store.
574    BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
575  } else {
576    // Otherwise do coercion through memory. This is stupid, but
577    // simple.
578
579    // Generally SrcSize is never greater than DstSize, since this means we are
580    // losing bits. However, this can happen in cases where the structure has
581    // additional padding, for example due to a user specified alignment.
582    //
583    // FIXME: Assert that we aren't truncating non-padding bits when have access
584    // to that information.
585    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
586    CGF.Builder.CreateStore(Src, Tmp);
587    llvm::Value *Casted =
588      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
589    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
590    // FIXME: Use better alignment / avoid requiring aligned load.
591    Load->setAlignment(1);
592    CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
593  }
594}
595
596/***/
597
598bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
599  return FI.getReturnInfo().isIndirect();
600}
601
602bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
603  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
604    switch (BT->getKind()) {
605    default:
606      return false;
607    case BuiltinType::Float:
608      return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
609    case BuiltinType::Double:
610      return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
611    case BuiltinType::LongDouble:
612      return getContext().Target.useObjCFPRetForRealType(
613        TargetInfo::LongDouble);
614    }
615  }
616
617  return false;
618}
619
620llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
621  const CGFunctionInfo &FI = getFunctionInfo(GD);
622
623  // For definition purposes, don't consider a K&R function variadic.
624  bool Variadic = false;
625  if (const FunctionProtoType *FPT =
626        cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
627    Variadic = FPT->isVariadic();
628
629  return GetFunctionType(FI, Variadic);
630}
631
632llvm::FunctionType *
633CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic) {
634  llvm::SmallVector<llvm::Type*, 8> argTypes;
635  const llvm::Type *resultType = 0;
636
637  const ABIArgInfo &retAI = FI.getReturnInfo();
638  switch (retAI.getKind()) {
639  case ABIArgInfo::Expand:
640    llvm_unreachable("Invalid ABI kind for return argument");
641
642  case ABIArgInfo::Extend:
643  case ABIArgInfo::Direct:
644    resultType = retAI.getCoerceToType();
645    break;
646
647  case ABIArgInfo::Indirect: {
648    assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
649    resultType = llvm::Type::getVoidTy(getLLVMContext());
650
651    QualType ret = FI.getReturnType();
652    const llvm::Type *ty = ConvertType(ret);
653    unsigned addressSpace = Context.getTargetAddressSpace(ret);
654    argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
655    break;
656  }
657
658  case ABIArgInfo::Ignore:
659    resultType = llvm::Type::getVoidTy(getLLVMContext());
660    break;
661  }
662
663  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
664         ie = FI.arg_end(); it != ie; ++it) {
665    const ABIArgInfo &argAI = it->info;
666
667    switch (argAI.getKind()) {
668    case ABIArgInfo::Ignore:
669      break;
670
671    case ABIArgInfo::Indirect: {
672      // indirect arguments are always on the stack, which is addr space #0.
673      const llvm::Type *LTy = ConvertTypeForMem(it->type);
674      argTypes.push_back(LTy->getPointerTo());
675      break;
676    }
677
678    case ABIArgInfo::Extend:
679    case ABIArgInfo::Direct: {
680      // If the coerce-to type is a first class aggregate, flatten it.  Either
681      // way is semantically identical, but fast-isel and the optimizer
682      // generally likes scalar values better than FCAs.
683      llvm::Type *argType = argAI.getCoerceToType();
684      if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
685        for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
686          argTypes.push_back(st->getElementType(i));
687      } else {
688        argTypes.push_back(argType);
689      }
690      break;
691    }
692
693    case ABIArgInfo::Expand:
694      GetExpandedTypes(it->type, argTypes);
695      break;
696    }
697  }
698
699  return llvm::FunctionType::get(resultType, argTypes, isVariadic);
700}
701
702const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
703  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
704  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
705
706  if (!isFuncTypeConvertible(FPT))
707    return llvm::StructType::get(getLLVMContext());
708
709  const CGFunctionInfo *Info;
710  if (isa<CXXDestructorDecl>(MD))
711    Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
712  else
713    Info = &getFunctionInfo(MD);
714  return GetFunctionType(*Info, FPT->isVariadic());
715}
716
717void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
718                                           const Decl *TargetDecl,
719                                           AttributeListType &PAL,
720                                           unsigned &CallingConv) {
721  unsigned FuncAttrs = 0;
722  unsigned RetAttrs = 0;
723
724  CallingConv = FI.getEffectiveCallingConvention();
725
726  if (FI.isNoReturn())
727    FuncAttrs |= llvm::Attribute::NoReturn;
728
729  // FIXME: handle sseregparm someday...
730  if (TargetDecl) {
731    if (TargetDecl->hasAttr<NoThrowAttr>())
732      FuncAttrs |= llvm::Attribute::NoUnwind;
733    else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
734      const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
735      if (FPT && FPT->isNothrow(getContext()))
736        FuncAttrs |= llvm::Attribute::NoUnwind;
737    }
738
739    if (TargetDecl->hasAttr<NoReturnAttr>())
740      FuncAttrs |= llvm::Attribute::NoReturn;
741    if (TargetDecl->hasAttr<ConstAttr>())
742      FuncAttrs |= llvm::Attribute::ReadNone;
743    else if (TargetDecl->hasAttr<PureAttr>())
744      FuncAttrs |= llvm::Attribute::ReadOnly;
745    if (TargetDecl->hasAttr<MallocAttr>())
746      RetAttrs |= llvm::Attribute::NoAlias;
747  }
748
749  if (CodeGenOpts.OptimizeSize)
750    FuncAttrs |= llvm::Attribute::OptimizeForSize;
751  if (CodeGenOpts.DisableRedZone)
752    FuncAttrs |= llvm::Attribute::NoRedZone;
753  if (CodeGenOpts.NoImplicitFloat)
754    FuncAttrs |= llvm::Attribute::NoImplicitFloat;
755
756  QualType RetTy = FI.getReturnType();
757  unsigned Index = 1;
758  const ABIArgInfo &RetAI = FI.getReturnInfo();
759  switch (RetAI.getKind()) {
760  case ABIArgInfo::Extend:
761   if (RetTy->hasSignedIntegerRepresentation())
762     RetAttrs |= llvm::Attribute::SExt;
763   else if (RetTy->hasUnsignedIntegerRepresentation())
764     RetAttrs |= llvm::Attribute::ZExt;
765    break;
766  case ABIArgInfo::Direct:
767  case ABIArgInfo::Ignore:
768    break;
769
770  case ABIArgInfo::Indirect:
771    PAL.push_back(llvm::AttributeWithIndex::get(Index,
772                                                llvm::Attribute::StructRet));
773    ++Index;
774    // sret disables readnone and readonly
775    FuncAttrs &= ~(llvm::Attribute::ReadOnly |
776                   llvm::Attribute::ReadNone);
777    break;
778
779  case ABIArgInfo::Expand:
780    assert(0 && "Invalid ABI kind for return argument");
781  }
782
783  if (RetAttrs)
784    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
785
786  // FIXME: RegParm should be reduced in case of global register variable.
787  signed RegParm;
788  if (FI.getHasRegParm())
789    RegParm = FI.getRegParm();
790  else
791    RegParm = CodeGenOpts.NumRegisterParameters;
792
793  unsigned PointerWidth = getContext().Target.getPointerWidth(0);
794  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
795         ie = FI.arg_end(); it != ie; ++it) {
796    QualType ParamType = it->type;
797    const ABIArgInfo &AI = it->info;
798    unsigned Attributes = 0;
799
800    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
801    // have the corresponding parameter variable.  It doesn't make
802    // sense to do it here because parameters are so messed up.
803    switch (AI.getKind()) {
804    case ABIArgInfo::Extend:
805      if (ParamType->isSignedIntegerOrEnumerationType())
806        Attributes |= llvm::Attribute::SExt;
807      else if (ParamType->isUnsignedIntegerOrEnumerationType())
808        Attributes |= llvm::Attribute::ZExt;
809      // FALL THROUGH
810    case ABIArgInfo::Direct:
811      if (RegParm > 0 &&
812          (ParamType->isIntegerType() || ParamType->isPointerType())) {
813        RegParm -=
814        (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
815        if (RegParm >= 0)
816          Attributes |= llvm::Attribute::InReg;
817      }
818      // FIXME: handle sseregparm someday...
819
820      if (const llvm::StructType *STy =
821            dyn_cast<llvm::StructType>(AI.getCoerceToType()))
822        Index += STy->getNumElements()-1;  // 1 will be added below.
823      break;
824
825    case ABIArgInfo::Indirect:
826      if (AI.getIndirectByVal())
827        Attributes |= llvm::Attribute::ByVal;
828
829      Attributes |=
830        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
831      // byval disables readnone and readonly.
832      FuncAttrs &= ~(llvm::Attribute::ReadOnly |
833                     llvm::Attribute::ReadNone);
834      break;
835
836    case ABIArgInfo::Ignore:
837      // Skip increment, no matching LLVM parameter.
838      continue;
839
840    case ABIArgInfo::Expand: {
841      llvm::SmallVector<llvm::Type*, 8> types;
842      // FIXME: This is rather inefficient. Do we ever actually need to do
843      // anything here? The result should be just reconstructed on the other
844      // side, so extension should be a non-issue.
845      getTypes().GetExpandedTypes(ParamType, types);
846      Index += types.size();
847      continue;
848    }
849    }
850
851    if (Attributes)
852      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
853    ++Index;
854  }
855  if (FuncAttrs)
856    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
857}
858
859/// An argument came in as a promoted argument; demote it back to its
860/// declared type.
861static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
862                                         const VarDecl *var,
863                                         llvm::Value *value) {
864  const llvm::Type *varType = CGF.ConvertType(var->getType());
865
866  // This can happen with promotions that actually don't change the
867  // underlying type, like the enum promotions.
868  if (value->getType() == varType) return value;
869
870  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
871         && "unexpected promotion type");
872
873  if (isa<llvm::IntegerType>(varType))
874    return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
875
876  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
877}
878
879void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
880                                         llvm::Function *Fn,
881                                         const FunctionArgList &Args) {
882  // If this is an implicit-return-zero function, go ahead and
883  // initialize the return value.  TODO: it might be nice to have
884  // a more general mechanism for this that didn't require synthesized
885  // return statements.
886  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
887    if (FD->hasImplicitReturnZero()) {
888      QualType RetTy = FD->getResultType().getUnqualifiedType();
889      const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
890      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
891      Builder.CreateStore(Zero, ReturnValue);
892    }
893  }
894
895  // FIXME: We no longer need the types from FunctionArgList; lift up and
896  // simplify.
897
898  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
899  llvm::Function::arg_iterator AI = Fn->arg_begin();
900
901  // Name the struct return argument.
902  if (CGM.ReturnTypeUsesSRet(FI)) {
903    AI->setName("agg.result");
904    ++AI;
905  }
906
907  assert(FI.arg_size() == Args.size() &&
908         "Mismatch between function signature & arguments.");
909  unsigned ArgNo = 1;
910  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
911  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
912       i != e; ++i, ++info_it, ++ArgNo) {
913    const VarDecl *Arg = *i;
914    QualType Ty = info_it->type;
915    const ABIArgInfo &ArgI = info_it->info;
916
917    bool isPromoted =
918      isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
919
920    switch (ArgI.getKind()) {
921    case ABIArgInfo::Indirect: {
922      llvm::Value *V = AI;
923
924      if (hasAggregateLLVMType(Ty)) {
925        // Aggregates and complex variables are accessed by reference.  All we
926        // need to do is realign the value, if requested
927        if (ArgI.getIndirectRealign()) {
928          llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
929
930          // Copy from the incoming argument pointer to the temporary with the
931          // appropriate alignment.
932          //
933          // FIXME: We should have a common utility for generating an aggregate
934          // copy.
935          const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
936          CharUnits Size = getContext().getTypeSizeInChars(Ty);
937          llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
938          llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
939          Builder.CreateMemCpy(Dst,
940                               Src,
941                               llvm::ConstantInt::get(IntPtrTy,
942                                                      Size.getQuantity()),
943                               ArgI.getIndirectAlign(),
944                               false);
945          V = AlignedTemp;
946        }
947      } else {
948        // Load scalar value from indirect argument.
949        CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
950        V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
951
952        if (isPromoted)
953          V = emitArgumentDemotion(*this, Arg, V);
954      }
955      EmitParmDecl(*Arg, V, ArgNo);
956      break;
957    }
958
959    case ABIArgInfo::Extend:
960    case ABIArgInfo::Direct: {
961      // If we have the trivial case, handle it with no muss and fuss.
962      if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
963          ArgI.getCoerceToType() == ConvertType(Ty) &&
964          ArgI.getDirectOffset() == 0) {
965        assert(AI != Fn->arg_end() && "Argument mismatch!");
966        llvm::Value *V = AI;
967
968        if (Arg->getType().isRestrictQualified())
969          AI->addAttr(llvm::Attribute::NoAlias);
970
971        if (isPromoted)
972          V = emitArgumentDemotion(*this, Arg, V);
973
974        EmitParmDecl(*Arg, V, ArgNo);
975        break;
976      }
977
978      llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
979
980      // The alignment we need to use is the max of the requested alignment for
981      // the argument plus the alignment required by our access code below.
982      unsigned AlignmentToUse =
983        CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
984      AlignmentToUse = std::max(AlignmentToUse,
985                        (unsigned)getContext().getDeclAlign(Arg).getQuantity());
986
987      Alloca->setAlignment(AlignmentToUse);
988      llvm::Value *V = Alloca;
989      llvm::Value *Ptr = V;    // Pointer to store into.
990
991      // If the value is offset in memory, apply the offset now.
992      if (unsigned Offs = ArgI.getDirectOffset()) {
993        Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
994        Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
995        Ptr = Builder.CreateBitCast(Ptr,
996                          llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
997      }
998
999      // If the coerce-to type is a first class aggregate, we flatten it and
1000      // pass the elements. Either way is semantically identical, but fast-isel
1001      // and the optimizer generally likes scalar values better than FCAs.
1002      if (const llvm::StructType *STy =
1003            dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
1004        Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
1005
1006        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1007          assert(AI != Fn->arg_end() && "Argument mismatch!");
1008          AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
1009          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1010          Builder.CreateStore(AI++, EltPtr);
1011        }
1012      } else {
1013        // Simple case, just do a coerced store of the argument into the alloca.
1014        assert(AI != Fn->arg_end() && "Argument mismatch!");
1015        AI->setName(Arg->getName() + ".coerce");
1016        CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
1017      }
1018
1019
1020      // Match to what EmitParmDecl is expecting for this type.
1021      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1022        V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1023        if (isPromoted)
1024          V = emitArgumentDemotion(*this, Arg, V);
1025      }
1026      EmitParmDecl(*Arg, V, ArgNo);
1027      continue;  // Skip ++AI increment, already done.
1028    }
1029
1030    case ABIArgInfo::Expand: {
1031      // If this structure was expanded into multiple arguments then
1032      // we need to create a temporary and reconstruct it from the
1033      // arguments.
1034      llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
1035      llvm::Function::arg_iterator End =
1036        ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
1037      EmitParmDecl(*Arg, Temp, ArgNo);
1038
1039      // Name the arguments used in expansion and increment AI.
1040      unsigned Index = 0;
1041      for (; AI != End; ++AI, ++Index)
1042        AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1043      continue;
1044    }
1045
1046    case ABIArgInfo::Ignore:
1047      // Initialize the local variable appropriately.
1048      if (hasAggregateLLVMType(Ty))
1049        EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1050      else
1051        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1052                     ArgNo);
1053
1054      // Skip increment, no matching LLVM parameter.
1055      continue;
1056    }
1057
1058    ++AI;
1059  }
1060  assert(AI == Fn->arg_end() && "Argument mismatch!");
1061}
1062
1063/// Try to emit a fused autorelease of a return result.
1064static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1065                                                    llvm::Value *result) {
1066  // We must be immediately followed the cast.
1067  llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1068  if (BB->empty()) return 0;
1069  if (&BB->back() != result) return 0;
1070
1071  const llvm::Type *resultType = result->getType();
1072
1073  // result is in a BasicBlock and is therefore an Instruction.
1074  llvm::Instruction *generator = cast<llvm::Instruction>(result);
1075
1076  llvm::SmallVector<llvm::Instruction*,4> insnsToKill;
1077
1078  // Look for:
1079  //  %generator = bitcast %type1* %generator2 to %type2*
1080  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1081    // We would have emitted this as a constant if the operand weren't
1082    // an Instruction.
1083    generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1084
1085    // Require the generator to be immediately followed by the cast.
1086    if (generator->getNextNode() != bitcast)
1087      return 0;
1088
1089    insnsToKill.push_back(bitcast);
1090  }
1091
1092  // Look for:
1093  //   %generator = call i8* @objc_retain(i8* %originalResult)
1094  // or
1095  //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1096  llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1097  if (!call) return 0;
1098
1099  bool doRetainAutorelease;
1100
1101  if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1102    doRetainAutorelease = true;
1103  } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1104                                          .objc_retainAutoreleasedReturnValue) {
1105    doRetainAutorelease = false;
1106
1107    // Look for an inline asm immediately preceding the call and kill it, too.
1108    llvm::Instruction *prev = call->getPrevNode();
1109    if (llvm::CallInst *asmCall = dyn_cast_or_null<llvm::CallInst>(prev))
1110      if (asmCall->getCalledValue()
1111            == CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker)
1112        insnsToKill.push_back(prev);
1113  } else {
1114    return 0;
1115  }
1116
1117  result = call->getArgOperand(0);
1118  insnsToKill.push_back(call);
1119
1120  // Keep killing bitcasts, for sanity.  Note that we no longer care
1121  // about precise ordering as long as there's exactly one use.
1122  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
1123    if (!bitcast->hasOneUse()) break;
1124    insnsToKill.push_back(bitcast);
1125    result = bitcast->getOperand(0);
1126  }
1127
1128  // Delete all the unnecessary instructions, from latest to earliest.
1129  for (llvm::SmallVectorImpl<llvm::Instruction*>::iterator
1130         i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
1131    (*i)->eraseFromParent();
1132
1133  // Do the fused retain/autorelease if we were asked to.
1134  if (doRetainAutorelease)
1135    result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
1136
1137  // Cast back to the result type.
1138  return CGF.Builder.CreateBitCast(result, resultType);
1139}
1140
1141/// Emit an ARC autorelease of the result of a function.
1142static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
1143                                            llvm::Value *result) {
1144  // At -O0, try to emit a fused retain/autorelease.
1145  if (CGF.shouldUseFusedARCCalls())
1146    if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
1147      return fused;
1148
1149  return CGF.EmitARCAutoreleaseReturnValue(result);
1150}
1151
1152void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
1153  // Functions with no result always return void.
1154  if (ReturnValue == 0) {
1155    Builder.CreateRetVoid();
1156    return;
1157  }
1158
1159  llvm::DebugLoc RetDbgLoc;
1160  llvm::Value *RV = 0;
1161  QualType RetTy = FI.getReturnType();
1162  const ABIArgInfo &RetAI = FI.getReturnInfo();
1163
1164  switch (RetAI.getKind()) {
1165  case ABIArgInfo::Indirect: {
1166    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1167    if (RetTy->isAnyComplexType()) {
1168      ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1169      StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1170    } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1171      // Do nothing; aggregrates get evaluated directly into the destination.
1172    } else {
1173      EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1174                        false, Alignment, RetTy);
1175    }
1176    break;
1177  }
1178
1179  case ABIArgInfo::Extend:
1180  case ABIArgInfo::Direct:
1181    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1182        RetAI.getDirectOffset() == 0) {
1183      // The internal return value temp always will have pointer-to-return-type
1184      // type, just do a load.
1185
1186      // If the instruction right before the insertion point is a store to the
1187      // return value, we can elide the load, zap the store, and usually zap the
1188      // alloca.
1189      llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1190      llvm::StoreInst *SI = 0;
1191      if (InsertBB->empty() ||
1192          !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1193          SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1194        RV = Builder.CreateLoad(ReturnValue);
1195      } else {
1196        // Get the stored value and nuke the now-dead store.
1197        RetDbgLoc = SI->getDebugLoc();
1198        RV = SI->getValueOperand();
1199        SI->eraseFromParent();
1200
1201        // If that was the only use of the return value, nuke it as well now.
1202        if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1203          cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1204          ReturnValue = 0;
1205        }
1206      }
1207    } else {
1208      llvm::Value *V = ReturnValue;
1209      // If the value is offset in memory, apply the offset now.
1210      if (unsigned Offs = RetAI.getDirectOffset()) {
1211        V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1212        V = Builder.CreateConstGEP1_32(V, Offs);
1213        V = Builder.CreateBitCast(V,
1214                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1215      }
1216
1217      RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
1218    }
1219
1220    // In ARC, end functions that return a retainable type with a call
1221    // to objc_autoreleaseReturnValue.
1222    if (AutoreleaseResult) {
1223      assert(getLangOptions().ObjCAutoRefCount &&
1224             !FI.isReturnsRetained() &&
1225             RetTy->isObjCRetainableType());
1226      RV = emitAutoreleaseOfResult(*this, RV);
1227    }
1228
1229    break;
1230
1231  case ABIArgInfo::Ignore:
1232    break;
1233
1234  case ABIArgInfo::Expand:
1235    assert(0 && "Invalid ABI kind for return argument");
1236  }
1237
1238  llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1239  if (!RetDbgLoc.isUnknown())
1240    Ret->setDebugLoc(RetDbgLoc);
1241}
1242
1243void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1244                                          const VarDecl *param) {
1245  // StartFunction converted the ABI-lowered parameter(s) into a
1246  // local alloca.  We need to turn that into an r-value suitable
1247  // for EmitCall.
1248  llvm::Value *local = GetAddrOfLocalVar(param);
1249
1250  QualType type = param->getType();
1251
1252  // For the most part, we just need to load the alloca, except:
1253  // 1) aggregate r-values are actually pointers to temporaries, and
1254  // 2) references to aggregates are pointers directly to the aggregate.
1255  // I don't know why references to non-aggregates are different here.
1256  if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1257    if (hasAggregateLLVMType(ref->getPointeeType()))
1258      return args.add(RValue::getAggregate(local), type);
1259
1260    // Locals which are references to scalars are represented
1261    // with allocas holding the pointer.
1262    return args.add(RValue::get(Builder.CreateLoad(local)), type);
1263  }
1264
1265  if (type->isAnyComplexType()) {
1266    ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1267    return args.add(RValue::getComplex(complex), type);
1268  }
1269
1270  if (hasAggregateLLVMType(type))
1271    return args.add(RValue::getAggregate(local), type);
1272
1273  unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1274  llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1275  return args.add(RValue::get(value), type);
1276}
1277
1278static bool isProvablyNull(llvm::Value *addr) {
1279  return isa<llvm::ConstantPointerNull>(addr);
1280}
1281
1282static bool isProvablyNonNull(llvm::Value *addr) {
1283  return isa<llvm::AllocaInst>(addr);
1284}
1285
1286/// Emit the actual writing-back of a writeback.
1287static void emitWriteback(CodeGenFunction &CGF,
1288                          const CallArgList::Writeback &writeback) {
1289  llvm::Value *srcAddr = writeback.Address;
1290  assert(!isProvablyNull(srcAddr) &&
1291         "shouldn't have writeback for provably null argument");
1292
1293  llvm::BasicBlock *contBB = 0;
1294
1295  // If the argument wasn't provably non-null, we need to null check
1296  // before doing the store.
1297  bool provablyNonNull = isProvablyNonNull(srcAddr);
1298  if (!provablyNonNull) {
1299    llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
1300    contBB = CGF.createBasicBlock("icr.done");
1301
1302    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1303    CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
1304    CGF.EmitBlock(writebackBB);
1305  }
1306
1307  // Load the value to writeback.
1308  llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
1309
1310  // Cast it back, in case we're writing an id to a Foo* or something.
1311  value = CGF.Builder.CreateBitCast(value,
1312               cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
1313                            "icr.writeback-cast");
1314
1315  // Perform the writeback.
1316  QualType srcAddrType = writeback.AddressType;
1317  CGF.EmitStoreThroughLValue(RValue::get(value),
1318                             CGF.MakeAddrLValue(srcAddr, srcAddrType));
1319
1320  // Jump to the continuation block.
1321  if (!provablyNonNull)
1322    CGF.EmitBlock(contBB);
1323}
1324
1325static void emitWritebacks(CodeGenFunction &CGF,
1326                           const CallArgList &args) {
1327  for (CallArgList::writeback_iterator
1328         i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i)
1329    emitWriteback(CGF, *i);
1330}
1331
1332/// Emit an argument that's being passed call-by-writeback.  That is,
1333/// we are passing the address of
1334static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
1335                             const ObjCIndirectCopyRestoreExpr *CRE) {
1336  llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
1337
1338  // The dest and src types don't necessarily match in LLVM terms
1339  // because of the crazy ObjC compatibility rules.
1340
1341  const llvm::PointerType *destType =
1342    cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
1343
1344  // If the address is a constant null, just pass the appropriate null.
1345  if (isProvablyNull(srcAddr)) {
1346    args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
1347             CRE->getType());
1348    return;
1349  }
1350
1351  QualType srcAddrType =
1352    CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
1353
1354  // Create the temporary.
1355  llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
1356                                           "icr.temp");
1357
1358  // Zero-initialize it if we're not doing a copy-initialization.
1359  bool shouldCopy = CRE->shouldCopy();
1360  if (!shouldCopy) {
1361    llvm::Value *null =
1362      llvm::ConstantPointerNull::get(
1363        cast<llvm::PointerType>(destType->getElementType()));
1364    CGF.Builder.CreateStore(null, temp);
1365  }
1366
1367  llvm::BasicBlock *contBB = 0;
1368
1369  // If the address is *not* known to be non-null, we need to switch.
1370  llvm::Value *finalArgument;
1371
1372  bool provablyNonNull = isProvablyNonNull(srcAddr);
1373  if (provablyNonNull) {
1374    finalArgument = temp;
1375  } else {
1376    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1377
1378    finalArgument = CGF.Builder.CreateSelect(isNull,
1379                                   llvm::ConstantPointerNull::get(destType),
1380                                             temp, "icr.argument");
1381
1382    // If we need to copy, then the load has to be conditional, which
1383    // means we need control flow.
1384    if (shouldCopy) {
1385      contBB = CGF.createBasicBlock("icr.cont");
1386      llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
1387      CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
1388      CGF.EmitBlock(copyBB);
1389    }
1390  }
1391
1392  // Perform a copy if necessary.
1393  if (shouldCopy) {
1394    LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
1395    RValue srcRV = CGF.EmitLoadOfLValue(srcLV);
1396    assert(srcRV.isScalar());
1397
1398    llvm::Value *src = srcRV.getScalarVal();
1399    src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
1400                                    "icr.cast");
1401
1402    // Use an ordinary store, not a store-to-lvalue.
1403    CGF.Builder.CreateStore(src, temp);
1404  }
1405
1406  // Finish the control flow if we needed it.
1407  if (shouldCopy && !provablyNonNull)
1408    CGF.EmitBlock(contBB);
1409
1410  args.addWriteback(srcAddr, srcAddrType, temp);
1411  args.add(RValue::get(finalArgument), CRE->getType());
1412}
1413
1414void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1415                                  QualType type) {
1416  if (const ObjCIndirectCopyRestoreExpr *CRE
1417        = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
1418    assert(getContext().getLangOptions().ObjCAutoRefCount);
1419    assert(getContext().hasSameType(E->getType(), type));
1420    return emitWritebackArg(*this, args, CRE);
1421  }
1422
1423  if (type->isReferenceType())
1424    return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1425                    type);
1426
1427  if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() &&
1428      isa<ImplicitCastExpr>(E) &&
1429      cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
1430    LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
1431    assert(L.isSimple());
1432    args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()),
1433             type, /*NeedsCopy*/true);
1434    return;
1435  }
1436
1437  args.add(EmitAnyExprToTemp(E), type);
1438}
1439
1440/// Emits a call or invoke instruction to the given function, depending
1441/// on the current state of the EH stack.
1442llvm::CallSite
1443CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1444                                  llvm::Value * const *ArgBegin,
1445                                  llvm::Value * const *ArgEnd,
1446                                  const llvm::Twine &Name) {
1447  llvm::BasicBlock *InvokeDest = getInvokeDest();
1448  if (!InvokeDest)
1449    return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1450
1451  llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1452  llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1453                                                  ArgBegin, ArgEnd, Name);
1454  EmitBlock(ContBB);
1455  return Invoke;
1456}
1457
1458static void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo,
1459                            llvm::FunctionType *FTy) {
1460  if (ArgNo < FTy->getNumParams())
1461    assert(Elt->getType() == FTy->getParamType(ArgNo));
1462  else
1463    assert(FTy->isVarArg());
1464  ++ArgNo;
1465}
1466
1467RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1468                                 llvm::Value *Callee,
1469                                 ReturnValueSlot ReturnValue,
1470                                 const CallArgList &CallArgs,
1471                                 const Decl *TargetDecl,
1472                                 llvm::Instruction **callOrInvoke) {
1473  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
1474  llvm::SmallVector<llvm::Value*, 16> Args;
1475
1476  // Handle struct-return functions by passing a pointer to the
1477  // location that we would like to return into.
1478  QualType RetTy = CallInfo.getReturnType();
1479  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
1480
1481  // IRArgNo - Keep track of the argument number in the callee we're looking at.
1482  unsigned IRArgNo = 0;
1483  llvm::FunctionType *IRFuncTy =
1484    cast<llvm::FunctionType>(
1485                  cast<llvm::PointerType>(Callee->getType())->getElementType());
1486
1487  // If the call returns a temporary with struct return, create a temporary
1488  // alloca to hold the result, unless one is given to us.
1489  if (CGM.ReturnTypeUsesSRet(CallInfo)) {
1490    llvm::Value *Value = ReturnValue.getValue();
1491    if (!Value)
1492      Value = CreateMemTemp(RetTy);
1493    Args.push_back(Value);
1494    checkArgMatches(Value, IRArgNo, IRFuncTy);
1495  }
1496
1497  assert(CallInfo.arg_size() == CallArgs.size() &&
1498         "Mismatch between function signature & arguments.");
1499  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
1500  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1501       I != E; ++I, ++info_it) {
1502    const ABIArgInfo &ArgInfo = info_it->info;
1503    RValue RV = I->RV;
1504
1505    unsigned TypeAlign =
1506      getContext().getTypeAlignInChars(I->Ty).getQuantity();
1507    switch (ArgInfo.getKind()) {
1508    case ABIArgInfo::Indirect: {
1509      if (RV.isScalar() || RV.isComplex()) {
1510        // Make a temporary alloca to pass the argument.
1511        llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
1512        if (ArgInfo.getIndirectAlign() > AI->getAlignment())
1513          AI->setAlignment(ArgInfo.getIndirectAlign());
1514        Args.push_back(AI);
1515
1516        if (RV.isScalar())
1517          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1518                            TypeAlign, I->Ty);
1519        else
1520          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1521
1522        // Validate argument match.
1523        checkArgMatches(AI, IRArgNo, IRFuncTy);
1524      } else {
1525        // We want to avoid creating an unnecessary temporary+copy here;
1526        // however, we need one in two cases:
1527        // 1. If the argument is not byval, and we are required to copy the
1528        //    source.  (This case doesn't occur on any common architecture.)
1529        // 2. If the argument is byval, RV is not sufficiently aligned, and
1530        //    we cannot force it to be sufficiently aligned.
1531        llvm::Value *Addr = RV.getAggregateAddr();
1532        unsigned Align = ArgInfo.getIndirectAlign();
1533        const llvm::TargetData *TD = &CGM.getTargetData();
1534        if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
1535            (ArgInfo.getIndirectByVal() && TypeAlign < Align &&
1536             llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) {
1537          // Create an aligned temporary, and copy to it.
1538          llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
1539          if (Align > AI->getAlignment())
1540            AI->setAlignment(Align);
1541          Args.push_back(AI);
1542          EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
1543
1544          // Validate argument match.
1545          checkArgMatches(AI, IRArgNo, IRFuncTy);
1546        } else {
1547          // Skip the extra memcpy call.
1548          Args.push_back(Addr);
1549
1550          // Validate argument match.
1551          checkArgMatches(Addr, IRArgNo, IRFuncTy);
1552        }
1553      }
1554      break;
1555    }
1556
1557    case ABIArgInfo::Ignore:
1558      break;
1559
1560    case ABIArgInfo::Extend:
1561    case ABIArgInfo::Direct: {
1562      if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
1563          ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1564          ArgInfo.getDirectOffset() == 0) {
1565        llvm::Value *V;
1566        if (RV.isScalar())
1567          V = RV.getScalarVal();
1568        else
1569          V = Builder.CreateLoad(RV.getAggregateAddr());
1570
1571        // If the argument doesn't match, perform a bitcast to coerce it.  This
1572        // can happen due to trivial type mismatches.
1573        if (IRArgNo < IRFuncTy->getNumParams() &&
1574            V->getType() != IRFuncTy->getParamType(IRArgNo))
1575          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo));
1576        Args.push_back(V);
1577
1578        checkArgMatches(V, IRArgNo, IRFuncTy);
1579        break;
1580      }
1581
1582      // FIXME: Avoid the conversion through memory if possible.
1583      llvm::Value *SrcPtr;
1584      if (RV.isScalar()) {
1585        SrcPtr = CreateMemTemp(I->Ty, "coerce");
1586        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty);
1587      } else if (RV.isComplex()) {
1588        SrcPtr = CreateMemTemp(I->Ty, "coerce");
1589        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1590      } else
1591        SrcPtr = RV.getAggregateAddr();
1592
1593      // If the value is offset in memory, apply the offset now.
1594      if (unsigned Offs = ArgInfo.getDirectOffset()) {
1595        SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1596        SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1597        SrcPtr = Builder.CreateBitCast(SrcPtr,
1598                       llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1599
1600      }
1601
1602      // If the coerce-to type is a first class aggregate, we flatten it and
1603      // pass the elements. Either way is semantically identical, but fast-isel
1604      // and the optimizer generally likes scalar values better than FCAs.
1605      if (const llvm::StructType *STy =
1606            dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
1607        SrcPtr = Builder.CreateBitCast(SrcPtr,
1608                                       llvm::PointerType::getUnqual(STy));
1609        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1610          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1611          llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1612          // We don't know what we're loading from.
1613          LI->setAlignment(1);
1614          Args.push_back(LI);
1615
1616          // Validate argument match.
1617          checkArgMatches(LI, IRArgNo, IRFuncTy);
1618        }
1619      } else {
1620        // In the simple case, just pass the coerced loaded value.
1621        Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1622                                         *this));
1623
1624        // Validate argument match.
1625        checkArgMatches(Args.back(), IRArgNo, IRFuncTy);
1626      }
1627
1628      break;
1629    }
1630
1631    case ABIArgInfo::Expand:
1632      ExpandTypeToArgs(I->Ty, RV, Args);
1633      IRArgNo = Args.size();
1634      break;
1635    }
1636  }
1637
1638  // If the callee is a bitcast of a function to a varargs pointer to function
1639  // type, check to see if we can remove the bitcast.  This handles some cases
1640  // with unprototyped functions.
1641  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1642    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1643      const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1644      const llvm::FunctionType *CurFT =
1645        cast<llvm::FunctionType>(CurPT->getElementType());
1646      const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
1647
1648      if (CE->getOpcode() == llvm::Instruction::BitCast &&
1649          ActualFT->getReturnType() == CurFT->getReturnType() &&
1650          ActualFT->getNumParams() == CurFT->getNumParams() &&
1651          ActualFT->getNumParams() == Args.size() &&
1652          (CurFT->isVarArg() || !ActualFT->isVarArg())) {
1653        bool ArgsMatch = true;
1654        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1655          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1656            ArgsMatch = false;
1657            break;
1658          }
1659
1660        // Strip the cast if we can get away with it.  This is a nice cleanup,
1661        // but also allows us to inline the function at -O0 if it is marked
1662        // always_inline.
1663        if (ArgsMatch)
1664          Callee = CalleeF;
1665      }
1666    }
1667
1668  unsigned CallingConv;
1669  CodeGen::AttributeListType AttributeList;
1670  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
1671  llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1672                                                   AttributeList.end());
1673
1674  llvm::BasicBlock *InvokeDest = 0;
1675  if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1676    InvokeDest = getInvokeDest();
1677
1678  llvm::CallSite CS;
1679  if (!InvokeDest) {
1680    CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
1681  } else {
1682    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1683    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1684                              Args.data(), Args.data()+Args.size());
1685    EmitBlock(Cont);
1686  }
1687  if (callOrInvoke)
1688    *callOrInvoke = CS.getInstruction();
1689
1690  CS.setAttributes(Attrs);
1691  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1692
1693  // If the call doesn't return, finish the basic block and clear the
1694  // insertion point; this allows the rest of IRgen to discard
1695  // unreachable code.
1696  if (CS.doesNotReturn()) {
1697    Builder.CreateUnreachable();
1698    Builder.ClearInsertionPoint();
1699
1700    // FIXME: For now, emit a dummy basic block because expr emitters in
1701    // generally are not ready to handle emitting expressions at unreachable
1702    // points.
1703    EnsureInsertPoint();
1704
1705    // Return a reasonable RValue.
1706    return GetUndefRValue(RetTy);
1707  }
1708
1709  llvm::Instruction *CI = CS.getInstruction();
1710  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
1711    CI->setName("call");
1712
1713  // Emit any writebacks immediately.  Arguably this should happen
1714  // after any return-value munging.
1715  if (CallArgs.hasWritebacks())
1716    emitWritebacks(*this, CallArgs);
1717
1718  switch (RetAI.getKind()) {
1719  case ABIArgInfo::Indirect: {
1720    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1721    if (RetTy->isAnyComplexType())
1722      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
1723    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1724      return RValue::getAggregate(Args[0]);
1725    return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1726  }
1727
1728  case ABIArgInfo::Ignore:
1729    // If we are ignoring an argument that had a result, make sure to
1730    // construct the appropriate return value for our caller.
1731    return GetUndefRValue(RetTy);
1732
1733  case ABIArgInfo::Extend:
1734  case ABIArgInfo::Direct: {
1735    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1736        RetAI.getDirectOffset() == 0) {
1737      if (RetTy->isAnyComplexType()) {
1738        llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1739        llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1740        return RValue::getComplex(std::make_pair(Real, Imag));
1741      }
1742      if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1743        llvm::Value *DestPtr = ReturnValue.getValue();
1744        bool DestIsVolatile = ReturnValue.isVolatile();
1745
1746        if (!DestPtr) {
1747          DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1748          DestIsVolatile = false;
1749        }
1750        BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
1751        return RValue::getAggregate(DestPtr);
1752      }
1753      return RValue::get(CI);
1754    }
1755
1756    llvm::Value *DestPtr = ReturnValue.getValue();
1757    bool DestIsVolatile = ReturnValue.isVolatile();
1758
1759    if (!DestPtr) {
1760      DestPtr = CreateMemTemp(RetTy, "coerce");
1761      DestIsVolatile = false;
1762    }
1763
1764    // If the value is offset in memory, apply the offset now.
1765    llvm::Value *StorePtr = DestPtr;
1766    if (unsigned Offs = RetAI.getDirectOffset()) {
1767      StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1768      StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1769      StorePtr = Builder.CreateBitCast(StorePtr,
1770                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1771    }
1772    CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1773
1774    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1775    if (RetTy->isAnyComplexType())
1776      return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
1777    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1778      return RValue::getAggregate(DestPtr);
1779    return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
1780  }
1781
1782  case ABIArgInfo::Expand:
1783    assert(0 && "Invalid ABI kind for return argument");
1784  }
1785
1786  assert(0 && "Unhandled ABIArgInfo::Kind");
1787  return RValue::get(0);
1788}
1789
1790/* VarArg handling */
1791
1792llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1793  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1794}
1795