clang API Documentation

CGObjC.cpp
Go to the documentation of this file.
00001 //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This contains code to emit Objective-C code as LLVM code.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "CGDebugInfo.h"
00015 #include "CGObjCRuntime.h"
00016 #include "CodeGenFunction.h"
00017 #include "CodeGenModule.h"
00018 #include "TargetInfo.h"
00019 #include "clang/AST/ASTContext.h"
00020 #include "clang/AST/DeclObjC.h"
00021 #include "clang/AST/StmtObjC.h"
00022 #include "clang/Basic/Diagnostic.h"
00023 #include "clang/CodeGen/CGFunctionInfo.h"
00024 #include "llvm/ADT/STLExtras.h"
00025 #include "llvm/IR/CallSite.h"
00026 #include "llvm/IR/DataLayout.h"
00027 #include "llvm/IR/InlineAsm.h"
00028 using namespace clang;
00029 using namespace CodeGen;
00030 
00031 typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
00032 static TryEmitResult
00033 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e);
00034 static RValue AdjustRelatedResultType(CodeGenFunction &CGF,
00035                                       QualType ET,
00036                                       const ObjCMethodDecl *Method,
00037                                       RValue Result);
00038 
00039 /// Given the address of a variable of pointer type, find the correct
00040 /// null to store into it.
00041 static llvm::Constant *getNullForVariable(llvm::Value *addr) {
00042   llvm::Type *type =
00043     cast<llvm::PointerType>(addr->getType())->getElementType();
00044   return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
00045 }
00046 
00047 /// Emits an instance of NSConstantString representing the object.
00048 llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
00049 {
00050   llvm::Constant *C = 
00051       CGM.getObjCRuntime().GenerateConstantString(E->getString());
00052   // FIXME: This bitcast should just be made an invariant on the Runtime.
00053   return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
00054 }
00055 
00056 /// EmitObjCBoxedExpr - This routine generates code to call
00057 /// the appropriate expression boxing method. This will either be
00058 /// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:].
00059 ///
00060 llvm::Value *
00061 CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) {
00062   // Generate the correct selector for this literal's concrete type.
00063   const Expr *SubExpr = E->getSubExpr();
00064   // Get the method.
00065   const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod();
00066   assert(BoxingMethod && "BoxingMethod is null");
00067   assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method");
00068   Selector Sel = BoxingMethod->getSelector();
00069   
00070   // Generate a reference to the class pointer, which will be the receiver.
00071   // Assumes that the method was introduced in the class that should be
00072   // messaged (avoids pulling it out of the result type).
00073   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
00074   const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface();
00075   llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl);
00076   
00077   const ParmVarDecl *argDecl = *BoxingMethod->param_begin();
00078   QualType ArgQT = argDecl->getType().getUnqualifiedType();
00079   RValue RV = EmitAnyExpr(SubExpr);
00080   CallArgList Args;
00081   Args.add(RV, ArgQT);
00082 
00083   RValue result = Runtime.GenerateMessageSend(
00084       *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver,
00085       Args, ClassDecl, BoxingMethod);
00086   return Builder.CreateBitCast(result.getScalarVal(), 
00087                                ConvertType(E->getType()));
00088 }
00089 
00090 llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E,
00091                                     const ObjCMethodDecl *MethodWithObjects) {
00092   ASTContext &Context = CGM.getContext();
00093   const ObjCDictionaryLiteral *DLE = nullptr;
00094   const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E);
00095   if (!ALE)
00096     DLE = cast<ObjCDictionaryLiteral>(E);
00097   
00098   // Compute the type of the array we're initializing.
00099   uint64_t NumElements = 
00100     ALE ? ALE->getNumElements() : DLE->getNumElements();
00101   llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
00102                             NumElements);
00103   QualType ElementType = Context.getObjCIdType().withConst();
00104   QualType ElementArrayType 
00105     = Context.getConstantArrayType(ElementType, APNumElements, 
00106                                    ArrayType::Normal, /*IndexTypeQuals=*/0);
00107 
00108   // Allocate the temporary array(s).
00109   llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects");
00110   llvm::Value *Keys = nullptr;
00111   if (DLE)
00112     Keys = CreateMemTemp(ElementArrayType, "keys");
00113   
00114   // In ARC, we may need to do extra work to keep all the keys and
00115   // values alive until after the call.
00116   SmallVector<llvm::Value *, 16> NeededObjects;
00117   bool TrackNeededObjects =
00118     (getLangOpts().ObjCAutoRefCount &&
00119     CGM.getCodeGenOpts().OptimizationLevel != 0);
00120 
00121   // Perform the actual initialialization of the array(s).
00122   for (uint64_t i = 0; i < NumElements; i++) {
00123     if (ALE) {
00124       // Emit the element and store it to the appropriate array slot.
00125       const Expr *Rhs = ALE->getElement(i);
00126       LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i),
00127                                    ElementType,
00128                                    Context.getTypeAlignInChars(Rhs->getType()),
00129                                    Context);
00130 
00131       llvm::Value *value = EmitScalarExpr(Rhs);
00132       EmitStoreThroughLValue(RValue::get(value), LV, true);
00133       if (TrackNeededObjects) {
00134         NeededObjects.push_back(value);
00135       }
00136     } else {      
00137       // Emit the key and store it to the appropriate array slot.
00138       const Expr *Key = DLE->getKeyValueElement(i).Key;
00139       LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i),
00140                                       ElementType,
00141                                     Context.getTypeAlignInChars(Key->getType()),
00142                                       Context);
00143       llvm::Value *keyValue = EmitScalarExpr(Key);
00144       EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
00145 
00146       // Emit the value and store it to the appropriate array slot.
00147       const Expr *Value = DLE->getKeyValueElement(i).Value;  
00148       LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 
00149                                         ElementType,
00150                                   Context.getTypeAlignInChars(Value->getType()),
00151                                         Context);
00152       llvm::Value *valueValue = EmitScalarExpr(Value);
00153       EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
00154       if (TrackNeededObjects) {
00155         NeededObjects.push_back(keyValue);
00156         NeededObjects.push_back(valueValue);
00157       }
00158     }
00159   }
00160   
00161   // Generate the argument list.
00162   CallArgList Args;  
00163   ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
00164   const ParmVarDecl *argDecl = *PI++;
00165   QualType ArgQT = argDecl->getType().getUnqualifiedType();
00166   Args.add(RValue::get(Objects), ArgQT);
00167   if (DLE) {
00168     argDecl = *PI++;
00169     ArgQT = argDecl->getType().getUnqualifiedType();
00170     Args.add(RValue::get(Keys), ArgQT);
00171   }
00172   argDecl = *PI;
00173   ArgQT = argDecl->getType().getUnqualifiedType();
00174   llvm::Value *Count = 
00175     llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
00176   Args.add(RValue::get(Count), ArgQT);
00177 
00178   // Generate a reference to the class pointer, which will be the receiver.
00179   Selector Sel = MethodWithObjects->getSelector();
00180   QualType ResultType = E->getType();
00181   const ObjCObjectPointerType *InterfacePointerType
00182     = ResultType->getAsObjCInterfacePointerType();
00183   ObjCInterfaceDecl *Class 
00184     = InterfacePointerType->getObjectType()->getInterface();
00185   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
00186   llvm::Value *Receiver = Runtime.GetClass(*this, Class);
00187 
00188   // Generate the message send.
00189   RValue result = Runtime.GenerateMessageSend(
00190       *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel,
00191       Receiver, Args, Class, MethodWithObjects);
00192 
00193   // The above message send needs these objects, but in ARC they are
00194   // passed in a buffer that is essentially __unsafe_unretained.
00195   // Therefore we must prevent the optimizer from releasing them until
00196   // after the call.
00197   if (TrackNeededObjects) {
00198     EmitARCIntrinsicUse(NeededObjects);
00199   }
00200 
00201   return Builder.CreateBitCast(result.getScalarVal(), 
00202                                ConvertType(E->getType()));
00203 }
00204 
00205 llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) {
00206   return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod());
00207 }
00208 
00209 llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral(
00210                                             const ObjCDictionaryLiteral *E) {
00211   return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod());
00212 }
00213 
00214 /// Emit a selector.
00215 llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
00216   // Untyped selector.
00217   // Note that this implementation allows for non-constant strings to be passed
00218   // as arguments to @selector().  Currently, the only thing preventing this
00219   // behaviour is the type checking in the front end.
00220   return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
00221 }
00222 
00223 llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
00224   // FIXME: This should pass the Decl not the name.
00225   return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
00226 }
00227 
00228 /// \brief Adjust the type of the result of an Objective-C message send 
00229 /// expression when the method has a related result type.
00230 static RValue AdjustRelatedResultType(CodeGenFunction &CGF,
00231                                       QualType ExpT,
00232                                       const ObjCMethodDecl *Method,
00233                                       RValue Result) {
00234   if (!Method)
00235     return Result;
00236 
00237   if (!Method->hasRelatedResultType() ||
00238       CGF.getContext().hasSameType(ExpT, Method->getReturnType()) ||
00239       !Result.isScalar())
00240     return Result;
00241   
00242   // We have applied a related result type. Cast the rvalue appropriately.
00243   return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
00244                                                CGF.ConvertType(ExpT)));
00245 }
00246 
00247 /// Decide whether to extend the lifetime of the receiver of a
00248 /// returns-inner-pointer message.
00249 static bool
00250 shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
00251   switch (message->getReceiverKind()) {
00252 
00253   // For a normal instance message, we should extend unless the
00254   // receiver is loaded from a variable with precise lifetime.
00255   case ObjCMessageExpr::Instance: {
00256     const Expr *receiver = message->getInstanceReceiver();
00257     const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
00258     if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
00259     receiver = ice->getSubExpr()->IgnoreParens();
00260 
00261     // Only __strong variables.
00262     if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
00263       return true;
00264 
00265     // All ivars and fields have precise lifetime.
00266     if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
00267       return false;
00268 
00269     // Otherwise, check for variables.
00270     const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
00271     if (!declRef) return true;
00272     const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
00273     if (!var) return true;
00274 
00275     // All variables have precise lifetime except local variables with
00276     // automatic storage duration that aren't specially marked.
00277     return (var->hasLocalStorage() &&
00278             !var->hasAttr<ObjCPreciseLifetimeAttr>());
00279   }
00280 
00281   case ObjCMessageExpr::Class:
00282   case ObjCMessageExpr::SuperClass:
00283     // It's never necessary for class objects.
00284     return false;
00285 
00286   case ObjCMessageExpr::SuperInstance:
00287     // We generally assume that 'self' lives throughout a method call.
00288     return false;
00289   }
00290 
00291   llvm_unreachable("invalid receiver kind");
00292 }
00293 
00294 RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
00295                                             ReturnValueSlot Return) {
00296   // Only the lookup mechanism and first two arguments of the method
00297   // implementation vary between runtimes.  We can get the receiver and
00298   // arguments in generic code.
00299 
00300   bool isDelegateInit = E->isDelegateInitCall();
00301 
00302   const ObjCMethodDecl *method = E->getMethodDecl();
00303 
00304   // We don't retain the receiver in delegate init calls, and this is
00305   // safe because the receiver value is always loaded from 'self',
00306   // which we zero out.  We don't want to Block_copy block receivers,
00307   // though.
00308   bool retainSelf =
00309     (!isDelegateInit &&
00310      CGM.getLangOpts().ObjCAutoRefCount &&
00311      method &&
00312      method->hasAttr<NSConsumesSelfAttr>());
00313 
00314   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
00315   bool isSuperMessage = false;
00316   bool isClassMessage = false;
00317   ObjCInterfaceDecl *OID = nullptr;
00318   // Find the receiver
00319   QualType ReceiverType;
00320   llvm::Value *Receiver = nullptr;
00321   switch (E->getReceiverKind()) {
00322   case ObjCMessageExpr::Instance:
00323     ReceiverType = E->getInstanceReceiver()->getType();
00324     if (retainSelf) {
00325       TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
00326                                                    E->getInstanceReceiver());
00327       Receiver = ter.getPointer();
00328       if (ter.getInt()) retainSelf = false;
00329     } else
00330       Receiver = EmitScalarExpr(E->getInstanceReceiver());
00331     break;
00332 
00333   case ObjCMessageExpr::Class: {
00334     ReceiverType = E->getClassReceiver();
00335     const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
00336     assert(ObjTy && "Invalid Objective-C class message send");
00337     OID = ObjTy->getInterface();
00338     assert(OID && "Invalid Objective-C class message send");
00339     Receiver = Runtime.GetClass(*this, OID);
00340     isClassMessage = true;
00341     break;
00342   }
00343 
00344   case ObjCMessageExpr::SuperInstance:
00345     ReceiverType = E->getSuperType();
00346     Receiver = LoadObjCSelf();
00347     isSuperMessage = true;
00348     break;
00349 
00350   case ObjCMessageExpr::SuperClass:
00351     ReceiverType = E->getSuperType();
00352     Receiver = LoadObjCSelf();
00353     isSuperMessage = true;
00354     isClassMessage = true;
00355     break;
00356   }
00357 
00358   if (retainSelf)
00359     Receiver = EmitARCRetainNonBlock(Receiver);
00360 
00361   // In ARC, we sometimes want to "extend the lifetime"
00362   // (i.e. retain+autorelease) of receivers of returns-inner-pointer
00363   // messages.
00364   if (getLangOpts().ObjCAutoRefCount && method &&
00365       method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
00366       shouldExtendReceiverForInnerPointerMessage(E))
00367     Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
00368 
00369   QualType ResultType = method ? method->getReturnType() : E->getType();
00370 
00371   CallArgList Args;
00372   EmitCallArgs(Args, method, E->arg_begin(), E->arg_end());
00373 
00374   // For delegate init calls in ARC, do an unsafe store of null into
00375   // self.  This represents the call taking direct ownership of that
00376   // value.  We have to do this after emitting the other call
00377   // arguments because they might also reference self, but we don't
00378   // have to worry about any of them modifying self because that would
00379   // be an undefined read and write of an object in unordered
00380   // expressions.
00381   if (isDelegateInit) {
00382     assert(getLangOpts().ObjCAutoRefCount &&
00383            "delegate init calls should only be marked in ARC");
00384 
00385     // Do an unsafe store of null into self.
00386     llvm::Value *selfAddr =
00387       LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
00388     assert(selfAddr && "no self entry for a delegate init call?");
00389 
00390     Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
00391   }
00392 
00393   RValue result;
00394   if (isSuperMessage) {
00395     // super is only valid in an Objective-C method
00396     const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
00397     bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
00398     result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
00399                                               E->getSelector(),
00400                                               OMD->getClassInterface(),
00401                                               isCategoryImpl,
00402                                               Receiver,
00403                                               isClassMessage,
00404                                               Args,
00405                                               method);
00406   } else {
00407     result = Runtime.GenerateMessageSend(*this, Return, ResultType,
00408                                          E->getSelector(),
00409                                          Receiver, Args, OID,
00410                                          method);
00411   }
00412 
00413   // For delegate init calls in ARC, implicitly store the result of
00414   // the call back into self.  This takes ownership of the value.
00415   if (isDelegateInit) {
00416     llvm::Value *selfAddr =
00417       LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
00418     llvm::Value *newSelf = result.getScalarVal();
00419 
00420     // The delegate return type isn't necessarily a matching type; in
00421     // fact, it's quite likely to be 'id'.
00422     llvm::Type *selfTy =
00423       cast<llvm::PointerType>(selfAddr->getType())->getElementType();
00424     newSelf = Builder.CreateBitCast(newSelf, selfTy);
00425 
00426     Builder.CreateStore(newSelf, selfAddr);
00427   }
00428 
00429   return AdjustRelatedResultType(*this, E->getType(), method, result);
00430 }
00431 
00432 namespace {
00433 struct FinishARCDealloc : EHScopeStack::Cleanup {
00434   void Emit(CodeGenFunction &CGF, Flags flags) override {
00435     const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
00436 
00437     const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
00438     const ObjCInterfaceDecl *iface = impl->getClassInterface();
00439     if (!iface->getSuperClass()) return;
00440 
00441     bool isCategory = isa<ObjCCategoryImplDecl>(impl);
00442 
00443     // Call [super dealloc] if we have a superclass.
00444     llvm::Value *self = CGF.LoadObjCSelf();
00445 
00446     CallArgList args;
00447     CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
00448                                                       CGF.getContext().VoidTy,
00449                                                       method->getSelector(),
00450                                                       iface,
00451                                                       isCategory,
00452                                                       self,
00453                                                       /*is class msg*/ false,
00454                                                       args,
00455                                                       method);
00456   }
00457 };
00458 }
00459 
00460 /// StartObjCMethod - Begin emission of an ObjCMethod. This generates
00461 /// the LLVM function and sets the other context used by
00462 /// CodeGenFunction.
00463 void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
00464                                       const ObjCContainerDecl *CD,
00465                                       SourceLocation StartLoc) {
00466   FunctionArgList args;
00467   // Check if we should generate debug info for this method.
00468   if (OMD->hasAttr<NoDebugAttr>())
00469     DebugInfo = nullptr; // disable debug info indefinitely for this function
00470 
00471   llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
00472 
00473   const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
00474   CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
00475 
00476   args.push_back(OMD->getSelfDecl());
00477   args.push_back(OMD->getCmdDecl());
00478 
00479   for (const auto *PI : OMD->params())
00480     args.push_back(PI);
00481 
00482   CurGD = OMD;
00483 
00484   StartFunction(OMD, OMD->getReturnType(), Fn, FI, args,
00485                 OMD->getLocation(), StartLoc);
00486 
00487   // In ARC, certain methods get an extra cleanup.
00488   if (CGM.getLangOpts().ObjCAutoRefCount &&
00489       OMD->isInstanceMethod() &&
00490       OMD->getSelector().isUnarySelector()) {
00491     const IdentifierInfo *ident = 
00492       OMD->getSelector().getIdentifierInfoForSlot(0);
00493     if (ident->isStr("dealloc"))
00494       EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
00495   }
00496 }
00497 
00498 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
00499                                               LValue lvalue, QualType type);
00500 
00501 /// Generate an Objective-C method.  An Objective-C method is a C function with
00502 /// its pointer, name, and types registered in the class struture.
00503 void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
00504   StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
00505   PGO.assignRegionCounters(OMD, CurFn);
00506   assert(isa<CompoundStmt>(OMD->getBody()));
00507   RegionCounter Cnt = getPGORegionCounter(OMD->getBody());
00508   Cnt.beginRegion(Builder);
00509   EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));
00510   FinishFunction(OMD->getBodyRBrace());
00511   PGO.emitInstrumentationData();
00512   PGO.destroyRegionCounters();
00513 }
00514 
00515 /// emitStructGetterCall - Call the runtime function to load a property
00516 /// into the return value slot.
00517 static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 
00518                                  bool isAtomic, bool hasStrong) {
00519   ASTContext &Context = CGF.getContext();
00520 
00521   llvm::Value *src =
00522     CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(),
00523                           ivar, 0).getAddress();
00524 
00525   // objc_copyStruct (ReturnValue, &structIvar, 
00526   //                  sizeof (Type of Ivar), isAtomic, false);
00527   CallArgList args;
00528 
00529   llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
00530   args.add(RValue::get(dest), Context.VoidPtrTy);
00531 
00532   src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
00533   args.add(RValue::get(src), Context.VoidPtrTy);
00534 
00535   CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
00536   args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
00537   args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
00538   args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
00539 
00540   llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
00541   CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args,
00542                                                       FunctionType::ExtInfo(),
00543                                                       RequiredArgs::All),
00544                fn, ReturnValueSlot(), args);
00545 }
00546 
00547 /// Determine whether the given architecture supports unaligned atomic
00548 /// accesses.  They don't have to be fast, just faster than a function
00549 /// call and a mutex.
00550 static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
00551   // FIXME: Allow unaligned atomic load/store on x86.  (It is not
00552   // currently supported by the backend.)
00553   return 0;
00554 }
00555 
00556 /// Return the maximum size that permits atomic accesses for the given
00557 /// architecture.
00558 static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
00559                                         llvm::Triple::ArchType arch) {
00560   // ARM has 8-byte atomic accesses, but it's not clear whether we
00561   // want to rely on them here.
00562 
00563   // In the default case, just assume that any size up to a pointer is
00564   // fine given adequate alignment.
00565   return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
00566 }
00567 
00568 namespace {
00569   class PropertyImplStrategy {
00570   public:
00571     enum StrategyKind {
00572       /// The 'native' strategy is to use the architecture's provided
00573       /// reads and writes.
00574       Native,
00575 
00576       /// Use objc_setProperty and objc_getProperty.
00577       GetSetProperty,
00578 
00579       /// Use objc_setProperty for the setter, but use expression
00580       /// evaluation for the getter.
00581       SetPropertyAndExpressionGet,
00582 
00583       /// Use objc_copyStruct.
00584       CopyStruct,
00585 
00586       /// The 'expression' strategy is to emit normal assignment or
00587       /// lvalue-to-rvalue expressions.
00588       Expression
00589     };
00590 
00591     StrategyKind getKind() const { return StrategyKind(Kind); }
00592 
00593     bool hasStrongMember() const { return HasStrong; }
00594     bool isAtomic() const { return IsAtomic; }
00595     bool isCopy() const { return IsCopy; }
00596 
00597     CharUnits getIvarSize() const { return IvarSize; }
00598     CharUnits getIvarAlignment() const { return IvarAlignment; }
00599 
00600     PropertyImplStrategy(CodeGenModule &CGM,
00601                          const ObjCPropertyImplDecl *propImpl);
00602 
00603   private:
00604     unsigned Kind : 8;
00605     unsigned IsAtomic : 1;
00606     unsigned IsCopy : 1;
00607     unsigned HasStrong : 1;
00608 
00609     CharUnits IvarSize;
00610     CharUnits IvarAlignment;
00611   };
00612 }
00613 
00614 /// Pick an implementation strategy for the given property synthesis.
00615 PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
00616                                      const ObjCPropertyImplDecl *propImpl) {
00617   const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
00618   ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
00619 
00620   IsCopy = (setterKind == ObjCPropertyDecl::Copy);
00621   IsAtomic = prop->isAtomic();
00622   HasStrong = false; // doesn't matter here.
00623 
00624   // Evaluate the ivar's size and alignment.
00625   ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
00626   QualType ivarType = ivar->getType();
00627   std::tie(IvarSize, IvarAlignment) =
00628       CGM.getContext().getTypeInfoInChars(ivarType);
00629 
00630   // If we have a copy property, we always have to use getProperty/setProperty.
00631   // TODO: we could actually use setProperty and an expression for non-atomics.
00632   if (IsCopy) {
00633     Kind = GetSetProperty;
00634     return;
00635   }
00636 
00637   // Handle retain.
00638   if (setterKind == ObjCPropertyDecl::Retain) {
00639     // In GC-only, there's nothing special that needs to be done.
00640     if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
00641       // fallthrough
00642 
00643     // In ARC, if the property is non-atomic, use expression emission,
00644     // which translates to objc_storeStrong.  This isn't required, but
00645     // it's slightly nicer.
00646     } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
00647       // Using standard expression emission for the setter is only
00648       // acceptable if the ivar is __strong, which won't be true if
00649       // the property is annotated with __attribute__((NSObject)).
00650       // TODO: falling all the way back to objc_setProperty here is
00651       // just laziness, though;  we could still use objc_storeStrong
00652       // if we hacked it right.
00653       if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
00654         Kind = Expression;
00655       else
00656         Kind = SetPropertyAndExpressionGet;
00657       return;
00658 
00659     // Otherwise, we need to at least use setProperty.  However, if
00660     // the property isn't atomic, we can use normal expression
00661     // emission for the getter.
00662     } else if (!IsAtomic) {
00663       Kind = SetPropertyAndExpressionGet;
00664       return;
00665 
00666     // Otherwise, we have to use both setProperty and getProperty.
00667     } else {
00668       Kind = GetSetProperty;
00669       return;
00670     }
00671   }
00672 
00673   // If we're not atomic, just use expression accesses.
00674   if (!IsAtomic) {
00675     Kind = Expression;
00676     return;
00677   }
00678 
00679   // Properties on bitfield ivars need to be emitted using expression
00680   // accesses even if they're nominally atomic.
00681   if (ivar->isBitField()) {
00682     Kind = Expression;
00683     return;
00684   }
00685 
00686   // GC-qualified or ARC-qualified ivars need to be emitted as
00687   // expressions.  This actually works out to being atomic anyway,
00688   // except for ARC __strong, but that should trigger the above code.
00689   if (ivarType.hasNonTrivialObjCLifetime() ||
00690       (CGM.getLangOpts().getGC() &&
00691        CGM.getContext().getObjCGCAttrKind(ivarType))) {
00692     Kind = Expression;
00693     return;
00694   }
00695 
00696   // Compute whether the ivar has strong members.
00697   if (CGM.getLangOpts().getGC())
00698     if (const RecordType *recordType = ivarType->getAs<RecordType>())
00699       HasStrong = recordType->getDecl()->hasObjectMember();
00700 
00701   // We can never access structs with object members with a native
00702   // access, because we need to use write barriers.  This is what
00703   // objc_copyStruct is for.
00704   if (HasStrong) {
00705     Kind = CopyStruct;
00706     return;
00707   }
00708 
00709   // Otherwise, this is target-dependent and based on the size and
00710   // alignment of the ivar.
00711 
00712   // If the size of the ivar is not a power of two, give up.  We don't
00713   // want to get into the business of doing compare-and-swaps.
00714   if (!IvarSize.isPowerOfTwo()) {
00715     Kind = CopyStruct;
00716     return;
00717   }
00718 
00719   llvm::Triple::ArchType arch =
00720     CGM.getTarget().getTriple().getArch();
00721 
00722   // Most architectures require memory to fit within a single cache
00723   // line, so the alignment has to be at least the size of the access.
00724   // Otherwise we have to grab a lock.
00725   if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
00726     Kind = CopyStruct;
00727     return;
00728   }
00729 
00730   // If the ivar's size exceeds the architecture's maximum atomic
00731   // access size, we have to use CopyStruct.
00732   if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
00733     Kind = CopyStruct;
00734     return;
00735   }
00736 
00737   // Otherwise, we can use native loads and stores.
00738   Kind = Native;
00739 }
00740 
00741 /// \brief Generate an Objective-C property getter function.
00742 ///
00743 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
00744 /// is illegal within a category.
00745 void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
00746                                          const ObjCPropertyImplDecl *PID) {
00747   llvm::Constant *AtomicHelperFn =
00748       CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID);
00749   const ObjCPropertyDecl *PD = PID->getPropertyDecl();
00750   ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
00751   assert(OMD && "Invalid call to generate getter (empty method)");
00752   StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart());
00753 
00754   generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
00755 
00756   FinishFunction();
00757 }
00758 
00759 static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
00760   const Expr *getter = propImpl->getGetterCXXConstructor();
00761   if (!getter) return true;
00762 
00763   // Sema only makes only of these when the ivar has a C++ class type,
00764   // so the form is pretty constrained.
00765 
00766   // If the property has a reference type, we might just be binding a
00767   // reference, in which case the result will be a gl-value.  We should
00768   // treat this as a non-trivial operation.
00769   if (getter->isGLValue())
00770     return false;
00771 
00772   // If we selected a trivial copy-constructor, we're okay.
00773   if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
00774     return (construct->getConstructor()->isTrivial());
00775 
00776   // The constructor might require cleanups (in which case it's never
00777   // trivial).
00778   assert(isa<ExprWithCleanups>(getter));
00779   return false;
00780 }
00781 
00782 /// emitCPPObjectAtomicGetterCall - Call the runtime function to 
00783 /// copy the ivar into the resturn slot.
00784 static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 
00785                                           llvm::Value *returnAddr,
00786                                           ObjCIvarDecl *ivar,
00787                                           llvm::Constant *AtomicHelperFn) {
00788   // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
00789   //                           AtomicHelperFn);
00790   CallArgList args;
00791   
00792   // The 1st argument is the return Slot.
00793   args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
00794   
00795   // The 2nd argument is the address of the ivar.
00796   llvm::Value *ivarAddr = 
00797   CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 
00798                         CGF.LoadObjCSelf(), ivar, 0).getAddress();
00799   ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
00800   args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
00801   
00802   // Third argument is the helper function.
00803   args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
00804   
00805   llvm::Value *copyCppAtomicObjectFn = 
00806     CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction();
00807   CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
00808                                                       args,
00809                                                       FunctionType::ExtInfo(),
00810                                                       RequiredArgs::All),
00811                copyCppAtomicObjectFn, ReturnValueSlot(), args);
00812 }
00813 
00814 void
00815 CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
00816                                         const ObjCPropertyImplDecl *propImpl,
00817                                         const ObjCMethodDecl *GetterMethodDecl,
00818                                         llvm::Constant *AtomicHelperFn) {
00819   // If there's a non-trivial 'get' expression, we just have to emit that.
00820   if (!hasTrivialGetExpr(propImpl)) {
00821     if (!AtomicHelperFn) {
00822       ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(),
00823                      /*nrvo*/ nullptr);
00824       EmitReturnStmt(ret);
00825     }
00826     else {
00827       ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
00828       emitCPPObjectAtomicGetterCall(*this, ReturnValue, 
00829                                     ivar, AtomicHelperFn);
00830     }
00831     return;
00832   }
00833 
00834   const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
00835   QualType propType = prop->getType();
00836   ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl();
00837 
00838   ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();  
00839 
00840   // Pick an implementation strategy.
00841   PropertyImplStrategy strategy(CGM, propImpl);
00842   switch (strategy.getKind()) {
00843   case PropertyImplStrategy::Native: {
00844     // We don't need to do anything for a zero-size struct.
00845     if (strategy.getIvarSize().isZero())
00846       return;
00847 
00848     LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
00849 
00850     // Currently, all atomic accesses have to be through integer
00851     // types, so there's no point in trying to pick a prettier type.
00852     llvm::Type *bitcastType =
00853       llvm::Type::getIntNTy(getLLVMContext(),
00854                             getContext().toBits(strategy.getIvarSize()));
00855     bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
00856 
00857     // Perform an atomic load.  This does not impose ordering constraints.
00858     llvm::Value *ivarAddr = LV.getAddress();
00859     ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
00860     llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
00861     load->setAlignment(strategy.getIvarAlignment().getQuantity());
00862     load->setAtomic(llvm::Unordered);
00863 
00864     // Store that value into the return address.  Doing this with a
00865     // bitcast is likely to produce some pretty ugly IR, but it's not
00866     // the *most* terrible thing in the world.
00867     Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType));
00868 
00869     // Make sure we don't do an autorelease.
00870     AutoreleaseResult = false;
00871     return;
00872   }
00873 
00874   case PropertyImplStrategy::GetSetProperty: {
00875     llvm::Value *getPropertyFn =
00876       CGM.getObjCRuntime().GetPropertyGetFunction();
00877     if (!getPropertyFn) {
00878       CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
00879       return;
00880     }
00881 
00882     // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
00883     // FIXME: Can't this be simpler? This might even be worse than the
00884     // corresponding gcc code.
00885     llvm::Value *cmd =
00886       Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd");
00887     llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
00888     llvm::Value *ivarOffset =
00889       EmitIvarOffset(classImpl->getClassInterface(), ivar);
00890 
00891     CallArgList args;
00892     args.add(RValue::get(self), getContext().getObjCIdType());
00893     args.add(RValue::get(cmd), getContext().getObjCSelType());
00894     args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
00895     args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
00896              getContext().BoolTy);
00897 
00898     // FIXME: We shouldn't need to get the function info here, the
00899     // runtime already should have computed it to build the function.
00900     llvm::Instruction *CallInstruction;
00901     RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args,
00902                                                        FunctionType::ExtInfo(),
00903                                                             RequiredArgs::All),
00904                          getPropertyFn, ReturnValueSlot(), args, nullptr,
00905                          &CallInstruction);
00906     if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction))
00907       call->setTailCall();
00908 
00909     // We need to fix the type here. Ivars with copy & retain are
00910     // always objects so we don't need to worry about complex or
00911     // aggregates.
00912     RV = RValue::get(Builder.CreateBitCast(
00913         RV.getScalarVal(),
00914         getTypes().ConvertType(getterMethod->getReturnType())));
00915 
00916     EmitReturnOfRValue(RV, propType);
00917 
00918     // objc_getProperty does an autorelease, so we should suppress ours.
00919     AutoreleaseResult = false;
00920 
00921     return;
00922   }
00923 
00924   case PropertyImplStrategy::CopyStruct:
00925     emitStructGetterCall(*this, ivar, strategy.isAtomic(),
00926                          strategy.hasStrongMember());
00927     return;
00928 
00929   case PropertyImplStrategy::Expression:
00930   case PropertyImplStrategy::SetPropertyAndExpressionGet: {
00931     LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
00932 
00933     QualType ivarType = ivar->getType();
00934     switch (getEvaluationKind(ivarType)) {
00935     case TEK_Complex: {
00936       ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation());
00937       EmitStoreOfComplex(pair,
00938                          MakeNaturalAlignAddrLValue(ReturnValue, ivarType),
00939                          /*init*/ true);
00940       return;
00941     }
00942     case TEK_Aggregate:
00943       // The return value slot is guaranteed to not be aliased, but
00944       // that's not necessarily the same as "on the stack", so
00945       // we still potentially need objc_memmove_collectable.
00946       EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType);
00947       return;
00948     case TEK_Scalar: {
00949       llvm::Value *value;
00950       if (propType->isReferenceType()) {
00951         value = LV.getAddress();
00952       } else {
00953         // We want to load and autoreleaseReturnValue ARC __weak ivars.
00954         if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
00955           value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
00956 
00957         // Otherwise we want to do a simple load, suppressing the
00958         // final autorelease.
00959         } else {
00960           value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal();
00961           AutoreleaseResult = false;
00962         }
00963 
00964         value = Builder.CreateBitCast(value, ConvertType(propType));
00965         value = Builder.CreateBitCast(
00966             value, ConvertType(GetterMethodDecl->getReturnType()));
00967       }
00968       
00969       EmitReturnOfRValue(RValue::get(value), propType);
00970       return;
00971     }
00972     }
00973     llvm_unreachable("bad evaluation kind");
00974   }
00975 
00976   }
00977   llvm_unreachable("bad @property implementation strategy!");
00978 }
00979 
00980 /// emitStructSetterCall - Call the runtime function to store the value
00981 /// from the first formal parameter into the given ivar.
00982 static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
00983                                  ObjCIvarDecl *ivar) {
00984   // objc_copyStruct (&structIvar, &Arg, 
00985   //                  sizeof (struct something), true, false);
00986   CallArgList args;
00987 
00988   // The first argument is the address of the ivar.
00989   llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
00990                                                 CGF.LoadObjCSelf(), ivar, 0)
00991     .getAddress();
00992   ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
00993   args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
00994 
00995   // The second argument is the address of the parameter variable.
00996   ParmVarDecl *argVar = *OMD->param_begin();
00997   DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 
00998                      VK_LValue, SourceLocation());
00999   llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
01000   argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
01001   args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
01002 
01003   // The third argument is the sizeof the type.
01004   llvm::Value *size =
01005     CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
01006   args.add(RValue::get(size), CGF.getContext().getSizeType());
01007 
01008   // The fourth argument is the 'isAtomic' flag.
01009   args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
01010 
01011   // The fifth argument is the 'hasStrong' flag.
01012   // FIXME: should this really always be false?
01013   args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
01014 
01015   llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
01016   CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
01017                                                       args,
01018                                                       FunctionType::ExtInfo(),
01019                                                       RequiredArgs::All),
01020                copyStructFn, ReturnValueSlot(), args);
01021 }
01022 
01023 /// emitCPPObjectAtomicSetterCall - Call the runtime function to store 
01024 /// the value from the first formal parameter into the given ivar, using 
01025 /// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
01026 static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 
01027                                           ObjCMethodDecl *OMD,
01028                                           ObjCIvarDecl *ivar,
01029                                           llvm::Constant *AtomicHelperFn) {
01030   // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 
01031   //                           AtomicHelperFn);
01032   CallArgList args;
01033   
01034   // The first argument is the address of the ivar.
01035   llvm::Value *ivarAddr = 
01036     CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 
01037                           CGF.LoadObjCSelf(), ivar, 0).getAddress();
01038   ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
01039   args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
01040   
01041   // The second argument is the address of the parameter variable.
01042   ParmVarDecl *argVar = *OMD->param_begin();
01043   DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 
01044                      VK_LValue, SourceLocation());
01045   llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
01046   argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
01047   args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
01048   
01049   // Third argument is the helper function.
01050   args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
01051   
01052   llvm::Value *copyCppAtomicObjectFn = 
01053     CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction();
01054   CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
01055                                                       args,
01056                                                       FunctionType::ExtInfo(),
01057                                                       RequiredArgs::All),
01058                copyCppAtomicObjectFn, ReturnValueSlot(), args);
01059 }
01060 
01061 
01062 static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
01063   Expr *setter = PID->getSetterCXXAssignment();
01064   if (!setter) return true;
01065 
01066   // Sema only makes only of these when the ivar has a C++ class type,
01067   // so the form is pretty constrained.
01068 
01069   // An operator call is trivial if the function it calls is trivial.
01070   // This also implies that there's nothing non-trivial going on with
01071   // the arguments, because operator= can only be trivial if it's a
01072   // synthesized assignment operator and therefore both parameters are
01073   // references.
01074   if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
01075     if (const FunctionDecl *callee
01076           = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
01077       if (callee->isTrivial())
01078         return true;
01079     return false;
01080   }
01081 
01082   assert(isa<ExprWithCleanups>(setter));
01083   return false;
01084 }
01085 
01086 static bool UseOptimizedSetter(CodeGenModule &CGM) {
01087   if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
01088     return false;
01089   return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter();
01090 }
01091 
01092 void
01093 CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
01094                                         const ObjCPropertyImplDecl *propImpl,
01095                                         llvm::Constant *AtomicHelperFn) {
01096   const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
01097   ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
01098   ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl();
01099   
01100   // Just use the setter expression if Sema gave us one and it's
01101   // non-trivial.
01102   if (!hasTrivialSetExpr(propImpl)) {
01103     if (!AtomicHelperFn)
01104       // If non-atomic, assignment is called directly.
01105       EmitStmt(propImpl->getSetterCXXAssignment());
01106     else
01107       // If atomic, assignment is called via a locking api.
01108       emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
01109                                     AtomicHelperFn);
01110     return;
01111   }
01112 
01113   PropertyImplStrategy strategy(CGM, propImpl);
01114   switch (strategy.getKind()) {
01115   case PropertyImplStrategy::Native: {
01116     // We don't need to do anything for a zero-size struct.
01117     if (strategy.getIvarSize().isZero())
01118       return;
01119 
01120     llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()];
01121 
01122     LValue ivarLValue =
01123       EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
01124     llvm::Value *ivarAddr = ivarLValue.getAddress();
01125 
01126     // Currently, all atomic accesses have to be through integer
01127     // types, so there's no point in trying to pick a prettier type.
01128     llvm::Type *bitcastType =
01129       llvm::Type::getIntNTy(getLLVMContext(),
01130                             getContext().toBits(strategy.getIvarSize()));
01131     bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
01132 
01133     // Cast both arguments to the chosen operation type.
01134     argAddr = Builder.CreateBitCast(argAddr, bitcastType);
01135     ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
01136 
01137     // This bitcast load is likely to cause some nasty IR.
01138     llvm::Value *load = Builder.CreateLoad(argAddr);
01139 
01140     // Perform an atomic store.  There are no memory ordering requirements.
01141     llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
01142     store->setAlignment(strategy.getIvarAlignment().getQuantity());
01143     store->setAtomic(llvm::Unordered);
01144     return;
01145   }
01146 
01147   case PropertyImplStrategy::GetSetProperty:
01148   case PropertyImplStrategy::SetPropertyAndExpressionGet: {
01149 
01150     llvm::Value *setOptimizedPropertyFn = nullptr;
01151     llvm::Value *setPropertyFn = nullptr;
01152     if (UseOptimizedSetter(CGM)) {
01153       // 10.8 and iOS 6.0 code and GC is off
01154       setOptimizedPropertyFn = 
01155         CGM.getObjCRuntime()
01156            .GetOptimizedPropertySetFunction(strategy.isAtomic(),
01157                                             strategy.isCopy());
01158       if (!setOptimizedPropertyFn) {
01159         CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
01160         return;
01161       }
01162     }
01163     else {
01164       setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
01165       if (!setPropertyFn) {
01166         CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
01167         return;
01168       }
01169     }
01170    
01171     // Emit objc_setProperty((id) self, _cmd, offset, arg,
01172     //                       <is-atomic>, <is-copy>).
01173     llvm::Value *cmd =
01174       Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]);
01175     llvm::Value *self =
01176       Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
01177     llvm::Value *ivarOffset =
01178       EmitIvarOffset(classImpl->getClassInterface(), ivar);
01179     llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()];
01180     arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy);
01181 
01182     CallArgList args;
01183     args.add(RValue::get(self), getContext().getObjCIdType());
01184     args.add(RValue::get(cmd), getContext().getObjCSelType());
01185     if (setOptimizedPropertyFn) {
01186       args.add(RValue::get(arg), getContext().getObjCIdType());
01187       args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
01188       EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args,
01189                                                   FunctionType::ExtInfo(),
01190                                                   RequiredArgs::All),
01191                setOptimizedPropertyFn, ReturnValueSlot(), args);
01192     } else {
01193       args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
01194       args.add(RValue::get(arg), getContext().getObjCIdType());
01195       args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
01196                getContext().BoolTy);
01197       args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
01198                getContext().BoolTy);
01199       // FIXME: We shouldn't need to get the function info here, the runtime
01200       // already should have computed it to build the function.
01201       EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args,
01202                                                   FunctionType::ExtInfo(),
01203                                                   RequiredArgs::All),
01204                setPropertyFn, ReturnValueSlot(), args);
01205     }
01206     
01207     return;
01208   }
01209 
01210   case PropertyImplStrategy::CopyStruct:
01211     emitStructSetterCall(*this, setterMethod, ivar);
01212     return;
01213 
01214   case PropertyImplStrategy::Expression:
01215     break;
01216   }
01217 
01218   // Otherwise, fake up some ASTs and emit a normal assignment.
01219   ValueDecl *selfDecl = setterMethod->getSelfDecl();
01220   DeclRefExpr self(selfDecl, false, selfDecl->getType(),
01221                    VK_LValue, SourceLocation());
01222   ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
01223                             selfDecl->getType(), CK_LValueToRValue, &self,
01224                             VK_RValue);
01225   ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
01226                           SourceLocation(), SourceLocation(),
01227                           &selfLoad, true, true);
01228 
01229   ParmVarDecl *argDecl = *setterMethod->param_begin();
01230   QualType argType = argDecl->getType().getNonReferenceType();
01231   DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation());
01232   ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
01233                            argType.getUnqualifiedType(), CK_LValueToRValue,
01234                            &arg, VK_RValue);
01235     
01236   // The property type can differ from the ivar type in some situations with
01237   // Objective-C pointer types, we can always bit cast the RHS in these cases.
01238   // The following absurdity is just to ensure well-formed IR.
01239   CastKind argCK = CK_NoOp;
01240   if (ivarRef.getType()->isObjCObjectPointerType()) {
01241     if (argLoad.getType()->isObjCObjectPointerType())
01242       argCK = CK_BitCast;
01243     else if (argLoad.getType()->isBlockPointerType())
01244       argCK = CK_BlockPointerToObjCPointerCast;
01245     else
01246       argCK = CK_CPointerToObjCPointerCast;
01247   } else if (ivarRef.getType()->isBlockPointerType()) {
01248      if (argLoad.getType()->isBlockPointerType())
01249       argCK = CK_BitCast;
01250     else
01251       argCK = CK_AnyPointerToBlockPointerCast;
01252   } else if (ivarRef.getType()->isPointerType()) {
01253     argCK = CK_BitCast;
01254   }
01255   ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
01256                            ivarRef.getType(), argCK, &argLoad,
01257                            VK_RValue);
01258   Expr *finalArg = &argLoad;
01259   if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
01260                                            argLoad.getType()))
01261     finalArg = &argCast;
01262 
01263 
01264   BinaryOperator assign(&ivarRef, finalArg, BO_Assign,
01265                         ivarRef.getType(), VK_RValue, OK_Ordinary,
01266                         SourceLocation(), false);
01267   EmitStmt(&assign);
01268 }
01269 
01270 /// \brief Generate an Objective-C property setter function.
01271 ///
01272 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
01273 /// is illegal within a category.
01274 void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
01275                                          const ObjCPropertyImplDecl *PID) {
01276   llvm::Constant *AtomicHelperFn =
01277       CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID);
01278   const ObjCPropertyDecl *PD = PID->getPropertyDecl();
01279   ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
01280   assert(OMD && "Invalid call to generate setter (empty method)");
01281   StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart());
01282 
01283   generateObjCSetterBody(IMP, PID, AtomicHelperFn);
01284 
01285   FinishFunction();
01286 }
01287 
01288 namespace {
01289   struct DestroyIvar : EHScopeStack::Cleanup {
01290   private:
01291     llvm::Value *addr;
01292     const ObjCIvarDecl *ivar;
01293     CodeGenFunction::Destroyer *destroyer;
01294     bool useEHCleanupForArray;
01295   public:
01296     DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
01297                 CodeGenFunction::Destroyer *destroyer,
01298                 bool useEHCleanupForArray)
01299       : addr(addr), ivar(ivar), destroyer(destroyer),
01300         useEHCleanupForArray(useEHCleanupForArray) {}
01301 
01302     void Emit(CodeGenFunction &CGF, Flags flags) override {
01303       LValue lvalue
01304         = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
01305       CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
01306                       flags.isForNormalCleanup() && useEHCleanupForArray);
01307     }
01308   };
01309 }
01310 
01311 /// Like CodeGenFunction::destroyARCStrong, but do it with a call.
01312 static void destroyARCStrongWithStore(CodeGenFunction &CGF,
01313                                       llvm::Value *addr,
01314                                       QualType type) {
01315   llvm::Value *null = getNullForVariable(addr);
01316   CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
01317 }
01318 
01319 static void emitCXXDestructMethod(CodeGenFunction &CGF,
01320                                   ObjCImplementationDecl *impl) {
01321   CodeGenFunction::RunCleanupsScope scope(CGF);
01322 
01323   llvm::Value *self = CGF.LoadObjCSelf();
01324 
01325   const ObjCInterfaceDecl *iface = impl->getClassInterface();
01326   for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
01327        ivar; ivar = ivar->getNextIvar()) {
01328     QualType type = ivar->getType();
01329 
01330     // Check whether the ivar is a destructible type.
01331     QualType::DestructionKind dtorKind = type.isDestructedType();
01332     if (!dtorKind) continue;
01333 
01334     CodeGenFunction::Destroyer *destroyer = nullptr;
01335 
01336     // Use a call to objc_storeStrong to destroy strong ivars, for the
01337     // general benefit of the tools.
01338     if (dtorKind == QualType::DK_objc_strong_lifetime) {
01339       destroyer = destroyARCStrongWithStore;
01340 
01341     // Otherwise use the default for the destruction kind.
01342     } else {
01343       destroyer = CGF.getDestroyer(dtorKind);
01344     }
01345 
01346     CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
01347 
01348     CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
01349                                          cleanupKind & EHCleanup);
01350   }
01351 
01352   assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
01353 }
01354 
01355 void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
01356                                                  ObjCMethodDecl *MD,
01357                                                  bool ctor) {
01358   MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
01359   StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
01360 
01361   // Emit .cxx_construct.
01362   if (ctor) {
01363     // Suppress the final autorelease in ARC.
01364     AutoreleaseResult = false;
01365 
01366     for (const auto *IvarInit : IMP->inits()) {
01367       FieldDecl *Field = IvarInit->getAnyMember();
01368       ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
01369       LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 
01370                                     LoadObjCSelf(), Ivar, 0);
01371       EmitAggExpr(IvarInit->getInit(),
01372                   AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
01373                                           AggValueSlot::DoesNotNeedGCBarriers,
01374                                           AggValueSlot::IsNotAliased));
01375     }
01376     // constructor returns 'self'.
01377     CodeGenTypes &Types = CGM.getTypes();
01378     QualType IdTy(CGM.getContext().getObjCIdType());
01379     llvm::Value *SelfAsId =
01380       Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
01381     EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
01382 
01383   // Emit .cxx_destruct.
01384   } else {
01385     emitCXXDestructMethod(*this, IMP);
01386   }
01387   FinishFunction();
01388 }
01389 
01390 bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
01391   CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
01392   it++; it++;
01393   const ABIArgInfo &AI = it->info;
01394   // FIXME. Is this sufficient check?
01395   return (AI.getKind() == ABIArgInfo::Indirect);
01396 }
01397 
01398 bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
01399   if (CGM.getLangOpts().getGC() == LangOptions::NonGC)
01400     return false;
01401   if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
01402     return FDTTy->getDecl()->hasObjectMember();
01403   return false;
01404 }
01405 
01406 llvm::Value *CodeGenFunction::LoadObjCSelf() {
01407   VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
01408   DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
01409                   Self->getType(), VK_LValue, SourceLocation());
01410   return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation());
01411 }
01412 
01413 QualType CodeGenFunction::TypeOfSelfObject() {
01414   const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
01415   ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
01416   const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
01417     getContext().getCanonicalType(selfDecl->getType()));
01418   return PTy->getPointeeType();
01419 }
01420 
01421 void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
01422   llvm::Constant *EnumerationMutationFn =
01423     CGM.getObjCRuntime().EnumerationMutationFunction();
01424 
01425   if (!EnumerationMutationFn) {
01426     CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
01427     return;
01428   }
01429 
01430   CGDebugInfo *DI = getDebugInfo();
01431   if (DI)
01432     DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
01433 
01434   // The local variable comes into scope immediately.
01435   AutoVarEmission variable = AutoVarEmission::invalid();
01436   if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
01437     variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
01438 
01439   JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
01440 
01441   // Fast enumeration state.
01442   QualType StateTy = CGM.getObjCFastEnumerationStateType();
01443   llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
01444   EmitNullInitialization(StatePtr, StateTy);
01445 
01446   // Number of elements in the items array.
01447   static const unsigned NumItems = 16;
01448 
01449   // Fetch the countByEnumeratingWithState:objects:count: selector.
01450   IdentifierInfo *II[] = {
01451     &CGM.getContext().Idents.get("countByEnumeratingWithState"),
01452     &CGM.getContext().Idents.get("objects"),
01453     &CGM.getContext().Idents.get("count")
01454   };
01455   Selector FastEnumSel =
01456     CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
01457 
01458   QualType ItemsTy =
01459     getContext().getConstantArrayType(getContext().getObjCIdType(),
01460                                       llvm::APInt(32, NumItems),
01461                                       ArrayType::Normal, 0);
01462   llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
01463 
01464   // Emit the collection pointer.  In ARC, we do a retain.
01465   llvm::Value *Collection;
01466   if (getLangOpts().ObjCAutoRefCount) {
01467     Collection = EmitARCRetainScalarExpr(S.getCollection());
01468 
01469     // Enter a cleanup to do the release.
01470     EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
01471   } else {
01472     Collection = EmitScalarExpr(S.getCollection());
01473   }
01474 
01475   // The 'continue' label needs to appear within the cleanup for the
01476   // collection object.
01477   JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
01478 
01479   // Send it our message:
01480   CallArgList Args;
01481 
01482   // The first argument is a temporary of the enumeration-state type.
01483   Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
01484 
01485   // The second argument is a temporary array with space for NumItems
01486   // pointers.  We'll actually be loading elements from the array
01487   // pointer written into the control state; this buffer is so that
01488   // collections that *aren't* backed by arrays can still queue up
01489   // batches of elements.
01490   Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
01491 
01492   // The third argument is the capacity of that temporary array.
01493   llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
01494   llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
01495   Args.add(RValue::get(Count), getContext().UnsignedLongTy);
01496 
01497   // Start the enumeration.
01498   RValue CountRV =
01499     CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
01500                                              getContext().UnsignedLongTy,
01501                                              FastEnumSel,
01502                                              Collection, Args);
01503 
01504   // The initial number of objects that were returned in the buffer.
01505   llvm::Value *initialBufferLimit = CountRV.getScalarVal();
01506 
01507   llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
01508   llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
01509 
01510   llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
01511 
01512   // If the limit pointer was zero to begin with, the collection is
01513   // empty; skip all this. Set the branch weight assuming this has the same
01514   // probability of exiting the loop as any other loop exit.
01515   uint64_t EntryCount = PGO.getCurrentRegionCount();
01516   RegionCounter Cnt = getPGORegionCounter(&S);
01517   Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
01518                        EmptyBB, LoopInitBB,
01519                        PGO.createBranchWeights(EntryCount, Cnt.getCount()));
01520 
01521   // Otherwise, initialize the loop.
01522   EmitBlock(LoopInitBB);
01523 
01524   // Save the initial mutations value.  This is the value at an
01525   // address that was written into the state object by
01526   // countByEnumeratingWithState:objects:count:.
01527   llvm::Value *StateMutationsPtrPtr =
01528     Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
01529   llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
01530                                                       "mutationsptr");
01531 
01532   llvm::Value *initialMutations =
01533     Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
01534 
01535   // Start looping.  This is the point we return to whenever we have a
01536   // fresh, non-empty batch of objects.
01537   llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
01538   EmitBlock(LoopBodyBB);
01539 
01540   // The current index into the buffer.
01541   llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
01542   index->addIncoming(zero, LoopInitBB);
01543 
01544   // The current buffer size.
01545   llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
01546   count->addIncoming(initialBufferLimit, LoopInitBB);
01547 
01548   Cnt.beginRegion(Builder);
01549 
01550   // Check whether the mutations value has changed from where it was
01551   // at start.  StateMutationsPtr should actually be invariant between
01552   // refreshes.
01553   StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
01554   llvm::Value *currentMutations
01555     = Builder.CreateLoad(StateMutationsPtr, "statemutations");
01556 
01557   llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
01558   llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
01559 
01560   Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
01561                        WasNotMutatedBB, WasMutatedBB);
01562 
01563   // If so, call the enumeration-mutation function.
01564   EmitBlock(WasMutatedBB);
01565   llvm::Value *V =
01566     Builder.CreateBitCast(Collection,
01567                           ConvertType(getContext().getObjCIdType()));
01568   CallArgList Args2;
01569   Args2.add(RValue::get(V), getContext().getObjCIdType());
01570   // FIXME: We shouldn't need to get the function info here, the runtime already
01571   // should have computed it to build the function.
01572   EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2,
01573                                                   FunctionType::ExtInfo(),
01574                                                   RequiredArgs::All),
01575            EnumerationMutationFn, ReturnValueSlot(), Args2);
01576 
01577   // Otherwise, or if the mutation function returns, just continue.
01578   EmitBlock(WasNotMutatedBB);
01579 
01580   // Initialize the element variable.
01581   RunCleanupsScope elementVariableScope(*this);
01582   bool elementIsVariable;
01583   LValue elementLValue;
01584   QualType elementType;
01585   if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
01586     // Initialize the variable, in case it's a __block variable or something.
01587     EmitAutoVarInit(variable);
01588 
01589     const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
01590     DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(),
01591                         VK_LValue, SourceLocation());
01592     elementLValue = EmitLValue(&tempDRE);
01593     elementType = D->getType();
01594     elementIsVariable = true;
01595 
01596     if (D->isARCPseudoStrong())
01597       elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
01598   } else {
01599     elementLValue = LValue(); // suppress warning
01600     elementType = cast<Expr>(S.getElement())->getType();
01601     elementIsVariable = false;
01602   }
01603   llvm::Type *convertedElementType = ConvertType(elementType);
01604 
01605   // Fetch the buffer out of the enumeration state.
01606   // TODO: this pointer should actually be invariant between
01607   // refreshes, which would help us do certain loop optimizations.
01608   llvm::Value *StateItemsPtr =
01609     Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
01610   llvm::Value *EnumStateItems =
01611     Builder.CreateLoad(StateItemsPtr, "stateitems");
01612 
01613   // Fetch the value at the current index from the buffer.
01614   llvm::Value *CurrentItemPtr =
01615     Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
01616   llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
01617 
01618   // Cast that value to the right type.
01619   CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
01620                                       "currentitem");
01621 
01622   // Make sure we have an l-value.  Yes, this gets evaluated every
01623   // time through the loop.
01624   if (!elementIsVariable) {
01625     elementLValue = EmitLValue(cast<Expr>(S.getElement()));
01626     EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
01627   } else {
01628     EmitScalarInit(CurrentItem, elementLValue);
01629   }
01630 
01631   // If we do have an element variable, this assignment is the end of
01632   // its initialization.
01633   if (elementIsVariable)
01634     EmitAutoVarCleanups(variable);
01635 
01636   // Perform the loop body, setting up break and continue labels.
01637   BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
01638   {
01639     RunCleanupsScope Scope(*this);
01640     EmitStmt(S.getBody());
01641   }
01642   BreakContinueStack.pop_back();
01643 
01644   // Destroy the element variable now.
01645   elementVariableScope.ForceCleanup();
01646 
01647   // Check whether there are more elements.
01648   EmitBlock(AfterBody.getBlock());
01649 
01650   llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
01651 
01652   // First we check in the local buffer.
01653   llvm::Value *indexPlusOne
01654     = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
01655 
01656   // If we haven't overrun the buffer yet, we can continue.
01657   // Set the branch weights based on the simplifying assumption that this is
01658   // like a while-loop, i.e., ignoring that the false branch fetches more
01659   // elements and then returns to the loop.
01660   Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
01661                        LoopBodyBB, FetchMoreBB,
01662                        PGO.createBranchWeights(Cnt.getCount(), EntryCount));
01663 
01664   index->addIncoming(indexPlusOne, AfterBody.getBlock());
01665   count->addIncoming(count, AfterBody.getBlock());
01666 
01667   // Otherwise, we have to fetch more elements.
01668   EmitBlock(FetchMoreBB);
01669 
01670   CountRV =
01671     CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
01672                                              getContext().UnsignedLongTy,
01673                                              FastEnumSel,
01674                                              Collection, Args);
01675 
01676   // If we got a zero count, we're done.
01677   llvm::Value *refetchCount = CountRV.getScalarVal();
01678 
01679   // (note that the message send might split FetchMoreBB)
01680   index->addIncoming(zero, Builder.GetInsertBlock());
01681   count->addIncoming(refetchCount, Builder.GetInsertBlock());
01682 
01683   Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
01684                        EmptyBB, LoopBodyBB);
01685 
01686   // No more elements.
01687   EmitBlock(EmptyBB);
01688 
01689   if (!elementIsVariable) {
01690     // If the element was not a declaration, set it to be null.
01691 
01692     llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
01693     elementLValue = EmitLValue(cast<Expr>(S.getElement()));
01694     EmitStoreThroughLValue(RValue::get(null), elementLValue);
01695   }
01696 
01697   if (DI)
01698     DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
01699 
01700   // Leave the cleanup we entered in ARC.
01701   if (getLangOpts().ObjCAutoRefCount)
01702     PopCleanupBlock();
01703 
01704   EmitBlock(LoopEnd.getBlock());
01705 }
01706 
01707 void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
01708   CGM.getObjCRuntime().EmitTryStmt(*this, S);
01709 }
01710 
01711 void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
01712   CGM.getObjCRuntime().EmitThrowStmt(*this, S);
01713 }
01714 
01715 void CodeGenFunction::EmitObjCAtSynchronizedStmt(
01716                                               const ObjCAtSynchronizedStmt &S) {
01717   CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
01718 }
01719 
01720 /// Produce the code for a CK_ARCProduceObject.  Just does a
01721 /// primitive retain.
01722 llvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type,
01723                                                     llvm::Value *value) {
01724   return EmitARCRetain(type, value);
01725 }
01726 
01727 namespace {
01728   struct CallObjCRelease : EHScopeStack::Cleanup {
01729     CallObjCRelease(llvm::Value *object) : object(object) {}
01730     llvm::Value *object;
01731 
01732     void Emit(CodeGenFunction &CGF, Flags flags) override {
01733       // Releases at the end of the full-expression are imprecise.
01734       CGF.EmitARCRelease(object, ARCImpreciseLifetime);
01735     }
01736   };
01737 }
01738 
01739 /// Produce the code for a CK_ARCConsumeObject.  Does a primitive
01740 /// release at the end of the full-expression.
01741 llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
01742                                                     llvm::Value *object) {
01743   // If we're in a conditional branch, we need to make the cleanup
01744   // conditional.
01745   pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
01746   return object;
01747 }
01748 
01749 llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
01750                                                            llvm::Value *value) {
01751   return EmitARCRetainAutorelease(type, value);
01752 }
01753 
01754 /// Given a number of pointers, inform the optimizer that they're
01755 /// being intrinsically used up until this point in the program.
01756 void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
01757   llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use;
01758   if (!fn) {
01759     llvm::FunctionType *fnType =
01760       llvm::FunctionType::get(CGM.VoidTy, None, true);
01761     fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use");
01762   }
01763 
01764   // This isn't really a "runtime" function, but as an intrinsic it
01765   // doesn't really matter as long as we align things up.
01766   EmitNounwindRuntimeCall(fn, values);
01767 }
01768 
01769 
01770 static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
01771                                                 llvm::FunctionType *type,
01772                                                 StringRef fnName) {
01773   llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName);
01774 
01775   if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) {
01776     // If the target runtime doesn't naturally support ARC, emit weak
01777     // references to the runtime support library.  We don't really
01778     // permit this to fail, but we need a particular relocation style.
01779     if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
01780       f->setLinkage(llvm::Function::ExternalWeakLinkage);
01781     } else if (fnName == "objc_retain" || fnName  == "objc_release") {
01782       // If we have Native ARC, set nonlazybind attribute for these APIs for
01783       // performance.
01784       f->addFnAttr(llvm::Attribute::NonLazyBind);
01785     }
01786   }
01787 
01788   return fn;
01789 }
01790 
01791 /// Perform an operation having the signature
01792 ///   i8* (i8*)
01793 /// where a null input causes a no-op and returns null.
01794 static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
01795                                           llvm::Value *value,
01796                                           llvm::Constant *&fn,
01797                                           StringRef fnName,
01798                                           bool isTailCall = false) {
01799   if (isa<llvm::ConstantPointerNull>(value)) return value;
01800 
01801   if (!fn) {
01802     llvm::FunctionType *fnType =
01803       llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
01804     fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
01805   }
01806 
01807   // Cast the argument to 'id'.
01808   llvm::Type *origType = value->getType();
01809   value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
01810 
01811   // Call the function.
01812   llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
01813   if (isTailCall)
01814     call->setTailCall();
01815 
01816   // Cast the result back to the original type.
01817   return CGF.Builder.CreateBitCast(call, origType);
01818 }
01819 
01820 /// Perform an operation having the following signature:
01821 ///   i8* (i8**)
01822 static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
01823                                          llvm::Value *addr,
01824                                          llvm::Constant *&fn,
01825                                          StringRef fnName) {
01826   if (!fn) {
01827     llvm::FunctionType *fnType =
01828       llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false);
01829     fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
01830   }
01831 
01832   // Cast the argument to 'id*'.
01833   llvm::Type *origType = addr->getType();
01834   addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
01835 
01836   // Call the function.
01837   llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr);
01838 
01839   // Cast the result back to a dereference of the original type.
01840   if (origType != CGF.Int8PtrPtrTy)
01841     result = CGF.Builder.CreateBitCast(result,
01842                         cast<llvm::PointerType>(origType)->getElementType());
01843 
01844   return result;
01845 }
01846 
01847 /// Perform an operation having the following signature:
01848 ///   i8* (i8**, i8*)
01849 static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
01850                                           llvm::Value *addr,
01851                                           llvm::Value *value,
01852                                           llvm::Constant *&fn,
01853                                           StringRef fnName,
01854                                           bool ignored) {
01855   assert(cast<llvm::PointerType>(addr->getType())->getElementType()
01856            == value->getType());
01857 
01858   if (!fn) {
01859     llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy };
01860 
01861     llvm::FunctionType *fnType
01862       = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
01863     fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
01864   }
01865 
01866   llvm::Type *origType = value->getType();
01867 
01868   llvm::Value *args[] = {
01869     CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy),
01870     CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
01871   };
01872   llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
01873 
01874   if (ignored) return nullptr;
01875 
01876   return CGF.Builder.CreateBitCast(result, origType);
01877 }
01878 
01879 /// Perform an operation having the following signature:
01880 ///   void (i8**, i8**)
01881 static void emitARCCopyOperation(CodeGenFunction &CGF,
01882                                  llvm::Value *dst,
01883                                  llvm::Value *src,
01884                                  llvm::Constant *&fn,
01885                                  StringRef fnName) {
01886   assert(dst->getType() == src->getType());
01887 
01888   if (!fn) {
01889     llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy };
01890 
01891     llvm::FunctionType *fnType
01892       = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
01893     fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
01894   }
01895 
01896   llvm::Value *args[] = {
01897     CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy),
01898     CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy)
01899   };
01900   CGF.EmitNounwindRuntimeCall(fn, args);
01901 }
01902 
01903 /// Produce the code to do a retain.  Based on the type, calls one of:
01904 ///   call i8* \@objc_retain(i8* %value)
01905 ///   call i8* \@objc_retainBlock(i8* %value)
01906 llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
01907   if (type->isBlockPointerType())
01908     return EmitARCRetainBlock(value, /*mandatory*/ false);
01909   else
01910     return EmitARCRetainNonBlock(value);
01911 }
01912 
01913 /// Retain the given object, with normal retain semantics.
01914 ///   call i8* \@objc_retain(i8* %value)
01915 llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
01916   return emitARCValueOperation(*this, value,
01917                                CGM.getARCEntrypoints().objc_retain,
01918                                "objc_retain");
01919 }
01920 
01921 /// Retain the given block, with _Block_copy semantics.
01922 ///   call i8* \@objc_retainBlock(i8* %value)
01923 ///
01924 /// \param mandatory - If false, emit the call with metadata
01925 /// indicating that it's okay for the optimizer to eliminate this call
01926 /// if it can prove that the block never escapes except down the stack.
01927 llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
01928                                                  bool mandatory) {
01929   llvm::Value *result
01930     = emitARCValueOperation(*this, value,
01931                             CGM.getARCEntrypoints().objc_retainBlock,
01932                             "objc_retainBlock");
01933 
01934   // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
01935   // tell the optimizer that it doesn't need to do this copy if the
01936   // block doesn't escape, where being passed as an argument doesn't
01937   // count as escaping.
01938   if (!mandatory && isa<llvm::Instruction>(result)) {
01939     llvm::CallInst *call
01940       = cast<llvm::CallInst>(result->stripPointerCasts());
01941     assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock);
01942 
01943     SmallVector<llvm::Value*,1> args;
01944     call->setMetadata("clang.arc.copy_on_escape",
01945                       llvm::MDNode::get(Builder.getContext(), args));
01946   }
01947 
01948   return result;
01949 }
01950 
01951 /// Retain the given object which is the result of a function call.
01952 ///   call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
01953 ///
01954 /// Yes, this function name is one character away from a different
01955 /// call with completely different semantics.
01956 llvm::Value *
01957 CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
01958   // Fetch the void(void) inline asm which marks that we're going to
01959   // retain the autoreleased return value.
01960   llvm::InlineAsm *&marker
01961     = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker;
01962   if (!marker) {
01963     StringRef assembly
01964       = CGM.getTargetCodeGenInfo()
01965            .getARCRetainAutoreleasedReturnValueMarker();
01966 
01967     // If we have an empty assembly string, there's nothing to do.
01968     if (assembly.empty()) {
01969 
01970     // Otherwise, at -O0, build an inline asm that we're going to call
01971     // in a moment.
01972     } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
01973       llvm::FunctionType *type =
01974         llvm::FunctionType::get(VoidTy, /*variadic*/false);
01975       
01976       marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
01977 
01978     // If we're at -O1 and above, we don't want to litter the code
01979     // with this marker yet, so leave a breadcrumb for the ARC
01980     // optimizer to pick up.
01981     } else {
01982       llvm::NamedMDNode *metadata =
01983         CGM.getModule().getOrInsertNamedMetadata(
01984                             "clang.arc.retainAutoreleasedReturnValueMarker");
01985       assert(metadata->getNumOperands() <= 1);
01986       if (metadata->getNumOperands() == 0) {
01987         llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly);
01988         metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string));
01989       }
01990     }
01991   }
01992 
01993   // Call the marker asm if we made one, which we do only at -O0.
01994   if (marker) Builder.CreateCall(marker);
01995 
01996   return emitARCValueOperation(*this, value,
01997                      CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue,
01998                                "objc_retainAutoreleasedReturnValue");
01999 }
02000 
02001 /// Release the given object.
02002 ///   call void \@objc_release(i8* %value)
02003 void CodeGenFunction::EmitARCRelease(llvm::Value *value,
02004                                      ARCPreciseLifetime_t precise) {
02005   if (isa<llvm::ConstantPointerNull>(value)) return;
02006 
02007   llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release;
02008   if (!fn) {
02009     llvm::FunctionType *fnType =
02010       llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
02011     fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
02012   }
02013 
02014   // Cast the argument to 'id'.
02015   value = Builder.CreateBitCast(value, Int8PtrTy);
02016 
02017   // Call objc_release.
02018   llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
02019 
02020   if (precise == ARCImpreciseLifetime) {
02021     SmallVector<llvm::Value*,1> args;
02022     call->setMetadata("clang.imprecise_release",
02023                       llvm::MDNode::get(Builder.getContext(), args));
02024   }
02025 }
02026 
02027 /// Destroy a __strong variable.
02028 ///
02029 /// At -O0, emit a call to store 'null' into the address;
02030 /// instrumenting tools prefer this because the address is exposed,
02031 /// but it's relatively cumbersome to optimize.
02032 ///
02033 /// At -O1 and above, just load and call objc_release.
02034 ///
02035 ///   call void \@objc_storeStrong(i8** %addr, i8* null)
02036 void CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr,
02037                                            ARCPreciseLifetime_t precise) {
02038   if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
02039     llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType());
02040     llvm::Value *null = llvm::ConstantPointerNull::get(
02041                           cast<llvm::PointerType>(addrTy->getElementType()));
02042     EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
02043     return;
02044   }
02045 
02046   llvm::Value *value = Builder.CreateLoad(addr);
02047   EmitARCRelease(value, precise);
02048 }
02049 
02050 /// Store into a strong object.  Always calls this:
02051 ///   call void \@objc_storeStrong(i8** %addr, i8* %value)
02052 llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr,
02053                                                      llvm::Value *value,
02054                                                      bool ignored) {
02055   assert(cast<llvm::PointerType>(addr->getType())->getElementType()
02056            == value->getType());
02057 
02058   llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong;
02059   if (!fn) {
02060     llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
02061     llvm::FunctionType *fnType
02062       = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
02063     fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
02064   }
02065 
02066   llvm::Value *args[] = {
02067     Builder.CreateBitCast(addr, Int8PtrPtrTy),
02068     Builder.CreateBitCast(value, Int8PtrTy)
02069   };
02070   EmitNounwindRuntimeCall(fn, args);
02071 
02072   if (ignored) return nullptr;
02073   return value;
02074 }
02075 
02076 /// Store into a strong object.  Sometimes calls this:
02077 ///   call void \@objc_storeStrong(i8** %addr, i8* %value)
02078 /// Other times, breaks it down into components.
02079 llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
02080                                                  llvm::Value *newValue,
02081                                                  bool ignored) {
02082   QualType type = dst.getType();
02083   bool isBlock = type->isBlockPointerType();
02084 
02085   // Use a store barrier at -O0 unless this is a block type or the
02086   // lvalue is inadequately aligned.
02087   if (shouldUseFusedARCCalls() &&
02088       !isBlock &&
02089       (dst.getAlignment().isZero() ||
02090        dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
02091     return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
02092   }
02093 
02094   // Otherwise, split it out.
02095 
02096   // Retain the new value.
02097   newValue = EmitARCRetain(type, newValue);
02098 
02099   // Read the old value.
02100   llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation());
02101 
02102   // Store.  We do this before the release so that any deallocs won't
02103   // see the old value.
02104   EmitStoreOfScalar(newValue, dst);
02105 
02106   // Finally, release the old value.
02107   EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
02108 
02109   return newValue;
02110 }
02111 
02112 /// Autorelease the given object.
02113 ///   call i8* \@objc_autorelease(i8* %value)
02114 llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
02115   return emitARCValueOperation(*this, value,
02116                                CGM.getARCEntrypoints().objc_autorelease,
02117                                "objc_autorelease");
02118 }
02119 
02120 /// Autorelease the given object.
02121 ///   call i8* \@objc_autoreleaseReturnValue(i8* %value)
02122 llvm::Value *
02123 CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
02124   return emitARCValueOperation(*this, value,
02125                             CGM.getARCEntrypoints().objc_autoreleaseReturnValue,
02126                                "objc_autoreleaseReturnValue",
02127                                /*isTailCall*/ true);
02128 }
02129 
02130 /// Do a fused retain/autorelease of the given object.
02131 ///   call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
02132 llvm::Value *
02133 CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
02134   return emitARCValueOperation(*this, value,
02135                      CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue,
02136                                "objc_retainAutoreleaseReturnValue",
02137                                /*isTailCall*/ true);
02138 }
02139 
02140 /// Do a fused retain/autorelease of the given object.
02141 ///   call i8* \@objc_retainAutorelease(i8* %value)
02142 /// or
02143 ///   %retain = call i8* \@objc_retainBlock(i8* %value)
02144 ///   call i8* \@objc_autorelease(i8* %retain)
02145 llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
02146                                                        llvm::Value *value) {
02147   if (!type->isBlockPointerType())
02148     return EmitARCRetainAutoreleaseNonBlock(value);
02149 
02150   if (isa<llvm::ConstantPointerNull>(value)) return value;
02151 
02152   llvm::Type *origType = value->getType();
02153   value = Builder.CreateBitCast(value, Int8PtrTy);
02154   value = EmitARCRetainBlock(value, /*mandatory*/ true);
02155   value = EmitARCAutorelease(value);
02156   return Builder.CreateBitCast(value, origType);
02157 }
02158 
02159 /// Do a fused retain/autorelease of the given object.
02160 ///   call i8* \@objc_retainAutorelease(i8* %value)
02161 llvm::Value *
02162 CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
02163   return emitARCValueOperation(*this, value,
02164                                CGM.getARCEntrypoints().objc_retainAutorelease,
02165                                "objc_retainAutorelease");
02166 }
02167 
02168 /// i8* \@objc_loadWeak(i8** %addr)
02169 /// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
02170 llvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) {
02171   return emitARCLoadOperation(*this, addr,
02172                               CGM.getARCEntrypoints().objc_loadWeak,
02173                               "objc_loadWeak");
02174 }
02175 
02176 /// i8* \@objc_loadWeakRetained(i8** %addr)
02177 llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) {
02178   return emitARCLoadOperation(*this, addr,
02179                               CGM.getARCEntrypoints().objc_loadWeakRetained,
02180                               "objc_loadWeakRetained");
02181 }
02182 
02183 /// i8* \@objc_storeWeak(i8** %addr, i8* %value)
02184 /// Returns %value.
02185 llvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr,
02186                                                llvm::Value *value,
02187                                                bool ignored) {
02188   return emitARCStoreOperation(*this, addr, value,
02189                                CGM.getARCEntrypoints().objc_storeWeak,
02190                                "objc_storeWeak", ignored);
02191 }
02192 
02193 /// i8* \@objc_initWeak(i8** %addr, i8* %value)
02194 /// Returns %value.  %addr is known to not have a current weak entry.
02195 /// Essentially equivalent to:
02196 ///   *addr = nil; objc_storeWeak(addr, value);
02197 void CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) {
02198   // If we're initializing to null, just write null to memory; no need
02199   // to get the runtime involved.  But don't do this if optimization
02200   // is enabled, because accounting for this would make the optimizer
02201   // much more complicated.
02202   if (isa<llvm::ConstantPointerNull>(value) &&
02203       CGM.getCodeGenOpts().OptimizationLevel == 0) {
02204     Builder.CreateStore(value, addr);
02205     return;
02206   }
02207 
02208   emitARCStoreOperation(*this, addr, value,
02209                         CGM.getARCEntrypoints().objc_initWeak,
02210                         "objc_initWeak", /*ignored*/ true);
02211 }
02212 
02213 /// void \@objc_destroyWeak(i8** %addr)
02214 /// Essentially objc_storeWeak(addr, nil).
02215 void CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) {
02216   llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak;
02217   if (!fn) {
02218     llvm::FunctionType *fnType =
02219       llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false);
02220     fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
02221   }
02222 
02223   // Cast the argument to 'id*'.
02224   addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
02225 
02226   EmitNounwindRuntimeCall(fn, addr);
02227 }
02228 
02229 /// void \@objc_moveWeak(i8** %dest, i8** %src)
02230 /// Disregards the current value in %dest.  Leaves %src pointing to nothing.
02231 /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
02232 void CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) {
02233   emitARCCopyOperation(*this, dst, src,
02234                        CGM.getARCEntrypoints().objc_moveWeak,
02235                        "objc_moveWeak");
02236 }
02237 
02238 /// void \@objc_copyWeak(i8** %dest, i8** %src)
02239 /// Disregards the current value in %dest.  Essentially
02240 ///   objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
02241 void CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) {
02242   emitARCCopyOperation(*this, dst, src,
02243                        CGM.getARCEntrypoints().objc_copyWeak,
02244                        "objc_copyWeak");
02245 }
02246 
02247 /// Produce the code to do a objc_autoreleasepool_push.
02248 ///   call i8* \@objc_autoreleasePoolPush(void)
02249 llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
02250   llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush;
02251   if (!fn) {
02252     llvm::FunctionType *fnType =
02253       llvm::FunctionType::get(Int8PtrTy, false);
02254     fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
02255   }
02256 
02257   return EmitNounwindRuntimeCall(fn);
02258 }
02259 
02260 /// Produce the code to do a primitive release.
02261 ///   call void \@objc_autoreleasePoolPop(i8* %ptr)
02262 void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
02263   assert(value->getType() == Int8PtrTy);
02264 
02265   llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop;
02266   if (!fn) {
02267     llvm::FunctionType *fnType =
02268       llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
02269 
02270     // We don't want to use a weak import here; instead we should not
02271     // fall into this path.
02272     fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
02273   }
02274 
02275   // objc_autoreleasePoolPop can throw.
02276   EmitRuntimeCallOrInvoke(fn, value);
02277 }
02278 
02279 /// Produce the code to do an MRR version objc_autoreleasepool_push.
02280 /// Which is: [[NSAutoreleasePool alloc] init];
02281 /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
02282 /// init is declared as: - (id) init; in its NSObject super class.
02283 ///
02284 llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
02285   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
02286   llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
02287   // [NSAutoreleasePool alloc]
02288   IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
02289   Selector AllocSel = getContext().Selectors.getSelector(0, &II);
02290   CallArgList Args;
02291   RValue AllocRV =  
02292     Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 
02293                                 getContext().getObjCIdType(),
02294                                 AllocSel, Receiver, Args); 
02295 
02296   // [Receiver init]
02297   Receiver = AllocRV.getScalarVal();
02298   II = &CGM.getContext().Idents.get("init");
02299   Selector InitSel = getContext().Selectors.getSelector(0, &II);
02300   RValue InitRV =
02301     Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
02302                                 getContext().getObjCIdType(),
02303                                 InitSel, Receiver, Args); 
02304   return InitRV.getScalarVal();
02305 }
02306 
02307 /// Produce the code to do a primitive release.
02308 /// [tmp drain];
02309 void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
02310   IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
02311   Selector DrainSel = getContext().Selectors.getSelector(0, &II);
02312   CallArgList Args;
02313   CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
02314                               getContext().VoidTy, DrainSel, Arg, Args); 
02315 }
02316 
02317 void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
02318                                               llvm::Value *addr,
02319                                               QualType type) {
02320   CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
02321 }
02322 
02323 void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
02324                                                 llvm::Value *addr,
02325                                                 QualType type) {
02326   CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
02327 }
02328 
02329 void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
02330                                      llvm::Value *addr,
02331                                      QualType type) {
02332   CGF.EmitARCDestroyWeak(addr);
02333 }
02334 
02335 namespace {
02336   struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup {
02337     llvm::Value *Token;
02338 
02339     CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
02340 
02341     void Emit(CodeGenFunction &CGF, Flags flags) override {
02342       CGF.EmitObjCAutoreleasePoolPop(Token);
02343     }
02344   };
02345   struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup {
02346     llvm::Value *Token;
02347 
02348     CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
02349 
02350     void Emit(CodeGenFunction &CGF, Flags flags) override {
02351       CGF.EmitObjCMRRAutoreleasePoolPop(Token);
02352     }
02353   };
02354 }
02355 
02356 void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
02357   if (CGM.getLangOpts().ObjCAutoRefCount)
02358     EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
02359   else
02360     EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
02361 }
02362 
02363 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
02364                                                   LValue lvalue,
02365                                                   QualType type) {
02366   switch (type.getObjCLifetime()) {
02367   case Qualifiers::OCL_None:
02368   case Qualifiers::OCL_ExplicitNone:
02369   case Qualifiers::OCL_Strong:
02370   case Qualifiers::OCL_Autoreleasing:
02371     return TryEmitResult(CGF.EmitLoadOfLValue(lvalue,
02372                                               SourceLocation()).getScalarVal(),
02373                          false);
02374 
02375   case Qualifiers::OCL_Weak:
02376     return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
02377                          true);
02378   }
02379 
02380   llvm_unreachable("impossible lifetime!");
02381 }
02382 
02383 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
02384                                                   const Expr *e) {
02385   e = e->IgnoreParens();
02386   QualType type = e->getType();
02387 
02388   // If we're loading retained from a __strong xvalue, we can avoid 
02389   // an extra retain/release pair by zeroing out the source of this
02390   // "move" operation.
02391   if (e->isXValue() &&
02392       !type.isConstQualified() &&
02393       type.getObjCLifetime() == Qualifiers::OCL_Strong) {
02394     // Emit the lvalue.
02395     LValue lv = CGF.EmitLValue(e);
02396     
02397     // Load the object pointer.
02398     llvm::Value *result = CGF.EmitLoadOfLValue(lv,
02399                                                SourceLocation()).getScalarVal();
02400     
02401     // Set the source pointer to NULL.
02402     CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
02403     
02404     return TryEmitResult(result, true);
02405   }
02406 
02407   // As a very special optimization, in ARC++, if the l-value is the
02408   // result of a non-volatile assignment, do a simple retain of the
02409   // result of the call to objc_storeWeak instead of reloading.
02410   if (CGF.getLangOpts().CPlusPlus &&
02411       !type.isVolatileQualified() &&
02412       type.getObjCLifetime() == Qualifiers::OCL_Weak &&
02413       isa<BinaryOperator>(e) &&
02414       cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
02415     return TryEmitResult(CGF.EmitScalarExpr(e), false);
02416 
02417   return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
02418 }
02419 
02420 static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
02421                                            llvm::Value *value);
02422 
02423 /// Given that the given expression is some sort of call (which does
02424 /// not return retained), emit a retain following it.
02425 static llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) {
02426   llvm::Value *value = CGF.EmitScalarExpr(e);
02427   return emitARCRetainAfterCall(CGF, value);
02428 }
02429 
02430 static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
02431                                            llvm::Value *value) {
02432   if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
02433     CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
02434 
02435     // Place the retain immediately following the call.
02436     CGF.Builder.SetInsertPoint(call->getParent(),
02437                                ++llvm::BasicBlock::iterator(call));
02438     value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
02439 
02440     CGF.Builder.restoreIP(ip);
02441     return value;
02442   } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
02443     CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
02444 
02445     // Place the retain at the beginning of the normal destination block.
02446     llvm::BasicBlock *BB = invoke->getNormalDest();
02447     CGF.Builder.SetInsertPoint(BB, BB->begin());
02448     value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
02449 
02450     CGF.Builder.restoreIP(ip);
02451     return value;
02452 
02453   // Bitcasts can arise because of related-result returns.  Rewrite
02454   // the operand.
02455   } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
02456     llvm::Value *operand = bitcast->getOperand(0);
02457     operand = emitARCRetainAfterCall(CGF, operand);
02458     bitcast->setOperand(0, operand);
02459     return bitcast;
02460 
02461   // Generic fall-back case.
02462   } else {
02463     // Retain using the non-block variant: we never need to do a copy
02464     // of a block that's been returned to us.
02465     return CGF.EmitARCRetainNonBlock(value);
02466   }
02467 }
02468 
02469 /// Determine whether it might be important to emit a separate
02470 /// objc_retain_block on the result of the given expression, or
02471 /// whether it's okay to just emit it in a +1 context.
02472 static bool shouldEmitSeparateBlockRetain(const Expr *e) {
02473   assert(e->getType()->isBlockPointerType());
02474   e = e->IgnoreParens();
02475 
02476   // For future goodness, emit block expressions directly in +1
02477   // contexts if we can.
02478   if (isa<BlockExpr>(e))
02479     return false;
02480 
02481   if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
02482     switch (cast->getCastKind()) {
02483     // Emitting these operations in +1 contexts is goodness.
02484     case CK_LValueToRValue:
02485     case CK_ARCReclaimReturnedObject:
02486     case CK_ARCConsumeObject:
02487     case CK_ARCProduceObject:
02488       return false;
02489 
02490     // These operations preserve a block type.
02491     case CK_NoOp:
02492     case CK_BitCast:
02493       return shouldEmitSeparateBlockRetain(cast->getSubExpr());
02494 
02495     // These operations are known to be bad (or haven't been considered).
02496     case CK_AnyPointerToBlockPointerCast:
02497     default:
02498       return true;
02499     }
02500   }
02501 
02502   return true;
02503 }
02504 
02505 /// Try to emit a PseudoObjectExpr at +1.
02506 ///
02507 /// This massively duplicates emitPseudoObjectRValue.
02508 static TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF,
02509                                                   const PseudoObjectExpr *E) {
02510   SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
02511 
02512   // Find the result expression.
02513   const Expr *resultExpr = E->getResultExpr();
02514   assert(resultExpr);
02515   TryEmitResult result;
02516 
02517   for (PseudoObjectExpr::const_semantics_iterator
02518          i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
02519     const Expr *semantic = *i;
02520 
02521     // If this semantic expression is an opaque value, bind it
02522     // to the result of its source expression.
02523     if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
02524       typedef CodeGenFunction::OpaqueValueMappingData OVMA;
02525       OVMA opaqueData;
02526 
02527       // If this semantic is the result of the pseudo-object
02528       // expression, try to evaluate the source as +1.
02529       if (ov == resultExpr) {
02530         assert(!OVMA::shouldBindAsLValue(ov));
02531         result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr());
02532         opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer()));
02533 
02534       // Otherwise, just bind it.
02535       } else {
02536         opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
02537       }
02538       opaques.push_back(opaqueData);
02539 
02540     // Otherwise, if the expression is the result, evaluate it
02541     // and remember the result.
02542     } else if (semantic == resultExpr) {
02543       result = tryEmitARCRetainScalarExpr(CGF, semantic);
02544 
02545     // Otherwise, evaluate the expression in an ignored context.
02546     } else {
02547       CGF.EmitIgnoredExpr(semantic);
02548     }
02549   }
02550 
02551   // Unbind all the opaques now.
02552   for (unsigned i = 0, e = opaques.size(); i != e; ++i)
02553     opaques[i].unbind(CGF);
02554 
02555   return result;
02556 }
02557 
02558 static TryEmitResult
02559 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
02560   // We should *never* see a nested full-expression here, because if
02561   // we fail to emit at +1, our caller must not retain after we close
02562   // out the full-expression.
02563   assert(!isa<ExprWithCleanups>(e));
02564 
02565   // The desired result type, if it differs from the type of the
02566   // ultimate opaque expression.
02567   llvm::Type *resultType = nullptr;
02568 
02569   while (true) {
02570     e = e->IgnoreParens();
02571 
02572     // There's a break at the end of this if-chain;  anything
02573     // that wants to keep looping has to explicitly continue.
02574     if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
02575       switch (ce->getCastKind()) {
02576       // No-op casts don't change the type, so we just ignore them.
02577       case CK_NoOp:
02578         e = ce->getSubExpr();
02579         continue;
02580 
02581       case CK_LValueToRValue: {
02582         TryEmitResult loadResult
02583           = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr());
02584         if (resultType) {
02585           llvm::Value *value = loadResult.getPointer();
02586           value = CGF.Builder.CreateBitCast(value, resultType);
02587           loadResult.setPointer(value);
02588         }
02589         return loadResult;
02590       }
02591 
02592       // These casts can change the type, so remember that and
02593       // soldier on.  We only need to remember the outermost such
02594       // cast, though.
02595       case CK_CPointerToObjCPointerCast:
02596       case CK_BlockPointerToObjCPointerCast:
02597       case CK_AnyPointerToBlockPointerCast:
02598       case CK_BitCast:
02599         if (!resultType)
02600           resultType = CGF.ConvertType(ce->getType());
02601         e = ce->getSubExpr();
02602         assert(e->getType()->hasPointerRepresentation());
02603         continue;
02604 
02605       // For consumptions, just emit the subexpression and thus elide
02606       // the retain/release pair.
02607       case CK_ARCConsumeObject: {
02608         llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr());
02609         if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
02610         return TryEmitResult(result, true);
02611       }
02612 
02613       // Block extends are net +0.  Naively, we could just recurse on
02614       // the subexpression, but actually we need to ensure that the
02615       // value is copied as a block, so there's a little filter here.
02616       case CK_ARCExtendBlockObject: {
02617         llvm::Value *result; // will be a +0 value
02618 
02619         // If we can't safely assume the sub-expression will produce a
02620         // block-copied value, emit the sub-expression at +0.
02621         if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) {
02622           result = CGF.EmitScalarExpr(ce->getSubExpr());
02623 
02624         // Otherwise, try to emit the sub-expression at +1 recursively.
02625         } else {
02626           TryEmitResult subresult
02627             = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr());
02628           result = subresult.getPointer();
02629 
02630           // If that produced a retained value, just use that,
02631           // possibly casting down.
02632           if (subresult.getInt()) {
02633             if (resultType)
02634               result = CGF.Builder.CreateBitCast(result, resultType);
02635             return TryEmitResult(result, true);
02636           }
02637 
02638           // Otherwise it's +0.
02639         }
02640 
02641         // Retain the object as a block, then cast down.
02642         result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
02643         if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
02644         return TryEmitResult(result, true);
02645       }
02646 
02647       // For reclaims, emit the subexpression as a retained call and
02648       // skip the consumption.
02649       case CK_ARCReclaimReturnedObject: {
02650         llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr());
02651         if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
02652         return TryEmitResult(result, true);
02653       }
02654 
02655       default:
02656         break;
02657       }
02658 
02659     // Skip __extension__.
02660     } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
02661       if (op->getOpcode() == UO_Extension) {
02662         e = op->getSubExpr();
02663         continue;
02664       }
02665 
02666     // For calls and message sends, use the retained-call logic.
02667     // Delegate inits are a special case in that they're the only
02668     // returns-retained expression that *isn't* surrounded by
02669     // a consume.
02670     } else if (isa<CallExpr>(e) ||
02671                (isa<ObjCMessageExpr>(e) &&
02672                 !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
02673       llvm::Value *result = emitARCRetainCall(CGF, e);
02674       if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
02675       return TryEmitResult(result, true);
02676 
02677     // Look through pseudo-object expressions.
02678     } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
02679       TryEmitResult result
02680         = tryEmitARCRetainPseudoObject(CGF, pseudo);
02681       if (resultType) {
02682         llvm::Value *value = result.getPointer();
02683         value = CGF.Builder.CreateBitCast(value, resultType);
02684         result.setPointer(value);
02685       }
02686       return result;
02687     }
02688 
02689     // Conservatively halt the search at any other expression kind.
02690     break;
02691   }
02692 
02693   // We didn't find an obvious production, so emit what we've got and
02694   // tell the caller that we didn't manage to retain.
02695   llvm::Value *result = CGF.EmitScalarExpr(e);
02696   if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
02697   return TryEmitResult(result, false);
02698 }
02699 
02700 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
02701                                                 LValue lvalue,
02702                                                 QualType type) {
02703   TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
02704   llvm::Value *value = result.getPointer();
02705   if (!result.getInt())
02706     value = CGF.EmitARCRetain(type, value);
02707   return value;
02708 }
02709 
02710 /// EmitARCRetainScalarExpr - Semantically equivalent to
02711 /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
02712 /// best-effort attempt to peephole expressions that naturally produce
02713 /// retained objects.
02714 llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
02715   // The retain needs to happen within the full-expression.
02716   if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
02717     enterFullExpression(cleanups);
02718     RunCleanupsScope scope(*this);
02719     return EmitARCRetainScalarExpr(cleanups->getSubExpr());
02720   }
02721 
02722   TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
02723   llvm::Value *value = result.getPointer();
02724   if (!result.getInt())
02725     value = EmitARCRetain(e->getType(), value);
02726   return value;
02727 }
02728 
02729 llvm::Value *
02730 CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
02731   // The retain needs to happen within the full-expression.
02732   if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
02733     enterFullExpression(cleanups);
02734     RunCleanupsScope scope(*this);
02735     return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
02736   }
02737 
02738   TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
02739   llvm::Value *value = result.getPointer();
02740   if (result.getInt())
02741     value = EmitARCAutorelease(value);
02742   else
02743     value = EmitARCRetainAutorelease(e->getType(), value);
02744   return value;
02745 }
02746 
02747 llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
02748   llvm::Value *result;
02749   bool doRetain;
02750 
02751   if (shouldEmitSeparateBlockRetain(e)) {
02752     result = EmitScalarExpr(e);
02753     doRetain = true;
02754   } else {
02755     TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
02756     result = subresult.getPointer();
02757     doRetain = !subresult.getInt();
02758   }
02759 
02760   if (doRetain)
02761     result = EmitARCRetainBlock(result, /*mandatory*/ true);
02762   return EmitObjCConsumeObject(e->getType(), result);
02763 }
02764 
02765 llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
02766   // In ARC, retain and autorelease the expression.
02767   if (getLangOpts().ObjCAutoRefCount) {
02768     // Do so before running any cleanups for the full-expression.
02769     // EmitARCRetainAutoreleaseScalarExpr does this for us.
02770     return EmitARCRetainAutoreleaseScalarExpr(expr);
02771   }
02772 
02773   // Otherwise, use the normal scalar-expression emission.  The
02774   // exception machinery doesn't do anything special with the
02775   // exception like retaining it, so there's no safety associated with
02776   // only running cleanups after the throw has started, and when it
02777   // matters it tends to be substantially inferior code.
02778   return EmitScalarExpr(expr);
02779 }
02780 
02781 std::pair<LValue,llvm::Value*>
02782 CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
02783                                     bool ignored) {
02784   // Evaluate the RHS first.
02785   TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
02786   llvm::Value *value = result.getPointer();
02787 
02788   bool hasImmediateRetain = result.getInt();
02789 
02790   // If we didn't emit a retained object, and the l-value is of block
02791   // type, then we need to emit the block-retain immediately in case
02792   // it invalidates the l-value.
02793   if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
02794     value = EmitARCRetainBlock(value, /*mandatory*/ false);
02795     hasImmediateRetain = true;
02796   }
02797 
02798   LValue lvalue = EmitLValue(e->getLHS());
02799 
02800   // If the RHS was emitted retained, expand this.
02801   if (hasImmediateRetain) {
02802     llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation());
02803     EmitStoreOfScalar(value, lvalue);
02804     EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
02805   } else {
02806     value = EmitARCStoreStrong(lvalue, value, ignored);
02807   }
02808 
02809   return std::pair<LValue,llvm::Value*>(lvalue, value);
02810 }
02811 
02812 std::pair<LValue,llvm::Value*>
02813 CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
02814   llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
02815   LValue lvalue = EmitLValue(e->getLHS());
02816 
02817   EmitStoreOfScalar(value, lvalue);
02818 
02819   return std::pair<LValue,llvm::Value*>(lvalue, value);
02820 }
02821 
02822 void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
02823                                           const ObjCAutoreleasePoolStmt &ARPS) {
02824   const Stmt *subStmt = ARPS.getSubStmt();
02825   const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
02826 
02827   CGDebugInfo *DI = getDebugInfo();
02828   if (DI)
02829     DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
02830 
02831   // Keep track of the current cleanup stack depth.
02832   RunCleanupsScope Scope(*this);
02833   if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
02834     llvm::Value *token = EmitObjCAutoreleasePoolPush();
02835     EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
02836   } else {
02837     llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
02838     EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
02839   }
02840 
02841   for (const auto *I : S.body())
02842     EmitStmt(I);
02843 
02844   if (DI)
02845     DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
02846 }
02847 
02848 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
02849 /// make sure it survives garbage collection until this point.
02850 void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
02851   // We just use an inline assembly.
02852   llvm::FunctionType *extenderType
02853     = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
02854   llvm::Value *extender
02855     = llvm::InlineAsm::get(extenderType,
02856                            /* assembly */ "",
02857                            /* constraints */ "r",
02858                            /* side effects */ true);
02859 
02860   object = Builder.CreateBitCast(object, VoidPtrTy);
02861   EmitNounwindRuntimeCall(extender, object);
02862 }
02863 
02864 /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
02865 /// non-trivial copy assignment function, produce following helper function.
02866 /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
02867 ///
02868 llvm::Constant *
02869 CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
02870                                         const ObjCPropertyImplDecl *PID) {
02871   if (!getLangOpts().CPlusPlus ||
02872       !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
02873     return nullptr;
02874   QualType Ty = PID->getPropertyIvarDecl()->getType();
02875   if (!Ty->isRecordType())
02876     return nullptr;
02877   const ObjCPropertyDecl *PD = PID->getPropertyDecl();
02878   if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
02879     return nullptr;
02880   llvm::Constant *HelperFn = nullptr;
02881   if (hasTrivialSetExpr(PID))
02882     return nullptr;
02883   assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
02884   if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
02885     return HelperFn;
02886   
02887   ASTContext &C = getContext();
02888   IdentifierInfo *II
02889     = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
02890   FunctionDecl *FD = FunctionDecl::Create(C,
02891                                           C.getTranslationUnitDecl(),
02892                                           SourceLocation(),
02893                                           SourceLocation(), II, C.VoidTy,
02894                                           nullptr, SC_Static,
02895                                           false,
02896                                           false);
02897 
02898   QualType DestTy = C.getPointerType(Ty);
02899   QualType SrcTy = Ty;
02900   SrcTy.addConst();
02901   SrcTy = C.getPointerType(SrcTy);
02902   
02903   FunctionArgList args;
02904   ImplicitParamDecl dstDecl(getContext(), FD, SourceLocation(), nullptr,DestTy);
02905   args.push_back(&dstDecl);
02906   ImplicitParamDecl srcDecl(getContext(), FD, SourceLocation(), nullptr, SrcTy);
02907   args.push_back(&srcDecl);
02908 
02909   const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
02910       C.VoidTy, args, FunctionType::ExtInfo(), RequiredArgs::All);
02911 
02912   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
02913   
02914   llvm::Function *Fn =
02915     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
02916                            "__assign_helper_atomic_property_",
02917                            &CGM.getModule());
02918   
02919   StartFunction(FD, C.VoidTy, Fn, FI, args);
02920   
02921   DeclRefExpr DstExpr(&dstDecl, false, DestTy,
02922                       VK_RValue, SourceLocation());
02923   UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(),
02924                     VK_LValue, OK_Ordinary, SourceLocation());
02925   
02926   DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
02927                       VK_RValue, SourceLocation());
02928   UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
02929                     VK_LValue, OK_Ordinary, SourceLocation());
02930   
02931   Expr *Args[2] = { &DST, &SRC };
02932   CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
02933   CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(),
02934                               Args, DestTy->getPointeeType(),
02935                               VK_LValue, SourceLocation(), false);
02936   
02937   EmitStmt(&TheCall);
02938 
02939   FinishFunction();
02940   HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
02941   CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
02942   return HelperFn;
02943 }
02944 
02945 llvm::Constant *
02946 CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
02947                                             const ObjCPropertyImplDecl *PID) {
02948   if (!getLangOpts().CPlusPlus ||
02949       !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
02950     return nullptr;
02951   const ObjCPropertyDecl *PD = PID->getPropertyDecl();
02952   QualType Ty = PD->getType();
02953   if (!Ty->isRecordType())
02954     return nullptr;
02955   if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
02956     return nullptr;
02957   llvm::Constant *HelperFn = nullptr;
02958 
02959   if (hasTrivialGetExpr(PID))
02960     return nullptr;
02961   assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
02962   if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
02963     return HelperFn;
02964   
02965   
02966   ASTContext &C = getContext();
02967   IdentifierInfo *II
02968   = &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
02969   FunctionDecl *FD = FunctionDecl::Create(C,
02970                                           C.getTranslationUnitDecl(),
02971                                           SourceLocation(),
02972                                           SourceLocation(), II, C.VoidTy,
02973                                           nullptr, SC_Static,
02974                                           false,
02975                                           false);
02976 
02977   QualType DestTy = C.getPointerType(Ty);
02978   QualType SrcTy = Ty;
02979   SrcTy.addConst();
02980   SrcTy = C.getPointerType(SrcTy);
02981   
02982   FunctionArgList args;
02983   ImplicitParamDecl dstDecl(getContext(), FD, SourceLocation(), nullptr,DestTy);
02984   args.push_back(&dstDecl);
02985   ImplicitParamDecl srcDecl(getContext(), FD, SourceLocation(), nullptr, SrcTy);
02986   args.push_back(&srcDecl);
02987 
02988   const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
02989       C.VoidTy, args, FunctionType::ExtInfo(), RequiredArgs::All);
02990 
02991   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
02992   
02993   llvm::Function *Fn =
02994   llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
02995                          "__copy_helper_atomic_property_", &CGM.getModule());
02996   
02997   StartFunction(FD, C.VoidTy, Fn, FI, args);
02998   
02999   DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
03000                       VK_RValue, SourceLocation());
03001   
03002   UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
03003                     VK_LValue, OK_Ordinary, SourceLocation());
03004   
03005   CXXConstructExpr *CXXConstExpr = 
03006     cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
03007   
03008   SmallVector<Expr*, 4> ConstructorArgs;
03009   ConstructorArgs.push_back(&SRC);
03010   CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin();
03011   ++A;
03012   
03013   for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end();
03014        A != AEnd; ++A)
03015     ConstructorArgs.push_back(*A);
03016   
03017   CXXConstructExpr *TheCXXConstructExpr =
03018     CXXConstructExpr::Create(C, Ty, SourceLocation(),
03019                              CXXConstExpr->getConstructor(),
03020                              CXXConstExpr->isElidable(),
03021                              ConstructorArgs,
03022                              CXXConstExpr->hadMultipleCandidates(),
03023                              CXXConstExpr->isListInitialization(),
03024                              CXXConstExpr->isStdInitListInitialization(),
03025                              CXXConstExpr->requiresZeroInitialization(),
03026                              CXXConstExpr->getConstructionKind(),
03027                              SourceRange());
03028   
03029   DeclRefExpr DstExpr(&dstDecl, false, DestTy,
03030                       VK_RValue, SourceLocation());
03031   
03032   RValue DV = EmitAnyExpr(&DstExpr);
03033   CharUnits Alignment
03034     = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
03035   EmitAggExpr(TheCXXConstructExpr, 
03036               AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(),
03037                                     AggValueSlot::IsDestructed,
03038                                     AggValueSlot::DoesNotNeedGCBarriers,
03039                                     AggValueSlot::IsNotAliased));
03040   
03041   FinishFunction();
03042   HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
03043   CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
03044   return HelperFn;
03045 }
03046 
03047 llvm::Value *
03048 CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
03049   // Get selectors for retain/autorelease.
03050   IdentifierInfo *CopyID = &getContext().Idents.get("copy");
03051   Selector CopySelector =
03052       getContext().Selectors.getNullarySelector(CopyID);
03053   IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
03054   Selector AutoreleaseSelector =
03055       getContext().Selectors.getNullarySelector(AutoreleaseID);
03056 
03057   // Emit calls to retain/autorelease.
03058   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
03059   llvm::Value *Val = Block;
03060   RValue Result;
03061   Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
03062                                        Ty, CopySelector,
03063                                        Val, CallArgList(), nullptr, nullptr);
03064   Val = Result.getScalarVal();
03065   Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
03066                                        Ty, AutoreleaseSelector,
03067                                        Val, CallArgList(), nullptr, nullptr);
03068   Val = Result.getScalarVal();
03069   return Val;
03070 }
03071 
03072 
03073 CGObjCRuntime::~CGObjCRuntime() {}