1 //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
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 "llvm/ADT/STLExtras.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/InlineAsm.h"
27 using namespace clang;
28 using namespace CodeGen;
30 typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
32 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e);
33 static RValue AdjustRelatedResultType(CodeGenFunction &CGF,
35 const ObjCMethodDecl *Method,
38 /// Given the address of a variable of pointer type, find the correct
39 /// null to store into it.
40 static llvm::Constant *getNullForVariable(llvm::Value *addr) {
42 cast<llvm::PointerType>(addr->getType())->getElementType();
43 return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
46 /// Emits an instance of NSConstantString representing the object.
47 llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
50 CGM.getObjCRuntime().GenerateConstantString(E->getString());
51 // FIXME: This bitcast should just be made an invariant on the Runtime.
52 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
55 /// EmitObjCBoxedExpr - This routine generates code to call
56 /// the appropriate expression boxing method. This will either be
57 /// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:].
60 CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) {
61 // Generate the correct selector for this literal's concrete type.
62 const Expr *SubExpr = E->getSubExpr();
64 const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod();
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);
76 const ParmVarDecl *argDecl = *BoxingMethod->param_begin();
77 QualType ArgQT = argDecl->getType().getUnqualifiedType();
78 RValue RV = EmitAnyExpr(SubExpr);
82 RValue result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
83 BoxingMethod->getResultType(), Sel, Receiver, Args,
84 ClassDecl, BoxingMethod);
85 return Builder.CreateBitCast(result.getScalarVal(),
86 ConvertType(E->getType()));
89 llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E,
90 const ObjCMethodDecl *MethodWithObjects) {
91 ASTContext &Context = CGM.getContext();
92 const ObjCDictionaryLiteral *DLE = 0;
93 const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E);
95 DLE = cast<ObjCDictionaryLiteral>(E);
97 // Compute the type of the array we're initializing.
98 uint64_t NumElements =
99 ALE ? ALE->getNumElements() : DLE->getNumElements();
100 llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()),
102 QualType ElementType = Context.getObjCIdType().withConst();
103 QualType ElementArrayType
104 = Context.getConstantArrayType(ElementType, APNumElements,
105 ArrayType::Normal, /*IndexTypeQuals=*/0);
107 // Allocate the temporary array(s).
108 llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects");
109 llvm::Value *Keys = 0;
111 Keys = CreateMemTemp(ElementArrayType, "keys");
113 // In ARC, we may need to do extra work to keep all the keys and
114 // values alive until after the call.
115 SmallVector<llvm::Value *, 16> NeededObjects;
116 bool TrackNeededObjects =
117 (getLangOpts().ObjCAutoRefCount &&
118 CGM.getCodeGenOpts().OptimizationLevel != 0);
120 // Perform the actual initialialization of the array(s).
121 for (uint64_t i = 0; i < NumElements; i++) {
123 // Emit the element and store it to the appropriate array slot.
124 const Expr *Rhs = ALE->getElement(i);
125 LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i),
127 Context.getTypeAlignInChars(Rhs->getType()),
130 llvm::Value *value = EmitScalarExpr(Rhs);
131 EmitStoreThroughLValue(RValue::get(value), LV, true);
132 if (TrackNeededObjects) {
133 NeededObjects.push_back(value);
136 // Emit the key and store it to the appropriate array slot.
137 const Expr *Key = DLE->getKeyValueElement(i).Key;
138 LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i),
140 Context.getTypeAlignInChars(Key->getType()),
142 llvm::Value *keyValue = EmitScalarExpr(Key);
143 EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true);
145 // Emit the value and store it to the appropriate array slot.
146 const Expr *Value = DLE->getKeyValueElement(i).Value;
147 LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i),
149 Context.getTypeAlignInChars(Value->getType()),
151 llvm::Value *valueValue = EmitScalarExpr(Value);
152 EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true);
153 if (TrackNeededObjects) {
154 NeededObjects.push_back(keyValue);
155 NeededObjects.push_back(valueValue);
160 // Generate the argument list.
162 ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin();
163 const ParmVarDecl *argDecl = *PI++;
164 QualType ArgQT = argDecl->getType().getUnqualifiedType();
165 Args.add(RValue::get(Objects), ArgQT);
168 ArgQT = argDecl->getType().getUnqualifiedType();
169 Args.add(RValue::get(Keys), ArgQT);
172 ArgQT = argDecl->getType().getUnqualifiedType();
174 llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements);
175 Args.add(RValue::get(Count), ArgQT);
177 // Generate a reference to the class pointer, which will be the receiver.
178 Selector Sel = MethodWithObjects->getSelector();
179 QualType ResultType = E->getType();
180 const ObjCObjectPointerType *InterfacePointerType
181 = ResultType->getAsObjCInterfacePointerType();
182 ObjCInterfaceDecl *Class
183 = InterfacePointerType->getObjectType()->getInterface();
184 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
185 llvm::Value *Receiver = Runtime.GetClass(*this, Class);
187 // Generate the message send.
189 = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
190 MethodWithObjects->getResultType(),
192 Receiver, Args, Class,
195 // The above message send needs these objects, but in ARC they are
196 // passed in a buffer that is essentially __unsafe_unretained.
197 // Therefore we must prevent the optimizer from releasing them until
199 if (TrackNeededObjects) {
200 EmitARCIntrinsicUse(NeededObjects);
203 return Builder.CreateBitCast(result.getScalarVal(),
204 ConvertType(E->getType()));
207 llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) {
208 return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod());
211 llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral(
212 const ObjCDictionaryLiteral *E) {
213 return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod());
217 llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
219 // Note that this implementation allows for non-constant strings to be passed
220 // as arguments to @selector(). Currently, the only thing preventing this
221 // behaviour is the type checking in the front end.
222 return CGM.getObjCRuntime().GetSelector(*this, E->getSelector());
225 llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
226 // FIXME: This should pass the Decl not the name.
227 return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol());
230 /// \brief Adjust the type of the result of an Objective-C message send
231 /// expression when the method has a related result type.
232 static RValue AdjustRelatedResultType(CodeGenFunction &CGF,
234 const ObjCMethodDecl *Method,
239 if (!Method->hasRelatedResultType() ||
240 CGF.getContext().hasSameType(ExpT, Method->getResultType()) ||
244 // We have applied a related result type. Cast the rvalue appropriately.
245 return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
246 CGF.ConvertType(ExpT)));
249 /// Decide whether to extend the lifetime of the receiver of a
250 /// returns-inner-pointer message.
252 shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
253 switch (message->getReceiverKind()) {
255 // For a normal instance message, we should extend unless the
256 // receiver is loaded from a variable with precise lifetime.
257 case ObjCMessageExpr::Instance: {
258 const Expr *receiver = message->getInstanceReceiver();
259 const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
260 if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
261 receiver = ice->getSubExpr()->IgnoreParens();
263 // Only __strong variables.
264 if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
267 // All ivars and fields have precise lifetime.
268 if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
271 // Otherwise, check for variables.
272 const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
273 if (!declRef) return true;
274 const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
275 if (!var) return true;
277 // All variables have precise lifetime except local variables with
278 // automatic storage duration that aren't specially marked.
279 return (var->hasLocalStorage() &&
280 !var->hasAttr<ObjCPreciseLifetimeAttr>());
283 case ObjCMessageExpr::Class:
284 case ObjCMessageExpr::SuperClass:
285 // It's never necessary for class objects.
288 case ObjCMessageExpr::SuperInstance:
289 // We generally assume that 'self' lives throughout a method call.
293 llvm_unreachable("invalid receiver kind");
296 RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
297 ReturnValueSlot Return) {
298 // Only the lookup mechanism and first two arguments of the method
299 // implementation vary between runtimes. We can get the receiver and
300 // arguments in generic code.
302 bool isDelegateInit = E->isDelegateInitCall();
304 const ObjCMethodDecl *method = E->getMethodDecl();
306 // We don't retain the receiver in delegate init calls, and this is
307 // safe because the receiver value is always loaded from 'self',
308 // which we zero out. We don't want to Block_copy block receivers,
312 CGM.getLangOpts().ObjCAutoRefCount &&
314 method->hasAttr<NSConsumesSelfAttr>());
316 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
317 bool isSuperMessage = false;
318 bool isClassMessage = false;
319 ObjCInterfaceDecl *OID = 0;
321 QualType ReceiverType;
322 llvm::Value *Receiver = 0;
323 switch (E->getReceiverKind()) {
324 case ObjCMessageExpr::Instance:
325 ReceiverType = E->getInstanceReceiver()->getType();
327 TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
328 E->getInstanceReceiver());
329 Receiver = ter.getPointer();
330 if (ter.getInt()) retainSelf = false;
332 Receiver = EmitScalarExpr(E->getInstanceReceiver());
335 case ObjCMessageExpr::Class: {
336 ReceiverType = E->getClassReceiver();
337 const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
338 assert(ObjTy && "Invalid Objective-C class message send");
339 OID = ObjTy->getInterface();
340 assert(OID && "Invalid Objective-C class message send");
341 Receiver = Runtime.GetClass(*this, OID);
342 isClassMessage = true;
346 case ObjCMessageExpr::SuperInstance:
347 ReceiverType = E->getSuperType();
348 Receiver = LoadObjCSelf();
349 isSuperMessage = true;
352 case ObjCMessageExpr::SuperClass:
353 ReceiverType = E->getSuperType();
354 Receiver = LoadObjCSelf();
355 isSuperMessage = true;
356 isClassMessage = true;
361 Receiver = EmitARCRetainNonBlock(Receiver);
363 // In ARC, we sometimes want to "extend the lifetime"
364 // (i.e. retain+autorelease) of receivers of returns-inner-pointer
366 if (getLangOpts().ObjCAutoRefCount && method &&
367 method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
368 shouldExtendReceiverForInnerPointerMessage(E))
369 Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
371 QualType ResultType =
372 method ? method->getResultType() : E->getType();
375 EmitCallArgs(Args, method, E->arg_begin(), E->arg_end());
377 // For delegate init calls in ARC, do an unsafe store of null into
378 // self. This represents the call taking direct ownership of that
379 // value. We have to do this after emitting the other call
380 // arguments because they might also reference self, but we don't
381 // have to worry about any of them modifying self because that would
382 // be an undefined read and write of an object in unordered
384 if (isDelegateInit) {
385 assert(getLangOpts().ObjCAutoRefCount &&
386 "delegate init calls should only be marked in ARC");
388 // Do an unsafe store of null into self.
389 llvm::Value *selfAddr =
390 LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
391 assert(selfAddr && "no self entry for a delegate init call?");
393 Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
397 if (isSuperMessage) {
398 // super is only valid in an Objective-C method
399 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
400 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
401 result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
403 OMD->getClassInterface(),
410 result = Runtime.GenerateMessageSend(*this, Return, ResultType,
416 // For delegate init calls in ARC, implicitly store the result of
417 // the call back into self. This takes ownership of the value.
418 if (isDelegateInit) {
419 llvm::Value *selfAddr =
420 LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
421 llvm::Value *newSelf = result.getScalarVal();
423 // The delegate return type isn't necessarily a matching type; in
424 // fact, it's quite likely to be 'id'.
426 cast<llvm::PointerType>(selfAddr->getType())->getElementType();
427 newSelf = Builder.CreateBitCast(newSelf, selfTy);
429 Builder.CreateStore(newSelf, selfAddr);
432 return AdjustRelatedResultType(*this, E->getType(), method, result);
436 struct FinishARCDealloc : EHScopeStack::Cleanup {
437 void Emit(CodeGenFunction &CGF, Flags flags) {
438 const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
440 const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
441 const ObjCInterfaceDecl *iface = impl->getClassInterface();
442 if (!iface->getSuperClass()) return;
444 bool isCategory = isa<ObjCCategoryImplDecl>(impl);
446 // Call [super dealloc] if we have a superclass.
447 llvm::Value *self = CGF.LoadObjCSelf();
450 CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
451 CGF.getContext().VoidTy,
452 method->getSelector(),
456 /*is class msg*/ false,
463 /// StartObjCMethod - Begin emission of an ObjCMethod. This generates
464 /// the LLVM function and sets the other context used by
466 void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
467 const ObjCContainerDecl *CD,
468 SourceLocation StartLoc) {
469 FunctionArgList args;
470 // Check if we should generate debug info for this method.
471 if (!OMD->hasAttr<NoDebugAttr>())
472 maybeInitializeDebugInfo();
474 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
476 const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD);
477 CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
479 args.push_back(OMD->getSelfDecl());
480 args.push_back(OMD->getCmdDecl());
482 for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(),
483 E = OMD->param_end(); PI != E; ++PI)
488 StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc);
490 // In ARC, certain methods get an extra cleanup.
491 if (CGM.getLangOpts().ObjCAutoRefCount &&
492 OMD->isInstanceMethod() &&
493 OMD->getSelector().isUnarySelector()) {
494 const IdentifierInfo *ident =
495 OMD->getSelector().getIdentifierInfoForSlot(0);
496 if (ident->isStr("dealloc"))
497 EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
501 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
502 LValue lvalue, QualType type);
504 /// Generate an Objective-C method. An Objective-C method is a C function with
505 /// its pointer, name, and types registered in the class struture.
506 void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
507 StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
508 EmitStmt(OMD->getBody());
509 FinishFunction(OMD->getBodyRBrace());
512 /// emitStructGetterCall - Call the runtime function to load a property
513 /// into the return value slot.
514 static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar,
515 bool isAtomic, bool hasStrong) {
516 ASTContext &Context = CGF.getContext();
519 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(),
520 ivar, 0).getAddress();
522 // objc_copyStruct (ReturnValue, &structIvar,
523 // sizeof (Type of Ivar), isAtomic, false);
526 llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy);
527 args.add(RValue::get(dest), Context.VoidPtrTy);
529 src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy);
530 args.add(RValue::get(src), Context.VoidPtrTy);
532 CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType());
533 args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType());
534 args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy);
535 args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy);
537 llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction();
538 CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args,
539 FunctionType::ExtInfo(),
541 fn, ReturnValueSlot(), args);
544 /// Determine whether the given architecture supports unaligned atomic
545 /// accesses. They don't have to be fast, just faster than a function
546 /// call and a mutex.
547 static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) {
548 // FIXME: Allow unaligned atomic load/store on x86. (It is not
549 // currently supported by the backend.)
553 /// Return the maximum size that permits atomic accesses for the given
555 static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM,
556 llvm::Triple::ArchType arch) {
557 // ARM has 8-byte atomic accesses, but it's not clear whether we
558 // want to rely on them here.
560 // In the default case, just assume that any size up to a pointer is
561 // fine given adequate alignment.
562 return CharUnits::fromQuantity(CGM.PointerSizeInBytes);
566 class PropertyImplStrategy {
569 /// The 'native' strategy is to use the architecture's provided
570 /// reads and writes.
573 /// Use objc_setProperty and objc_getProperty.
576 /// Use objc_setProperty for the setter, but use expression
577 /// evaluation for the getter.
578 SetPropertyAndExpressionGet,
580 /// Use objc_copyStruct.
583 /// The 'expression' strategy is to emit normal assignment or
584 /// lvalue-to-rvalue expressions.
588 StrategyKind getKind() const { return StrategyKind(Kind); }
590 bool hasStrongMember() const { return HasStrong; }
591 bool isAtomic() const { return IsAtomic; }
592 bool isCopy() const { return IsCopy; }
594 CharUnits getIvarSize() const { return IvarSize; }
595 CharUnits getIvarAlignment() const { return IvarAlignment; }
597 PropertyImplStrategy(CodeGenModule &CGM,
598 const ObjCPropertyImplDecl *propImpl);
602 unsigned IsAtomic : 1;
604 unsigned HasStrong : 1;
607 CharUnits IvarAlignment;
611 /// Pick an implementation strategy for the given property synthesis.
612 PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM,
613 const ObjCPropertyImplDecl *propImpl) {
614 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
615 ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind();
617 IsCopy = (setterKind == ObjCPropertyDecl::Copy);
618 IsAtomic = prop->isAtomic();
619 HasStrong = false; // doesn't matter here.
621 // Evaluate the ivar's size and alignment.
622 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
623 QualType ivarType = ivar->getType();
624 llvm::tie(IvarSize, IvarAlignment)
625 = CGM.getContext().getTypeInfoInChars(ivarType);
627 // If we have a copy property, we always have to use getProperty/setProperty.
628 // TODO: we could actually use setProperty and an expression for non-atomics.
630 Kind = GetSetProperty;
635 if (setterKind == ObjCPropertyDecl::Retain) {
636 // In GC-only, there's nothing special that needs to be done.
637 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
640 // In ARC, if the property is non-atomic, use expression emission,
641 // which translates to objc_storeStrong. This isn't required, but
642 // it's slightly nicer.
643 } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) {
644 // Using standard expression emission for the setter is only
645 // acceptable if the ivar is __strong, which won't be true if
646 // the property is annotated with __attribute__((NSObject)).
647 // TODO: falling all the way back to objc_setProperty here is
648 // just laziness, though; we could still use objc_storeStrong
649 // if we hacked it right.
650 if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong)
653 Kind = SetPropertyAndExpressionGet;
656 // Otherwise, we need to at least use setProperty. However, if
657 // the property isn't atomic, we can use normal expression
658 // emission for the getter.
659 } else if (!IsAtomic) {
660 Kind = SetPropertyAndExpressionGet;
663 // Otherwise, we have to use both setProperty and getProperty.
665 Kind = GetSetProperty;
670 // If we're not atomic, just use expression accesses.
676 // Properties on bitfield ivars need to be emitted using expression
677 // accesses even if they're nominally atomic.
678 if (ivar->isBitField()) {
683 // GC-qualified or ARC-qualified ivars need to be emitted as
684 // expressions. This actually works out to being atomic anyway,
685 // except for ARC __strong, but that should trigger the above code.
686 if (ivarType.hasNonTrivialObjCLifetime() ||
687 (CGM.getLangOpts().getGC() &&
688 CGM.getContext().getObjCGCAttrKind(ivarType))) {
693 // Compute whether the ivar has strong members.
694 if (CGM.getLangOpts().getGC())
695 if (const RecordType *recordType = ivarType->getAs<RecordType>())
696 HasStrong = recordType->getDecl()->hasObjectMember();
698 // We can never access structs with object members with a native
699 // access, because we need to use write barriers. This is what
700 // objc_copyStruct is for.
706 // Otherwise, this is target-dependent and based on the size and
707 // alignment of the ivar.
709 // If the size of the ivar is not a power of two, give up. We don't
710 // want to get into the business of doing compare-and-swaps.
711 if (!IvarSize.isPowerOfTwo()) {
716 llvm::Triple::ArchType arch =
717 CGM.getTarget().getTriple().getArch();
719 // Most architectures require memory to fit within a single cache
720 // line, so the alignment has to be at least the size of the access.
721 // Otherwise we have to grab a lock.
722 if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) {
727 // If the ivar's size exceeds the architecture's maximum atomic
728 // access size, we have to use CopyStruct.
729 if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) {
734 // Otherwise, we can use native loads and stores.
738 /// \brief Generate an Objective-C property getter function.
740 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
741 /// is illegal within a category.
742 void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
743 const ObjCPropertyImplDecl *PID) {
744 llvm::Constant *AtomicHelperFn =
745 GenerateObjCAtomicGetterCopyHelperFunction(PID);
746 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
747 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
748 assert(OMD && "Invalid call to generate getter (empty method)");
749 StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart());
751 generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn);
756 static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) {
757 const Expr *getter = propImpl->getGetterCXXConstructor();
758 if (!getter) return true;
760 // Sema only makes only of these when the ivar has a C++ class type,
761 // so the form is pretty constrained.
763 // If the property has a reference type, we might just be binding a
764 // reference, in which case the result will be a gl-value. We should
765 // treat this as a non-trivial operation.
766 if (getter->isGLValue())
769 // If we selected a trivial copy-constructor, we're okay.
770 if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter))
771 return (construct->getConstructor()->isTrivial());
773 // The constructor might require cleanups (in which case it's never
775 assert(isa<ExprWithCleanups>(getter));
779 /// emitCPPObjectAtomicGetterCall - Call the runtime function to
780 /// copy the ivar into the resturn slot.
781 static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF,
782 llvm::Value *returnAddr,
784 llvm::Constant *AtomicHelperFn) {
785 // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar,
789 // The 1st argument is the return Slot.
790 args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy);
792 // The 2nd argument is the address of the ivar.
793 llvm::Value *ivarAddr =
794 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
795 CGF.LoadObjCSelf(), ivar, 0).getAddress();
796 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
797 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
799 // Third argument is the helper function.
800 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
802 llvm::Value *copyCppAtomicObjectFn =
803 CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction();
804 CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
806 FunctionType::ExtInfo(),
808 copyCppAtomicObjectFn, ReturnValueSlot(), args);
812 CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
813 const ObjCPropertyImplDecl *propImpl,
814 const ObjCMethodDecl *GetterMethodDecl,
815 llvm::Constant *AtomicHelperFn) {
816 // If there's a non-trivial 'get' expression, we just have to emit that.
817 if (!hasTrivialGetExpr(propImpl)) {
818 if (!AtomicHelperFn) {
819 ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(),
824 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
825 emitCPPObjectAtomicGetterCall(*this, ReturnValue,
826 ivar, AtomicHelperFn);
831 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
832 QualType propType = prop->getType();
833 ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl();
835 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
837 // Pick an implementation strategy.
838 PropertyImplStrategy strategy(CGM, propImpl);
839 switch (strategy.getKind()) {
840 case PropertyImplStrategy::Native: {
841 // We don't need to do anything for a zero-size struct.
842 if (strategy.getIvarSize().isZero())
845 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
847 // Currently, all atomic accesses have to be through integer
848 // types, so there's no point in trying to pick a prettier type.
849 llvm::Type *bitcastType =
850 llvm::Type::getIntNTy(getLLVMContext(),
851 getContext().toBits(strategy.getIvarSize()));
852 bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
854 // Perform an atomic load. This does not impose ordering constraints.
855 llvm::Value *ivarAddr = LV.getAddress();
856 ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
857 llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load");
858 load->setAlignment(strategy.getIvarAlignment().getQuantity());
859 load->setAtomic(llvm::Unordered);
861 // Store that value into the return address. Doing this with a
862 // bitcast is likely to produce some pretty ugly IR, but it's not
863 // the *most* terrible thing in the world.
864 Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType));
866 // Make sure we don't do an autorelease.
867 AutoreleaseResult = false;
871 case PropertyImplStrategy::GetSetProperty: {
872 llvm::Value *getPropertyFn =
873 CGM.getObjCRuntime().GetPropertyGetFunction();
874 if (!getPropertyFn) {
875 CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy");
879 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
880 // FIXME: Can't this be simpler? This might even be worse than the
881 // corresponding gcc code.
883 Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd");
884 llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
885 llvm::Value *ivarOffset =
886 EmitIvarOffset(classImpl->getClassInterface(), ivar);
889 args.add(RValue::get(self), getContext().getObjCIdType());
890 args.add(RValue::get(cmd), getContext().getObjCSelType());
891 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
892 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
893 getContext().BoolTy);
895 // FIXME: We shouldn't need to get the function info here, the
896 // runtime already should have computed it to build the function.
897 RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args,
898 FunctionType::ExtInfo(),
900 getPropertyFn, ReturnValueSlot(), args);
902 // We need to fix the type here. Ivars with copy & retain are
903 // always objects so we don't need to worry about complex or
905 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
906 getTypes().ConvertType(getterMethod->getResultType())));
908 EmitReturnOfRValue(RV, propType);
910 // objc_getProperty does an autorelease, so we should suppress ours.
911 AutoreleaseResult = false;
916 case PropertyImplStrategy::CopyStruct:
917 emitStructGetterCall(*this, ivar, strategy.isAtomic(),
918 strategy.hasStrongMember());
921 case PropertyImplStrategy::Expression:
922 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
923 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0);
925 QualType ivarType = ivar->getType();
926 switch (getEvaluationKind(ivarType)) {
928 ComplexPairTy pair = EmitLoadOfComplex(LV);
929 EmitStoreOfComplex(pair,
930 MakeNaturalAlignAddrLValue(ReturnValue, ivarType),
935 // The return value slot is guaranteed to not be aliased, but
936 // that's not necessarily the same as "on the stack", so
937 // we still potentially need objc_memmove_collectable.
938 EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType);
942 if (propType->isReferenceType()) {
943 value = LV.getAddress();
945 // We want to load and autoreleaseReturnValue ARC __weak ivars.
946 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
947 value = emitARCRetainLoadOfScalar(*this, LV, ivarType);
949 // Otherwise we want to do a simple load, suppressing the
950 // final autorelease.
952 value = EmitLoadOfLValue(LV).getScalarVal();
953 AutoreleaseResult = false;
956 value = Builder.CreateBitCast(value, ConvertType(propType));
957 value = Builder.CreateBitCast(value,
958 ConvertType(GetterMethodDecl->getResultType()));
961 EmitReturnOfRValue(RValue::get(value), propType);
965 llvm_unreachable("bad evaluation kind");
969 llvm_unreachable("bad @property implementation strategy!");
972 /// emitStructSetterCall - Call the runtime function to store the value
973 /// from the first formal parameter into the given ivar.
974 static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD,
975 ObjCIvarDecl *ivar) {
976 // objc_copyStruct (&structIvar, &Arg,
977 // sizeof (struct something), true, false);
980 // The first argument is the address of the ivar.
981 llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
982 CGF.LoadObjCSelf(), ivar, 0)
984 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
985 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
987 // The second argument is the address of the parameter variable.
988 ParmVarDecl *argVar = *OMD->param_begin();
989 DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
990 VK_LValue, SourceLocation());
991 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
992 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
993 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
995 // The third argument is the sizeof the type.
997 CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType()));
998 args.add(RValue::get(size), CGF.getContext().getSizeType());
1000 // The fourth argument is the 'isAtomic' flag.
1001 args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy);
1003 // The fifth argument is the 'hasStrong' flag.
1004 // FIXME: should this really always be false?
1005 args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy);
1007 llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction();
1008 CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
1010 FunctionType::ExtInfo(),
1012 copyStructFn, ReturnValueSlot(), args);
1015 /// emitCPPObjectAtomicSetterCall - Call the runtime function to store
1016 /// the value from the first formal parameter into the given ivar, using
1017 /// the Cpp API for atomic Cpp objects with non-trivial copy assignment.
1018 static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF,
1019 ObjCMethodDecl *OMD,
1021 llvm::Constant *AtomicHelperFn) {
1022 // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg,
1026 // The first argument is the address of the ivar.
1027 llvm::Value *ivarAddr =
1028 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(),
1029 CGF.LoadObjCSelf(), ivar, 0).getAddress();
1030 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy);
1031 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy);
1033 // The second argument is the address of the parameter variable.
1034 ParmVarDecl *argVar = *OMD->param_begin();
1035 DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(),
1036 VK_LValue, SourceLocation());
1037 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress();
1038 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy);
1039 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy);
1041 // Third argument is the helper function.
1042 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy);
1044 llvm::Value *copyCppAtomicObjectFn =
1045 CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction();
1046 CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy,
1048 FunctionType::ExtInfo(),
1050 copyCppAtomicObjectFn, ReturnValueSlot(), args);
1056 static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) {
1057 Expr *setter = PID->getSetterCXXAssignment();
1058 if (!setter) return true;
1060 // Sema only makes only of these when the ivar has a C++ class type,
1061 // so the form is pretty constrained.
1063 // An operator call is trivial if the function it calls is trivial.
1064 // This also implies that there's nothing non-trivial going on with
1065 // the arguments, because operator= can only be trivial if it's a
1066 // synthesized assignment operator and therefore both parameters are
1068 if (CallExpr *call = dyn_cast<CallExpr>(setter)) {
1069 if (const FunctionDecl *callee
1070 = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl()))
1071 if (callee->isTrivial())
1076 assert(isa<ExprWithCleanups>(setter));
1080 static bool UseOptimizedSetter(CodeGenModule &CGM) {
1081 if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
1083 return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter();
1087 CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1088 const ObjCPropertyImplDecl *propImpl,
1089 llvm::Constant *AtomicHelperFn) {
1090 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl();
1091 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl();
1092 ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl();
1094 // Just use the setter expression if Sema gave us one and it's
1096 if (!hasTrivialSetExpr(propImpl)) {
1097 if (!AtomicHelperFn)
1098 // If non-atomic, assignment is called directly.
1099 EmitStmt(propImpl->getSetterCXXAssignment());
1101 // If atomic, assignment is called via a locking api.
1102 emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar,
1107 PropertyImplStrategy strategy(CGM, propImpl);
1108 switch (strategy.getKind()) {
1109 case PropertyImplStrategy::Native: {
1110 // We don't need to do anything for a zero-size struct.
1111 if (strategy.getIvarSize().isZero())
1114 llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()];
1117 EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0);
1118 llvm::Value *ivarAddr = ivarLValue.getAddress();
1120 // Currently, all atomic accesses have to be through integer
1121 // types, so there's no point in trying to pick a prettier type.
1122 llvm::Type *bitcastType =
1123 llvm::Type::getIntNTy(getLLVMContext(),
1124 getContext().toBits(strategy.getIvarSize()));
1125 bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay
1127 // Cast both arguments to the chosen operation type.
1128 argAddr = Builder.CreateBitCast(argAddr, bitcastType);
1129 ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType);
1131 // This bitcast load is likely to cause some nasty IR.
1132 llvm::Value *load = Builder.CreateLoad(argAddr);
1134 // Perform an atomic store. There are no memory ordering requirements.
1135 llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr);
1136 store->setAlignment(strategy.getIvarAlignment().getQuantity());
1137 store->setAtomic(llvm::Unordered);
1141 case PropertyImplStrategy::GetSetProperty:
1142 case PropertyImplStrategy::SetPropertyAndExpressionGet: {
1144 llvm::Value *setOptimizedPropertyFn = 0;
1145 llvm::Value *setPropertyFn = 0;
1146 if (UseOptimizedSetter(CGM)) {
1147 // 10.8 and iOS 6.0 code and GC is off
1148 setOptimizedPropertyFn =
1149 CGM.getObjCRuntime()
1150 .GetOptimizedPropertySetFunction(strategy.isAtomic(),
1152 if (!setOptimizedPropertyFn) {
1153 CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI");
1158 setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction();
1159 if (!setPropertyFn) {
1160 CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy");
1165 // Emit objc_setProperty((id) self, _cmd, offset, arg,
1166 // <is-atomic>, <is-copy>).
1168 Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]);
1170 Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy);
1171 llvm::Value *ivarOffset =
1172 EmitIvarOffset(classImpl->getClassInterface(), ivar);
1173 llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()];
1174 arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy);
1177 args.add(RValue::get(self), getContext().getObjCIdType());
1178 args.add(RValue::get(cmd), getContext().getObjCSelType());
1179 if (setOptimizedPropertyFn) {
1180 args.add(RValue::get(arg), getContext().getObjCIdType());
1181 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1182 EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args,
1183 FunctionType::ExtInfo(),
1185 setOptimizedPropertyFn, ReturnValueSlot(), args);
1187 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType());
1188 args.add(RValue::get(arg), getContext().getObjCIdType());
1189 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())),
1190 getContext().BoolTy);
1191 args.add(RValue::get(Builder.getInt1(strategy.isCopy())),
1192 getContext().BoolTy);
1193 // FIXME: We shouldn't need to get the function info here, the runtime
1194 // already should have computed it to build the function.
1195 EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args,
1196 FunctionType::ExtInfo(),
1198 setPropertyFn, ReturnValueSlot(), args);
1204 case PropertyImplStrategy::CopyStruct:
1205 emitStructSetterCall(*this, setterMethod, ivar);
1208 case PropertyImplStrategy::Expression:
1212 // Otherwise, fake up some ASTs and emit a normal assignment.
1213 ValueDecl *selfDecl = setterMethod->getSelfDecl();
1214 DeclRefExpr self(selfDecl, false, selfDecl->getType(),
1215 VK_LValue, SourceLocation());
1216 ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack,
1217 selfDecl->getType(), CK_LValueToRValue, &self,
1219 ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(),
1220 SourceLocation(), SourceLocation(),
1221 &selfLoad, true, true);
1223 ParmVarDecl *argDecl = *setterMethod->param_begin();
1224 QualType argType = argDecl->getType().getNonReferenceType();
1225 DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation());
1226 ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack,
1227 argType.getUnqualifiedType(), CK_LValueToRValue,
1230 // The property type can differ from the ivar type in some situations with
1231 // Objective-C pointer types, we can always bit cast the RHS in these cases.
1232 // The following absurdity is just to ensure well-formed IR.
1233 CastKind argCK = CK_NoOp;
1234 if (ivarRef.getType()->isObjCObjectPointerType()) {
1235 if (argLoad.getType()->isObjCObjectPointerType())
1237 else if (argLoad.getType()->isBlockPointerType())
1238 argCK = CK_BlockPointerToObjCPointerCast;
1240 argCK = CK_CPointerToObjCPointerCast;
1241 } else if (ivarRef.getType()->isBlockPointerType()) {
1242 if (argLoad.getType()->isBlockPointerType())
1245 argCK = CK_AnyPointerToBlockPointerCast;
1246 } else if (ivarRef.getType()->isPointerType()) {
1249 ImplicitCastExpr argCast(ImplicitCastExpr::OnStack,
1250 ivarRef.getType(), argCK, &argLoad,
1252 Expr *finalArg = &argLoad;
1253 if (!getContext().hasSameUnqualifiedType(ivarRef.getType(),
1255 finalArg = &argCast;
1258 BinaryOperator assign(&ivarRef, finalArg, BO_Assign,
1259 ivarRef.getType(), VK_RValue, OK_Ordinary,
1260 SourceLocation(), false);
1264 /// \brief Generate an Objective-C property setter function.
1266 /// The given Decl must be an ObjCImplementationDecl. \@synthesize
1267 /// is illegal within a category.
1268 void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
1269 const ObjCPropertyImplDecl *PID) {
1270 llvm::Constant *AtomicHelperFn =
1271 GenerateObjCAtomicSetterCopyHelperFunction(PID);
1272 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1273 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
1274 assert(OMD && "Invalid call to generate setter (empty method)");
1275 StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart());
1277 generateObjCSetterBody(IMP, PID, AtomicHelperFn);
1283 struct DestroyIvar : EHScopeStack::Cleanup {
1286 const ObjCIvarDecl *ivar;
1287 CodeGenFunction::Destroyer *destroyer;
1288 bool useEHCleanupForArray;
1290 DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
1291 CodeGenFunction::Destroyer *destroyer,
1292 bool useEHCleanupForArray)
1293 : addr(addr), ivar(ivar), destroyer(destroyer),
1294 useEHCleanupForArray(useEHCleanupForArray) {}
1296 void Emit(CodeGenFunction &CGF, Flags flags) {
1298 = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
1299 CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
1300 flags.isForNormalCleanup() && useEHCleanupForArray);
1305 /// Like CodeGenFunction::destroyARCStrong, but do it with a call.
1306 static void destroyARCStrongWithStore(CodeGenFunction &CGF,
1309 llvm::Value *null = getNullForVariable(addr);
1310 CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
1313 static void emitCXXDestructMethod(CodeGenFunction &CGF,
1314 ObjCImplementationDecl *impl) {
1315 CodeGenFunction::RunCleanupsScope scope(CGF);
1317 llvm::Value *self = CGF.LoadObjCSelf();
1319 const ObjCInterfaceDecl *iface = impl->getClassInterface();
1320 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
1321 ivar; ivar = ivar->getNextIvar()) {
1322 QualType type = ivar->getType();
1324 // Check whether the ivar is a destructible type.
1325 QualType::DestructionKind dtorKind = type.isDestructedType();
1326 if (!dtorKind) continue;
1328 CodeGenFunction::Destroyer *destroyer = 0;
1330 // Use a call to objc_storeStrong to destroy strong ivars, for the
1331 // general benefit of the tools.
1332 if (dtorKind == QualType::DK_objc_strong_lifetime) {
1333 destroyer = destroyARCStrongWithStore;
1335 // Otherwise use the default for the destruction kind.
1337 destroyer = CGF.getDestroyer(dtorKind);
1340 CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
1342 CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
1343 cleanupKind & EHCleanup);
1346 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
1349 void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1352 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
1353 StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
1355 // Emit .cxx_construct.
1357 // Suppress the final autorelease in ARC.
1358 AutoreleaseResult = false;
1360 SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
1361 for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
1362 E = IMP->init_end(); B != E; ++B) {
1363 CXXCtorInitializer *IvarInit = (*B);
1364 FieldDecl *Field = IvarInit->getAnyMember();
1365 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1366 LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
1367 LoadObjCSelf(), Ivar, 0);
1368 EmitAggExpr(IvarInit->getInit(),
1369 AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed,
1370 AggValueSlot::DoesNotNeedGCBarriers,
1371 AggValueSlot::IsNotAliased));
1373 // constructor returns 'self'.
1374 CodeGenTypes &Types = CGM.getTypes();
1375 QualType IdTy(CGM.getContext().getObjCIdType());
1376 llvm::Value *SelfAsId =
1377 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
1378 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
1380 // Emit .cxx_destruct.
1382 emitCXXDestructMethod(*this, IMP);
1387 bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
1388 CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
1390 const ABIArgInfo &AI = it->info;
1391 // FIXME. Is this sufficient check?
1392 return (AI.getKind() == ABIArgInfo::Indirect);
1395 bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
1396 if (CGM.getLangOpts().getGC() == LangOptions::NonGC)
1398 if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
1399 return FDTTy->getDecl()->hasObjectMember();
1403 llvm::Value *CodeGenFunction::LoadObjCSelf() {
1404 VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl();
1405 DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl),
1406 Self->getType(), VK_LValue, SourceLocation());
1407 return EmitLoadOfScalar(EmitDeclRefLValue(&DRE));
1410 QualType CodeGenFunction::TypeOfSelfObject() {
1411 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
1412 ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
1413 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
1414 getContext().getCanonicalType(selfDecl->getType()));
1415 return PTy->getPointeeType();
1418 void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
1419 llvm::Constant *EnumerationMutationFn =
1420 CGM.getObjCRuntime().EnumerationMutationFunction();
1422 if (!EnumerationMutationFn) {
1423 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
1427 CGDebugInfo *DI = getDebugInfo();
1429 DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
1431 // The local variable comes into scope immediately.
1432 AutoVarEmission variable = AutoVarEmission::invalid();
1433 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
1434 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
1436 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
1438 // Fast enumeration state.
1439 QualType StateTy = CGM.getObjCFastEnumerationStateType();
1440 llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
1441 EmitNullInitialization(StatePtr, StateTy);
1443 // Number of elements in the items array.
1444 static const unsigned NumItems = 16;
1446 // Fetch the countByEnumeratingWithState:objects:count: selector.
1447 IdentifierInfo *II[] = {
1448 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
1449 &CGM.getContext().Idents.get("objects"),
1450 &CGM.getContext().Idents.get("count")
1452 Selector FastEnumSel =
1453 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
1456 getContext().getConstantArrayType(getContext().getObjCIdType(),
1457 llvm::APInt(32, NumItems),
1458 ArrayType::Normal, 0);
1459 llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
1461 // Emit the collection pointer. In ARC, we do a retain.
1462 llvm::Value *Collection;
1463 if (getLangOpts().ObjCAutoRefCount) {
1464 Collection = EmitARCRetainScalarExpr(S.getCollection());
1466 // Enter a cleanup to do the release.
1467 EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
1469 Collection = EmitScalarExpr(S.getCollection());
1472 // The 'continue' label needs to appear within the cleanup for the
1473 // collection object.
1474 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
1476 // Send it our message:
1479 // The first argument is a temporary of the enumeration-state type.
1480 Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
1482 // The second argument is a temporary array with space for NumItems
1483 // pointers. We'll actually be loading elements from the array
1484 // pointer written into the control state; this buffer is so that
1485 // collections that *aren't* backed by arrays can still queue up
1486 // batches of elements.
1487 Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
1489 // The third argument is the capacity of that temporary array.
1490 llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
1491 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
1492 Args.add(RValue::get(Count), getContext().UnsignedLongTy);
1494 // Start the enumeration.
1496 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1497 getContext().UnsignedLongTy,
1501 // The initial number of objects that were returned in the buffer.
1502 llvm::Value *initialBufferLimit = CountRV.getScalarVal();
1504 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
1505 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
1507 llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
1509 // If the limit pointer was zero to begin with, the collection is
1510 // empty; skip all this.
1511 Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
1512 EmptyBB, LoopInitBB);
1514 // Otherwise, initialize the loop.
1515 EmitBlock(LoopInitBB);
1517 // Save the initial mutations value. This is the value at an
1518 // address that was written into the state object by
1519 // countByEnumeratingWithState:objects:count:.
1520 llvm::Value *StateMutationsPtrPtr =
1521 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
1522 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
1525 llvm::Value *initialMutations =
1526 Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
1528 // Start looping. This is the point we return to whenever we have a
1529 // fresh, non-empty batch of objects.
1530 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
1531 EmitBlock(LoopBodyBB);
1533 // The current index into the buffer.
1534 llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
1535 index->addIncoming(zero, LoopInitBB);
1537 // The current buffer size.
1538 llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
1539 count->addIncoming(initialBufferLimit, LoopInitBB);
1541 // Check whether the mutations value has changed from where it was
1542 // at start. StateMutationsPtr should actually be invariant between
1544 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
1545 llvm::Value *currentMutations
1546 = Builder.CreateLoad(StateMutationsPtr, "statemutations");
1548 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
1549 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
1551 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
1552 WasNotMutatedBB, WasMutatedBB);
1554 // If so, call the enumeration-mutation function.
1555 EmitBlock(WasMutatedBB);
1557 Builder.CreateBitCast(Collection,
1558 ConvertType(getContext().getObjCIdType()));
1560 Args2.add(RValue::get(V), getContext().getObjCIdType());
1561 // FIXME: We shouldn't need to get the function info here, the runtime already
1562 // should have computed it to build the function.
1563 EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2,
1564 FunctionType::ExtInfo(),
1566 EnumerationMutationFn, ReturnValueSlot(), Args2);
1568 // Otherwise, or if the mutation function returns, just continue.
1569 EmitBlock(WasNotMutatedBB);
1571 // Initialize the element variable.
1572 RunCleanupsScope elementVariableScope(*this);
1573 bool elementIsVariable;
1574 LValue elementLValue;
1575 QualType elementType;
1576 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
1577 // Initialize the variable, in case it's a __block variable or something.
1578 EmitAutoVarInit(variable);
1580 const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
1581 DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(),
1582 VK_LValue, SourceLocation());
1583 elementLValue = EmitLValue(&tempDRE);
1584 elementType = D->getType();
1585 elementIsVariable = true;
1587 if (D->isARCPseudoStrong())
1588 elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
1590 elementLValue = LValue(); // suppress warning
1591 elementType = cast<Expr>(S.getElement())->getType();
1592 elementIsVariable = false;
1594 llvm::Type *convertedElementType = ConvertType(elementType);
1596 // Fetch the buffer out of the enumeration state.
1597 // TODO: this pointer should actually be invariant between
1598 // refreshes, which would help us do certain loop optimizations.
1599 llvm::Value *StateItemsPtr =
1600 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
1601 llvm::Value *EnumStateItems =
1602 Builder.CreateLoad(StateItemsPtr, "stateitems");
1604 // Fetch the value at the current index from the buffer.
1605 llvm::Value *CurrentItemPtr =
1606 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
1607 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
1609 // Cast that value to the right type.
1610 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
1613 // Make sure we have an l-value. Yes, this gets evaluated every
1614 // time through the loop.
1615 if (!elementIsVariable) {
1616 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1617 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
1619 EmitScalarInit(CurrentItem, elementLValue);
1622 // If we do have an element variable, this assignment is the end of
1623 // its initialization.
1624 if (elementIsVariable)
1625 EmitAutoVarCleanups(variable);
1627 // Perform the loop body, setting up break and continue labels.
1628 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
1630 RunCleanupsScope Scope(*this);
1631 EmitStmt(S.getBody());
1633 BreakContinueStack.pop_back();
1635 // Destroy the element variable now.
1636 elementVariableScope.ForceCleanup();
1638 // Check whether there are more elements.
1639 EmitBlock(AfterBody.getBlock());
1641 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
1643 // First we check in the local buffer.
1644 llvm::Value *indexPlusOne
1645 = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
1647 // If we haven't overrun the buffer yet, we can continue.
1648 Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
1649 LoopBodyBB, FetchMoreBB);
1651 index->addIncoming(indexPlusOne, AfterBody.getBlock());
1652 count->addIncoming(count, AfterBody.getBlock());
1654 // Otherwise, we have to fetch more elements.
1655 EmitBlock(FetchMoreBB);
1658 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
1659 getContext().UnsignedLongTy,
1663 // If we got a zero count, we're done.
1664 llvm::Value *refetchCount = CountRV.getScalarVal();
1666 // (note that the message send might split FetchMoreBB)
1667 index->addIncoming(zero, Builder.GetInsertBlock());
1668 count->addIncoming(refetchCount, Builder.GetInsertBlock());
1670 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
1671 EmptyBB, LoopBodyBB);
1673 // No more elements.
1676 if (!elementIsVariable) {
1677 // If the element was not a declaration, set it to be null.
1679 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
1680 elementLValue = EmitLValue(cast<Expr>(S.getElement()));
1681 EmitStoreThroughLValue(RValue::get(null), elementLValue);
1685 DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
1687 // Leave the cleanup we entered in ARC.
1688 if (getLangOpts().ObjCAutoRefCount)
1691 EmitBlock(LoopEnd.getBlock());
1694 void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
1695 CGM.getObjCRuntime().EmitTryStmt(*this, S);
1698 void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
1699 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
1702 void CodeGenFunction::EmitObjCAtSynchronizedStmt(
1703 const ObjCAtSynchronizedStmt &S) {
1704 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
1707 /// Produce the code for a CK_ARCProduceObject. Just does a
1708 /// primitive retain.
1709 llvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type,
1710 llvm::Value *value) {
1711 return EmitARCRetain(type, value);
1715 struct CallObjCRelease : EHScopeStack::Cleanup {
1716 CallObjCRelease(llvm::Value *object) : object(object) {}
1717 llvm::Value *object;
1719 void Emit(CodeGenFunction &CGF, Flags flags) {
1720 // Releases at the end of the full-expression are imprecise.
1721 CGF.EmitARCRelease(object, ARCImpreciseLifetime);
1726 /// Produce the code for a CK_ARCConsumeObject. Does a primitive
1727 /// release at the end of the full-expression.
1728 llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
1729 llvm::Value *object) {
1730 // If we're in a conditional branch, we need to make the cleanup
1732 pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
1736 llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
1737 llvm::Value *value) {
1738 return EmitARCRetainAutorelease(type, value);
1741 /// Given a number of pointers, inform the optimizer that they're
1742 /// being intrinsically used up until this point in the program.
1743 void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) {
1744 llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use;
1746 llvm::FunctionType *fnType =
1747 llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true);
1748 fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use");
1751 // This isn't really a "runtime" function, but as an intrinsic it
1752 // doesn't really matter as long as we align things up.
1753 EmitNounwindRuntimeCall(fn, values);
1757 static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
1758 llvm::FunctionType *type,
1760 llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName);
1762 if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) {
1763 // If the target runtime doesn't naturally support ARC, emit weak
1764 // references to the runtime support library. We don't really
1765 // permit this to fail, but we need a particular relocation style.
1766 if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
1767 f->setLinkage(llvm::Function::ExternalWeakLinkage);
1768 } else if (fnName == "objc_retain" || fnName == "objc_release") {
1769 // If we have Native ARC, set nonlazybind attribute for these APIs for
1771 f->addFnAttr(llvm::Attribute::NonLazyBind);
1778 /// Perform an operation having the signature
1780 /// where a null input causes a no-op and returns null.
1781 static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
1783 llvm::Constant *&fn,
1785 bool isTailCall = false) {
1786 if (isa<llvm::ConstantPointerNull>(value)) return value;
1789 llvm::FunctionType *fnType =
1790 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false);
1791 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1794 // Cast the argument to 'id'.
1795 llvm::Type *origType = value->getType();
1796 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
1798 // Call the function.
1799 llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value);
1801 call->setTailCall();
1803 // Cast the result back to the original type.
1804 return CGF.Builder.CreateBitCast(call, origType);
1807 /// Perform an operation having the following signature:
1809 static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
1811 llvm::Constant *&fn,
1814 llvm::FunctionType *fnType =
1815 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false);
1816 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1819 // Cast the argument to 'id*'.
1820 llvm::Type *origType = addr->getType();
1821 addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
1823 // Call the function.
1824 llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr);
1826 // Cast the result back to a dereference of the original type.
1827 if (origType != CGF.Int8PtrPtrTy)
1828 result = CGF.Builder.CreateBitCast(result,
1829 cast<llvm::PointerType>(origType)->getElementType());
1834 /// Perform an operation having the following signature:
1836 static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
1839 llvm::Constant *&fn,
1842 assert(cast<llvm::PointerType>(addr->getType())->getElementType()
1843 == value->getType());
1846 llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy };
1848 llvm::FunctionType *fnType
1849 = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
1850 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1853 llvm::Type *origType = value->getType();
1855 llvm::Value *args[] = {
1856 CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy),
1857 CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy)
1859 llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args);
1861 if (ignored) return 0;
1863 return CGF.Builder.CreateBitCast(result, origType);
1866 /// Perform an operation having the following signature:
1867 /// void (i8**, i8**)
1868 static void emitARCCopyOperation(CodeGenFunction &CGF,
1871 llvm::Constant *&fn,
1873 assert(dst->getType() == src->getType());
1876 llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy };
1878 llvm::FunctionType *fnType
1879 = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
1880 fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
1883 llvm::Value *args[] = {
1884 CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy),
1885 CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy)
1887 CGF.EmitNounwindRuntimeCall(fn, args);
1890 /// Produce the code to do a retain. Based on the type, calls one of:
1891 /// call i8* \@objc_retain(i8* %value)
1892 /// call i8* \@objc_retainBlock(i8* %value)
1893 llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
1894 if (type->isBlockPointerType())
1895 return EmitARCRetainBlock(value, /*mandatory*/ false);
1897 return EmitARCRetainNonBlock(value);
1900 /// Retain the given object, with normal retain semantics.
1901 /// call i8* \@objc_retain(i8* %value)
1902 llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
1903 return emitARCValueOperation(*this, value,
1904 CGM.getARCEntrypoints().objc_retain,
1908 /// Retain the given block, with _Block_copy semantics.
1909 /// call i8* \@objc_retainBlock(i8* %value)
1911 /// \param mandatory - If false, emit the call with metadata
1912 /// indicating that it's okay for the optimizer to eliminate this call
1913 /// if it can prove that the block never escapes except down the stack.
1914 llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value,
1917 = emitARCValueOperation(*this, value,
1918 CGM.getARCEntrypoints().objc_retainBlock,
1919 "objc_retainBlock");
1921 // If the copy isn't mandatory, add !clang.arc.copy_on_escape to
1922 // tell the optimizer that it doesn't need to do this copy if the
1923 // block doesn't escape, where being passed as an argument doesn't
1924 // count as escaping.
1925 if (!mandatory && isa<llvm::Instruction>(result)) {
1926 llvm::CallInst *call
1927 = cast<llvm::CallInst>(result->stripPointerCasts());
1928 assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock);
1930 SmallVector<llvm::Value*,1> args;
1931 call->setMetadata("clang.arc.copy_on_escape",
1932 llvm::MDNode::get(Builder.getContext(), args));
1938 /// Retain the given object which is the result of a function call.
1939 /// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value)
1941 /// Yes, this function name is one character away from a different
1942 /// call with completely different semantics.
1944 CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
1945 // Fetch the void(void) inline asm which marks that we're going to
1946 // retain the autoreleased return value.
1947 llvm::InlineAsm *&marker
1948 = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker;
1951 = CGM.getTargetCodeGenInfo()
1952 .getARCRetainAutoreleasedReturnValueMarker();
1954 // If we have an empty assembly string, there's nothing to do.
1955 if (assembly.empty()) {
1957 // Otherwise, at -O0, build an inline asm that we're going to call
1959 } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1960 llvm::FunctionType *type =
1961 llvm::FunctionType::get(VoidTy, /*variadic*/false);
1963 marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
1965 // If we're at -O1 and above, we don't want to litter the code
1966 // with this marker yet, so leave a breadcrumb for the ARC
1967 // optimizer to pick up.
1969 llvm::NamedMDNode *metadata =
1970 CGM.getModule().getOrInsertNamedMetadata(
1971 "clang.arc.retainAutoreleasedReturnValueMarker");
1972 assert(metadata->getNumOperands() <= 1);
1973 if (metadata->getNumOperands() == 0) {
1974 llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly);
1975 metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string));
1980 // Call the marker asm if we made one, which we do only at -O0.
1981 if (marker) Builder.CreateCall(marker);
1983 return emitARCValueOperation(*this, value,
1984 CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue,
1985 "objc_retainAutoreleasedReturnValue");
1988 /// Release the given object.
1989 /// call void \@objc_release(i8* %value)
1990 void CodeGenFunction::EmitARCRelease(llvm::Value *value,
1991 ARCPreciseLifetime_t precise) {
1992 if (isa<llvm::ConstantPointerNull>(value)) return;
1994 llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release;
1996 llvm::FunctionType *fnType =
1997 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
1998 fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
2001 // Cast the argument to 'id'.
2002 value = Builder.CreateBitCast(value, Int8PtrTy);
2004 // Call objc_release.
2005 llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value);
2007 if (precise == ARCImpreciseLifetime) {
2008 SmallVector<llvm::Value*,1> args;
2009 call->setMetadata("clang.imprecise_release",
2010 llvm::MDNode::get(Builder.getContext(), args));
2014 /// Destroy a __strong variable.
2016 /// At -O0, emit a call to store 'null' into the address;
2017 /// instrumenting tools prefer this because the address is exposed,
2018 /// but it's relatively cumbersome to optimize.
2020 /// At -O1 and above, just load and call objc_release.
2022 /// call void \@objc_storeStrong(i8** %addr, i8* null)
2023 void CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr,
2024 ARCPreciseLifetime_t precise) {
2025 if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2026 llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType());
2027 llvm::Value *null = llvm::ConstantPointerNull::get(
2028 cast<llvm::PointerType>(addrTy->getElementType()));
2029 EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
2033 llvm::Value *value = Builder.CreateLoad(addr);
2034 EmitARCRelease(value, precise);
2037 /// Store into a strong object. Always calls this:
2038 /// call void \@objc_storeStrong(i8** %addr, i8* %value)
2039 llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr,
2042 assert(cast<llvm::PointerType>(addr->getType())->getElementType()
2043 == value->getType());
2045 llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong;
2047 llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
2048 llvm::FunctionType *fnType
2049 = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
2050 fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
2053 llvm::Value *args[] = {
2054 Builder.CreateBitCast(addr, Int8PtrPtrTy),
2055 Builder.CreateBitCast(value, Int8PtrTy)
2057 EmitNounwindRuntimeCall(fn, args);
2059 if (ignored) return 0;
2063 /// Store into a strong object. Sometimes calls this:
2064 /// call void \@objc_storeStrong(i8** %addr, i8* %value)
2065 /// Other times, breaks it down into components.
2066 llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
2067 llvm::Value *newValue,
2069 QualType type = dst.getType();
2070 bool isBlock = type->isBlockPointerType();
2072 // Use a store barrier at -O0 unless this is a block type or the
2073 // lvalue is inadequately aligned.
2074 if (shouldUseFusedARCCalls() &&
2076 (dst.getAlignment().isZero() ||
2077 dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) {
2078 return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
2081 // Otherwise, split it out.
2083 // Retain the new value.
2084 newValue = EmitARCRetain(type, newValue);
2086 // Read the old value.
2087 llvm::Value *oldValue = EmitLoadOfScalar(dst);
2089 // Store. We do this before the release so that any deallocs won't
2090 // see the old value.
2091 EmitStoreOfScalar(newValue, dst);
2093 // Finally, release the old value.
2094 EmitARCRelease(oldValue, dst.isARCPreciseLifetime());
2099 /// Autorelease the given object.
2100 /// call i8* \@objc_autorelease(i8* %value)
2101 llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
2102 return emitARCValueOperation(*this, value,
2103 CGM.getARCEntrypoints().objc_autorelease,
2104 "objc_autorelease");
2107 /// Autorelease the given object.
2108 /// call i8* \@objc_autoreleaseReturnValue(i8* %value)
2110 CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
2111 return emitARCValueOperation(*this, value,
2112 CGM.getARCEntrypoints().objc_autoreleaseReturnValue,
2113 "objc_autoreleaseReturnValue",
2114 /*isTailCall*/ true);
2117 /// Do a fused retain/autorelease of the given object.
2118 /// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value)
2120 CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
2121 return emitARCValueOperation(*this, value,
2122 CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue,
2123 "objc_retainAutoreleaseReturnValue",
2124 /*isTailCall*/ true);
2127 /// Do a fused retain/autorelease of the given object.
2128 /// call i8* \@objc_retainAutorelease(i8* %value)
2130 /// %retain = call i8* \@objc_retainBlock(i8* %value)
2131 /// call i8* \@objc_autorelease(i8* %retain)
2132 llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
2133 llvm::Value *value) {
2134 if (!type->isBlockPointerType())
2135 return EmitARCRetainAutoreleaseNonBlock(value);
2137 if (isa<llvm::ConstantPointerNull>(value)) return value;
2139 llvm::Type *origType = value->getType();
2140 value = Builder.CreateBitCast(value, Int8PtrTy);
2141 value = EmitARCRetainBlock(value, /*mandatory*/ true);
2142 value = EmitARCAutorelease(value);
2143 return Builder.CreateBitCast(value, origType);
2146 /// Do a fused retain/autorelease of the given object.
2147 /// call i8* \@objc_retainAutorelease(i8* %value)
2149 CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
2150 return emitARCValueOperation(*this, value,
2151 CGM.getARCEntrypoints().objc_retainAutorelease,
2152 "objc_retainAutorelease");
2155 /// i8* \@objc_loadWeak(i8** %addr)
2156 /// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
2157 llvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) {
2158 return emitARCLoadOperation(*this, addr,
2159 CGM.getARCEntrypoints().objc_loadWeak,
2163 /// i8* \@objc_loadWeakRetained(i8** %addr)
2164 llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) {
2165 return emitARCLoadOperation(*this, addr,
2166 CGM.getARCEntrypoints().objc_loadWeakRetained,
2167 "objc_loadWeakRetained");
2170 /// i8* \@objc_storeWeak(i8** %addr, i8* %value)
2172 llvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr,
2175 return emitARCStoreOperation(*this, addr, value,
2176 CGM.getARCEntrypoints().objc_storeWeak,
2177 "objc_storeWeak", ignored);
2180 /// i8* \@objc_initWeak(i8** %addr, i8* %value)
2181 /// Returns %value. %addr is known to not have a current weak entry.
2182 /// Essentially equivalent to:
2183 /// *addr = nil; objc_storeWeak(addr, value);
2184 void CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) {
2185 // If we're initializing to null, just write null to memory; no need
2186 // to get the runtime involved. But don't do this if optimization
2187 // is enabled, because accounting for this would make the optimizer
2188 // much more complicated.
2189 if (isa<llvm::ConstantPointerNull>(value) &&
2190 CGM.getCodeGenOpts().OptimizationLevel == 0) {
2191 Builder.CreateStore(value, addr);
2195 emitARCStoreOperation(*this, addr, value,
2196 CGM.getARCEntrypoints().objc_initWeak,
2197 "objc_initWeak", /*ignored*/ true);
2200 /// void \@objc_destroyWeak(i8** %addr)
2201 /// Essentially objc_storeWeak(addr, nil).
2202 void CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) {
2203 llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak;
2205 llvm::FunctionType *fnType =
2206 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false);
2207 fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
2210 // Cast the argument to 'id*'.
2211 addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
2213 EmitNounwindRuntimeCall(fn, addr);
2216 /// void \@objc_moveWeak(i8** %dest, i8** %src)
2217 /// Disregards the current value in %dest. Leaves %src pointing to nothing.
2218 /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
2219 void CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) {
2220 emitARCCopyOperation(*this, dst, src,
2221 CGM.getARCEntrypoints().objc_moveWeak,
2225 /// void \@objc_copyWeak(i8** %dest, i8** %src)
2226 /// Disregards the current value in %dest. Essentially
2227 /// objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
2228 void CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) {
2229 emitARCCopyOperation(*this, dst, src,
2230 CGM.getARCEntrypoints().objc_copyWeak,
2234 /// Produce the code to do a objc_autoreleasepool_push.
2235 /// call i8* \@objc_autoreleasePoolPush(void)
2236 llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
2237 llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush;
2239 llvm::FunctionType *fnType =
2240 llvm::FunctionType::get(Int8PtrTy, false);
2241 fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
2244 return EmitNounwindRuntimeCall(fn);
2247 /// Produce the code to do a primitive release.
2248 /// call void \@objc_autoreleasePoolPop(i8* %ptr)
2249 void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
2250 assert(value->getType() == Int8PtrTy);
2252 llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop;
2254 llvm::FunctionType *fnType =
2255 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false);
2257 // We don't want to use a weak import here; instead we should not
2258 // fall into this path.
2259 fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
2262 // objc_autoreleasePoolPop can throw.
2263 EmitRuntimeCallOrInvoke(fn, value);
2266 /// Produce the code to do an MRR version objc_autoreleasepool_push.
2267 /// Which is: [[NSAutoreleasePool alloc] init];
2268 /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
2269 /// init is declared as: - (id) init; in its NSObject super class.
2271 llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
2272 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2273 llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this);
2274 // [NSAutoreleasePool alloc]
2275 IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
2276 Selector AllocSel = getContext().Selectors.getSelector(0, &II);
2279 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2280 getContext().getObjCIdType(),
2281 AllocSel, Receiver, Args);
2284 Receiver = AllocRV.getScalarVal();
2285 II = &CGM.getContext().Idents.get("init");
2286 Selector InitSel = getContext().Selectors.getSelector(0, &II);
2288 Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
2289 getContext().getObjCIdType(),
2290 InitSel, Receiver, Args);
2291 return InitRV.getScalarVal();
2294 /// Produce the code to do a primitive release.
2296 void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
2297 IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
2298 Selector DrainSel = getContext().Selectors.getSelector(0, &II);
2300 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
2301 getContext().VoidTy, DrainSel, Arg, Args);
2304 void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
2307 CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime);
2310 void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
2313 CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime);
2316 void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
2319 CGF.EmitARCDestroyWeak(addr);
2323 struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup {
2326 CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2328 void Emit(CodeGenFunction &CGF, Flags flags) {
2329 CGF.EmitObjCAutoreleasePoolPop(Token);
2332 struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup {
2335 CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
2337 void Emit(CodeGenFunction &CGF, Flags flags) {
2338 CGF.EmitObjCMRRAutoreleasePoolPop(Token);
2343 void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
2344 if (CGM.getLangOpts().ObjCAutoRefCount)
2345 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
2347 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
2350 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2353 switch (type.getObjCLifetime()) {
2354 case Qualifiers::OCL_None:
2355 case Qualifiers::OCL_ExplicitNone:
2356 case Qualifiers::OCL_Strong:
2357 case Qualifiers::OCL_Autoreleasing:
2358 return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(),
2361 case Qualifiers::OCL_Weak:
2362 return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
2366 llvm_unreachable("impossible lifetime!");
2369 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2371 e = e->IgnoreParens();
2372 QualType type = e->getType();
2374 // If we're loading retained from a __strong xvalue, we can avoid
2375 // an extra retain/release pair by zeroing out the source of this
2376 // "move" operation.
2377 if (e->isXValue() &&
2378 !type.isConstQualified() &&
2379 type.getObjCLifetime() == Qualifiers::OCL_Strong) {
2381 LValue lv = CGF.EmitLValue(e);
2383 // Load the object pointer.
2384 llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal();
2386 // Set the source pointer to NULL.
2387 CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv);
2389 return TryEmitResult(result, true);
2392 // As a very special optimization, in ARC++, if the l-value is the
2393 // result of a non-volatile assignment, do a simple retain of the
2394 // result of the call to objc_storeWeak instead of reloading.
2395 if (CGF.getLangOpts().CPlusPlus &&
2396 !type.isVolatileQualified() &&
2397 type.getObjCLifetime() == Qualifiers::OCL_Weak &&
2398 isa<BinaryOperator>(e) &&
2399 cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
2400 return TryEmitResult(CGF.EmitScalarExpr(e), false);
2402 return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
2405 static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
2406 llvm::Value *value);
2408 /// Given that the given expression is some sort of call (which does
2409 /// not return retained), emit a retain following it.
2410 static llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) {
2411 llvm::Value *value = CGF.EmitScalarExpr(e);
2412 return emitARCRetainAfterCall(CGF, value);
2415 static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
2416 llvm::Value *value) {
2417 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
2418 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2420 // Place the retain immediately following the call.
2421 CGF.Builder.SetInsertPoint(call->getParent(),
2422 ++llvm::BasicBlock::iterator(call));
2423 value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
2425 CGF.Builder.restoreIP(ip);
2427 } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
2428 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
2430 // Place the retain at the beginning of the normal destination block.
2431 llvm::BasicBlock *BB = invoke->getNormalDest();
2432 CGF.Builder.SetInsertPoint(BB, BB->begin());
2433 value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
2435 CGF.Builder.restoreIP(ip);
2438 // Bitcasts can arise because of related-result returns. Rewrite
2440 } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
2441 llvm::Value *operand = bitcast->getOperand(0);
2442 operand = emitARCRetainAfterCall(CGF, operand);
2443 bitcast->setOperand(0, operand);
2446 // Generic fall-back case.
2448 // Retain using the non-block variant: we never need to do a copy
2449 // of a block that's been returned to us.
2450 return CGF.EmitARCRetainNonBlock(value);
2454 /// Determine whether it might be important to emit a separate
2455 /// objc_retain_block on the result of the given expression, or
2456 /// whether it's okay to just emit it in a +1 context.
2457 static bool shouldEmitSeparateBlockRetain(const Expr *e) {
2458 assert(e->getType()->isBlockPointerType());
2459 e = e->IgnoreParens();
2461 // For future goodness, emit block expressions directly in +1
2462 // contexts if we can.
2463 if (isa<BlockExpr>(e))
2466 if (const CastExpr *cast = dyn_cast<CastExpr>(e)) {
2467 switch (cast->getCastKind()) {
2468 // Emitting these operations in +1 contexts is goodness.
2469 case CK_LValueToRValue:
2470 case CK_ARCReclaimReturnedObject:
2471 case CK_ARCConsumeObject:
2472 case CK_ARCProduceObject:
2475 // These operations preserve a block type.
2478 return shouldEmitSeparateBlockRetain(cast->getSubExpr());
2480 // These operations are known to be bad (or haven't been considered).
2481 case CK_AnyPointerToBlockPointerCast:
2490 /// Try to emit a PseudoObjectExpr at +1.
2492 /// This massively duplicates emitPseudoObjectRValue.
2493 static TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF,
2494 const PseudoObjectExpr *E) {
2495 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
2497 // Find the result expression.
2498 const Expr *resultExpr = E->getResultExpr();
2500 TryEmitResult result;
2502 for (PseudoObjectExpr::const_semantics_iterator
2503 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
2504 const Expr *semantic = *i;
2506 // If this semantic expression is an opaque value, bind it
2507 // to the result of its source expression.
2508 if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
2509 typedef CodeGenFunction::OpaqueValueMappingData OVMA;
2512 // If this semantic is the result of the pseudo-object
2513 // expression, try to evaluate the source as +1.
2514 if (ov == resultExpr) {
2515 assert(!OVMA::shouldBindAsLValue(ov));
2516 result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr());
2517 opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer()));
2519 // Otherwise, just bind it.
2521 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
2523 opaques.push_back(opaqueData);
2525 // Otherwise, if the expression is the result, evaluate it
2526 // and remember the result.
2527 } else if (semantic == resultExpr) {
2528 result = tryEmitARCRetainScalarExpr(CGF, semantic);
2530 // Otherwise, evaluate the expression in an ignored context.
2532 CGF.EmitIgnoredExpr(semantic);
2536 // Unbind all the opaques now.
2537 for (unsigned i = 0, e = opaques.size(); i != e; ++i)
2538 opaques[i].unbind(CGF);
2543 static TryEmitResult
2544 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
2545 // We should *never* see a nested full-expression here, because if
2546 // we fail to emit at +1, our caller must not retain after we close
2547 // out the full-expression.
2548 assert(!isa<ExprWithCleanups>(e));
2550 // The desired result type, if it differs from the type of the
2551 // ultimate opaque expression.
2552 llvm::Type *resultType = 0;
2555 e = e->IgnoreParens();
2557 // There's a break at the end of this if-chain; anything
2558 // that wants to keep looping has to explicitly continue.
2559 if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
2560 switch (ce->getCastKind()) {
2561 // No-op casts don't change the type, so we just ignore them.
2563 e = ce->getSubExpr();
2566 case CK_LValueToRValue: {
2567 TryEmitResult loadResult
2568 = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr());
2570 llvm::Value *value = loadResult.getPointer();
2571 value = CGF.Builder.CreateBitCast(value, resultType);
2572 loadResult.setPointer(value);
2577 // These casts can change the type, so remember that and
2578 // soldier on. We only need to remember the outermost such
2580 case CK_CPointerToObjCPointerCast:
2581 case CK_BlockPointerToObjCPointerCast:
2582 case CK_AnyPointerToBlockPointerCast:
2585 resultType = CGF.ConvertType(ce->getType());
2586 e = ce->getSubExpr();
2587 assert(e->getType()->hasPointerRepresentation());
2590 // For consumptions, just emit the subexpression and thus elide
2591 // the retain/release pair.
2592 case CK_ARCConsumeObject: {
2593 llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr());
2594 if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2595 return TryEmitResult(result, true);
2598 // Block extends are net +0. Naively, we could just recurse on
2599 // the subexpression, but actually we need to ensure that the
2600 // value is copied as a block, so there's a little filter here.
2601 case CK_ARCExtendBlockObject: {
2602 llvm::Value *result; // will be a +0 value
2604 // If we can't safely assume the sub-expression will produce a
2605 // block-copied value, emit the sub-expression at +0.
2606 if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) {
2607 result = CGF.EmitScalarExpr(ce->getSubExpr());
2609 // Otherwise, try to emit the sub-expression at +1 recursively.
2611 TryEmitResult subresult
2612 = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr());
2613 result = subresult.getPointer();
2615 // If that produced a retained value, just use that,
2616 // possibly casting down.
2617 if (subresult.getInt()) {
2619 result = CGF.Builder.CreateBitCast(result, resultType);
2620 return TryEmitResult(result, true);
2623 // Otherwise it's +0.
2626 // Retain the object as a block, then cast down.
2627 result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true);
2628 if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2629 return TryEmitResult(result, true);
2632 // For reclaims, emit the subexpression as a retained call and
2633 // skip the consumption.
2634 case CK_ARCReclaimReturnedObject: {
2635 llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr());
2636 if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2637 return TryEmitResult(result, true);
2644 // Skip __extension__.
2645 } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
2646 if (op->getOpcode() == UO_Extension) {
2647 e = op->getSubExpr();
2651 // For calls and message sends, use the retained-call logic.
2652 // Delegate inits are a special case in that they're the only
2653 // returns-retained expression that *isn't* surrounded by
2655 } else if (isa<CallExpr>(e) ||
2656 (isa<ObjCMessageExpr>(e) &&
2657 !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
2658 llvm::Value *result = emitARCRetainCall(CGF, e);
2659 if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2660 return TryEmitResult(result, true);
2662 // Look through pseudo-object expressions.
2663 } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) {
2664 TryEmitResult result
2665 = tryEmitARCRetainPseudoObject(CGF, pseudo);
2667 llvm::Value *value = result.getPointer();
2668 value = CGF.Builder.CreateBitCast(value, resultType);
2669 result.setPointer(value);
2674 // Conservatively halt the search at any other expression kind.
2678 // We didn't find an obvious production, so emit what we've got and
2679 // tell the caller that we didn't manage to retain.
2680 llvm::Value *result = CGF.EmitScalarExpr(e);
2681 if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
2682 return TryEmitResult(result, false);
2685 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
2688 TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
2689 llvm::Value *value = result.getPointer();
2690 if (!result.getInt())
2691 value = CGF.EmitARCRetain(type, value);
2695 /// EmitARCRetainScalarExpr - Semantically equivalent to
2696 /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
2697 /// best-effort attempt to peephole expressions that naturally produce
2698 /// retained objects.
2699 llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
2700 // The retain needs to happen within the full-expression.
2701 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2702 enterFullExpression(cleanups);
2703 RunCleanupsScope scope(*this);
2704 return EmitARCRetainScalarExpr(cleanups->getSubExpr());
2707 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2708 llvm::Value *value = result.getPointer();
2709 if (!result.getInt())
2710 value = EmitARCRetain(e->getType(), value);
2715 CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
2716 // The retain needs to happen within the full-expression.
2717 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
2718 enterFullExpression(cleanups);
2719 RunCleanupsScope scope(*this);
2720 return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr());
2723 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
2724 llvm::Value *value = result.getPointer();
2725 if (result.getInt())
2726 value = EmitARCAutorelease(value);
2728 value = EmitARCRetainAutorelease(e->getType(), value);
2732 llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) {
2733 llvm::Value *result;
2736 if (shouldEmitSeparateBlockRetain(e)) {
2737 result = EmitScalarExpr(e);
2740 TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e);
2741 result = subresult.getPointer();
2742 doRetain = !subresult.getInt();
2746 result = EmitARCRetainBlock(result, /*mandatory*/ true);
2747 return EmitObjCConsumeObject(e->getType(), result);
2750 llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) {
2751 // In ARC, retain and autorelease the expression.
2752 if (getLangOpts().ObjCAutoRefCount) {
2753 // Do so before running any cleanups for the full-expression.
2754 // EmitARCRetainAutoreleaseScalarExpr does this for us.
2755 return EmitARCRetainAutoreleaseScalarExpr(expr);
2758 // Otherwise, use the normal scalar-expression emission. The
2759 // exception machinery doesn't do anything special with the
2760 // exception like retaining it, so there's no safety associated with
2761 // only running cleanups after the throw has started, and when it
2762 // matters it tends to be substantially inferior code.
2763 return EmitScalarExpr(expr);
2766 std::pair<LValue,llvm::Value*>
2767 CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
2769 // Evaluate the RHS first.
2770 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
2771 llvm::Value *value = result.getPointer();
2773 bool hasImmediateRetain = result.getInt();
2775 // If we didn't emit a retained object, and the l-value is of block
2776 // type, then we need to emit the block-retain immediately in case
2777 // it invalidates the l-value.
2778 if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
2779 value = EmitARCRetainBlock(value, /*mandatory*/ false);
2780 hasImmediateRetain = true;
2783 LValue lvalue = EmitLValue(e->getLHS());
2785 // If the RHS was emitted retained, expand this.
2786 if (hasImmediateRetain) {
2787 llvm::Value *oldValue =
2788 EmitLoadOfScalar(lvalue);
2789 EmitStoreOfScalar(value, lvalue);
2790 EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime());
2792 value = EmitARCStoreStrong(lvalue, value, ignored);
2795 return std::pair<LValue,llvm::Value*>(lvalue, value);
2798 std::pair<LValue,llvm::Value*>
2799 CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
2800 llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
2801 LValue lvalue = EmitLValue(e->getLHS());
2803 EmitStoreOfScalar(value, lvalue);
2805 return std::pair<LValue,llvm::Value*>(lvalue, value);
2808 void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
2809 const ObjCAutoreleasePoolStmt &ARPS) {
2810 const Stmt *subStmt = ARPS.getSubStmt();
2811 const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
2813 CGDebugInfo *DI = getDebugInfo();
2815 DI->EmitLexicalBlockStart(Builder, S.getLBracLoc());
2817 // Keep track of the current cleanup stack depth.
2818 RunCleanupsScope Scope(*this);
2819 if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) {
2820 llvm::Value *token = EmitObjCAutoreleasePoolPush();
2821 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
2823 llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
2824 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
2827 for (CompoundStmt::const_body_iterator I = S.body_begin(),
2828 E = S.body_end(); I != E; ++I)
2832 DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc());
2835 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2836 /// make sure it survives garbage collection until this point.
2837 void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
2838 // We just use an inline assembly.
2839 llvm::FunctionType *extenderType
2840 = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All);
2841 llvm::Value *extender
2842 = llvm::InlineAsm::get(extenderType,
2844 /* constraints */ "r",
2845 /* side effects */ true);
2847 object = Builder.CreateBitCast(object, VoidPtrTy);
2848 EmitNounwindRuntimeCall(extender, object);
2851 /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with
2852 /// non-trivial copy assignment function, produce following helper function.
2853 /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; }
2856 CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction(
2857 const ObjCPropertyImplDecl *PID) {
2858 if (!getLangOpts().CPlusPlus ||
2859 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
2861 QualType Ty = PID->getPropertyIvarDecl()->getType();
2862 if (!Ty->isRecordType())
2864 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
2865 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
2867 llvm::Constant * HelperFn = 0;
2868 if (hasTrivialSetExpr(PID))
2870 assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null");
2871 if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty)))
2874 ASTContext &C = getContext();
2876 = &CGM.getContext().Idents.get("__assign_helper_atomic_property_");
2877 FunctionDecl *FD = FunctionDecl::Create(C,
2878 C.getTranslationUnitDecl(),
2880 SourceLocation(), II, C.VoidTy, 0,
2885 QualType DestTy = C.getPointerType(Ty);
2886 QualType SrcTy = Ty;
2888 SrcTy = C.getPointerType(SrcTy);
2890 FunctionArgList args;
2891 ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy);
2892 args.push_back(&dstDecl);
2893 ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy);
2894 args.push_back(&srcDecl);
2896 const CGFunctionInfo &FI =
2897 CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args,
2898 FunctionType::ExtInfo(),
2901 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
2903 llvm::Function *Fn =
2904 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2905 "__assign_helper_atomic_property_",
2908 // Initialize debug info if needed.
2909 maybeInitializeDebugInfo();
2911 StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
2913 DeclRefExpr DstExpr(&dstDecl, false, DestTy,
2914 VK_RValue, SourceLocation());
2915 UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(),
2916 VK_LValue, OK_Ordinary, SourceLocation());
2918 DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
2919 VK_RValue, SourceLocation());
2920 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
2921 VK_LValue, OK_Ordinary, SourceLocation());
2923 Expr *Args[2] = { &DST, &SRC };
2924 CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment());
2925 CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(),
2926 Args, DestTy->getPointeeType(),
2927 VK_LValue, SourceLocation(), false);
2932 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
2933 CGM.setAtomicSetterHelperFnMap(Ty, HelperFn);
2938 CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction(
2939 const ObjCPropertyImplDecl *PID) {
2940 if (!getLangOpts().CPlusPlus ||
2941 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper())
2943 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
2944 QualType Ty = PD->getType();
2945 if (!Ty->isRecordType())
2947 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic)))
2949 llvm::Constant * HelperFn = 0;
2951 if (hasTrivialGetExpr(PID))
2953 assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null");
2954 if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty)))
2958 ASTContext &C = getContext();
2960 = &CGM.getContext().Idents.get("__copy_helper_atomic_property_");
2961 FunctionDecl *FD = FunctionDecl::Create(C,
2962 C.getTranslationUnitDecl(),
2964 SourceLocation(), II, C.VoidTy, 0,
2969 QualType DestTy = C.getPointerType(Ty);
2970 QualType SrcTy = Ty;
2972 SrcTy = C.getPointerType(SrcTy);
2974 FunctionArgList args;
2975 ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy);
2976 args.push_back(&dstDecl);
2977 ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy);
2978 args.push_back(&srcDecl);
2980 const CGFunctionInfo &FI =
2981 CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args,
2982 FunctionType::ExtInfo(),
2985 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
2987 llvm::Function *Fn =
2988 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2989 "__copy_helper_atomic_property_", &CGM.getModule());
2991 // Initialize debug info if needed.
2992 maybeInitializeDebugInfo();
2994 StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
2996 DeclRefExpr SrcExpr(&srcDecl, false, SrcTy,
2997 VK_RValue, SourceLocation());
2999 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(),
3000 VK_LValue, OK_Ordinary, SourceLocation());
3002 CXXConstructExpr *CXXConstExpr =
3003 cast<CXXConstructExpr>(PID->getGetterCXXConstructor());
3005 SmallVector<Expr*, 4> ConstructorArgs;
3006 ConstructorArgs.push_back(&SRC);
3007 CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin();
3010 for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end();
3012 ConstructorArgs.push_back(*A);
3014 CXXConstructExpr *TheCXXConstructExpr =
3015 CXXConstructExpr::Create(C, Ty, SourceLocation(),
3016 CXXConstExpr->getConstructor(),
3017 CXXConstExpr->isElidable(),
3019 CXXConstExpr->hadMultipleCandidates(),
3020 CXXConstExpr->isListInitialization(),
3021 CXXConstExpr->requiresZeroInitialization(),
3022 CXXConstExpr->getConstructionKind(),
3025 DeclRefExpr DstExpr(&dstDecl, false, DestTy,
3026 VK_RValue, SourceLocation());
3028 RValue DV = EmitAnyExpr(&DstExpr);
3030 = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType());
3031 EmitAggExpr(TheCXXConstructExpr,
3032 AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(),
3033 AggValueSlot::IsDestructed,
3034 AggValueSlot::DoesNotNeedGCBarriers,
3035 AggValueSlot::IsNotAliased));
3038 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
3039 CGM.setAtomicGetterHelperFnMap(Ty, HelperFn);
3044 CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) {
3045 // Get selectors for retain/autorelease.
3046 IdentifierInfo *CopyID = &getContext().Idents.get("copy");
3047 Selector CopySelector =
3048 getContext().Selectors.getNullarySelector(CopyID);
3049 IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease");
3050 Selector AutoreleaseSelector =
3051 getContext().Selectors.getNullarySelector(AutoreleaseID);
3053 // Emit calls to retain/autorelease.
3054 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
3055 llvm::Value *Val = Block;
3057 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3059 Val, CallArgList(), 0, 0);
3060 Val = Result.getScalarVal();
3061 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
3062 Ty, AutoreleaseSelector,
3063 Val, CallArgList(), 0, 0);
3064 Val = Result.getScalarVal();
3069 CGObjCRuntime::~CGObjCRuntime() {}