1 //===---- CGObjC.cpp - Emit LLVM Code for Objective-C ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code to emit Objective-C code as LLVM code.
12 //===----------------------------------------------------------------------===//
14 #include "CGDebugInfo.h"
15 #include "CGObjCRuntime.h"
16 #include "CodeGenFunction.h"
17 #include "CodeGenModule.h"
18 #include "TargetInfo.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/StmtObjC.h"
22 #include "clang/Basic/Diagnostic.h"
23 #include "clang/CodeGen/CGFunctionInfo.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/IR/CallSite.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/InlineAsm.h"
28 using namespace clang;
29 using namespace CodeGen;
31 typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
33 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e);
34 static RValue AdjustObjCObjectType(CodeGenFunction &CGF,
38 /// Given the address of a variable of pointer type, find the correct
39 /// null to store into it.
40 static llvm::Constant *getNullForVariable(Address addr) {
41 llvm::Type *type = addr.getElementType();
42 return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
45 /// Emits an instance of NSConstantString representing the object.
46 llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
49 CGM.getObjCRuntime().GenerateConstantString(E->getString()).getPointer();
50 // FIXME: This bitcast should just be made an invariant on the Runtime.
51 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
54 /// EmitObjCBoxedExpr - This routine generates code to call
55 /// the appropriate expression boxing method. This will either be
56 /// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:],
57 /// or [NSValue valueWithBytes:objCType:].
60 CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) {
61 // Generate the correct selector for this literal's concrete type.
63 const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod();
64 const Expr *SubExpr = E->getSubExpr();
65 assert(BoxingMethod && "BoxingMethod is null");
66 assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method");
67 Selector Sel = BoxingMethod->getSelector();
69 // Generate a reference to the class pointer, which will be the receiver.
70 // Assumes that the method was introduced in the class that should be
71 // messaged (avoids pulling it out of the result type).
72 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
73 const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface();
74 llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl);
77 const ParmVarDecl *ArgDecl = *BoxingMethod->param_begin();
78 QualType ArgQT = ArgDecl->getType().getUnqualifiedType();
80 // ObjCBoxedExpr supports boxing of structs and unions
81 // via [NSValue valueWithBytes:objCType:]
82 const QualType ValueType(SubExpr->getType().getCanonicalType());
83 if (ValueType->isObjCBoxableRecordType()) {
84 // Emit CodeGen for first parameter
85 // and cast value to correct type
86 Address Temporary = CreateMemTemp(SubExpr->getType());
87 EmitAnyExprToMem(SubExpr, Temporary, Qualifiers(), /*isInit*/ true);
88 Address BitCast = Builder.CreateBitCast(Temporary, ConvertType(ArgQT));
89 Args.add(RValue::get(BitCast.getPointer()), ArgQT);
91 // Create char array to store type encoding
93 getContext().getObjCEncodingForType(ValueType, Str);
94 llvm::Constant *GV = CGM.GetAddrOfConstantCString(Str).getPointer();
96 // Cast type encoding to correct type
97 const ParmVarDecl *EncodingDecl = BoxingMethod->parameters()[1];
98 QualType EncodingQT = EncodingDecl->getType().getUnqualifiedType();
99 llvm::Value *Cast = Builder.CreateBitCast(GV, ConvertType(EncodingQT));
101 Args.add(RValue::get(Cast), EncodingQT);
103 Args.add(EmitAnyExpr(SubExpr), ArgQT);
106 RValue result = Runtime.GenerateMessageSend(
107 *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver,
108 Args, ClassDecl, BoxingMethod);
109 return Builder.CreateBitCast(result.getScalarVal(),
110 ConvertType(E->getType()));
113 llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E,
114 const ObjCMethodDecl *MethodWithObjects) {
115 ASTContext &Context = CGM.getContext();
116 const ObjCDictionaryLiteral *DLE = nullptr;
117 const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E);
119 DLE = cast<ObjCDictionaryLiteral>(E);
121 // Optimize empty collections by referencing constants, when available.
122 uint64_t NumElements =
123 ALE ? ALE->getNumElements() : DLE->getNumElements();
124 if (NumElements == 0 && CGM.getLangOpts().ObjCRuntime.hasEmptyCollections()) {
125 StringRef ConstantName = ALE ? "__NSArray0__" : "__NSDictionary0__";
126 QualType IdTy(CGM.getContext().getObjCIdType());
127 llvm::Constant *Constant =
128 CGM.CreateRuntimeVariable(ConvertType(IdTy), ConstantName);
129 Address Addr(Constant, Context.getTypeAlignInChars(IdTy));
130 LValue LV = MakeAddrLValue(Addr, IdTy);
131 return Builder.CreateBitCast(EmitLoadOfScalar(LV, E->getLocStart()),
132 ConvertType(E->getType()));
135 // Compute the type of the array we're initializing.
136 llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
138 QualType ElementType = Context.getObjCIdType().withConst();
139 QualType ElementArrayType
140 = Context.getConstantArrayType(ElementType, APNumElements,
141 ArrayType::Normal, /*IndexTypeQuals=*/0);
143 // Allocate the temporary array(s).
144 Address Objects = CreateMemTemp(ElementArrayType, "objects");
145 Address Keys = Address::invalid();
147 Keys = CreateMemTemp(ElementArrayType, "keys");
149 // In ARC, we may need to do extra work to keep all the keys and
150 // values alive until after the call.
151 SmallVector<llvm::Value *, 16> NeededObjects;
152 bool TrackNeededObjects =
153 (getLangOpts().ObjCAutoRefCount &&
154 CGM.getCodeGenOpts().OptimizationLevel != 0);
156 // Perform the actual initialialization of the array(s).
157 for (uint64_t i = 0; i < NumElements; i++) {
159 // Emit the element and store it to the appropriate array slot.
160 const Expr *Rhs = ALE->getElement(i);
161 LValue LV = MakeAddrLValue(
162 Builder.CreateConstArrayGEP(Objects, i, getPointerSize()),
163 ElementType, AlignmentSource::Decl);
165 llvm::Value *value = EmitScalarExpr(Rhs);
166 EmitStoreThroughLValue(RValue::get(value), LV, true);
167 if (TrackNeededObjects) {
168 NeededObjects.push_back(value);
171 // Emit the key and store it to the appropriate array slot.
172 const Expr *Key = DLE->getKeyValueElement(i).Key;
173 LValue KeyLV = MakeAddrLValue(
174 Builder.CreateConstArrayGEP(Keys, i, getPointerSize()),
175 ElementType, AlignmentSource::Decl);
176 llvm::Value *keyValue = EmitScalarExpr(Key);
177 EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
179 // Emit the value and store it to the appropriate array slot.
180 const Expr *Value = DLE->getKeyValueElement(i).Value;
181 LValue ValueLV = MakeAddrLValue(
182 Builder.CreateConstArrayGEP(Objects, i, getPointerSize()),
183 ElementType, AlignmentSource::Decl);
184 llvm::Value *valueValue = EmitScalarExpr(Value);
185 EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
186 if (TrackNeededObjects) {
187 NeededObjects.push_back(keyValue);
188 NeededObjects.push_back(valueValue);
193 // Generate the argument list.
195 ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
196 const ParmVarDecl *argDecl = *PI++;
197 QualType ArgQT = argDecl->getType().getUnqualifiedType();
198 Args.add(RValue::get(Objects.getPointer()), ArgQT);
201 ArgQT = argDecl->getType().getUnqualifiedType();
202 Args.add(RValue::get(Keys.getPointer()), ArgQT);
205 ArgQT = argDecl->getType().getUnqualifiedType();
207 llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
208 Args.add(RValue::get(Count), ArgQT);
210 // Generate a reference to the class pointer, which will be the receiver.
211 Selector Sel = MethodWithObjects->getSelector();
212 QualType ResultType = E->getType();
213 const ObjCObjectPointerType *InterfacePointerType
214 = ResultType->getAsObjCInterfacePointerType();
215 ObjCInterfaceDecl *Class
216 = InterfacePointerType->getObjectType()->getInterface();
217 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
218 llvm::Value *Receiver = Runtime.GetClass(*this, Class);
220 // Generate the message send.
221 RValue result = Runtime.GenerateMessageSend(
222 *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel,
223 Receiver, Args, Class, MethodWithObjects);
225 // The above message send needs these objects, but in ARC they are
226 // passed in a buffer that is essentially __unsafe_unretained.
227 // Therefore we must prevent the optimizer from releasing them until
229 if (TrackNeededObjects) {
230 EmitARCIntrinsicUse(NeededObjects);
233 return Builder.CreateBitCast(result.getScalarVal(),
234 ConvertType(E->getType()));
237 llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) {
238 return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod());
241 llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral(
242 const ObjCDictionaryLiteral *E) {
243 return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod());
247 llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
249 // Note that this implementation allows for non-constant strings to be passed
250 // as arguments to @selector(). Currently, the only thing preventing this
251 // behaviour is the type checking in the front end.
252 return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
255 llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
256 // FIXME: This should pass the Decl not the name.
257 return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
260 /// \brief Adjust the type of an Objective-C object that doesn't match up due
261 /// to type erasure at various points, e.g., related result types or the use
262 /// of parameterized classes.
263 static RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ExpT,
265 if (!ExpT->isObjCRetainableType())
268 // If the converted types are the same, we're done.
269 llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT);
270 if (ExpLLVMTy == Result.getScalarVal()->getType())
273 // We have applied a substitution. Cast the rvalue appropriately.
274 return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
278 /// Decide whether to extend the lifetime of the receiver of a
279 /// returns-inner-pointer message.
281 shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
282 switch (message->getReceiverKind()) {
284 // For a normal instance message, we should extend unless the
285 // receiver is loaded from a variable with precise lifetime.
286 case ObjCMessageExpr::Instance: {
287 const Expr *receiver = message->getInstanceReceiver();
289 // Look through OVEs.
290 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
291 if (opaque->getSourceExpr())
292 receiver = opaque->getSourceExpr()->IgnoreParens();
295 const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
296 if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
297 receiver = ice->getSubExpr()->IgnoreParens();
299 // Look through OVEs.
300 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
301 if (opaque->getSourceExpr())
302 receiver = opaque->getSourceExpr()->IgnoreParens();
305 // Only __strong variables.
306 if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
309 // All ivars and fields have precise lifetime.
310 if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
313 // Otherwise, check for variables.
314 const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
315 if (!declRef) return true;
316 const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
317 if (!var) return true;
319 // All variables have precise lifetime except local variables with
320 // automatic storage duration that aren't specially marked.
321 return (var->hasLocalStorage() &&
322 !var->hasAttr<ObjCPreciseLifetimeAttr>());
325 case ObjCMessageExpr::Class:
326 case ObjCMessageExpr::SuperClass:
327 // It's never necessary for class objects.
330 case ObjCMessageExpr::SuperInstance:
331 // We generally assume that 'self' lives throughout a method call.
335 llvm_unreachable("invalid receiver kind");
338 /// Given an expression of ObjC pointer type, check whether it was
339 /// immediately loaded from an ARC __weak l-value.
340 static const Expr *findWeakLValue(const Expr *E) {
341 assert(E->getType()->isObjCRetainableType());
342 E = E->IgnoreParens();
343 if (auto CE = dyn_cast<CastExpr>(E)) {
344 if (CE->getCastKind() == CK_LValueToRValue) {
345 if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
346 return CE->getSubExpr();
353 RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
354 ReturnValueSlot Return) {
355 // Only the lookup mechanism and first two arguments of the method
356 // implementation vary between runtimes. We can get the receiver and
357 // arguments in generic code.
359 bool isDelegateInit = E->isDelegateInitCall();
361 const ObjCMethodDecl *method = E->getMethodDecl();
363 // If the method is -retain, and the receiver's being loaded from
364 // a __weak variable, peephole the entire operation to objc_loadWeakRetained.
365 if (method && E->getReceiverKind() == ObjCMessageExpr::Instance &&
366 method->getMethodFamily() == OMF_retain) {
367 if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) {
368 LValue lvalue = EmitLValue(lvalueExpr);
369 llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress());
370 return AdjustObjCObjectType(*this, E->getType(), RValue::get(result));
374 // We don't retain the receiver in delegate init calls, and this is
375 // safe because the receiver value is always loaded from 'self',
376 // which we zero out. We don't want to Block_copy block receivers,
380 CGM.getLangOpts().ObjCAutoRefCount &&
382 method->hasAttr<NSConsumesSelfAttr>());
384 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
385 bool isSuperMessage = false;
386 bool isClassMessage = false;
387 ObjCInterfaceDecl *OID = nullptr;
389 QualType ReceiverType;
390 llvm::Value *Receiver = nullptr;
391 switch (E->getReceiverKind()) {
392 case ObjCMessageExpr::Instance:
393 ReceiverType = E->getInstanceReceiver()->getType();
395 TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
396 E->getInstanceReceiver());
397 Receiver = ter.getPointer();
398 if (ter.getInt()) retainSelf = false;
400 Receiver = EmitScalarExpr(E->getInstanceReceiver());
403 case ObjCMessageExpr::Class: {
404 ReceiverType = E->getClassReceiver();
405 const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
406 assert(ObjTy && "Invalid Objective-C class message send");
407 OID = ObjTy->getInterface();
408 assert(OID && "Invalid Objective-C class message send");
409 Receiver = Runtime.GetClass(*this, OID);
410 isClassMessage = true;
414 case ObjCMessageExpr::SuperInstance:
415 ReceiverType = E->getSuperType();
416 Receiver = LoadObjCSelf();
417 isSuperMessage = true;
420 case ObjCMessageExpr::SuperClass:
421 ReceiverType = E->getSuperType();
422 Receiver = LoadObjCSelf();
423 isSuperMessage = true;
424 isClassMessage = true;
429 Receiver = EmitARCRetainNonBlock(Receiver);
431 // In ARC, we sometimes want to "extend the lifetime"
432 // (i.e. retain+autorelease) of receivers of returns-inner-pointer
434 if (getLangOpts().ObjCAutoRefCount && method &&
435 method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
436 shouldExtendReceiverForInnerPointerMessage(E))
437 Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
439 QualType ResultType = method ? method->getReturnType() : E->getType();
442 EmitCallArgs(Args, method, E->arguments(), /*AC*/AbstractCallee(method));
444 // For delegate init calls in ARC, do an unsafe store of null into
445 // self. This represents the call taking direct ownership of that
446 // value. We have to do this after emitting the other call
447 // arguments because they might also reference self, but we don't
448 // have to worry about any of them modifying self because that would
449 // be an undefined read and write of an object in unordered
451 if (isDelegateInit) {
452 assert(getLangOpts().ObjCAutoRefCount &&
453 "delegate init calls should only be marked in ARC");
455 // Do an unsafe store of null into self.
457 GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl());
458 Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
462 if (isSuperMessage) {
463 // super is only valid in an Objective-C method
464 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
465 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
466 result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
468 OMD->getClassInterface(),
475 result = Runtime.GenerateMessageSend(*this, Return, ResultType,
481 // For delegate init calls in ARC, implicitly store the result of
482 // the call back into self. This takes ownership of the value.
483 if (isDelegateInit) {
485 GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl());
486 llvm::Value *newSelf = result.getScalarVal();
488 // The delegate return type isn't necessarily a matching type; in
489 // fact, it's quite likely to be 'id'.
490 llvm::Type *selfTy = selfAddr.getElementType();
491 newSelf = Builder.CreateBitCast(newSelf, selfTy);
493 Builder.CreateStore(newSelf, selfAddr);
496 return AdjustObjCObjectType(*this, E->getType(), result);
500 struct FinishARCDealloc final : EHScopeStack::Cleanup {
501 void Emit(CodeGenFunction &CGF, Flags flags) override {
502 const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
504 const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
505 const ObjCInterfaceDecl *iface = impl->getClassInterface();
506 if (!iface->getSuperClass()) return;
508 bool isCategory = isa<ObjCCategoryImplDecl>(impl);
510 // Call [super dealloc] if we have a superclass.
511 llvm::Value *self = CGF.LoadObjCSelf();
514 CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
515 CGF.getContext().VoidTy,
516 method->getSelector(),
520 /*is class msg*/ false,
527 /// StartObjCMethod - Begin emission of an ObjCMethod. This generates
528 /// the LLVM function and sets the other context used by
530 void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
531 const ObjCContainerDecl *CD) {
532 SourceLocation StartLoc = OMD->getLocStart();
533 FunctionArgList args;
534 // Check if we should generate debug info for this method.
535 if (OMD->hasAttr<NoDebugAttr>())
536 DebugInfo = nullptr; // disable debug info indefinitely for this function
538 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
540 const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
541 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
543 args.push_back(OMD->getSelfDecl());
544 args.push_back(OMD->getCmdDecl());
546 args.append(OMD->param_begin(), OMD->param_end());
549 CurEHLocation = OMD->getLocEnd();
551 StartFunction(OMD, OMD->getReturnType(), Fn, FI, args,
552 OMD->getLocation(), StartLoc);
554 // In ARC, certain methods get an extra cleanup.
555 if (CGM.getLangOpts().ObjCAutoRefCount &&
556 OMD->isInstanceMethod() &&
557 OMD->getSelector().isUnarySelector()) {
558 const IdentifierInfo *ident =
559 OMD->getSelector().getIdentifierInfoForSlot(0);
560 if (ident->isStr("dealloc"))
561 EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
565 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
566 LValue lvalue, QualType type);
568 /// Generate an Objective-C method. An Objective-C method is a C function with
569 /// its pointer, name, and types registered in the class struture.
570 void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
571 StartObjCMethod(OMD, OMD->getClassInterface());
572 PGO.assignRegionCounters(GlobalDecl(OMD), CurFn);
573 assert(isa<CompoundStmt>(OMD->getBody()));
574 incrementProfileCounter(OMD->getBody());
575 EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));
576 FinishFunction(OMD->getBodyRBrace());
579 /// emitStructGetterCall - Call the runtime function to load a property
580 /// into the return value slot.
581 static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar,
582 bool isAtomic, bool hasStrong) {
583 ASTContext &Context = CGF.getContext();
586 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
589 // objc_copyStruct (ReturnValue, &structIvar,
590 // sizeof (Type of Ivar), isAtomic, false);
593 Address dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
594 args.add(RValue::get(dest.getPointer()), Context.VoidPtrTy);
596 src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
597 args.add(RValue::get(src.getPointer()), Context.VoidPtrTy);
599 CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
600 args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
601 args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
602 args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
604 llvm::Constant *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
605 CGCallee callee = CGCallee::forDirect(fn);
606 CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args),
607 callee, ReturnValueSlot(), args);
610 /// Determine whether the given architecture supports unaligned atomic
611 /// accesses. They don't have to be fast, just faster than a function
612 /// call and a mutex.
613 static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
614 // FIXME: Allow unaligned atomic load/store on x86. (It is not
615 // currently supported by the backend.)
619 /// Return the maximum size that permits atomic accesses for the given
621 static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
622 llvm::Triple::ArchType arch) {
623 // ARM has 8-byte atomic accesses, but it's not clear whether we
624 // want to rely on them here.
626 // In the default case, just assume that any size up to a pointer is
627 // fine given adequate alignment.
628 return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
632 class PropertyImplStrategy {
635 /// The 'native' strategy is to use the architecture's provided
636 /// reads and writes.
639 /// Use objc_setProperty and objc_getProperty.
642 /// Use objc_setProperty for the setter, but use expression
643 /// evaluation for the getter.
644 SetPropertyAndExpressionGet,
646 /// Use objc_copyStruct.
649 /// The 'expression' strategy is to emit normal assignment or
650 /// lvalue-to-rvalue expressions.
654 StrategyKind getKind() const { return StrategyKind(Kind); }
656 bool hasStrongMember() const { return HasStrong; }
657 bool isAtomic() const { return IsAtomic; }
658 bool isCopy() const { return IsCopy; }
660 CharUnits getIvarSize() const { return IvarSize; }
661 CharUnits getIvarAlignment() const { return IvarAlignment; }
663 PropertyImplStrategy(CodeGenModule &CGM,
664 const ObjCPropertyImplDecl *propImpl);
668 unsigned IsAtomic : 1;
670 unsigned HasStrong : 1;
673 CharUnits IvarAlignment;
677 /// Pick an implementation strategy for the given property synthesis.
678 PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
679 const ObjCPropertyImplDecl *propImpl) {
680 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
681 ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
683 IsCopy = (setterKind == ObjCPropertyDecl::Copy);
684 IsAtomic = prop->isAtomic();
685 HasStrong = false; // doesn't matter here.
687 // Evaluate the ivar's size and alignment.
688 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
689 QualType ivarType = ivar->getType();
690 std::tie(IvarSize, IvarAlignment) =
691 CGM.getContext().getTypeInfoInChars(ivarType);
693 // If we have a copy property, we always have to use getProperty/setProperty.
694 // TODO: we could actually use setProperty and an expression for non-atomics.
696 Kind = GetSetProperty;
701 if (setterKind == ObjCPropertyDecl::Retain) {
702 // In GC-only, there's nothing special that needs to be done.
703 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
706 // In ARC, if the property is non-atomic, use expression emission,
707 // which translates to objc_storeStrong. This isn't required, but
708 // it's slightly nicer.
709 } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
710 // Using standard expression emission for the setter is only
711 // acceptable if the ivar is __strong, which won't be true if
712 // the property is annotated with __attribute__((NSObject)).
713 // TODO: falling all the way back to objc_setProperty here is
714 // just laziness, though; we could still use objc_storeStrong
715 // if we hacked it right.
716 if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
719 Kind = SetPropertyAndExpressionGet;
722 // Otherwise, we need to at least use setProperty. However, if
723 // the property isn't atomic, we can use normal expression
724 // emission for the getter.
725 } else if (!IsAtomic) {
726 Kind = SetPropertyAndExpressionGet;
729 // Otherwise, we have to use both setProperty and getProperty.
731 Kind = GetSetProperty;
736 // If we're not atomic, just use expression accesses.
742 // Properties on bitfield ivars need to be emitted using expression
743 // accesses even if they're nominally atomic.
744 if (ivar->isBitField()) {
749 // GC-qualified or ARC-qualified ivars need to be emitted as
750 // expressions. This actually works out to being atomic anyway,
751 // except for ARC __strong, but that should trigger the above code.
752 if (ivarType.hasNonTrivialObjCLifetime() ||
753 (CGM.getLangOpts().getGC() &&
754 CGM.getContext().getObjCGCAttrKind(ivarType))) {
759 // Compute whether the ivar has strong members.
760 if (CGM.getLangOpts().getGC())
761 if (const RecordType *recordType = ivarType->getAs<RecordType>())
762 HasStrong = recordType->getDecl()->hasObjectMember();
764 // We can never access structs with object members with a native
765 // access, because we need to use write barriers. This is what
766 // objc_copyStruct is for.
772 // Otherwise, this is target-dependent and based on the size and
773 // alignment of the ivar.
775 // If the size of the ivar is not a power of two, give up. We don't
776 // want to get into the business of doing compare-and-swaps.
777 if (!IvarSize.isPowerOfTwo()) {
782 llvm::Triple::ArchType arch =
783 CGM.getTarget().getTriple().getArch();
785 // Most architectures require memory to fit within a single cache
786 // line, so the alignment has to be at least the size of the access.
787 // Otherwise we have to grab a lock.
788 if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
793 // If the ivar's size exceeds the architecture's maximum atomic
794 // access size, we have to use CopyStruct.
795 if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
800 // Otherwise, we can use native loads and stores.
804 /// \brief Generate an Objective-C property getter function.
806 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
807 /// is illegal within a category.
808 void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
809 const ObjCPropertyImplDecl *PID) {
810 llvm::Constant *AtomicHelperFn =
811 CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID);
812 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
813 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
814 assert(OMD && "Invalid call to generate getter (empty method)");
815 StartObjCMethod(OMD, IMP->getClassInterface());
817 generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
822 static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
823 const Expr *getter = propImpl->getGetterCXXConstructor();
824 if (!getter) return true;
826 // Sema only makes only of these when the ivar has a C++ class type,
827 // so the form is pretty constrained.
829 // If the property has a reference type, we might just be binding a
830 // reference, in which case the result will be a gl-value. We should
831 // treat this as a non-trivial operation.
832 if (getter->isGLValue())
835 // If we selected a trivial copy-constructor, we're okay.
836 if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
837 return (construct->getConstructor()->isTrivial());
839 // The constructor might require cleanups (in which case it's never
841 assert(isa<ExprWithCleanups>(getter));
845 /// emitCPPObjectAtomicGetterCall - Call the runtime function to
846 /// copy the ivar into the resturn slot.
847 static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF,
848 llvm::Value *returnAddr,
850 llvm::Constant *AtomicHelperFn) {
851 // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
855 // The 1st argument is the return Slot.
856 args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
858 // The 2nd argument is the address of the ivar.
859 llvm::Value *ivarAddr =
860 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
861 CGF.LoadObjCSelf(), ivar, 0).getPointer();
862 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
863 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
865 // Third argument is the helper function.
866 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
868 llvm::Constant *copyCppAtomicObjectFn =
869 CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction();
870 CGCallee callee = CGCallee::forDirect(copyCppAtomicObjectFn);
872 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
873 callee, ReturnValueSlot(), args);
877 CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
878 const ObjCPropertyImplDecl *propImpl,
879 const ObjCMethodDecl *GetterMethodDecl,
880 llvm::Constant *AtomicHelperFn) {
881 // If there's a non-trivial 'get' expression, we just have to emit that.
882 if (!hasTrivialGetExpr(propImpl)) {
883 if (!AtomicHelperFn) {
884 ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(),
889 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
890 emitCPPObjectAtomicGetterCall(*this, ReturnValue.getPointer(),
891 ivar, AtomicHelperFn);
896 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
897 QualType propType = prop->getType();
898 ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl();
900 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
902 // Pick an implementation strategy.
903 PropertyImplStrategy strategy(CGM, propImpl);
904 switch (strategy.getKind()) {
905 case PropertyImplStrategy::Native: {
906 // We don't need to do anything for a zero-size struct.
907 if (strategy.getIvarSize().isZero())
910 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
912 // Currently, all atomic accesses have to be through integer
913 // types, so there's no point in trying to pick a prettier type.
914 uint64_t ivarSize = getContext().toBits(strategy.getIvarSize());
915 llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize);
916 bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
918 // Perform an atomic load. This does not impose ordering constraints.
919 Address ivarAddr = LV.getAddress();
920 ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
921 llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
922 load->setAtomic(llvm::AtomicOrdering::Unordered);
924 // Store that value into the return address. Doing this with a
925 // bitcast is likely to produce some pretty ugly IR, but it's not
926 // the *most* terrible thing in the world.
927 llvm::Type *retTy = ConvertType(getterMethod->getReturnType());
928 uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy);
929 llvm::Value *ivarVal = load;
930 if (ivarSize > retTySize) {
931 llvm::Type *newTy = llvm::Type::getIntNTy(getLLVMContext(), retTySize);
932 ivarVal = Builder.CreateTrunc(load, newTy);
933 bitcastType = newTy->getPointerTo();
935 Builder.CreateStore(ivarVal,
936 Builder.CreateBitCast(ReturnValue, bitcastType));
938 // Make sure we don't do an autorelease.
939 AutoreleaseResult = false;
943 case PropertyImplStrategy::GetSetProperty: {
944 llvm::Constant *getPropertyFn =
945 CGM.getObjCRuntime().GetPropertyGetFunction();
946 if (!getPropertyFn) {
947 CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
950 CGCallee callee = CGCallee::forDirect(getPropertyFn);
952 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
953 // FIXME: Can't this be simpler? This might even be worse than the
954 // corresponding gcc code.
956 Builder.CreateLoad(GetAddrOfLocalVar(getterMethod->getCmdDecl()), "cmd");
957 llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
958 llvm::Value *ivarOffset =
959 EmitIvarOffset(classImpl->getClassInterface(), ivar);
962 args.add(RValue::get(self), getContext().getObjCIdType());
963 args.add(RValue::get(cmd), getContext().getObjCSelType());
964 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
965 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
966 getContext().BoolTy);
968 // FIXME: We shouldn't need to get the function info here, the
969 // runtime already should have computed it to build the function.
970 llvm::Instruction *CallInstruction;
971 RValue RV = EmitCall(
972 getTypes().arrangeBuiltinFunctionCall(propType, args),
973 callee, ReturnValueSlot(), args, &CallInstruction);
974 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction))
977 // We need to fix the type here. Ivars with copy & retain are
978 // always objects so we don't need to worry about complex or
980 RV = RValue::get(Builder.CreateBitCast(
982 getTypes().ConvertType(getterMethod->getReturnType())));
984 EmitReturnOfRValue(RV, propType);
986 // objc_getProperty does an autorelease, so we should suppress ours.
987 AutoreleaseResult = false;
992 case PropertyImplStrategy::CopyStruct:
993 emitStructGetterCall(*this, ivar, strategy.isAtomic(),
994 strategy.hasStrongMember());
997 case PropertyImplStrategy::Expression:
998 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
999 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
1001 QualType ivarType = ivar->getType();
1002 switch (getEvaluationKind(ivarType)) {
1004 ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation());
1005 EmitStoreOfComplex(pair, MakeAddrLValue(ReturnValue, ivarType),
1010 // The return value slot is guaranteed to not be aliased, but
1011 // that's not necessarily the same as "on the stack", so
1012 // we still potentially need objc_memmove_collectable.
1013 EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType);
1017 if (propType->isReferenceType()) {
1018 value = LV.getAddress().getPointer();
1020 // We want to load and autoreleaseReturnValue ARC __weak ivars.
1021 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
1022 if (getLangOpts().ObjCAutoRefCount) {
1023 value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
1025 value = EmitARCLoadWeak(LV.getAddress());
1028 // Otherwise we want to do a simple load, suppressing the
1029 // final autorelease.
1031 value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal();
1032 AutoreleaseResult = false;
1035 value = Builder.CreateBitCast(
1036 value, ConvertType(GetterMethodDecl->getReturnType()));
1039 EmitReturnOfRValue(RValue::get(value), propType);
1043 llvm_unreachable("bad evaluation kind");
1047 llvm_unreachable("bad @property implementation strategy!");
1050 /// emitStructSetterCall - Call the runtime function to store the value
1051 /// from the first formal parameter into the given ivar.
1052 static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
1053 ObjCIvarDecl *ivar) {
1054 // objc_copyStruct (&structIvar, &Arg,
1055 // sizeof (struct something), true, false);
1058 // The first argument is the address of the ivar.
1059 llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
1060 CGF.LoadObjCSelf(), ivar, 0)
1062 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1063 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1065 // The second argument is the address of the parameter variable.
1066 ParmVarDecl *argVar = *OMD->param_begin();
1067 DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
1068 VK_LValue, SourceLocation());
1069 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer();
1070 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1071 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1073 // The third argument is the sizeof the type.
1075 CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
1076 args.add(RValue::get(size), CGF.getContext().getSizeType());
1078 // The fourth argument is the 'isAtomic' flag.
1079 args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
1081 // The fifth argument is the 'hasStrong' flag.
1082 // FIXME: should this really always be false?
1083 args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
1085 llvm::Constant *fn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
1086 CGCallee callee = CGCallee::forDirect(fn);
1088 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1089 callee, ReturnValueSlot(), args);
1092 /// emitCPPObjectAtomicSetterCall - Call the runtime function to store
1093 /// the value from the first formal parameter into the given ivar, using
1094 /// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
1095 static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF,
1096 ObjCMethodDecl *OMD,
1098 llvm::Constant *AtomicHelperFn) {
1099 // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg,
1103 // The first argument is the address of the ivar.
1104 llvm::Value *ivarAddr =
1105 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
1106 CGF.LoadObjCSelf(), ivar, 0).getPointer();
1107 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1108 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1110 // The second argument is the address of the parameter variable.
1111 ParmVarDecl *argVar = *OMD->param_begin();
1112 DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
1113 VK_LValue, SourceLocation());
1114 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer();
1115 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1116 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1118 // Third argument is the helper function.
1119 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1121 llvm::Constant *fn =
1122 CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction();
1123 CGCallee callee = CGCallee::forDirect(fn);
1125 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1126 callee, ReturnValueSlot(), args);
1130 static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
1131 Expr *setter = PID->getSetterCXXAssignment();
1132 if (!setter) return true;
1134 // Sema only makes only of these when the ivar has a C++ class type,
1135 // so the form is pretty constrained.
1137 // An operator call is trivial if the function it calls is trivial.
1138 // This also implies that there's nothing non-trivial going on with
1139 // the arguments, because operator= can only be trivial if it's a
1140 // synthesized assignment operator and therefore both parameters are
1142 if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
1143 if (const FunctionDecl *callee
1144 = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
1145 if (callee->isTrivial())
1150 assert(isa<ExprWithCleanups>(setter));
1154 static bool UseOptimizedSetter(CodeGenModule &CGM) {
1155 if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
1157 return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter();
1161 CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1162 const ObjCPropertyImplDecl *propImpl,
1163 llvm::Constant *AtomicHelperFn) {
1164 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
1165 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1166 ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl();
1168 // Just use the setter expression if Sema gave us one and it's
1170 if (!hasTrivialSetExpr(propImpl)) {
1171 if (!AtomicHelperFn)
1172 // If non-atomic, assignment is called directly.
1173 EmitStmt(propImpl->getSetterCXXAssignment());
1175 // If atomic, assignment is called via a locking api.
1176 emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
1181 PropertyImplStrategy strategy(CGM, propImpl);
1182 switch (strategy.getKind()) {
1183 case PropertyImplStrategy::Native: {
1184 // We don't need to do anything for a zero-size struct.
1185 if (strategy.getIvarSize().isZero())
1188 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1191 EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
1192 Address ivarAddr = ivarLValue.getAddress();
1194 // Currently, all atomic accesses have to be through integer
1195 // types, so there's no point in trying to pick a prettier type.
1196 llvm::Type *bitcastType =
1197 llvm::Type::getIntNTy(getLLVMContext(),
1198 getContext().toBits(strategy.getIvarSize()));
1200 // Cast both arguments to the chosen operation type.
1201 argAddr = Builder.CreateElementBitCast(argAddr, bitcastType);
1202 ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType);
1204 // This bitcast load is likely to cause some nasty IR.
1205 llvm::Value *load = Builder.CreateLoad(argAddr);
1207 // Perform an atomic store. There are no memory ordering requirements.
1208 llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
1209 store->setAtomic(llvm::AtomicOrdering::Unordered);
1213 case PropertyImplStrategy::GetSetProperty:
1214 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1216 llvm::Constant *setOptimizedPropertyFn = nullptr;
1217 llvm::Constant *setPropertyFn = nullptr;
1218 if (UseOptimizedSetter(CGM)) {
1219 // 10.8 and iOS 6.0 code and GC is off
1220 setOptimizedPropertyFn =
1221 CGM.getObjCRuntime()
1222 .GetOptimizedPropertySetFunction(strategy.isAtomic(),
1224 if (!setOptimizedPropertyFn) {
1225 CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
1230 setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
1231 if (!setPropertyFn) {
1232 CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
1237 // Emit objc_setProperty((id) self, _cmd, offset, arg,
1238 // <is-atomic>, <is-copy>).
1240 Builder.CreateLoad(GetAddrOfLocalVar(setterMethod->getCmdDecl()));
1242 Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1243 llvm::Value *ivarOffset =
1244 EmitIvarOffset(classImpl->getClassInterface(), ivar);
1245 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1246 llvm::Value *arg = Builder.CreateLoad(argAddr, "arg");
1247 arg = Builder.CreateBitCast(arg, VoidPtrTy);
1250 args.add(RValue::get(self), getContext().getObjCIdType());
1251 args.add(RValue::get(cmd), getContext().getObjCSelType());
1252 if (setOptimizedPropertyFn) {
1253 args.add(RValue::get(arg), getContext().getObjCIdType());
1254 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1255 CGCallee callee = CGCallee::forDirect(setOptimizedPropertyFn);
1256 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1257 callee, ReturnValueSlot(), args);
1259 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1260 args.add(RValue::get(arg), getContext().getObjCIdType());
1261 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1262 getContext().BoolTy);
1263 args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
1264 getContext().BoolTy);
1265 // FIXME: We shouldn't need to get the function info here, the runtime
1266 // already should have computed it to build the function.
1267 CGCallee callee = CGCallee::forDirect(setPropertyFn);
1268 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1269 callee, ReturnValueSlot(), args);
1275 case PropertyImplStrategy::CopyStruct:
1276 emitStructSetterCall(*this, setterMethod, ivar);
1279 case PropertyImplStrategy::Expression:
1283 // Otherwise, fake up some ASTs and emit a normal assignment.
1284 ValueDecl *selfDecl = setterMethod->getSelfDecl();
1285 DeclRefExpr self(selfDecl, false, selfDecl->getType(),
1286 VK_LValue, SourceLocation());
1287 ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
1288 selfDecl->getType(), CK_LValueToRValue, &self,
1290 ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
1291 SourceLocation(), SourceLocation(),
1292 &selfLoad, true, true);
1294 ParmVarDecl *argDecl = *setterMethod->param_begin();
1295 QualType argType = argDecl->getType().getNonReferenceType();
1296 DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation());
1297 ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
1298 argType.getUnqualifiedType(), CK_LValueToRValue,
1301 // The property type can differ from the ivar type in some situations with
1302 // Objective-C pointer types, we can always bit cast the RHS in these cases.
1303 // The following absurdity is just to ensure well-formed IR.
1304 CastKind argCK = CK_NoOp;
1305 if (ivarRef.getType()->isObjCObjectPointerType()) {
1306 if (argLoad.getType()->isObjCObjectPointerType())
1308 else if (argLoad.getType()->isBlockPointerType())
1309 argCK = CK_BlockPointerToObjCPointerCast;
1311 argCK = CK_CPointerToObjCPointerCast;
1312 } else if (ivarRef.getType()->isBlockPointerType()) {
1313 if (argLoad.getType()->isBlockPointerType())
1316 argCK = CK_AnyPointerToBlockPointerCast;
1317 } else if (ivarRef.getType()->isPointerType()) {
1320 ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
1321 ivarRef.getType(), argCK, &argLoad,
1323 Expr *finalArg = &argLoad;
1324 if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
1326 finalArg = &argCast;
1329 BinaryOperator assign(&ivarRef, finalArg, BO_Assign,
1330 ivarRef.getType(), VK_RValue, OK_Ordinary,
1331 SourceLocation(), FPOptions());
1335 /// \brief Generate an Objective-C property setter function.
1337 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
1338 /// is illegal within a category.
1339 void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
1340 const ObjCPropertyImplDecl *PID) {
1341 llvm::Constant *AtomicHelperFn =
1342 CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID);
1343 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1344 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
1345 assert(OMD && "Invalid call to generate setter (empty method)");
1346 StartObjCMethod(OMD, IMP->getClassInterface());
1348 generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1354 struct DestroyIvar final : EHScopeStack::Cleanup {
1357 const ObjCIvarDecl *ivar;
1358 CodeGenFunction::Destroyer *destroyer;
1359 bool useEHCleanupForArray;
1361 DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
1362 CodeGenFunction::Destroyer *destroyer,
1363 bool useEHCleanupForArray)
1364 : addr(addr), ivar(ivar), destroyer(destroyer),
1365 useEHCleanupForArray(useEHCleanupForArray) {}
1367 void Emit(CodeGenFunction &CGF, Flags flags) override {
1369 = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
1370 CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
1371 flags.isForNormalCleanup() && useEHCleanupForArray);
1376 /// Like CodeGenFunction::destroyARCStrong, but do it with a call.
1377 static void destroyARCStrongWithStore(CodeGenFunction &CGF,
1380 llvm::Value *null = getNullForVariable(addr);
1381 CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
1384 static void emitCXXDestructMethod(CodeGenFunction &CGF,
1385 ObjCImplementationDecl *impl) {
1386 CodeGenFunction::RunCleanupsScope scope(CGF);
1388 llvm::Value *self = CGF.LoadObjCSelf();
1390 const ObjCInterfaceDecl *iface = impl->getClassInterface();
1391 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1392 ivar; ivar = ivar->getNextIvar()) {
1393 QualType type = ivar->getType();
1395 // Check whether the ivar is a destructible type.
1396 QualType::DestructionKind dtorKind = type.isDestructedType();
1397 if (!dtorKind) continue;
1399 CodeGenFunction::Destroyer *destroyer = nullptr;
1401 // Use a call to objc_storeStrong to destroy strong ivars, for the
1402 // general benefit of the tools.
1403 if (dtorKind == QualType::DK_objc_strong_lifetime) {
1404 destroyer = destroyARCStrongWithStore;
1406 // Otherwise use the default for the destruction kind.
1408 destroyer = CGF.getDestroyer(dtorKind);
1411 CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
1413 CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
1414 cleanupKind & EHCleanup);
1417 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1420 void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1423 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
1424 StartObjCMethod(MD, IMP->getClassInterface());
1426 // Emit .cxx_construct.
1428 // Suppress the final autorelease in ARC.
1429 AutoreleaseResult = false;
1431 for (const auto *IvarInit : IMP->inits()) {
1432 FieldDecl *Field = IvarInit->getAnyMember();
1433 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1434 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
1435 LoadObjCSelf(), Ivar, 0);
1436 EmitAggExpr(IvarInit->getInit(),
1437 AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
1438 AggValueSlot::DoesNotNeedGCBarriers,
1439 AggValueSlot::IsNotAliased));
1441 // constructor returns 'self'.
1442 CodeGenTypes &Types = CGM.getTypes();
1443 QualType IdTy(CGM.getContext().getObjCIdType());
1444 llvm::Value *SelfAsId =
1445 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1446 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1448 // Emit .cxx_destruct.
1450 emitCXXDestructMethod(*this, IMP);
1455 llvm::Value *CodeGenFunction::LoadObjCSelf() {
1456 VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
1457 DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
1458 Self->getType(), VK_LValue, SourceLocation());
1459 return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation());
1462 QualType CodeGenFunction::TypeOfSelfObject() {
1463 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1464 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
1465 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
1466 getContext().getCanonicalType(selfDecl->getType()));
1467 return PTy->getPointeeType();
1470 void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
1471 llvm::Constant *EnumerationMutationFnPtr =
1472 CGM.getObjCRuntime().EnumerationMutationFunction();
1473 if (!EnumerationMutationFnPtr) {
1474 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1477 CGCallee EnumerationMutationFn =
1478 CGCallee::forDirect(EnumerationMutationFnPtr);
1480 CGDebugInfo *DI = getDebugInfo();
1482 DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1484 RunCleanupsScope ForScope(*this);
1486 // The local variable comes into scope immediately.
1487 AutoVarEmission variable = AutoVarEmission::invalid();
1488 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
1489 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
1491 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
1493 // Fast enumeration state.
1494 QualType StateTy = CGM.getObjCFastEnumerationStateType();
1495 Address StatePtr = CreateMemTemp(StateTy, "state.ptr");
1496 EmitNullInitialization(StatePtr, StateTy);
1498 // Number of elements in the items array.
1499 static const unsigned NumItems = 16;
1501 // Fetch the countByEnumeratingWithState:objects:count: selector.
1502 IdentifierInfo *II[] = {
1503 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1504 &CGM.getContext().Idents.get("objects"),
1505 &CGM.getContext().Idents.get("count")
1507 Selector FastEnumSel =
1508 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1511 getContext().getConstantArrayType(getContext().getObjCIdType(),
1512 llvm::APInt(32, NumItems),
1513 ArrayType::Normal, 0);
1514 Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
1516 // Emit the collection pointer. In ARC, we do a retain.
1517 llvm::Value *Collection;
1518 if (getLangOpts().ObjCAutoRefCount) {
1519 Collection = EmitARCRetainScalarExpr(S.getCollection());
1521 // Enter a cleanup to do the release.
1522 EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1524 Collection = EmitScalarExpr(S.getCollection());
1527 // The 'continue' label needs to appear within the cleanup for the
1528 // collection object.
1529 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
1531 // Send it our message:
1534 // The first argument is a temporary of the enumeration-state type.
1535 Args.add(RValue::get(StatePtr.getPointer()),
1536 getContext().getPointerType(StateTy));
1538 // The second argument is a temporary array with space for NumItems
1539 // pointers. We'll actually be loading elements from the array
1540 // pointer written into the control state; this buffer is so that
1541 // collections that *aren't* backed by arrays can still queue up
1542 // batches of elements.
1543 Args.add(RValue::get(ItemsPtr.getPointer()),
1544 getContext().getPointerType(ItemsTy));
1546 // The third argument is the capacity of that temporary array.
1547 llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
1548 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
1549 Args.add(RValue::get(Count), getContext().UnsignedLongTy);
1551 // Start the enumeration.
1553 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1554 getContext().UnsignedLongTy,
1558 // The initial number of objects that were returned in the buffer.
1559 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
1561 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1562 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1564 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
1566 // If the limit pointer was zero to begin with, the collection is
1567 // empty; skip all this. Set the branch weight assuming this has the same
1568 // probability of exiting the loop as any other loop exit.
1569 uint64_t EntryCount = getCurrentProfileCount();
1570 Builder.CreateCondBr(
1571 Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB,
1573 createProfileWeights(EntryCount, getProfileCount(S.getBody())));
1575 // Otherwise, initialize the loop.
1576 EmitBlock(LoopInitBB);
1578 // Save the initial mutations value. This is the value at an
1579 // address that was written into the state object by
1580 // countByEnumeratingWithState:objects:count:.
1581 Address StateMutationsPtrPtr = Builder.CreateStructGEP(
1582 StatePtr, 2, 2 * getPointerSize(), "mutationsptr.ptr");
1583 llvm::Value *StateMutationsPtr
1584 = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1586 llvm::Value *initialMutations =
1587 Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(),
1588 "forcoll.initial-mutations");
1590 // Start looping. This is the point we return to whenever we have a
1591 // fresh, non-empty batch of objects.
1592 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1593 EmitBlock(LoopBodyBB);
1595 // The current index into the buffer.
1596 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
1597 index->addIncoming(zero, LoopInitBB);
1599 // The current buffer size.
1600 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
1601 count->addIncoming(initialBufferLimit, LoopInitBB);
1603 incrementProfileCounter(&S);
1605 // Check whether the mutations value has changed from where it was
1606 // at start. StateMutationsPtr should actually be invariant between
1608 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1609 llvm::Value *currentMutations
1610 = Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(),
1613 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1614 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
1616 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1617 WasNotMutatedBB, WasMutatedBB);
1619 // If so, call the enumeration-mutation function.
1620 EmitBlock(WasMutatedBB);
1622 Builder.CreateBitCast(Collection,
1623 ConvertType(getContext().getObjCIdType()));
1625 Args2.add(RValue::get(V), getContext().getObjCIdType());
1626 // FIXME: We shouldn't need to get the function info here, the runtime already
1627 // should have computed it to build the function.
1629 CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2),
1630 EnumerationMutationFn, ReturnValueSlot(), Args2);
1632 // Otherwise, or if the mutation function returns, just continue.
1633 EmitBlock(WasNotMutatedBB);
1635 // Initialize the element variable.
1636 RunCleanupsScope elementVariableScope(*this);
1637 bool elementIsVariable;
1638 LValue elementLValue;
1639 QualType elementType;
1640 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
1641 // Initialize the variable, in case it's a __block variable or something.
1642 EmitAutoVarInit(variable);
1644 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
1645 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(),
1646 VK_LValue, SourceLocation());
1647 elementLValue = EmitLValue(&tempDRE);
1648 elementType = D->getType();
1649 elementIsVariable = true;
1651 if (D->isARCPseudoStrong())
1652 elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1654 elementLValue = LValue(); // suppress warning
1655 elementType = cast<Expr>(S.getElement())->getType();
1656 elementIsVariable = false;
1658 llvm::Type *convertedElementType = ConvertType(elementType);
1660 // Fetch the buffer out of the enumeration state.
1661 // TODO: this pointer should actually be invariant between
1662 // refreshes, which would help us do certain loop optimizations.
1663 Address StateItemsPtr = Builder.CreateStructGEP(
1664 StatePtr, 1, getPointerSize(), "stateitems.ptr");
1665 llvm::Value *EnumStateItems =
1666 Builder.CreateLoad(StateItemsPtr, "stateitems");
1668 // Fetch the value at the current index from the buffer.
1669 llvm::Value *CurrentItemPtr =
1670 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
1671 llvm::Value *CurrentItem =
1672 Builder.CreateAlignedLoad(CurrentItemPtr, getPointerAlign());
1674 // Cast that value to the right type.
1675 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1678 // Make sure we have an l-value. Yes, this gets evaluated every
1679 // time through the loop.
1680 if (!elementIsVariable) {
1681 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1682 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
1684 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue,
1688 // If we do have an element variable, this assignment is the end of
1689 // its initialization.
1690 if (elementIsVariable)
1691 EmitAutoVarCleanups(variable);
1693 // Perform the loop body, setting up break and continue labels.
1694 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1696 RunCleanupsScope Scope(*this);
1697 EmitStmt(S.getBody());
1699 BreakContinueStack.pop_back();
1701 // Destroy the element variable now.
1702 elementVariableScope.ForceCleanup();
1704 // Check whether there are more elements.
1705 EmitBlock(AfterBody.getBlock());
1707 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1709 // First we check in the local buffer.
1710 llvm::Value *indexPlusOne
1711 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
1713 // If we haven't overrun the buffer yet, we can continue.
1714 // Set the branch weights based on the simplifying assumption that this is
1715 // like a while-loop, i.e., ignoring that the false branch fetches more
1716 // elements and then returns to the loop.
1717 Builder.CreateCondBr(
1718 Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB,
1719 createProfileWeights(getProfileCount(S.getBody()), EntryCount));
1721 index->addIncoming(indexPlusOne, AfterBody.getBlock());
1722 count->addIncoming(count, AfterBody.getBlock());
1724 // Otherwise, we have to fetch more elements.
1725 EmitBlock(FetchMoreBB);
1728 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1729 getContext().UnsignedLongTy,
1733 // If we got a zero count, we're done.
1734 llvm::Value *refetchCount = CountRV.getScalarVal();
1736 // (note that the message send might split FetchMoreBB)
1737 index->addIncoming(zero, Builder.GetInsertBlock());
1738 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1740 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1741 EmptyBB, LoopBodyBB);
1743 // No more elements.
1746 if (!elementIsVariable) {
1747 // If the element was not a declaration, set it to be null.
1749 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1750 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1751 EmitStoreThroughLValue(RValue::get(null), elementLValue);
1755 DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
1757 ForScope.ForceCleanup();
1758 EmitBlock(LoopEnd.getBlock());
1761 void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1762 CGM.getObjCRuntime().EmitTryStmt(*this, S);
1765 void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
1766 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1769 void CodeGenFunction::EmitObjCAtSynchronizedStmt(
1770 const ObjCAtSynchronizedStmt &S) {
1771 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
1775 struct CallObjCRelease final : EHScopeStack::Cleanup {
1776 CallObjCRelease(llvm::Value *object) : object(object) {}
1777 llvm::Value *object;
1779 void Emit(CodeGenFunction &CGF, Flags flags) override {
1780 // Releases at the end of the full-expression are imprecise.
1781 CGF.EmitARCRelease(object, ARCImpreciseLifetime);
1786 /// Produce the code for a CK_ARCConsumeObject. Does a primitive
1787 /// release at the end of the full-expression.
1788 llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
1789 llvm::Value *object) {
1790 // If we're in a conditional branch, we need to make the cleanup
1792 pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
1796 llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
1797 llvm::Value *value) {
1798 return EmitARCRetainAutorelease(type, value);
1801 /// Given a number of pointers, inform the optimizer that they're
1802 /// being intrinsically used up until this point in the program.
1803 void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
1804 llvm::Constant *&fn = CGM.getObjCEntrypoints().clang_arc_use;
1806 llvm::FunctionType *fnType =
1807 llvm::FunctionType::get(CGM.VoidTy, None, true);
1808 fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use");
1811 // This isn't really a "runtime" function, but as an intrinsic it
1812 // doesn't really matter as long as we align things up.
1813 EmitNounwindRuntimeCall(fn, values);
1817 static bool IsForwarding(StringRef Name) {
1818 return llvm::StringSwitch<bool>(Name)
1819 .Cases("objc_autoreleaseReturnValue", // ARCInstKind::AutoreleaseRV
1820 "objc_autorelease", // ARCInstKind::Autorelease
1821 "objc_retainAutoreleaseReturnValue", // ARCInstKind::FusedRetainAutoreleaseRV
1822 "objc_retainAutoreleasedReturnValue", // ARCInstKind::RetainRV
1823 "objc_retainAutorelease", // ARCInstKind::FusedRetainAutorelease
1824 "objc_retainedObject", // ARCInstKind::NoopCast
1825 "objc_retain", // ARCInstKind::Retain
1826 "objc_unretainedObject", // ARCInstKind::NoopCast
1827 "objc_unretainedPointer", // ARCInstKind::NoopCast
1828 "objc_unsafeClaimAutoreleasedReturnValue", // ARCInstKind::ClaimRV
1833 static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
1834 llvm::FunctionType *FTy,
1836 llvm::Constant *RTF = CGM.CreateRuntimeFunction(FTy, Name);
1838 if (auto *F = dyn_cast<llvm::Function>(RTF)) {
1839 // If the target runtime doesn't naturally support ARC, emit weak
1840 // references to the runtime support library. We don't really
1841 // permit this to fail, but we need a particular relocation style.
1842 if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() &&
1843 !CGM.getTriple().isOSBinFormatCOFF()) {
1844 F->setLinkage(llvm::Function::ExternalWeakLinkage);
1845 } else if (Name == "objc_retain" || Name == "objc_release") {
1846 // If we have Native ARC, set nonlazybind attribute for these APIs for
1848 F->addFnAttr(llvm::Attribute::NonLazyBind);
1851 if (IsForwarding(Name)) {
1852 llvm::AttrBuilder B;
1853 B.addAttribute(llvm::Attribute::Returned);
1855 F->arg_begin()->addAttr(llvm::AttributeList::get(F->getContext(), 1, B));
1862 /// Perform an operation having the signature
1864 /// where a null input causes a no-op and returns null.
1865 static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
1867 llvm::Constant *&fn,
1869 bool isTailCall = false) {
1870 if (isa<llvm::ConstantPointerNull>(value))
1874 llvm::FunctionType *fnType =
1875 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
1876 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1879 // Cast the argument to 'id'.
1880 llvm::Type *origType = value->getType();
1881 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
1883 // Call the function.
1884 llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
1886 call->setTailCall();
1888 // Cast the result back to the original type.
1889 return CGF.Builder.CreateBitCast(call, origType);
1892 /// Perform an operation having the following signature:
1894 static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
1896 llvm::Constant *&fn,
1899 llvm::FunctionType *fnType =
1900 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false);
1901 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1904 // Cast the argument to 'id*'.
1905 llvm::Type *origType = addr.getElementType();
1906 addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
1908 // Call the function.
1909 llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer());
1911 // Cast the result back to a dereference of the original type.
1912 if (origType != CGF.Int8PtrTy)
1913 result = CGF.Builder.CreateBitCast(result, origType);
1918 /// Perform an operation having the following signature:
1920 static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
1923 llvm::Constant *&fn,
1926 assert(addr.getElementType() == value->getType());
1929 llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy };
1931 llvm::FunctionType *fnType
1932 = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
1933 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1936 llvm::Type *origType = value->getType();
1938 llvm::Value *args[] = {
1939 CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy),
1940 CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
1942 llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
1944 if (ignored) return nullptr;
1946 return CGF.Builder.CreateBitCast(result, origType);
1949 /// Perform an operation having the following signature:
1950 /// void (i8**, i8**)
1951 static void emitARCCopyOperation(CodeGenFunction &CGF,
1954 llvm::Constant *&fn,
1956 assert(dst.getType() == src.getType());
1959 llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy };
1961 llvm::FunctionType *fnType
1962 = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
1963 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1966 llvm::Value *args[] = {
1967 CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy),
1968 CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy)
1970 CGF.EmitNounwindRuntimeCall(fn, args);
1973 /// Produce the code to do a retain. Based on the type, calls one of:
1974 /// call i8* \@objc_retain(i8* %value)
1975 /// call i8* \@objc_retainBlock(i8* %value)
1976 llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
1977 if (type->isBlockPointerType())
1978 return EmitARCRetainBlock(value, /*mandatory*/ false);
1980 return EmitARCRetainNonBlock(value);
1983 /// Retain the given object, with normal retain semantics.
1984 /// call i8* \@objc_retain(i8* %value)
1985 llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
1986 return emitARCValueOperation(*this, value,
1987 CGM.getObjCEntrypoints().objc_retain,
1991 /// Retain the given block, with _Block_copy semantics.
1992 /// call i8* \@objc_retainBlock(i8* %value)
1994 /// \param mandatory - If false, emit the call with metadata
1995 /// indicating that it's okay for the optimizer to eliminate this call
1996 /// if it can prove that the block never escapes except down the stack.
1997 llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
2000 = emitARCValueOperation(*this, value,
2001 CGM.getObjCEntrypoints().objc_retainBlock,
2002 "objc_retainBlock");
2004 // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
2005 // tell the optimizer that it doesn't need to do this copy if the
2006 // block doesn't escape, where being passed as an argument doesn't
2007 // count as escaping.
2008 if (!mandatory && isa<llvm::Instruction>(result)) {
2009 llvm::CallInst *call
2010 = cast<llvm::CallInst>(result->stripPointerCasts());
2011 assert(call->getCalledValue() == CGM.getObjCEntrypoints().objc_retainBlock);
2013 call->setMetadata("clang.arc.copy_on_escape",
2014 llvm::MDNode::get(Builder.getContext(), None));
2020 static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) {
2021 // Fetch the void(void) inline asm which marks that we're going to
2022 // do something with the autoreleased return value.
2023 llvm::InlineAsm *&marker
2024 = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker;
2027 = CGF.CGM.getTargetCodeGenInfo()
2028 .getARCRetainAutoreleasedReturnValueMarker();
2030 // If we have an empty assembly string, there's nothing to do.
2031 if (assembly.empty()) {
2033 // Otherwise, at -O0, build an inline asm that we're going to call
2035 } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
2036 llvm::FunctionType *type =
2037 llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false);
2039 marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
2041 // If we're at -O1 and above, we don't want to litter the code
2042 // with this marker yet, so leave a breadcrumb for the ARC
2043 // optimizer to pick up.
2045 llvm::NamedMDNode *metadata =
2046 CGF.CGM.getModule().getOrInsertNamedMetadata(
2047 "clang.arc.retainAutoreleasedReturnValueMarker");
2048 assert(metadata->getNumOperands() <= 1);
2049 if (metadata->getNumOperands() == 0) {
2050 auto &ctx = CGF.getLLVMContext();
2051 metadata->addOperand(llvm::MDNode::get(ctx,
2052 llvm::MDString::get(ctx, assembly)));
2057 // Call the marker asm if we made one, which we do only at -O0.
2059 CGF.Builder.CreateCall(marker);
2062 /// Retain the given object which is the result of a function call.
2063 /// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
2065 /// Yes, this function name is one character away from a different
2066 /// call with completely different semantics.
2068 CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
2069 emitAutoreleasedReturnValueMarker(*this);
2070 return emitARCValueOperation(*this, value,
2071 CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue,
2072 "objc_retainAutoreleasedReturnValue");
2075 /// Claim a possibly-autoreleased return value at +0. This is only
2076 /// valid to do in contexts which do not rely on the retain to keep
2077 /// the object valid for for all of its uses; for example, when
2078 /// the value is ignored, or when it is being assigned to an
2079 /// __unsafe_unretained variable.
2081 /// call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value)
2083 CodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) {
2084 emitAutoreleasedReturnValueMarker(*this);
2085 return emitARCValueOperation(*this, value,
2086 CGM.getObjCEntrypoints().objc_unsafeClaimAutoreleasedReturnValue,
2087 "objc_unsafeClaimAutoreleasedReturnValue");
2090 /// Release the given object.
2091 /// call void \@objc_release(i8* %value)
2092 void CodeGenFunction::EmitARCRelease(llvm::Value *value,
2093 ARCPreciseLifetime_t precise) {
2094 if (isa<llvm::ConstantPointerNull>(value)) return;
2096 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_release;
2098 llvm::FunctionType *fnType =
2099 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2100 fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
2103 // Cast the argument to 'id'.
2104 value = Builder.CreateBitCast(value, Int8PtrTy);
2106 // Call objc_release.
2107 llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2109 if (precise == ARCImpreciseLifetime) {
2110 call->setMetadata("clang.imprecise_release",
2111 llvm::MDNode::get(Builder.getContext(), None));
2115 /// Destroy a __strong variable.
2117 /// At -O0, emit a call to store 'null' into the address;
2118 /// instrumenting tools prefer this because the address is exposed,
2119 /// but it's relatively cumbersome to optimize.
2121 /// At -O1 and above, just load and call objc_release.
2123 /// call void \@objc_storeStrong(i8** %addr, i8* null)
2124 void CodeGenFunction::EmitARCDestroyStrong(Address addr,
2125 ARCPreciseLifetime_t precise) {
2126 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2127 llvm::Value *null = getNullForVariable(addr);
2128 EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2132 llvm::Value *value = Builder.CreateLoad(addr);
2133 EmitARCRelease(value, precise);
2136 /// Store into a strong object. Always calls this:
2137 /// call void \@objc_storeStrong(i8** %addr, i8* %value)
2138 llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr,
2141 assert(addr.getElementType() == value->getType());
2143 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_storeStrong;
2145 llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
2146 llvm::FunctionType *fnType
2147 = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
2148 fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
2151 llvm::Value *args[] = {
2152 Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy),
2153 Builder.CreateBitCast(value, Int8PtrTy)
2155 EmitNounwindRuntimeCall(fn, args);
2157 if (ignored) return nullptr;
2161 /// Store into a strong object. Sometimes calls this:
2162 /// call void \@objc_storeStrong(i8** %addr, i8* %value)
2163 /// Other times, breaks it down into components.
2164 llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
2165 llvm::Value *newValue,
2167 QualType type = dst.getType();
2168 bool isBlock = type->isBlockPointerType();
2170 // Use a store barrier at -O0 unless this is a block type or the
2171 // lvalue is inadequately aligned.
2172 if (shouldUseFusedARCCalls() &&
2174 (dst.getAlignment().isZero() ||
2175 dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
2176 return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
2179 // Otherwise, split it out.
2181 // Retain the new value.
2182 newValue = EmitARCRetain(type, newValue);
2184 // Read the old value.
2185 llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation());
2187 // Store. We do this before the release so that any deallocs won't
2188 // see the old value.
2189 EmitStoreOfScalar(newValue, dst);
2191 // Finally, release the old value.
2192 EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2197 /// Autorelease the given object.
2198 /// call i8* \@objc_autorelease(i8* %value)
2199 llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2200 return emitARCValueOperation(*this, value,
2201 CGM.getObjCEntrypoints().objc_autorelease,
2202 "objc_autorelease");
2205 /// Autorelease the given object.
2206 /// call i8* \@objc_autoreleaseReturnValue(i8* %value)
2208 CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
2209 return emitARCValueOperation(*this, value,
2210 CGM.getObjCEntrypoints().objc_autoreleaseReturnValue,
2211 "objc_autoreleaseReturnValue",
2212 /*isTailCall*/ true);
2215 /// Do a fused retain/autorelease of the given object.
2216 /// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2218 CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
2219 return emitARCValueOperation(*this, value,
2220 CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue,
2221 "objc_retainAutoreleaseReturnValue",
2222 /*isTailCall*/ true);
2225 /// Do a fused retain/autorelease of the given object.
2226 /// call i8* \@objc_retainAutorelease(i8* %value)
2228 /// %retain = call i8* \@objc_retainBlock(i8* %value)
2229 /// call i8* \@objc_autorelease(i8* %retain)
2230 llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
2231 llvm::Value *value) {
2232 if (!type->isBlockPointerType())
2233 return EmitARCRetainAutoreleaseNonBlock(value);
2235 if (isa<llvm::ConstantPointerNull>(value)) return value;
2237 llvm::Type *origType = value->getType();
2238 value = Builder.CreateBitCast(value, Int8PtrTy);
2239 value = EmitARCRetainBlock(value, /*mandatory*/ true);
2240 value = EmitARCAutorelease(value);
2241 return Builder.CreateBitCast(value, origType);
2244 /// Do a fused retain/autorelease of the given object.
2245 /// call i8* \@objc_retainAutorelease(i8* %value)
2247 CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
2248 return emitARCValueOperation(*this, value,
2249 CGM.getObjCEntrypoints().objc_retainAutorelease,
2250 "objc_retainAutorelease");
2253 /// i8* \@objc_loadWeak(i8** %addr)
2254 /// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2255 llvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) {
2256 return emitARCLoadOperation(*this, addr,
2257 CGM.getObjCEntrypoints().objc_loadWeak,
2261 /// i8* \@objc_loadWeakRetained(i8** %addr)
2262 llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) {
2263 return emitARCLoadOperation(*this, addr,
2264 CGM.getObjCEntrypoints().objc_loadWeakRetained,
2265 "objc_loadWeakRetained");
2268 /// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2270 llvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr,
2273 return emitARCStoreOperation(*this, addr, value,
2274 CGM.getObjCEntrypoints().objc_storeWeak,
2275 "objc_storeWeak", ignored);
2278 /// i8* \@objc_initWeak(i8** %addr, i8* %value)
2279 /// Returns %value. %addr is known to not have a current weak entry.
2280 /// Essentially equivalent to:
2281 /// *addr = nil; objc_storeWeak(addr, value);
2282 void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) {
2283 // If we're initializing to null, just write null to memory; no need
2284 // to get the runtime involved. But don't do this if optimization
2285 // is enabled, because accounting for this would make the optimizer
2286 // much more complicated.
2287 if (isa<llvm::ConstantPointerNull>(value) &&
2288 CGM.getCodeGenOpts().OptimizationLevel == 0) {
2289 Builder.CreateStore(value, addr);
2293 emitARCStoreOperation(*this, addr, value,
2294 CGM.getObjCEntrypoints().objc_initWeak,
2295 "objc_initWeak", /*ignored*/ true);
2298 /// void \@objc_destroyWeak(i8** %addr)
2299 /// Essentially objc_storeWeak(addr, nil).
2300 void CodeGenFunction::EmitARCDestroyWeak(Address addr) {
2301 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_destroyWeak;
2303 llvm::FunctionType *fnType =
2304 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false);
2305 fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
2308 // Cast the argument to 'id*'.
2309 addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
2311 EmitNounwindRuntimeCall(fn, addr.getPointer());
2314 /// void \@objc_moveWeak(i8** %dest, i8** %src)
2315 /// Disregards the current value in %dest. Leaves %src pointing to nothing.
2316 /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2317 void CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) {
2318 emitARCCopyOperation(*this, dst, src,
2319 CGM.getObjCEntrypoints().objc_moveWeak,
2323 /// void \@objc_copyWeak(i8** %dest, i8** %src)
2324 /// Disregards the current value in %dest. Essentially
2325 /// objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2326 void CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) {
2327 emitARCCopyOperation(*this, dst, src,
2328 CGM.getObjCEntrypoints().objc_copyWeak,
2332 /// Produce the code to do a objc_autoreleasepool_push.
2333 /// call i8* \@objc_autoreleasePoolPush(void)
2334 llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
2335 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush;
2337 llvm::FunctionType *fnType =
2338 llvm::FunctionType::get(Int8PtrTy, false);
2339 fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
2342 return EmitNounwindRuntimeCall(fn);
2345 /// Produce the code to do a primitive release.
2346 /// call void \@objc_autoreleasePoolPop(i8* %ptr)
2347 void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
2348 assert(value->getType() == Int8PtrTy);
2350 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop;
2352 llvm::FunctionType *fnType =
2353 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2355 // We don't want to use a weak import here; instead we should not
2356 // fall into this path.
2357 fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
2360 // objc_autoreleasePoolPop can throw.
2361 EmitRuntimeCallOrInvoke(fn, value);
2364 /// Produce the code to do an MRR version objc_autoreleasepool_push.
2365 /// Which is: [[NSAutoreleasePool alloc] init];
2366 /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2367 /// init is declared as: - (id) init; in its NSObject super class.
2369 llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2370 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2371 llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2372 // [NSAutoreleasePool alloc]
2373 IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2374 Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2377 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2378 getContext().getObjCIdType(),
2379 AllocSel, Receiver, Args);
2382 Receiver = AllocRV.getScalarVal();
2383 II = &CGM.getContext().Idents.get("init");
2384 Selector InitSel = getContext().Selectors.getSelector(0, &II);
2386 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2387 getContext().getObjCIdType(),
2388 InitSel, Receiver, Args);
2389 return InitRV.getScalarVal();
2392 /// Produce the code to do a primitive release.
2394 void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2395 IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2396 Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2398 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2399 getContext().VoidTy, DrainSel, Arg, Args);
2402 void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2405 CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
2408 void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2411 CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
2414 void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2417 CGF.EmitARCDestroyWeak(addr);
2421 struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup {
2424 CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2426 void Emit(CodeGenFunction &CGF, Flags flags) override {
2427 CGF.EmitObjCAutoreleasePoolPop(Token);
2430 struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup {
2433 CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2435 void Emit(CodeGenFunction &CGF, Flags flags) override {
2436 CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2441 void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
2442 if (CGM.getLangOpts().ObjCAutoRefCount)
2443 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2445 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2448 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2451 switch (type.getObjCLifetime()) {
2452 case Qualifiers::OCL_None:
2453 case Qualifiers::OCL_ExplicitNone:
2454 case Qualifiers::OCL_Strong:
2455 case Qualifiers::OCL_Autoreleasing:
2456 return TryEmitResult(CGF.EmitLoadOfLValue(lvalue,
2457 SourceLocation()).getScalarVal(),
2460 case Qualifiers::OCL_Weak:
2461 return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
2465 llvm_unreachable("impossible lifetime!");
2468 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2470 e = e->IgnoreParens();
2471 QualType type = e->getType();
2473 // If we're loading retained from a __strong xvalue, we can avoid
2474 // an extra retain/release pair by zeroing out the source of this
2475 // "move" operation.
2476 if (e->isXValue() &&
2477 !type.isConstQualified() &&
2478 type.getObjCLifetime() == Qualifiers::OCL_Strong) {
2480 LValue lv = CGF.EmitLValue(e);
2482 // Load the object pointer.
2483 llvm::Value *result = CGF.EmitLoadOfLValue(lv,
2484 SourceLocation()).getScalarVal();
2486 // Set the source pointer to NULL.
2487 CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
2489 return TryEmitResult(result, true);
2492 // As a very special optimization, in ARC++, if the l-value is the
2493 // result of a non-volatile assignment, do a simple retain of the
2494 // result of the call to objc_storeWeak instead of reloading.
2495 if (CGF.getLangOpts().CPlusPlus &&
2496 !type.isVolatileQualified() &&
2497 type.getObjCLifetime() == Qualifiers::OCL_Weak &&
2498 isa<BinaryOperator>(e) &&
2499 cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
2500 return TryEmitResult(CGF.EmitScalarExpr(e), false);
2502 return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2505 typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2506 llvm::Value *value)>
2509 /// Insert code immediately after a call.
2510 static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF,
2512 ValueTransform doAfterCall,
2513 ValueTransform doFallback) {
2514 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2515 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2517 // Place the retain immediately following the call.
2518 CGF.Builder.SetInsertPoint(call->getParent(),
2519 ++llvm::BasicBlock::iterator(call));
2520 value = doAfterCall(CGF, value);
2522 CGF.Builder.restoreIP(ip);
2524 } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
2525 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2527 // Place the retain at the beginning of the normal destination block.
2528 llvm::BasicBlock *BB = invoke->getNormalDest();
2529 CGF.Builder.SetInsertPoint(BB, BB->begin());
2530 value = doAfterCall(CGF, value);
2532 CGF.Builder.restoreIP(ip);
2535 // Bitcasts can arise because of related-result returns. Rewrite
2537 } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2538 llvm::Value *operand = bitcast->getOperand(0);
2539 operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback);
2540 bitcast->setOperand(0, operand);
2543 // Generic fall-back case.
2545 // Retain using the non-block variant: we never need to do a copy
2546 // of a block that's been returned to us.
2547 return doFallback(CGF, value);
2551 /// Given that the given expression is some sort of call (which does
2552 /// not return retained), emit a retain following it.
2553 static llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF,
2555 llvm::Value *value = CGF.EmitScalarExpr(e);
2556 return emitARCOperationAfterCall(CGF, value,
2557 [](CodeGenFunction &CGF, llvm::Value *value) {
2558 return CGF.EmitARCRetainAutoreleasedReturnValue(value);
2560 [](CodeGenFunction &CGF, llvm::Value *value) {
2561 return CGF.EmitARCRetainNonBlock(value);
2565 /// Given that the given expression is some sort of call (which does
2566 /// not return retained), perform an unsafeClaim following it.
2567 static llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF,
2569 llvm::Value *value = CGF.EmitScalarExpr(e);
2570 return emitARCOperationAfterCall(CGF, value,
2571 [](CodeGenFunction &CGF, llvm::Value *value) {
2572 return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value);
2574 [](CodeGenFunction &CGF, llvm::Value *value) {
2579 llvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E,
2580 bool allowUnsafeClaim) {
2581 if (allowUnsafeClaim &&
2582 CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) {
2583 return emitARCUnsafeClaimCallResult(*this, E);
2585 llvm::Value *value = emitARCRetainCallResult(*this, E);
2586 return EmitObjCConsumeObject(E->getType(), value);
2590 /// Determine whether it might be important to emit a separate
2591 /// objc_retain_block on the result of the given expression, or
2592 /// whether it's okay to just emit it in a +1 context.
2593 static bool shouldEmitSeparateBlockRetain(const Expr *e) {
2594 assert(e->getType()->isBlockPointerType());
2595 e = e->IgnoreParens();
2597 // For future goodness, emit block expressions directly in +1
2598 // contexts if we can.
2599 if (isa<BlockExpr>(e))
2602 if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
2603 switch (cast->getCastKind()) {
2604 // Emitting these operations in +1 contexts is goodness.
2605 case CK_LValueToRValue:
2606 case CK_ARCReclaimReturnedObject:
2607 case CK_ARCConsumeObject:
2608 case CK_ARCProduceObject:
2611 // These operations preserve a block type.
2614 return shouldEmitSeparateBlockRetain(cast->getSubExpr());
2616 // These operations are known to be bad (or haven't been considered).
2617 case CK_AnyPointerToBlockPointerCast:
2627 /// A CRTP base class for emitting expressions of retainable object
2628 /// pointer type in ARC.
2629 template <typename Impl, typename Result> class ARCExprEmitter {
2631 CodeGenFunction &CGF;
2632 Impl &asImpl() { return *static_cast<Impl*>(this); }
2634 ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {}
2637 Result visit(const Expr *e);
2638 Result visitCastExpr(const CastExpr *e);
2639 Result visitPseudoObjectExpr(const PseudoObjectExpr *e);
2640 Result visitBinaryOperator(const BinaryOperator *e);
2641 Result visitBinAssign(const BinaryOperator *e);
2642 Result visitBinAssignUnsafeUnretained(const BinaryOperator *e);
2643 Result visitBinAssignAutoreleasing(const BinaryOperator *e);
2644 Result visitBinAssignWeak(const BinaryOperator *e);
2645 Result visitBinAssignStrong(const BinaryOperator *e);
2647 // Minimal implementation:
2648 // Result visitLValueToRValue(const Expr *e)
2649 // Result visitConsumeObject(const Expr *e)
2650 // Result visitExtendBlockObject(const Expr *e)
2651 // Result visitReclaimReturnedObject(const Expr *e)
2652 // Result visitCall(const Expr *e)
2653 // Result visitExpr(const Expr *e)
2655 // Result emitBitCast(Result result, llvm::Type *resultType)
2656 // llvm::Value *getValueOfResult(Result result)
2660 /// Try to emit a PseudoObjectExpr under special ARC rules.
2662 /// This massively duplicates emitPseudoObjectRValue.
2663 template <typename Impl, typename Result>
2665 ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) {
2666 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
2668 // Find the result expression.
2669 const Expr *resultExpr = E->getResultExpr();
2673 for (PseudoObjectExpr::const_semantics_iterator
2674 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
2675 const Expr *semantic = *i;
2677 // If this semantic expression is an opaque value, bind it
2678 // to the result of its source expression.
2679 if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
2680 typedef CodeGenFunction::OpaqueValueMappingData OVMA;
2683 // If this semantic is the result of the pseudo-object
2684 // expression, try to evaluate the source as +1.
2685 if (ov == resultExpr) {
2686 assert(!OVMA::shouldBindAsLValue(ov));
2687 result = asImpl().visit(ov->getSourceExpr());
2688 opaqueData = OVMA::bind(CGF, ov,
2689 RValue::get(asImpl().getValueOfResult(result)));
2691 // Otherwise, just bind it.
2693 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
2695 opaques.push_back(opaqueData);
2697 // Otherwise, if the expression is the result, evaluate it
2698 // and remember the result.
2699 } else if (semantic == resultExpr) {
2700 result = asImpl().visit(semantic);
2702 // Otherwise, evaluate the expression in an ignored context.
2704 CGF.EmitIgnoredExpr(semantic);
2708 // Unbind all the opaques now.
2709 for (unsigned i = 0, e = opaques.size(); i != e; ++i)
2710 opaques[i].unbind(CGF);
2715 template <typename Impl, typename Result>
2716 Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
2717 switch (e->getCastKind()) {
2719 // No-op casts don't change the type, so we just ignore them.
2721 return asImpl().visit(e->getSubExpr());
2723 // These casts can change the type.
2724 case CK_CPointerToObjCPointerCast:
2725 case CK_BlockPointerToObjCPointerCast:
2726 case CK_AnyPointerToBlockPointerCast:
2728 llvm::Type *resultType = CGF.ConvertType(e->getType());
2729 assert(e->getSubExpr()->getType()->hasPointerRepresentation());
2730 Result result = asImpl().visit(e->getSubExpr());
2731 return asImpl().emitBitCast(result, resultType);
2734 // Handle some casts specially.
2735 case CK_LValueToRValue:
2736 return asImpl().visitLValueToRValue(e->getSubExpr());
2737 case CK_ARCConsumeObject:
2738 return asImpl().visitConsumeObject(e->getSubExpr());
2739 case CK_ARCExtendBlockObject:
2740 return asImpl().visitExtendBlockObject(e->getSubExpr());
2741 case CK_ARCReclaimReturnedObject:
2742 return asImpl().visitReclaimReturnedObject(e->getSubExpr());
2744 // Otherwise, use the default logic.
2746 return asImpl().visitExpr(e);
2750 template <typename Impl, typename Result>
2752 ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
2753 switch (e->getOpcode()) {
2755 CGF.EmitIgnoredExpr(e->getLHS());
2756 CGF.EnsureInsertPoint();
2757 return asImpl().visit(e->getRHS());
2760 return asImpl().visitBinAssign(e);
2763 return asImpl().visitExpr(e);
2767 template <typename Impl, typename Result>
2768 Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
2769 switch (e->getLHS()->getType().getObjCLifetime()) {
2770 case Qualifiers::OCL_ExplicitNone:
2771 return asImpl().visitBinAssignUnsafeUnretained(e);
2773 case Qualifiers::OCL_Weak:
2774 return asImpl().visitBinAssignWeak(e);
2776 case Qualifiers::OCL_Autoreleasing:
2777 return asImpl().visitBinAssignAutoreleasing(e);
2779 case Qualifiers::OCL_Strong:
2780 return asImpl().visitBinAssignStrong(e);
2782 case Qualifiers::OCL_None:
2783 return asImpl().visitExpr(e);
2785 llvm_unreachable("bad ObjC ownership qualifier");
2788 /// The default rule for __unsafe_unretained emits the RHS recursively,
2789 /// stores into the unsafe variable, and propagates the result outward.
2790 template <typename Impl, typename Result>
2791 Result ARCExprEmitter<Impl,Result>::
2792 visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
2793 // Recursively emit the RHS.
2794 // For __block safety, do this before emitting the LHS.
2795 Result result = asImpl().visit(e->getRHS());
2797 // Perform the store.
2799 CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
2800 CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
2806 template <typename Impl, typename Result>
2808 ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) {
2809 return asImpl().visitExpr(e);
2812 template <typename Impl, typename Result>
2814 ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) {
2815 return asImpl().visitExpr(e);
2818 template <typename Impl, typename Result>
2820 ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
2821 return asImpl().visitExpr(e);
2824 /// The general expression-emission logic.
2825 template <typename Impl, typename Result>
2826 Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
2827 // We should *never* see a nested full-expression here, because if
2828 // we fail to emit at +1, our caller must not retain after we close
2829 // out the full-expression. This isn't as important in the unsafe
2831 assert(!isa<ExprWithCleanups>(e));
2833 // Look through parens, __extension__, generic selection, etc.
2834 e = e->IgnoreParens();
2836 // Handle certain kinds of casts.
2837 if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
2838 return asImpl().visitCastExpr(ce);
2840 // Handle the comma operator.
2841 } else if (auto op = dyn_cast<BinaryOperator>(e)) {
2842 return asImpl().visitBinaryOperator(op);
2844 // TODO: handle conditional operators here
2846 // For calls and message sends, use the retained-call logic.
2847 // Delegate inits are a special case in that they're the only
2848 // returns-retained expression that *isn't* surrounded by
2850 } else if (isa<CallExpr>(e) ||
2851 (isa<ObjCMessageExpr>(e) &&
2852 !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
2853 return asImpl().visitCall(e);
2855 // Look through pseudo-object expressions.
2856 } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
2857 return asImpl().visitPseudoObjectExpr(pseudo);
2860 return asImpl().visitExpr(e);
2865 /// An emitter for +1 results.
2866 struct ARCRetainExprEmitter :
2867 public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> {
2869 ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
2871 llvm::Value *getValueOfResult(TryEmitResult result) {
2872 return result.getPointer();
2875 TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) {
2876 llvm::Value *value = result.getPointer();
2877 value = CGF.Builder.CreateBitCast(value, resultType);
2878 result.setPointer(value);
2882 TryEmitResult visitLValueToRValue(const Expr *e) {
2883 return tryEmitARCRetainLoadOfScalar(CGF, e);
2886 /// For consumptions, just emit the subexpression and thus elide
2887 /// the retain/release pair.
2888 TryEmitResult visitConsumeObject(const Expr *e) {
2889 llvm::Value *result = CGF.EmitScalarExpr(e);
2890 return TryEmitResult(result, true);
2893 /// Block extends are net +0. Naively, we could just recurse on
2894 /// the subexpression, but actually we need to ensure that the
2895 /// value is copied as a block, so there's a little filter here.
2896 TryEmitResult visitExtendBlockObject(const Expr *e) {
2897 llvm::Value *result; // will be a +0 value
2899 // If we can't safely assume the sub-expression will produce a
2900 // block-copied value, emit the sub-expression at +0.
2901 if (shouldEmitSeparateBlockRetain(e)) {
2902 result = CGF.EmitScalarExpr(e);
2904 // Otherwise, try to emit the sub-expression at +1 recursively.
2906 TryEmitResult subresult = asImpl().visit(e);
2908 // If that produced a retained value, just use that.
2909 if (subresult.getInt()) {
2913 // Otherwise it's +0.
2914 result = subresult.getPointer();
2917 // Retain the object as a block.
2918 result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
2919 return TryEmitResult(result, true);
2922 /// For reclaims, emit the subexpression as a retained call and
2923 /// skip the consumption.
2924 TryEmitResult visitReclaimReturnedObject(const Expr *e) {
2925 llvm::Value *result = emitARCRetainCallResult(CGF, e);
2926 return TryEmitResult(result, true);
2929 /// When we have an undecorated call, retroactively do a claim.
2930 TryEmitResult visitCall(const Expr *e) {
2931 llvm::Value *result = emitARCRetainCallResult(CGF, e);
2932 return TryEmitResult(result, true);
2935 // TODO: maybe special-case visitBinAssignWeak?
2937 TryEmitResult visitExpr(const Expr *e) {
2938 // We didn't find an obvious production, so emit what we've got and
2939 // tell the caller that we didn't manage to retain.
2940 llvm::Value *result = CGF.EmitScalarExpr(e);
2941 return TryEmitResult(result, false);
2946 static TryEmitResult
2947 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
2948 return ARCRetainExprEmitter(CGF).visit(e);
2951 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2954 TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
2955 llvm::Value *value = result.getPointer();
2956 if (!result.getInt())
2957 value = CGF.EmitARCRetain(type, value);
2961 /// EmitARCRetainScalarExpr - Semantically equivalent to
2962 /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
2963 /// best-effort attempt to peephole expressions that naturally produce
2964 /// retained objects.
2965 llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
2966 // The retain needs to happen within the full-expression.
2967 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2968 enterFullExpression(cleanups);
2969 RunCleanupsScope scope(*this);
2970 return EmitARCRetainScalarExpr(cleanups->getSubExpr());
2973 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2974 llvm::Value *value = result.getPointer();
2975 if (!result.getInt())
2976 value = EmitARCRetain(e->getType(), value);
2981 CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
2982 // The retain needs to happen within the full-expression.
2983 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2984 enterFullExpression(cleanups);
2985 RunCleanupsScope scope(*this);
2986 return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
2989 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2990 llvm::Value *value = result.getPointer();
2991 if (result.getInt())
2992 value = EmitARCAutorelease(value);
2994 value = EmitARCRetainAutorelease(e->getType(), value);
2998 llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
2999 llvm::Value *result;
3002 if (shouldEmitSeparateBlockRetain(e)) {
3003 result = EmitScalarExpr(e);
3006 TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
3007 result = subresult.getPointer();
3008 doRetain = !subresult.getInt();
3012 result = EmitARCRetainBlock(result, /*mandatory*/ true);
3013 return EmitObjCConsumeObject(e->getType(), result);
3016 llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
3017 // In ARC, retain and autorelease the expression.
3018 if (getLangOpts().ObjCAutoRefCount) {
3019 // Do so before running any cleanups for the full-expression.
3020 // EmitARCRetainAutoreleaseScalarExpr does this for us.
3021 return EmitARCRetainAutoreleaseScalarExpr(expr);
3024 // Otherwise, use the normal scalar-expression emission. The
3025 // exception machinery doesn't do anything special with the
3026 // exception like retaining it, so there's no safety associated with
3027 // only running cleanups after the throw has started, and when it
3028 // matters it tends to be substantially inferior code.
3029 return EmitScalarExpr(expr);
3034 /// An emitter for assigning into an __unsafe_unretained context.
3035 struct ARCUnsafeUnretainedExprEmitter :
3036 public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> {
3038 ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3040 llvm::Value *getValueOfResult(llvm::Value *value) {
3044 llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) {
3045 return CGF.Builder.CreateBitCast(value, resultType);
3048 llvm::Value *visitLValueToRValue(const Expr *e) {
3049 return CGF.EmitScalarExpr(e);
3052 /// For consumptions, just emit the subexpression and perform the
3053 /// consumption like normal.
3054 llvm::Value *visitConsumeObject(const Expr *e) {
3055 llvm::Value *value = CGF.EmitScalarExpr(e);
3056 return CGF.EmitObjCConsumeObject(e->getType(), value);
3059 /// No special logic for block extensions. (This probably can't
3060 /// actually happen in this emitter, though.)
3061 llvm::Value *visitExtendBlockObject(const Expr *e) {
3062 return CGF.EmitARCExtendBlockObject(e);
3065 /// For reclaims, perform an unsafeClaim if that's enabled.
3066 llvm::Value *visitReclaimReturnedObject(const Expr *e) {
3067 return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true);
3070 /// When we have an undecorated call, just emit it without adding
3071 /// the unsafeClaim.
3072 llvm::Value *visitCall(const Expr *e) {
3073 return CGF.EmitScalarExpr(e);
3076 /// Just do normal scalar emission in the default case.
3077 llvm::Value *visitExpr(const Expr *e) {
3078 return CGF.EmitScalarExpr(e);
3083 static llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF,
3085 return ARCUnsafeUnretainedExprEmitter(CGF).visit(e);
3088 /// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to
3089 /// immediately releasing the resut of EmitARCRetainScalarExpr, but
3090 /// avoiding any spurious retains, including by performing reclaims
3091 /// with objc_unsafeClaimAutoreleasedReturnValue.
3092 llvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) {
3093 // Look through full-expressions.
3094 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3095 enterFullExpression(cleanups);
3096 RunCleanupsScope scope(*this);
3097 return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr());
3100 return emitARCUnsafeUnretainedScalarExpr(*this, e);
3103 std::pair<LValue,llvm::Value*>
3104 CodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e,
3106 // Evaluate the RHS first. If we're ignoring the result, assume
3107 // that we can emit at an unsafe +0.
3110 value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS());
3112 value = EmitScalarExpr(e->getRHS());
3115 // Emit the LHS and perform the store.
3116 LValue lvalue = EmitLValue(e->getLHS());
3117 EmitStoreOfScalar(value, lvalue);
3119 return std::pair<LValue,llvm::Value*>(std::move(lvalue), value);
3122 std::pair<LValue,llvm::Value*>
3123 CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
3125 // Evaluate the RHS first.
3126 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
3127 llvm::Value *value = result.getPointer();
3129 bool hasImmediateRetain = result.getInt();
3131 // If we didn't emit a retained object, and the l-value is of block
3132 // type, then we need to emit the block-retain immediately in case
3133 // it invalidates the l-value.
3134 if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
3135 value = EmitARCRetainBlock(value, /*mandatory*/ false);
3136 hasImmediateRetain = true;
3139 LValue lvalue = EmitLValue(e->getLHS());
3141 // If the RHS was emitted retained, expand this.
3142 if (hasImmediateRetain) {
3143 llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation());
3144 EmitStoreOfScalar(value, lvalue);
3145 EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
3147 value = EmitARCStoreStrong(lvalue, value, ignored);
3150 return std::pair<LValue,llvm::Value*>(lvalue, value);
3153 std::pair<LValue,llvm::Value*>
3154 CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
3155 llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
3156 LValue lvalue = EmitLValue(e->getLHS());
3158 EmitStoreOfScalar(value, lvalue);
3160 return std::pair<LValue,llvm::Value*>(lvalue, value);
3163 void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
3164 const ObjCAutoreleasePoolStmt &ARPS) {
3165 const Stmt *subStmt = ARPS.getSubStmt();
3166 const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
3168 CGDebugInfo *DI = getDebugInfo();
3170 DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
3172 // Keep track of the current cleanup stack depth.
3173 RunCleanupsScope Scope(*this);
3174 if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
3175 llvm::Value *token = EmitObjCAutoreleasePoolPush();
3176 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
3178 llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
3179 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
3182 for (const auto *I : S.body())
3186 DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
3189 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3190 /// make sure it survives garbage collection until this point.
3191 void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
3192 // We just use an inline assembly.
3193 llvm::FunctionType *extenderType
3194 = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
3195 llvm::Value *extender
3196 = llvm::InlineAsm::get(extenderType,
3198 /* constraints */ "r",
3199 /* side effects */ true);
3201 object = Builder.CreateBitCast(object, VoidPtrTy);
3202 EmitNounwindRuntimeCall(extender, object);
3205 /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
3206 /// non-trivial copy assignment function, produce following helper function.
3207 /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
3210 CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
3211 const ObjCPropertyImplDecl *PID) {
3212 if (!getLangOpts().CPlusPlus ||
3213 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
3215 QualType Ty = PID->getPropertyIvarDecl()->getType();
3216 if (!Ty->isRecordType())
3218 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3219 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
3221 llvm::Constant *HelperFn = nullptr;
3222 if (hasTrivialSetExpr(PID))
3224 assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
3225 if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
3228 ASTContext &C = getContext();
3230 = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
3231 FunctionDecl *FD = FunctionDecl::Create(C,
3232 C.getTranslationUnitDecl(),
3234 SourceLocation(), II, C.VoidTy,
3239 QualType DestTy = C.getPointerType(Ty);
3240 QualType SrcTy = Ty;
3242 SrcTy = C.getPointerType(SrcTy);
3244 FunctionArgList args;
3245 ImplicitParamDecl dstDecl(getContext(), FD, SourceLocation(), nullptr,DestTy);
3246 args.push_back(&dstDecl);
3247 ImplicitParamDecl srcDecl(getContext(), FD, SourceLocation(), nullptr, SrcTy);
3248 args.push_back(&srcDecl);
3250 const CGFunctionInfo &FI =
3251 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args);
3253 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3255 llvm::Function *Fn =
3256 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3257 "__assign_helper_atomic_property_",
3260 CGM.SetInternalFunctionAttributes(nullptr, Fn, FI);
3262 StartFunction(FD, C.VoidTy, Fn, FI, args);
3264 DeclRefExpr DstExpr(&dstDecl, false, DestTy,
3265 VK_RValue, SourceLocation());
3266 UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(),
3267 VK_LValue, OK_Ordinary, SourceLocation());
3269 DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
3270 VK_RValue, SourceLocation());
3271 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
3272 VK_LValue, OK_Ordinary, SourceLocation());
3274 Expr *Args[2] = { &DST, &SRC };
3275 CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
3276 CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(),
3277 Args, DestTy->getPointeeType(),
3278 VK_LValue, SourceLocation(), FPOptions());
3283 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3284 CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
3289 CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
3290 const ObjCPropertyImplDecl *PID) {
3291 if (!getLangOpts().CPlusPlus ||
3292 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
3294 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3295 QualType Ty = PD->getType();
3296 if (!Ty->isRecordType())
3298 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
3300 llvm::Constant *HelperFn = nullptr;
3302 if (hasTrivialGetExpr(PID))
3304 assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
3305 if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
3309 ASTContext &C = getContext();
3311 = &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
3312 FunctionDecl *FD = FunctionDecl::Create(C,
3313 C.getTranslationUnitDecl(),
3315 SourceLocation(), II, C.VoidTy,
3320 QualType DestTy = C.getPointerType(Ty);
3321 QualType SrcTy = Ty;
3323 SrcTy = C.getPointerType(SrcTy);
3325 FunctionArgList args;
3326 ImplicitParamDecl dstDecl(getContext(), FD, SourceLocation(), nullptr,DestTy);
3327 args.push_back(&dstDecl);
3328 ImplicitParamDecl srcDecl(getContext(), FD, SourceLocation(), nullptr, SrcTy);
3329 args.push_back(&srcDecl);
3331 const CGFunctionInfo &FI =
3332 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args);
3334 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3336 llvm::Function *Fn =
3337 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3338 "__copy_helper_atomic_property_", &CGM.getModule());
3340 CGM.SetInternalFunctionAttributes(nullptr, Fn, FI);
3342 StartFunction(FD, C.VoidTy, Fn, FI, args);
3344 DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
3345 VK_RValue, SourceLocation());
3347 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
3348 VK_LValue, OK_Ordinary, SourceLocation());
3350 CXXConstructExpr *CXXConstExpr =
3351 cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
3353 SmallVector<Expr*, 4> ConstructorArgs;
3354 ConstructorArgs.push_back(&SRC);
3355 ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()),
3356 CXXConstExpr->arg_end());
3358 CXXConstructExpr *TheCXXConstructExpr =
3359 CXXConstructExpr::Create(C, Ty, SourceLocation(),
3360 CXXConstExpr->getConstructor(),
3361 CXXConstExpr->isElidable(),
3363 CXXConstExpr->hadMultipleCandidates(),
3364 CXXConstExpr->isListInitialization(),
3365 CXXConstExpr->isStdInitListInitialization(),
3366 CXXConstExpr->requiresZeroInitialization(),
3367 CXXConstExpr->getConstructionKind(),
3370 DeclRefExpr DstExpr(&dstDecl, false, DestTy,
3371 VK_RValue, SourceLocation());
3373 RValue DV = EmitAnyExpr(&DstExpr);
3375 = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
3376 EmitAggExpr(TheCXXConstructExpr,
3377 AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment),
3379 AggValueSlot::IsDestructed,
3380 AggValueSlot::DoesNotNeedGCBarriers,
3381 AggValueSlot::IsNotAliased));
3384 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3385 CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
3390 CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
3391 // Get selectors for retain/autorelease.
3392 IdentifierInfo *CopyID = &getContext().Idents.get("copy");
3393 Selector CopySelector =
3394 getContext().Selectors.getNullarySelector(CopyID);
3395 IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3396 Selector AutoreleaseSelector =
3397 getContext().Selectors.getNullarySelector(AutoreleaseID);
3399 // Emit calls to retain/autorelease.
3400 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3401 llvm::Value *Val = Block;
3403 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3405 Val, CallArgList(), nullptr, nullptr);
3406 Val = Result.getScalarVal();
3407 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3408 Ty, AutoreleaseSelector,
3409 Val, CallArgList(), nullptr, nullptr);
3410 Val = Result.getScalarVal();
3415 CodeGenFunction::EmitBuiltinAvailable(ArrayRef<llvm::Value *> Args) {
3416 assert(Args.size() == 3 && "Expected 3 argument here!");
3418 if (!CGM.IsOSVersionAtLeastFn) {
3419 llvm::FunctionType *FTy =
3420 llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false);
3421 CGM.IsOSVersionAtLeastFn =
3422 CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast");
3425 llvm::Value *CallRes =
3426 EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args);
3428 return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty));
3431 void CodeGenModule::emitAtAvailableLinkGuard() {
3432 if (!IsOSVersionAtLeastFn)
3434 // @available requires CoreFoundation only on Darwin.
3435 if (!Target.getTriple().isOSDarwin())
3437 // Add -framework CoreFoundation to the linker commands. We still want to
3438 // emit the core foundation reference down below because otherwise if
3439 // CoreFoundation is not used in the code, the linker won't link the
3441 auto &Context = getLLVMContext();
3442 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
3443 llvm::MDString::get(Context, "CoreFoundation")};
3444 LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args));
3445 // Emit a reference to a symbol from CoreFoundation to ensure that
3446 // CoreFoundation is linked into the final binary.
3447 llvm::FunctionType *FTy =
3448 llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false);
3449 llvm::Constant *CFFunc =
3450 CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber");
3452 llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false);
3453 llvm::Function *CFLinkCheckFunc = cast<llvm::Function>(CreateBuiltinFunction(
3454 CheckFTy, "__clang_at_available_requires_core_foundation_framework"));
3455 CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
3456 CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility);
3457 CodeGenFunction CGF(*this);
3458 CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc));
3459 CGF.EmitNounwindRuntimeCall(CFFunc, llvm::Constant::getNullValue(VoidPtrTy));
3460 CGF.Builder.CreateUnreachable();
3461 addCompilerUsedGlobal(CFLinkCheckFunc);
3464 CGObjCRuntime::~CGObjCRuntime() {}