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