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 LValue LV = MakeNaturalAlignAddrLValue(Constant, IdTy);
130 llvm::Value *Ptr = EmitLoadOfScalar(LV, E->getLocStart());
131 cast<llvm::LoadInst>(Ptr)->setMetadata(
132 CGM.getModule().getMDKindID("invariant.load"),
133 llvm::MDNode::get(getLLVMContext(), None));
134 return Builder.CreateBitCast(Ptr, ConvertType(E->getType()));
137 // Compute the type of the array we're initializing.
138 llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
140 QualType ElementType = Context.getObjCIdType().withConst();
141 QualType ElementArrayType
142 = Context.getConstantArrayType(ElementType, APNumElements,
143 ArrayType::Normal, /*IndexTypeQuals=*/0);
145 // Allocate the temporary array(s).
146 Address Objects = CreateMemTemp(ElementArrayType, "objects");
147 Address Keys = Address::invalid();
149 Keys = CreateMemTemp(ElementArrayType, "keys");
151 // In ARC, we may need to do extra work to keep all the keys and
152 // values alive until after the call.
153 SmallVector<llvm::Value *, 16> NeededObjects;
154 bool TrackNeededObjects =
155 (getLangOpts().ObjCAutoRefCount &&
156 CGM.getCodeGenOpts().OptimizationLevel != 0);
158 // Perform the actual initialialization of the array(s).
159 for (uint64_t i = 0; i < NumElements; i++) {
161 // Emit the element and store it to the appropriate array slot.
162 const Expr *Rhs = ALE->getElement(i);
163 LValue LV = MakeAddrLValue(
164 Builder.CreateConstArrayGEP(Objects, i, getPointerSize()),
165 ElementType, LValueBaseInfo(AlignmentSource::Decl, false));
167 llvm::Value *value = EmitScalarExpr(Rhs);
168 EmitStoreThroughLValue(RValue::get(value), LV, true);
169 if (TrackNeededObjects) {
170 NeededObjects.push_back(value);
173 // Emit the key and store it to the appropriate array slot.
174 const Expr *Key = DLE->getKeyValueElement(i).Key;
175 LValue KeyLV = MakeAddrLValue(
176 Builder.CreateConstArrayGEP(Keys, i, getPointerSize()),
177 ElementType, LValueBaseInfo(AlignmentSource::Decl, false));
178 llvm::Value *keyValue = EmitScalarExpr(Key);
179 EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
181 // Emit the value and store it to the appropriate array slot.
182 const Expr *Value = DLE->getKeyValueElement(i).Value;
183 LValue ValueLV = MakeAddrLValue(
184 Builder.CreateConstArrayGEP(Objects, i, getPointerSize()),
185 ElementType, LValueBaseInfo(AlignmentSource::Decl, false));
186 llvm::Value *valueValue = EmitScalarExpr(Value);
187 EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
188 if (TrackNeededObjects) {
189 NeededObjects.push_back(keyValue);
190 NeededObjects.push_back(valueValue);
195 // Generate the argument list.
197 ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
198 const ParmVarDecl *argDecl = *PI++;
199 QualType ArgQT = argDecl->getType().getUnqualifiedType();
200 Args.add(RValue::get(Objects.getPointer()), ArgQT);
203 ArgQT = argDecl->getType().getUnqualifiedType();
204 Args.add(RValue::get(Keys.getPointer()), ArgQT);
207 ArgQT = argDecl->getType().getUnqualifiedType();
209 llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
210 Args.add(RValue::get(Count), ArgQT);
212 // Generate a reference to the class pointer, which will be the receiver.
213 Selector Sel = MethodWithObjects->getSelector();
214 QualType ResultType = E->getType();
215 const ObjCObjectPointerType *InterfacePointerType
216 = ResultType->getAsObjCInterfacePointerType();
217 ObjCInterfaceDecl *Class
218 = InterfacePointerType->getObjectType()->getInterface();
219 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
220 llvm::Value *Receiver = Runtime.GetClass(*this, Class);
222 // Generate the message send.
223 RValue result = Runtime.GenerateMessageSend(
224 *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel,
225 Receiver, Args, Class, MethodWithObjects);
227 // The above message send needs these objects, but in ARC they are
228 // passed in a buffer that is essentially __unsafe_unretained.
229 // Therefore we must prevent the optimizer from releasing them until
231 if (TrackNeededObjects) {
232 EmitARCIntrinsicUse(NeededObjects);
235 return Builder.CreateBitCast(result.getScalarVal(),
236 ConvertType(E->getType()));
239 llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) {
240 return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod());
243 llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral(
244 const ObjCDictionaryLiteral *E) {
245 return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod());
249 llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
251 // Note that this implementation allows for non-constant strings to be passed
252 // as arguments to @selector(). Currently, the only thing preventing this
253 // behaviour is the type checking in the front end.
254 return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
257 llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
258 // FIXME: This should pass the Decl not the name.
259 return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
262 /// \brief Adjust the type of an Objective-C object that doesn't match up due
263 /// to type erasure at various points, e.g., related result types or the use
264 /// of parameterized classes.
265 static RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ExpT,
267 if (!ExpT->isObjCRetainableType())
270 // If the converted types are the same, we're done.
271 llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT);
272 if (ExpLLVMTy == Result.getScalarVal()->getType())
275 // We have applied a substitution. Cast the rvalue appropriately.
276 return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
280 /// Decide whether to extend the lifetime of the receiver of a
281 /// returns-inner-pointer message.
283 shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
284 switch (message->getReceiverKind()) {
286 // For a normal instance message, we should extend unless the
287 // receiver is loaded from a variable with precise lifetime.
288 case ObjCMessageExpr::Instance: {
289 const Expr *receiver = message->getInstanceReceiver();
291 // Look through OVEs.
292 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
293 if (opaque->getSourceExpr())
294 receiver = opaque->getSourceExpr()->IgnoreParens();
297 const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
298 if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
299 receiver = ice->getSubExpr()->IgnoreParens();
301 // Look through OVEs.
302 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) {
303 if (opaque->getSourceExpr())
304 receiver = opaque->getSourceExpr()->IgnoreParens();
307 // Only __strong variables.
308 if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
311 // All ivars and fields have precise lifetime.
312 if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
315 // Otherwise, check for variables.
316 const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
317 if (!declRef) return true;
318 const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
319 if (!var) return true;
321 // All variables have precise lifetime except local variables with
322 // automatic storage duration that aren't specially marked.
323 return (var->hasLocalStorage() &&
324 !var->hasAttr<ObjCPreciseLifetimeAttr>());
327 case ObjCMessageExpr::Class:
328 case ObjCMessageExpr::SuperClass:
329 // It's never necessary for class objects.
332 case ObjCMessageExpr::SuperInstance:
333 // We generally assume that 'self' lives throughout a method call.
337 llvm_unreachable("invalid receiver kind");
340 /// Given an expression of ObjC pointer type, check whether it was
341 /// immediately loaded from an ARC __weak l-value.
342 static const Expr *findWeakLValue(const Expr *E) {
343 assert(E->getType()->isObjCRetainableType());
344 E = E->IgnoreParens();
345 if (auto CE = dyn_cast<CastExpr>(E)) {
346 if (CE->getCastKind() == CK_LValueToRValue) {
347 if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
348 return CE->getSubExpr();
355 RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
356 ReturnValueSlot Return) {
357 // Only the lookup mechanism and first two arguments of the method
358 // implementation vary between runtimes. We can get the receiver and
359 // arguments in generic code.
361 bool isDelegateInit = E->isDelegateInitCall();
363 const ObjCMethodDecl *method = E->getMethodDecl();
365 // If the method is -retain, and the receiver's being loaded from
366 // a __weak variable, peephole the entire operation to objc_loadWeakRetained.
367 if (method && E->getReceiverKind() == ObjCMessageExpr::Instance &&
368 method->getMethodFamily() == OMF_retain) {
369 if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) {
370 LValue lvalue = EmitLValue(lvalueExpr);
371 llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress());
372 return AdjustObjCObjectType(*this, E->getType(), RValue::get(result));
376 // We don't retain the receiver in delegate init calls, and this is
377 // safe because the receiver value is always loaded from 'self',
378 // which we zero out. We don't want to Block_copy block receivers,
382 CGM.getLangOpts().ObjCAutoRefCount &&
384 method->hasAttr<NSConsumesSelfAttr>());
386 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
387 bool isSuperMessage = false;
388 bool isClassMessage = false;
389 ObjCInterfaceDecl *OID = nullptr;
391 QualType ReceiverType;
392 llvm::Value *Receiver = nullptr;
393 switch (E->getReceiverKind()) {
394 case ObjCMessageExpr::Instance:
395 ReceiverType = E->getInstanceReceiver()->getType();
397 TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
398 E->getInstanceReceiver());
399 Receiver = ter.getPointer();
400 if (ter.getInt()) retainSelf = false;
402 Receiver = EmitScalarExpr(E->getInstanceReceiver());
405 case ObjCMessageExpr::Class: {
406 ReceiverType = E->getClassReceiver();
407 const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
408 assert(ObjTy && "Invalid Objective-C class message send");
409 OID = ObjTy->getInterface();
410 assert(OID && "Invalid Objective-C class message send");
411 Receiver = Runtime.GetClass(*this, OID);
412 isClassMessage = true;
416 case ObjCMessageExpr::SuperInstance:
417 ReceiverType = E->getSuperType();
418 Receiver = LoadObjCSelf();
419 isSuperMessage = true;
422 case ObjCMessageExpr::SuperClass:
423 ReceiverType = E->getSuperType();
424 Receiver = LoadObjCSelf();
425 isSuperMessage = true;
426 isClassMessage = true;
431 Receiver = EmitARCRetainNonBlock(Receiver);
433 // In ARC, we sometimes want to "extend the lifetime"
434 // (i.e. retain+autorelease) of receivers of returns-inner-pointer
436 if (getLangOpts().ObjCAutoRefCount && method &&
437 method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
438 shouldExtendReceiverForInnerPointerMessage(E))
439 Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
441 QualType ResultType = method ? method->getReturnType() : E->getType();
444 EmitCallArgs(Args, method, E->arguments(), /*AC*/AbstractCallee(method));
446 // For delegate init calls in ARC, do an unsafe store of null into
447 // self. This represents the call taking direct ownership of that
448 // value. We have to do this after emitting the other call
449 // arguments because they might also reference self, but we don't
450 // have to worry about any of them modifying self because that would
451 // be an undefined read and write of an object in unordered
453 if (isDelegateInit) {
454 assert(getLangOpts().ObjCAutoRefCount &&
455 "delegate init calls should only be marked in ARC");
457 // Do an unsafe store of null into self.
459 GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl());
460 Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
464 if (isSuperMessage) {
465 // super is only valid in an Objective-C method
466 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
467 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
468 result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
470 OMD->getClassInterface(),
477 result = Runtime.GenerateMessageSend(*this, Return, ResultType,
483 // For delegate init calls in ARC, implicitly store the result of
484 // the call back into self. This takes ownership of the value.
485 if (isDelegateInit) {
487 GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl());
488 llvm::Value *newSelf = result.getScalarVal();
490 // The delegate return type isn't necessarily a matching type; in
491 // fact, it's quite likely to be 'id'.
492 llvm::Type *selfTy = selfAddr.getElementType();
493 newSelf = Builder.CreateBitCast(newSelf, selfTy);
495 Builder.CreateStore(newSelf, selfAddr);
498 return AdjustObjCObjectType(*this, E->getType(), result);
502 struct FinishARCDealloc final : EHScopeStack::Cleanup {
503 void Emit(CodeGenFunction &CGF, Flags flags) override {
504 const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
506 const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
507 const ObjCInterfaceDecl *iface = impl->getClassInterface();
508 if (!iface->getSuperClass()) return;
510 bool isCategory = isa<ObjCCategoryImplDecl>(impl);
512 // Call [super dealloc] if we have a superclass.
513 llvm::Value *self = CGF.LoadObjCSelf();
516 CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
517 CGF.getContext().VoidTy,
518 method->getSelector(),
522 /*is class msg*/ false,
529 /// StartObjCMethod - Begin emission of an ObjCMethod. This generates
530 /// the LLVM function and sets the other context used by
532 void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
533 const ObjCContainerDecl *CD) {
534 SourceLocation StartLoc = OMD->getLocStart();
535 FunctionArgList args;
536 // Check if we should generate debug info for this method.
537 if (OMD->hasAttr<NoDebugAttr>())
538 DebugInfo = nullptr; // disable debug info indefinitely for this function
540 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
542 const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
543 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
545 args.push_back(OMD->getSelfDecl());
546 args.push_back(OMD->getCmdDecl());
548 args.append(OMD->param_begin(), OMD->param_end());
551 CurEHLocation = OMD->getLocEnd();
553 StartFunction(OMD, OMD->getReturnType(), Fn, FI, args,
554 OMD->getLocation(), StartLoc);
556 // In ARC, certain methods get an extra cleanup.
557 if (CGM.getLangOpts().ObjCAutoRefCount &&
558 OMD->isInstanceMethod() &&
559 OMD->getSelector().isUnarySelector()) {
560 const IdentifierInfo *ident =
561 OMD->getSelector().getIdentifierInfoForSlot(0);
562 if (ident->isStr("dealloc"))
563 EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
567 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
568 LValue lvalue, QualType type);
570 /// Generate an Objective-C method. An Objective-C method is a C function with
571 /// its pointer, name, and types registered in the class struture.
572 void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
573 StartObjCMethod(OMD, OMD->getClassInterface());
574 PGO.assignRegionCounters(GlobalDecl(OMD), CurFn);
575 assert(isa<CompoundStmt>(OMD->getBody()));
576 incrementProfileCounter(OMD->getBody());
577 EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody()));
578 FinishFunction(OMD->getBodyRBrace());
581 /// emitStructGetterCall - Call the runtime function to load a property
582 /// into the return value slot.
583 static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar,
584 bool isAtomic, bool hasStrong) {
585 ASTContext &Context = CGF.getContext();
588 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0)
591 // objc_copyStruct (ReturnValue, &structIvar,
592 // sizeof (Type of Ivar), isAtomic, false);
595 Address dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
596 args.add(RValue::get(dest.getPointer()), Context.VoidPtrTy);
598 src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
599 args.add(RValue::get(src.getPointer()), Context.VoidPtrTy);
601 CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
602 args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
603 args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
604 args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
606 llvm::Constant *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
607 CGCallee callee = CGCallee::forDirect(fn);
608 CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args),
609 callee, ReturnValueSlot(), args);
612 /// Determine whether the given architecture supports unaligned atomic
613 /// accesses. They don't have to be fast, just faster than a function
614 /// call and a mutex.
615 static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
616 // FIXME: Allow unaligned atomic load/store on x86. (It is not
617 // currently supported by the backend.)
621 /// Return the maximum size that permits atomic accesses for the given
623 static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
624 llvm::Triple::ArchType arch) {
625 // ARM has 8-byte atomic accesses, but it's not clear whether we
626 // want to rely on them here.
628 // In the default case, just assume that any size up to a pointer is
629 // fine given adequate alignment.
630 return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
634 class PropertyImplStrategy {
637 /// The 'native' strategy is to use the architecture's provided
638 /// reads and writes.
641 /// Use objc_setProperty and objc_getProperty.
644 /// Use objc_setProperty for the setter, but use expression
645 /// evaluation for the getter.
646 SetPropertyAndExpressionGet,
648 /// Use objc_copyStruct.
651 /// The 'expression' strategy is to emit normal assignment or
652 /// lvalue-to-rvalue expressions.
656 StrategyKind getKind() const { return StrategyKind(Kind); }
658 bool hasStrongMember() const { return HasStrong; }
659 bool isAtomic() const { return IsAtomic; }
660 bool isCopy() const { return IsCopy; }
662 CharUnits getIvarSize() const { return IvarSize; }
663 CharUnits getIvarAlignment() const { return IvarAlignment; }
665 PropertyImplStrategy(CodeGenModule &CGM,
666 const ObjCPropertyImplDecl *propImpl);
670 unsigned IsAtomic : 1;
672 unsigned HasStrong : 1;
675 CharUnits IvarAlignment;
679 /// Pick an implementation strategy for the given property synthesis.
680 PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
681 const ObjCPropertyImplDecl *propImpl) {
682 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
683 ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
685 IsCopy = (setterKind == ObjCPropertyDecl::Copy);
686 IsAtomic = prop->isAtomic();
687 HasStrong = false; // doesn't matter here.
689 // Evaluate the ivar's size and alignment.
690 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
691 QualType ivarType = ivar->getType();
692 std::tie(IvarSize, IvarAlignment) =
693 CGM.getContext().getTypeInfoInChars(ivarType);
695 // If we have a copy property, we always have to use getProperty/setProperty.
696 // TODO: we could actually use setProperty and an expression for non-atomics.
698 Kind = GetSetProperty;
703 if (setterKind == ObjCPropertyDecl::Retain) {
704 // In GC-only, there's nothing special that needs to be done.
705 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
708 // In ARC, if the property is non-atomic, use expression emission,
709 // which translates to objc_storeStrong. This isn't required, but
710 // it's slightly nicer.
711 } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
712 // Using standard expression emission for the setter is only
713 // acceptable if the ivar is __strong, which won't be true if
714 // the property is annotated with __attribute__((NSObject)).
715 // TODO: falling all the way back to objc_setProperty here is
716 // just laziness, though; we could still use objc_storeStrong
717 // if we hacked it right.
718 if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
721 Kind = SetPropertyAndExpressionGet;
724 // Otherwise, we need to at least use setProperty. However, if
725 // the property isn't atomic, we can use normal expression
726 // emission for the getter.
727 } else if (!IsAtomic) {
728 Kind = SetPropertyAndExpressionGet;
731 // Otherwise, we have to use both setProperty and getProperty.
733 Kind = GetSetProperty;
738 // If we're not atomic, just use expression accesses.
744 // Properties on bitfield ivars need to be emitted using expression
745 // accesses even if they're nominally atomic.
746 if (ivar->isBitField()) {
751 // GC-qualified or ARC-qualified ivars need to be emitted as
752 // expressions. This actually works out to being atomic anyway,
753 // except for ARC __strong, but that should trigger the above code.
754 if (ivarType.hasNonTrivialObjCLifetime() ||
755 (CGM.getLangOpts().getGC() &&
756 CGM.getContext().getObjCGCAttrKind(ivarType))) {
761 // Compute whether the ivar has strong members.
762 if (CGM.getLangOpts().getGC())
763 if (const RecordType *recordType = ivarType->getAs<RecordType>())
764 HasStrong = recordType->getDecl()->hasObjectMember();
766 // We can never access structs with object members with a native
767 // access, because we need to use write barriers. This is what
768 // objc_copyStruct is for.
774 // Otherwise, this is target-dependent and based on the size and
775 // alignment of the ivar.
777 // If the size of the ivar is not a power of two, give up. We don't
778 // want to get into the business of doing compare-and-swaps.
779 if (!IvarSize.isPowerOfTwo()) {
784 llvm::Triple::ArchType arch =
785 CGM.getTarget().getTriple().getArch();
787 // Most architectures require memory to fit within a single cache
788 // line, so the alignment has to be at least the size of the access.
789 // Otherwise we have to grab a lock.
790 if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
795 // If the ivar's size exceeds the architecture's maximum atomic
796 // access size, we have to use CopyStruct.
797 if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
802 // Otherwise, we can use native loads and stores.
806 /// \brief Generate an Objective-C property getter function.
808 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
809 /// is illegal within a category.
810 void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
811 const ObjCPropertyImplDecl *PID) {
812 llvm::Constant *AtomicHelperFn =
813 CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID);
814 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
815 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
816 assert(OMD && "Invalid call to generate getter (empty method)");
817 StartObjCMethod(OMD, IMP->getClassInterface());
819 generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
824 static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
825 const Expr *getter = propImpl->getGetterCXXConstructor();
826 if (!getter) return true;
828 // Sema only makes only of these when the ivar has a C++ class type,
829 // so the form is pretty constrained.
831 // If the property has a reference type, we might just be binding a
832 // reference, in which case the result will be a gl-value. We should
833 // treat this as a non-trivial operation.
834 if (getter->isGLValue())
837 // If we selected a trivial copy-constructor, we're okay.
838 if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
839 return (construct->getConstructor()->isTrivial());
841 // The constructor might require cleanups (in which case it's never
843 assert(isa<ExprWithCleanups>(getter));
847 /// emitCPPObjectAtomicGetterCall - Call the runtime function to
848 /// copy the ivar into the resturn slot.
849 static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF,
850 llvm::Value *returnAddr,
852 llvm::Constant *AtomicHelperFn) {
853 // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
857 // The 1st argument is the return Slot.
858 args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
860 // The 2nd argument is the address of the ivar.
861 llvm::Value *ivarAddr =
862 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
863 CGF.LoadObjCSelf(), ivar, 0).getPointer();
864 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
865 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
867 // Third argument is the helper function.
868 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
870 llvm::Constant *copyCppAtomicObjectFn =
871 CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction();
872 CGCallee callee = CGCallee::forDirect(copyCppAtomicObjectFn);
874 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
875 callee, ReturnValueSlot(), args);
879 CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
880 const ObjCPropertyImplDecl *propImpl,
881 const ObjCMethodDecl *GetterMethodDecl,
882 llvm::Constant *AtomicHelperFn) {
883 // If there's a non-trivial 'get' expression, we just have to emit that.
884 if (!hasTrivialGetExpr(propImpl)) {
885 if (!AtomicHelperFn) {
886 ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(),
891 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
892 emitCPPObjectAtomicGetterCall(*this, ReturnValue.getPointer(),
893 ivar, AtomicHelperFn);
898 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
899 QualType propType = prop->getType();
900 ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl();
902 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
904 // Pick an implementation strategy.
905 PropertyImplStrategy strategy(CGM, propImpl);
906 switch (strategy.getKind()) {
907 case PropertyImplStrategy::Native: {
908 // We don't need to do anything for a zero-size struct.
909 if (strategy.getIvarSize().isZero())
912 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
914 // Currently, all atomic accesses have to be through integer
915 // types, so there's no point in trying to pick a prettier type.
916 uint64_t ivarSize = getContext().toBits(strategy.getIvarSize());
917 llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize);
918 bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
920 // Perform an atomic load. This does not impose ordering constraints.
921 Address ivarAddr = LV.getAddress();
922 ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
923 llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
924 load->setAtomic(llvm::AtomicOrdering::Unordered);
926 // Store that value into the return address. Doing this with a
927 // bitcast is likely to produce some pretty ugly IR, but it's not
928 // the *most* terrible thing in the world.
929 llvm::Type *retTy = ConvertType(getterMethod->getReturnType());
930 uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy);
931 llvm::Value *ivarVal = load;
932 if (ivarSize > retTySize) {
933 llvm::Type *newTy = llvm::Type::getIntNTy(getLLVMContext(), retTySize);
934 ivarVal = Builder.CreateTrunc(load, newTy);
935 bitcastType = newTy->getPointerTo();
937 Builder.CreateStore(ivarVal,
938 Builder.CreateBitCast(ReturnValue, bitcastType));
940 // Make sure we don't do an autorelease.
941 AutoreleaseResult = false;
945 case PropertyImplStrategy::GetSetProperty: {
946 llvm::Constant *getPropertyFn =
947 CGM.getObjCRuntime().GetPropertyGetFunction();
948 if (!getPropertyFn) {
949 CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
952 CGCallee callee = CGCallee::forDirect(getPropertyFn);
954 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
955 // FIXME: Can't this be simpler? This might even be worse than the
956 // corresponding gcc code.
958 Builder.CreateLoad(GetAddrOfLocalVar(getterMethod->getCmdDecl()), "cmd");
959 llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
960 llvm::Value *ivarOffset =
961 EmitIvarOffset(classImpl->getClassInterface(), ivar);
964 args.add(RValue::get(self), getContext().getObjCIdType());
965 args.add(RValue::get(cmd), getContext().getObjCSelType());
966 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
967 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
968 getContext().BoolTy);
970 // FIXME: We shouldn't need to get the function info here, the
971 // runtime already should have computed it to build the function.
972 llvm::Instruction *CallInstruction;
973 RValue RV = EmitCall(
974 getTypes().arrangeBuiltinFunctionCall(propType, args),
975 callee, ReturnValueSlot(), args, &CallInstruction);
976 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction))
979 // We need to fix the type here. Ivars with copy & retain are
980 // always objects so we don't need to worry about complex or
982 RV = RValue::get(Builder.CreateBitCast(
984 getTypes().ConvertType(getterMethod->getReturnType())));
986 EmitReturnOfRValue(RV, propType);
988 // objc_getProperty does an autorelease, so we should suppress ours.
989 AutoreleaseResult = false;
994 case PropertyImplStrategy::CopyStruct:
995 emitStructGetterCall(*this, ivar, strategy.isAtomic(),
996 strategy.hasStrongMember());
999 case PropertyImplStrategy::Expression:
1000 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1001 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
1003 QualType ivarType = ivar->getType();
1004 switch (getEvaluationKind(ivarType)) {
1006 ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation());
1007 EmitStoreOfComplex(pair, MakeAddrLValue(ReturnValue, ivarType),
1012 // The return value slot is guaranteed to not be aliased, but
1013 // that's not necessarily the same as "on the stack", so
1014 // we still potentially need objc_memmove_collectable.
1015 EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType);
1019 if (propType->isReferenceType()) {
1020 value = LV.getAddress().getPointer();
1022 // We want to load and autoreleaseReturnValue ARC __weak ivars.
1023 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
1024 if (getLangOpts().ObjCAutoRefCount) {
1025 value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
1027 value = EmitARCLoadWeak(LV.getAddress());
1030 // Otherwise we want to do a simple load, suppressing the
1031 // final autorelease.
1033 value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal();
1034 AutoreleaseResult = false;
1037 value = Builder.CreateBitCast(
1038 value, ConvertType(GetterMethodDecl->getReturnType()));
1041 EmitReturnOfRValue(RValue::get(value), propType);
1045 llvm_unreachable("bad evaluation kind");
1049 llvm_unreachable("bad @property implementation strategy!");
1052 /// emitStructSetterCall - Call the runtime function to store the value
1053 /// from the first formal parameter into the given ivar.
1054 static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
1055 ObjCIvarDecl *ivar) {
1056 // objc_copyStruct (&structIvar, &Arg,
1057 // sizeof (struct something), true, false);
1060 // The first argument is the address of the ivar.
1061 llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
1062 CGF.LoadObjCSelf(), ivar, 0)
1064 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1065 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1067 // The second argument is the address of the parameter variable.
1068 ParmVarDecl *argVar = *OMD->param_begin();
1069 DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
1070 VK_LValue, SourceLocation());
1071 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer();
1072 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1073 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1075 // The third argument is the sizeof the type.
1077 CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
1078 args.add(RValue::get(size), CGF.getContext().getSizeType());
1080 // The fourth argument is the 'isAtomic' flag.
1081 args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
1083 // The fifth argument is the 'hasStrong' flag.
1084 // FIXME: should this really always be false?
1085 args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
1087 llvm::Constant *fn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
1088 CGCallee callee = CGCallee::forDirect(fn);
1090 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1091 callee, ReturnValueSlot(), args);
1094 /// emitCPPObjectAtomicSetterCall - Call the runtime function to store
1095 /// the value from the first formal parameter into the given ivar, using
1096 /// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
1097 static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF,
1098 ObjCMethodDecl *OMD,
1100 llvm::Constant *AtomicHelperFn) {
1101 // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg,
1105 // The first argument is the address of the ivar.
1106 llvm::Value *ivarAddr =
1107 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
1108 CGF.LoadObjCSelf(), ivar, 0).getPointer();
1109 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1110 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1112 // The second argument is the address of the parameter variable.
1113 ParmVarDecl *argVar = *OMD->param_begin();
1114 DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
1115 VK_LValue, SourceLocation());
1116 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer();
1117 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1118 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1120 // Third argument is the helper function.
1121 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1123 llvm::Constant *fn =
1124 CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction();
1125 CGCallee callee = CGCallee::forDirect(fn);
1127 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args),
1128 callee, ReturnValueSlot(), args);
1132 static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
1133 Expr *setter = PID->getSetterCXXAssignment();
1134 if (!setter) return true;
1136 // Sema only makes only of these when the ivar has a C++ class type,
1137 // so the form is pretty constrained.
1139 // An operator call is trivial if the function it calls is trivial.
1140 // This also implies that there's nothing non-trivial going on with
1141 // the arguments, because operator= can only be trivial if it's a
1142 // synthesized assignment operator and therefore both parameters are
1144 if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
1145 if (const FunctionDecl *callee
1146 = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
1147 if (callee->isTrivial())
1152 assert(isa<ExprWithCleanups>(setter));
1156 static bool UseOptimizedSetter(CodeGenModule &CGM) {
1157 if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
1159 return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter();
1163 CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1164 const ObjCPropertyImplDecl *propImpl,
1165 llvm::Constant *AtomicHelperFn) {
1166 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
1167 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1168 ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl();
1170 // Just use the setter expression if Sema gave us one and it's
1172 if (!hasTrivialSetExpr(propImpl)) {
1173 if (!AtomicHelperFn)
1174 // If non-atomic, assignment is called directly.
1175 EmitStmt(propImpl->getSetterCXXAssignment());
1177 // If atomic, assignment is called via a locking api.
1178 emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
1183 PropertyImplStrategy strategy(CGM, propImpl);
1184 switch (strategy.getKind()) {
1185 case PropertyImplStrategy::Native: {
1186 // We don't need to do anything for a zero-size struct.
1187 if (strategy.getIvarSize().isZero())
1190 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1193 EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
1194 Address ivarAddr = ivarLValue.getAddress();
1196 // Currently, all atomic accesses have to be through integer
1197 // types, so there's no point in trying to pick a prettier type.
1198 llvm::Type *bitcastType =
1199 llvm::Type::getIntNTy(getLLVMContext(),
1200 getContext().toBits(strategy.getIvarSize()));
1202 // Cast both arguments to the chosen operation type.
1203 argAddr = Builder.CreateElementBitCast(argAddr, bitcastType);
1204 ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType);
1206 // This bitcast load is likely to cause some nasty IR.
1207 llvm::Value *load = Builder.CreateLoad(argAddr);
1209 // Perform an atomic store. There are no memory ordering requirements.
1210 llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
1211 store->setAtomic(llvm::AtomicOrdering::Unordered);
1215 case PropertyImplStrategy::GetSetProperty:
1216 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1218 llvm::Constant *setOptimizedPropertyFn = nullptr;
1219 llvm::Constant *setPropertyFn = nullptr;
1220 if (UseOptimizedSetter(CGM)) {
1221 // 10.8 and iOS 6.0 code and GC is off
1222 setOptimizedPropertyFn =
1223 CGM.getObjCRuntime()
1224 .GetOptimizedPropertySetFunction(strategy.isAtomic(),
1226 if (!setOptimizedPropertyFn) {
1227 CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
1232 setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
1233 if (!setPropertyFn) {
1234 CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
1239 // Emit objc_setProperty((id) self, _cmd, offset, arg,
1240 // <is-atomic>, <is-copy>).
1242 Builder.CreateLoad(GetAddrOfLocalVar(setterMethod->getCmdDecl()));
1244 Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1245 llvm::Value *ivarOffset =
1246 EmitIvarOffset(classImpl->getClassInterface(), ivar);
1247 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin());
1248 llvm::Value *arg = Builder.CreateLoad(argAddr, "arg");
1249 arg = Builder.CreateBitCast(arg, VoidPtrTy);
1252 args.add(RValue::get(self), getContext().getObjCIdType());
1253 args.add(RValue::get(cmd), getContext().getObjCSelType());
1254 if (setOptimizedPropertyFn) {
1255 args.add(RValue::get(arg), getContext().getObjCIdType());
1256 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1257 CGCallee callee = CGCallee::forDirect(setOptimizedPropertyFn);
1258 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1259 callee, ReturnValueSlot(), args);
1261 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1262 args.add(RValue::get(arg), getContext().getObjCIdType());
1263 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1264 getContext().BoolTy);
1265 args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
1266 getContext().BoolTy);
1267 // FIXME: We shouldn't need to get the function info here, the runtime
1268 // already should have computed it to build the function.
1269 CGCallee callee = CGCallee::forDirect(setPropertyFn);
1270 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args),
1271 callee, ReturnValueSlot(), args);
1277 case PropertyImplStrategy::CopyStruct:
1278 emitStructSetterCall(*this, setterMethod, ivar);
1281 case PropertyImplStrategy::Expression:
1285 // Otherwise, fake up some ASTs and emit a normal assignment.
1286 ValueDecl *selfDecl = setterMethod->getSelfDecl();
1287 DeclRefExpr self(selfDecl, false, selfDecl->getType(),
1288 VK_LValue, SourceLocation());
1289 ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
1290 selfDecl->getType(), CK_LValueToRValue, &self,
1292 ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
1293 SourceLocation(), SourceLocation(),
1294 &selfLoad, true, true);
1296 ParmVarDecl *argDecl = *setterMethod->param_begin();
1297 QualType argType = argDecl->getType().getNonReferenceType();
1298 DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation());
1299 ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
1300 argType.getUnqualifiedType(), CK_LValueToRValue,
1303 // The property type can differ from the ivar type in some situations with
1304 // Objective-C pointer types, we can always bit cast the RHS in these cases.
1305 // The following absurdity is just to ensure well-formed IR.
1306 CastKind argCK = CK_NoOp;
1307 if (ivarRef.getType()->isObjCObjectPointerType()) {
1308 if (argLoad.getType()->isObjCObjectPointerType())
1310 else if (argLoad.getType()->isBlockPointerType())
1311 argCK = CK_BlockPointerToObjCPointerCast;
1313 argCK = CK_CPointerToObjCPointerCast;
1314 } else if (ivarRef.getType()->isBlockPointerType()) {
1315 if (argLoad.getType()->isBlockPointerType())
1318 argCK = CK_AnyPointerToBlockPointerCast;
1319 } else if (ivarRef.getType()->isPointerType()) {
1322 ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
1323 ivarRef.getType(), argCK, &argLoad,
1325 Expr *finalArg = &argLoad;
1326 if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
1328 finalArg = &argCast;
1331 BinaryOperator assign(&ivarRef, finalArg, BO_Assign,
1332 ivarRef.getType(), VK_RValue, OK_Ordinary,
1333 SourceLocation(), FPOptions());
1337 /// \brief Generate an Objective-C property setter function.
1339 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
1340 /// is illegal within a category.
1341 void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
1342 const ObjCPropertyImplDecl *PID) {
1343 llvm::Constant *AtomicHelperFn =
1344 CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID);
1345 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1346 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
1347 assert(OMD && "Invalid call to generate setter (empty method)");
1348 StartObjCMethod(OMD, IMP->getClassInterface());
1350 generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1356 struct DestroyIvar final : EHScopeStack::Cleanup {
1359 const ObjCIvarDecl *ivar;
1360 CodeGenFunction::Destroyer *destroyer;
1361 bool useEHCleanupForArray;
1363 DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
1364 CodeGenFunction::Destroyer *destroyer,
1365 bool useEHCleanupForArray)
1366 : addr(addr), ivar(ivar), destroyer(destroyer),
1367 useEHCleanupForArray(useEHCleanupForArray) {}
1369 void Emit(CodeGenFunction &CGF, Flags flags) override {
1371 = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
1372 CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
1373 flags.isForNormalCleanup() && useEHCleanupForArray);
1378 /// Like CodeGenFunction::destroyARCStrong, but do it with a call.
1379 static void destroyARCStrongWithStore(CodeGenFunction &CGF,
1382 llvm::Value *null = getNullForVariable(addr);
1383 CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
1386 static void emitCXXDestructMethod(CodeGenFunction &CGF,
1387 ObjCImplementationDecl *impl) {
1388 CodeGenFunction::RunCleanupsScope scope(CGF);
1390 llvm::Value *self = CGF.LoadObjCSelf();
1392 const ObjCInterfaceDecl *iface = impl->getClassInterface();
1393 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1394 ivar; ivar = ivar->getNextIvar()) {
1395 QualType type = ivar->getType();
1397 // Check whether the ivar is a destructible type.
1398 QualType::DestructionKind dtorKind = type.isDestructedType();
1399 if (!dtorKind) continue;
1401 CodeGenFunction::Destroyer *destroyer = nullptr;
1403 // Use a call to objc_storeStrong to destroy strong ivars, for the
1404 // general benefit of the tools.
1405 if (dtorKind == QualType::DK_objc_strong_lifetime) {
1406 destroyer = destroyARCStrongWithStore;
1408 // Otherwise use the default for the destruction kind.
1410 destroyer = CGF.getDestroyer(dtorKind);
1413 CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
1415 CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
1416 cleanupKind & EHCleanup);
1419 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1422 void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1425 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
1426 StartObjCMethod(MD, IMP->getClassInterface());
1428 // Emit .cxx_construct.
1430 // Suppress the final autorelease in ARC.
1431 AutoreleaseResult = false;
1433 for (const auto *IvarInit : IMP->inits()) {
1434 FieldDecl *Field = IvarInit->getAnyMember();
1435 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1436 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
1437 LoadObjCSelf(), Ivar, 0);
1438 EmitAggExpr(IvarInit->getInit(),
1439 AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
1440 AggValueSlot::DoesNotNeedGCBarriers,
1441 AggValueSlot::IsNotAliased));
1443 // constructor returns 'self'.
1444 CodeGenTypes &Types = CGM.getTypes();
1445 QualType IdTy(CGM.getContext().getObjCIdType());
1446 llvm::Value *SelfAsId =
1447 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1448 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1450 // Emit .cxx_destruct.
1452 emitCXXDestructMethod(*this, IMP);
1457 llvm::Value *CodeGenFunction::LoadObjCSelf() {
1458 VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
1459 DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
1460 Self->getType(), VK_LValue, SourceLocation());
1461 return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation());
1464 QualType CodeGenFunction::TypeOfSelfObject() {
1465 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1466 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
1467 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
1468 getContext().getCanonicalType(selfDecl->getType()));
1469 return PTy->getPointeeType();
1472 void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
1473 llvm::Constant *EnumerationMutationFnPtr =
1474 CGM.getObjCRuntime().EnumerationMutationFunction();
1475 if (!EnumerationMutationFnPtr) {
1476 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1479 CGCallee EnumerationMutationFn =
1480 CGCallee::forDirect(EnumerationMutationFnPtr);
1482 CGDebugInfo *DI = getDebugInfo();
1484 DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1486 RunCleanupsScope ForScope(*this);
1488 // The local variable comes into scope immediately.
1489 AutoVarEmission variable = AutoVarEmission::invalid();
1490 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
1491 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
1493 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
1495 // Fast enumeration state.
1496 QualType StateTy = CGM.getObjCFastEnumerationStateType();
1497 Address StatePtr = CreateMemTemp(StateTy, "state.ptr");
1498 EmitNullInitialization(StatePtr, StateTy);
1500 // Number of elements in the items array.
1501 static const unsigned NumItems = 16;
1503 // Fetch the countByEnumeratingWithState:objects:count: selector.
1504 IdentifierInfo *II[] = {
1505 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1506 &CGM.getContext().Idents.get("objects"),
1507 &CGM.getContext().Idents.get("count")
1509 Selector FastEnumSel =
1510 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1513 getContext().getConstantArrayType(getContext().getObjCIdType(),
1514 llvm::APInt(32, NumItems),
1515 ArrayType::Normal, 0);
1516 Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
1518 // Emit the collection pointer. In ARC, we do a retain.
1519 llvm::Value *Collection;
1520 if (getLangOpts().ObjCAutoRefCount) {
1521 Collection = EmitARCRetainScalarExpr(S.getCollection());
1523 // Enter a cleanup to do the release.
1524 EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1526 Collection = EmitScalarExpr(S.getCollection());
1529 // The 'continue' label needs to appear within the cleanup for the
1530 // collection object.
1531 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
1533 // Send it our message:
1536 // The first argument is a temporary of the enumeration-state type.
1537 Args.add(RValue::get(StatePtr.getPointer()),
1538 getContext().getPointerType(StateTy));
1540 // The second argument is a temporary array with space for NumItems
1541 // pointers. We'll actually be loading elements from the array
1542 // pointer written into the control state; this buffer is so that
1543 // collections that *aren't* backed by arrays can still queue up
1544 // batches of elements.
1545 Args.add(RValue::get(ItemsPtr.getPointer()),
1546 getContext().getPointerType(ItemsTy));
1548 // The third argument is the capacity of that temporary array.
1549 llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
1550 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
1551 Args.add(RValue::get(Count), getContext().UnsignedLongTy);
1553 // Start the enumeration.
1555 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1556 getContext().UnsignedLongTy,
1560 // The initial number of objects that were returned in the buffer.
1561 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
1563 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1564 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1566 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
1568 // If the limit pointer was zero to begin with, the collection is
1569 // empty; skip all this. Set the branch weight assuming this has the same
1570 // probability of exiting the loop as any other loop exit.
1571 uint64_t EntryCount = getCurrentProfileCount();
1572 Builder.CreateCondBr(
1573 Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB,
1575 createProfileWeights(EntryCount, getProfileCount(S.getBody())));
1577 // Otherwise, initialize the loop.
1578 EmitBlock(LoopInitBB);
1580 // Save the initial mutations value. This is the value at an
1581 // address that was written into the state object by
1582 // countByEnumeratingWithState:objects:count:.
1583 Address StateMutationsPtrPtr = Builder.CreateStructGEP(
1584 StatePtr, 2, 2 * getPointerSize(), "mutationsptr.ptr");
1585 llvm::Value *StateMutationsPtr
1586 = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1588 llvm::Value *initialMutations =
1589 Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(),
1590 "forcoll.initial-mutations");
1592 // Start looping. This is the point we return to whenever we have a
1593 // fresh, non-empty batch of objects.
1594 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1595 EmitBlock(LoopBodyBB);
1597 // The current index into the buffer.
1598 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
1599 index->addIncoming(zero, LoopInitBB);
1601 // The current buffer size.
1602 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
1603 count->addIncoming(initialBufferLimit, LoopInitBB);
1605 incrementProfileCounter(&S);
1607 // Check whether the mutations value has changed from where it was
1608 // at start. StateMutationsPtr should actually be invariant between
1610 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1611 llvm::Value *currentMutations
1612 = Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(),
1615 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1616 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
1618 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1619 WasNotMutatedBB, WasMutatedBB);
1621 // If so, call the enumeration-mutation function.
1622 EmitBlock(WasMutatedBB);
1624 Builder.CreateBitCast(Collection,
1625 ConvertType(getContext().getObjCIdType()));
1627 Args2.add(RValue::get(V), getContext().getObjCIdType());
1628 // FIXME: We shouldn't need to get the function info here, the runtime already
1629 // should have computed it to build the function.
1631 CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2),
1632 EnumerationMutationFn, ReturnValueSlot(), Args2);
1634 // Otherwise, or if the mutation function returns, just continue.
1635 EmitBlock(WasNotMutatedBB);
1637 // Initialize the element variable.
1638 RunCleanupsScope elementVariableScope(*this);
1639 bool elementIsVariable;
1640 LValue elementLValue;
1641 QualType elementType;
1642 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
1643 // Initialize the variable, in case it's a __block variable or something.
1644 EmitAutoVarInit(variable);
1646 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
1647 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(),
1648 VK_LValue, SourceLocation());
1649 elementLValue = EmitLValue(&tempDRE);
1650 elementType = D->getType();
1651 elementIsVariable = true;
1653 if (D->isARCPseudoStrong())
1654 elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1656 elementLValue = LValue(); // suppress warning
1657 elementType = cast<Expr>(S.getElement())->getType();
1658 elementIsVariable = false;
1660 llvm::Type *convertedElementType = ConvertType(elementType);
1662 // Fetch the buffer out of the enumeration state.
1663 // TODO: this pointer should actually be invariant between
1664 // refreshes, which would help us do certain loop optimizations.
1665 Address StateItemsPtr = Builder.CreateStructGEP(
1666 StatePtr, 1, getPointerSize(), "stateitems.ptr");
1667 llvm::Value *EnumStateItems =
1668 Builder.CreateLoad(StateItemsPtr, "stateitems");
1670 // Fetch the value at the current index from the buffer.
1671 llvm::Value *CurrentItemPtr =
1672 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
1673 llvm::Value *CurrentItem =
1674 Builder.CreateAlignedLoad(CurrentItemPtr, getPointerAlign());
1676 // Cast that value to the right type.
1677 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1680 // Make sure we have an l-value. Yes, this gets evaluated every
1681 // time through the loop.
1682 if (!elementIsVariable) {
1683 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1684 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
1686 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue,
1690 // If we do have an element variable, this assignment is the end of
1691 // its initialization.
1692 if (elementIsVariable)
1693 EmitAutoVarCleanups(variable);
1695 // Perform the loop body, setting up break and continue labels.
1696 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1698 RunCleanupsScope Scope(*this);
1699 EmitStmt(S.getBody());
1701 BreakContinueStack.pop_back();
1703 // Destroy the element variable now.
1704 elementVariableScope.ForceCleanup();
1706 // Check whether there are more elements.
1707 EmitBlock(AfterBody.getBlock());
1709 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1711 // First we check in the local buffer.
1712 llvm::Value *indexPlusOne
1713 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
1715 // If we haven't overrun the buffer yet, we can continue.
1716 // Set the branch weights based on the simplifying assumption that this is
1717 // like a while-loop, i.e., ignoring that the false branch fetches more
1718 // elements and then returns to the loop.
1719 Builder.CreateCondBr(
1720 Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB,
1721 createProfileWeights(getProfileCount(S.getBody()), EntryCount));
1723 index->addIncoming(indexPlusOne, AfterBody.getBlock());
1724 count->addIncoming(count, AfterBody.getBlock());
1726 // Otherwise, we have to fetch more elements.
1727 EmitBlock(FetchMoreBB);
1730 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1731 getContext().UnsignedLongTy,
1735 // If we got a zero count, we're done.
1736 llvm::Value *refetchCount = CountRV.getScalarVal();
1738 // (note that the message send might split FetchMoreBB)
1739 index->addIncoming(zero, Builder.GetInsertBlock());
1740 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1742 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1743 EmptyBB, LoopBodyBB);
1745 // No more elements.
1748 if (!elementIsVariable) {
1749 // If the element was not a declaration, set it to be null.
1751 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1752 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1753 EmitStoreThroughLValue(RValue::get(null), elementLValue);
1757 DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
1759 ForScope.ForceCleanup();
1760 EmitBlock(LoopEnd.getBlock());
1763 void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1764 CGM.getObjCRuntime().EmitTryStmt(*this, S);
1767 void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
1768 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1771 void CodeGenFunction::EmitObjCAtSynchronizedStmt(
1772 const ObjCAtSynchronizedStmt &S) {
1773 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
1777 struct CallObjCRelease final : EHScopeStack::Cleanup {
1778 CallObjCRelease(llvm::Value *object) : object(object) {}
1779 llvm::Value *object;
1781 void Emit(CodeGenFunction &CGF, Flags flags) override {
1782 // Releases at the end of the full-expression are imprecise.
1783 CGF.EmitARCRelease(object, ARCImpreciseLifetime);
1788 /// Produce the code for a CK_ARCConsumeObject. Does a primitive
1789 /// release at the end of the full-expression.
1790 llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
1791 llvm::Value *object) {
1792 // If we're in a conditional branch, we need to make the cleanup
1794 pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
1798 llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
1799 llvm::Value *value) {
1800 return EmitARCRetainAutorelease(type, value);
1803 /// Given a number of pointers, inform the optimizer that they're
1804 /// being intrinsically used up until this point in the program.
1805 void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
1806 llvm::Constant *&fn = CGM.getObjCEntrypoints().clang_arc_use;
1808 llvm::FunctionType *fnType =
1809 llvm::FunctionType::get(CGM.VoidTy, None, true);
1810 fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use");
1813 // This isn't really a "runtime" function, but as an intrinsic it
1814 // doesn't really matter as long as we align things up.
1815 EmitNounwindRuntimeCall(fn, values);
1819 static bool IsForwarding(StringRef Name) {
1820 return llvm::StringSwitch<bool>(Name)
1821 .Cases("objc_autoreleaseReturnValue", // ARCInstKind::AutoreleaseRV
1822 "objc_autorelease", // ARCInstKind::Autorelease
1823 "objc_retainAutoreleaseReturnValue", // ARCInstKind::FusedRetainAutoreleaseRV
1824 "objc_retainAutoreleasedReturnValue", // ARCInstKind::RetainRV
1825 "objc_retainAutorelease", // ARCInstKind::FusedRetainAutorelease
1826 "objc_retainedObject", // ARCInstKind::NoopCast
1827 "objc_retain", // ARCInstKind::Retain
1828 "objc_unretainedObject", // ARCInstKind::NoopCast
1829 "objc_unretainedPointer", // ARCInstKind::NoopCast
1830 "objc_unsafeClaimAutoreleasedReturnValue", // ARCInstKind::ClaimRV
1835 static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
1836 llvm::FunctionType *FTy,
1838 llvm::Constant *RTF = CGM.CreateRuntimeFunction(FTy, Name);
1840 if (auto *F = dyn_cast<llvm::Function>(RTF)) {
1841 // If the target runtime doesn't naturally support ARC, emit weak
1842 // references to the runtime support library. We don't really
1843 // permit this to fail, but we need a particular relocation style.
1844 if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() &&
1845 !CGM.getTriple().isOSBinFormatCOFF()) {
1846 F->setLinkage(llvm::Function::ExternalWeakLinkage);
1847 } else if (Name == "objc_retain" || Name == "objc_release") {
1848 // If we have Native ARC, set nonlazybind attribute for these APIs for
1850 F->addFnAttr(llvm::Attribute::NonLazyBind);
1853 if (IsForwarding(Name))
1854 F->arg_begin()->addAttr(llvm::Attribute::Returned);
1860 /// Perform an operation having the signature
1862 /// where a null input causes a no-op and returns null.
1863 static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
1865 llvm::Constant *&fn,
1867 bool isTailCall = false) {
1868 if (isa<llvm::ConstantPointerNull>(value))
1872 llvm::FunctionType *fnType =
1873 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
1874 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1877 // Cast the argument to 'id'.
1878 llvm::Type *origType = value->getType();
1879 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
1881 // Call the function.
1882 llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
1884 call->setTailCall();
1886 // Cast the result back to the original type.
1887 return CGF.Builder.CreateBitCast(call, origType);
1890 /// Perform an operation having the following signature:
1892 static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
1894 llvm::Constant *&fn,
1897 llvm::FunctionType *fnType =
1898 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false);
1899 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1902 // Cast the argument to 'id*'.
1903 llvm::Type *origType = addr.getElementType();
1904 addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
1906 // Call the function.
1907 llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer());
1909 // Cast the result back to a dereference of the original type.
1910 if (origType != CGF.Int8PtrTy)
1911 result = CGF.Builder.CreateBitCast(result, origType);
1916 /// Perform an operation having the following signature:
1918 static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
1921 llvm::Constant *&fn,
1924 assert(addr.getElementType() == value->getType());
1927 llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy };
1929 llvm::FunctionType *fnType
1930 = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
1931 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1934 llvm::Type *origType = value->getType();
1936 llvm::Value *args[] = {
1937 CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy),
1938 CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
1940 llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
1942 if (ignored) return nullptr;
1944 return CGF.Builder.CreateBitCast(result, origType);
1947 /// Perform an operation having the following signature:
1948 /// void (i8**, i8**)
1949 static void emitARCCopyOperation(CodeGenFunction &CGF,
1952 llvm::Constant *&fn,
1954 assert(dst.getType() == src.getType());
1957 llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy };
1959 llvm::FunctionType *fnType
1960 = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
1961 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1964 llvm::Value *args[] = {
1965 CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy),
1966 CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy)
1968 CGF.EmitNounwindRuntimeCall(fn, args);
1971 /// Produce the code to do a retain. Based on the type, calls one of:
1972 /// call i8* \@objc_retain(i8* %value)
1973 /// call i8* \@objc_retainBlock(i8* %value)
1974 llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
1975 if (type->isBlockPointerType())
1976 return EmitARCRetainBlock(value, /*mandatory*/ false);
1978 return EmitARCRetainNonBlock(value);
1981 /// Retain the given object, with normal retain semantics.
1982 /// call i8* \@objc_retain(i8* %value)
1983 llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
1984 return emitARCValueOperation(*this, value,
1985 CGM.getObjCEntrypoints().objc_retain,
1989 /// Retain the given block, with _Block_copy semantics.
1990 /// call i8* \@objc_retainBlock(i8* %value)
1992 /// \param mandatory - If false, emit the call with metadata
1993 /// indicating that it's okay for the optimizer to eliminate this call
1994 /// if it can prove that the block never escapes except down the stack.
1995 llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
1998 = emitARCValueOperation(*this, value,
1999 CGM.getObjCEntrypoints().objc_retainBlock,
2000 "objc_retainBlock");
2002 // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
2003 // tell the optimizer that it doesn't need to do this copy if the
2004 // block doesn't escape, where being passed as an argument doesn't
2005 // count as escaping.
2006 if (!mandatory && isa<llvm::Instruction>(result)) {
2007 llvm::CallInst *call
2008 = cast<llvm::CallInst>(result->stripPointerCasts());
2009 assert(call->getCalledValue() == CGM.getObjCEntrypoints().objc_retainBlock);
2011 call->setMetadata("clang.arc.copy_on_escape",
2012 llvm::MDNode::get(Builder.getContext(), None));
2018 static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) {
2019 // Fetch the void(void) inline asm which marks that we're going to
2020 // do something with the autoreleased return value.
2021 llvm::InlineAsm *&marker
2022 = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker;
2025 = CGF.CGM.getTargetCodeGenInfo()
2026 .getARCRetainAutoreleasedReturnValueMarker();
2028 // If we have an empty assembly string, there's nothing to do.
2029 if (assembly.empty()) {
2031 // Otherwise, at -O0, build an inline asm that we're going to call
2033 } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
2034 llvm::FunctionType *type =
2035 llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false);
2037 marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
2039 // If we're at -O1 and above, we don't want to litter the code
2040 // with this marker yet, so leave a breadcrumb for the ARC
2041 // optimizer to pick up.
2043 llvm::NamedMDNode *metadata =
2044 CGF.CGM.getModule().getOrInsertNamedMetadata(
2045 "clang.arc.retainAutoreleasedReturnValueMarker");
2046 assert(metadata->getNumOperands() <= 1);
2047 if (metadata->getNumOperands() == 0) {
2048 auto &ctx = CGF.getLLVMContext();
2049 metadata->addOperand(llvm::MDNode::get(ctx,
2050 llvm::MDString::get(ctx, assembly)));
2055 // Call the marker asm if we made one, which we do only at -O0.
2057 CGF.Builder.CreateCall(marker);
2060 /// Retain the given object which is the result of a function call.
2061 /// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
2063 /// Yes, this function name is one character away from a different
2064 /// call with completely different semantics.
2066 CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
2067 emitAutoreleasedReturnValueMarker(*this);
2068 return emitARCValueOperation(*this, value,
2069 CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue,
2070 "objc_retainAutoreleasedReturnValue");
2073 /// Claim a possibly-autoreleased return value at +0. This is only
2074 /// valid to do in contexts which do not rely on the retain to keep
2075 /// the object valid for for all of its uses; for example, when
2076 /// the value is ignored, or when it is being assigned to an
2077 /// __unsafe_unretained variable.
2079 /// call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value)
2081 CodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) {
2082 emitAutoreleasedReturnValueMarker(*this);
2083 return emitARCValueOperation(*this, value,
2084 CGM.getObjCEntrypoints().objc_unsafeClaimAutoreleasedReturnValue,
2085 "objc_unsafeClaimAutoreleasedReturnValue");
2088 /// Release the given object.
2089 /// call void \@objc_release(i8* %value)
2090 void CodeGenFunction::EmitARCRelease(llvm::Value *value,
2091 ARCPreciseLifetime_t precise) {
2092 if (isa<llvm::ConstantPointerNull>(value)) return;
2094 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_release;
2096 llvm::FunctionType *fnType =
2097 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2098 fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
2101 // Cast the argument to 'id'.
2102 value = Builder.CreateBitCast(value, Int8PtrTy);
2104 // Call objc_release.
2105 llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2107 if (precise == ARCImpreciseLifetime) {
2108 call->setMetadata("clang.imprecise_release",
2109 llvm::MDNode::get(Builder.getContext(), None));
2113 /// Destroy a __strong variable.
2115 /// At -O0, emit a call to store 'null' into the address;
2116 /// instrumenting tools prefer this because the address is exposed,
2117 /// but it's relatively cumbersome to optimize.
2119 /// At -O1 and above, just load and call objc_release.
2121 /// call void \@objc_storeStrong(i8** %addr, i8* null)
2122 void CodeGenFunction::EmitARCDestroyStrong(Address addr,
2123 ARCPreciseLifetime_t precise) {
2124 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2125 llvm::Value *null = getNullForVariable(addr);
2126 EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2130 llvm::Value *value = Builder.CreateLoad(addr);
2131 EmitARCRelease(value, precise);
2134 /// Store into a strong object. Always calls this:
2135 /// call void \@objc_storeStrong(i8** %addr, i8* %value)
2136 llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr,
2139 assert(addr.getElementType() == value->getType());
2141 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_storeStrong;
2143 llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
2144 llvm::FunctionType *fnType
2145 = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
2146 fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
2149 llvm::Value *args[] = {
2150 Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy),
2151 Builder.CreateBitCast(value, Int8PtrTy)
2153 EmitNounwindRuntimeCall(fn, args);
2155 if (ignored) return nullptr;
2159 /// Store into a strong object. Sometimes calls this:
2160 /// call void \@objc_storeStrong(i8** %addr, i8* %value)
2161 /// Other times, breaks it down into components.
2162 llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
2163 llvm::Value *newValue,
2165 QualType type = dst.getType();
2166 bool isBlock = type->isBlockPointerType();
2168 // Use a store barrier at -O0 unless this is a block type or the
2169 // lvalue is inadequately aligned.
2170 if (shouldUseFusedARCCalls() &&
2172 (dst.getAlignment().isZero() ||
2173 dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
2174 return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
2177 // Otherwise, split it out.
2179 // Retain the new value.
2180 newValue = EmitARCRetain(type, newValue);
2182 // Read the old value.
2183 llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation());
2185 // Store. We do this before the release so that any deallocs won't
2186 // see the old value.
2187 EmitStoreOfScalar(newValue, dst);
2189 // Finally, release the old value.
2190 EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2195 /// Autorelease the given object.
2196 /// call i8* \@objc_autorelease(i8* %value)
2197 llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2198 return emitARCValueOperation(*this, value,
2199 CGM.getObjCEntrypoints().objc_autorelease,
2200 "objc_autorelease");
2203 /// Autorelease the given object.
2204 /// call i8* \@objc_autoreleaseReturnValue(i8* %value)
2206 CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
2207 return emitARCValueOperation(*this, value,
2208 CGM.getObjCEntrypoints().objc_autoreleaseReturnValue,
2209 "objc_autoreleaseReturnValue",
2210 /*isTailCall*/ true);
2213 /// Do a fused retain/autorelease of the given object.
2214 /// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2216 CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
2217 return emitARCValueOperation(*this, value,
2218 CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue,
2219 "objc_retainAutoreleaseReturnValue",
2220 /*isTailCall*/ true);
2223 /// Do a fused retain/autorelease of the given object.
2224 /// call i8* \@objc_retainAutorelease(i8* %value)
2226 /// %retain = call i8* \@objc_retainBlock(i8* %value)
2227 /// call i8* \@objc_autorelease(i8* %retain)
2228 llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
2229 llvm::Value *value) {
2230 if (!type->isBlockPointerType())
2231 return EmitARCRetainAutoreleaseNonBlock(value);
2233 if (isa<llvm::ConstantPointerNull>(value)) return value;
2235 llvm::Type *origType = value->getType();
2236 value = Builder.CreateBitCast(value, Int8PtrTy);
2237 value = EmitARCRetainBlock(value, /*mandatory*/ true);
2238 value = EmitARCAutorelease(value);
2239 return Builder.CreateBitCast(value, origType);
2242 /// Do a fused retain/autorelease of the given object.
2243 /// call i8* \@objc_retainAutorelease(i8* %value)
2245 CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
2246 return emitARCValueOperation(*this, value,
2247 CGM.getObjCEntrypoints().objc_retainAutorelease,
2248 "objc_retainAutorelease");
2251 /// i8* \@objc_loadWeak(i8** %addr)
2252 /// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2253 llvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) {
2254 return emitARCLoadOperation(*this, addr,
2255 CGM.getObjCEntrypoints().objc_loadWeak,
2259 /// i8* \@objc_loadWeakRetained(i8** %addr)
2260 llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) {
2261 return emitARCLoadOperation(*this, addr,
2262 CGM.getObjCEntrypoints().objc_loadWeakRetained,
2263 "objc_loadWeakRetained");
2266 /// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2268 llvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr,
2271 return emitARCStoreOperation(*this, addr, value,
2272 CGM.getObjCEntrypoints().objc_storeWeak,
2273 "objc_storeWeak", ignored);
2276 /// i8* \@objc_initWeak(i8** %addr, i8* %value)
2277 /// Returns %value. %addr is known to not have a current weak entry.
2278 /// Essentially equivalent to:
2279 /// *addr = nil; objc_storeWeak(addr, value);
2280 void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) {
2281 // If we're initializing to null, just write null to memory; no need
2282 // to get the runtime involved. But don't do this if optimization
2283 // is enabled, because accounting for this would make the optimizer
2284 // much more complicated.
2285 if (isa<llvm::ConstantPointerNull>(value) &&
2286 CGM.getCodeGenOpts().OptimizationLevel == 0) {
2287 Builder.CreateStore(value, addr);
2291 emitARCStoreOperation(*this, addr, value,
2292 CGM.getObjCEntrypoints().objc_initWeak,
2293 "objc_initWeak", /*ignored*/ true);
2296 /// void \@objc_destroyWeak(i8** %addr)
2297 /// Essentially objc_storeWeak(addr, nil).
2298 void CodeGenFunction::EmitARCDestroyWeak(Address addr) {
2299 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_destroyWeak;
2301 llvm::FunctionType *fnType =
2302 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false);
2303 fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
2306 // Cast the argument to 'id*'.
2307 addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
2309 EmitNounwindRuntimeCall(fn, addr.getPointer());
2312 /// void \@objc_moveWeak(i8** %dest, i8** %src)
2313 /// Disregards the current value in %dest. Leaves %src pointing to nothing.
2314 /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2315 void CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) {
2316 emitARCCopyOperation(*this, dst, src,
2317 CGM.getObjCEntrypoints().objc_moveWeak,
2321 /// void \@objc_copyWeak(i8** %dest, i8** %src)
2322 /// Disregards the current value in %dest. Essentially
2323 /// objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2324 void CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) {
2325 emitARCCopyOperation(*this, dst, src,
2326 CGM.getObjCEntrypoints().objc_copyWeak,
2330 /// Produce the code to do a objc_autoreleasepool_push.
2331 /// call i8* \@objc_autoreleasePoolPush(void)
2332 llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
2333 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush;
2335 llvm::FunctionType *fnType =
2336 llvm::FunctionType::get(Int8PtrTy, false);
2337 fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
2340 return EmitNounwindRuntimeCall(fn);
2343 /// Produce the code to do a primitive release.
2344 /// call void \@objc_autoreleasePoolPop(i8* %ptr)
2345 void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
2346 assert(value->getType() == Int8PtrTy);
2348 llvm::Constant *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop;
2350 llvm::FunctionType *fnType =
2351 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2353 // We don't want to use a weak import here; instead we should not
2354 // fall into this path.
2355 fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
2358 // objc_autoreleasePoolPop can throw.
2359 EmitRuntimeCallOrInvoke(fn, value);
2362 /// Produce the code to do an MRR version objc_autoreleasepool_push.
2363 /// Which is: [[NSAutoreleasePool alloc] init];
2364 /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2365 /// init is declared as: - (id) init; in its NSObject super class.
2367 llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2368 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2369 llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2370 // [NSAutoreleasePool alloc]
2371 IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2372 Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2375 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2376 getContext().getObjCIdType(),
2377 AllocSel, Receiver, Args);
2380 Receiver = AllocRV.getScalarVal();
2381 II = &CGM.getContext().Idents.get("init");
2382 Selector InitSel = getContext().Selectors.getSelector(0, &II);
2384 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2385 getContext().getObjCIdType(),
2386 InitSel, Receiver, Args);
2387 return InitRV.getScalarVal();
2390 /// Produce the code to do a primitive release.
2392 void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2393 IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2394 Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2396 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2397 getContext().VoidTy, DrainSel, Arg, Args);
2400 void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2403 CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
2406 void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2409 CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
2412 void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2415 CGF.EmitARCDestroyWeak(addr);
2418 void CodeGenFunction::emitARCIntrinsicUse(CodeGenFunction &CGF, Address addr,
2420 llvm::Value *value = CGF.Builder.CreateLoad(addr);
2421 CGF.EmitARCIntrinsicUse(value);
2425 struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup {
2428 CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2430 void Emit(CodeGenFunction &CGF, Flags flags) override {
2431 CGF.EmitObjCAutoreleasePoolPop(Token);
2434 struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup {
2437 CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2439 void Emit(CodeGenFunction &CGF, Flags flags) override {
2440 CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2445 void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
2446 if (CGM.getLangOpts().ObjCAutoRefCount)
2447 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2449 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2452 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2455 switch (type.getObjCLifetime()) {
2456 case Qualifiers::OCL_None:
2457 case Qualifiers::OCL_ExplicitNone:
2458 case Qualifiers::OCL_Strong:
2459 case Qualifiers::OCL_Autoreleasing:
2460 return TryEmitResult(CGF.EmitLoadOfLValue(lvalue,
2461 SourceLocation()).getScalarVal(),
2464 case Qualifiers::OCL_Weak:
2465 return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
2469 llvm_unreachable("impossible lifetime!");
2472 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2474 e = e->IgnoreParens();
2475 QualType type = e->getType();
2477 // If we're loading retained from a __strong xvalue, we can avoid
2478 // an extra retain/release pair by zeroing out the source of this
2479 // "move" operation.
2480 if (e->isXValue() &&
2481 !type.isConstQualified() &&
2482 type.getObjCLifetime() == Qualifiers::OCL_Strong) {
2484 LValue lv = CGF.EmitLValue(e);
2486 // Load the object pointer.
2487 llvm::Value *result = CGF.EmitLoadOfLValue(lv,
2488 SourceLocation()).getScalarVal();
2490 // Set the source pointer to NULL.
2491 CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
2493 return TryEmitResult(result, true);
2496 // As a very special optimization, in ARC++, if the l-value is the
2497 // result of a non-volatile assignment, do a simple retain of the
2498 // result of the call to objc_storeWeak instead of reloading.
2499 if (CGF.getLangOpts().CPlusPlus &&
2500 !type.isVolatileQualified() &&
2501 type.getObjCLifetime() == Qualifiers::OCL_Weak &&
2502 isa<BinaryOperator>(e) &&
2503 cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
2504 return TryEmitResult(CGF.EmitScalarExpr(e), false);
2506 return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2509 typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
2510 llvm::Value *value)>
2513 /// Insert code immediately after a call.
2514 static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF,
2516 ValueTransform doAfterCall,
2517 ValueTransform doFallback) {
2518 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2519 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2521 // Place the retain immediately following the call.
2522 CGF.Builder.SetInsertPoint(call->getParent(),
2523 ++llvm::BasicBlock::iterator(call));
2524 value = doAfterCall(CGF, value);
2526 CGF.Builder.restoreIP(ip);
2528 } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
2529 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2531 // Place the retain at the beginning of the normal destination block.
2532 llvm::BasicBlock *BB = invoke->getNormalDest();
2533 CGF.Builder.SetInsertPoint(BB, BB->begin());
2534 value = doAfterCall(CGF, value);
2536 CGF.Builder.restoreIP(ip);
2539 // Bitcasts can arise because of related-result returns. Rewrite
2541 } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2542 llvm::Value *operand = bitcast->getOperand(0);
2543 operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback);
2544 bitcast->setOperand(0, operand);
2547 // Generic fall-back case.
2549 // Retain using the non-block variant: we never need to do a copy
2550 // of a block that's been returned to us.
2551 return doFallback(CGF, value);
2555 /// Given that the given expression is some sort of call (which does
2556 /// not return retained), emit a retain following it.
2557 static llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF,
2559 llvm::Value *value = CGF.EmitScalarExpr(e);
2560 return emitARCOperationAfterCall(CGF, value,
2561 [](CodeGenFunction &CGF, llvm::Value *value) {
2562 return CGF.EmitARCRetainAutoreleasedReturnValue(value);
2564 [](CodeGenFunction &CGF, llvm::Value *value) {
2565 return CGF.EmitARCRetainNonBlock(value);
2569 /// Given that the given expression is some sort of call (which does
2570 /// not return retained), perform an unsafeClaim following it.
2571 static llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF,
2573 llvm::Value *value = CGF.EmitScalarExpr(e);
2574 return emitARCOperationAfterCall(CGF, value,
2575 [](CodeGenFunction &CGF, llvm::Value *value) {
2576 return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value);
2578 [](CodeGenFunction &CGF, llvm::Value *value) {
2583 llvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E,
2584 bool allowUnsafeClaim) {
2585 if (allowUnsafeClaim &&
2586 CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) {
2587 return emitARCUnsafeClaimCallResult(*this, E);
2589 llvm::Value *value = emitARCRetainCallResult(*this, E);
2590 return EmitObjCConsumeObject(E->getType(), value);
2594 /// Determine whether it might be important to emit a separate
2595 /// objc_retain_block on the result of the given expression, or
2596 /// whether it's okay to just emit it in a +1 context.
2597 static bool shouldEmitSeparateBlockRetain(const Expr *e) {
2598 assert(e->getType()->isBlockPointerType());
2599 e = e->IgnoreParens();
2601 // For future goodness, emit block expressions directly in +1
2602 // contexts if we can.
2603 if (isa<BlockExpr>(e))
2606 if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
2607 switch (cast->getCastKind()) {
2608 // Emitting these operations in +1 contexts is goodness.
2609 case CK_LValueToRValue:
2610 case CK_ARCReclaimReturnedObject:
2611 case CK_ARCConsumeObject:
2612 case CK_ARCProduceObject:
2615 // These operations preserve a block type.
2618 return shouldEmitSeparateBlockRetain(cast->getSubExpr());
2620 // These operations are known to be bad (or haven't been considered).
2621 case CK_AnyPointerToBlockPointerCast:
2631 /// A CRTP base class for emitting expressions of retainable object
2632 /// pointer type in ARC.
2633 template <typename Impl, typename Result> class ARCExprEmitter {
2635 CodeGenFunction &CGF;
2636 Impl &asImpl() { return *static_cast<Impl*>(this); }
2638 ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {}
2641 Result visit(const Expr *e);
2642 Result visitCastExpr(const CastExpr *e);
2643 Result visitPseudoObjectExpr(const PseudoObjectExpr *e);
2644 Result visitBinaryOperator(const BinaryOperator *e);
2645 Result visitBinAssign(const BinaryOperator *e);
2646 Result visitBinAssignUnsafeUnretained(const BinaryOperator *e);
2647 Result visitBinAssignAutoreleasing(const BinaryOperator *e);
2648 Result visitBinAssignWeak(const BinaryOperator *e);
2649 Result visitBinAssignStrong(const BinaryOperator *e);
2651 // Minimal implementation:
2652 // Result visitLValueToRValue(const Expr *e)
2653 // Result visitConsumeObject(const Expr *e)
2654 // Result visitExtendBlockObject(const Expr *e)
2655 // Result visitReclaimReturnedObject(const Expr *e)
2656 // Result visitCall(const Expr *e)
2657 // Result visitExpr(const Expr *e)
2659 // Result emitBitCast(Result result, llvm::Type *resultType)
2660 // llvm::Value *getValueOfResult(Result result)
2664 /// Try to emit a PseudoObjectExpr under special ARC rules.
2666 /// This massively duplicates emitPseudoObjectRValue.
2667 template <typename Impl, typename Result>
2669 ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) {
2670 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
2672 // Find the result expression.
2673 const Expr *resultExpr = E->getResultExpr();
2677 for (PseudoObjectExpr::const_semantics_iterator
2678 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
2679 const Expr *semantic = *i;
2681 // If this semantic expression is an opaque value, bind it
2682 // to the result of its source expression.
2683 if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
2684 typedef CodeGenFunction::OpaqueValueMappingData OVMA;
2687 // If this semantic is the result of the pseudo-object
2688 // expression, try to evaluate the source as +1.
2689 if (ov == resultExpr) {
2690 assert(!OVMA::shouldBindAsLValue(ov));
2691 result = asImpl().visit(ov->getSourceExpr());
2692 opaqueData = OVMA::bind(CGF, ov,
2693 RValue::get(asImpl().getValueOfResult(result)));
2695 // Otherwise, just bind it.
2697 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
2699 opaques.push_back(opaqueData);
2701 // Otherwise, if the expression is the result, evaluate it
2702 // and remember the result.
2703 } else if (semantic == resultExpr) {
2704 result = asImpl().visit(semantic);
2706 // Otherwise, evaluate the expression in an ignored context.
2708 CGF.EmitIgnoredExpr(semantic);
2712 // Unbind all the opaques now.
2713 for (unsigned i = 0, e = opaques.size(); i != e; ++i)
2714 opaques[i].unbind(CGF);
2719 template <typename Impl, typename Result>
2720 Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) {
2721 switch (e->getCastKind()) {
2723 // No-op casts don't change the type, so we just ignore them.
2725 return asImpl().visit(e->getSubExpr());
2727 // These casts can change the type.
2728 case CK_CPointerToObjCPointerCast:
2729 case CK_BlockPointerToObjCPointerCast:
2730 case CK_AnyPointerToBlockPointerCast:
2732 llvm::Type *resultType = CGF.ConvertType(e->getType());
2733 assert(e->getSubExpr()->getType()->hasPointerRepresentation());
2734 Result result = asImpl().visit(e->getSubExpr());
2735 return asImpl().emitBitCast(result, resultType);
2738 // Handle some casts specially.
2739 case CK_LValueToRValue:
2740 return asImpl().visitLValueToRValue(e->getSubExpr());
2741 case CK_ARCConsumeObject:
2742 return asImpl().visitConsumeObject(e->getSubExpr());
2743 case CK_ARCExtendBlockObject:
2744 return asImpl().visitExtendBlockObject(e->getSubExpr());
2745 case CK_ARCReclaimReturnedObject:
2746 return asImpl().visitReclaimReturnedObject(e->getSubExpr());
2748 // Otherwise, use the default logic.
2750 return asImpl().visitExpr(e);
2754 template <typename Impl, typename Result>
2756 ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) {
2757 switch (e->getOpcode()) {
2759 CGF.EmitIgnoredExpr(e->getLHS());
2760 CGF.EnsureInsertPoint();
2761 return asImpl().visit(e->getRHS());
2764 return asImpl().visitBinAssign(e);
2767 return asImpl().visitExpr(e);
2771 template <typename Impl, typename Result>
2772 Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) {
2773 switch (e->getLHS()->getType().getObjCLifetime()) {
2774 case Qualifiers::OCL_ExplicitNone:
2775 return asImpl().visitBinAssignUnsafeUnretained(e);
2777 case Qualifiers::OCL_Weak:
2778 return asImpl().visitBinAssignWeak(e);
2780 case Qualifiers::OCL_Autoreleasing:
2781 return asImpl().visitBinAssignAutoreleasing(e);
2783 case Qualifiers::OCL_Strong:
2784 return asImpl().visitBinAssignStrong(e);
2786 case Qualifiers::OCL_None:
2787 return asImpl().visitExpr(e);
2789 llvm_unreachable("bad ObjC ownership qualifier");
2792 /// The default rule for __unsafe_unretained emits the RHS recursively,
2793 /// stores into the unsafe variable, and propagates the result outward.
2794 template <typename Impl, typename Result>
2795 Result ARCExprEmitter<Impl,Result>::
2796 visitBinAssignUnsafeUnretained(const BinaryOperator *e) {
2797 // Recursively emit the RHS.
2798 // For __block safety, do this before emitting the LHS.
2799 Result result = asImpl().visit(e->getRHS());
2801 // Perform the store.
2803 CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store);
2804 CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)),
2810 template <typename Impl, typename Result>
2812 ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) {
2813 return asImpl().visitExpr(e);
2816 template <typename Impl, typename Result>
2818 ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) {
2819 return asImpl().visitExpr(e);
2822 template <typename Impl, typename Result>
2824 ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) {
2825 return asImpl().visitExpr(e);
2828 /// The general expression-emission logic.
2829 template <typename Impl, typename Result>
2830 Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) {
2831 // We should *never* see a nested full-expression here, because if
2832 // we fail to emit at +1, our caller must not retain after we close
2833 // out the full-expression. This isn't as important in the unsafe
2835 assert(!isa<ExprWithCleanups>(e));
2837 // Look through parens, __extension__, generic selection, etc.
2838 e = e->IgnoreParens();
2840 // Handle certain kinds of casts.
2841 if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
2842 return asImpl().visitCastExpr(ce);
2844 // Handle the comma operator.
2845 } else if (auto op = dyn_cast<BinaryOperator>(e)) {
2846 return asImpl().visitBinaryOperator(op);
2848 // TODO: handle conditional operators here
2850 // For calls and message sends, use the retained-call logic.
2851 // Delegate inits are a special case in that they're the only
2852 // returns-retained expression that *isn't* surrounded by
2854 } else if (isa<CallExpr>(e) ||
2855 (isa<ObjCMessageExpr>(e) &&
2856 !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
2857 return asImpl().visitCall(e);
2859 // Look through pseudo-object expressions.
2860 } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
2861 return asImpl().visitPseudoObjectExpr(pseudo);
2864 return asImpl().visitExpr(e);
2869 /// An emitter for +1 results.
2870 struct ARCRetainExprEmitter :
2871 public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> {
2873 ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
2875 llvm::Value *getValueOfResult(TryEmitResult result) {
2876 return result.getPointer();
2879 TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) {
2880 llvm::Value *value = result.getPointer();
2881 value = CGF.Builder.CreateBitCast(value, resultType);
2882 result.setPointer(value);
2886 TryEmitResult visitLValueToRValue(const Expr *e) {
2887 return tryEmitARCRetainLoadOfScalar(CGF, e);
2890 /// For consumptions, just emit the subexpression and thus elide
2891 /// the retain/release pair.
2892 TryEmitResult visitConsumeObject(const Expr *e) {
2893 llvm::Value *result = CGF.EmitScalarExpr(e);
2894 return TryEmitResult(result, true);
2897 /// Block extends are net +0. Naively, we could just recurse on
2898 /// the subexpression, but actually we need to ensure that the
2899 /// value is copied as a block, so there's a little filter here.
2900 TryEmitResult visitExtendBlockObject(const Expr *e) {
2901 llvm::Value *result; // will be a +0 value
2903 // If we can't safely assume the sub-expression will produce a
2904 // block-copied value, emit the sub-expression at +0.
2905 if (shouldEmitSeparateBlockRetain(e)) {
2906 result = CGF.EmitScalarExpr(e);
2908 // Otherwise, try to emit the sub-expression at +1 recursively.
2910 TryEmitResult subresult = asImpl().visit(e);
2912 // If that produced a retained value, just use that.
2913 if (subresult.getInt()) {
2917 // Otherwise it's +0.
2918 result = subresult.getPointer();
2921 // Retain the object as a block.
2922 result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
2923 return TryEmitResult(result, true);
2926 /// For reclaims, emit the subexpression as a retained call and
2927 /// skip the consumption.
2928 TryEmitResult visitReclaimReturnedObject(const Expr *e) {
2929 llvm::Value *result = emitARCRetainCallResult(CGF, e);
2930 return TryEmitResult(result, true);
2933 /// When we have an undecorated call, retroactively do a claim.
2934 TryEmitResult visitCall(const Expr *e) {
2935 llvm::Value *result = emitARCRetainCallResult(CGF, e);
2936 return TryEmitResult(result, true);
2939 // TODO: maybe special-case visitBinAssignWeak?
2941 TryEmitResult visitExpr(const Expr *e) {
2942 // We didn't find an obvious production, so emit what we've got and
2943 // tell the caller that we didn't manage to retain.
2944 llvm::Value *result = CGF.EmitScalarExpr(e);
2945 return TryEmitResult(result, false);
2950 static TryEmitResult
2951 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
2952 return ARCRetainExprEmitter(CGF).visit(e);
2955 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2958 TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
2959 llvm::Value *value = result.getPointer();
2960 if (!result.getInt())
2961 value = CGF.EmitARCRetain(type, value);
2965 /// EmitARCRetainScalarExpr - Semantically equivalent to
2966 /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
2967 /// best-effort attempt to peephole expressions that naturally produce
2968 /// retained objects.
2969 llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
2970 // The retain needs to happen within the full-expression.
2971 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2972 enterFullExpression(cleanups);
2973 RunCleanupsScope scope(*this);
2974 return EmitARCRetainScalarExpr(cleanups->getSubExpr());
2977 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2978 llvm::Value *value = result.getPointer();
2979 if (!result.getInt())
2980 value = EmitARCRetain(e->getType(), value);
2985 CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
2986 // The retain needs to happen within the full-expression.
2987 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2988 enterFullExpression(cleanups);
2989 RunCleanupsScope scope(*this);
2990 return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
2993 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2994 llvm::Value *value = result.getPointer();
2995 if (result.getInt())
2996 value = EmitARCAutorelease(value);
2998 value = EmitARCRetainAutorelease(e->getType(), value);
3002 llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
3003 llvm::Value *result;
3006 if (shouldEmitSeparateBlockRetain(e)) {
3007 result = EmitScalarExpr(e);
3010 TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
3011 result = subresult.getPointer();
3012 doRetain = !subresult.getInt();
3016 result = EmitARCRetainBlock(result, /*mandatory*/ true);
3017 return EmitObjCConsumeObject(e->getType(), result);
3020 llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
3021 // In ARC, retain and autorelease the expression.
3022 if (getLangOpts().ObjCAutoRefCount) {
3023 // Do so before running any cleanups for the full-expression.
3024 // EmitARCRetainAutoreleaseScalarExpr does this for us.
3025 return EmitARCRetainAutoreleaseScalarExpr(expr);
3028 // Otherwise, use the normal scalar-expression emission. The
3029 // exception machinery doesn't do anything special with the
3030 // exception like retaining it, so there's no safety associated with
3031 // only running cleanups after the throw has started, and when it
3032 // matters it tends to be substantially inferior code.
3033 return EmitScalarExpr(expr);
3038 /// An emitter for assigning into an __unsafe_unretained context.
3039 struct ARCUnsafeUnretainedExprEmitter :
3040 public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> {
3042 ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {}
3044 llvm::Value *getValueOfResult(llvm::Value *value) {
3048 llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) {
3049 return CGF.Builder.CreateBitCast(value, resultType);
3052 llvm::Value *visitLValueToRValue(const Expr *e) {
3053 return CGF.EmitScalarExpr(e);
3056 /// For consumptions, just emit the subexpression and perform the
3057 /// consumption like normal.
3058 llvm::Value *visitConsumeObject(const Expr *e) {
3059 llvm::Value *value = CGF.EmitScalarExpr(e);
3060 return CGF.EmitObjCConsumeObject(e->getType(), value);
3063 /// No special logic for block extensions. (This probably can't
3064 /// actually happen in this emitter, though.)
3065 llvm::Value *visitExtendBlockObject(const Expr *e) {
3066 return CGF.EmitARCExtendBlockObject(e);
3069 /// For reclaims, perform an unsafeClaim if that's enabled.
3070 llvm::Value *visitReclaimReturnedObject(const Expr *e) {
3071 return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true);
3074 /// When we have an undecorated call, just emit it without adding
3075 /// the unsafeClaim.
3076 llvm::Value *visitCall(const Expr *e) {
3077 return CGF.EmitScalarExpr(e);
3080 /// Just do normal scalar emission in the default case.
3081 llvm::Value *visitExpr(const Expr *e) {
3082 return CGF.EmitScalarExpr(e);
3087 static llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF,
3089 return ARCUnsafeUnretainedExprEmitter(CGF).visit(e);
3092 /// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to
3093 /// immediately releasing the resut of EmitARCRetainScalarExpr, but
3094 /// avoiding any spurious retains, including by performing reclaims
3095 /// with objc_unsafeClaimAutoreleasedReturnValue.
3096 llvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) {
3097 // Look through full-expressions.
3098 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
3099 enterFullExpression(cleanups);
3100 RunCleanupsScope scope(*this);
3101 return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr());
3104 return emitARCUnsafeUnretainedScalarExpr(*this, e);
3107 std::pair<LValue,llvm::Value*>
3108 CodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e,
3110 // Evaluate the RHS first. If we're ignoring the result, assume
3111 // that we can emit at an unsafe +0.
3114 value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS());
3116 value = EmitScalarExpr(e->getRHS());
3119 // Emit the LHS and perform the store.
3120 LValue lvalue = EmitLValue(e->getLHS());
3121 EmitStoreOfScalar(value, lvalue);
3123 return std::pair<LValue,llvm::Value*>(std::move(lvalue), value);
3126 std::pair<LValue,llvm::Value*>
3127 CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
3129 // Evaluate the RHS first.
3130 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
3131 llvm::Value *value = result.getPointer();
3133 bool hasImmediateRetain = result.getInt();
3135 // If we didn't emit a retained object, and the l-value is of block
3136 // type, then we need to emit the block-retain immediately in case
3137 // it invalidates the l-value.
3138 if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
3139 value = EmitARCRetainBlock(value, /*mandatory*/ false);
3140 hasImmediateRetain = true;
3143 LValue lvalue = EmitLValue(e->getLHS());
3145 // If the RHS was emitted retained, expand this.
3146 if (hasImmediateRetain) {
3147 llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation());
3148 EmitStoreOfScalar(value, lvalue);
3149 EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
3151 value = EmitARCStoreStrong(lvalue, value, ignored);
3154 return std::pair<LValue,llvm::Value*>(lvalue, value);
3157 std::pair<LValue,llvm::Value*>
3158 CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
3159 llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
3160 LValue lvalue = EmitLValue(e->getLHS());
3162 EmitStoreOfScalar(value, lvalue);
3164 return std::pair<LValue,llvm::Value*>(lvalue, value);
3167 void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
3168 const ObjCAutoreleasePoolStmt &ARPS) {
3169 const Stmt *subStmt = ARPS.getSubStmt();
3170 const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
3172 CGDebugInfo *DI = getDebugInfo();
3174 DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
3176 // Keep track of the current cleanup stack depth.
3177 RunCleanupsScope Scope(*this);
3178 if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
3179 llvm::Value *token = EmitObjCAutoreleasePoolPush();
3180 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
3182 llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
3183 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
3186 for (const auto *I : S.body())
3190 DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
3193 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
3194 /// make sure it survives garbage collection until this point.
3195 void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
3196 // We just use an inline assembly.
3197 llvm::FunctionType *extenderType
3198 = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
3199 llvm::Value *extender
3200 = llvm::InlineAsm::get(extenderType,
3202 /* constraints */ "r",
3203 /* side effects */ true);
3205 object = Builder.CreateBitCast(object, VoidPtrTy);
3206 EmitNounwindRuntimeCall(extender, object);
3209 /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
3210 /// non-trivial copy assignment function, produce following helper function.
3211 /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
3214 CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
3215 const ObjCPropertyImplDecl *PID) {
3216 if (!getLangOpts().CPlusPlus ||
3217 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
3219 QualType Ty = PID->getPropertyIvarDecl()->getType();
3220 if (!Ty->isRecordType())
3222 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3223 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
3225 llvm::Constant *HelperFn = nullptr;
3226 if (hasTrivialSetExpr(PID))
3228 assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
3229 if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
3232 ASTContext &C = getContext();
3234 = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
3235 FunctionDecl *FD = FunctionDecl::Create(C,
3236 C.getTranslationUnitDecl(),
3238 SourceLocation(), II, C.VoidTy,
3243 QualType DestTy = C.getPointerType(Ty);
3244 QualType SrcTy = Ty;
3246 SrcTy = C.getPointerType(SrcTy);
3248 FunctionArgList args;
3249 ImplicitParamDecl DstDecl(getContext(), FD, SourceLocation(), /*Id=*/nullptr,
3250 DestTy, ImplicitParamDecl::Other);
3251 args.push_back(&DstDecl);
3252 ImplicitParamDecl SrcDecl(getContext(), FD, SourceLocation(), /*Id=*/nullptr,
3253 SrcTy, ImplicitParamDecl::Other);
3254 args.push_back(&SrcDecl);
3256 const CGFunctionInfo &FI =
3257 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args);
3259 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3261 llvm::Function *Fn =
3262 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3263 "__assign_helper_atomic_property_",
3266 CGM.SetInternalFunctionAttributes(nullptr, Fn, FI);
3268 StartFunction(FD, C.VoidTy, Fn, FI, args);
3270 DeclRefExpr DstExpr(&DstDecl, false, DestTy,
3271 VK_RValue, SourceLocation());
3272 UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(),
3273 VK_LValue, OK_Ordinary, SourceLocation());
3275 DeclRefExpr SrcExpr(&SrcDecl, false, SrcTy,
3276 VK_RValue, SourceLocation());
3277 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
3278 VK_LValue, OK_Ordinary, SourceLocation());
3280 Expr *Args[2] = { &DST, &SRC };
3281 CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
3282 CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(),
3283 Args, DestTy->getPointeeType(),
3284 VK_LValue, SourceLocation(), FPOptions());
3289 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3290 CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
3295 CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
3296 const ObjCPropertyImplDecl *PID) {
3297 if (!getLangOpts().CPlusPlus ||
3298 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
3300 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
3301 QualType Ty = PD->getType();
3302 if (!Ty->isRecordType())
3304 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
3306 llvm::Constant *HelperFn = nullptr;
3308 if (hasTrivialGetExpr(PID))
3310 assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
3311 if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
3315 ASTContext &C = getContext();
3317 = &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
3318 FunctionDecl *FD = FunctionDecl::Create(C,
3319 C.getTranslationUnitDecl(),
3321 SourceLocation(), II, C.VoidTy,
3326 QualType DestTy = C.getPointerType(Ty);
3327 QualType SrcTy = Ty;
3329 SrcTy = C.getPointerType(SrcTy);
3331 FunctionArgList args;
3332 ImplicitParamDecl DstDecl(getContext(), FD, SourceLocation(), /*Id=*/nullptr,
3333 DestTy, ImplicitParamDecl::Other);
3334 args.push_back(&DstDecl);
3335 ImplicitParamDecl SrcDecl(getContext(), FD, SourceLocation(), /*Id=*/nullptr,
3336 SrcTy, ImplicitParamDecl::Other);
3337 args.push_back(&SrcDecl);
3339 const CGFunctionInfo &FI =
3340 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args);
3342 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
3344 llvm::Function *Fn =
3345 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
3346 "__copy_helper_atomic_property_", &CGM.getModule());
3348 CGM.SetInternalFunctionAttributes(nullptr, Fn, FI);
3350 StartFunction(FD, C.VoidTy, Fn, FI, args);
3352 DeclRefExpr SrcExpr(&SrcDecl, false, SrcTy,
3353 VK_RValue, SourceLocation());
3355 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
3356 VK_LValue, OK_Ordinary, SourceLocation());
3358 CXXConstructExpr *CXXConstExpr =
3359 cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
3361 SmallVector<Expr*, 4> ConstructorArgs;
3362 ConstructorArgs.push_back(&SRC);
3363 ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()),
3364 CXXConstExpr->arg_end());
3366 CXXConstructExpr *TheCXXConstructExpr =
3367 CXXConstructExpr::Create(C, Ty, SourceLocation(),
3368 CXXConstExpr->getConstructor(),
3369 CXXConstExpr->isElidable(),
3371 CXXConstExpr->hadMultipleCandidates(),
3372 CXXConstExpr->isListInitialization(),
3373 CXXConstExpr->isStdInitListInitialization(),
3374 CXXConstExpr->requiresZeroInitialization(),
3375 CXXConstExpr->getConstructionKind(),
3378 DeclRefExpr DstExpr(&DstDecl, false, DestTy,
3379 VK_RValue, SourceLocation());
3381 RValue DV = EmitAnyExpr(&DstExpr);
3383 = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
3384 EmitAggExpr(TheCXXConstructExpr,
3385 AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment),
3387 AggValueSlot::IsDestructed,
3388 AggValueSlot::DoesNotNeedGCBarriers,
3389 AggValueSlot::IsNotAliased));
3392 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3393 CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
3398 CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
3399 // Get selectors for retain/autorelease.
3400 IdentifierInfo *CopyID = &getContext().Idents.get("copy");
3401 Selector CopySelector =
3402 getContext().Selectors.getNullarySelector(CopyID);
3403 IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3404 Selector AutoreleaseSelector =
3405 getContext().Selectors.getNullarySelector(AutoreleaseID);
3407 // Emit calls to retain/autorelease.
3408 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3409 llvm::Value *Val = Block;
3411 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3413 Val, CallArgList(), nullptr, nullptr);
3414 Val = Result.getScalarVal();
3415 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3416 Ty, AutoreleaseSelector,
3417 Val, CallArgList(), nullptr, nullptr);
3418 Val = Result.getScalarVal();
3423 CodeGenFunction::EmitBuiltinAvailable(ArrayRef<llvm::Value *> Args) {
3424 assert(Args.size() == 3 && "Expected 3 argument here!");
3426 if (!CGM.IsOSVersionAtLeastFn) {
3427 llvm::FunctionType *FTy =
3428 llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false);
3429 CGM.IsOSVersionAtLeastFn =
3430 CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast");
3433 llvm::Value *CallRes =
3434 EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args);
3436 return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty));
3439 void CodeGenModule::emitAtAvailableLinkGuard() {
3440 if (!IsOSVersionAtLeastFn)
3442 // @available requires CoreFoundation only on Darwin.
3443 if (!Target.getTriple().isOSDarwin())
3445 // Add -framework CoreFoundation to the linker commands. We still want to
3446 // emit the core foundation reference down below because otherwise if
3447 // CoreFoundation is not used in the code, the linker won't link the
3449 auto &Context = getLLVMContext();
3450 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
3451 llvm::MDString::get(Context, "CoreFoundation")};
3452 LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args));
3453 // Emit a reference to a symbol from CoreFoundation to ensure that
3454 // CoreFoundation is linked into the final binary.
3455 llvm::FunctionType *FTy =
3456 llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false);
3457 llvm::Constant *CFFunc =
3458 CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber");
3460 llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false);
3461 llvm::Function *CFLinkCheckFunc = cast<llvm::Function>(CreateBuiltinFunction(
3462 CheckFTy, "__clang_at_available_requires_core_foundation_framework"));
3463 CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
3464 CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility);
3465 CodeGenFunction CGF(*this);
3466 CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc));
3467 CGF.EmitNounwindRuntimeCall(CFFunc, llvm::Constant::getNullValue(VoidPtrTy));
3468 CGF.Builder.CreateUnreachable();
3469 addCompilerUsedGlobal(CFLinkCheckFunc);
3472 CGObjCRuntime::~CGObjCRuntime() {}