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