CGCall.cpp revision 64aa4b3ec7e62288e2e66c1935487ece995ca94b
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 "ABIInfo.h"
17#include "CGCXXABI.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
20#include "TargetInfo.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/Basic/TargetInfo.h"
25#include "clang/Frontend/CodeGenOptions.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/IR/Attributes.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/InlineAsm.h"
30#include "llvm/MC/SubtargetFeature.h"
31#include "llvm/Support/CallSite.h"
32#include "llvm/Transforms/Utils/Local.h"
33using namespace clang;
34using namespace CodeGen;
35
36/***/
37
38static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
39  switch (CC) {
40  default: return llvm::CallingConv::C;
41  case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
42  case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
43  case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
44  case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
45  case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
46  case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
47  // TODO: add support for CC_X86Pascal to llvm
48  }
49}
50
51/// Derives the 'this' type for codegen purposes, i.e. ignoring method
52/// qualification.
53/// FIXME: address space qualification?
54static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
55  QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
56  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
57}
58
59/// Returns the canonical formal type of the given C++ method.
60static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
61  return MD->getType()->getCanonicalTypeUnqualified()
62           .getAs<FunctionProtoType>();
63}
64
65/// Returns the "extra-canonicalized" return type, which discards
66/// qualifiers on the return type.  Codegen doesn't care about them,
67/// and it makes ABI code a little easier to be able to assume that
68/// all parameter and return types are top-level unqualified.
69static CanQualType GetReturnType(QualType RetTy) {
70  return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
71}
72
73/// Arrange the argument and result information for a value of the given
74/// unprototyped freestanding function type.
75const CGFunctionInfo &
76CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
77  // When translating an unprototyped function type, always use a
78  // variadic type.
79  return arrangeLLVMFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
80                                 ArrayRef<CanQualType>(),
81                                 FTNP->getExtInfo(),
82                                 RequiredArgs(0));
83}
84
85/// Arrange the LLVM function layout for a value of the given function
86/// type, on top of any implicit parameters already stored.  Use the
87/// given ExtInfo instead of the ExtInfo from the function type.
88static const CGFunctionInfo &arrangeLLVMFunctionInfo(CodeGenTypes &CGT,
89                                       SmallVectorImpl<CanQualType> &prefix,
90                                             CanQual<FunctionProtoType> FTP,
91                                              FunctionType::ExtInfo extInfo) {
92  RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
93  // FIXME: Kill copy.
94  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
95    prefix.push_back(FTP->getArgType(i));
96  CanQualType resultType = FTP->getResultType().getUnqualifiedType();
97  return CGT.arrangeLLVMFunctionInfo(resultType, prefix, extInfo, required);
98}
99
100/// Arrange the argument and result information for a free function (i.e.
101/// not a C++ or ObjC instance method) of the given type.
102static const CGFunctionInfo &arrangeFreeFunctionType(CodeGenTypes &CGT,
103                                      SmallVectorImpl<CanQualType> &prefix,
104                                            CanQual<FunctionProtoType> FTP) {
105  return arrangeLLVMFunctionInfo(CGT, prefix, FTP, FTP->getExtInfo());
106}
107
108/// Given the formal ext-info of a C++ instance method, adjust it
109/// according to the C++ ABI in effect.
110static void adjustCXXMethodInfo(CodeGenTypes &CGT,
111                                FunctionType::ExtInfo &extInfo,
112                                bool isVariadic) {
113  if (extInfo.getCC() == CC_Default) {
114    CallingConv CC = CGT.getContext().getDefaultCXXMethodCallConv(isVariadic);
115    extInfo = extInfo.withCallingConv(CC);
116  }
117}
118
119/// Arrange the argument and result information for a free function (i.e.
120/// not a C++ or ObjC instance method) of the given type.
121static const CGFunctionInfo &arrangeCXXMethodType(CodeGenTypes &CGT,
122                                      SmallVectorImpl<CanQualType> &prefix,
123                                            CanQual<FunctionProtoType> FTP) {
124  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
125  adjustCXXMethodInfo(CGT, extInfo, FTP->isVariadic());
126  return arrangeLLVMFunctionInfo(CGT, prefix, FTP, extInfo);
127}
128
129/// Arrange the argument and result information for a value of the
130/// given freestanding function type.
131const CGFunctionInfo &
132CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
133  SmallVector<CanQualType, 16> argTypes;
134  return ::arrangeFreeFunctionType(*this, argTypes, FTP);
135}
136
137static CallingConv getCallingConventionForDecl(const Decl *D) {
138  // Set the appropriate calling convention for the Function.
139  if (D->hasAttr<StdCallAttr>())
140    return CC_X86StdCall;
141
142  if (D->hasAttr<FastCallAttr>())
143    return CC_X86FastCall;
144
145  if (D->hasAttr<ThisCallAttr>())
146    return CC_X86ThisCall;
147
148  if (D->hasAttr<PascalAttr>())
149    return CC_X86Pascal;
150
151  if (PcsAttr *PCS = D->getAttr<PcsAttr>())
152    return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
153
154  if (D->hasAttr<PnaclCallAttr>())
155    return CC_PnaclCall;
156
157  if (D->hasAttr<IntelOclBiccAttr>())
158    return CC_IntelOclBicc;
159
160  return CC_C;
161}
162
163/// Arrange the argument and result information for a call to an
164/// unknown C++ non-static member function of the given abstract type.
165/// The member function must be an ordinary function, i.e. not a
166/// constructor or destructor.
167const CGFunctionInfo &
168CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
169                                   const FunctionProtoType *FTP) {
170  SmallVector<CanQualType, 16> argTypes;
171
172  // Add the 'this' pointer.
173  argTypes.push_back(GetThisType(Context, RD));
174
175  return ::arrangeCXXMethodType(*this, argTypes,
176              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
177}
178
179/// Arrange the argument and result information for a declaration or
180/// definition of the given C++ non-static member function.  The
181/// member function must be an ordinary function, i.e. not a
182/// constructor or destructor.
183const CGFunctionInfo &
184CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
185  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
186  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
187
188  CanQual<FunctionProtoType> prototype = GetFormalType(MD);
189
190  if (MD->isInstance()) {
191    // The abstract case is perfectly fine.
192    return arrangeCXXMethodType(MD->getParent(), prototype.getTypePtr());
193  }
194
195  return arrangeFreeFunctionType(prototype);
196}
197
198/// Arrange the argument and result information for a declaration
199/// or definition to the given constructor variant.
200const CGFunctionInfo &
201CodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D,
202                                               CXXCtorType ctorKind) {
203  SmallVector<CanQualType, 16> argTypes;
204  argTypes.push_back(GetThisType(Context, D->getParent()));
205  CanQualType resultType = Context.VoidTy;
206
207  TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes);
208
209  CanQual<FunctionProtoType> FTP = GetFormalType(D);
210
211  RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, argTypes.size());
212
213  // Add the formal parameters.
214  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
215    argTypes.push_back(FTP->getArgType(i));
216
217  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
218  adjustCXXMethodInfo(*this, extInfo, FTP->isVariadic());
219  return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo, required);
220}
221
222/// Arrange the argument and result information for a declaration,
223/// definition, or call to the given destructor variant.  It so
224/// happens that all three cases produce the same information.
225const CGFunctionInfo &
226CodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D,
227                                   CXXDtorType dtorKind) {
228  SmallVector<CanQualType, 2> argTypes;
229  argTypes.push_back(GetThisType(Context, D->getParent()));
230  CanQualType resultType = Context.VoidTy;
231
232  TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes);
233
234  CanQual<FunctionProtoType> FTP = GetFormalType(D);
235  assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
236  assert(FTP->isVariadic() == 0 && "dtor with formal parameters");
237
238  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
239  adjustCXXMethodInfo(*this, extInfo, false);
240  return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo,
241                                 RequiredArgs::All);
242}
243
244/// Arrange the argument and result information for the declaration or
245/// definition of the given function.
246const CGFunctionInfo &
247CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
248  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
249    if (MD->isInstance())
250      return arrangeCXXMethodDeclaration(MD);
251
252  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
253
254  assert(isa<FunctionType>(FTy));
255
256  // When declaring a function without a prototype, always use a
257  // non-variadic type.
258  if (isa<FunctionNoProtoType>(FTy)) {
259    CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>();
260    return arrangeLLVMFunctionInfo(noProto->getResultType(),
261                                   ArrayRef<CanQualType>(),
262                                   noProto->getExtInfo(),
263                                   RequiredArgs::All);
264  }
265
266  assert(isa<FunctionProtoType>(FTy));
267  return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>());
268}
269
270/// Arrange the argument and result information for the declaration or
271/// definition of an Objective-C method.
272const CGFunctionInfo &
273CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
274  // It happens that this is the same as a call with no optional
275  // arguments, except also using the formal 'self' type.
276  return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
277}
278
279/// Arrange the argument and result information for the function type
280/// through which to perform a send to the given Objective-C method,
281/// using the given receiver type.  The receiver type is not always
282/// the 'self' type of the method or even an Objective-C pointer type.
283/// This is *not* the right method for actually performing such a
284/// message send, due to the possibility of optional arguments.
285const CGFunctionInfo &
286CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
287                                              QualType receiverType) {
288  SmallVector<CanQualType, 16> argTys;
289  argTys.push_back(Context.getCanonicalParamType(receiverType));
290  argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
291  // FIXME: Kill copy?
292  for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
293         e = MD->param_end(); i != e; ++i) {
294    argTys.push_back(Context.getCanonicalParamType((*i)->getType()));
295  }
296
297  FunctionType::ExtInfo einfo;
298  einfo = einfo.withCallingConv(getCallingConventionForDecl(MD));
299
300  if (getContext().getLangOpts().ObjCAutoRefCount &&
301      MD->hasAttr<NSReturnsRetainedAttr>())
302    einfo = einfo.withProducesResult(true);
303
304  RequiredArgs required =
305    (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
306
307  return arrangeLLVMFunctionInfo(GetReturnType(MD->getResultType()), argTys,
308                                 einfo, required);
309}
310
311const CGFunctionInfo &
312CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
313  // FIXME: Do we need to handle ObjCMethodDecl?
314  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
315
316  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
317    return arrangeCXXConstructorDeclaration(CD, GD.getCtorType());
318
319  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
320    return arrangeCXXDestructor(DD, GD.getDtorType());
321
322  return arrangeFunctionDeclaration(FD);
323}
324
325/// Arrange a call as unto a free function, except possibly with an
326/// additional number of formal parameters considered required.
327static const CGFunctionInfo &
328arrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
329                            const CallArgList &args,
330                            const FunctionType *fnType,
331                            unsigned numExtraRequiredArgs) {
332  assert(args.size() >= numExtraRequiredArgs);
333
334  // In most cases, there are no optional arguments.
335  RequiredArgs required = RequiredArgs::All;
336
337  // If we have a variadic prototype, the required arguments are the
338  // extra prefix plus the arguments in the prototype.
339  if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
340    if (proto->isVariadic())
341      required = RequiredArgs(proto->getNumArgs() + numExtraRequiredArgs);
342
343  // If we don't have a prototype at all, but we're supposed to
344  // explicitly use the variadic convention for unprototyped calls,
345  // treat all of the arguments as required but preserve the nominal
346  // possibility of variadics.
347  } else if (CGT.CGM.getTargetCodeGenInfo()
348               .isNoProtoCallVariadic(args, cast<FunctionNoProtoType>(fnType))) {
349    required = RequiredArgs(args.size());
350  }
351
352  return CGT.arrangeFreeFunctionCall(fnType->getResultType(), args,
353                                     fnType->getExtInfo(), required);
354}
355
356/// Figure out the rules for calling a function with the given formal
357/// type using the given arguments.  The arguments are necessary
358/// because the function might be unprototyped, in which case it's
359/// target-dependent in crazy ways.
360const CGFunctionInfo &
361CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
362                                      const FunctionType *fnType) {
363  return arrangeFreeFunctionLikeCall(*this, args, fnType, 0);
364}
365
366/// A block function call is essentially a free-function call with an
367/// extra implicit argument.
368const CGFunctionInfo &
369CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
370                                       const FunctionType *fnType) {
371  return arrangeFreeFunctionLikeCall(*this, args, fnType, 1);
372}
373
374const CGFunctionInfo &
375CodeGenTypes::arrangeFreeFunctionCall(QualType resultType,
376                                      const CallArgList &args,
377                                      FunctionType::ExtInfo info,
378                                      RequiredArgs required) {
379  // FIXME: Kill copy.
380  SmallVector<CanQualType, 16> argTypes;
381  for (CallArgList::const_iterator i = args.begin(), e = args.end();
382       i != e; ++i)
383    argTypes.push_back(Context.getCanonicalParamType(i->Ty));
384  return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info,
385                                 required);
386}
387
388/// Arrange a call to a C++ method, passing the given arguments.
389const CGFunctionInfo &
390CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
391                                   const FunctionProtoType *FPT,
392                                   RequiredArgs required) {
393  // FIXME: Kill copy.
394  SmallVector<CanQualType, 16> argTypes;
395  for (CallArgList::const_iterator i = args.begin(), e = args.end();
396       i != e; ++i)
397    argTypes.push_back(Context.getCanonicalParamType(i->Ty));
398
399  FunctionType::ExtInfo info = FPT->getExtInfo();
400  adjustCXXMethodInfo(*this, info, FPT->isVariadic());
401  return arrangeLLVMFunctionInfo(GetReturnType(FPT->getResultType()),
402                                 argTypes, info, required);
403}
404
405const CGFunctionInfo &
406CodeGenTypes::arrangeFunctionDeclaration(QualType resultType,
407                                         const FunctionArgList &args,
408                                         const FunctionType::ExtInfo &info,
409                                         bool isVariadic) {
410  // FIXME: Kill copy.
411  SmallVector<CanQualType, 16> argTypes;
412  for (FunctionArgList::const_iterator i = args.begin(), e = args.end();
413       i != e; ++i)
414    argTypes.push_back(Context.getCanonicalParamType((*i)->getType()));
415
416  RequiredArgs required =
417    (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All);
418  return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info,
419                                 required);
420}
421
422const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
423  return arrangeLLVMFunctionInfo(getContext().VoidTy, ArrayRef<CanQualType>(),
424                                 FunctionType::ExtInfo(), RequiredArgs::All);
425}
426
427/// Arrange the argument and result information for an abstract value
428/// of a given function type.  This is the method which all of the
429/// above functions ultimately defer to.
430const CGFunctionInfo &
431CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
432                                      ArrayRef<CanQualType> argTypes,
433                                      FunctionType::ExtInfo info,
434                                      RequiredArgs required) {
435#ifndef NDEBUG
436  for (ArrayRef<CanQualType>::const_iterator
437         I = argTypes.begin(), E = argTypes.end(); I != E; ++I)
438    assert(I->isCanonicalAsParam());
439#endif
440
441  unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
442
443  // Lookup or create unique function info.
444  llvm::FoldingSetNodeID ID;
445  CGFunctionInfo::Profile(ID, info, required, resultType, argTypes);
446
447  void *insertPos = 0;
448  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
449  if (FI)
450    return *FI;
451
452  // Construct the function info.  We co-allocate the ArgInfos.
453  FI = CGFunctionInfo::create(CC, info, resultType, argTypes, required);
454  FunctionInfos.InsertNode(FI, insertPos);
455
456  bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted;
457  assert(inserted && "Recursively being processed?");
458
459  // Compute ABI information.
460  getABIInfo().computeInfo(*FI);
461
462  // Loop over all of the computed argument and return value info.  If any of
463  // them are direct or extend without a specified coerce type, specify the
464  // default now.
465  ABIArgInfo &retInfo = FI->getReturnInfo();
466  if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0)
467    retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
468
469  for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
470       I != E; ++I)
471    if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
472      I->info.setCoerceToType(ConvertType(I->type));
473
474  bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
475  assert(erased && "Not in set?");
476
477  return *FI;
478}
479
480CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
481                                       const FunctionType::ExtInfo &info,
482                                       CanQualType resultType,
483                                       ArrayRef<CanQualType> argTypes,
484                                       RequiredArgs required) {
485  void *buffer = operator new(sizeof(CGFunctionInfo) +
486                              sizeof(ArgInfo) * (argTypes.size() + 1));
487  CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
488  FI->CallingConvention = llvmCC;
489  FI->EffectiveCallingConvention = llvmCC;
490  FI->ASTCallingConvention = info.getCC();
491  FI->NoReturn = info.getNoReturn();
492  FI->ReturnsRetained = info.getProducesResult();
493  FI->Required = required;
494  FI->HasRegParm = info.getHasRegParm();
495  FI->RegParm = info.getRegParm();
496  FI->NumArgs = argTypes.size();
497  FI->getArgsBuffer()[0].type = resultType;
498  for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
499    FI->getArgsBuffer()[i + 1].type = argTypes[i];
500  return FI;
501}
502
503/***/
504
505void CodeGenTypes::GetExpandedTypes(QualType type,
506                     SmallVectorImpl<llvm::Type*> &expandedTypes) {
507  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) {
508    uint64_t NumElts = AT->getSize().getZExtValue();
509    for (uint64_t Elt = 0; Elt < NumElts; ++Elt)
510      GetExpandedTypes(AT->getElementType(), expandedTypes);
511  } else if (const RecordType *RT = type->getAs<RecordType>()) {
512    const RecordDecl *RD = RT->getDecl();
513    assert(!RD->hasFlexibleArrayMember() &&
514           "Cannot expand structure with flexible array.");
515    if (RD->isUnion()) {
516      // Unions can be here only in degenerative cases - all the fields are same
517      // after flattening. Thus we have to use the "largest" field.
518      const FieldDecl *LargestFD = 0;
519      CharUnits UnionSize = CharUnits::Zero();
520
521      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
522           i != e; ++i) {
523        const FieldDecl *FD = *i;
524        assert(!FD->isBitField() &&
525               "Cannot expand structure with bit-field members.");
526        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
527        if (UnionSize < FieldSize) {
528          UnionSize = FieldSize;
529          LargestFD = FD;
530        }
531      }
532      if (LargestFD)
533        GetExpandedTypes(LargestFD->getType(), expandedTypes);
534    } else {
535      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
536           i != e; ++i) {
537        assert(!i->isBitField() &&
538               "Cannot expand structure with bit-field members.");
539        GetExpandedTypes(i->getType(), expandedTypes);
540      }
541    }
542  } else if (const ComplexType *CT = type->getAs<ComplexType>()) {
543    llvm::Type *EltTy = ConvertType(CT->getElementType());
544    expandedTypes.push_back(EltTy);
545    expandedTypes.push_back(EltTy);
546  } else
547    expandedTypes.push_back(ConvertType(type));
548}
549
550llvm::Function::arg_iterator
551CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
552                                    llvm::Function::arg_iterator AI) {
553  assert(LV.isSimple() &&
554         "Unexpected non-simple lvalue during struct expansion.");
555
556  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
557    unsigned NumElts = AT->getSize().getZExtValue();
558    QualType EltTy = AT->getElementType();
559    for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
560      llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt);
561      LValue LV = MakeAddrLValue(EltAddr, EltTy);
562      AI = ExpandTypeFromArgs(EltTy, LV, AI);
563    }
564  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
565    RecordDecl *RD = RT->getDecl();
566    if (RD->isUnion()) {
567      // Unions can be here only in degenerative cases - all the fields are same
568      // after flattening. Thus we have to use the "largest" field.
569      const FieldDecl *LargestFD = 0;
570      CharUnits UnionSize = CharUnits::Zero();
571
572      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
573           i != e; ++i) {
574        const FieldDecl *FD = *i;
575        assert(!FD->isBitField() &&
576               "Cannot expand structure with bit-field members.");
577        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
578        if (UnionSize < FieldSize) {
579          UnionSize = FieldSize;
580          LargestFD = FD;
581        }
582      }
583      if (LargestFD) {
584        // FIXME: What are the right qualifiers here?
585        LValue SubLV = EmitLValueForField(LV, LargestFD);
586        AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI);
587      }
588    } else {
589      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
590           i != e; ++i) {
591        FieldDecl *FD = *i;
592        QualType FT = FD->getType();
593
594        // FIXME: What are the right qualifiers here?
595        LValue SubLV = EmitLValueForField(LV, FD);
596        AI = ExpandTypeFromArgs(FT, SubLV, AI);
597      }
598    }
599  } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
600    QualType EltTy = CT->getElementType();
601    llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real");
602    EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy));
603    llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag");
604    EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy));
605  } else {
606    EmitStoreThroughLValue(RValue::get(AI), LV);
607    ++AI;
608  }
609
610  return AI;
611}
612
613/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
614/// accessing some number of bytes out of it, try to gep into the struct to get
615/// at its inner goodness.  Dive as deep as possible without entering an element
616/// with an in-memory size smaller than DstSize.
617static llvm::Value *
618EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
619                                   llvm::StructType *SrcSTy,
620                                   uint64_t DstSize, CodeGenFunction &CGF) {
621  // We can't dive into a zero-element struct.
622  if (SrcSTy->getNumElements() == 0) return SrcPtr;
623
624  llvm::Type *FirstElt = SrcSTy->getElementType(0);
625
626  // If the first elt is at least as large as what we're looking for, or if the
627  // first element is the same size as the whole struct, we can enter it.
628  uint64_t FirstEltSize =
629    CGF.CGM.getDataLayout().getTypeAllocSize(FirstElt);
630  if (FirstEltSize < DstSize &&
631      FirstEltSize < CGF.CGM.getDataLayout().getTypeAllocSize(SrcSTy))
632    return SrcPtr;
633
634  // GEP into the first element.
635  SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
636
637  // If the first element is a struct, recurse.
638  llvm::Type *SrcTy =
639    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
640  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
641    return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
642
643  return SrcPtr;
644}
645
646/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
647/// are either integers or pointers.  This does a truncation of the value if it
648/// is too large or a zero extension if it is too small.
649static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
650                                             llvm::Type *Ty,
651                                             CodeGenFunction &CGF) {
652  if (Val->getType() == Ty)
653    return Val;
654
655  if (isa<llvm::PointerType>(Val->getType())) {
656    // If this is Pointer->Pointer avoid conversion to and from int.
657    if (isa<llvm::PointerType>(Ty))
658      return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
659
660    // Convert the pointer to an integer so we can play with its width.
661    Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
662  }
663
664  llvm::Type *DestIntTy = Ty;
665  if (isa<llvm::PointerType>(DestIntTy))
666    DestIntTy = CGF.IntPtrTy;
667
668  if (Val->getType() != DestIntTy)
669    Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
670
671  if (isa<llvm::PointerType>(Ty))
672    Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
673  return Val;
674}
675
676
677
678/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
679/// a pointer to an object of type \arg Ty.
680///
681/// This safely handles the case when the src type is smaller than the
682/// destination type; in this situation the values of bits which not
683/// present in the src are undefined.
684static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
685                                      llvm::Type *Ty,
686                                      CodeGenFunction &CGF) {
687  llvm::Type *SrcTy =
688    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
689
690  // If SrcTy and Ty are the same, just do a load.
691  if (SrcTy == Ty)
692    return CGF.Builder.CreateLoad(SrcPtr);
693
694  uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
695
696  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
697    SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
698    SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
699  }
700
701  uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
702
703  // If the source and destination are integer or pointer types, just do an
704  // extension or truncation to the desired type.
705  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
706      (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
707    llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
708    return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
709  }
710
711  // If load is legal, just bitcast the src pointer.
712  if (SrcSize >= DstSize) {
713    // Generally SrcSize is never greater than DstSize, since this means we are
714    // losing bits. However, this can happen in cases where the structure has
715    // additional padding, for example due to a user specified alignment.
716    //
717    // FIXME: Assert that we aren't truncating non-padding bits when have access
718    // to that information.
719    llvm::Value *Casted =
720      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
721    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
722    // FIXME: Use better alignment / avoid requiring aligned load.
723    Load->setAlignment(1);
724    return Load;
725  }
726
727  // Otherwise do coercion through memory. This is stupid, but
728  // simple.
729  llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
730  llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
731  llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
732  llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy);
733  // FIXME: Use better alignment.
734  CGF.Builder.CreateMemCpy(Casted, SrcCasted,
735      llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
736      1, false);
737  return CGF.Builder.CreateLoad(Tmp);
738}
739
740// Function to store a first-class aggregate into memory.  We prefer to
741// store the elements rather than the aggregate to be more friendly to
742// fast-isel.
743// FIXME: Do we need to recurse here?
744static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
745                          llvm::Value *DestPtr, bool DestIsVolatile,
746                          bool LowAlignment) {
747  // Prefer scalar stores to first-class aggregate stores.
748  if (llvm::StructType *STy =
749        dyn_cast<llvm::StructType>(Val->getType())) {
750    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
751      llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
752      llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
753      llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
754                                                    DestIsVolatile);
755      if (LowAlignment)
756        SI->setAlignment(1);
757    }
758  } else {
759    llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
760    if (LowAlignment)
761      SI->setAlignment(1);
762  }
763}
764
765/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
766/// where the source and destination may have different types.
767///
768/// This safely handles the case when the src type is larger than the
769/// destination type; the upper bits of the src will be lost.
770static void CreateCoercedStore(llvm::Value *Src,
771                               llvm::Value *DstPtr,
772                               bool DstIsVolatile,
773                               CodeGenFunction &CGF) {
774  llvm::Type *SrcTy = Src->getType();
775  llvm::Type *DstTy =
776    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
777  if (SrcTy == DstTy) {
778    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
779    return;
780  }
781
782  uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
783
784  if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
785    DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
786    DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
787  }
788
789  // If the source and destination are integer or pointer types, just do an
790  // extension or truncation to the desired type.
791  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
792      (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
793    Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
794    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
795    return;
796  }
797
798  uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
799
800  // If store is legal, just bitcast the src pointer.
801  if (SrcSize <= DstSize) {
802    llvm::Value *Casted =
803      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
804    // FIXME: Use better alignment / avoid requiring aligned store.
805    BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
806  } else {
807    // Otherwise do coercion through memory. This is stupid, but
808    // simple.
809
810    // Generally SrcSize is never greater than DstSize, since this means we are
811    // losing bits. However, this can happen in cases where the structure has
812    // additional padding, for example due to a user specified alignment.
813    //
814    // FIXME: Assert that we aren't truncating non-padding bits when have access
815    // to that information.
816    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
817    CGF.Builder.CreateStore(Src, Tmp);
818    llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
819    llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
820    llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy);
821    // FIXME: Use better alignment.
822    CGF.Builder.CreateMemCpy(DstCasted, Casted,
823        llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
824        1, false);
825  }
826}
827
828/***/
829
830bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
831  return FI.getReturnInfo().isIndirect();
832}
833
834bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
835  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
836    switch (BT->getKind()) {
837    default:
838      return false;
839    case BuiltinType::Float:
840      return getTarget().useObjCFPRetForRealType(TargetInfo::Float);
841    case BuiltinType::Double:
842      return getTarget().useObjCFPRetForRealType(TargetInfo::Double);
843    case BuiltinType::LongDouble:
844      return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble);
845    }
846  }
847
848  return false;
849}
850
851bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
852  if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
853    if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
854      if (BT->getKind() == BuiltinType::LongDouble)
855        return getTarget().useObjCFP2RetForComplexLongDouble();
856    }
857  }
858
859  return false;
860}
861
862llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
863  const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
864  return GetFunctionType(FI);
865}
866
867llvm::FunctionType *
868CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
869
870  bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted;
871  assert(Inserted && "Recursively being processed?");
872
873  SmallVector<llvm::Type*, 8> argTypes;
874  llvm::Type *resultType = 0;
875
876  const ABIArgInfo &retAI = FI.getReturnInfo();
877  switch (retAI.getKind()) {
878  case ABIArgInfo::Expand:
879    llvm_unreachable("Invalid ABI kind for return argument");
880
881  case ABIArgInfo::Extend:
882  case ABIArgInfo::Direct:
883    resultType = retAI.getCoerceToType();
884    break;
885
886  case ABIArgInfo::Indirect: {
887    assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
888    resultType = llvm::Type::getVoidTy(getLLVMContext());
889
890    QualType ret = FI.getReturnType();
891    llvm::Type *ty = ConvertType(ret);
892    unsigned addressSpace = Context.getTargetAddressSpace(ret);
893    argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
894    break;
895  }
896
897  case ABIArgInfo::Ignore:
898    resultType = llvm::Type::getVoidTy(getLLVMContext());
899    break;
900  }
901
902  // Add in all of the required arguments.
903  CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), ie;
904  if (FI.isVariadic()) {
905    ie = it + FI.getRequiredArgs().getNumRequiredArgs();
906  } else {
907    ie = FI.arg_end();
908  }
909  for (; it != ie; ++it) {
910    const ABIArgInfo &argAI = it->info;
911
912    // Insert a padding type to ensure proper alignment.
913    if (llvm::Type *PaddingType = argAI.getPaddingType())
914      argTypes.push_back(PaddingType);
915
916    switch (argAI.getKind()) {
917    case ABIArgInfo::Ignore:
918      break;
919
920    case ABIArgInfo::Indirect: {
921      // indirect arguments are always on the stack, which is addr space #0.
922      llvm::Type *LTy = ConvertTypeForMem(it->type);
923      argTypes.push_back(LTy->getPointerTo());
924      break;
925    }
926
927    case ABIArgInfo::Extend:
928    case ABIArgInfo::Direct: {
929      // If the coerce-to type is a first class aggregate, flatten it.  Either
930      // way is semantically identical, but fast-isel and the optimizer
931      // generally likes scalar values better than FCAs.
932      llvm::Type *argType = argAI.getCoerceToType();
933      if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
934        for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
935          argTypes.push_back(st->getElementType(i));
936      } else {
937        argTypes.push_back(argType);
938      }
939      break;
940    }
941
942    case ABIArgInfo::Expand:
943      GetExpandedTypes(it->type, argTypes);
944      break;
945    }
946  }
947
948  bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
949  assert(Erased && "Not in set?");
950
951  return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic());
952}
953
954llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
955  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
956  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
957
958  if (!isFuncTypeConvertible(FPT))
959    return llvm::StructType::get(getLLVMContext());
960
961  const CGFunctionInfo *Info;
962  if (isa<CXXDestructorDecl>(MD))
963    Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType());
964  else
965    Info = &arrangeCXXMethodDeclaration(MD);
966  return GetFunctionType(*Info);
967}
968
969void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
970                                           const Decl *TargetDecl,
971                                           AttributeListType &PAL,
972                                           unsigned &CallingConv,
973                                           bool AttrOnCallSite) {
974  llvm::AttrBuilder FuncAttrs;
975  llvm::AttrBuilder RetAttrs;
976
977  CallingConv = FI.getEffectiveCallingConvention();
978
979  if (FI.isNoReturn())
980    FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
981
982  // FIXME: handle sseregparm someday...
983  if (TargetDecl) {
984    if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
985      FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
986    if (TargetDecl->hasAttr<NoThrowAttr>())
987      FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
988    if (TargetDecl->hasAttr<NoReturnAttr>())
989      FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
990
991    if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
992      const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
993      if (FPT && FPT->isNothrow(getContext()))
994        FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
995      // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
996      // These attributes are not inherited by overloads.
997      const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
998      if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
999        FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
1000    }
1001
1002    // 'const' and 'pure' attribute functions are also nounwind.
1003    if (TargetDecl->hasAttr<ConstAttr>()) {
1004      FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
1005      FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1006    } else if (TargetDecl->hasAttr<PureAttr>()) {
1007      FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
1008      FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1009    }
1010    if (TargetDecl->hasAttr<MallocAttr>())
1011      RetAttrs.addAttribute(llvm::Attribute::NoAlias);
1012  }
1013
1014  if (CodeGenOpts.OptimizeSize)
1015    FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
1016  if (CodeGenOpts.OptimizeSize == 2)
1017    FuncAttrs.addAttribute(llvm::Attribute::MinSize);
1018  if (CodeGenOpts.DisableRedZone)
1019    FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
1020  if (CodeGenOpts.NoImplicitFloat)
1021    FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
1022
1023  if (AttrOnCallSite) {
1024    // Attributes that should go on the call site only.
1025    if (!CodeGenOpts.SimplifyLibCalls)
1026      FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
1027  } else {
1028    // Attributes that should go on the function, but not the call site.
1029    if (!CodeGenOpts.DisableFPElim) {
1030      FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
1031      FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf", "false");
1032    } else if (CodeGenOpts.OmitLeafFramePointer) {
1033      FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
1034      FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf", "true");
1035    } else {
1036      FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
1037      FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf", "true");
1038    }
1039
1040    FuncAttrs.addAttribute("less-precise-fpmad",
1041                           CodeGenOpts.LessPreciseFPMAD ? "true" : "false");
1042    FuncAttrs.addAttribute("no-infs-fp-math",
1043                           CodeGenOpts.NoInfsFPMath ? "true" : "false");
1044    FuncAttrs.addAttribute("no-nans-fp-math",
1045                           CodeGenOpts.NoNaNsFPMath ? "true" : "false");
1046    FuncAttrs.addAttribute("unsafe-fp-math",
1047                           CodeGenOpts.UnsafeFPMath ? "true" : "false");
1048    FuncAttrs.addAttribute("use-soft-float",
1049                           CodeGenOpts.SoftFloat ? "true" : "false");
1050  }
1051
1052  QualType RetTy = FI.getReturnType();
1053  unsigned Index = 1;
1054  const ABIArgInfo &RetAI = FI.getReturnInfo();
1055  switch (RetAI.getKind()) {
1056  case ABIArgInfo::Extend:
1057   if (RetTy->hasSignedIntegerRepresentation())
1058     RetAttrs.addAttribute(llvm::Attribute::SExt);
1059   else if (RetTy->hasUnsignedIntegerRepresentation())
1060     RetAttrs.addAttribute(llvm::Attribute::ZExt);
1061    break;
1062  case ABIArgInfo::Direct:
1063  case ABIArgInfo::Ignore:
1064    break;
1065
1066  case ABIArgInfo::Indirect: {
1067    llvm::AttrBuilder SRETAttrs;
1068    SRETAttrs.addAttribute(llvm::Attribute::StructRet);
1069    if (RetAI.getInReg())
1070      SRETAttrs.addAttribute(llvm::Attribute::InReg);
1071    PAL.push_back(llvm::
1072                  AttributeSet::get(getLLVMContext(), Index, SRETAttrs));
1073
1074    ++Index;
1075    // sret disables readnone and readonly
1076    FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1077      .removeAttribute(llvm::Attribute::ReadNone);
1078    break;
1079  }
1080
1081  case ABIArgInfo::Expand:
1082    llvm_unreachable("Invalid ABI kind for return argument");
1083  }
1084
1085  if (RetAttrs.hasAttributes())
1086    PAL.push_back(llvm::
1087                  AttributeSet::get(getLLVMContext(),
1088                                    llvm::AttributeSet::ReturnIndex,
1089                                    RetAttrs));
1090
1091  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1092         ie = FI.arg_end(); it != ie; ++it) {
1093    QualType ParamType = it->type;
1094    const ABIArgInfo &AI = it->info;
1095    llvm::AttrBuilder Attrs;
1096
1097    if (AI.getPaddingType()) {
1098      if (AI.getPaddingInReg())
1099        PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index,
1100                                              llvm::Attribute::InReg));
1101      // Increment Index if there is padding.
1102      ++Index;
1103    }
1104
1105    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
1106    // have the corresponding parameter variable.  It doesn't make
1107    // sense to do it here because parameters are so messed up.
1108    switch (AI.getKind()) {
1109    case ABIArgInfo::Extend:
1110      if (ParamType->isSignedIntegerOrEnumerationType())
1111        Attrs.addAttribute(llvm::Attribute::SExt);
1112      else if (ParamType->isUnsignedIntegerOrEnumerationType())
1113        Attrs.addAttribute(llvm::Attribute::ZExt);
1114      // FALL THROUGH
1115    case ABIArgInfo::Direct:
1116      if (AI.getInReg())
1117        Attrs.addAttribute(llvm::Attribute::InReg);
1118
1119      // FIXME: handle sseregparm someday...
1120
1121      if (llvm::StructType *STy =
1122          dyn_cast<llvm::StructType>(AI.getCoerceToType())) {
1123        unsigned Extra = STy->getNumElements()-1;  // 1 will be added below.
1124        if (Attrs.hasAttributes())
1125          for (unsigned I = 0; I < Extra; ++I)
1126            PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index + I,
1127                                                  Attrs));
1128        Index += Extra;
1129      }
1130      break;
1131
1132    case ABIArgInfo::Indirect:
1133      if (AI.getInReg())
1134        Attrs.addAttribute(llvm::Attribute::InReg);
1135
1136      if (AI.getIndirectByVal())
1137        Attrs.addAttribute(llvm::Attribute::ByVal);
1138
1139      Attrs.addAlignmentAttr(AI.getIndirectAlign());
1140
1141      // byval disables readnone and readonly.
1142      FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
1143        .removeAttribute(llvm::Attribute::ReadNone);
1144      break;
1145
1146    case ABIArgInfo::Ignore:
1147      // Skip increment, no matching LLVM parameter.
1148      continue;
1149
1150    case ABIArgInfo::Expand: {
1151      SmallVector<llvm::Type*, 8> types;
1152      // FIXME: This is rather inefficient. Do we ever actually need to do
1153      // anything here? The result should be just reconstructed on the other
1154      // side, so extension should be a non-issue.
1155      getTypes().GetExpandedTypes(ParamType, types);
1156      Index += types.size();
1157      continue;
1158    }
1159    }
1160
1161    if (Attrs.hasAttributes())
1162      PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs));
1163    ++Index;
1164  }
1165  if (FuncAttrs.hasAttributes())
1166    PAL.push_back(llvm::
1167                  AttributeSet::get(getLLVMContext(),
1168                                    llvm::AttributeSet::FunctionIndex,
1169                                    FuncAttrs));
1170}
1171
1172/// An argument came in as a promoted argument; demote it back to its
1173/// declared type.
1174static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
1175                                         const VarDecl *var,
1176                                         llvm::Value *value) {
1177  llvm::Type *varType = CGF.ConvertType(var->getType());
1178
1179  // This can happen with promotions that actually don't change the
1180  // underlying type, like the enum promotions.
1181  if (value->getType() == varType) return value;
1182
1183  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
1184         && "unexpected promotion type");
1185
1186  if (isa<llvm::IntegerType>(varType))
1187    return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
1188
1189  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
1190}
1191
1192void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1193                                         llvm::Function *Fn,
1194                                         const FunctionArgList &Args) {
1195  // If this is an implicit-return-zero function, go ahead and
1196  // initialize the return value.  TODO: it might be nice to have
1197  // a more general mechanism for this that didn't require synthesized
1198  // return statements.
1199  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
1200    if (FD->hasImplicitReturnZero()) {
1201      QualType RetTy = FD->getResultType().getUnqualifiedType();
1202      llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
1203      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
1204      Builder.CreateStore(Zero, ReturnValue);
1205    }
1206  }
1207
1208  // FIXME: We no longer need the types from FunctionArgList; lift up and
1209  // simplify.
1210
1211  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
1212  llvm::Function::arg_iterator AI = Fn->arg_begin();
1213
1214  // Name the struct return argument.
1215  if (CGM.ReturnTypeUsesSRet(FI)) {
1216    AI->setName("agg.result");
1217    AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1218                                        AI->getArgNo() + 1,
1219                                        llvm::Attribute::NoAlias));
1220    ++AI;
1221  }
1222
1223  assert(FI.arg_size() == Args.size() &&
1224         "Mismatch between function signature & arguments.");
1225  unsigned ArgNo = 1;
1226  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
1227  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1228       i != e; ++i, ++info_it, ++ArgNo) {
1229    const VarDecl *Arg = *i;
1230    QualType Ty = info_it->type;
1231    const ABIArgInfo &ArgI = info_it->info;
1232
1233    bool isPromoted =
1234      isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
1235
1236    // Skip the dummy padding argument.
1237    if (ArgI.getPaddingType())
1238      ++AI;
1239
1240    switch (ArgI.getKind()) {
1241    case ABIArgInfo::Indirect: {
1242      llvm::Value *V = AI;
1243
1244      if (!hasScalarEvaluationKind(Ty)) {
1245        // Aggregates and complex variables are accessed by reference.  All we
1246        // need to do is realign the value, if requested
1247        if (ArgI.getIndirectRealign()) {
1248          llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
1249
1250          // Copy from the incoming argument pointer to the temporary with the
1251          // appropriate alignment.
1252          //
1253          // FIXME: We should have a common utility for generating an aggregate
1254          // copy.
1255          llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
1256          CharUnits Size = getContext().getTypeSizeInChars(Ty);
1257          llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
1258          llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
1259          Builder.CreateMemCpy(Dst,
1260                               Src,
1261                               llvm::ConstantInt::get(IntPtrTy,
1262                                                      Size.getQuantity()),
1263                               ArgI.getIndirectAlign(),
1264                               false);
1265          V = AlignedTemp;
1266        }
1267      } else {
1268        // Load scalar value from indirect argument.
1269        CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1270        V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
1271
1272        if (isPromoted)
1273          V = emitArgumentDemotion(*this, Arg, V);
1274      }
1275      EmitParmDecl(*Arg, V, ArgNo);
1276      break;
1277    }
1278
1279    case ABIArgInfo::Extend:
1280    case ABIArgInfo::Direct: {
1281
1282      // If we have the trivial case, handle it with no muss and fuss.
1283      if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
1284          ArgI.getCoerceToType() == ConvertType(Ty) &&
1285          ArgI.getDirectOffset() == 0) {
1286        assert(AI != Fn->arg_end() && "Argument mismatch!");
1287        llvm::Value *V = AI;
1288
1289        if (Arg->getType().isRestrictQualified())
1290          AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
1291                                              AI->getArgNo() + 1,
1292                                              llvm::Attribute::NoAlias));
1293
1294        // Ensure the argument is the correct type.
1295        if (V->getType() != ArgI.getCoerceToType())
1296          V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
1297
1298        if (isPromoted)
1299          V = emitArgumentDemotion(*this, Arg, V);
1300
1301        // Because of merging of function types from multiple decls it is
1302        // possible for the type of an argument to not match the corresponding
1303        // type in the function type. Since we are codegening the callee
1304        // in here, add a cast to the argument type.
1305        llvm::Type *LTy = ConvertType(Arg->getType());
1306        if (V->getType() != LTy)
1307          V = Builder.CreateBitCast(V, LTy);
1308
1309        EmitParmDecl(*Arg, V, ArgNo);
1310        break;
1311      }
1312
1313      llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName());
1314
1315      // The alignment we need to use is the max of the requested alignment for
1316      // the argument plus the alignment required by our access code below.
1317      unsigned AlignmentToUse =
1318        CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType());
1319      AlignmentToUse = std::max(AlignmentToUse,
1320                        (unsigned)getContext().getDeclAlign(Arg).getQuantity());
1321
1322      Alloca->setAlignment(AlignmentToUse);
1323      llvm::Value *V = Alloca;
1324      llvm::Value *Ptr = V;    // Pointer to store into.
1325
1326      // If the value is offset in memory, apply the offset now.
1327      if (unsigned Offs = ArgI.getDirectOffset()) {
1328        Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1329        Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
1330        Ptr = Builder.CreateBitCast(Ptr,
1331                          llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1332      }
1333
1334      // If the coerce-to type is a first class aggregate, we flatten it and
1335      // pass the elements. Either way is semantically identical, but fast-isel
1336      // and the optimizer generally likes scalar values better than FCAs.
1337      llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
1338      if (STy && STy->getNumElements() > 1) {
1339        uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
1340        llvm::Type *DstTy =
1341          cast<llvm::PointerType>(Ptr->getType())->getElementType();
1342        uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
1343
1344        if (SrcSize <= DstSize) {
1345          Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
1346
1347          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1348            assert(AI != Fn->arg_end() && "Argument mismatch!");
1349            AI->setName(Arg->getName() + ".coerce" + Twine(i));
1350            llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1351            Builder.CreateStore(AI++, EltPtr);
1352          }
1353        } else {
1354          llvm::AllocaInst *TempAlloca =
1355            CreateTempAlloca(ArgI.getCoerceToType(), "coerce");
1356          TempAlloca->setAlignment(AlignmentToUse);
1357          llvm::Value *TempV = TempAlloca;
1358
1359          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1360            assert(AI != Fn->arg_end() && "Argument mismatch!");
1361            AI->setName(Arg->getName() + ".coerce" + Twine(i));
1362            llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i);
1363            Builder.CreateStore(AI++, EltPtr);
1364          }
1365
1366          Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse);
1367        }
1368      } else {
1369        // Simple case, just do a coerced store of the argument into the alloca.
1370        assert(AI != Fn->arg_end() && "Argument mismatch!");
1371        AI->setName(Arg->getName() + ".coerce");
1372        CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
1373      }
1374
1375
1376      // Match to what EmitParmDecl is expecting for this type.
1377      if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
1378        V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1379        if (isPromoted)
1380          V = emitArgumentDemotion(*this, Arg, V);
1381      }
1382      EmitParmDecl(*Arg, V, ArgNo);
1383      continue;  // Skip ++AI increment, already done.
1384    }
1385
1386    case ABIArgInfo::Expand: {
1387      // If this structure was expanded into multiple arguments then
1388      // we need to create a temporary and reconstruct it from the
1389      // arguments.
1390      llvm::AllocaInst *Alloca = CreateMemTemp(Ty);
1391      CharUnits Align = getContext().getDeclAlign(Arg);
1392      Alloca->setAlignment(Align.getQuantity());
1393      LValue LV = MakeAddrLValue(Alloca, Ty, Align);
1394      llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI);
1395      EmitParmDecl(*Arg, Alloca, ArgNo);
1396
1397      // Name the arguments used in expansion and increment AI.
1398      unsigned Index = 0;
1399      for (; AI != End; ++AI, ++Index)
1400        AI->setName(Arg->getName() + "." + Twine(Index));
1401      continue;
1402    }
1403
1404    case ABIArgInfo::Ignore:
1405      // Initialize the local variable appropriately.
1406      if (!hasScalarEvaluationKind(Ty))
1407        EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1408      else
1409        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1410                     ArgNo);
1411
1412      // Skip increment, no matching LLVM parameter.
1413      continue;
1414    }
1415
1416    ++AI;
1417  }
1418  assert(AI == Fn->arg_end() && "Argument mismatch!");
1419}
1420
1421static void eraseUnusedBitCasts(llvm::Instruction *insn) {
1422  while (insn->use_empty()) {
1423    llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
1424    if (!bitcast) return;
1425
1426    // This is "safe" because we would have used a ConstantExpr otherwise.
1427    insn = cast<llvm::Instruction>(bitcast->getOperand(0));
1428    bitcast->eraseFromParent();
1429  }
1430}
1431
1432/// Try to emit a fused autorelease of a return result.
1433static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1434                                                    llvm::Value *result) {
1435  // We must be immediately followed the cast.
1436  llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1437  if (BB->empty()) return 0;
1438  if (&BB->back() != result) return 0;
1439
1440  llvm::Type *resultType = result->getType();
1441
1442  // result is in a BasicBlock and is therefore an Instruction.
1443  llvm::Instruction *generator = cast<llvm::Instruction>(result);
1444
1445  SmallVector<llvm::Instruction*,4> insnsToKill;
1446
1447  // Look for:
1448  //  %generator = bitcast %type1* %generator2 to %type2*
1449  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1450    // We would have emitted this as a constant if the operand weren't
1451    // an Instruction.
1452    generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1453
1454    // Require the generator to be immediately followed by the cast.
1455    if (generator->getNextNode() != bitcast)
1456      return 0;
1457
1458    insnsToKill.push_back(bitcast);
1459  }
1460
1461  // Look for:
1462  //   %generator = call i8* @objc_retain(i8* %originalResult)
1463  // or
1464  //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1465  llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1466  if (!call) return 0;
1467
1468  bool doRetainAutorelease;
1469
1470  if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1471    doRetainAutorelease = true;
1472  } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1473                                          .objc_retainAutoreleasedReturnValue) {
1474    doRetainAutorelease = false;
1475
1476    // If we emitted an assembly marker for this call (and the
1477    // ARCEntrypoints field should have been set if so), go looking
1478    // for that call.  If we can't find it, we can't do this
1479    // optimization.  But it should always be the immediately previous
1480    // instruction, unless we needed bitcasts around the call.
1481    if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) {
1482      llvm::Instruction *prev = call->getPrevNode();
1483      assert(prev);
1484      if (isa<llvm::BitCastInst>(prev)) {
1485        prev = prev->getPrevNode();
1486        assert(prev);
1487      }
1488      assert(isa<llvm::CallInst>(prev));
1489      assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
1490               CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker);
1491      insnsToKill.push_back(prev);
1492    }
1493  } else {
1494    return 0;
1495  }
1496
1497  result = call->getArgOperand(0);
1498  insnsToKill.push_back(call);
1499
1500  // Keep killing bitcasts, for sanity.  Note that we no longer care
1501  // about precise ordering as long as there's exactly one use.
1502  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
1503    if (!bitcast->hasOneUse()) break;
1504    insnsToKill.push_back(bitcast);
1505    result = bitcast->getOperand(0);
1506  }
1507
1508  // Delete all the unnecessary instructions, from latest to earliest.
1509  for (SmallVectorImpl<llvm::Instruction*>::iterator
1510         i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
1511    (*i)->eraseFromParent();
1512
1513  // Do the fused retain/autorelease if we were asked to.
1514  if (doRetainAutorelease)
1515    result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
1516
1517  // Cast back to the result type.
1518  return CGF.Builder.CreateBitCast(result, resultType);
1519}
1520
1521/// If this is a +1 of the value of an immutable 'self', remove it.
1522static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
1523                                          llvm::Value *result) {
1524  // This is only applicable to a method with an immutable 'self'.
1525  const ObjCMethodDecl *method =
1526    dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
1527  if (!method) return 0;
1528  const VarDecl *self = method->getSelfDecl();
1529  if (!self->getType().isConstQualified()) return 0;
1530
1531  // Look for a retain call.
1532  llvm::CallInst *retainCall =
1533    dyn_cast<llvm::CallInst>(result->stripPointerCasts());
1534  if (!retainCall ||
1535      retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain)
1536    return 0;
1537
1538  // Look for an ordinary load of 'self'.
1539  llvm::Value *retainedValue = retainCall->getArgOperand(0);
1540  llvm::LoadInst *load =
1541    dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
1542  if (!load || load->isAtomic() || load->isVolatile() ||
1543      load->getPointerOperand() != CGF.GetAddrOfLocalVar(self))
1544    return 0;
1545
1546  // Okay!  Burn it all down.  This relies for correctness on the
1547  // assumption that the retain is emitted as part of the return and
1548  // that thereafter everything is used "linearly".
1549  llvm::Type *resultType = result->getType();
1550  eraseUnusedBitCasts(cast<llvm::Instruction>(result));
1551  assert(retainCall->use_empty());
1552  retainCall->eraseFromParent();
1553  eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
1554
1555  return CGF.Builder.CreateBitCast(load, resultType);
1556}
1557
1558/// Emit an ARC autorelease of the result of a function.
1559///
1560/// \return the value to actually return from the function
1561static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
1562                                            llvm::Value *result) {
1563  // If we're returning 'self', kill the initial retain.  This is a
1564  // heuristic attempt to "encourage correctness" in the really unfortunate
1565  // case where we have a return of self during a dealloc and we desperately
1566  // need to avoid the possible autorelease.
1567  if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
1568    return self;
1569
1570  // At -O0, try to emit a fused retain/autorelease.
1571  if (CGF.shouldUseFusedARCCalls())
1572    if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
1573      return fused;
1574
1575  return CGF.EmitARCAutoreleaseReturnValue(result);
1576}
1577
1578/// Heuristically search for a dominating store to the return-value slot.
1579static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
1580  // If there are multiple uses of the return-value slot, just check
1581  // for something immediately preceding the IP.  Sometimes this can
1582  // happen with how we generate implicit-returns; it can also happen
1583  // with noreturn cleanups.
1584  if (!CGF.ReturnValue->hasOneUse()) {
1585    llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1586    if (IP->empty()) return 0;
1587    llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back());
1588    if (!store) return 0;
1589    if (store->getPointerOperand() != CGF.ReturnValue) return 0;
1590    assert(!store->isAtomic() && !store->isVolatile()); // see below
1591    return store;
1592  }
1593
1594  llvm::StoreInst *store =
1595    dyn_cast<llvm::StoreInst>(CGF.ReturnValue->use_back());
1596  if (!store) return 0;
1597
1598  // These aren't actually possible for non-coerced returns, and we
1599  // only care about non-coerced returns on this code path.
1600  assert(!store->isAtomic() && !store->isVolatile());
1601
1602  // Now do a first-and-dirty dominance check: just walk up the
1603  // single-predecessors chain from the current insertion point.
1604  llvm::BasicBlock *StoreBB = store->getParent();
1605  llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1606  while (IP != StoreBB) {
1607    if (!(IP = IP->getSinglePredecessor()))
1608      return 0;
1609  }
1610
1611  // Okay, the store's basic block dominates the insertion point; we
1612  // can do our thing.
1613  return store;
1614}
1615
1616/// Check whether 'this' argument of a callsite matches 'this' of the caller.
1617static bool checkThisPointer(llvm::Value *ThisArg, llvm::Value *This) {
1618  if (ThisArg == This)
1619    return true;
1620  // Check whether ThisArg is a bitcast of This.
1621  llvm::BitCastInst *Bitcast;
1622  if ((Bitcast = dyn_cast<llvm::BitCastInst>(ThisArg)) &&
1623      Bitcast->getOperand(0) == This)
1624    return true;
1625  return false;
1626}
1627
1628void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
1629  // Functions with no result always return void.
1630  if (ReturnValue == 0) {
1631    Builder.CreateRetVoid();
1632    return;
1633  }
1634
1635  llvm::DebugLoc RetDbgLoc;
1636  llvm::Value *RV = 0;
1637  QualType RetTy = FI.getReturnType();
1638  const ABIArgInfo &RetAI = FI.getReturnInfo();
1639
1640  switch (RetAI.getKind()) {
1641  case ABIArgInfo::Indirect: {
1642    switch (getEvaluationKind(RetTy)) {
1643    case TEK_Complex: {
1644      ComplexPairTy RT =
1645        EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy));
1646      EmitStoreOfComplex(RT,
1647                       MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy),
1648                         /*isInit*/ true);
1649      break;
1650    }
1651    case TEK_Aggregate:
1652      // Do nothing; aggregrates get evaluated directly into the destination.
1653      break;
1654    case TEK_Scalar:
1655      EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
1656                        MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy),
1657                        /*isInit*/ true);
1658      break;
1659    }
1660    break;
1661  }
1662
1663  case ABIArgInfo::Extend:
1664  case ABIArgInfo::Direct:
1665    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1666        RetAI.getDirectOffset() == 0) {
1667      // The internal return value temp always will have pointer-to-return-type
1668      // type, just do a load.
1669
1670      // If there is a dominating store to ReturnValue, we can elide
1671      // the load, zap the store, and usually zap the alloca.
1672      if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) {
1673        // Get the stored value and nuke the now-dead store.
1674        RetDbgLoc = SI->getDebugLoc();
1675        RV = SI->getValueOperand();
1676        SI->eraseFromParent();
1677
1678        // If that was the only use of the return value, nuke it as well now.
1679        if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1680          cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1681          ReturnValue = 0;
1682        }
1683
1684      // Otherwise, we have to do a simple load.
1685      } else {
1686        RV = Builder.CreateLoad(ReturnValue);
1687      }
1688    } else {
1689      llvm::Value *V = ReturnValue;
1690      // If the value is offset in memory, apply the offset now.
1691      if (unsigned Offs = RetAI.getDirectOffset()) {
1692        V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1693        V = Builder.CreateConstGEP1_32(V, Offs);
1694        V = Builder.CreateBitCast(V,
1695                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1696      }
1697
1698      RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
1699    }
1700
1701    // In ARC, end functions that return a retainable type with a call
1702    // to objc_autoreleaseReturnValue.
1703    if (AutoreleaseResult) {
1704      assert(getLangOpts().ObjCAutoRefCount &&
1705             !FI.isReturnsRetained() &&
1706             RetTy->isObjCRetainableType());
1707      RV = emitAutoreleaseOfResult(*this, RV);
1708    }
1709
1710    break;
1711
1712  case ABIArgInfo::Ignore:
1713    break;
1714
1715  case ABIArgInfo::Expand:
1716    llvm_unreachable("Invalid ABI kind for return argument");
1717  }
1718
1719  // If this function returns 'this', the last instruction is a CallInst
1720  // that returns 'this', and 'this' argument of the CallInst points to
1721  // the same object as CXXThisValue, use the return value from the CallInst.
1722  // We will not need to keep 'this' alive through the callsite. It also enables
1723  // optimizations in the backend, such as tail call optimization.
1724  if (CalleeWithThisReturn && CGM.getCXXABI().HasThisReturn(CurGD)) {
1725    llvm::BasicBlock *IP = Builder.GetInsertBlock();
1726    llvm::CallInst *Callsite;
1727    if (!IP->empty() && (Callsite = dyn_cast<llvm::CallInst>(&IP->back())) &&
1728        Callsite->getCalledFunction() == CalleeWithThisReturn &&
1729        checkThisPointer(Callsite->getOperand(0), CXXThisValue))
1730      RV = Builder.CreateBitCast(Callsite, RetAI.getCoerceToType());
1731  }
1732  llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1733  if (!RetDbgLoc.isUnknown())
1734    Ret->setDebugLoc(RetDbgLoc);
1735}
1736
1737void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1738                                          const VarDecl *param) {
1739  // StartFunction converted the ABI-lowered parameter(s) into a
1740  // local alloca.  We need to turn that into an r-value suitable
1741  // for EmitCall.
1742  llvm::Value *local = GetAddrOfLocalVar(param);
1743
1744  QualType type = param->getType();
1745
1746  // For the most part, we just need to load the alloca, except:
1747  // 1) aggregate r-values are actually pointers to temporaries, and
1748  // 2) references to non-scalars are pointers directly to the aggregate.
1749  // I don't know why references to scalars are different here.
1750  if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1751    if (!hasScalarEvaluationKind(ref->getPointeeType()))
1752      return args.add(RValue::getAggregate(local), type);
1753
1754    // Locals which are references to scalars are represented
1755    // with allocas holding the pointer.
1756    return args.add(RValue::get(Builder.CreateLoad(local)), type);
1757  }
1758
1759  args.add(convertTempToRValue(local, type), type);
1760}
1761
1762static bool isProvablyNull(llvm::Value *addr) {
1763  return isa<llvm::ConstantPointerNull>(addr);
1764}
1765
1766static bool isProvablyNonNull(llvm::Value *addr) {
1767  return isa<llvm::AllocaInst>(addr);
1768}
1769
1770/// Emit the actual writing-back of a writeback.
1771static void emitWriteback(CodeGenFunction &CGF,
1772                          const CallArgList::Writeback &writeback) {
1773  const LValue &srcLV = writeback.Source;
1774  llvm::Value *srcAddr = srcLV.getAddress();
1775  assert(!isProvablyNull(srcAddr) &&
1776         "shouldn't have writeback for provably null argument");
1777
1778  llvm::BasicBlock *contBB = 0;
1779
1780  // If the argument wasn't provably non-null, we need to null check
1781  // before doing the store.
1782  bool provablyNonNull = isProvablyNonNull(srcAddr);
1783  if (!provablyNonNull) {
1784    llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
1785    contBB = CGF.createBasicBlock("icr.done");
1786
1787    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1788    CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
1789    CGF.EmitBlock(writebackBB);
1790  }
1791
1792  // Load the value to writeback.
1793  llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
1794
1795  // Cast it back, in case we're writing an id to a Foo* or something.
1796  value = CGF.Builder.CreateBitCast(value,
1797               cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
1798                            "icr.writeback-cast");
1799
1800  // Perform the writeback.
1801
1802  // If we have a "to use" value, it's something we need to emit a use
1803  // of.  This has to be carefully threaded in: if it's done after the
1804  // release it's potentially undefined behavior (and the optimizer
1805  // will ignore it), and if it happens before the retain then the
1806  // optimizer could move the release there.
1807  if (writeback.ToUse) {
1808    assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong);
1809
1810    // Retain the new value.  No need to block-copy here:  the block's
1811    // being passed up the stack.
1812    value = CGF.EmitARCRetainNonBlock(value);
1813
1814    // Emit the intrinsic use here.
1815    CGF.EmitARCIntrinsicUse(writeback.ToUse);
1816
1817    // Load the old value (primitively).
1818    llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV);
1819
1820    // Put the new value in place (primitively).
1821    CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false);
1822
1823    // Release the old value.
1824    CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime());
1825
1826  // Otherwise, we can just do a normal lvalue store.
1827  } else {
1828    CGF.EmitStoreThroughLValue(RValue::get(value), srcLV);
1829  }
1830
1831  // Jump to the continuation block.
1832  if (!provablyNonNull)
1833    CGF.EmitBlock(contBB);
1834}
1835
1836static void emitWritebacks(CodeGenFunction &CGF,
1837                           const CallArgList &args) {
1838  for (CallArgList::writeback_iterator
1839         i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i)
1840    emitWriteback(CGF, *i);
1841}
1842
1843static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) {
1844  if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens()))
1845    if (uop->getOpcode() == UO_AddrOf)
1846      return uop->getSubExpr();
1847  return 0;
1848}
1849
1850/// Emit an argument that's being passed call-by-writeback.  That is,
1851/// we are passing the address of
1852static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
1853                             const ObjCIndirectCopyRestoreExpr *CRE) {
1854  LValue srcLV;
1855
1856  // Make an optimistic effort to emit the address as an l-value.
1857  // This can fail if the the argument expression is more complicated.
1858  if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) {
1859    srcLV = CGF.EmitLValue(lvExpr);
1860
1861  // Otherwise, just emit it as a scalar.
1862  } else {
1863    llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
1864
1865    QualType srcAddrType =
1866      CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
1867    srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType);
1868  }
1869  llvm::Value *srcAddr = srcLV.getAddress();
1870
1871  // The dest and src types don't necessarily match in LLVM terms
1872  // because of the crazy ObjC compatibility rules.
1873
1874  llvm::PointerType *destType =
1875    cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
1876
1877  // If the address is a constant null, just pass the appropriate null.
1878  if (isProvablyNull(srcAddr)) {
1879    args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
1880             CRE->getType());
1881    return;
1882  }
1883
1884  // Create the temporary.
1885  llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
1886                                           "icr.temp");
1887  // Loading an l-value can introduce a cleanup if the l-value is __weak,
1888  // and that cleanup will be conditional if we can't prove that the l-value
1889  // isn't null, so we need to register a dominating point so that the cleanups
1890  // system will make valid IR.
1891  CodeGenFunction::ConditionalEvaluation condEval(CGF);
1892
1893  // Zero-initialize it if we're not doing a copy-initialization.
1894  bool shouldCopy = CRE->shouldCopy();
1895  if (!shouldCopy) {
1896    llvm::Value *null =
1897      llvm::ConstantPointerNull::get(
1898        cast<llvm::PointerType>(destType->getElementType()));
1899    CGF.Builder.CreateStore(null, temp);
1900  }
1901
1902  llvm::BasicBlock *contBB = 0;
1903  llvm::BasicBlock *originBB = 0;
1904
1905  // If the address is *not* known to be non-null, we need to switch.
1906  llvm::Value *finalArgument;
1907
1908  bool provablyNonNull = isProvablyNonNull(srcAddr);
1909  if (provablyNonNull) {
1910    finalArgument = temp;
1911  } else {
1912    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1913
1914    finalArgument = CGF.Builder.CreateSelect(isNull,
1915                                   llvm::ConstantPointerNull::get(destType),
1916                                             temp, "icr.argument");
1917
1918    // If we need to copy, then the load has to be conditional, which
1919    // means we need control flow.
1920    if (shouldCopy) {
1921      originBB = CGF.Builder.GetInsertBlock();
1922      contBB = CGF.createBasicBlock("icr.cont");
1923      llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
1924      CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
1925      CGF.EmitBlock(copyBB);
1926      condEval.begin(CGF);
1927    }
1928  }
1929
1930  llvm::Value *valueToUse = 0;
1931
1932  // Perform a copy if necessary.
1933  if (shouldCopy) {
1934    RValue srcRV = CGF.EmitLoadOfLValue(srcLV);
1935    assert(srcRV.isScalar());
1936
1937    llvm::Value *src = srcRV.getScalarVal();
1938    src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
1939                                    "icr.cast");
1940
1941    // Use an ordinary store, not a store-to-lvalue.
1942    CGF.Builder.CreateStore(src, temp);
1943
1944    // If optimization is enabled, and the value was held in a
1945    // __strong variable, we need to tell the optimizer that this
1946    // value has to stay alive until we're doing the store back.
1947    // This is because the temporary is effectively unretained,
1948    // and so otherwise we can violate the high-level semantics.
1949    if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 &&
1950        srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) {
1951      valueToUse = src;
1952    }
1953  }
1954
1955  // Finish the control flow if we needed it.
1956  if (shouldCopy && !provablyNonNull) {
1957    llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock();
1958    CGF.EmitBlock(contBB);
1959
1960    // Make a phi for the value to intrinsically use.
1961    if (valueToUse) {
1962      llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2,
1963                                                      "icr.to-use");
1964      phiToUse->addIncoming(valueToUse, copyBB);
1965      phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()),
1966                            originBB);
1967      valueToUse = phiToUse;
1968    }
1969
1970    condEval.end(CGF);
1971  }
1972
1973  args.addWriteback(srcLV, temp, valueToUse);
1974  args.add(RValue::get(finalArgument), CRE->getType());
1975}
1976
1977void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1978                                  QualType type) {
1979  if (const ObjCIndirectCopyRestoreExpr *CRE
1980        = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
1981    assert(getLangOpts().ObjCAutoRefCount);
1982    assert(getContext().hasSameType(E->getType(), type));
1983    return emitWritebackArg(*this, args, CRE);
1984  }
1985
1986  assert(type->isReferenceType() == E->isGLValue() &&
1987         "reference binding to unmaterialized r-value!");
1988
1989  if (E->isGLValue()) {
1990    assert(E->getObjectKind() == OK_Ordinary);
1991    return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1992                    type);
1993  }
1994
1995  if (hasAggregateEvaluationKind(type) &&
1996      isa<ImplicitCastExpr>(E) &&
1997      cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
1998    LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
1999    assert(L.isSimple());
2000    args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
2001    return;
2002  }
2003
2004  args.add(EmitAnyExprToTemp(E), type);
2005}
2006
2007// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2008// optimizer it can aggressively ignore unwind edges.
2009void
2010CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
2011  if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
2012      !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
2013    Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
2014                      CGM.getNoObjCARCExceptionsMetadata());
2015}
2016
2017/// Emits a call to the given no-arguments nounwind runtime function.
2018llvm::CallInst *
2019CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
2020                                         const llvm::Twine &name) {
2021  return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value*>(), name);
2022}
2023
2024/// Emits a call to the given nounwind runtime function.
2025llvm::CallInst *
2026CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
2027                                         ArrayRef<llvm::Value*> args,
2028                                         const llvm::Twine &name) {
2029  llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
2030  call->setDoesNotThrow();
2031  return call;
2032}
2033
2034/// Emits a simple call (never an invoke) to the given no-arguments
2035/// runtime function.
2036llvm::CallInst *
2037CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
2038                                 const llvm::Twine &name) {
2039  return EmitRuntimeCall(callee, ArrayRef<llvm::Value*>(), name);
2040}
2041
2042/// Emits a simple call (never an invoke) to the given runtime
2043/// function.
2044llvm::CallInst *
2045CodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
2046                                 ArrayRef<llvm::Value*> args,
2047                                 const llvm::Twine &name) {
2048  llvm::CallInst *call = Builder.CreateCall(callee, args, name);
2049  call->setCallingConv(getRuntimeCC());
2050  return call;
2051}
2052
2053/// Emits a call or invoke to the given noreturn runtime function.
2054void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2055                                               ArrayRef<llvm::Value*> args) {
2056  if (getInvokeDest()) {
2057    llvm::InvokeInst *invoke =
2058      Builder.CreateInvoke(callee,
2059                           getUnreachableBlock(),
2060                           getInvokeDest(),
2061                           args);
2062    invoke->setDoesNotReturn();
2063    invoke->setCallingConv(getRuntimeCC());
2064  } else {
2065    llvm::CallInst *call = Builder.CreateCall(callee, args);
2066    call->setDoesNotReturn();
2067    call->setCallingConv(getRuntimeCC());
2068    Builder.CreateUnreachable();
2069  }
2070}
2071
2072/// Emits a call or invoke instruction to the given nullary runtime
2073/// function.
2074llvm::CallSite
2075CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
2076                                         const Twine &name) {
2077  return EmitRuntimeCallOrInvoke(callee, ArrayRef<llvm::Value*>(), name);
2078}
2079
2080/// Emits a call or invoke instruction to the given runtime function.
2081llvm::CallSite
2082CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
2083                                         ArrayRef<llvm::Value*> args,
2084                                         const Twine &name) {
2085  llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
2086  callSite.setCallingConv(getRuntimeCC());
2087  return callSite;
2088}
2089
2090llvm::CallSite
2091CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
2092                                  const Twine &Name) {
2093  return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name);
2094}
2095
2096/// Emits a call or invoke instruction to the given function, depending
2097/// on the current state of the EH stack.
2098llvm::CallSite
2099CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
2100                                  ArrayRef<llvm::Value *> Args,
2101                                  const Twine &Name) {
2102  llvm::BasicBlock *InvokeDest = getInvokeDest();
2103
2104  llvm::Instruction *Inst;
2105  if (!InvokeDest)
2106    Inst = Builder.CreateCall(Callee, Args, Name);
2107  else {
2108    llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
2109    Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name);
2110    EmitBlock(ContBB);
2111  }
2112
2113  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2114  // optimizer it can aggressively ignore unwind edges.
2115  if (CGM.getLangOpts().ObjCAutoRefCount)
2116    AddObjCARCExceptionMetadata(Inst);
2117
2118  return Inst;
2119}
2120
2121static void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo,
2122                            llvm::FunctionType *FTy) {
2123  if (ArgNo < FTy->getNumParams())
2124    assert(Elt->getType() == FTy->getParamType(ArgNo));
2125  else
2126    assert(FTy->isVarArg());
2127  ++ArgNo;
2128}
2129
2130void CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
2131                                       SmallVector<llvm::Value*,16> &Args,
2132                                       llvm::FunctionType *IRFuncTy) {
2133  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2134    unsigned NumElts = AT->getSize().getZExtValue();
2135    QualType EltTy = AT->getElementType();
2136    llvm::Value *Addr = RV.getAggregateAddr();
2137    for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
2138      llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt);
2139      RValue EltRV = convertTempToRValue(EltAddr, EltTy);
2140      ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy);
2141    }
2142  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2143    RecordDecl *RD = RT->getDecl();
2144    assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
2145    LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty);
2146
2147    if (RD->isUnion()) {
2148      const FieldDecl *LargestFD = 0;
2149      CharUnits UnionSize = CharUnits::Zero();
2150
2151      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2152           i != e; ++i) {
2153        const FieldDecl *FD = *i;
2154        assert(!FD->isBitField() &&
2155               "Cannot expand structure with bit-field members.");
2156        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
2157        if (UnionSize < FieldSize) {
2158          UnionSize = FieldSize;
2159          LargestFD = FD;
2160        }
2161      }
2162      if (LargestFD) {
2163        RValue FldRV = EmitRValueForField(LV, LargestFD);
2164        ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy);
2165      }
2166    } else {
2167      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2168           i != e; ++i) {
2169        FieldDecl *FD = *i;
2170
2171        RValue FldRV = EmitRValueForField(LV, FD);
2172        ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy);
2173      }
2174    }
2175  } else if (Ty->isAnyComplexType()) {
2176    ComplexPairTy CV = RV.getComplexVal();
2177    Args.push_back(CV.first);
2178    Args.push_back(CV.second);
2179  } else {
2180    assert(RV.isScalar() &&
2181           "Unexpected non-scalar rvalue during struct expansion.");
2182
2183    // Insert a bitcast as needed.
2184    llvm::Value *V = RV.getScalarVal();
2185    if (Args.size() < IRFuncTy->getNumParams() &&
2186        V->getType() != IRFuncTy->getParamType(Args.size()))
2187      V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size()));
2188
2189    Args.push_back(V);
2190  }
2191}
2192
2193
2194RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
2195                                 llvm::Value *Callee,
2196                                 ReturnValueSlot ReturnValue,
2197                                 const CallArgList &CallArgs,
2198                                 const Decl *TargetDecl,
2199                                 llvm::Instruction **callOrInvoke) {
2200  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
2201  SmallVector<llvm::Value*, 16> Args;
2202
2203  // Handle struct-return functions by passing a pointer to the
2204  // location that we would like to return into.
2205  QualType RetTy = CallInfo.getReturnType();
2206  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
2207
2208  // IRArgNo - Keep track of the argument number in the callee we're looking at.
2209  unsigned IRArgNo = 0;
2210  llvm::FunctionType *IRFuncTy =
2211    cast<llvm::FunctionType>(
2212                  cast<llvm::PointerType>(Callee->getType())->getElementType());
2213
2214  // If the call returns a temporary with struct return, create a temporary
2215  // alloca to hold the result, unless one is given to us.
2216  if (CGM.ReturnTypeUsesSRet(CallInfo)) {
2217    llvm::Value *Value = ReturnValue.getValue();
2218    if (!Value)
2219      Value = CreateMemTemp(RetTy);
2220    Args.push_back(Value);
2221    checkArgMatches(Value, IRArgNo, IRFuncTy);
2222  }
2223
2224  assert(CallInfo.arg_size() == CallArgs.size() &&
2225         "Mismatch between function signature & arguments.");
2226  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
2227  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
2228       I != E; ++I, ++info_it) {
2229    const ABIArgInfo &ArgInfo = info_it->info;
2230    RValue RV = I->RV;
2231
2232    CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty);
2233
2234    // Insert a padding argument to ensure proper alignment.
2235    if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) {
2236      Args.push_back(llvm::UndefValue::get(PaddingType));
2237      ++IRArgNo;
2238    }
2239
2240    switch (ArgInfo.getKind()) {
2241    case ABIArgInfo::Indirect: {
2242      if (RV.isScalar() || RV.isComplex()) {
2243        // Make a temporary alloca to pass the argument.
2244        llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
2245        if (ArgInfo.getIndirectAlign() > AI->getAlignment())
2246          AI->setAlignment(ArgInfo.getIndirectAlign());
2247        Args.push_back(AI);
2248
2249        LValue argLV =
2250          MakeAddrLValue(Args.back(), I->Ty, TypeAlign);
2251
2252        if (RV.isScalar())
2253          EmitStoreOfScalar(RV.getScalarVal(), argLV, /*init*/ true);
2254        else
2255          EmitStoreOfComplex(RV.getComplexVal(), argLV, /*init*/ true);
2256
2257        // Validate argument match.
2258        checkArgMatches(AI, IRArgNo, IRFuncTy);
2259      } else {
2260        // We want to avoid creating an unnecessary temporary+copy here;
2261        // however, we need one in three cases:
2262        // 1. If the argument is not byval, and we are required to copy the
2263        //    source.  (This case doesn't occur on any common architecture.)
2264        // 2. If the argument is byval, RV is not sufficiently aligned, and
2265        //    we cannot force it to be sufficiently aligned.
2266        // 3. If the argument is byval, but RV is located in an address space
2267        //    different than that of the argument (0).
2268        llvm::Value *Addr = RV.getAggregateAddr();
2269        unsigned Align = ArgInfo.getIndirectAlign();
2270        const llvm::DataLayout *TD = &CGM.getDataLayout();
2271        const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace();
2272        const unsigned ArgAddrSpace = (IRArgNo < IRFuncTy->getNumParams() ?
2273          IRFuncTy->getParamType(IRArgNo)->getPointerAddressSpace() : 0);
2274        if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
2275            (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align &&
2276             llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) ||
2277             (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) {
2278          // Create an aligned temporary, and copy to it.
2279          llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
2280          if (Align > AI->getAlignment())
2281            AI->setAlignment(Align);
2282          Args.push_back(AI);
2283          EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
2284
2285          // Validate argument match.
2286          checkArgMatches(AI, IRArgNo, IRFuncTy);
2287        } else {
2288          // Skip the extra memcpy call.
2289          Args.push_back(Addr);
2290
2291          // Validate argument match.
2292          checkArgMatches(Addr, IRArgNo, IRFuncTy);
2293        }
2294      }
2295      break;
2296    }
2297
2298    case ABIArgInfo::Ignore:
2299      break;
2300
2301    case ABIArgInfo::Extend:
2302    case ABIArgInfo::Direct: {
2303      if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
2304          ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
2305          ArgInfo.getDirectOffset() == 0) {
2306        llvm::Value *V;
2307        if (RV.isScalar())
2308          V = RV.getScalarVal();
2309        else
2310          V = Builder.CreateLoad(RV.getAggregateAddr());
2311
2312        // If the argument doesn't match, perform a bitcast to coerce it.  This
2313        // can happen due to trivial type mismatches.
2314        if (IRArgNo < IRFuncTy->getNumParams() &&
2315            V->getType() != IRFuncTy->getParamType(IRArgNo))
2316          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo));
2317        Args.push_back(V);
2318
2319        checkArgMatches(V, IRArgNo, IRFuncTy);
2320        break;
2321      }
2322
2323      // FIXME: Avoid the conversion through memory if possible.
2324      llvm::Value *SrcPtr;
2325      if (RV.isScalar() || RV.isComplex()) {
2326        SrcPtr = CreateMemTemp(I->Ty, "coerce");
2327        LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign);
2328        if (RV.isScalar()) {
2329          EmitStoreOfScalar(RV.getScalarVal(), SrcLV, /*init*/ true);
2330        } else {
2331          EmitStoreOfComplex(RV.getComplexVal(), SrcLV, /*init*/ true);
2332        }
2333      } else
2334        SrcPtr = RV.getAggregateAddr();
2335
2336      // If the value is offset in memory, apply the offset now.
2337      if (unsigned Offs = ArgInfo.getDirectOffset()) {
2338        SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
2339        SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
2340        SrcPtr = Builder.CreateBitCast(SrcPtr,
2341                       llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
2342
2343      }
2344
2345      // If the coerce-to type is a first class aggregate, we flatten it and
2346      // pass the elements. Either way is semantically identical, but fast-isel
2347      // and the optimizer generally likes scalar values better than FCAs.
2348      if (llvm::StructType *STy =
2349            dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
2350        llvm::Type *SrcTy =
2351          cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
2352        uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
2353        uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
2354
2355        // If the source type is smaller than the destination type of the
2356        // coerce-to logic, copy the source value into a temp alloca the size
2357        // of the destination type to allow loading all of it. The bits past
2358        // the source value are left undef.
2359        if (SrcSize < DstSize) {
2360          llvm::AllocaInst *TempAlloca
2361            = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce");
2362          Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0);
2363          SrcPtr = TempAlloca;
2364        } else {
2365          SrcPtr = Builder.CreateBitCast(SrcPtr,
2366                                         llvm::PointerType::getUnqual(STy));
2367        }
2368
2369        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
2370          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
2371          llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
2372          // We don't know what we're loading from.
2373          LI->setAlignment(1);
2374          Args.push_back(LI);
2375
2376          // Validate argument match.
2377          checkArgMatches(LI, IRArgNo, IRFuncTy);
2378        }
2379      } else {
2380        // In the simple case, just pass the coerced loaded value.
2381        Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2382                                         *this));
2383
2384        // Validate argument match.
2385        checkArgMatches(Args.back(), IRArgNo, IRFuncTy);
2386      }
2387
2388      break;
2389    }
2390
2391    case ABIArgInfo::Expand:
2392      ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy);
2393      IRArgNo = Args.size();
2394      break;
2395    }
2396  }
2397
2398  // If the callee is a bitcast of a function to a varargs pointer to function
2399  // type, check to see if we can remove the bitcast.  This handles some cases
2400  // with unprototyped functions.
2401  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
2402    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
2403      llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
2404      llvm::FunctionType *CurFT =
2405        cast<llvm::FunctionType>(CurPT->getElementType());
2406      llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
2407
2408      if (CE->getOpcode() == llvm::Instruction::BitCast &&
2409          ActualFT->getReturnType() == CurFT->getReturnType() &&
2410          ActualFT->getNumParams() == CurFT->getNumParams() &&
2411          ActualFT->getNumParams() == Args.size() &&
2412          (CurFT->isVarArg() || !ActualFT->isVarArg())) {
2413        bool ArgsMatch = true;
2414        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
2415          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
2416            ArgsMatch = false;
2417            break;
2418          }
2419
2420        // Strip the cast if we can get away with it.  This is a nice cleanup,
2421        // but also allows us to inline the function at -O0 if it is marked
2422        // always_inline.
2423        if (ArgsMatch)
2424          Callee = CalleeF;
2425      }
2426    }
2427
2428  unsigned CallingConv;
2429  CodeGen::AttributeListType AttributeList;
2430  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList,
2431                             CallingConv, true);
2432  llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(),
2433                                                     AttributeList);
2434
2435  llvm::BasicBlock *InvokeDest = 0;
2436  if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex,
2437                          llvm::Attribute::NoUnwind))
2438    InvokeDest = getInvokeDest();
2439
2440  llvm::CallSite CS;
2441  if (!InvokeDest) {
2442    CS = Builder.CreateCall(Callee, Args);
2443  } else {
2444    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
2445    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args);
2446    EmitBlock(Cont);
2447  }
2448  if (callOrInvoke)
2449    *callOrInvoke = CS.getInstruction();
2450
2451  CS.setAttributes(Attrs);
2452  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2453
2454  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2455  // optimizer it can aggressively ignore unwind edges.
2456  if (CGM.getLangOpts().ObjCAutoRefCount)
2457    AddObjCARCExceptionMetadata(CS.getInstruction());
2458
2459  // If the call doesn't return, finish the basic block and clear the
2460  // insertion point; this allows the rest of IRgen to discard
2461  // unreachable code.
2462  if (CS.doesNotReturn()) {
2463    Builder.CreateUnreachable();
2464    Builder.ClearInsertionPoint();
2465
2466    // FIXME: For now, emit a dummy basic block because expr emitters in
2467    // generally are not ready to handle emitting expressions at unreachable
2468    // points.
2469    EnsureInsertPoint();
2470
2471    // Return a reasonable RValue.
2472    return GetUndefRValue(RetTy);
2473  }
2474
2475  llvm::Instruction *CI = CS.getInstruction();
2476  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
2477    CI->setName("call");
2478
2479  // Emit any writebacks immediately.  Arguably this should happen
2480  // after any return-value munging.
2481  if (CallArgs.hasWritebacks())
2482    emitWritebacks(*this, CallArgs);
2483
2484  switch (RetAI.getKind()) {
2485  case ABIArgInfo::Indirect:
2486    return convertTempToRValue(Args[0], RetTy);
2487
2488  case ABIArgInfo::Ignore:
2489    // If we are ignoring an argument that had a result, make sure to
2490    // construct the appropriate return value for our caller.
2491    return GetUndefRValue(RetTy);
2492
2493  case ABIArgInfo::Extend:
2494  case ABIArgInfo::Direct: {
2495    llvm::Type *RetIRTy = ConvertType(RetTy);
2496    if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
2497      switch (getEvaluationKind(RetTy)) {
2498      case TEK_Complex: {
2499        llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2500        llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2501        return RValue::getComplex(std::make_pair(Real, Imag));
2502      }
2503      case TEK_Aggregate: {
2504        llvm::Value *DestPtr = ReturnValue.getValue();
2505        bool DestIsVolatile = ReturnValue.isVolatile();
2506
2507        if (!DestPtr) {
2508          DestPtr = CreateMemTemp(RetTy, "agg.tmp");
2509          DestIsVolatile = false;
2510        }
2511        BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
2512        return RValue::getAggregate(DestPtr);
2513      }
2514      case TEK_Scalar: {
2515        // If the argument doesn't match, perform a bitcast to coerce it.  This
2516        // can happen due to trivial type mismatches.
2517        llvm::Value *V = CI;
2518        if (V->getType() != RetIRTy)
2519          V = Builder.CreateBitCast(V, RetIRTy);
2520        return RValue::get(V);
2521      }
2522      }
2523      llvm_unreachable("bad evaluation kind");
2524    }
2525
2526    llvm::Value *DestPtr = ReturnValue.getValue();
2527    bool DestIsVolatile = ReturnValue.isVolatile();
2528
2529    if (!DestPtr) {
2530      DestPtr = CreateMemTemp(RetTy, "coerce");
2531      DestIsVolatile = false;
2532    }
2533
2534    // If the value is offset in memory, apply the offset now.
2535    llvm::Value *StorePtr = DestPtr;
2536    if (unsigned Offs = RetAI.getDirectOffset()) {
2537      StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
2538      StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
2539      StorePtr = Builder.CreateBitCast(StorePtr,
2540                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
2541    }
2542    CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
2543
2544    return convertTempToRValue(DestPtr, RetTy);
2545  }
2546
2547  case ABIArgInfo::Expand:
2548    llvm_unreachable("Invalid ABI kind for return argument");
2549  }
2550
2551  llvm_unreachable("Unhandled ABIArgInfo::Kind");
2552}
2553
2554/* VarArg handling */
2555
2556llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2557  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2558}
2559