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