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