]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/clang/lib/CodeGen/CGObjC.cpp
Update to bmake-20201101
[FreeBSD/FreeBSD.git] / contrib / llvm-project / clang / lib / CodeGen / CGObjC.cpp
1 //===---- CGObjC.cpp - Emit LLVM Code for Objective-C ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit Objective-C code as LLVM code.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "CGDebugInfo.h"
14 #include "CGObjCRuntime.h"
15 #include "CodeGenFunction.h"
16 #include "CodeGenModule.h"
17 #include "ConstantEmitter.h"
18 #include "TargetInfo.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Attr.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/StmtObjC.h"
23 #include "clang/Basic/Diagnostic.h"
24 #include "clang/CodeGen/CGFunctionInfo.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/InlineAsm.h"
28 using namespace clang;
29 using namespace CodeGen;
30
31 typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
32 static TryEmitResult
33 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e);
34 static RValue AdjustObjCObjectType(CodeGenFunction &CGF,
35                                    QualType ET,
36                                    RValue Result);
37
38 /// Given the address of a variable of pointer type, find the correct
39 /// null to store into it.
40 static llvm::Constant *getNullForVariable(Address addr) {
41   llvm::Type *type = addr.getElementType();
42   return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
43 }
44
45 /// Emits an instance of NSConstantString representing the object.
46 llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
47 {
48   llvm::Constant *C =
49       CGM.getObjCRuntime().GenerateConstantString(E->getString()).getPointer();
50   // FIXME: This bitcast should just be made an invariant on the Runtime.
51   return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
52 }
53
54 /// EmitObjCBoxedExpr - This routine generates code to call
55 /// the appropriate expression boxing method. This will either be
56 /// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:],
57 /// or [NSValue valueWithBytes:objCType:].
58 ///
59 llvm::Value *
60 CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) {
61   // Generate the correct selector for this literal's concrete type.
62   // Get the method.
63   const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod();
64   const Expr *SubExpr = E->getSubExpr();
65
66   if (E->isExpressibleAsConstantInitializer()) {
67     ConstantEmitter ConstEmitter(CGM);
68     return ConstEmitter.tryEmitAbstract(E, E->getType());
69   }
70
71   assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method");
72   Selector Sel = BoxingMethod->getSelector();
73
74   // Generate a reference to the class pointer, which will be the receiver.
75   // Assumes that the method was introduced in the class that should be
76   // messaged (avoids pulling it out of the result type).
77   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
78   const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface();
79   llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl);
80
81   CallArgList Args;
82   const ParmVarDecl *ArgDecl = *BoxingMethod->param_begin();
83   QualType ArgQT = ArgDecl->getType().getUnqualifiedType();
84
85   // ObjCBoxedExpr supports boxing of structs and unions
86   // via [NSValue valueWithBytes:objCType:]
87   const QualType ValueType(SubExpr->getType().getCanonicalType());
88   if (ValueType->isObjCBoxableRecordType()) {
89     // Emit CodeGen for first parameter
90     // and cast value to correct type
91     Address Temporary = CreateMemTemp(SubExpr->getType());
92     EmitAnyExprToMem(SubExpr, Temporary, Qualifiers(), /*isInit*/ true);
93     Address BitCast = Builder.CreateBitCast(Temporary, ConvertType(ArgQT));
94     Args.add(RValue::get(BitCast.getPointer()), ArgQT);
95
96     // Create char array to store type encoding
97     std::string Str;
98     getContext().getObjCEncodingForType(ValueType, Str);
99     llvm::Constant *GV = CGM.GetAddrOfConstantCString(Str).getPointer();
100
101     // Cast type encoding to correct type
102     const ParmVarDecl *EncodingDecl = BoxingMethod->parameters()[1];
103     QualType EncodingQT = EncodingDecl->getType().getUnqualifiedType();
104     llvm::Value *Cast = Builder.CreateBitCast(GV, ConvertType(EncodingQT));
105
106     Args.add(RValue::get(Cast), EncodingQT);
107   } else {
108     Args.add(EmitAnyExpr(SubExpr), ArgQT);
109   }
110
111   RValue result = Runtime.GenerateMessageSend(
112       *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver,
113       Args, ClassDecl, BoxingMethod);
114   return Builder.CreateBitCast(result.getScalarVal(),
115                                ConvertType(E->getType()));
116 }
117
118 llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E,
119                                     const ObjCMethodDecl *MethodWithObjects) {
120   ASTContext &Context = CGM.getContext();
121   const ObjCDictionaryLiteral *DLE = nullptr;
122   const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E);
123   if (!ALE)
124     DLE = cast<ObjCDictionaryLiteral>(E);
125
126   // Optimize empty collections by referencing constants, when available.
127   uint64_t NumElements =
128     ALE ? ALE->getNumElements() : DLE->getNumElements();
129   if (NumElements == 0 && CGM.getLangOpts().ObjCRuntime.hasEmptyCollections()) {
130     StringRef ConstantName = ALE ? "__NSArray0__" : "__NSDictionary0__";
131     QualType IdTy(CGM.getContext().getObjCIdType());
132     llvm::Constant *Constant =
133         CGM.CreateRuntimeVariable(ConvertType(IdTy), ConstantName);
134     LValue LV = MakeNaturalAlignAddrLValue(Constant, IdTy);
135     llvm::Value *Ptr = EmitLoadOfScalar(LV, E->getBeginLoc());
136     cast<llvm::LoadInst>(Ptr)->setMetadata(
137         CGM.getModule().getMDKindID("invariant.load"),
138         llvm::MDNode::get(getLLVMContext(), None));
139     return Builder.CreateBitCast(Ptr, ConvertType(E->getType()));
140   }
141
142   // Compute the type of the array we're initializing.
143   llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
144                             NumElements);
145   QualType ElementType = Context.getObjCIdType().withConst();
146   QualType ElementArrayType
147     = Context.getConstantArrayType(ElementType, APNumElements, nullptr,
148                                    ArrayType::Normal, /*IndexTypeQuals=*/0);
149
150   // Allocate the temporary array(s).
151   Address Objects = CreateMemTemp(ElementArrayType, "objects");
152   Address Keys = Address::invalid();
153   if (DLE)
154     Keys = CreateMemTemp(ElementArrayType, "keys");
155
156   // In ARC, we may need to do extra work to keep all the keys and
157   // values alive until after the call.
158   SmallVector<llvm::Value *, 16> NeededObjects;
159   bool TrackNeededObjects =
160     (getLangOpts().ObjCAutoRefCount &&
161     CGM.getCodeGenOpts().OptimizationLevel != 0);
162
163   // Perform the actual initialialization of the array(s).
164   for (uint64_t i = 0; i < NumElements; i++) {
165     if (ALE) {
166       // Emit the element and store it to the appropriate array slot.
167       const Expr *Rhs = ALE->getElement(i);
168       LValue LV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i),
169                                  ElementType, AlignmentSource::Decl);
170
171       llvm::Value *value = EmitScalarExpr(Rhs);
172       EmitStoreThroughLValue(RValue::get(value), LV, true);
173       if (TrackNeededObjects) {
174         NeededObjects.push_back(value);
175       }
176     } else {
177       // Emit the key and store it to the appropriate array slot.
178       const Expr *Key = DLE->getKeyValueElement(i).Key;
179       LValue KeyLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Keys, i),
180                                     ElementType, AlignmentSource::Decl);
181       llvm::Value *keyValue = EmitScalarExpr(Key);
182       EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
183
184       // Emit the value and store it to the appropriate array slot.
185       const Expr *Value = DLE->getKeyValueElement(i).Value;
186       LValue ValueLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i),
187                                       ElementType, AlignmentSource::Decl);
188       llvm::Value *valueValue = EmitScalarExpr(Value);
189       EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
190       if (TrackNeededObjects) {
191         NeededObjects.push_back(keyValue);
192         NeededObjects.push_back(valueValue);
193       }
194     }
195   }
196
197   // Generate the argument list.
198   CallArgList Args;
199   ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
200   const ParmVarDecl *argDecl = *PI++;
201   QualType ArgQT = argDecl->getType().getUnqualifiedType();
202   Args.add(RValue::get(Objects.getPointer()), ArgQT);
203   if (DLE) {
204     argDecl = *PI++;
205     ArgQT = argDecl->getType().getUnqualifiedType();
206     Args.add(RValue::get(Keys.getPointer()), ArgQT);
207   }
208   argDecl = *PI;
209   ArgQT = argDecl->getType().getUnqualifiedType();
210   llvm::Value *Count =
211     llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
212   Args.add(RValue::get(Count), ArgQT);
213
214   // Generate a reference to the class pointer, which will be the receiver.
215   Selector Sel = MethodWithObjects->getSelector();
216   QualType ResultType = E->getType();
217   const ObjCObjectPointerType *InterfacePointerType
218     = ResultType->getAsObjCInterfacePointerType();
219   ObjCInterfaceDecl *Class
220     = InterfacePointerType->getObjectType()->getInterface();
221   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
222   llvm::Value *Receiver = Runtime.GetClass(*this, Class);
223
224   // Generate the message send.
225   RValue result = Runtime.GenerateMessageSend(
226       *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel,
227       Receiver, Args, Class, MethodWithObjects);
228
229   // The above message send needs these objects, but in ARC they are
230   // passed in a buffer that is essentially __unsafe_unretained.
231   // Therefore we must prevent the optimizer from releasing them until
232   // after the call.
233   if (TrackNeededObjects) {
234     EmitARCIntrinsicUse(NeededObjects);
235   }
236
237   return Builder.CreateBitCast(result.getScalarVal(),
238                                ConvertType(E->getType()));
239 }
240
241 llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) {
242   return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod());
243 }
244
245 llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral(
246                                             const ObjCDictionaryLiteral *E) {
247   return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod());
248 }
249
250 /// Emit a selector.
251 llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
252   // Untyped selector.
253   // Note that this implementation allows for non-constant strings to be passed
254   // as arguments to @selector().  Currently, the only thing preventing this
255   // behaviour is the type checking in the front end.
256   return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
257 }
258
259 llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
260   // FIXME: This should pass the Decl not the name.
261   return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
262 }
263
264 /// Adjust the type of an Objective-C object that doesn't match up due
265 /// to type erasure at various points, e.g., related result types or the use
266 /// of parameterized classes.
267 static RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ExpT,
268                                    RValue Result) {
269   if (!ExpT->isObjCRetainableType())
270     return Result;
271
272   // If the converted types are the same, we're done.
273   llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT);
274   if (ExpLLVMTy == Result.getScalarVal()->getType())
275     return Result;
276
277   // We have applied a substitution. Cast the rvalue appropriately.
278   return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
279                                                ExpLLVMTy));
280 }
281
282 /// Decide whether to extend the lifetime of the receiver of a
283 /// returns-inner-pointer message.
284 static bool
285 shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
286   switch (message->getReceiverKind()) {
287
288   // For a normal instance message, we should extend unless the
289   // receiver is loaded from a variable with precise lifetime.
290   case ObjCMessageExpr::Instance: {
291     const Expr *receiver = message->getInstanceReceiver();
292
293     // Look through OVEs.
294     if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
295       if (opaque->getSourceExpr())
296         receiver = opaque->getSourceExpr()->IgnoreParens();
297     }
298
299     const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
300     if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
301     receiver = ice->getSubExpr()->IgnoreParens();
302
303     // Look through OVEs.
304     if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
305       if (opaque->getSourceExpr())
306         receiver = opaque->getSourceExpr()->IgnoreParens();
307     }
308
309     // Only __strong variables.
310     if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
311       return true;
312
313     // All ivars and fields have precise lifetime.
314     if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
315       return false;
316
317     // Otherwise, check for variables.
318     const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
319     if (!declRef) return true;
320     const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
321     if (!var) return true;
322
323     // All variables have precise lifetime except local variables with
324     // automatic storage duration that aren't specially marked.
325     return (var->hasLocalStorage() &&
326             !var->hasAttr<ObjCPreciseLifetimeAttr>());
327   }
328
329   case ObjCMessageExpr::Class:
330   case ObjCMessageExpr::SuperClass:
331     // It's never necessary for class objects.
332     return false;
333
334   case ObjCMessageExpr::SuperInstance:
335     // We generally assume that 'self' lives throughout a method call.
336     return false;
337   }
338
339   llvm_unreachable("invalid receiver kind");
340 }
341
342 /// Given an expression of ObjC pointer type, check whether it was
343 /// immediately loaded from an ARC __weak l-value.
344 static const Expr *findWeakLValue(const Expr *E) {
345   assert(E->getType()->isObjCRetainableType());
346   E = E->IgnoreParens();
347   if (auto CE = dyn_cast<CastExpr>(E)) {
348     if (CE->getCastKind() == CK_LValueToRValue) {
349       if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
350         return CE->getSubExpr();
351     }
352   }
353
354   return nullptr;
355 }
356
357 /// The ObjC runtime may provide entrypoints that are likely to be faster
358 /// than an ordinary message send of the appropriate selector.
359 ///
360 /// The entrypoints are guaranteed to be equivalent to just sending the
361 /// corresponding message.  If the entrypoint is implemented naively as just a
362 /// message send, using it is a trade-off: it sacrifices a few cycles of
363 /// overhead to save a small amount of code.  However, it's possible for
364 /// runtimes to detect and special-case classes that use "standard"
365 /// behavior; if that's dynamically a large proportion of all objects, using
366 /// the entrypoint will also be faster than using a message send.
367 ///
368 /// If the runtime does support a required entrypoint, then this method will
369 /// generate a call and return the resulting value.  Otherwise it will return
370 /// None and the caller can generate a msgSend instead.
371 static Optional<llvm::Value *>
372 tryGenerateSpecializedMessageSend(CodeGenFunction &CGF, QualType ResultType,
373                                   llvm::Value *Receiver,
374                                   const CallArgList& Args, Selector Sel,
375                                   const ObjCMethodDecl *method,
376                                   bool isClassMessage) {
377   auto &CGM = CGF.CGM;
378   if (!CGM.getCodeGenOpts().ObjCConvertMessagesToRuntimeCalls)
379     return None;
380
381   auto &Runtime = CGM.getLangOpts().ObjCRuntime;
382   switch (Sel.getMethodFamily()) {
383   case OMF_alloc:
384     if (isClassMessage &&
385         Runtime.shouldUseRuntimeFunctionsForAlloc() &&
386         ResultType->isObjCObjectPointerType()) {
387         // [Foo alloc] -> objc_alloc(Foo) or
388         // [self alloc] -> objc_alloc(self)
389         if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "alloc")
390           return CGF.EmitObjCAlloc(Receiver, CGF.ConvertType(ResultType));
391         // [Foo allocWithZone:nil] -> objc_allocWithZone(Foo) or
392         // [self allocWithZone:nil] -> objc_allocWithZone(self)
393         if (Sel.isKeywordSelector() && Sel.getNumArgs() == 1 &&
394             Args.size() == 1 && Args.front().getType()->isPointerType() &&
395             Sel.getNameForSlot(0) == "allocWithZone") {
396           const llvm::Value* arg = Args.front().getKnownRValue().getScalarVal();
397           if (isa<llvm::ConstantPointerNull>(arg))
398             return CGF.EmitObjCAllocWithZone(Receiver,
399                                              CGF.ConvertType(ResultType));
400           return None;
401         }
402     }
403     break;
404
405   case OMF_autorelease:
406     if (ResultType->isObjCObjectPointerType() &&
407         CGM.getLangOpts().getGC() == LangOptions::NonGC &&
408         Runtime.shouldUseARCFunctionsForRetainRelease())
409       return CGF.EmitObjCAutorelease(Receiver, CGF.ConvertType(ResultType));
410     break;
411
412   case OMF_retain:
413     if (ResultType->isObjCObjectPointerType() &&
414         CGM.getLangOpts().getGC() == LangOptions::NonGC &&
415         Runtime.shouldUseARCFunctionsForRetainRelease())
416       return CGF.EmitObjCRetainNonBlock(Receiver, CGF.ConvertType(ResultType));
417     break;
418
419   case OMF_release:
420     if (ResultType->isVoidType() &&
421         CGM.getLangOpts().getGC() == LangOptions::NonGC &&
422         Runtime.shouldUseARCFunctionsForRetainRelease()) {
423       CGF.EmitObjCRelease(Receiver, ARCPreciseLifetime);
424       return nullptr;
425     }
426     break;
427
428   default:
429     break;
430   }
431   return None;
432 }
433
434 CodeGen::RValue CGObjCRuntime::GeneratePossiblySpecializedMessageSend(
435     CodeGenFunction &CGF, ReturnValueSlot Return, QualType ResultType,
436     Selector Sel, llvm::Value *Receiver, const CallArgList &Args,
437     const ObjCInterfaceDecl *OID, const ObjCMethodDecl *Method,
438     bool isClassMessage) {
439   if (Optional<llvm::Value *> SpecializedResult =
440           tryGenerateSpecializedMessageSend(CGF, ResultType, Receiver, Args,
441                                             Sel, Method, isClassMessage)) {
442     return RValue::get(SpecializedResult.getValue());
443   }
444   return GenerateMessageSend(CGF, Return, ResultType, Sel, Receiver, Args, OID,
445                              Method);
446 }
447
448 /// Instead of '[[MyClass alloc] init]', try to generate
449 /// 'objc_alloc_init(MyClass)'. This provides a code size improvement on the
450 /// caller side, as well as the optimized objc_alloc.
451 static Optional<llvm::Value *>
452 tryEmitSpecializedAllocInit(CodeGenFunction &CGF, const ObjCMessageExpr *OME) {
453   auto &Runtime = CGF.getLangOpts().ObjCRuntime;
454   if (!Runtime.shouldUseRuntimeFunctionForCombinedAllocInit())
455     return None;
456
457   // Match the exact pattern '[[MyClass alloc] init]'.
458   Selector Sel = OME->getSelector();
459   if (OME->getReceiverKind() != ObjCMessageExpr::Instance ||
460       !OME->getType()->isObjCObjectPointerType() || !Sel.isUnarySelector() ||
461       Sel.getNameForSlot(0) != "init")
462     return None;
463
464   // Okay, this is '[receiver init]', check if 'receiver' is '[cls alloc]'
465   // with 'cls' a Class.
466   auto *SubOME =
467       dyn_cast<ObjCMessageExpr>(OME->getInstanceReceiver()->IgnoreParenCasts());
468   if (!SubOME)
469     return None;
470   Selector SubSel = SubOME->getSelector();
471
472   if (!SubOME->getType()->isObjCObjectPointerType() ||
473       !SubSel.isUnarySelector() || SubSel.getNameForSlot(0) != "alloc")
474     return None;
475
476   llvm::Value *Receiver = nullptr;
477   switch (SubOME->getReceiverKind()) {
478   case ObjCMessageExpr::Instance:
479     if (!SubOME->getInstanceReceiver()->getType()->isObjCClassType())
480       return None;
481     Receiver = CGF.EmitScalarExpr(SubOME->getInstanceReceiver());
482     break;
483
484   case ObjCMessageExpr::Class: {
485     QualType ReceiverType = SubOME->getClassReceiver();
486     const ObjCObjectType *ObjTy = ReceiverType->castAs<ObjCObjectType>();
487     const ObjCInterfaceDecl *ID = ObjTy->getInterface();
488     assert(ID && "null interface should be impossible here");
489     Receiver = CGF.CGM.getObjCRuntime().GetClass(CGF, ID);
490     break;
491   }
492   case ObjCMessageExpr::SuperInstance:
493   case ObjCMessageExpr::SuperClass:
494     return None;
495   }
496
497   return CGF.EmitObjCAllocInit(Receiver, CGF.ConvertType(OME->getType()));
498 }
499
500 RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
501                                             ReturnValueSlot Return) {
502   // Only the lookup mechanism and first two arguments of the method
503   // implementation vary between runtimes.  We can get the receiver and
504   // arguments in generic code.
505
506   bool isDelegateInit = E->isDelegateInitCall();
507
508   const ObjCMethodDecl *method = E->getMethodDecl();
509
510   // If the method is -retain, and the receiver's being loaded from
511   // a __weak variable, peephole the entire operation to objc_loadWeakRetained.
512   if (method && E->getReceiverKind() == ObjCMessageExpr::Instance &&
513       method->getMethodFamily() == OMF_retain) {
514     if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) {
515       LValue lvalue = EmitLValue(lvalueExpr);
516       llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress(*this));
517       return AdjustObjCObjectType(*this, E->getType(), RValue::get(result));
518     }
519   }
520
521   if (Optional<llvm::Value *> Val = tryEmitSpecializedAllocInit(*this, E))
522     return AdjustObjCObjectType(*this, E->getType(), RValue::get(*Val));
523
524   // We don't retain the receiver in delegate init calls, and this is
525   // safe because the receiver value is always loaded from 'self',
526   // which we zero out.  We don't want to Block_copy block receivers,
527   // though.
528   bool retainSelf =
529     (!isDelegateInit &&
530      CGM.getLangOpts().ObjCAutoRefCount &&
531      method &&
532      method->hasAttr<NSConsumesSelfAttr>());
533
534   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
535   bool isSuperMessage = false;
536   bool isClassMessage = false;
537   ObjCInterfaceDecl *OID = nullptr;
538   // Find the receiver
539   QualType ReceiverType;
540   llvm::Value *Receiver = nullptr;
541   switch (E->getReceiverKind()) {
542   case ObjCMessageExpr::Instance:
543     ReceiverType = E->getInstanceReceiver()->getType();
544     isClassMessage = ReceiverType->isObjCClassType();
545     if (retainSelf) {
546       TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
547                                                    E->getInstanceReceiver());
548       Receiver = ter.getPointer();
549       if (ter.getInt()) retainSelf = false;
550     } else
551       Receiver = EmitScalarExpr(E->getInstanceReceiver());
552     break;
553
554   case ObjCMessageExpr::Class: {
555     ReceiverType = E->getClassReceiver();
556     OID = ReceiverType->castAs<ObjCObjectType>()->getInterface();
557     assert(OID && "Invalid Objective-C class message send");
558     Receiver = Runtime.GetClass(*this, OID);
559     isClassMessage = true;
560     break;
561   }
562
563   case ObjCMessageExpr::SuperInstance:
564     ReceiverType = E->getSuperType();
565     Receiver = LoadObjCSelf();
566     isSuperMessage = true;
567     break;
568
569   case ObjCMessageExpr::SuperClass:
570     ReceiverType = E->getSuperType();
571     Receiver = LoadObjCSelf();
572     isSuperMessage = true;
573     isClassMessage = true;
574     break;
575   }
576
577   if (retainSelf)
578     Receiver = EmitARCRetainNonBlock(Receiver);
579
580   // In ARC, we sometimes want to "extend the lifetime"
581   // (i.e. retain+autorelease) of receivers of returns-inner-pointer
582   // messages.
583   if (getLangOpts().ObjCAutoRefCount && method &&
584       method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
585       shouldExtendReceiverForInnerPointerMessage(E))
586     Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
587
588   QualType ResultType = method ? method->getReturnType() : E->getType();
589
590   CallArgList Args;
591   EmitCallArgs(Args, method, E->arguments(), /*AC*/AbstractCallee(method));
592
593   // For delegate init calls in ARC, do an unsafe store of null into
594   // self.  This represents the call taking direct ownership of that
595   // value.  We have to do this after emitting the other call
596   // arguments because they might also reference self, but we don't
597   // have to worry about any of them modifying self because that would
598   // be an undefined read and write of an object in unordered
599   // expressions.
600   if (isDelegateInit) {
601     assert(getLangOpts().ObjCAutoRefCount &&
602            "delegate init calls should only be marked in ARC");
603
604     // Do an unsafe store of null into self.
605     Address selfAddr =
606       GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl());
607     Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
608   }
609
610   RValue result;
611   if (isSuperMessage) {
612     // super is only valid in an Objective-C method
613     const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
614     bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
615     result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
616                                               E->getSelector(),
617                                               OMD->getClassInterface(),
618                                               isCategoryImpl,
619                                               Receiver,
620                                               isClassMessage,
621                                               Args,
622                                               method);
623   } else {
624     // Call runtime methods directly if we can.
625     result = Runtime.GeneratePossiblySpecializedMessageSend(
626         *this, Return, ResultType, E->getSelector(), Receiver, Args, OID,
627         method, isClassMessage);
628   }
629
630   // For delegate init calls in ARC, implicitly store the result of
631   // the call back into self.  This takes ownership of the value.
632   if (isDelegateInit) {
633     Address selfAddr =
634       GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl());
635     llvm::Value *newSelf = result.getScalarVal();
636
637     // The delegate return type isn't necessarily a matching type; in
638     // fact, it's quite likely to be 'id'.
639     llvm::Type *selfTy = selfAddr.getElementType();
640     newSelf = Builder.CreateBitCast(newSelf, selfTy);
641
642     Builder.CreateStore(newSelf, selfAddr);
643   }
644
645   return AdjustObjCObjectType(*this, E->getType(), result);
646 }
647
648 namespace {
649 struct FinishARCDealloc final : EHScopeStack::Cleanup {
650   void Emit(CodeGenFunction &CGF, Flags flags) override {
651     const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
652
653     const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
654     const ObjCInterfaceDecl *iface = impl->getClassInterface();
655     if (!iface->getSuperClass()) return;
656
657     bool isCategory = isa<ObjCCategoryImplDecl>(impl);
658
659     // Call [super dealloc] if we have a superclass.
660     llvm::Value *self = CGF.LoadObjCSelf();
661
662     CallArgList args;
663     CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
664                                                       CGF.getContext().VoidTy,
665                                                       method->getSelector(),
666                                                       iface,
667                                                       isCategory,
668                                                       self,
669                                                       /*is class msg*/ false,
670                                                       args,
671                                                       method);
672   }
673 };
674 }
675
676 /// StartObjCMethod - Begin emission of an ObjCMethod. This generates
677 /// the LLVM function and sets the other context used by
678 /// CodeGenFunction.
679 void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
680                                       const ObjCContainerDecl *CD) {
681   SourceLocation StartLoc = OMD->getBeginLoc();
682   FunctionArgList args;
683   // Check if we should generate debug info for this method.
684   if (OMD->hasAttr<NoDebugAttr>())
685     DebugInfo = nullptr; // disable debug info indefinitely for this function
686
687   llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
688
689   const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
690   if (OMD->isDirectMethod()) {
691     Fn->setVisibility(llvm::Function::HiddenVisibility);
692     CGM.SetLLVMFunctionAttributes(OMD, FI, Fn);
693     CGM.SetLLVMFunctionAttributesForDefinition(OMD, Fn);
694   } else {
695     CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
696   }
697
698   args.push_back(OMD->getSelfDecl());
699   args.push_back(OMD->getCmdDecl());
700
701   args.append(OMD->param_begin(), OMD->param_end());
702
703   CurGD = OMD;
704   CurEHLocation = OMD->getEndLoc();
705
706   StartFunction(OMD, OMD->getReturnType(), Fn, FI, args,
707                 OMD->getLocation(), StartLoc);
708
709   if (OMD->isDirectMethod()) {
710     // This function is a direct call, it has to implement a nil check
711     // on entry.
712     //
713     // TODO: possibly have several entry points to elide the check
714     CGM.getObjCRuntime().GenerateDirectMethodPrologue(*this, Fn, OMD, CD);
715   }
716
717   // In ARC, certain methods get an extra cleanup.
718   if (CGM.getLangOpts().ObjCAutoRefCount &&
719       OMD->isInstanceMethod() &&
720       OMD->getSelector().isUnarySelector()) {
721     const IdentifierInfo *ident =
722       OMD->getSelector().getIdentifierInfoForSlot(0);
723     if (ident->isStr("dealloc"))
724       EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
725   }
726 }
727
728 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
729                                               LValue lvalue, QualType type);
730
731 /// Generate an Objective-C method.  An Objective-C method is a C function with
732 /// its pointer, name, and types registered in the class structure.
733 void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
734   StartObjCMethod(OMD, OMD->getClassInterface());
735   PGO.assignRegionCounters(GlobalDecl(OMD), CurFn);
736   assert(isa<CompoundStmt>(OMD->getBody()));
737   incrementProfileCounter(OMD->getBody());
738   EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));
739   FinishFunction(OMD->getBodyRBrace());
740 }
741
742 /// emitStructGetterCall - Call the runtime function to load a property
743 /// into the return value slot.
744 static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar,
745                                  bool isAtomic, bool hasStrong) {
746   ASTContext &Context = CGF.getContext();
747
748   Address src =
749       CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
750           .getAddress(CGF);
751
752   // objc_copyStruct (ReturnValue, &structIvar,
753   //                  sizeof (Type of Ivar), isAtomic, false);
754   CallArgList args;
755
756   Address dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
757   args.add(RValue::get(dest.getPointer()), Context.VoidPtrTy);
758
759   src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
760   args.add(RValue::get(src.getPointer()), Context.VoidPtrTy);
761
762   CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
763   args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
764   args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
765   args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
766
767   llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
768   CGCallee callee = CGCallee::forDirect(fn);
769   CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args),
770                callee, ReturnValueSlot(), args);
771 }
772
773 /// Determine whether the given architecture supports unaligned atomic
774 /// accesses.  They don't have to be fast, just faster than a function
775 /// call and a mutex.
776 static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
777   // FIXME: Allow unaligned atomic load/store on x86.  (It is not
778   // currently supported by the backend.)
779   return 0;
780 }
781
782 /// Return the maximum size that permits atomic accesses for the given
783 /// architecture.
784 static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
785                                         llvm::Triple::ArchType arch) {
786   // ARM has 8-byte atomic accesses, but it's not clear whether we
787   // want to rely on them here.
788
789   // In the default case, just assume that any size up to a pointer is
790   // fine given adequate alignment.
791   return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
792 }
793
794 namespace {
795   class PropertyImplStrategy {
796   public:
797     enum StrategyKind {
798       /// The 'native' strategy is to use the architecture's provided
799       /// reads and writes.
800       Native,
801
802       /// Use objc_setProperty and objc_getProperty.
803       GetSetProperty,
804
805       /// Use objc_setProperty for the setter, but use expression
806       /// evaluation for the getter.
807       SetPropertyAndExpressionGet,
808
809       /// Use objc_copyStruct.
810       CopyStruct,
811
812       /// The 'expression' strategy is to emit normal assignment or
813       /// lvalue-to-rvalue expressions.
814       Expression
815     };
816
817     StrategyKind getKind() const { return StrategyKind(Kind); }
818
819     bool hasStrongMember() const { return HasStrong; }
820     bool isAtomic() const { return IsAtomic; }
821     bool isCopy() const { return IsCopy; }
822
823     CharUnits getIvarSize() const { return IvarSize; }
824     CharUnits getIvarAlignment() const { return IvarAlignment; }
825
826     PropertyImplStrategy(CodeGenModule &CGM,
827                          const ObjCPropertyImplDecl *propImpl);
828
829   private:
830     unsigned Kind : 8;
831     unsigned IsAtomic : 1;
832     unsigned IsCopy : 1;
833     unsigned HasStrong : 1;
834
835     CharUnits IvarSize;
836     CharUnits IvarAlignment;
837   };
838 }
839
840 /// Pick an implementation strategy for the given property synthesis.
841 PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
842                                      const ObjCPropertyImplDecl *propImpl) {
843   const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
844   ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
845
846   IsCopy = (setterKind == ObjCPropertyDecl::Copy);
847   IsAtomic = prop->isAtomic();
848   HasStrong = false; // doesn't matter here.
849
850   // Evaluate the ivar's size and alignment.
851   ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
852   QualType ivarType = ivar->getType();
853   std::tie(IvarSize, IvarAlignment) =
854       CGM.getContext().getTypeInfoInChars(ivarType);
855
856   // If we have a copy property, we always have to use getProperty/setProperty.
857   // TODO: we could actually use setProperty and an expression for non-atomics.
858   if (IsCopy) {
859     Kind = GetSetProperty;
860     return;
861   }
862
863   // Handle retain.
864   if (setterKind == ObjCPropertyDecl::Retain) {
865     // In GC-only, there's nothing special that needs to be done.
866     if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
867       // fallthrough
868
869     // In ARC, if the property is non-atomic, use expression emission,
870     // which translates to objc_storeStrong.  This isn't required, but
871     // it's slightly nicer.
872     } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
873       // Using standard expression emission for the setter is only
874       // acceptable if the ivar is __strong, which won't be true if
875       // the property is annotated with __attribute__((NSObject)).
876       // TODO: falling all the way back to objc_setProperty here is
877       // just laziness, though;  we could still use objc_storeStrong
878       // if we hacked it right.
879       if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
880         Kind = Expression;
881       else
882         Kind = SetPropertyAndExpressionGet;
883       return;
884
885     // Otherwise, we need to at least use setProperty.  However, if
886     // the property isn't atomic, we can use normal expression
887     // emission for the getter.
888     } else if (!IsAtomic) {
889       Kind = SetPropertyAndExpressionGet;
890       return;
891
892     // Otherwise, we have to use both setProperty and getProperty.
893     } else {
894       Kind = GetSetProperty;
895       return;
896     }
897   }
898
899   // If we're not atomic, just use expression accesses.
900   if (!IsAtomic) {
901     Kind = Expression;
902     return;
903   }
904
905   // Properties on bitfield ivars need to be emitted using expression
906   // accesses even if they're nominally atomic.
907   if (ivar->isBitField()) {
908     Kind = Expression;
909     return;
910   }
911
912   // GC-qualified or ARC-qualified ivars need to be emitted as
913   // expressions.  This actually works out to being atomic anyway,
914   // except for ARC __strong, but that should trigger the above code.
915   if (ivarType.hasNonTrivialObjCLifetime() ||
916       (CGM.getLangOpts().getGC() &&
917        CGM.getContext().getObjCGCAttrKind(ivarType))) {
918     Kind = Expression;
919     return;
920   }
921
922   // Compute whether the ivar has strong members.
923   if (CGM.getLangOpts().getGC())
924     if (const RecordType *recordType = ivarType->getAs<RecordType>())
925       HasStrong = recordType->getDecl()->hasObjectMember();
926
927   // We can never access structs with object members with a native
928   // access, because we need to use write barriers.  This is what
929   // objc_copyStruct is for.
930   if (HasStrong) {
931     Kind = CopyStruct;
932     return;
933   }
934
935   // Otherwise, this is target-dependent and based on the size and
936   // alignment of the ivar.
937
938   // If the size of the ivar is not a power of two, give up.  We don't
939   // want to get into the business of doing compare-and-swaps.
940   if (!IvarSize.isPowerOfTwo()) {
941     Kind = CopyStruct;
942     return;
943   }
944
945   llvm::Triple::ArchType arch =
946     CGM.getTarget().getTriple().getArch();
947
948   // Most architectures require memory to fit within a single cache
949   // line, so the alignment has to be at least the size of the access.
950   // Otherwise we have to grab a lock.
951   if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
952     Kind = CopyStruct;
953     return;
954   }
955
956   // If the ivar's size exceeds the architecture's maximum atomic
957   // access size, we have to use CopyStruct.
958   if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
959     Kind = CopyStruct;
960     return;
961   }
962
963   // Otherwise, we can use native loads and stores.
964   Kind = Native;
965 }
966
967 /// Generate an Objective-C property getter function.
968 ///
969 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
970 /// is illegal within a category.
971 void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
972                                          const ObjCPropertyImplDecl *PID) {
973   llvm::Constant *AtomicHelperFn =
974       CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID);
975   ObjCMethodDecl *OMD = PID->getGetterMethodDecl();
976   assert(OMD && "Invalid call to generate getter (empty method)");
977   StartObjCMethod(OMD, IMP->getClassInterface());
978
979   generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
980
981   FinishFunction(OMD->getEndLoc());
982 }
983
984 static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
985   const Expr *getter = propImpl->getGetterCXXConstructor();
986   if (!getter) return true;
987
988   // Sema only makes only of these when the ivar has a C++ class type,
989   // so the form is pretty constrained.
990
991   // If the property has a reference type, we might just be binding a
992   // reference, in which case the result will be a gl-value.  We should
993   // treat this as a non-trivial operation.
994   if (getter->isGLValue())
995     return false;
996
997   // If we selected a trivial copy-constructor, we're okay.
998   if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
999     return (construct->getConstructor()->isTrivial());
1000
1001   // The constructor might require cleanups (in which case it's never
1002   // trivial).
1003   assert(isa<ExprWithCleanups>(getter));
1004   return false;
1005 }
1006
1007 /// emitCPPObjectAtomicGetterCall - Call the runtime function to
1008 /// copy the ivar into the resturn slot.
1009 static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF,
1010                                           llvm::Value *returnAddr,
1011                                           ObjCIvarDecl *ivar,
1012                                           llvm::Constant *AtomicHelperFn) {
1013   // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
1014   //                           AtomicHelperFn);
1015   CallArgList args;
1016
1017   // The 1st argument is the return Slot.
1018   args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
1019
1020   // The 2nd argument is the address of the ivar.
1021   llvm::Value *ivarAddr =
1022       CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
1023           .getPointer(CGF);
1024   ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1025   args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1026
1027   // Third argument is the helper function.
1028   args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1029
1030   llvm::FunctionCallee copyCppAtomicObjectFn =
1031       CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction();
1032   CGCallee callee = CGCallee::forDirect(copyCppAtomicObjectFn);
1033   CGF.EmitCall(
1034       CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1035                callee, ReturnValueSlot(), args);
1036 }
1037
1038 void
1039 CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1040                                         const ObjCPropertyImplDecl *propImpl,
1041                                         const ObjCMethodDecl *GetterMethodDecl,
1042                                         llvm::Constant *AtomicHelperFn) {
1043   // If there's a non-trivial 'get' expression, we just have to emit that.
1044   if (!hasTrivialGetExpr(propImpl)) {
1045     if (!AtomicHelperFn) {
1046       auto *ret = ReturnStmt::Create(getContext(), SourceLocation(),
1047                                      propImpl->getGetterCXXConstructor(),
1048                                      /* NRVOCandidate=*/nullptr);
1049       EmitReturnStmt(*ret);
1050     }
1051     else {
1052       ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1053       emitCPPObjectAtomicGetterCall(*this, ReturnValue.getPointer(),
1054                                     ivar, AtomicHelperFn);
1055     }
1056     return;
1057   }
1058
1059   const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
1060   QualType propType = prop->getType();
1061   ObjCMethodDecl *getterMethod = propImpl->getGetterMethodDecl();
1062
1063   ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1064
1065   // Pick an implementation strategy.
1066   PropertyImplStrategy strategy(CGM, propImpl);
1067   switch (strategy.getKind()) {
1068   case PropertyImplStrategy::Native: {
1069     // We don't need to do anything for a zero-size struct.
1070     if (strategy.getIvarSize().isZero())
1071       return;
1072
1073     LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
1074
1075     // Currently, all atomic accesses have to be through integer
1076     // types, so there's no point in trying to pick a prettier type.
1077     uint64_t ivarSize = getContext().toBits(strategy.getIvarSize());
1078     llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize);
1079     bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
1080
1081     // Perform an atomic load.  This does not impose ordering constraints.
1082     Address ivarAddr = LV.getAddress(*this);
1083     ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
1084     llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
1085     load->setAtomic(llvm::AtomicOrdering::Unordered);
1086
1087     // Store that value into the return address.  Doing this with a
1088     // bitcast is likely to produce some pretty ugly IR, but it's not
1089     // the *most* terrible thing in the world.
1090     llvm::Type *retTy = ConvertType(getterMethod->getReturnType());
1091     uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy);
1092     llvm::Value *ivarVal = load;
1093     if (ivarSize > retTySize) {
1094       llvm::Type *newTy = llvm::Type::getIntNTy(getLLVMContext(), retTySize);
1095       ivarVal = Builder.CreateTrunc(load, newTy);
1096       bitcastType = newTy->getPointerTo();
1097     }
1098     Builder.CreateStore(ivarVal,
1099                         Builder.CreateBitCast(ReturnValue, bitcastType));
1100
1101     // Make sure we don't do an autorelease.
1102     AutoreleaseResult = false;
1103     return;
1104   }
1105
1106   case PropertyImplStrategy::GetSetProperty: {
1107     llvm::FunctionCallee getPropertyFn =
1108         CGM.getObjCRuntime().GetPropertyGetFunction();
1109     if (!getPropertyFn) {
1110       CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
1111       return;
1112     }
1113     CGCallee callee = CGCallee::forDirect(getPropertyFn);
1114
1115     // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
1116     // FIXME: Can't this be simpler? This might even be worse than the
1117     // corresponding gcc code.
1118     llvm::Value *cmd =
1119       Builder.CreateLoad(GetAddrOfLocalVar(getterMethod->getCmdDecl()), "cmd");
1120     llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1121     llvm::Value *ivarOffset =
1122       EmitIvarOffset(classImpl->getClassInterface(), ivar);
1123
1124     CallArgList args;
1125     args.add(RValue::get(self), getContext().getObjCIdType());
1126     args.add(RValue::get(cmd), getContext().getObjCSelType());
1127     args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1128     args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1129              getContext().BoolTy);
1130
1131     // FIXME: We shouldn't need to get the function info here, the
1132     // runtime already should have computed it to build the function.
1133     llvm::CallBase *CallInstruction;
1134     RValue RV = EmitCall(getTypes().arrangeBuiltinFunctionCall(
1135                              getContext().getObjCIdType(), args),
1136                          callee, ReturnValueSlot(), args, &CallInstruction);
1137     if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction))
1138       call->setTailCall();
1139
1140     // We need to fix the type here. Ivars with copy & retain are
1141     // always objects so we don't need to worry about complex or
1142     // aggregates.
1143     RV = RValue::get(Builder.CreateBitCast(
1144         RV.getScalarVal(),
1145         getTypes().ConvertType(getterMethod->getReturnType())));
1146
1147     EmitReturnOfRValue(RV, propType);
1148
1149     // objc_getProperty does an autorelease, so we should suppress ours.
1150     AutoreleaseResult = false;
1151
1152     return;
1153   }
1154
1155   case PropertyImplStrategy::CopyStruct:
1156     emitStructGetterCall(*this, ivar, strategy.isAtomic(),
1157                          strategy.hasStrongMember());
1158     return;
1159
1160   case PropertyImplStrategy::Expression:
1161   case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1162     LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
1163
1164     QualType ivarType = ivar->getType();
1165     switch (getEvaluationKind(ivarType)) {
1166     case TEK_Complex: {
1167       ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation());
1168       EmitStoreOfComplex(pair, MakeAddrLValue(ReturnValue, ivarType),
1169                          /*init*/ true);
1170       return;
1171     }
1172     case TEK_Aggregate: {
1173       // The return value slot is guaranteed to not be aliased, but
1174       // that's not necessarily the same as "on the stack", so
1175       // we still potentially need objc_memmove_collectable.
1176       EmitAggregateCopy(/* Dest= */ MakeAddrLValue(ReturnValue, ivarType),
1177                         /* Src= */ LV, ivarType, getOverlapForReturnValue());
1178       return;
1179     }
1180     case TEK_Scalar: {
1181       llvm::Value *value;
1182       if (propType->isReferenceType()) {
1183         value = LV.getAddress(*this).getPointer();
1184       } else {
1185         // We want to load and autoreleaseReturnValue ARC __weak ivars.
1186         if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
1187           if (getLangOpts().ObjCAutoRefCount) {
1188             value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
1189           } else {
1190             value = EmitARCLoadWeak(LV.getAddress(*this));
1191           }
1192
1193         // Otherwise we want to do a simple load, suppressing the
1194         // final autorelease.
1195         } else {
1196           value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal();
1197           AutoreleaseResult = false;
1198         }
1199
1200         value = Builder.CreateBitCast(
1201             value, ConvertType(GetterMethodDecl->getReturnType()));
1202       }
1203
1204       EmitReturnOfRValue(RValue::get(value), propType);
1205       return;
1206     }
1207     }
1208     llvm_unreachable("bad evaluation kind");
1209   }
1210
1211   }
1212   llvm_unreachable("bad @property implementation strategy!");
1213 }
1214
1215 /// emitStructSetterCall - Call the runtime function to store the value
1216 /// from the first formal parameter into the given ivar.
1217 static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
1218                                  ObjCIvarDecl *ivar) {
1219   // objc_copyStruct (&structIvar, &Arg,
1220   //                  sizeof (struct something), true, false);
1221   CallArgList args;
1222
1223   // The first argument is the address of the ivar.
1224   llvm::Value *ivarAddr =
1225       CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
1226           .getPointer(CGF);
1227   ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1228   args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1229
1230   // The second argument is the address of the parameter variable.
1231   ParmVarDecl *argVar = *OMD->param_begin();
1232   DeclRefExpr argRef(CGF.getContext(), argVar, false,
1233                      argVar->getType().getNonReferenceType(), VK_LValue,
1234                      SourceLocation());
1235   llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF);
1236   argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1237   args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1238
1239   // The third argument is the sizeof the type.
1240   llvm::Value *size =
1241     CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
1242   args.add(RValue::get(size), CGF.getContext().getSizeType());
1243
1244   // The fourth argument is the 'isAtomic' flag.
1245   args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
1246
1247   // The fifth argument is the 'hasStrong' flag.
1248   // FIXME: should this really always be false?
1249   args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
1250
1251   llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
1252   CGCallee callee = CGCallee::forDirect(fn);
1253   CGF.EmitCall(
1254       CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1255                callee, ReturnValueSlot(), args);
1256 }
1257
1258 /// emitCPPObjectAtomicSetterCall - Call the runtime function to store
1259 /// the value from the first formal parameter into the given ivar, using
1260 /// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
1261 static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF,
1262                                           ObjCMethodDecl *OMD,
1263                                           ObjCIvarDecl *ivar,
1264                                           llvm::Constant *AtomicHelperFn) {
1265   // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg,
1266   //                           AtomicHelperFn);
1267   CallArgList args;
1268
1269   // The first argument is the address of the ivar.
1270   llvm::Value *ivarAddr =
1271       CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
1272           .getPointer(CGF);
1273   ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1274   args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1275
1276   // The second argument is the address of the parameter variable.
1277   ParmVarDecl *argVar = *OMD->param_begin();
1278   DeclRefExpr argRef(CGF.getContext(), argVar, false,
1279                      argVar->getType().getNonReferenceType(), VK_LValue,
1280                      SourceLocation());
1281   llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF);
1282   argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1283   args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1284
1285   // Third argument is the helper function.
1286   args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1287
1288   llvm::FunctionCallee fn =
1289       CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction();
1290   CGCallee callee = CGCallee::forDirect(fn);
1291   CGF.EmitCall(
1292       CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1293                callee, ReturnValueSlot(), args);
1294 }
1295
1296
1297 static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
1298   Expr *setter = PID->getSetterCXXAssignment();
1299   if (!setter) return true;
1300
1301   // Sema only makes only of these when the ivar has a C++ class type,
1302   // so the form is pretty constrained.
1303
1304   // An operator call is trivial if the function it calls is trivial.
1305   // This also implies that there's nothing non-trivial going on with
1306   // the arguments, because operator= can only be trivial if it's a
1307   // synthesized assignment operator and therefore both parameters are
1308   // references.
1309   if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
1310     if (const FunctionDecl *callee
1311           = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
1312       if (callee->isTrivial())
1313         return true;
1314     return false;
1315   }
1316
1317   assert(isa<ExprWithCleanups>(setter));
1318   return false;
1319 }
1320
1321 static bool UseOptimizedSetter(CodeGenModule &CGM) {
1322   if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
1323     return false;
1324   return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter();
1325 }
1326
1327 void
1328 CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1329                                         const ObjCPropertyImplDecl *propImpl,
1330                                         llvm::Constant *AtomicHelperFn) {
1331   ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1332   ObjCMethodDecl *setterMethod = propImpl->getSetterMethodDecl();
1333
1334   // Just use the setter expression if Sema gave us one and it's
1335   // non-trivial.
1336   if (!hasTrivialSetExpr(propImpl)) {
1337     if (!AtomicHelperFn)
1338       // If non-atomic, assignment is called directly.
1339       EmitStmt(propImpl->getSetterCXXAssignment());
1340     else
1341       // If atomic, assignment is called via a locking api.
1342       emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
1343                                     AtomicHelperFn);
1344     return;
1345   }
1346
1347   PropertyImplStrategy strategy(CGM, propImpl);
1348   switch (strategy.getKind()) {
1349   case PropertyImplStrategy::Native: {
1350     // We don't need to do anything for a zero-size struct.
1351     if (strategy.getIvarSize().isZero())
1352       return;
1353
1354     Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1355
1356     LValue ivarLValue =
1357       EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
1358     Address ivarAddr = ivarLValue.getAddress(*this);
1359
1360     // Currently, all atomic accesses have to be through integer
1361     // types, so there's no point in trying to pick a prettier type.
1362     llvm::Type *bitcastType =
1363       llvm::Type::getIntNTy(getLLVMContext(),
1364                             getContext().toBits(strategy.getIvarSize()));
1365
1366     // Cast both arguments to the chosen operation type.
1367     argAddr = Builder.CreateElementBitCast(argAddr, bitcastType);
1368     ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType);
1369
1370     // This bitcast load is likely to cause some nasty IR.
1371     llvm::Value *load = Builder.CreateLoad(argAddr);
1372
1373     // Perform an atomic store.  There are no memory ordering requirements.
1374     llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
1375     store->setAtomic(llvm::AtomicOrdering::Unordered);
1376     return;
1377   }
1378
1379   case PropertyImplStrategy::GetSetProperty:
1380   case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1381
1382     llvm::FunctionCallee setOptimizedPropertyFn = nullptr;
1383     llvm::FunctionCallee setPropertyFn = nullptr;
1384     if (UseOptimizedSetter(CGM)) {
1385       // 10.8 and iOS 6.0 code and GC is off
1386       setOptimizedPropertyFn =
1387           CGM.getObjCRuntime().GetOptimizedPropertySetFunction(
1388               strategy.isAtomic(), strategy.isCopy());
1389       if (!setOptimizedPropertyFn) {
1390         CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
1391         return;
1392       }
1393     }
1394     else {
1395       setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
1396       if (!setPropertyFn) {
1397         CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
1398         return;
1399       }
1400     }
1401
1402     // Emit objc_setProperty((id) self, _cmd, offset, arg,
1403     //                       <is-atomic>, <is-copy>).
1404     llvm::Value *cmd =
1405       Builder.CreateLoad(GetAddrOfLocalVar(setterMethod->getCmdDecl()));
1406     llvm::Value *self =
1407       Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1408     llvm::Value *ivarOffset =
1409       EmitIvarOffset(classImpl->getClassInterface(), ivar);
1410     Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1411     llvm::Value *arg = Builder.CreateLoad(argAddr, "arg");
1412     arg = Builder.CreateBitCast(arg, VoidPtrTy);
1413
1414     CallArgList args;
1415     args.add(RValue::get(self), getContext().getObjCIdType());
1416     args.add(RValue::get(cmd), getContext().getObjCSelType());
1417     if (setOptimizedPropertyFn) {
1418       args.add(RValue::get(arg), getContext().getObjCIdType());
1419       args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1420       CGCallee callee = CGCallee::forDirect(setOptimizedPropertyFn);
1421       EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1422                callee, ReturnValueSlot(), args);
1423     } else {
1424       args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1425       args.add(RValue::get(arg), getContext().getObjCIdType());
1426       args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1427                getContext().BoolTy);
1428       args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
1429                getContext().BoolTy);
1430       // FIXME: We shouldn't need to get the function info here, the runtime
1431       // already should have computed it to build the function.
1432       CGCallee callee = CGCallee::forDirect(setPropertyFn);
1433       EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1434                callee, ReturnValueSlot(), args);
1435     }
1436
1437     return;
1438   }
1439
1440   case PropertyImplStrategy::CopyStruct:
1441     emitStructSetterCall(*this, setterMethod, ivar);
1442     return;
1443
1444   case PropertyImplStrategy::Expression:
1445     break;
1446   }
1447
1448   // Otherwise, fake up some ASTs and emit a normal assignment.
1449   ValueDecl *selfDecl = setterMethod->getSelfDecl();
1450   DeclRefExpr self(getContext(), selfDecl, false, selfDecl->getType(),
1451                    VK_LValue, SourceLocation());
1452   ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
1453                             selfDecl->getType(), CK_LValueToRValue, &self,
1454                             VK_RValue);
1455   ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
1456                           SourceLocation(), SourceLocation(),
1457                           &selfLoad, true, true);
1458
1459   ParmVarDecl *argDecl = *setterMethod->param_begin();
1460   QualType argType = argDecl->getType().getNonReferenceType();
1461   DeclRefExpr arg(getContext(), argDecl, false, argType, VK_LValue,
1462                   SourceLocation());
1463   ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
1464                            argType.getUnqualifiedType(), CK_LValueToRValue,
1465                            &arg, VK_RValue);
1466
1467   // The property type can differ from the ivar type in some situations with
1468   // Objective-C pointer types, we can always bit cast the RHS in these cases.
1469   // The following absurdity is just to ensure well-formed IR.
1470   CastKind argCK = CK_NoOp;
1471   if (ivarRef.getType()->isObjCObjectPointerType()) {
1472     if (argLoad.getType()->isObjCObjectPointerType())
1473       argCK = CK_BitCast;
1474     else if (argLoad.getType()->isBlockPointerType())
1475       argCK = CK_BlockPointerToObjCPointerCast;
1476     else
1477       argCK = CK_CPointerToObjCPointerCast;
1478   } else if (ivarRef.getType()->isBlockPointerType()) {
1479      if (argLoad.getType()->isBlockPointerType())
1480       argCK = CK_BitCast;
1481     else
1482       argCK = CK_AnyPointerToBlockPointerCast;
1483   } else if (ivarRef.getType()->isPointerType()) {
1484     argCK = CK_BitCast;
1485   }
1486   ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
1487                            ivarRef.getType(), argCK, &argLoad,
1488                            VK_RValue);
1489   Expr *finalArg = &argLoad;
1490   if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
1491                                            argLoad.getType()))
1492     finalArg = &argCast;
1493
1494   BinaryOperator *assign = BinaryOperator::Create(
1495       getContext(), &ivarRef, finalArg, BO_Assign, ivarRef.getType(), VK_RValue,
1496       OK_Ordinary, SourceLocation(), FPOptionsOverride());
1497   EmitStmt(assign);
1498 }
1499
1500 /// Generate an Objective-C property setter function.
1501 ///
1502 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
1503 /// is illegal within a category.
1504 void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
1505                                          const ObjCPropertyImplDecl *PID) {
1506   llvm::Constant *AtomicHelperFn =
1507       CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID);
1508   ObjCMethodDecl *OMD = PID->getSetterMethodDecl();
1509   assert(OMD && "Invalid call to generate setter (empty method)");
1510   StartObjCMethod(OMD, IMP->getClassInterface());
1511
1512   generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1513
1514   FinishFunction(OMD->getEndLoc());
1515 }
1516
1517 namespace {
1518   struct DestroyIvar final : EHScopeStack::Cleanup {
1519   private:
1520     llvm::Value *addr;
1521     const ObjCIvarDecl *ivar;
1522     CodeGenFunction::Destroyer *destroyer;
1523     bool useEHCleanupForArray;
1524   public:
1525     DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
1526                 CodeGenFunction::Destroyer *destroyer,
1527                 bool useEHCleanupForArray)
1528       : addr(addr), ivar(ivar), destroyer(destroyer),
1529         useEHCleanupForArray(useEHCleanupForArray) {}
1530
1531     void Emit(CodeGenFunction &CGF, Flags flags) override {
1532       LValue lvalue
1533         = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
1534       CGF.emitDestroy(lvalue.getAddress(CGF), ivar->getType(), destroyer,
1535                       flags.isForNormalCleanup() && useEHCleanupForArray);
1536     }
1537   };
1538 }
1539
1540 /// Like CodeGenFunction::destroyARCStrong, but do it with a call.
1541 static void destroyARCStrongWithStore(CodeGenFunction &CGF,
1542                                       Address addr,
1543                                       QualType type) {
1544   llvm::Value *null = getNullForVariable(addr);
1545   CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
1546 }
1547
1548 static void emitCXXDestructMethod(CodeGenFunction &CGF,
1549                                   ObjCImplementationDecl *impl) {
1550   CodeGenFunction::RunCleanupsScope scope(CGF);
1551
1552   llvm::Value *self = CGF.LoadObjCSelf();
1553
1554   const ObjCInterfaceDecl *iface = impl->getClassInterface();
1555   for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1556        ivar; ivar = ivar->getNextIvar()) {
1557     QualType type = ivar->getType();
1558
1559     // Check whether the ivar is a destructible type.
1560     QualType::DestructionKind dtorKind = type.isDestructedType();
1561     if (!dtorKind) continue;
1562
1563     CodeGenFunction::Destroyer *destroyer = nullptr;
1564
1565     // Use a call to objc_storeStrong to destroy strong ivars, for the
1566     // general benefit of the tools.
1567     if (dtorKind == QualType::DK_objc_strong_lifetime) {
1568       destroyer = destroyARCStrongWithStore;
1569
1570     // Otherwise use the default for the destruction kind.
1571     } else {
1572       destroyer = CGF.getDestroyer(dtorKind);
1573     }
1574
1575     CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
1576
1577     CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
1578                                          cleanupKind & EHCleanup);
1579   }
1580
1581   assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1582 }
1583
1584 void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1585                                                  ObjCMethodDecl *MD,
1586                                                  bool ctor) {
1587   MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
1588   StartObjCMethod(MD, IMP->getClassInterface());
1589
1590   // Emit .cxx_construct.
1591   if (ctor) {
1592     // Suppress the final autorelease in ARC.
1593     AutoreleaseResult = false;
1594
1595     for (const auto *IvarInit : IMP->inits()) {
1596       FieldDecl *Field = IvarInit->getAnyMember();
1597       ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1598       LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
1599                                     LoadObjCSelf(), Ivar, 0);
1600       EmitAggExpr(IvarInit->getInit(),
1601                   AggValueSlot::forLValue(LV, *this, AggValueSlot::IsDestructed,
1602                                           AggValueSlot::DoesNotNeedGCBarriers,
1603                                           AggValueSlot::IsNotAliased,
1604                                           AggValueSlot::DoesNotOverlap));
1605     }
1606     // constructor returns 'self'.
1607     CodeGenTypes &Types = CGM.getTypes();
1608     QualType IdTy(CGM.getContext().getObjCIdType());
1609     llvm::Value *SelfAsId =
1610       Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1611     EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1612
1613   // Emit .cxx_destruct.
1614   } else {
1615     emitCXXDestructMethod(*this, IMP);
1616   }
1617   FinishFunction();
1618 }
1619
1620 llvm::Value *CodeGenFunction::LoadObjCSelf() {
1621   VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
1622   DeclRefExpr DRE(getContext(), Self,
1623                   /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
1624                   Self->getType(), VK_LValue, SourceLocation());
1625   return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation());
1626 }
1627
1628 QualType CodeGenFunction::TypeOfSelfObject() {
1629   const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1630   ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
1631   const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
1632     getContext().getCanonicalType(selfDecl->getType()));
1633   return PTy->getPointeeType();
1634 }
1635
1636 void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
1637   llvm::FunctionCallee EnumerationMutationFnPtr =
1638       CGM.getObjCRuntime().EnumerationMutationFunction();
1639   if (!EnumerationMutationFnPtr) {
1640     CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1641     return;
1642   }
1643   CGCallee EnumerationMutationFn =
1644     CGCallee::forDirect(EnumerationMutationFnPtr);
1645
1646   CGDebugInfo *DI = getDebugInfo();
1647   if (DI)
1648     DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1649
1650   RunCleanupsScope ForScope(*this);
1651
1652   // The local variable comes into scope immediately.
1653   AutoVarEmission variable = AutoVarEmission::invalid();
1654   if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
1655     variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
1656
1657   JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
1658
1659   // Fast enumeration state.
1660   QualType StateTy = CGM.getObjCFastEnumerationStateType();
1661   Address StatePtr = CreateMemTemp(StateTy, "state.ptr");
1662   EmitNullInitialization(StatePtr, StateTy);
1663
1664   // Number of elements in the items array.
1665   static const unsigned NumItems = 16;
1666
1667   // Fetch the countByEnumeratingWithState:objects:count: selector.
1668   IdentifierInfo *II[] = {
1669     &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1670     &CGM.getContext().Idents.get("objects"),
1671     &CGM.getContext().Idents.get("count")
1672   };
1673   Selector FastEnumSel =
1674     CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1675
1676   QualType ItemsTy =
1677     getContext().getConstantArrayType(getContext().getObjCIdType(),
1678                                       llvm::APInt(32, NumItems), nullptr,
1679                                       ArrayType::Normal, 0);
1680   Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
1681
1682   // Emit the collection pointer.  In ARC, we do a retain.
1683   llvm::Value *Collection;
1684   if (getLangOpts().ObjCAutoRefCount) {
1685     Collection = EmitARCRetainScalarExpr(S.getCollection());
1686
1687     // Enter a cleanup to do the release.
1688     EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1689   } else {
1690     Collection = EmitScalarExpr(S.getCollection());
1691   }
1692
1693   // The 'continue' label needs to appear within the cleanup for the
1694   // collection object.
1695   JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
1696
1697   // Send it our message:
1698   CallArgList Args;
1699
1700   // The first argument is a temporary of the enumeration-state type.
1701   Args.add(RValue::get(StatePtr.getPointer()),
1702            getContext().getPointerType(StateTy));
1703
1704   // The second argument is a temporary array with space for NumItems
1705   // pointers.  We'll actually be loading elements from the array
1706   // pointer written into the control state; this buffer is so that
1707   // collections that *aren't* backed by arrays can still queue up
1708   // batches of elements.
1709   Args.add(RValue::get(ItemsPtr.getPointer()),
1710            getContext().getPointerType(ItemsTy));
1711
1712   // The third argument is the capacity of that temporary array.
1713   llvm::Type *NSUIntegerTy = ConvertType(getContext().getNSUIntegerType());
1714   llvm::Constant *Count = llvm::ConstantInt::get(NSUIntegerTy, NumItems);
1715   Args.add(RValue::get(Count), getContext().getNSUIntegerType());
1716
1717   // Start the enumeration.
1718   RValue CountRV =
1719       CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1720                                                getContext().getNSUIntegerType(),
1721                                                FastEnumSel, Collection, Args);
1722
1723   // The initial number of objects that were returned in the buffer.
1724   llvm::Value *initialBufferLimit = CountRV.getScalarVal();
1725
1726   llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1727   llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1728
1729   llvm::Value *zero = llvm::Constant::getNullValue(NSUIntegerTy);
1730
1731   // If the limit pointer was zero to begin with, the collection is
1732   // empty; skip all this. Set the branch weight assuming this has the same
1733   // probability of exiting the loop as any other loop exit.
1734   uint64_t EntryCount = getCurrentProfileCount();
1735   Builder.CreateCondBr(
1736       Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB,
1737       LoopInitBB,
1738       createProfileWeights(EntryCount, getProfileCount(S.getBody())));
1739
1740   // Otherwise, initialize the loop.
1741   EmitBlock(LoopInitBB);
1742
1743   // Save the initial mutations value.  This is the value at an
1744   // address that was written into the state object by
1745   // countByEnumeratingWithState:objects:count:.
1746   Address StateMutationsPtrPtr =
1747       Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
1748   llvm::Value *StateMutationsPtr
1749     = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1750
1751   llvm::Value *initialMutations =
1752     Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(),
1753                               "forcoll.initial-mutations");
1754
1755   // Start looping.  This is the point we return to whenever we have a
1756   // fresh, non-empty batch of objects.
1757   llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1758   EmitBlock(LoopBodyBB);
1759
1760   // The current index into the buffer.
1761   llvm::PHINode *index = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.index");
1762   index->addIncoming(zero, LoopInitBB);
1763
1764   // The current buffer size.
1765   llvm::PHINode *count = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.count");
1766   count->addIncoming(initialBufferLimit, LoopInitBB);
1767
1768   incrementProfileCounter(&S);
1769
1770   // Check whether the mutations value has changed from where it was
1771   // at start.  StateMutationsPtr should actually be invariant between
1772   // refreshes.
1773   StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1774   llvm::Value *currentMutations
1775     = Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(),
1776                                 "statemutations");
1777
1778   llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1779   llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
1780
1781   Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1782                        WasNotMutatedBB, WasMutatedBB);
1783
1784   // If so, call the enumeration-mutation function.
1785   EmitBlock(WasMutatedBB);
1786   llvm::Value *V =
1787     Builder.CreateBitCast(Collection,
1788                           ConvertType(getContext().getObjCIdType()));
1789   CallArgList Args2;
1790   Args2.add(RValue::get(V), getContext().getObjCIdType());
1791   // FIXME: We shouldn't need to get the function info here, the runtime already
1792   // should have computed it to build the function.
1793   EmitCall(
1794           CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2),
1795            EnumerationMutationFn, ReturnValueSlot(), Args2);
1796
1797   // Otherwise, or if the mutation function returns, just continue.
1798   EmitBlock(WasNotMutatedBB);
1799
1800   // Initialize the element variable.
1801   RunCleanupsScope elementVariableScope(*this);
1802   bool elementIsVariable;
1803   LValue elementLValue;
1804   QualType elementType;
1805   if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
1806     // Initialize the variable, in case it's a __block variable or something.
1807     EmitAutoVarInit(variable);
1808
1809     const VarDecl *D = cast<VarDecl>(SD->getSingleDecl());
1810     DeclRefExpr tempDRE(getContext(), const_cast<VarDecl *>(D), false,
1811                         D->getType(), VK_LValue, SourceLocation());
1812     elementLValue = EmitLValue(&tempDRE);
1813     elementType = D->getType();
1814     elementIsVariable = true;
1815
1816     if (D->isARCPseudoStrong())
1817       elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1818   } else {
1819     elementLValue = LValue(); // suppress warning
1820     elementType = cast<Expr>(S.getElement())->getType();
1821     elementIsVariable = false;
1822   }
1823   llvm::Type *convertedElementType = ConvertType(elementType);
1824
1825   // Fetch the buffer out of the enumeration state.
1826   // TODO: this pointer should actually be invariant between
1827   // refreshes, which would help us do certain loop optimizations.
1828   Address StateItemsPtr =
1829       Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
1830   llvm::Value *EnumStateItems =
1831     Builder.CreateLoad(StateItemsPtr, "stateitems");
1832
1833   // Fetch the value at the current index from the buffer.
1834   llvm::Value *CurrentItemPtr =
1835     Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
1836   llvm::Value *CurrentItem =
1837     Builder.CreateAlignedLoad(CurrentItemPtr, getPointerAlign());
1838
1839   if (SanOpts.has(SanitizerKind::ObjCCast)) {
1840     // Before using an item from the collection, check that the implicit cast
1841     // from id to the element type is valid. This is done with instrumentation
1842     // roughly corresponding to:
1843     //
1844     //   if (![item isKindOfClass:expectedCls]) { /* emit diagnostic */ }
1845     const ObjCObjectPointerType *ObjPtrTy =
1846         elementType->getAsObjCInterfacePointerType();
1847     const ObjCInterfaceType *InterfaceTy =
1848         ObjPtrTy ? ObjPtrTy->getInterfaceType() : nullptr;
1849     if (InterfaceTy) {
1850       SanitizerScope SanScope(this);
1851       auto &C = CGM.getContext();
1852       assert(InterfaceTy->getDecl() && "No decl for ObjC interface type");
1853       Selector IsKindOfClassSel = GetUnarySelector("isKindOfClass", C);
1854       CallArgList IsKindOfClassArgs;
1855       llvm::Value *Cls =
1856           CGM.getObjCRuntime().GetClass(*this, InterfaceTy->getDecl());
1857       IsKindOfClassArgs.add(RValue::get(Cls), C.getObjCClassType());
1858       llvm::Value *IsClass =
1859           CGM.getObjCRuntime()
1860               .GenerateMessageSend(*this, ReturnValueSlot(), C.BoolTy,
1861                                    IsKindOfClassSel, CurrentItem,
1862                                    IsKindOfClassArgs)
1863               .getScalarVal();
1864       llvm::Constant *StaticData[] = {
1865           EmitCheckSourceLocation(S.getBeginLoc()),
1866           EmitCheckTypeDescriptor(QualType(InterfaceTy, 0))};
1867       EmitCheck({{IsClass, SanitizerKind::ObjCCast}},
1868                 SanitizerHandler::InvalidObjCCast,
1869                 ArrayRef<llvm::Constant *>(StaticData), CurrentItem);
1870     }
1871   }
1872
1873   // Cast that value to the right type.
1874   CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1875                                       "currentitem");
1876
1877   // Make sure we have an l-value.  Yes, this gets evaluated every
1878   // time through the loop.
1879   if (!elementIsVariable) {
1880     elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1881     EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
1882   } else {
1883     EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue,
1884                            /*isInit*/ true);
1885   }
1886
1887   // If we do have an element variable, this assignment is the end of
1888   // its initialization.
1889   if (elementIsVariable)
1890     EmitAutoVarCleanups(variable);
1891
1892   // Perform the loop body, setting up break and continue labels.
1893   BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1894   {
1895     RunCleanupsScope Scope(*this);
1896     EmitStmt(S.getBody());
1897   }
1898   BreakContinueStack.pop_back();
1899
1900   // Destroy the element variable now.
1901   elementVariableScope.ForceCleanup();
1902
1903   // Check whether there are more elements.
1904   EmitBlock(AfterBody.getBlock());
1905
1906   llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1907
1908   // First we check in the local buffer.
1909   llvm::Value *indexPlusOne =
1910       Builder.CreateAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1));
1911
1912   // If we haven't overrun the buffer yet, we can continue.
1913   // Set the branch weights based on the simplifying assumption that this is
1914   // like a while-loop, i.e., ignoring that the false branch fetches more
1915   // elements and then returns to the loop.
1916   Builder.CreateCondBr(
1917       Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB,
1918       createProfileWeights(getProfileCount(S.getBody()), EntryCount));
1919
1920   index->addIncoming(indexPlusOne, AfterBody.getBlock());
1921   count->addIncoming(count, AfterBody.getBlock());
1922
1923   // Otherwise, we have to fetch more elements.
1924   EmitBlock(FetchMoreBB);
1925
1926   CountRV =
1927       CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1928                                                getContext().getNSUIntegerType(),
1929                                                FastEnumSel, Collection, Args);
1930
1931   // If we got a zero count, we're done.
1932   llvm::Value *refetchCount = CountRV.getScalarVal();
1933
1934   // (note that the message send might split FetchMoreBB)
1935   index->addIncoming(zero, Builder.GetInsertBlock());
1936   count->addIncoming(refetchCount, Builder.GetInsertBlock());
1937
1938   Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1939                        EmptyBB, LoopBodyBB);
1940
1941   // No more elements.
1942   EmitBlock(EmptyBB);
1943
1944   if (!elementIsVariable) {
1945     // If the element was not a declaration, set it to be null.
1946
1947     llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1948     elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1949     EmitStoreThroughLValue(RValue::get(null), elementLValue);
1950   }
1951
1952   if (DI)
1953     DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
1954
1955   ForScope.ForceCleanup();
1956   EmitBlock(LoopEnd.getBlock());
1957 }
1958
1959 void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1960   CGM.getObjCRuntime().EmitTryStmt(*this, S);
1961 }
1962
1963 void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
1964   CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1965 }
1966
1967 void CodeGenFunction::EmitObjCAtSynchronizedStmt(
1968                                               const ObjCAtSynchronizedStmt &S) {
1969   CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
1970 }
1971
1972 namespace {
1973   struct CallObjCRelease final : EHScopeStack::Cleanup {
1974     CallObjCRelease(llvm::Value *object) : object(object) {}
1975     llvm::Value *object;
1976
1977     void Emit(CodeGenFunction &CGF, Flags flags) override {
1978       // Releases at the end of the full-expression are imprecise.
1979       CGF.EmitARCRelease(object, ARCImpreciseLifetime);
1980     }
1981   };
1982 }
1983
1984 /// Produce the code for a CK_ARCConsumeObject.  Does a primitive
1985 /// release at the end of the full-expression.
1986 llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
1987                                                     llvm::Value *object) {
1988   // If we're in a conditional branch, we need to make the cleanup
1989   // conditional.
1990   pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
1991   return object;
1992 }
1993
1994 llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
1995                                                            llvm::Value *value) {
1996   return EmitARCRetainAutorelease(type, value);
1997 }
1998
1999 /// Given a number of pointers, inform the optimizer that they're
2000 /// being intrinsically used up until this point in the program.
2001 void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
2002   llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_use;
2003   if (!fn)
2004     fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_use);
2005
2006   // This isn't really a "runtime" function, but as an intrinsic it
2007   // doesn't really matter as long as we align things up.
2008   EmitNounwindRuntimeCall(fn, values);
2009 }
2010
2011 static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, llvm::Value *RTF) {
2012   if (auto *F = dyn_cast<llvm::Function>(RTF)) {
2013     // If the target runtime doesn't naturally support ARC, emit weak
2014     // references to the runtime support library.  We don't really
2015     // permit this to fail, but we need a particular relocation style.
2016     if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() &&
2017         !CGM.getTriple().isOSBinFormatCOFF()) {
2018       F->setLinkage(llvm::Function::ExternalWeakLinkage);
2019     }
2020   }
2021 }
2022
2023 static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM,
2024                                          llvm::FunctionCallee RTF) {
2025   setARCRuntimeFunctionLinkage(CGM, RTF.getCallee());
2026 }
2027
2028 /// Perform an operation having the signature
2029 ///   i8* (i8*)
2030 /// where a null input causes a no-op and returns null.
2031 static llvm::Value *emitARCValueOperation(
2032     CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType,
2033     llvm::Function *&fn, llvm::Intrinsic::ID IntID,
2034     llvm::CallInst::TailCallKind tailKind = llvm::CallInst::TCK_None) {
2035   if (isa<llvm::ConstantPointerNull>(value))
2036     return value;
2037
2038   if (!fn) {
2039     fn = CGF.CGM.getIntrinsic(IntID);
2040     setARCRuntimeFunctionLinkage(CGF.CGM, fn);
2041   }
2042
2043   // Cast the argument to 'id'.
2044   llvm::Type *origType = returnType ? returnType : value->getType();
2045   value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
2046
2047   // Call the function.
2048   llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
2049   call->setTailCallKind(tailKind);
2050
2051   // Cast the result back to the original type.
2052   return CGF.Builder.CreateBitCast(call, origType);
2053 }
2054
2055 /// Perform an operation having the following signature:
2056 ///   i8* (i8**)
2057 static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, Address addr,
2058                                          llvm::Function *&fn,
2059                                          llvm::Intrinsic::ID IntID) {
2060   if (!fn) {
2061     fn = CGF.CGM.getIntrinsic(IntID);
2062     setARCRuntimeFunctionLinkage(CGF.CGM, fn);
2063   }
2064
2065   // Cast the argument to 'id*'.
2066   llvm::Type *origType = addr.getElementType();
2067   addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
2068
2069   // Call the function.
2070   llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer());
2071
2072   // Cast the result back to a dereference of the original type.
2073   if (origType != CGF.Int8PtrTy)
2074     result = CGF.Builder.CreateBitCast(result, origType);
2075
2076   return result;
2077 }
2078
2079 /// Perform an operation having the following signature:
2080 ///   i8* (i8**, i8*)
2081 static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, Address addr,
2082                                           llvm::Value *value,
2083                                           llvm::Function *&fn,
2084                                           llvm::Intrinsic::ID IntID,
2085                                           bool ignored) {
2086   assert(addr.getElementType() == value->getType());
2087
2088   if (!fn) {
2089     fn = CGF.CGM.getIntrinsic(IntID);
2090     setARCRuntimeFunctionLinkage(CGF.CGM, fn);
2091   }
2092
2093   llvm::Type *origType = value->getType();
2094
2095   llvm::Value *args[] = {
2096     CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy),
2097     CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
2098   };
2099   llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
2100
2101   if (ignored) return nullptr;
2102
2103   return CGF.Builder.CreateBitCast(result, origType);
2104 }
2105
2106 /// Perform an operation having the following signature:
2107 ///   void (i8**, i8**)
2108 static void emitARCCopyOperation(CodeGenFunction &CGF, Address dst, Address src,
2109                                  llvm::Function *&fn,
2110                                  llvm::Intrinsic::ID IntID) {
2111   assert(dst.getType() == src.getType());
2112
2113   if (!fn) {
2114     fn = CGF.CGM.getIntrinsic(IntID);
2115     setARCRuntimeFunctionLinkage(CGF.CGM, fn);
2116   }
2117
2118   llvm::Value *args[] = {
2119     CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy),
2120     CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy)
2121   };
2122   CGF.EmitNounwindRuntimeCall(fn, args);
2123 }
2124
2125 /// Perform an operation having the signature
2126 ///   i8* (i8*)
2127 /// where a null input causes a no-op and returns null.
2128 static llvm::Value *emitObjCValueOperation(CodeGenFunction &CGF,
2129                                            llvm::Value *value,
2130                                            llvm::Type *returnType,
2131                                            llvm::FunctionCallee &fn,
2132                                            StringRef fnName) {
2133   if (isa<llvm::ConstantPointerNull>(value))
2134     return value;
2135
2136   if (!fn) {
2137     llvm::FunctionType *fnType =
2138       llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
2139     fn = CGF.CGM.CreateRuntimeFunction(fnType, fnName);
2140
2141     // We have Native ARC, so set nonlazybind attribute for performance
2142     if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee()))
2143       if (fnName == "objc_retain")
2144         f->addFnAttr(llvm::Attribute::NonLazyBind);
2145   }
2146
2147   // Cast the argument to 'id'.
2148   llvm::Type *origType = returnType ? returnType : value->getType();
2149   value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
2150
2151   // Call the function.
2152   llvm::CallBase *Inst = CGF.EmitCallOrInvoke(fn, value);
2153
2154   // Cast the result back to the original type.
2155   return CGF.Builder.CreateBitCast(Inst, origType);
2156 }
2157
2158 /// Produce the code to do a retain.  Based on the type, calls one of:
2159 ///   call i8* \@objc_retain(i8* %value)
2160 ///   call i8* \@objc_retainBlock(i8* %value)
2161 llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
2162   if (type->isBlockPointerType())
2163     return EmitARCRetainBlock(value, /*mandatory*/ false);
2164   else
2165     return EmitARCRetainNonBlock(value);
2166 }
2167
2168 /// Retain the given object, with normal retain semantics.
2169 ///   call i8* \@objc_retain(i8* %value)
2170 llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
2171   return emitARCValueOperation(*this, value, nullptr,
2172                                CGM.getObjCEntrypoints().objc_retain,
2173                                llvm::Intrinsic::objc_retain);
2174 }
2175
2176 /// Retain the given block, with _Block_copy semantics.
2177 ///   call i8* \@objc_retainBlock(i8* %value)
2178 ///
2179 /// \param mandatory - If false, emit the call with metadata
2180 /// indicating that it's okay for the optimizer to eliminate this call
2181 /// if it can prove that the block never escapes except down the stack.
2182 llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
2183                                                  bool mandatory) {
2184   llvm::Value *result
2185     = emitARCValueOperation(*this, value, nullptr,
2186                             CGM.getObjCEntrypoints().objc_retainBlock,
2187                             llvm::Intrinsic::objc_retainBlock);
2188
2189   // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
2190   // tell the optimizer that it doesn't need to do this copy if the
2191   // block doesn't escape, where being passed as an argument doesn't
2192   // count as escaping.
2193   if (!mandatory && isa<llvm::Instruction>(result)) {
2194     llvm::CallInst *call
2195       = cast<llvm::CallInst>(result->stripPointerCasts());
2196     assert(call->getCalledOperand() ==
2197            CGM.getObjCEntrypoints().objc_retainBlock);
2198
2199     call->setMetadata("clang.arc.copy_on_escape",
2200                       llvm::MDNode::get(Builder.getContext(), None));
2201   }
2202
2203   return result;
2204 }
2205
2206 static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) {
2207   // Fetch the void(void) inline asm which marks that we're going to
2208   // do something with the autoreleased return value.
2209   llvm::InlineAsm *&marker
2210     = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker;
2211   if (!marker) {
2212     StringRef assembly
2213       = CGF.CGM.getTargetCodeGenInfo()
2214            .getARCRetainAutoreleasedReturnValueMarker();
2215
2216     // If we have an empty assembly string, there's nothing to do.
2217     if (assembly.empty()) {
2218
2219     // Otherwise, at -O0, build an inline asm that we're going to call
2220     // in a moment.
2221     } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
2222       llvm::FunctionType *type =
2223         llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false);
2224
2225       marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
2226
2227     // If we're at -O1 and above, we don't want to litter the code
2228     // with this marker yet, so leave a breadcrumb for the ARC
2229     // optimizer to pick up.
2230     } else {
2231       const char *markerKey = "clang.arc.retainAutoreleasedReturnValueMarker";
2232       if (!CGF.CGM.getModule().getModuleFlag(markerKey)) {
2233         auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly);
2234         CGF.CGM.getModule().addModuleFlag(llvm::Module::Error, markerKey, str);
2235       }
2236     }
2237   }
2238
2239   // Call the marker asm if we made one, which we do only at -O0.
2240   if (marker)
2241     CGF.Builder.CreateCall(marker, None, CGF.getBundlesForFunclet(marker));
2242 }
2243
2244 /// Retain the given object which is the result of a function call.
2245 ///   call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
2246 ///
2247 /// Yes, this function name is one character away from a different
2248 /// call with completely different semantics.
2249 llvm::Value *
2250 CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
2251   emitAutoreleasedReturnValueMarker(*this);
2252   llvm::CallInst::TailCallKind tailKind =
2253       CGM.getTargetCodeGenInfo()
2254               .shouldSuppressTailCallsOfRetainAutoreleasedReturnValue()
2255           ? llvm::CallInst::TCK_NoTail
2256           : llvm::CallInst::TCK_None;
2257   return emitARCValueOperation(
2258       *this, value, nullptr,
2259       CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue,
2260       llvm::Intrinsic::objc_retainAutoreleasedReturnValue, tailKind);
2261 }
2262
2263 /// Claim a possibly-autoreleased return value at +0.  This is only
2264 /// valid to do in contexts which do not rely on the retain to keep
2265 /// the object valid for all of its uses; for example, when
2266 /// the value is ignored, or when it is being assigned to an
2267 /// __unsafe_unretained variable.
2268 ///
2269 ///   call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value)
2270 llvm::Value *
2271 CodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) {
2272   emitAutoreleasedReturnValueMarker(*this);
2273   return emitARCValueOperation(*this, value, nullptr,
2274               CGM.getObjCEntrypoints().objc_unsafeClaimAutoreleasedReturnValue,
2275                      llvm::Intrinsic::objc_unsafeClaimAutoreleasedReturnValue);
2276 }
2277
2278 /// Release the given object.
2279 ///   call void \@objc_release(i8* %value)
2280 void CodeGenFunction::EmitARCRelease(llvm::Value *value,
2281                                      ARCPreciseLifetime_t precise) {
2282   if (isa<llvm::ConstantPointerNull>(value)) return;
2283
2284   llvm::Function *&fn = CGM.getObjCEntrypoints().objc_release;
2285   if (!fn) {
2286     fn = CGM.getIntrinsic(llvm::Intrinsic::objc_release);
2287     setARCRuntimeFunctionLinkage(CGM, fn);
2288   }
2289
2290   // Cast the argument to 'id'.
2291   value = Builder.CreateBitCast(value, Int8PtrTy);
2292
2293   // Call objc_release.
2294   llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2295
2296   if (precise == ARCImpreciseLifetime) {
2297     call->setMetadata("clang.imprecise_release",
2298                       llvm::MDNode::get(Builder.getContext(), None));
2299   }
2300 }
2301
2302 /// Destroy a __strong variable.
2303 ///
2304 /// At -O0, emit a call to store 'null' into the address;
2305 /// instrumenting tools prefer this because the address is exposed,
2306 /// but it's relatively cumbersome to optimize.
2307 ///
2308 /// At -O1 and above, just load and call objc_release.
2309 ///
2310 ///   call void \@objc_storeStrong(i8** %addr, i8* null)
2311 void CodeGenFunction::EmitARCDestroyStrong(Address addr,
2312                                            ARCPreciseLifetime_t precise) {
2313   if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2314     llvm::Value *null = getNullForVariable(addr);
2315     EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2316     return;
2317   }
2318
2319   llvm::Value *value = Builder.CreateLoad(addr);
2320   EmitARCRelease(value, precise);
2321 }
2322
2323 /// Store into a strong object.  Always calls this:
2324 ///   call void \@objc_storeStrong(i8** %addr, i8* %value)
2325 llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr,
2326                                                      llvm::Value *value,
2327                                                      bool ignored) {
2328   assert(addr.getElementType() == value->getType());
2329
2330   llvm::Function *&fn = CGM.getObjCEntrypoints().objc_storeStrong;
2331   if (!fn) {
2332     fn = CGM.getIntrinsic(llvm::Intrinsic::objc_storeStrong);
2333     setARCRuntimeFunctionLinkage(CGM, fn);
2334   }
2335
2336   llvm::Value *args[] = {
2337     Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy),
2338     Builder.CreateBitCast(value, Int8PtrTy)
2339   };
2340   EmitNounwindRuntimeCall(fn, args);
2341
2342   if (ignored) return nullptr;
2343   return value;
2344 }
2345
2346 /// Store into a strong object.  Sometimes calls this:
2347 ///   call void \@objc_storeStrong(i8** %addr, i8* %value)
2348 /// Other times, breaks it down into components.
2349 llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
2350                                                  llvm::Value *newValue,
2351                                                  bool ignored) {
2352   QualType type = dst.getType();
2353   bool isBlock = type->isBlockPointerType();
2354
2355   // Use a store barrier at -O0 unless this is a block type or the
2356   // lvalue is inadequately aligned.
2357   if (shouldUseFusedARCCalls() &&
2358       !isBlock &&
2359       (dst.getAlignment().isZero() ||
2360        dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
2361     return EmitARCStoreStrongCall(dst.getAddress(*this), newValue, ignored);
2362   }
2363
2364   // Otherwise, split it out.
2365
2366   // Retain the new value.
2367   newValue = EmitARCRetain(type, newValue);
2368
2369   // Read the old value.
2370   llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation());
2371
2372   // Store.  We do this before the release so that any deallocs won't
2373   // see the old value.
2374   EmitStoreOfScalar(newValue, dst);
2375
2376   // Finally, release the old value.
2377   EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2378
2379   return newValue;
2380 }
2381
2382 /// Autorelease the given object.
2383 ///   call i8* \@objc_autorelease(i8* %value)
2384 llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2385   return emitARCValueOperation(*this, value, nullptr,
2386                                CGM.getObjCEntrypoints().objc_autorelease,
2387                                llvm::Intrinsic::objc_autorelease);
2388 }
2389
2390 /// Autorelease the given object.
2391 ///   call i8* \@objc_autoreleaseReturnValue(i8* %value)
2392 llvm::Value *
2393 CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
2394   return emitARCValueOperation(*this, value, nullptr,
2395                             CGM.getObjCEntrypoints().objc_autoreleaseReturnValue,
2396                                llvm::Intrinsic::objc_autoreleaseReturnValue,
2397                                llvm::CallInst::TCK_Tail);
2398 }
2399
2400 /// Do a fused retain/autorelease of the given object.
2401 ///   call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2402 llvm::Value *
2403 CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
2404   return emitARCValueOperation(*this, value, nullptr,
2405                      CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue,
2406                              llvm::Intrinsic::objc_retainAutoreleaseReturnValue,
2407                                llvm::CallInst::TCK_Tail);
2408 }
2409
2410 /// Do a fused retain/autorelease of the given object.
2411 ///   call i8* \@objc_retainAutorelease(i8* %value)
2412 /// or
2413 ///   %retain = call i8* \@objc_retainBlock(i8* %value)
2414 ///   call i8* \@objc_autorelease(i8* %retain)
2415 llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
2416                                                        llvm::Value *value) {
2417   if (!type->isBlockPointerType())
2418     return EmitARCRetainAutoreleaseNonBlock(value);
2419
2420   if (isa<llvm::ConstantPointerNull>(value)) return value;
2421
2422   llvm::Type *origType = value->getType();
2423   value = Builder.CreateBitCast(value, Int8PtrTy);
2424   value = EmitARCRetainBlock(value, /*mandatory*/ true);
2425   value = EmitARCAutorelease(value);
2426   return Builder.CreateBitCast(value, origType);
2427 }
2428
2429 /// Do a fused retain/autorelease of the given object.
2430 ///   call i8* \@objc_retainAutorelease(i8* %value)
2431 llvm::Value *
2432 CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
2433   return emitARCValueOperation(*this, value, nullptr,
2434                                CGM.getObjCEntrypoints().objc_retainAutorelease,
2435                                llvm::Intrinsic::objc_retainAutorelease);
2436 }
2437
2438 /// i8* \@objc_loadWeak(i8** %addr)
2439 /// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2440 llvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) {
2441   return emitARCLoadOperation(*this, addr,
2442                               CGM.getObjCEntrypoints().objc_loadWeak,
2443                               llvm::Intrinsic::objc_loadWeak);
2444 }
2445
2446 /// i8* \@objc_loadWeakRetained(i8** %addr)
2447 llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) {
2448   return emitARCLoadOperation(*this, addr,
2449                               CGM.getObjCEntrypoints().objc_loadWeakRetained,
2450                               llvm::Intrinsic::objc_loadWeakRetained);
2451 }
2452
2453 /// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2454 /// Returns %value.
2455 llvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr,
2456                                                llvm::Value *value,
2457                                                bool ignored) {
2458   return emitARCStoreOperation(*this, addr, value,
2459                                CGM.getObjCEntrypoints().objc_storeWeak,
2460                                llvm::Intrinsic::objc_storeWeak, ignored);
2461 }
2462
2463 /// i8* \@objc_initWeak(i8** %addr, i8* %value)
2464 /// Returns %value.  %addr is known to not have a current weak entry.
2465 /// Essentially equivalent to:
2466 ///   *addr = nil; objc_storeWeak(addr, value);
2467 void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) {
2468   // If we're initializing to null, just write null to memory; no need
2469   // to get the runtime involved.  But don't do this if optimization
2470   // is enabled, because accounting for this would make the optimizer
2471   // much more complicated.
2472   if (isa<llvm::ConstantPointerNull>(value) &&
2473       CGM.getCodeGenOpts().OptimizationLevel == 0) {
2474     Builder.CreateStore(value, addr);
2475     return;
2476   }
2477
2478   emitARCStoreOperation(*this, addr, value,
2479                         CGM.getObjCEntrypoints().objc_initWeak,
2480                         llvm::Intrinsic::objc_initWeak, /*ignored*/ true);
2481 }
2482
2483 /// void \@objc_destroyWeak(i8** %addr)
2484 /// Essentially objc_storeWeak(addr, nil).
2485 void CodeGenFunction::EmitARCDestroyWeak(Address addr) {
2486   llvm::Function *&fn = CGM.getObjCEntrypoints().objc_destroyWeak;
2487   if (!fn) {
2488     fn = CGM.getIntrinsic(llvm::Intrinsic::objc_destroyWeak);
2489     setARCRuntimeFunctionLinkage(CGM, fn);
2490   }
2491
2492   // Cast the argument to 'id*'.
2493   addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
2494
2495   EmitNounwindRuntimeCall(fn, addr.getPointer());
2496 }
2497
2498 /// void \@objc_moveWeak(i8** %dest, i8** %src)
2499 /// Disregards the current value in %dest.  Leaves %src pointing to nothing.
2500 /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2501 void CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) {
2502   emitARCCopyOperation(*this, dst, src,
2503                        CGM.getObjCEntrypoints().objc_moveWeak,
2504                        llvm::Intrinsic::objc_moveWeak);
2505 }
2506
2507 /// void \@objc_copyWeak(i8** %dest, i8** %src)
2508 /// Disregards the current value in %dest.  Essentially
2509 ///   objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2510 void CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) {
2511   emitARCCopyOperation(*this, dst, src,
2512                        CGM.getObjCEntrypoints().objc_copyWeak,
2513                        llvm::Intrinsic::objc_copyWeak);
2514 }
2515
2516 void CodeGenFunction::emitARCCopyAssignWeak(QualType Ty, Address DstAddr,
2517                                             Address SrcAddr) {
2518   llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr);
2519   Object = EmitObjCConsumeObject(Ty, Object);
2520   EmitARCStoreWeak(DstAddr, Object, false);
2521 }
2522
2523 void CodeGenFunction::emitARCMoveAssignWeak(QualType Ty, Address DstAddr,
2524                                             Address SrcAddr) {
2525   llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr);
2526   Object = EmitObjCConsumeObject(Ty, Object);
2527   EmitARCStoreWeak(DstAddr, Object, false);
2528   EmitARCDestroyWeak(SrcAddr);
2529 }
2530
2531 /// Produce the code to do a objc_autoreleasepool_push.
2532 ///   call i8* \@objc_autoreleasePoolPush(void)
2533 llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
2534   llvm::Function *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush;
2535   if (!fn) {
2536     fn = CGM.getIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPush);
2537     setARCRuntimeFunctionLinkage(CGM, fn);
2538   }
2539
2540   return EmitNounwindRuntimeCall(fn);
2541 }
2542
2543 /// Produce the code to do a primitive release.
2544 ///   call void \@objc_autoreleasePoolPop(i8* %ptr)
2545 void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
2546   assert(value->getType() == Int8PtrTy);
2547
2548   if (getInvokeDest()) {
2549     // Call the runtime method not the intrinsic if we are handling exceptions
2550     llvm::FunctionCallee &fn =
2551         CGM.getObjCEntrypoints().objc_autoreleasePoolPopInvoke;
2552     if (!fn) {
2553       llvm::FunctionType *fnType =
2554         llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2555       fn = CGM.CreateRuntimeFunction(fnType, "objc_autoreleasePoolPop");
2556       setARCRuntimeFunctionLinkage(CGM, fn);
2557     }
2558
2559     // objc_autoreleasePoolPop can throw.
2560     EmitRuntimeCallOrInvoke(fn, value);
2561   } else {
2562     llvm::FunctionCallee &fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop;
2563     if (!fn) {
2564       fn = CGM.getIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPop);
2565       setARCRuntimeFunctionLinkage(CGM, fn);
2566     }
2567
2568     EmitRuntimeCall(fn, value);
2569   }
2570 }
2571
2572 /// Produce the code to do an MRR version objc_autoreleasepool_push.
2573 /// Which is: [[NSAutoreleasePool alloc] init];
2574 /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2575 /// init is declared as: - (id) init; in its NSObject super class.
2576 ///
2577 llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2578   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2579   llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2580   // [NSAutoreleasePool alloc]
2581   IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2582   Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2583   CallArgList Args;
2584   RValue AllocRV =
2585     Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2586                                 getContext().getObjCIdType(),
2587                                 AllocSel, Receiver, Args);
2588
2589   // [Receiver init]
2590   Receiver = AllocRV.getScalarVal();
2591   II = &CGM.getContext().Idents.get("init");
2592   Selector InitSel = getContext().Selectors.getSelector(0, &II);
2593   RValue InitRV =
2594     Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2595                                 getContext().getObjCIdType(),
2596                                 InitSel, Receiver, Args);
2597   return InitRV.getScalarVal();
2598 }
2599
2600 /// Allocate the given objc object.
2601 ///   call i8* \@objc_alloc(i8* %value)
2602 llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value,
2603                                             llvm::Type *resultType) {
2604   return emitObjCValueOperation(*this, value, resultType,
2605                                 CGM.getObjCEntrypoints().objc_alloc,
2606                                 "objc_alloc");
2607 }
2608
2609 /// Allocate the given objc object.
2610 ///   call i8* \@objc_allocWithZone(i8* %value)
2611 llvm::Value *CodeGenFunction::EmitObjCAllocWithZone(llvm::Value *value,
2612                                                     llvm::Type *resultType) {
2613   return emitObjCValueOperation(*this, value, resultType,
2614                                 CGM.getObjCEntrypoints().objc_allocWithZone,
2615                                 "objc_allocWithZone");
2616 }
2617
2618 llvm::Value *CodeGenFunction::EmitObjCAllocInit(llvm::Value *value,
2619                                                 llvm::Type *resultType) {
2620   return emitObjCValueOperation(*this, value, resultType,
2621                                 CGM.getObjCEntrypoints().objc_alloc_init,
2622                                 "objc_alloc_init");
2623 }
2624
2625 /// Produce the code to do a primitive release.
2626 /// [tmp drain];
2627 void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2628   IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2629   Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2630   CallArgList Args;
2631   CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2632                               getContext().VoidTy, DrainSel, Arg, Args);
2633 }
2634
2635 void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2636                                               Address addr,
2637                                               QualType type) {
2638   CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
2639 }
2640
2641 void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2642                                                 Address addr,
2643                                                 QualType type) {
2644   CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
2645 }
2646
2647 void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2648                                      Address addr,
2649                                      QualType type) {
2650   CGF.EmitARCDestroyWeak(addr);
2651 }
2652
2653 void CodeGenFunction::emitARCIntrinsicUse(CodeGenFunction &CGF, Address addr,
2654                                           QualType type) {
2655   llvm::Value *value = CGF.Builder.CreateLoad(addr);
2656   CGF.EmitARCIntrinsicUse(value);
2657 }
2658
2659 /// Autorelease the given object.
2660 ///   call i8* \@objc_autorelease(i8* %value)
2661 llvm::Value *CodeGenFunction::EmitObjCAutorelease(llvm::Value *value,
2662                                                   llvm::Type *returnType) {
2663   return emitObjCValueOperation(
2664       *this, value, returnType,
2665       CGM.getObjCEntrypoints().objc_autoreleaseRuntimeFunction,
2666       "objc_autorelease");
2667 }
2668
2669 /// Retain the given object, with normal retain semantics.
2670 ///   call i8* \@objc_retain(i8* %value)
2671 llvm::Value *CodeGenFunction::EmitObjCRetainNonBlock(llvm::Value *value,
2672                                                      llvm::Type *returnType) {
2673   return emitObjCValueOperation(
2674       *this, value, returnType,
2675       CGM.getObjCEntrypoints().objc_retainRuntimeFunction, "objc_retain");
2676 }
2677
2678 /// Release the given object.
2679 ///   call void \@objc_release(i8* %value)
2680 void CodeGenFunction::EmitObjCRelease(llvm::Value *value,
2681                                       ARCPreciseLifetime_t precise) {
2682   if (isa<llvm::ConstantPointerNull>(value)) return;
2683
2684   llvm::FunctionCallee &fn =
2685       CGM.getObjCEntrypoints().objc_releaseRuntimeFunction;
2686   if (!fn) {
2687     llvm::FunctionType *fnType =
2688         llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2689     fn = CGM.CreateRuntimeFunction(fnType, "objc_release");
2690     setARCRuntimeFunctionLinkage(CGM, fn);
2691     // We have Native ARC, so set nonlazybind attribute for performance
2692     if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee()))
2693       f->addFnAttr(llvm::Attribute::NonLazyBind);
2694   }
2695
2696   // Cast the argument to 'id'.
2697   value = Builder.CreateBitCast(value, Int8PtrTy);
2698
2699   // Call objc_release.
2700   llvm::CallBase *call = EmitCallOrInvoke(fn, value);
2701
2702   if (precise == ARCImpreciseLifetime) {
2703     call->setMetadata("clang.imprecise_release",
2704                       llvm::MDNode::get(Builder.getContext(), None));
2705   }
2706 }
2707
2708 namespace {
2709   struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup {
2710     llvm::Value *Token;
2711
2712     CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2713
2714     void Emit(CodeGenFunction &CGF, Flags flags) override {
2715       CGF.EmitObjCAutoreleasePoolPop(Token);
2716     }
2717   };
2718   struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup {
2719     llvm::Value *Token;
2720
2721     CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2722
2723     void Emit(CodeGenFunction &CGF, Flags flags) override {
2724       CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2725     }
2726   };
2727 }
2728
2729 void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
2730   if (CGM.getLangOpts().ObjCAutoRefCount)
2731     EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2732   else
2733     EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2734 }
2735
2736 static bool shouldRetainObjCLifetime(Qualifiers::ObjCLifetime lifetime) {
2737   switch (lifetime) {
2738   case Qualifiers::OCL_None:
2739   case Qualifiers::OCL_ExplicitNone:
2740   case Qualifiers::OCL_Strong:
2741   case Qualifiers::OCL_Autoreleasing:
2742     return true;
2743
2744   case Qualifiers::OCL_Weak:
2745     return false;
2746   }
2747
2748   llvm_unreachable("impossible lifetime!");
2749 }
2750
2751 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2752                                                   LValue lvalue,
2753                                                   QualType type) {
2754   llvm::Value *result;
2755   bool shouldRetain = shouldRetainObjCLifetime(type.getObjCLifetime());
2756   if (shouldRetain) {
2757     result = CGF.EmitLoadOfLValue(lvalue, SourceLocation()).getScalarVal();
2758   } else {
2759     assert(type.getObjCLifetime() == Qualifiers::OCL_Weak);
2760     result = CGF.EmitARCLoadWeakRetained(lvalue.getAddress(CGF));
2761   }
2762   return TryEmitResult(result, !shouldRetain);
2763 }
2764
2765 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2766                                                   const Expr *e) {
2767   e = e->IgnoreParens();
2768   QualType type = e->getType();
2769
2770   // If we're loading retained from a __strong xvalue, we can avoid
2771   // an extra retain/release pair by zeroing out the source of this
2772   // "move" operation.
2773   if (e->isXValue() &&
2774       !type.isConstQualified() &&
2775       type.getObjCLifetime() == Qualifiers::OCL_Strong) {
2776     // Emit the lvalue.
2777     LValue lv = CGF.EmitLValue(e);
2778
2779     // Load the object pointer.
2780     llvm::Value *result = CGF.EmitLoadOfLValue(lv,
2781                                                SourceLocation()).getScalarVal();
2782
2783     // Set the source pointer to NULL.
2784     CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress(CGF)), lv);
2785
2786     return TryEmitResult(result, true);
2787   }
2788
2789   // As a very special optimization, in ARC++, if the l-value is the
2790   // result of a non-volatile assignment, do a simple retain of the
2791   // result of the call to objc_storeWeak instead of reloading.
2792   if (CGF.getLangOpts().CPlusPlus &&
2793       !type.isVolatileQualified() &&
2794       type.getObjCLifetime() == Qualifiers::OCL_Weak &&
2795       isa<BinaryOperator>(e) &&
2796       cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
2797     return TryEmitResult(CGF.EmitScalarExpr(e), false);
2798
2799   // Try to emit code for scalar constant instead of emitting LValue and
2800   // loading it because we are not guaranteed to have an l-value. One of such
2801   // cases is DeclRefExpr referencing non-odr-used constant-evaluated variable.
2802   if (const auto *decl_expr = dyn_cast<DeclRefExpr>(e)) {
2803     auto *DRE = const_cast<DeclRefExpr *>(decl_expr);
2804     if (CodeGenFunction::ConstantEmission constant = CGF.tryEmitAsConstant(DRE))
2805       return TryEmitResult(CGF.emitScalarConstant(constant, DRE),
2806                            !shouldRetainObjCLifetime(type.getObjCLifetime()));
2807   }
2808
2809   return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2810 }
2811
2812 typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2813                                          llvm::Value *value)>
2814   ValueTransform;
2815
2816 /// Insert code immediately after a call.
2817 static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF,
2818                                               llvm::Value *value,
2819                                               ValueTransform doAfterCall,
2820                                               ValueTransform doFallback) {
2821   if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2822     CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2823
2824     // Place the retain immediately following the call.
2825     CGF.Builder.SetInsertPoint(call->getParent(),
2826                                ++llvm::BasicBlock::iterator(call));
2827     value = doAfterCall(CGF, value);
2828
2829     CGF.Builder.restoreIP(ip);
2830     return value;
2831   } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
2832     CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2833
2834     // Place the retain at the beginning of the normal destination block.
2835     llvm::BasicBlock *BB = invoke->getNormalDest();
2836     CGF.Builder.SetInsertPoint(BB, BB->begin());
2837     value = doAfterCall(CGF, value);
2838
2839     CGF.Builder.restoreIP(ip);
2840     return value;
2841
2842   // Bitcasts can arise because of related-result returns.  Rewrite
2843   // the operand.
2844   } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2845     llvm::Value *operand = bitcast->getOperand(0);
2846     operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback);
2847     bitcast->setOperand(0, operand);
2848     return bitcast;
2849
2850   // Generic fall-back case.
2851   } else {
2852     // Retain using the non-block variant: we never need to do a copy
2853     // of a block that's been returned to us.
2854     return doFallback(CGF, value);
2855   }
2856 }
2857
2858 /// Given that the given expression is some sort of call (which does
2859 /// not return retained), emit a retain following it.
2860 static llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF,
2861                                             const Expr *e) {
2862   llvm::Value *value = CGF.EmitScalarExpr(e);
2863   return emitARCOperationAfterCall(CGF, value,
2864            [](CodeGenFunction &CGF, llvm::Value *value) {
2865              return CGF.EmitARCRetainAutoreleasedReturnValue(value);
2866            },
2867            [](CodeGenFunction &CGF, llvm::Value *value) {
2868              return CGF.EmitARCRetainNonBlock(value);
2869            });
2870 }
2871
2872 /// Given that the given expression is some sort of call (which does
2873 /// not return retained), perform an unsafeClaim following it.
2874 static llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF,
2875                                                  const Expr *e) {
2876   llvm::Value *value = CGF.EmitScalarExpr(e);
2877   return emitARCOperationAfterCall(CGF, value,
2878            [](CodeGenFunction &CGF, llvm::Value *value) {
2879              return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value);
2880            },
2881            [](CodeGenFunction &CGF, llvm::Value *value) {
2882              return value;
2883            });
2884 }
2885
2886 llvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E,
2887                                                       bool allowUnsafeClaim) {
2888   if (allowUnsafeClaim &&
2889       CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) {
2890     return emitARCUnsafeClaimCallResult(*this, E);
2891   } else {
2892     llvm::Value *value = emitARCRetainCallResult(*this, E);
2893     return EmitObjCConsumeObject(E->getType(), value);
2894   }
2895 }
2896
2897 /// Determine whether it might be important to emit a separate
2898 /// objc_retain_block on the result of the given expression, or
2899 /// whether it's okay to just emit it in a +1 context.
2900 static bool shouldEmitSeparateBlockRetain(const Expr *e) {
2901   assert(e->getType()->isBlockPointerType());
2902   e = e->IgnoreParens();
2903
2904   // For future goodness, emit block expressions directly in +1
2905   // contexts if we can.
2906   if (isa<BlockExpr>(e))
2907     return false;
2908
2909   if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
2910     switch (cast->getCastKind()) {
2911     // Emitting these operations in +1 contexts is goodness.
2912     case CK_LValueToRValue:
2913     case CK_ARCReclaimReturnedObject:
2914     case CK_ARCConsumeObject:
2915     case CK_ARCProduceObject:
2916       return false;
2917
2918     // These operations preserve a block type.
2919     case CK_NoOp:
2920     case CK_BitCast:
2921       return shouldEmitSeparateBlockRetain(cast->getSubExpr());
2922
2923     // These operations are known to be bad (or haven't been considered).
2924     case CK_AnyPointerToBlockPointerCast:
2925     default:
2926       return true;
2927     }
2928   }
2929
2930   return true;
2931 }
2932
2933 namespace {
2934 /// A CRTP base class for emitting expressions of retainable object
2935 /// pointer type in ARC.
2936 template <typename Impl, typename Result> class ARCExprEmitter {
2937 protected:
2938   CodeGenFunction &CGF;
2939   Impl &asImpl() { return *static_cast<Impl*>(this); }
2940
2941   ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {}
2942
2943 public:
2944   Result visit(const Expr *e);
2945   Result visitCastExpr(const CastExpr *e);
2946   Result visitPseudoObjectExpr(const PseudoObjectExpr *e);
2947   Result visitBlockExpr(const BlockExpr *e);
2948   Result visitBinaryOperator(const BinaryOperator *e);
2949   Result visitBinAssign(const BinaryOperator *e);
2950   Result visitBinAssignUnsafeUnretained(const BinaryOperator *e);
2951   Result visitBinAssignAutoreleasing(const BinaryOperator *e);
2952   Result visitBinAssignWeak(const BinaryOperator *e);
2953   Result visitBinAssignStrong(const BinaryOperator *e);
2954
2955   // Minimal implementation:
2956   //   Result visitLValueToRValue(const Expr *e)
2957   //   Result visitConsumeObject(const Expr *e)
2958   //   Result visitExtendBlockObject(const Expr *e)
2959   //   Result visitReclaimReturnedObject(const Expr *e)
2960   //   Result visitCall(const Expr *e)
2961   //   Result visitExpr(const Expr *e)
2962   //
2963   //   Result emitBitCast(Result result, llvm::Type *resultType)
2964   //   llvm::Value *getValueOfResult(Result result)
2965 };
2966 }
2967
2968 /// Try to emit a PseudoObjectExpr under special ARC rules.
2969 ///
2970 /// This massively duplicates emitPseudoObjectRValue.
2971 template <typename Impl, typename Result>
2972 Result
2973 ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) {
2974   SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
2975
2976   // Find the result expression.
2977   const Expr *resultExpr = E->getResultExpr();
2978   assert(resultExpr);
2979   Result result;
2980
2981   for (PseudoObjectExpr::const_semantics_iterator
2982          i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
2983     const Expr *semantic = *i;
2984
2985     // If this semantic expression is an opaque value, bind it
2986     // to the result of its source expression.
2987     if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
2988       typedef CodeGenFunction::OpaqueValueMappingData OVMA;
2989       OVMA opaqueData;
2990
2991       // If this semantic is the result of the pseudo-object
2992       // expression, try to evaluate the source as +1.
2993       if (ov == resultExpr) {
2994         assert(!OVMA::shouldBindAsLValue(ov));
2995         result = asImpl().visit(ov->getSourceExpr());
2996         opaqueData = OVMA::bind(CGF, ov,
2997                             RValue::get(asImpl().getValueOfResult(result)));
2998
2999       // Otherwise, just bind it.
3000       } else {
3001         opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
3002       }
3003       opaques.push_back(opaqueData);
3004
3005     // Otherwise, if the expression is the result, evaluate it
3006     // and remember the result.
3007     } else if (semantic == resultExpr) {
3008       result = asImpl().visit(semantic);
3009
3010     // Otherwise, evaluate the expression in an ignored context.
3011     } else {
3012       CGF.EmitIgnoredExpr(semantic);
3013     }
3014   }
3015
3016   // Unbind all the opaques now.
3017   for (unsigned i = 0, e = opaques.size(); i != e; ++i)
3018     opaques[i].unbind(CGF);
3019
3020   return result;
3021 }
3022
3023 template <typename Impl, typename Result>
3024 Result ARCExprEmitter<Impl, Result>::visitBlockExpr(const BlockExpr *e) {
3025   // The default implementation just forwards the expression to visitExpr.
3026   return asImpl().visitExpr(e);
3027 }
3028
3029 template <typename Impl, typename Result>
3030 Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
3031   switch (e->getCastKind()) {
3032
3033   // No-op casts don't change the type, so we just ignore them.
3034   case CK_NoOp:
3035     return asImpl().visit(e->getSubExpr());
3036
3037   // These casts can change the type.
3038   case CK_CPointerToObjCPointerCast:
3039   case CK_BlockPointerToObjCPointerCast:
3040   case CK_AnyPointerToBlockPointerCast:
3041   case CK_BitCast: {
3042     llvm::Type *resultType = CGF.ConvertType(e->getType());
3043     assert(e->getSubExpr()->getType()->hasPointerRepresentation());
3044     Result result = asImpl().visit(e->getSubExpr());
3045     return asImpl().emitBitCast(result, resultType);
3046   }
3047
3048   // Handle some casts specially.
3049   case CK_LValueToRValue:
3050     return asImpl().visitLValueToRValue(e->getSubExpr());
3051   case CK_ARCConsumeObject:
3052     return asImpl().visitConsumeObject(e->getSubExpr());
3053   case CK_ARCExtendBlockObject:
3054     return asImpl().visitExtendBlockObject(e->getSubExpr());
3055   case CK_ARCReclaimReturnedObject:
3056     return asImpl().visitReclaimReturnedObject(e->getSubExpr());
3057
3058   // Otherwise, use the default logic.
3059   default:
3060     return asImpl().visitExpr(e);
3061   }
3062 }
3063
3064 template <typename Impl, typename Result>
3065 Result
3066 ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
3067   switch (e->getOpcode()) {
3068   case BO_Comma:
3069     CGF.EmitIgnoredExpr(e->getLHS());
3070     CGF.EnsureInsertPoint();
3071     return asImpl().visit(e->getRHS());
3072
3073   case BO_Assign:
3074     return asImpl().visitBinAssign(e);
3075
3076   default:
3077     return asImpl().visitExpr(e);
3078   }
3079 }
3080
3081 template <typename Impl, typename Result>
3082 Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
3083   switch (e->getLHS()->getType().getObjCLifetime()) {
3084   case Qualifiers::OCL_ExplicitNone:
3085     return asImpl().visitBinAssignUnsafeUnretained(e);
3086
3087   case Qualifiers::OCL_Weak:
3088     return asImpl().visitBinAssignWeak(e);
3089
3090   case Qualifiers::OCL_Autoreleasing:
3091     return asImpl().visitBinAssignAutoreleasing(e);
3092
3093   case Qualifiers::OCL_Strong:
3094     return asImpl().visitBinAssignStrong(e);
3095
3096   case Qualifiers::OCL_None:
3097     return asImpl().visitExpr(e);
3098   }
3099   llvm_unreachable("bad ObjC ownership qualifier");
3100 }
3101
3102 /// The default rule for __unsafe_unretained emits the RHS recursively,
3103 /// stores into the unsafe variable, and propagates the result outward.
3104 template <typename Impl, typename Result>
3105 Result ARCExprEmitter<Impl,Result>::
3106                     visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
3107   // Recursively emit the RHS.
3108   // For __block safety, do this before emitting the LHS.
3109   Result result = asImpl().visit(e->getRHS());
3110
3111   // Perform the store.
3112   LValue lvalue =
3113     CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
3114   CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
3115                              lvalue);
3116
3117   return result;
3118 }
3119
3120 template <typename Impl, typename Result>
3121 Result
3122 ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) {
3123   return asImpl().visitExpr(e);
3124 }
3125
3126 template <typename Impl, typename Result>
3127 Result
3128 ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) {
3129   return asImpl().visitExpr(e);
3130 }
3131
3132 template <typename Impl, typename Result>
3133 Result
3134 ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
3135   return asImpl().visitExpr(e);
3136 }
3137
3138 /// The general expression-emission logic.
3139 template <typename Impl, typename Result>
3140 Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
3141   // We should *never* see a nested full-expression here, because if
3142   // we fail to emit at +1, our caller must not retain after we close
3143   // out the full-expression.  This isn't as important in the unsafe
3144   // emitter.
3145   assert(!isa<ExprWithCleanups>(e));
3146
3147   // Look through parens, __extension__, generic selection, etc.
3148   e = e->IgnoreParens();
3149
3150   // Handle certain kinds of casts.
3151   if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
3152     return asImpl().visitCastExpr(ce);
3153
3154   // Handle the comma operator.
3155   } else if (auto op = dyn_cast<BinaryOperator>(e)) {
3156     return asImpl().visitBinaryOperator(op);
3157
3158   // TODO: handle conditional operators here
3159
3160   // For calls and message sends, use the retained-call logic.
3161   // Delegate inits are a special case in that they're the only
3162   // returns-retained expression that *isn't* surrounded by
3163   // a consume.
3164   } else if (isa<CallExpr>(e) ||
3165              (isa<ObjCMessageExpr>(e) &&
3166               !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
3167     return asImpl().visitCall(e);
3168
3169   // Look through pseudo-object expressions.
3170   } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
3171     return asImpl().visitPseudoObjectExpr(pseudo);
3172   } else if (auto *be = dyn_cast<BlockExpr>(e))
3173     return asImpl().visitBlockExpr(be);
3174
3175   return asImpl().visitExpr(e);
3176 }
3177
3178 namespace {
3179
3180 /// An emitter for +1 results.
3181 struct ARCRetainExprEmitter :
3182   public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> {
3183
3184   ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3185
3186   llvm::Value *getValueOfResult(TryEmitResult result) {
3187     return result.getPointer();
3188   }
3189
3190   TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) {
3191     llvm::Value *value = result.getPointer();
3192     value = CGF.Builder.CreateBitCast(value, resultType);
3193     result.setPointer(value);
3194     return result;
3195   }
3196
3197   TryEmitResult visitLValueToRValue(const Expr *e) {
3198     return tryEmitARCRetainLoadOfScalar(CGF, e);
3199   }
3200
3201   /// For consumptions, just emit the subexpression and thus elide
3202   /// the retain/release pair.
3203   TryEmitResult visitConsumeObject(const Expr *e) {
3204     llvm::Value *result = CGF.EmitScalarExpr(e);
3205     return TryEmitResult(result, true);
3206   }
3207
3208   TryEmitResult visitBlockExpr(const BlockExpr *e) {
3209     TryEmitResult result = visitExpr(e);
3210     // Avoid the block-retain if this is a block literal that doesn't need to be
3211     // copied to the heap.
3212     if (e->getBlockDecl()->canAvoidCopyToHeap())
3213       result.setInt(true);
3214     return result;
3215   }
3216
3217   /// Block extends are net +0.  Naively, we could just recurse on
3218   /// the subexpression, but actually we need to ensure that the
3219   /// value is copied as a block, so there's a little filter here.
3220   TryEmitResult visitExtendBlockObject(const Expr *e) {
3221     llvm::Value *result; // will be a +0 value
3222
3223     // If we can't safely assume the sub-expression will produce a
3224     // block-copied value, emit the sub-expression at +0.
3225     if (shouldEmitSeparateBlockRetain(e)) {
3226       result = CGF.EmitScalarExpr(e);
3227
3228     // Otherwise, try to emit the sub-expression at +1 recursively.
3229     } else {
3230       TryEmitResult subresult = asImpl().visit(e);
3231
3232       // If that produced a retained value, just use that.
3233       if (subresult.getInt()) {
3234         return subresult;
3235       }
3236
3237       // Otherwise it's +0.
3238       result = subresult.getPointer();
3239     }
3240
3241     // Retain the object as a block.
3242     result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
3243     return TryEmitResult(result, true);
3244   }
3245
3246   /// For reclaims, emit the subexpression as a retained call and
3247   /// skip the consumption.
3248   TryEmitResult visitReclaimReturnedObject(const Expr *e) {
3249     llvm::Value *result = emitARCRetainCallResult(CGF, e);
3250     return TryEmitResult(result, true);
3251   }
3252
3253   /// When we have an undecorated call, retroactively do a claim.
3254   TryEmitResult visitCall(const Expr *e) {
3255     llvm::Value *result = emitARCRetainCallResult(CGF, e);
3256     return TryEmitResult(result, true);
3257   }
3258
3259   // TODO: maybe special-case visitBinAssignWeak?
3260
3261   TryEmitResult visitExpr(const Expr *e) {
3262     // We didn't find an obvious production, so emit what we've got and
3263     // tell the caller that we didn't manage to retain.
3264     llvm::Value *result = CGF.EmitScalarExpr(e);
3265     return TryEmitResult(result, false);
3266   }
3267 };
3268 }
3269
3270 static TryEmitResult
3271 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
3272   return ARCRetainExprEmitter(CGF).visit(e);
3273 }
3274
3275 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
3276                                                 LValue lvalue,
3277                                                 QualType type) {
3278   TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
3279   llvm::Value *value = result.getPointer();
3280   if (!result.getInt())
3281     value = CGF.EmitARCRetain(type, value);
3282   return value;
3283 }
3284
3285 /// EmitARCRetainScalarExpr - Semantically equivalent to
3286 /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
3287 /// best-effort attempt to peephole expressions that naturally produce
3288 /// retained objects.
3289 llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
3290   // The retain needs to happen within the full-expression.
3291   if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3292     RunCleanupsScope scope(*this);
3293     return EmitARCRetainScalarExpr(cleanups->getSubExpr());
3294   }
3295
3296   TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
3297   llvm::Value *value = result.getPointer();
3298   if (!result.getInt())
3299     value = EmitARCRetain(e->getType(), value);
3300   return value;
3301 }
3302
3303 llvm::Value *
3304 CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
3305   // The retain needs to happen within the full-expression.
3306   if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3307     RunCleanupsScope scope(*this);
3308     return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
3309   }
3310
3311   TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
3312   llvm::Value *value = result.getPointer();
3313   if (result.getInt())
3314     value = EmitARCAutorelease(value);
3315   else
3316     value = EmitARCRetainAutorelease(e->getType(), value);
3317   return value;
3318 }
3319
3320 llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
3321   llvm::Value *result;
3322   bool doRetain;
3323
3324   if (shouldEmitSeparateBlockRetain(e)) {
3325     result = EmitScalarExpr(e);
3326     doRetain = true;
3327   } else {
3328     TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
3329     result = subresult.getPointer();
3330     doRetain = !subresult.getInt();
3331   }
3332
3333   if (doRetain)
3334     result = EmitARCRetainBlock(result, /*mandatory*/ true);
3335   return EmitObjCConsumeObject(e->getType(), result);
3336 }
3337
3338 llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
3339   // In ARC, retain and autorelease the expression.
3340   if (getLangOpts().ObjCAutoRefCount) {
3341     // Do so before running any cleanups for the full-expression.
3342     // EmitARCRetainAutoreleaseScalarExpr does this for us.
3343     return EmitARCRetainAutoreleaseScalarExpr(expr);
3344   }
3345
3346   // Otherwise, use the normal scalar-expression emission.  The
3347   // exception machinery doesn't do anything special with the
3348   // exception like retaining it, so there's no safety associated with
3349   // only running cleanups after the throw has started, and when it
3350   // matters it tends to be substantially inferior code.
3351   return EmitScalarExpr(expr);
3352 }
3353
3354 namespace {
3355
3356 /// An emitter for assigning into an __unsafe_unretained context.
3357 struct ARCUnsafeUnretainedExprEmitter :
3358   public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> {
3359
3360   ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3361
3362   llvm::Value *getValueOfResult(llvm::Value *value) {
3363     return value;
3364   }
3365
3366   llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) {
3367     return CGF.Builder.CreateBitCast(value, resultType);
3368   }
3369
3370   llvm::Value *visitLValueToRValue(const Expr *e) {
3371     return CGF.EmitScalarExpr(e);
3372   }
3373
3374   /// For consumptions, just emit the subexpression and perform the
3375   /// consumption like normal.
3376   llvm::Value *visitConsumeObject(const Expr *e) {
3377     llvm::Value *value = CGF.EmitScalarExpr(e);
3378     return CGF.EmitObjCConsumeObject(e->getType(), value);
3379   }
3380
3381   /// No special logic for block extensions.  (This probably can't
3382   /// actually happen in this emitter, though.)
3383   llvm::Value *visitExtendBlockObject(const Expr *e) {
3384     return CGF.EmitARCExtendBlockObject(e);
3385   }
3386
3387   /// For reclaims, perform an unsafeClaim if that's enabled.
3388   llvm::Value *visitReclaimReturnedObject(const Expr *e) {
3389     return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true);
3390   }
3391
3392   /// When we have an undecorated call, just emit it without adding
3393   /// the unsafeClaim.
3394   llvm::Value *visitCall(const Expr *e) {
3395     return CGF.EmitScalarExpr(e);
3396   }
3397
3398   /// Just do normal scalar emission in the default case.
3399   llvm::Value *visitExpr(const Expr *e) {
3400     return CGF.EmitScalarExpr(e);
3401   }
3402 };
3403 }
3404
3405 static llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF,
3406                                                       const Expr *e) {
3407   return ARCUnsafeUnretainedExprEmitter(CGF).visit(e);
3408 }
3409
3410 /// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to
3411 /// immediately releasing the resut of EmitARCRetainScalarExpr, but
3412 /// avoiding any spurious retains, including by performing reclaims
3413 /// with objc_unsafeClaimAutoreleasedReturnValue.
3414 llvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) {
3415   // Look through full-expressions.
3416   if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3417     RunCleanupsScope scope(*this);
3418     return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr());
3419   }
3420
3421   return emitARCUnsafeUnretainedScalarExpr(*this, e);
3422 }
3423
3424 std::pair<LValue,llvm::Value*>
3425 CodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e,
3426                                               bool ignored) {
3427   // Evaluate the RHS first.  If we're ignoring the result, assume
3428   // that we can emit at an unsafe +0.
3429   llvm::Value *value;
3430   if (ignored) {
3431     value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS());
3432   } else {
3433     value = EmitScalarExpr(e->getRHS());
3434   }
3435
3436   // Emit the LHS and perform the store.
3437   LValue lvalue = EmitLValue(e->getLHS());
3438   EmitStoreOfScalar(value, lvalue);
3439
3440   return std::pair<LValue,llvm::Value*>(std::move(lvalue), value);
3441 }
3442
3443 std::pair<LValue,llvm::Value*>
3444 CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
3445                                     bool ignored) {
3446   // Evaluate the RHS first.
3447   TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
3448   llvm::Value *value = result.getPointer();
3449
3450   bool hasImmediateRetain = result.getInt();
3451
3452   // If we didn't emit a retained object, and the l-value is of block
3453   // type, then we need to emit the block-retain immediately in case
3454   // it invalidates the l-value.
3455   if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
3456     value = EmitARCRetainBlock(value, /*mandatory*/ false);
3457     hasImmediateRetain = true;
3458   }
3459
3460   LValue lvalue = EmitLValue(e->getLHS());
3461
3462   // If the RHS was emitted retained, expand this.
3463   if (hasImmediateRetain) {
3464     llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation());
3465     EmitStoreOfScalar(value, lvalue);
3466     EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
3467   } else {
3468     value = EmitARCStoreStrong(lvalue, value, ignored);
3469   }
3470
3471   return std::pair<LValue,llvm::Value*>(lvalue, value);
3472 }
3473
3474 std::pair<LValue,llvm::Value*>
3475 CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
3476   llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
3477   LValue lvalue = EmitLValue(e->getLHS());
3478
3479   EmitStoreOfScalar(value, lvalue);
3480
3481   return std::pair<LValue,llvm::Value*>(lvalue, value);
3482 }
3483
3484 void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
3485                                           const ObjCAutoreleasePoolStmt &ARPS) {
3486   const Stmt *subStmt = ARPS.getSubStmt();
3487   const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
3488
3489   CGDebugInfo *DI = getDebugInfo();
3490   if (DI)
3491     DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
3492
3493   // Keep track of the current cleanup stack depth.
3494   RunCleanupsScope Scope(*this);
3495   if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
3496     llvm::Value *token = EmitObjCAutoreleasePoolPush();
3497     EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
3498   } else {
3499     llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
3500     EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
3501   }
3502
3503   for (const auto *I : S.body())
3504     EmitStmt(I);
3505
3506   if (DI)
3507     DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
3508 }
3509
3510 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3511 /// make sure it survives garbage collection until this point.
3512 void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
3513   // We just use an inline assembly.
3514   llvm::FunctionType *extenderType
3515     = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
3516   llvm::InlineAsm *extender = llvm::InlineAsm::get(extenderType,
3517                                                    /* assembly */ "",
3518                                                    /* constraints */ "r",
3519                                                    /* side effects */ true);
3520
3521   object = Builder.CreateBitCast(object, VoidPtrTy);
3522   EmitNounwindRuntimeCall(extender, object);
3523 }
3524
3525 /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
3526 /// non-trivial copy assignment function, produce following helper function.
3527 /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
3528 ///
3529 llvm::Constant *
3530 CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
3531                                         const ObjCPropertyImplDecl *PID) {
3532   if (!getLangOpts().CPlusPlus ||
3533       !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
3534     return nullptr;
3535   QualType Ty = PID->getPropertyIvarDecl()->getType();
3536   if (!Ty->isRecordType())
3537     return nullptr;
3538   const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3539   if ((!(PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic)))
3540     return nullptr;
3541   llvm::Constant *HelperFn = nullptr;
3542   if (hasTrivialSetExpr(PID))
3543     return nullptr;
3544   assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
3545   if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
3546     return HelperFn;
3547
3548   ASTContext &C = getContext();
3549   IdentifierInfo *II
3550     = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
3551
3552   QualType ReturnTy = C.VoidTy;
3553   QualType DestTy = C.getPointerType(Ty);
3554   QualType SrcTy = Ty;
3555   SrcTy.addConst();
3556   SrcTy = C.getPointerType(SrcTy);
3557
3558   SmallVector<QualType, 2> ArgTys;
3559   ArgTys.push_back(DestTy);
3560   ArgTys.push_back(SrcTy);
3561   QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {});
3562
3563   FunctionDecl *FD = FunctionDecl::Create(
3564       C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
3565       FunctionTy, nullptr, SC_Static, false, false);
3566
3567   FunctionArgList args;
3568   ImplicitParamDecl DstDecl(C, FD, SourceLocation(), /*Id=*/nullptr, DestTy,
3569                             ImplicitParamDecl::Other);
3570   args.push_back(&DstDecl);
3571   ImplicitParamDecl SrcDecl(C, FD, SourceLocation(), /*Id=*/nullptr, SrcTy,
3572                             ImplicitParamDecl::Other);
3573   args.push_back(&SrcDecl);
3574
3575   const CGFunctionInfo &FI =
3576       CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
3577
3578   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3579
3580   llvm::Function *Fn =
3581     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3582                            "__assign_helper_atomic_property_",
3583                            &CGM.getModule());
3584
3585   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
3586
3587   StartFunction(FD, ReturnTy, Fn, FI, args);
3588
3589   DeclRefExpr DstExpr(C, &DstDecl, false, DestTy, VK_RValue, SourceLocation());
3590   UnaryOperator *DST = UnaryOperator::Create(
3591       C, &DstExpr, UO_Deref, DestTy->getPointeeType(), VK_LValue, OK_Ordinary,
3592       SourceLocation(), false, FPOptionsOverride());
3593
3594   DeclRefExpr SrcExpr(C, &SrcDecl, false, SrcTy, VK_RValue, SourceLocation());
3595   UnaryOperator *SRC = UnaryOperator::Create(
3596       C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary,
3597       SourceLocation(), false, FPOptionsOverride());
3598
3599   Expr *Args[2] = {DST, SRC};
3600   CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
3601   CXXOperatorCallExpr *TheCall = CXXOperatorCallExpr::Create(
3602       C, OO_Equal, CalleeExp->getCallee(), Args, DestTy->getPointeeType(),
3603       VK_LValue, SourceLocation(), FPOptionsOverride());
3604
3605   EmitStmt(TheCall);
3606
3607   FinishFunction();
3608   HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3609   CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
3610   return HelperFn;
3611 }
3612
3613 llvm::Constant *
3614 CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
3615                                             const ObjCPropertyImplDecl *PID) {
3616   if (!getLangOpts().CPlusPlus ||
3617       !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
3618     return nullptr;
3619   const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3620   QualType Ty = PD->getType();
3621   if (!Ty->isRecordType())
3622     return nullptr;
3623   if ((!(PD->getPropertyAttributes() & ObjCPropertyAttribute::kind_atomic)))
3624     return nullptr;
3625   llvm::Constant *HelperFn = nullptr;
3626   if (hasTrivialGetExpr(PID))
3627     return nullptr;
3628   assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
3629   if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
3630     return HelperFn;
3631
3632   ASTContext &C = getContext();
3633   IdentifierInfo *II =
3634       &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
3635
3636   QualType ReturnTy = C.VoidTy;
3637   QualType DestTy = C.getPointerType(Ty);
3638   QualType SrcTy = Ty;
3639   SrcTy.addConst();
3640   SrcTy = C.getPointerType(SrcTy);
3641
3642   SmallVector<QualType, 2> ArgTys;
3643   ArgTys.push_back(DestTy);
3644   ArgTys.push_back(SrcTy);
3645   QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {});
3646
3647   FunctionDecl *FD = FunctionDecl::Create(
3648       C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
3649       FunctionTy, nullptr, SC_Static, false, false);
3650
3651   FunctionArgList args;
3652   ImplicitParamDecl DstDecl(C, FD, SourceLocation(), /*Id=*/nullptr, DestTy,
3653                             ImplicitParamDecl::Other);
3654   args.push_back(&DstDecl);
3655   ImplicitParamDecl SrcDecl(C, FD, SourceLocation(), /*Id=*/nullptr, SrcTy,
3656                             ImplicitParamDecl::Other);
3657   args.push_back(&SrcDecl);
3658
3659   const CGFunctionInfo &FI =
3660       CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
3661
3662   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3663
3664   llvm::Function *Fn = llvm::Function::Create(
3665       LTy, llvm::GlobalValue::InternalLinkage, "__copy_helper_atomic_property_",
3666       &CGM.getModule());
3667
3668   CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
3669
3670   StartFunction(FD, ReturnTy, Fn, FI, args);
3671
3672   DeclRefExpr SrcExpr(getContext(), &SrcDecl, false, SrcTy, VK_RValue,
3673                       SourceLocation());
3674
3675   UnaryOperator *SRC = UnaryOperator::Create(
3676       C, &SrcExpr, UO_Deref, SrcTy->getPointeeType(), VK_LValue, OK_Ordinary,
3677       SourceLocation(), false, FPOptionsOverride());
3678
3679   CXXConstructExpr *CXXConstExpr =
3680     cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
3681
3682   SmallVector<Expr*, 4> ConstructorArgs;
3683   ConstructorArgs.push_back(SRC);
3684   ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()),
3685                          CXXConstExpr->arg_end());
3686
3687   CXXConstructExpr *TheCXXConstructExpr =
3688     CXXConstructExpr::Create(C, Ty, SourceLocation(),
3689                              CXXConstExpr->getConstructor(),
3690                              CXXConstExpr->isElidable(),
3691                              ConstructorArgs,
3692                              CXXConstExpr->hadMultipleCandidates(),
3693                              CXXConstExpr->isListInitialization(),
3694                              CXXConstExpr->isStdInitListInitialization(),
3695                              CXXConstExpr->requiresZeroInitialization(),
3696                              CXXConstExpr->getConstructionKind(),
3697                              SourceRange());
3698
3699   DeclRefExpr DstExpr(getContext(), &DstDecl, false, DestTy, VK_RValue,
3700                       SourceLocation());
3701
3702   RValue DV = EmitAnyExpr(&DstExpr);
3703   CharUnits Alignment
3704     = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
3705   EmitAggExpr(TheCXXConstructExpr,
3706               AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment),
3707                                     Qualifiers(),
3708                                     AggValueSlot::IsDestructed,
3709                                     AggValueSlot::DoesNotNeedGCBarriers,
3710                                     AggValueSlot::IsNotAliased,
3711                                     AggValueSlot::DoesNotOverlap));
3712
3713   FinishFunction();
3714   HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3715   CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
3716   return HelperFn;
3717 }
3718
3719 llvm::Value *
3720 CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
3721   // Get selectors for retain/autorelease.
3722   IdentifierInfo *CopyID = &getContext().Idents.get("copy");
3723   Selector CopySelector =
3724       getContext().Selectors.getNullarySelector(CopyID);
3725   IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3726   Selector AutoreleaseSelector =
3727       getContext().Selectors.getNullarySelector(AutoreleaseID);
3728
3729   // Emit calls to retain/autorelease.
3730   CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3731   llvm::Value *Val = Block;
3732   RValue Result;
3733   Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3734                                        Ty, CopySelector,
3735                                        Val, CallArgList(), nullptr, nullptr);
3736   Val = Result.getScalarVal();
3737   Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3738                                        Ty, AutoreleaseSelector,
3739                                        Val, CallArgList(), nullptr, nullptr);
3740   Val = Result.getScalarVal();
3741   return Val;
3742 }
3743
3744 llvm::Value *
3745 CodeGenFunction::EmitBuiltinAvailable(ArrayRef<llvm::Value *> Args) {
3746   assert(Args.size() == 3 && "Expected 3 argument here!");
3747
3748   if (!CGM.IsOSVersionAtLeastFn) {
3749     llvm::FunctionType *FTy =
3750         llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false);
3751     CGM.IsOSVersionAtLeastFn =
3752         CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast");
3753   }
3754
3755   llvm::Value *CallRes =
3756       EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args);
3757
3758   return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty));
3759 }
3760
3761 void CodeGenModule::emitAtAvailableLinkGuard() {
3762   if (!IsOSVersionAtLeastFn)
3763     return;
3764   // @available requires CoreFoundation only on Darwin.
3765   if (!Target.getTriple().isOSDarwin())
3766     return;
3767   // Add -framework CoreFoundation to the linker commands. We still want to
3768   // emit the core foundation reference down below because otherwise if
3769   // CoreFoundation is not used in the code, the linker won't link the
3770   // framework.
3771   auto &Context = getLLVMContext();
3772   llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
3773                              llvm::MDString::get(Context, "CoreFoundation")};
3774   LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args));
3775   // Emit a reference to a symbol from CoreFoundation to ensure that
3776   // CoreFoundation is linked into the final binary.
3777   llvm::FunctionType *FTy =
3778       llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false);
3779   llvm::FunctionCallee CFFunc =
3780       CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber");
3781
3782   llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false);
3783   llvm::FunctionCallee CFLinkCheckFuncRef = CreateRuntimeFunction(
3784       CheckFTy, "__clang_at_available_requires_core_foundation_framework",
3785       llvm::AttributeList(), /*Local=*/true);
3786   llvm::Function *CFLinkCheckFunc =
3787       cast<llvm::Function>(CFLinkCheckFuncRef.getCallee()->stripPointerCasts());
3788   if (CFLinkCheckFunc->empty()) {
3789     CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
3790     CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility);
3791     CodeGenFunction CGF(*this);
3792     CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc));
3793     CGF.EmitNounwindRuntimeCall(CFFunc,
3794                                 llvm::Constant::getNullValue(VoidPtrTy));
3795     CGF.Builder.CreateUnreachable();
3796     addCompilerUsedGlobal(CFLinkCheckFunc);
3797   }
3798 }
3799
3800 CGObjCRuntime::~CGObjCRuntime() {}