CGObjC.cpp revision 76ecdfc53778a8468b7177c556fdbfe797fdbb29
15508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// 25508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 35508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// The LLVM Compiler Infrastructure 45508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 85508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===// 95508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// This contains code to emit Objective-C code as LLVM code. 115508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 125508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===// 135508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 14bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel#include "CGDebugInfo.h" 152979ec73b4f974d85f2ce84167712177a44c6f09Ted Kremenek#include "CGObjCRuntime.h" 165508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenFunction.h" 175508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenModule.h" 18f85e193739c953358c865005855253af4f68a497John McCall#include "TargetInfo.h" 1985c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar#include "clang/AST/ASTContext.h" 20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2116f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h" 22e66f4e3e3ae9d7d11b0c302211066fad69228abaDaniel Dunbar#include "clang/Basic/Diagnostic.h" 233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson#include "llvm/ADT/STLExtras.h" 243b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 253b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h" 265508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace clang; 275508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace CodeGen; 285508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 29f85e193739c953358c865005855253af4f68a497John McCalltypedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; 30f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 31f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); 32ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 33490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian QualType ET, 34ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *Method, 35ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek RValue Result); 36f85e193739c953358c865005855253af4f68a497John McCall 37f85e193739c953358c865005855253af4f68a497John McCall/// Given the address of a variable of pointer type, find the correct 38f85e193739c953358c865005855253af4f68a497John McCall/// null to store into it. 39f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *getNullForVariable(llvm::Value *addr) { 402acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *type = 41f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(addr->getType())->getElementType(); 42f85e193739c953358c865005855253af4f68a497John McCall return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); 43f85e193739c953358c865005855253af4f68a497John McCall} 44f85e193739c953358c865005855253af4f68a497John McCall 458fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object. 461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 4771fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{ 480d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall llvm::Constant *C = 490d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall CGM.getObjCRuntime().GenerateConstantString(E->getString()); 50ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This bitcast should just be made an invariant on the Runtime. 513c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 528fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 54eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// EmitObjCBoxedExpr - This routine generates code to call 55eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// the appropriate expression boxing method. This will either be 56eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:]. 57ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// 5816098f366097305c348b356a44638d6c959c6e60Eric Christopherllvm::Value * 59eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardCodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { 60ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the correct selector for this literal's concrete type. 61eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const Expr *SubExpr = E->getSubExpr(); 62ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Get the method. 63eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); 64eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod && "BoxingMethod is null"); 65eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); 66eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard Selector Sel = BoxingMethod->getSelector(); 67ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 68ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 69eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // Assumes that the method was introduced in the class that should be 70eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // messaged (avoids pulling it out of the result type). 71ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 72eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); 73bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); 74eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard 75eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ParmVarDecl *argDecl = *BoxingMethod->param_begin(); 76ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 77eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard RValue RV = EmitAnyExpr(SubExpr); 78ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 79ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RV, ArgQT); 80eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard 81ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek RValue result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 82eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard BoxingMethod->getResultType(), Sel, Receiver, Args, 83eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard ClassDecl, BoxingMethod); 84ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 85ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 86ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 87ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 88ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, 89ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *MethodWithObjects) { 90ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ASTContext &Context = CGM.getContext(); 91ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *DLE = 0; 92ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); 93ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!ALE) 94ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DLE = cast<ObjCDictionaryLiteral>(E); 95ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 96ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Compute the type of the array we're initializing. 97ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek uint64_t NumElements = 98ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ALE ? ALE->getNumElements() : DLE->getNumElements(); 99ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), 100ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek NumElements); 101ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementType = Context.getObjCIdType().withConst(); 102ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementArrayType 103ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = Context.getConstantArrayType(ElementType, APNumElements, 104ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArrayType::Normal, /*IndexTypeQuals=*/0); 105ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 106ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Allocate the temporary array(s). 107ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects"); 108ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Keys = 0; 109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) 110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Keys = CreateMemTemp(ElementArrayType, "keys"); 111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Perform the actual initialialization of the array(s). 113ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek for (uint64_t i = 0; i < NumElements; i++) { 114ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (ALE) { 115ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Emit the initializer. 116ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Rhs = ALE->getElement(i); 117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 118ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 119ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Rhs->getType()), 120ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 121ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek EmitScalarInit(Rhs, /*D=*/0, LV, /*capturedByInit=*/false); 122ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Emit the key initializer. 124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Key = DLE->getKeyValueElement(i).Key; 125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i), 126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Key->getType()), 128ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 129ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek EmitScalarInit(Key, /*D=*/0, KeyLV, /*capturedByInit=*/false); 130ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 131ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Emit the value initializer. 132ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Value = DLE->getKeyValueElement(i).Value; 133ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 134ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 135ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Value->getType()), 136ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 137ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek EmitScalarInit(Value, /*D=*/0, ValueLV, /*capturedByInit=*/false); 138ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 139ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 140ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 141ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the argument list. 142ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 143ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); 144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ParmVarDecl *argDecl = *PI++; 145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Objects), ArgQT); 147ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) { 148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI++; 149ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Keys), ArgQT); 151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 152ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI; 153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 154ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Count = 155ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); 156ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Count), ArgQT); 157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Selector Sel = MethodWithObjects->getSelector(); 160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ResultType = E->getType(); 161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCObjectPointerType *InterfacePointerType 162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = ResultType->getAsObjCInterfacePointerType(); 163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCInterfaceDecl *Class 164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = InterfacePointerType->getObjectType()->getInterface(); 165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 166bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, Class); 167ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 168ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the message send. 16916098f366097305c348b356a44638d6c959c6e60Eric Christopher RValue result 17016098f366097305c348b356a44638d6c959c6e60Eric Christopher = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 17116098f366097305c348b356a44638d6c959c6e60Eric Christopher MethodWithObjects->getResultType(), 17216098f366097305c348b356a44638d6c959c6e60Eric Christopher Sel, 17316098f366097305c348b356a44638d6c959c6e60Eric Christopher Receiver, Args, Class, 17416098f366097305c348b356a44638d6c959c6e60Eric Christopher MethodWithObjects); 175ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 176ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 177ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { 180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); 181ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 182ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 183ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( 184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *E) { 185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); 186ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 187ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 1888fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector. 1898fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 1908fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Untyped selector. 1918fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Note that this implementation allows for non-constant strings to be passed 1928fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // as arguments to @selector(). Currently, the only thing preventing this 1938fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // behaviour is the type checking in the front end. 194bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); 1958fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 1968fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 197ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { 198ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This should pass the Decl not the name. 199bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); 200ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar} 2018fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 202926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// \brief Adjust the type of the result of an Objective-C message send 203926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// expression when the method has a related result type. 204926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 205490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian QualType ExpT, 206926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCMethodDecl *Method, 207926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue Result) { 208926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method) 209926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 210f85e193739c953358c865005855253af4f68a497John McCall 211926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method->hasRelatedResultType() || 212490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian CGF.getContext().hasSameType(ExpT, Method->getResultType()) || 213926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor !Result.isScalar()) 214926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 215926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 216926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // We have applied a related result type. Cast the rvalue appropriately. 217926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), 218490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian CGF.ConvertType(ExpT))); 219926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor} 2208fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 221dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// Decide whether to extend the lifetime of the receiver of a 222dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// returns-inner-pointer message. 223dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallstatic bool 224dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { 225dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall switch (message->getReceiverKind()) { 226dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 227dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // For a normal instance message, we should extend unless the 228dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // receiver is loaded from a variable with precise lifetime. 229dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Instance: { 230dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const Expr *receiver = message->getInstanceReceiver(); 231dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); 232dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; 233dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall receiver = ice->getSubExpr()->IgnoreParens(); 234dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 235dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Only __strong variables. 236dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 237dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return true; 238dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 239dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All ivars and fields have precise lifetime. 240dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) 241dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 242dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 243dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Otherwise, check for variables. 244dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); 245dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!declRef) return true; 246dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); 247dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!var) return true; 248dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 249dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All variables have precise lifetime except local variables with 250dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // automatic storage duration that aren't specially marked. 251dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return (var->hasLocalStorage() && 252dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall !var->hasAttr<ObjCPreciseLifetimeAttr>()); 253dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 254dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 255dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Class: 256dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperClass: 257dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // It's never necessary for class objects. 258dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 259dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 260dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperInstance: 261dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // We generally assume that 'self' lives throughout a method call. 262dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 263dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 264dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 265dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall llvm_unreachable("invalid receiver kind"); 266dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall} 267dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 268ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, 269ef072fd2f3347cfd857d6eb787b245b950771430John McCall ReturnValueSlot Return) { 2708fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Only the lookup mechanism and first two arguments of the method 2718fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // implementation vary between runtimes. We can get the receiver and 2728fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // arguments in generic code. 2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 274f85e193739c953358c865005855253af4f68a497John McCall bool isDelegateInit = E->isDelegateInitCall(); 275f85e193739c953358c865005855253af4f68a497John McCall 276dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ObjCMethodDecl *method = E->getMethodDecl(); 2774e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 278f85e193739c953358c865005855253af4f68a497John McCall // We don't retain the receiver in delegate init calls, and this is 279f85e193739c953358c865005855253af4f68a497John McCall // safe because the receiver value is always loaded from 'self', 280f85e193739c953358c865005855253af4f68a497John McCall // which we zero out. We don't want to Block_copy block receivers, 281f85e193739c953358c865005855253af4f68a497John McCall // though. 282f85e193739c953358c865005855253af4f68a497John McCall bool retainSelf = 283f85e193739c953358c865005855253af4f68a497John McCall (!isDelegateInit && 2844e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie CGM.getLangOpts().ObjCAutoRefCount && 285dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method && 286dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<NSConsumesSelfAttr>()); 287f85e193739c953358c865005855253af4f68a497John McCall 288208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2898fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner bool isSuperMessage = false; 290f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar bool isClassMessage = false; 291c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall ObjCInterfaceDecl *OID = 0; 2928fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Find the receiver 293926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor QualType ReceiverType; 2940b647a6ea18151149d624ab373e6fe0e819e4a9aDaniel Dunbar llvm::Value *Receiver = 0; 29504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor switch (E->getReceiverKind()) { 29604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Instance: 297926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getInstanceReceiver()->getType(); 298f85e193739c953358c865005855253af4f68a497John McCall if (retainSelf) { 299f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, 300f85e193739c953358c865005855253af4f68a497John McCall E->getInstanceReceiver()); 301f85e193739c953358c865005855253af4f68a497John McCall Receiver = ter.getPointer(); 302dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (ter.getInt()) retainSelf = false; 303f85e193739c953358c865005855253af4f68a497John McCall } else 304f85e193739c953358c865005855253af4f68a497John McCall Receiver = EmitScalarExpr(E->getInstanceReceiver()); 30504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Class: { 308926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getClassReceiver(); 309926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 3103031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(ObjTy && "Invalid Objective-C class message send"); 3113031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall OID = ObjTy->getInterface(); 3123031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(OID && "Invalid Objective-C class message send"); 313bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Receiver = Runtime.GetClass(*this, OID); 314f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar isClassMessage = true; 31504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 31604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 31704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 31804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperInstance: 319926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 32004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor Receiver = LoadObjCSelf(); 3218fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner isSuperMessage = true; 32204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 32304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 32404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperClass: 325926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 3268fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner Receiver = LoadObjCSelf(); 32704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isSuperMessage = true; 32804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isClassMessage = true; 32904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3308fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 3318fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 332dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (retainSelf) 333dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainNonBlock(Receiver); 334dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 335dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // In ARC, we sometimes want to "extend the lifetime" 336dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // (i.e. retain+autorelease) of receivers of returns-inner-pointer 337dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // messages. 3384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount && method && 339dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<ObjCReturnsInnerPointerAttr>() && 340dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall shouldExtendReceiverForInnerPointerMessage(E)) 341dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); 342dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 343f85e193739c953358c865005855253af4f68a497John McCall QualType ResultType = 344dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method ? method->getResultType() : E->getType(); 345f85e193739c953358c865005855253af4f68a497John McCall 34619cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar CallArgList Args; 347dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall EmitCallArgs(Args, method, E->arg_begin(), E->arg_end()); 3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 349f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, do an unsafe store of null into 350f85e193739c953358c865005855253af4f68a497John McCall // self. This represents the call taking direct ownership of that 351f85e193739c953358c865005855253af4f68a497John McCall // value. We have to do this after emitting the other call 352f85e193739c953358c865005855253af4f68a497John McCall // arguments because they might also reference self, but we don't 353f85e193739c953358c865005855253af4f68a497John McCall // have to worry about any of them modifying self because that would 354f85e193739c953358c865005855253af4f68a497John McCall // be an undefined read and write of an object in unordered 355f85e193739c953358c865005855253af4f68a497John McCall // expressions. 356f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 3574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().ObjCAutoRefCount && 358f85e193739c953358c865005855253af4f68a497John McCall "delegate init calls should only be marked in ARC"); 359f85e193739c953358c865005855253af4f68a497John McCall 360f85e193739c953358c865005855253af4f68a497John McCall // Do an unsafe store of null into self. 361f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *selfAddr = 362f85e193739c953358c865005855253af4f68a497John McCall LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 363f85e193739c953358c865005855253af4f68a497John McCall assert(selfAddr && "no self entry for a delegate init call?"); 364f85e193739c953358c865005855253af4f68a497John McCall 365f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); 366f85e193739c953358c865005855253af4f68a497John McCall } 3677e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson 368926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue result; 3698fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner if (isSuperMessage) { 3709384c768e93f270118a30ce96546083a666da284Chris Lattner // super is only valid in an Objective-C method 3719384c768e93f270118a30ce96546083a666da284Chris Lattner const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 3727ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); 373926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, 374926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 375926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor OMD->getClassInterface(), 376926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isCategoryImpl, 377926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, 378926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isClassMessage, 379926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Args, 380dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 381926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else { 382926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSend(*this, Return, ResultType, 383926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 384926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, Args, OID, 385dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 3868fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 387f85e193739c953358c865005855253af4f68a497John McCall 388f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, implicitly store the result of 389f85e193739c953358c865005855253af4f68a497John McCall // the call back into self. This takes ownership of the value. 390f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 391f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *selfAddr = 392f85e193739c953358c865005855253af4f68a497John McCall LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 393f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newSelf = result.getScalarVal(); 394f85e193739c953358c865005855253af4f68a497John McCall 395f85e193739c953358c865005855253af4f68a497John McCall // The delegate return type isn't necessarily a matching type; in 396f85e193739c953358c865005855253af4f68a497John McCall // fact, it's quite likely to be 'id'. 3972acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *selfTy = 398f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(selfAddr->getType())->getElementType(); 399f85e193739c953358c865005855253af4f68a497John McCall newSelf = Builder.CreateBitCast(newSelf, selfTy); 400f85e193739c953358c865005855253af4f68a497John McCall 401f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(newSelf, selfAddr); 402f85e193739c953358c865005855253af4f68a497John McCall } 4034e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 404490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian return AdjustRelatedResultType(*this, E->getType(), method, result); 4055508518a2702b00be3b15a26d772bde968972f54Anders Carlsson} 4065508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 407f85e193739c953358c865005855253af4f68a497John McCallnamespace { 408f85e193739c953358c865005855253af4f68a497John McCallstruct FinishARCDealloc : EHScopeStack::Cleanup { 409ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 410f85e193739c953358c865005855253af4f68a497John McCall const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); 411799d34e9505a833549c71f2ac5f842da157ea031John McCall 412799d34e9505a833549c71f2ac5f842da157ea031John McCall const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); 413f85e193739c953358c865005855253af4f68a497John McCall const ObjCInterfaceDecl *iface = impl->getClassInterface(); 414f85e193739c953358c865005855253af4f68a497John McCall if (!iface->getSuperClass()) return; 415f85e193739c953358c865005855253af4f68a497John McCall 416799d34e9505a833549c71f2ac5f842da157ea031John McCall bool isCategory = isa<ObjCCategoryImplDecl>(impl); 417799d34e9505a833549c71f2ac5f842da157ea031John McCall 418f85e193739c953358c865005855253af4f68a497John McCall // Call [super dealloc] if we have a superclass. 419f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *self = CGF.LoadObjCSelf(); 420f85e193739c953358c865005855253af4f68a497John McCall 421f85e193739c953358c865005855253af4f68a497John McCall CallArgList args; 422f85e193739c953358c865005855253af4f68a497John McCall CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), 423f85e193739c953358c865005855253af4f68a497John McCall CGF.getContext().VoidTy, 424f85e193739c953358c865005855253af4f68a497John McCall method->getSelector(), 425f85e193739c953358c865005855253af4f68a497John McCall iface, 426799d34e9505a833549c71f2ac5f842da157ea031John McCall isCategory, 427f85e193739c953358c865005855253af4f68a497John McCall self, 428f85e193739c953358c865005855253af4f68a497John McCall /*is class msg*/ false, 429f85e193739c953358c865005855253af4f68a497John McCall args, 430f85e193739c953358c865005855253af4f68a497John McCall method); 431f85e193739c953358c865005855253af4f68a497John McCall } 432f85e193739c953358c865005855253af4f68a497John McCall}; 433f85e193739c953358c865005855253af4f68a497John McCall} 434f85e193739c953358c865005855253af4f68a497John McCall 435af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates 436af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by 437af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction. 438679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, 4398d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel const ObjCContainerDecl *CD, 4408d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel SourceLocation StartLoc) { 441d26bc76c98006609002d9930f8840490e88ac5b5John McCall FunctionArgList args; 4424800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel // Check if we should generate debug info for this method. 443a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov if (!OMD->hasAttr<NoDebugAttr>()) 444a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov maybeInitializeDebugInfo(); 4454800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel 446679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); 447f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 448de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); 4490e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar CGM.SetInternalFunctionAttributes(OMD, Fn, FI); 4504111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 451d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getSelfDecl()); 452d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getCmdDecl()); 4534111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 454491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(), 45516098f366097305c348b356a44638d6c959c6e60Eric Christopher E = OMD->param_end(); PI != E; ++PI) 456d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(*PI); 457b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar 45814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne CurGD = OMD; 45914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne 4608d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc); 461f85e193739c953358c865005855253af4f68a497John McCall 462f85e193739c953358c865005855253af4f68a497John McCall // In ARC, certain methods get an extra cleanup. 4634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount && 464f85e193739c953358c865005855253af4f68a497John McCall OMD->isInstanceMethod() && 465f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().isUnarySelector()) { 466f85e193739c953358c865005855253af4f68a497John McCall const IdentifierInfo *ident = 467f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().getIdentifierInfoForSlot(0); 468f85e193739c953358c865005855253af4f68a497John McCall if (ident->isStr("dealloc")) 469f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); 470f85e193739c953358c865005855253af4f68a497John McCall } 471af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 472af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 473f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 474f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, QualType type); 475f85e193739c953358c865005855253af4f68a497John McCall 476af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method. An Objective-C method is a C function with 4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture. 478af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 4798d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); 4806fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis EmitStmt(OMD->getBody()); 4816fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis FinishFunction(OMD->getBodyRBrace()); 482af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 483af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 48441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructGetterCall - Call the runtime function to load a property 48541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// into the return value slot. 48641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 48741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall bool isAtomic, bool hasStrong) { 48841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ASTContext &Context = CGF.getContext(); 48941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 49041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *src = 49141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), 49241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivar, 0).getAddress(); 49341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 49441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // objc_copyStruct (ReturnValue, &structIvar, 49541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // sizeof (Type of Ivar), isAtomic, false); 49641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CallArgList args; 49741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 49841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); 49941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(dest), Context.VoidPtrTy); 50041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 50141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); 50241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(src), Context.VoidPtrTy); 50341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 50441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); 50541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); 50641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); 50741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); 50841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 50941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); 5100f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args, 5110f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 5120f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 51341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall fn, ReturnValueSlot(), args); 51441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall} 51541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 5161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Determine whether the given architecture supports unaligned atomic 5171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// accesses. They don't have to be fast, just faster than a function 5181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// call and a mutex. 5191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { 520de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // FIXME: Allow unaligned atomic load/store on x86. (It is not 521de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // currently supported by the backend.) 522de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman return 0; 5231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Return the maximum size that permits atomic accesses for the given 5261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// architecture. 5271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, 5281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch) { 5291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // ARM has 8-byte atomic accesses, but it's not clear whether we 5301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // want to rely on them here. 5311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In the default case, just assume that any size up to a pointer is 5331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fine given adequate alignment. 5341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return CharUnits::fromQuantity(CGM.PointerSizeInBytes); 5351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallnamespace { 5381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall class PropertyImplStrategy { 5391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall public: 5401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall enum StrategyKind { 5411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'native' strategy is to use the architecture's provided 5421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// reads and writes. 5431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Native, 5441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty and objc_getProperty. 5461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall GetSetProperty, 5471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty for the setter, but use expression 5491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// evaluation for the getter. 5501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall SetPropertyAndExpressionGet, 5511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_copyStruct. 5531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CopyStruct, 5541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'expression' strategy is to emit normal assignment or 5561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// lvalue-to-rvalue expressions. 5571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expression 5581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 5591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall StrategyKind getKind() const { return StrategyKind(Kind); } 5611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool hasStrongMember() const { return HasStrong; } 5631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isAtomic() const { return IsAtomic; } 5641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isCopy() const { return IsCopy; } 5651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarSize() const { return IvarSize; } 5671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarAlignment() const { return IvarAlignment; } 5681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy(CodeGenModule &CGM, 5701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl); 5711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall private: 5731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned Kind : 8; 5741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsAtomic : 1; 5751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsCopy : 1; 5761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned HasStrong : 1; 5771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarSize; 5791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarAlignment; 5801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 5811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 583bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru/// Pick an implementation strategy for the given property synthesis. 5841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, 5851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl) { 5861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 587265941bc308d65cc270d5c4de5806f37ce405606John McCall ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); 5881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 589265941bc308d65cc270d5c4de5806f37ce405606John McCall IsCopy = (setterKind == ObjCPropertyDecl::Copy); 590265941bc308d65cc270d5c4de5806f37ce405606John McCall IsAtomic = prop->isAtomic(); 5911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = false; // doesn't matter here. 5921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Evaluate the ivar's size and alignment. 5941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 5951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 5961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::tie(IvarSize, IvarAlignment) 5971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall = CGM.getContext().getTypeInfoInChars(ivarType); 5981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we have a copy property, we always have to use getProperty/setProperty. 600265941bc308d65cc270d5c4de5806f37ce405606John McCall // TODO: we could actually use setProperty and an expression for non-atomics. 6011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IsCopy) { 6021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 606265941bc308d65cc270d5c4de5806f37ce405606John McCall // Handle retain. 607265941bc308d65cc270d5c4de5806f37ce405606John McCall if (setterKind == ObjCPropertyDecl::Retain) { 6081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In GC-only, there's nothing special that needs to be done. 6094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { 6101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fallthrough 6111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In ARC, if the property is non-atomic, use expression emission, 6131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // which translates to objc_storeStrong. This isn't required, but 6141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // it's slightly nicer. 6154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { 616d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // Using standard expression emission for the setter is only 617d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // acceptable if the ivar is __strong, which won't be true if 618d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // the property is annotated with __attribute__((NSObject)). 619d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // TODO: falling all the way back to objc_setProperty here is 620d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // just laziness, though; we could still use objc_storeStrong 621d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // if we hacked it right. 622d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) 623d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = Expression; 624d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall else 625d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = SetPropertyAndExpressionGet; 6261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we need to at least use setProperty. However, if 6291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the property isn't atomic, we can use normal expression 6301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // emission for the getter. 6311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else if (!IsAtomic) { 6321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = SetPropertyAndExpressionGet; 6331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we have to use both setProperty and getProperty. 6361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else { 6371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we're not atomic, just use expression accesses. 6431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!IsAtomic) { 6441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 6451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6485889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // Properties on bitfield ivars need to be emitted using expression 6495889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // accesses even if they're nominally atomic. 6505889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall if (ivar->isBitField()) { 6515889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall Kind = Expression; 6525889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall return; 6535889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall } 6545889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall 6551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // GC-qualified or ARC-qualified ivars need to be emitted as 6561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // expressions. This actually works out to being atomic anyway, 6571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // except for ARC __strong, but that should trigger the above code. 6581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (ivarType.hasNonTrivialObjCLifetime() || 6594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (CGM.getLangOpts().getGC() && 6601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getContext().getObjCGCAttrKind(ivarType))) { 6611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 6621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Compute whether the ivar has strong members. 6664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC()) 6671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const RecordType *recordType = ivarType->getAs<RecordType>()) 6681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = recordType->getDecl()->hasObjectMember(); 6691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // We can never access structs with object members with a native 6711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access, because we need to use write barriers. This is what 6721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // objc_copyStruct is for. 6731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (HasStrong) { 6741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 6751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, this is target-dependent and based on the size and 6791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // alignment of the ivar. 680c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 681c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // If the size of the ivar is not a power of two, give up. We don't 682c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // want to get into the business of doing compare-and-swaps. 683c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall if (!IvarSize.isPowerOfTwo()) { 684c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall Kind = CopyStruct; 685c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall return; 686c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall } 687c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 6881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch = 6891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getContext().getTargetInfo().getTriple().getArch(); 6901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Most architectures require memory to fit within a single cache 6921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // line, so the alignment has to be at least the size of the access. 6931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise we have to grab a lock. 6941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { 6951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 6961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If the ivar's size exceeds the architecture's maximum atomic 7001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access size, we have to use CopyStruct. 7011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { 7021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we can use native loads and stores. 7071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Native; 7081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 709af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 7102ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property getter function. 7112ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 7122ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 713489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category. 714fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, 715fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const ObjCPropertyImplDecl *PID) { 716b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 71720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicGetterCopyHelperFunction(PID); 718af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 719af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); 720af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar assert(OMD && "Invalid call to generate getter (empty method)"); 721ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 723490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); 7241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall FinishFunction(); 7261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 7271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7286c11f0b3106263600af2ea0438ebb372821514aaJohn McCallstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { 7296c11f0b3106263600af2ea0438ebb372821514aaJohn McCall const Expr *getter = propImpl->getGetterCXXConstructor(); 7301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getter) return true; 7311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 7331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 7341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7356c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // If the property has a reference type, we might just be binding a 7366c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // reference, in which case the result will be a gl-value. We should 7376c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // treat this as a non-trivial operation. 7386c11f0b3106263600af2ea0438ebb372821514aaJohn McCall if (getter->isGLValue()) 7396c11f0b3106263600af2ea0438ebb372821514aaJohn McCall return false; 7406c11f0b3106263600af2ea0438ebb372821514aaJohn McCall 7411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we selected a trivial copy-constructor, we're okay. 7421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) 7431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return (construct->getConstructor()->isTrivial()); 7441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The constructor might require cleanups (in which case it's never 7461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // trivial). 7471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(getter)); 7481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return false; 7491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 7501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 75120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// emitCPPObjectAtomicGetterCall - Call the runtime function to 75220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// copy the ivar into the resturn slot. 75320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianstatic void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 75420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *returnAddr, 75520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar, 75620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant *AtomicHelperFn) { 75720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, 75820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // AtomicHelperFn); 75920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallArgList args; 76020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 76120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 1st argument is the return Slot. 76220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); 76320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 76420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 2nd argument is the address of the ivar. 76520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *ivarAddr = 76620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 76720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 76820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 76920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 77020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 77120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // Third argument is the helper function. 77220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 77320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 77420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 775d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); 7760f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 7770f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 7780f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 7790f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 78020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 78120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 78220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 7831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallvoid 7841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 785b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian const ObjCPropertyImplDecl *propImpl, 786490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian const ObjCMethodDecl *GetterMethodDecl, 787b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn) { 7881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If there's a non-trivial 'get' expression, we just have to emit that. 7891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialGetExpr(propImpl)) { 79020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!AtomicHelperFn) { 79120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), 79220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian /*nrvo*/ 0); 79320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitReturnStmt(ret); 79420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 79520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian else { 79620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 79720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian emitCPPObjectAtomicGetterCall(*this, ReturnValue, 79820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivar, AtomicHelperFn); 79920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 8001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 8041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType propType = prop->getType(); 8051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); 8061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 8081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Pick an implementation strategy. 8101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 8111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 8121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 813aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 814aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 815aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 816aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 8171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 8181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 8201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 8211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 8221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 8231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 8241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 8251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic load. This does not impose ordering constraints. 8271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = LV.getAddress(); 8281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 8291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); 8301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall load->setAlignment(strategy.getIvarAlignment().getQuantity()); 8311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall load->setAtomic(llvm::Unordered); 8321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Store that value into the return address. Doing this with a 8341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // bitcast is likely to produce some pretty ugly IR, but it's not 8351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the *most* terrible thing in the world. 8361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType)); 8371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Make sure we don't do an autorelease. 8391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall AutoreleaseResult = false; 8401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: { 8441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *getPropertyFn = 8451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getObjCRuntime().GetPropertyGetFunction(); 8461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getPropertyFn) { 8471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); 848c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 849c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 850c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 851c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). 852c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // FIXME: Can't this be simpler? This might even be worse than the 853c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // corresponding gcc code. 8541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *cmd = 8551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd"); 8561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 8571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarOffset = 8581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 8591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CallArgList args; 8611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 8621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 8631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 864265941bc308d65cc270d5c4de5806f37ce405606John McCall args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 865265941bc308d65cc270d5c4de5806f37ce405606John McCall getContext().BoolTy); 8661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 867e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // FIXME: We shouldn't need to get the function info here, the 868e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // runtime already should have computed it to build the function. 8690f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args, 8700f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 8710f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 8721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getPropertyFn, ReturnValueSlot(), args); 8731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 874c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // We need to fix the type here. Ivars with copy & retain are 875c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // always objects so we don't need to worry about complex or 876c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // aggregates. 8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), 87852c18b0933006acaf5c865f6e86a4694b5f288e7Fariborz Jahanian getTypes().ConvertType(getterMethod->getResultType()))); 8791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitReturnOfRValue(RV, propType); 881f85e193739c953358c865005855253af4f68a497John McCall 882f85e193739c953358c865005855253af4f68a497John McCall // objc_getProperty does an autorelease, so we should suppress ours. 883f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 8841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 8891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall emitStructGetterCall(*this, ivar, strategy.isAtomic(), 8901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall strategy.hasStrongMember()); 8911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 8941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 8951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 8961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 8981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (ivarType->isAnyComplexType()) { 8991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ComplexPairTy pair = LoadComplexFromAddr(LV.getAddress(), 9001b23fe61cf4437668280212d0ad6cb7196f51529Fariborz Jahanian LV.isVolatileQualified()); 9011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall StoreComplexToAddr(pair, ReturnValue, LV.isVolatileQualified()); 9021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else if (hasAggregateLLVMType(ivarType)) { 9031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The return value slot is guaranteed to not be aliased, but 9041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // that's not necessarily the same as "on the stack", so 9051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // we still potentially need objc_memmove_collectable. 906649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); 907ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } else { 908ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall llvm::Value *value; 909ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (propType->isReferenceType()) { 910ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = LV.getAddress(); 911ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } else { 912ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // We want to load and autoreleaseReturnValue ARC __weak ivars. 913ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 9141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 915ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 916ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // Otherwise we want to do a simple load, suppressing the 917ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // final autorelease. 918f85e193739c953358c865005855253af4f68a497John McCall } else { 919ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = EmitLoadOfLValue(LV).getScalarVal(); 920ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall AutoreleaseResult = false; 92114086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian } 922f85e193739c953358c865005855253af4f68a497John McCall 923ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = Builder.CreateBitCast(value, ConvertType(propType)); 924490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian value = Builder.CreateBitCast(value, 925490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian ConvertType(GetterMethodDecl->getResultType())); 926ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } 927ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 928ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall EmitReturnOfRValue(RValue::get(value), propType); 929ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian } 9301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 931c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 932af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 9331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm_unreachable("bad @property implementation strategy!"); 935af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 936af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 93741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value 93841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar. 93941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 94041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ObjCIvarDecl *ivar) { 9412846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // objc_copyStruct (&structIvar, &Arg, 9422846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // sizeof (struct something), true, false); 943bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall CallArgList args; 944bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 945bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The first argument is the address of the ivar. 94641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 94741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.LoadObjCSelf(), ivar, 0) 94841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall .getAddress(); 94941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 95041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 951bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 952bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The second argument is the address of the parameter variable. 95341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ParmVarDecl *argVar = *OMD->param_begin(); 954f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 955c3953aab1b6167b7f394ca87096e91ab840026adFariborz Jahanian VK_LValue, SourceLocation()); 95641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 95741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 95841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 959bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 960bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The third argument is the sizeof the type. 961bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall llvm::Value *size = 96241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 96341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(size), CGF.getContext().getSizeType()); 96441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 96541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fourth argument is the 'isAtomic' flag. 96641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 967bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 96841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fifth argument is the 'hasStrong' flag. 96941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // FIXME: should this really always be false? 97041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 971bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 97241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 9730f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 9740f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 9750f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 9760f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 97741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall copyStructFn, ReturnValueSlot(), args); 9782846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian} 9792846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian 980cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// emitCPPObjectAtomicSetterCall - Call the runtime function to store 981cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the value from the first formal parameter into the given ivar, using 982cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 983cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanianstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 984cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *OMD, 985cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar, 986cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 987cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 988cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // AtomicHelperFn); 989cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CallArgList args; 990cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 991cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The first argument is the address of the ivar. 992cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *ivarAddr = 993cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 994cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 995cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 996cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 997cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 998cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The second argument is the address of the parameter variable. 999cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ParmVarDecl *argVar = *OMD->param_begin(); 1000f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1001cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian VK_LValue, SourceLocation()); 1002cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 1003cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1004cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1005cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1006cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // Third argument is the helper function. 1007cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1008cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1009cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 1010d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 10110f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 10120f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 10130f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 10140f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1015cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 1016cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1017cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1018cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian} 1019cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 102020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 10211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 10221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expr *setter = PID->getSetterCXXAssignment(); 10231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!setter) return true; 10241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 10261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 102771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 102871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // An operator call is trivial if the function it calls is trivial. 10291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This also implies that there's nothing non-trivial going on with 10301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the arguments, because operator= can only be trivial if it's a 10311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // synthesized assignment operator and therefore both parameters are 10321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // references. 10331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 103471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (const FunctionDecl *callee 103571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 103671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (callee->isTrivial()) 103771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return true; 103871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 103901cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian } 104071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(setter)); 104271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 104371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 104486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar 10454e494cf56325f26a6c3eb5a065c35e54835f14e6Benjamin Kramerstatic bool UseOptimizedSetter(CodeGenModule &CGM) { 10464e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1047ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return false; 10487a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1049ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 1050ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 105171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid 105271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1053cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyImplDecl *propImpl, 1054cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1055cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1056cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1057cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); 1058cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 105971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Just use the setter expression if Sema gave us one and it's 1060cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // non-trivial. 10611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialSetExpr(propImpl)) { 1062cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian if (!AtomicHelperFn) 1063cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If non-atomic, assignment is called directly. 1064cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian EmitStmt(propImpl->getSetterCXXAssignment()); 1065cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian else 1066cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If atomic, assignment is called via a locking api. 1067cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1068cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian AtomicHelperFn); 106971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 107071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 107171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 10731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 10741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 1075aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 1076aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 1077aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 1078aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 10791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; 108071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue ivarLValue = 10821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 10831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = ivarLValue.getAddress(); 10841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 10861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 10871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 10881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 10891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 10901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 10911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Cast both arguments to the chosen operation type. 10931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall argAddr = Builder.CreateBitCast(argAddr, bitcastType); 10941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 109571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This bitcast load is likely to cause some nasty IR. 10971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *load = Builder.CreateLoad(argAddr); 10981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic store. There are no memory ordering requirements. 11001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 11011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAlignment(strategy.getIvarAlignment().getQuantity()); 11021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAtomic(llvm::Unordered); 11031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 11041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 11051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: 11071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1108ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 1109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setOptimizedPropertyFn = 0; 1110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setPropertyFn = 0; 1111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (UseOptimizedSetter(CGM)) { 11127a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar // 10.8 and iOS 6.0 code and GC is off 1113ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn = 111416098f366097305c348b356a44638d6c959c6e60Eric Christopher CGM.getObjCRuntime() 111516098f366097305c348b356a44638d6c959c6e60Eric Christopher .GetOptimizedPropertySetFunction(strategy.isAtomic(), 111616098f366097305c348b356a44638d6c959c6e60Eric Christopher strategy.isCopy()); 1117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setOptimizedPropertyFn) { 1118ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1119ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1120ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 112186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 1122ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else { 1123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setPropertyFn) { 1125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1128ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1129ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit objc_setProperty((id) self, _cmd, offset, arg, 113186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar // <is-atomic>, <is-copy>). 113271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *cmd = 113371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); 113471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *self = 113571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 113671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *ivarOffset = 113771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 113871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; 113971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); 114071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 114171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CallArgList args; 114271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 114371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 1144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (setOptimizedPropertyFn) { 1145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 11470f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11480f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11490f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn, ReturnValueSlot(), args); 1151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 1152ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1154ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1155ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1156ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // FIXME: We shouldn't need to get the function info here, the runtime 1159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // already should have computed it to build the function. 11600f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11610f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11620f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn, ReturnValueSlot(), args); 1164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 116671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 116771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 116871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 117041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall emitStructSetterCall(*this, setterMethod, ivar); 117171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 11721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 11741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall break; 117571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 117671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 117771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Otherwise, fake up some ASTs and emit a normal assignment. 117871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1179f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr self(selfDecl, false, selfDecl->getType(), 1180f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, SourceLocation()); 118171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 118271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall selfDecl->getType(), CK_LValueToRValue, &self, 118371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 118471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 118571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall SourceLocation(), &selfLoad, true, true); 118671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 118771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ParmVarDecl *argDecl = *setterMethod->param_begin(); 118871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall QualType argType = argDecl->getType().getNonReferenceType(); 1189f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); 119071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 119171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argType.getUnqualifiedType(), CK_LValueToRValue, 119271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall &arg, VK_RValue); 119345e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar 119471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The property type can differ from the ivar type in some situations with 119571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Objective-C pointer types, we can always bit cast the RHS in these cases. 119671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The following absurdity is just to ensure well-formed IR. 119771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CastKind argCK = CK_NoOp; 119871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (ivarRef.getType()->isObjCObjectPointerType()) { 119971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isObjCObjectPointerType()) 120071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 120171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else if (argLoad.getType()->isBlockPointerType()) 120271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BlockPointerToObjCPointerCast; 120371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 120471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_CPointerToObjCPointerCast; 120571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isBlockPointerType()) { 120671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isBlockPointerType()) 120771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 120871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 120971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_AnyPointerToBlockPointerCast; 121071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isPointerType()) { 121171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 121286957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 121371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 121471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), argCK, &argLoad, 121571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 121671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Expr *finalArg = &argLoad; 121771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 121871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argLoad.getType())) 121971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall finalArg = &argCast; 122071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 122171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 122271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 122371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), VK_RValue, OK_Ordinary, 1224be9af1288881110e406b87914162eaa59f1e5918Lang Hames SourceLocation(), false); 122571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitStmt(&assign); 122671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 122771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12282ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property setter function. 12292ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 12302ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 123171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category. 123271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 123371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyImplDecl *PID) { 1234b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 123520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicSetterCopyHelperFunction(PID); 123671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 123771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); 123871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall assert(OMD && "Invalid call to generate setter (empty method)"); 1239ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 124071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 1241cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1242af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1243af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(); 12444111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 12454111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 1246e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace { 12479928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall struct DestroyIvar : EHScopeStack::Cleanup { 12489928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall private: 12499928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr; 1250e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall const ObjCIvarDecl *ivar; 1251516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne CodeGenFunction::Destroyer *destroyer; 12529928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray; 12539928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall public: 12549928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 12559928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer, 12569928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray) 1257516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne : addr(addr), ivar(ivar), destroyer(destroyer), 12589928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall useEHCleanupForArray(useEHCleanupForArray) {} 1259e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1260ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 12619928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall LValue lvalue 12629928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 12639928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, 1264ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall flags.isForNormalCleanup() && useEHCleanupForArray); 1265e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1266e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall }; 1267e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1268e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 12699928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call. 12709928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF, 12719928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr, 12729928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType type) { 12739928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *null = getNullForVariable(addr); 12749928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 12759928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall} 1276f85e193739c953358c865005855253af4f68a497John McCall 1277e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF, 1278e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ObjCImplementationDecl *impl) { 1279e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CodeGenFunction::RunCleanupsScope scope(CGF); 1280e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1281e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall llvm::Value *self = CGF.LoadObjCSelf(); 1282e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1283db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1284db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1285e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ivar; ivar = ivar->getNextIvar()) { 1286e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall QualType type = ivar->getType(); 1287e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1288e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Check whether the ivar is a destructible type. 12899928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType::DestructionKind dtorKind = type.isDestructedType(); 12909928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (!dtorKind) continue; 12919928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 12929928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer = 0; 12939928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 12949928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Use a call to objc_storeStrong to destroy strong ivars, for the 12959928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // general benefit of the tools. 12969928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (dtorKind == QualType::DK_objc_strong_lifetime) { 1297516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = destroyARCStrongWithStore; 12989928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 12999928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Otherwise use the default for the destruction kind. 13009928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall } else { 1301516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = CGF.getDestroyer(dtorKind); 1302e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 13039928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 13059928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13069928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 13079928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall cleanupKind & EHCleanup); 1308e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1309e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1310e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1311e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1312e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1313109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1314109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *MD, 1315109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian bool ctor) { 1316109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 13178d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); 1318e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1319e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_construct. 1320109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian if (ctor) { 1321f85e193739c953358c865005855253af4f68a497John McCall // Suppress the final autorelease in ARC. 1322f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 1323f85e193739c953358c865005855253af4f68a497John McCall 13245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<CXXCtorInitializer *, 8> IvarInitializers; 1325e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), 1326e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall E = IMP->init_end(); B != E; ++B) { 1327e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CXXCtorInitializer *IvarInit = (*B); 132800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet FieldDecl *Field = IvarInit->getAnyMember(); 1329109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 13309b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 13319b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LoadObjCSelf(), Ivar, 0); 13327c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall EmitAggExpr(IvarInit->getInit(), 13337c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, 1334410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 1335649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 1336109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1337109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian // constructor returns 'self'. 1338109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenTypes &Types = CGM.getTypes(); 1339109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian QualType IdTy(CGM.getContext().getObjCIdType()); 1340109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian llvm::Value *SelfAsId = 1341109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1342109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1343e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1344e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_destruct. 1345bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth } else { 1346e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall emitCXXDestructMethod(*this, IMP); 1347109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1348109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian FinishFunction(); 1349109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian} 1350109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 13510b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { 13520b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); 13530b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian it++; it++; 13540b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian const ABIArgInfo &AI = it->info; 13550b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian // FIXME. Is this sufficient check? 13560b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian return (AI.getKind() == ABIArgInfo::Indirect); 13570b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian} 13580b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian 135915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { 13604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::NonGC) 136115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 136215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) 136315bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return FDTTy->getDecl()->hasObjectMember(); 136415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 136515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian} 136615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian 1367c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() { 1368b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 1369b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); 13704111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 13714111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 137245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() { 137345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 137445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 137514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 137614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getContext().getCanonicalType(selfDecl->getType())); 137745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian return PTy->getPointeeType(); 137845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian} 137945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian 138074391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *EnumerationMutationFn = 1382c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.getObjCRuntime().EnumerationMutationFunction(); 13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1384c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar if (!EnumerationMutationFn) { 1385c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1386c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 1387c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 1388c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 1389bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel CGDebugInfo *DI = getDebugInfo(); 139073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 139173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1392bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 13939d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel // The local variable comes into scope immediately. 13949d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel AutoVarEmission variable = AutoVarEmission::invalid(); 13959d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 13969d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 13979d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel 1398d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 13991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1400f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Fast enumeration state. 14010815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor QualType StateTy = CGM.getObjCFastEnumerationStateType(); 1402195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); 14031884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson EmitNullInitialization(StatePtr, StateTy); 14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1405f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Number of elements in the items array. 14062abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson static const unsigned NumItems = 16; 14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1408d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the countByEnumeratingWithState:objects:count: selector. 1409ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer IdentifierInfo *II[] = { 1410ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1411ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("objects"), 1412ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("count") 1413ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer }; 1414ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer Selector FastEnumSel = 1415ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1416f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1417f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson QualType ItemsTy = 1418f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().getConstantArrayType(getContext().getObjCIdType(), 14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::APInt(32, NumItems), 1420f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson ArrayType::Normal, 0); 1421195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1423990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Emit the collection pointer. In ARC, we do a retain. 1424990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall llvm::Value *Collection; 14254e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 1426990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitARCRetainScalarExpr(S.getCollection()); 1427990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1428990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Enter a cleanup to do the release. 1429990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1430990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else { 1431990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitScalarExpr(S.getCollection()); 1432990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 14331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14344b302d32378b364703b212834f908762e570c29cJohn McCall // The 'continue' label needs to appear within the cleanup for the 14354b302d32378b364703b212834f908762e570c29cJohn McCall // collection object. 14364b302d32378b364703b212834f908762e570c29cJohn McCall JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 14374b302d32378b364703b212834f908762e570c29cJohn McCall 1438d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Send it our message: 1439f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson CallArgList Args; 1440d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1441d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The first argument is a temporary of the enumeration-state type. 144204c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); 14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1444d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The second argument is a temporary array with space for NumItems 1445d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointers. We'll actually be loading elements from the array 1446d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointer written into the control state; this buffer is so that 1447d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // collections that *aren't* backed by arrays can still queue up 1448d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // batches of elements. 144904c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); 14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1451d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The third argument is the capacity of that temporary array. 14522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); 14534a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); 145404c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(Count), getContext().UnsignedLongTy); 14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1456d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start the enumeration. 14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RValue CountRV = 1458ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1459f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 1460f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson FastEnumSel, 1461c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 1462f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1463d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The initial number of objects that were returned in the buffer. 1464d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1466d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1467d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1468f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1469d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); 1470f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1471d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If the limit pointer was zero to begin with, the collection is 1472d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // empty; skip all this. 1473d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), 1474d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopInitBB); 14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1476d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, initialize the loop. 1477d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopInitBB); 14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1479d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Save the initial mutations value. This is the value at an 1480d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // address that was written into the state object by 1481d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // countByEnumeratingWithState:objects:count:. 14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtrPtr = 14832abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); 14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, 14852abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson "mutationsptr"); 14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1487d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialMutations = 1488d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); 14891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1490d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start looping. This is the point we return to whenever we have a 1491d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // fresh, non-empty batch of objects. 1492d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1493d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopBodyBB); 14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1495d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current index into the buffer. 1496bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); 1497d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, LoopInitBB); 1498f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1499d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current buffer size. 1500bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); 1501d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(initialBufferLimit, LoopInitBB); 1502f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1503d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether the mutations value has changed from where it was 1504d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // at start. StateMutationsPtr should actually be invariant between 1505d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes. 15062abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1507d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *currentMutations 1508d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateLoad(StateMutationsPtr, "statemutations"); 15091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1510d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1511361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1513d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1514d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall WasNotMutatedBB, WasMutatedBB); 15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1516d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If so, call the enumeration-mutation function. 1517d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasMutatedBB); 15182abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson llvm::Value *V = 15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Builder.CreateBitCast(Collection, 1520578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer ConvertType(getContext().getObjCIdType())); 15212b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar CallArgList Args2; 152204c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args2.add(RValue::get(V), getContext().getObjCIdType()); 1523f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We shouldn't need to get the function info here, the runtime already 1524f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // should have computed it to build the function. 15250f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2, 15260f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 15270f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1528f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson EnumerationMutationFn, ReturnValueSlot(), Args2); 15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1530d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, or if the mutation function returns, just continue. 1531d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasNotMutatedBB); 15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1533d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Initialize the element variable. 1534d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope elementVariableScope(*this); 153557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall bool elementIsVariable; 1536d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LValue elementLValue; 1537d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall QualType elementType; 1538d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 153957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // Initialize the variable, in case it's a __block variable or something. 154057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarInit(variable); 1541f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 154257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); 1543f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), 1544d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall VK_LValue, SourceLocation()); 1545d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(&tempDRE); 1546d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = D->getType(); 154757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = true; 15487acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 15497acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (D->isARCPseudoStrong()) 15507acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1551d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } else { 1552d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = LValue(); // suppress warning 1553d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = cast<Expr>(S.getElement())->getType(); 155457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = false; 1555d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 15562acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *convertedElementType = ConvertType(elementType); 1557f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1558d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the buffer out of the enumeration state. 1559d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // TODO: this pointer should actually be invariant between 1560d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes, which would help us do certain loop optimizations. 1561d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *StateItemsPtr = 1562d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); 1563d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *EnumStateItems = 1564d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateItemsPtr, "stateitems"); 1565f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1566d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the value at the current index from the buffer. 15671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *CurrentItemPtr = 1568d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 1569d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); 15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1571d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Cast that value to the right type. 1572d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1573d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall "currentitem"); 15741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1575d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Make sure we have an l-value. Yes, this gets evaluated every 1576d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // time through the loop. 15777acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (!elementIsVariable) { 1578d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1579545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 15807acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } else { 15817acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall EmitScalarInit(CurrentItem, elementLValue); 15827acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } 15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 158457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // If we do have an element variable, this assignment is the end of 158557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // its initialization. 158657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (elementIsVariable) 158757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarCleanups(variable); 158857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall 1589d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Perform the loop body, setting up break and continue labels. 1590e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1591d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall { 1592d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope Scope(*this); 1593d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitStmt(S.getBody()); 1594d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 1595f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson BreakContinueStack.pop_back(); 15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1597d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Destroy the element variable now. 1598d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementVariableScope.ForceCleanup(); 1599d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1600d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether there are more elements. 1601ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(AfterBody.getBlock()); 16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1603d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1604d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1605d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // First we check in the local buffer. 1606d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *indexPlusOne 1607d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); 1608d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1609d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we haven't overrun the buffer yet, we can continue. 1610d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), 1611d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LoopBodyBB, FetchMoreBB); 1612f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian 1613d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1614d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(count, AfterBody.getBlock()); 1615f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1616d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, we have to fetch more elements. 1617d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(FetchMoreBB); 16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CountRV = 1620ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1621f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FastEnumSel, 1623c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 16241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1625d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we got a zero count, we're done. 1626d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *refetchCount = CountRV.getScalarVal(); 1627d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1628d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // (note that the message send might split FetchMoreBB) 1629d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, Builder.GetInsertBlock()); 1630d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1631d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1632d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1633d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopBodyBB); 16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1635f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // No more elements. 1636d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(EmptyBB); 1637f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 163857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (!elementIsVariable) { 1639f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // If the element was not a declaration, set it to be null. 1640f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1641d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1642d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1643545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(null), elementLValue); 1644f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson } 1645f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 164673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 164773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1648bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 1649990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Leave the cleanup we entered in ARC. 16504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) 1651990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall PopCleanupBlock(); 1652990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1653ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopEnd.getBlock()); 16543d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 16553d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1657f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitTryStmt(*this, S); 165864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 165964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 166164d5d6c5903157c521af496479d06dc26032d718Anders Carlsson CGM.getObjCRuntime().EmitThrowStmt(*this, S); 166264d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 166364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 166410cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt( 16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ObjCAtSynchronizedStmt &S) { 1666f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 166710cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner} 166810cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner 166933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCProduceObject. Just does a 1670f85e193739c953358c865005855253af4f68a497John McCall/// primitive retain. 1671f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, 1672f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1673f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetain(type, value); 1674f85e193739c953358c865005855253af4f68a497John McCall} 1675f85e193739c953358c865005855253af4f68a497John McCall 1676f85e193739c953358c865005855253af4f68a497John McCallnamespace { 1677f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCRelease : EHScopeStack::Cleanup { 1678bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall CallObjCRelease(llvm::Value *object) : object(object) {} 1679bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall llvm::Value *object; 1680f85e193739c953358c865005855253af4f68a497John McCall 1681ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 1682f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitARCRelease(object, /*precise*/ true); 1683f85e193739c953358c865005855253af4f68a497John McCall } 1684f85e193739c953358c865005855253af4f68a497John McCall }; 1685f85e193739c953358c865005855253af4f68a497John McCall} 1686f85e193739c953358c865005855253af4f68a497John McCall 168733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject. Does a primitive 1688f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression. 1689f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1690f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *object) { 1691f85e193739c953358c865005855253af4f68a497John McCall // If we're in a conditional branch, we need to make the cleanup 1692bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall // conditional. 1693bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1694f85e193739c953358c865005855253af4f68a497John McCall return object; 1695f85e193739c953358c865005855253af4f68a497John McCall} 1696f85e193739c953358c865005855253af4f68a497John McCall 1697f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1698f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1699f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutorelease(type, value); 1700f85e193739c953358c865005855253af4f68a497John McCall} 1701f85e193739c953358c865005855253af4f68a497John McCall 1702f85e193739c953358c865005855253af4f68a497John McCall 1703f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, 17042acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *type, 17055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1706f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); 1707f85e193739c953358c865005855253af4f68a497John McCall 1708554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { 1709cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // If the target runtime doesn't naturally support ARC, emit weak 1710cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // references to the runtime support library. We don't really 1711cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // permit this to fail, but we need a particular relocation style. 1712554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 1713f85e193739c953358c865005855253af4f68a497John McCall f->setLinkage(llvm::Function::ExternalWeakLinkage); 1714554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } else if (fnName == "objc_retain" || fnName == "objc_release") { 1715554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // If we have Native ARC, set nonlazybind attribute for these APIs for 1716554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // performance. 171772390b39c545426023ec104afe8706395d732badBill Wendling f->addFnAttr(llvm::Attribute::NonLazyBind); 1718db99e8b72ae2b66d3c2f8d896f49513b8d8e2633Michael Gottesman } 1719554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } 1720f85e193739c953358c865005855253af4f68a497John McCall 1721f85e193739c953358c865005855253af4f68a497John McCall return fn; 1722f85e193739c953358c865005855253af4f68a497John McCall} 1723f85e193739c953358c865005855253af4f68a497John McCall 1724f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature 1725f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8*) 1726f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null. 1727f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, 1728f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1729f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 1730df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier StringRef fnName, 1731df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier bool isTailCall = false) { 1732f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 1733f85e193739c953358c865005855253af4f68a497John McCall 1734f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 17352acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 173676ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 1737f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1738f85e193739c953358c865005855253af4f68a497John McCall } 1739f85e193739c953358c865005855253af4f68a497John McCall 1740f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 17412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1742f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 1743f85e193739c953358c865005855253af4f68a497John McCall 1744f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1745bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 1746df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier if (isTailCall) 1747df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier call->setTailCall(); 1748f85e193739c953358c865005855253af4f68a497John McCall 1749f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to the original type. 1750f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(call, origType); 1751f85e193739c953358c865005855253af4f68a497John McCall} 1752f85e193739c953358c865005855253af4f68a497John McCall 1753f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1754f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**) 1755f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, 1756f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1757f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 17585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1759f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 17602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 176176ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); 1762f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1763f85e193739c953358c865005855253af4f68a497John McCall } 1764f85e193739c953358c865005855253af4f68a497John McCall 1765f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 17662acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = addr->getType(); 1767f85e193739c953358c865005855253af4f68a497John McCall addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 1768f85e193739c953358c865005855253af4f68a497John McCall 1769f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1770bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr); 1771f85e193739c953358c865005855253af4f68a497John McCall 1772f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to a dereference of the original type. 1773f85e193739c953358c865005855253af4f68a497John McCall if (origType != CGF.Int8PtrPtrTy) 1774f85e193739c953358c865005855253af4f68a497John McCall result = CGF.Builder.CreateBitCast(result, 1775f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(origType)->getElementType()); 1776f85e193739c953358c865005855253af4f68a497John McCall 1777f85e193739c953358c865005855253af4f68a497John McCall return result; 1778f85e193739c953358c865005855253af4f68a497John McCall} 1779f85e193739c953358c865005855253af4f68a497John McCall 1780f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1781f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**, i8*) 1782f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, 1783f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1784f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1785f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 17865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName, 1787f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 1788f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 1789f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 1790f85e193739c953358c865005855253af4f68a497John McCall 1791f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 17921d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; 1793f85e193739c953358c865005855253af4f68a497John McCall 17942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1795f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); 1796f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1797f85e193739c953358c865005855253af4f68a497John McCall } 1798f85e193739c953358c865005855253af4f68a497John McCall 17992acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1800f85e193739c953358c865005855253af4f68a497John McCall 1801bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1802bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy), 1803bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 1804bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1805bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 1806f85e193739c953358c865005855253af4f68a497John McCall 1807f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 1808f85e193739c953358c865005855253af4f68a497John McCall 1809f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(result, origType); 1810f85e193739c953358c865005855253af4f68a497John McCall} 1811f85e193739c953358c865005855253af4f68a497John McCall 1812f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1813f85e193739c953358c865005855253af4f68a497John McCall/// void (i8**, i8**) 1814f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF, 1815f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *dst, 1816f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *src, 1817f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1819f85e193739c953358c865005855253af4f68a497John McCall assert(dst->getType() == src->getType()); 1820f85e193739c953358c865005855253af4f68a497John McCall 1821f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 182276ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; 182376ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer 18242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1825f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); 1826f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1827f85e193739c953358c865005855253af4f68a497John McCall } 1828f85e193739c953358c865005855253af4f68a497John McCall 1829bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1830bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy), 1831bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy) 1832bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1833bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(fn, args); 1834f85e193739c953358c865005855253af4f68a497John McCall} 1835f85e193739c953358c865005855253af4f68a497John McCall 1836f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain. Based on the type, calls one of: 18379d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 18389d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1839f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 1840f85e193739c953358c865005855253af4f68a497John McCall if (type->isBlockPointerType()) 1841348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitARCRetainBlock(value, /*mandatory*/ false); 1842f85e193739c953358c865005855253af4f68a497John McCall else 1843f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainNonBlock(value); 1844f85e193739c953358c865005855253af4f68a497John McCall} 1845f85e193739c953358c865005855253af4f68a497John McCall 1846f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics. 18479d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 1848f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 1849f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1850f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retain, 1851f85e193739c953358c865005855253af4f68a497John McCall "objc_retain"); 1852f85e193739c953358c865005855253af4f68a497John McCall} 1853f85e193739c953358c865005855253af4f68a497John McCall 1854f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics. 18559d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1856348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// 1857348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata 1858348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call 1859348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack. 1860348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 1861348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool mandatory) { 1862348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result 1863348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = emitARCValueOperation(*this, value, 1864348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall CGM.getARCEntrypoints().objc_retainBlock, 1865348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall "objc_retainBlock"); 1866348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1867348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 1868348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // tell the optimizer that it doesn't need to do this copy if the 1869348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // block doesn't escape, where being passed as an argument doesn't 1870348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // count as escaping. 1871348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (!mandatory && isa<llvm::Instruction>(result)) { 1872348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::CallInst *call 1873348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = cast<llvm::CallInst>(result->stripPointerCasts()); 1874348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); 1875348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1876348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall SmallVector<llvm::Value*,1> args; 1877348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall call->setMetadata("clang.arc.copy_on_escape", 1878348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::MDNode::get(Builder.getContext(), args)); 1879348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 1880348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1881348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return result; 1882f85e193739c953358c865005855253af4f68a497John McCall} 1883f85e193739c953358c865005855253af4f68a497John McCall 1884f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object which is the result of a function call. 18859d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 1886f85e193739c953358c865005855253af4f68a497John McCall/// 1887f85e193739c953358c865005855253af4f68a497John McCall/// Yes, this function name is one character away from a different 1888f85e193739c953358c865005855253af4f68a497John McCall/// call with completely different semantics. 1889f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 1890f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 1891f85e193739c953358c865005855253af4f68a497John McCall // Fetch the void(void) inline asm which marks that we're going to 1892f85e193739c953358c865005855253af4f68a497John McCall // retain the autoreleased return value. 1893f85e193739c953358c865005855253af4f68a497John McCall llvm::InlineAsm *&marker 1894f85e193739c953358c865005855253af4f68a497John McCall = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; 1895f85e193739c953358c865005855253af4f68a497John McCall if (!marker) { 18965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef assembly 1897f85e193739c953358c865005855253af4f68a497John McCall = CGM.getTargetCodeGenInfo() 1898f85e193739c953358c865005855253af4f68a497John McCall .getARCRetainAutoreleasedReturnValueMarker(); 1899f85e193739c953358c865005855253af4f68a497John McCall 1900f85e193739c953358c865005855253af4f68a497John McCall // If we have an empty assembly string, there's nothing to do. 1901f85e193739c953358c865005855253af4f68a497John McCall if (assembly.empty()) { 1902f85e193739c953358c865005855253af4f68a497John McCall 1903f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, at -O0, build an inline asm that we're going to call 1904f85e193739c953358c865005855253af4f68a497John McCall // in a moment. 1905f85e193739c953358c865005855253af4f68a497John McCall } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1906f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType *type = 19078b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(VoidTy, /*variadic*/false); 1908f85e193739c953358c865005855253af4f68a497John McCall 1909f85e193739c953358c865005855253af4f68a497John McCall marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 1910f85e193739c953358c865005855253af4f68a497John McCall 1911f85e193739c953358c865005855253af4f68a497John McCall // If we're at -O1 and above, we don't want to litter the code 1912f85e193739c953358c865005855253af4f68a497John McCall // with this marker yet, so leave a breadcrumb for the ARC 1913f85e193739c953358c865005855253af4f68a497John McCall // optimizer to pick up. 1914f85e193739c953358c865005855253af4f68a497John McCall } else { 1915f85e193739c953358c865005855253af4f68a497John McCall llvm::NamedMDNode *metadata = 1916f85e193739c953358c865005855253af4f68a497John McCall CGM.getModule().getOrInsertNamedMetadata( 1917f85e193739c953358c865005855253af4f68a497John McCall "clang.arc.retainAutoreleasedReturnValueMarker"); 1918f85e193739c953358c865005855253af4f68a497John McCall assert(metadata->getNumOperands() <= 1); 1919f85e193739c953358c865005855253af4f68a497John McCall if (metadata->getNumOperands() == 0) { 1920f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); 1921da549e8995c447542d5631b8b67fcc3a9582797aJay Foad metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); 1922f85e193739c953358c865005855253af4f68a497John McCall } 1923f85e193739c953358c865005855253af4f68a497John McCall } 1924f85e193739c953358c865005855253af4f68a497John McCall } 1925f85e193739c953358c865005855253af4f68a497John McCall 1926f85e193739c953358c865005855253af4f68a497John McCall // Call the marker asm if we made one, which we do only at -O0. 1927f85e193739c953358c865005855253af4f68a497John McCall if (marker) Builder.CreateCall(marker); 1928f85e193739c953358c865005855253af4f68a497John McCall 1929f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1930f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, 1931f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutoreleasedReturnValue"); 1932f85e193739c953358c865005855253af4f68a497John McCall} 1933f85e193739c953358c865005855253af4f68a497John McCall 1934f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object. 19359d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_release(i8* %value) 1936f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, bool precise) { 1937f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return; 1938f85e193739c953358c865005855253af4f68a497John McCall 1939f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; 1940f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 19412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 194276ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 1943f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); 1944f85e193739c953358c865005855253af4f68a497John McCall } 1945f85e193739c953358c865005855253af4f68a497John McCall 1946f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 1947f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 1948f85e193739c953358c865005855253af4f68a497John McCall 1949f85e193739c953358c865005855253af4f68a497John McCall // Call objc_release. 1950bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 1951f85e193739c953358c865005855253af4f68a497John McCall 1952f85e193739c953358c865005855253af4f68a497John McCall if (!precise) { 19535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*,1> args; 1954f85e193739c953358c865005855253af4f68a497John McCall call->setMetadata("clang.imprecise_release", 1955f85e193739c953358c865005855253af4f68a497John McCall llvm::MDNode::get(Builder.getContext(), args)); 1956f85e193739c953358c865005855253af4f68a497John McCall } 1957f85e193739c953358c865005855253af4f68a497John McCall} 1958f85e193739c953358c865005855253af4f68a497John McCall 1959015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// Destroy a __strong variable. 1960015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 1961015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O0, emit a call to store 'null' into the address; 1962015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// instrumenting tools prefer this because the address is exposed, 1963015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// but it's relatively cumbersome to optimize. 1964015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 1965015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O1 and above, just load and call objc_release. 1966015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 1967015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// call void \@objc_storeStrong(i8** %addr, i8* null) 1968015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCallvoid CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr, bool precise) { 1969015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1970015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType()); 1971015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *null = llvm::ConstantPointerNull::get( 1972015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall cast<llvm::PointerType>(addrTy->getElementType())); 1973015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 1974015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall return; 1975015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall } 1976015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 1977015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *value = Builder.CreateLoad(addr); 1978015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCRelease(value, precise); 1979015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall} 1980015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 1981f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Always calls this: 19829d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 1983f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, 1984f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1985f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 1986f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 1987f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 1988f85e193739c953358c865005855253af4f68a497John McCall 1989f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; 1990f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 19919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; 19922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1993f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); 1994f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); 1995f85e193739c953358c865005855253af4f68a497John McCall } 1996f85e193739c953358c865005855253af4f68a497John McCall 1997bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1998bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(addr, Int8PtrPtrTy), 1999bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(value, Int8PtrTy) 2000bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 2001bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, args); 2002f85e193739c953358c865005855253af4f68a497John McCall 2003f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 2004f85e193739c953358c865005855253af4f68a497John McCall return value; 2005f85e193739c953358c865005855253af4f68a497John McCall} 2006f85e193739c953358c865005855253af4f68a497John McCall 2007f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Sometimes calls this: 20089d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2009f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components. 2010545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2011f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newValue, 2012f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2013545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall QualType type = dst.getType(); 2014f85e193739c953358c865005855253af4f68a497John McCall bool isBlock = type->isBlockPointerType(); 2015f85e193739c953358c865005855253af4f68a497John McCall 2016f85e193739c953358c865005855253af4f68a497John McCall // Use a store barrier at -O0 unless this is a block type or the 2017f85e193739c953358c865005855253af4f68a497John McCall // lvalue is inadequately aligned. 2018f85e193739c953358c865005855253af4f68a497John McCall if (shouldUseFusedARCCalls() && 2019f85e193739c953358c865005855253af4f68a497John McCall !isBlock && 20206da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman (dst.getAlignment().isZero() || 20216da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2022f85e193739c953358c865005855253af4f68a497John McCall return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); 2023f85e193739c953358c865005855253af4f68a497John McCall } 2024f85e193739c953358c865005855253af4f68a497John McCall 2025f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, split it out. 2026f85e193739c953358c865005855253af4f68a497John McCall 2027f85e193739c953358c865005855253af4f68a497John McCall // Retain the new value. 2028f85e193739c953358c865005855253af4f68a497John McCall newValue = EmitARCRetain(type, newValue); 2029f85e193739c953358c865005855253af4f68a497John McCall 2030f85e193739c953358c865005855253af4f68a497John McCall // Read the old value. 2031545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall llvm::Value *oldValue = EmitLoadOfScalar(dst); 2032f85e193739c953358c865005855253af4f68a497John McCall 2033f85e193739c953358c865005855253af4f68a497John McCall // Store. We do this before the release so that any deallocs won't 2034f85e193739c953358c865005855253af4f68a497John McCall // see the old value. 2035545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreOfScalar(newValue, dst); 2036f85e193739c953358c865005855253af4f68a497John McCall 2037f85e193739c953358c865005855253af4f68a497John McCall // Finally, release the old value. 2038f85e193739c953358c865005855253af4f68a497John McCall EmitARCRelease(oldValue, /*precise*/ false); 2039f85e193739c953358c865005855253af4f68a497John McCall 2040f85e193739c953358c865005855253af4f68a497John McCall return newValue; 2041f85e193739c953358c865005855253af4f68a497John McCall} 2042f85e193739c953358c865005855253af4f68a497John McCall 2043f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 20449d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %value) 2045f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2046f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2047f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autorelease, 2048f85e193739c953358c865005855253af4f68a497John McCall "objc_autorelease"); 2049f85e193739c953358c865005855253af4f68a497John McCall} 2050f85e193739c953358c865005855253af4f68a497John McCall 2051f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 20529d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2053f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2054f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2055f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2056f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autoreleaseReturnValue, 2057df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_autoreleaseReturnValue", 2058df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2059f85e193739c953358c865005855253af4f68a497John McCall} 2060f85e193739c953358c865005855253af4f68a497John McCall 2061f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 20629d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2063f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2064f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2065f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2066f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, 2067df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_retainAutoreleaseReturnValue", 2068df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2069f85e193739c953358c865005855253af4f68a497John McCall} 2070f85e193739c953358c865005855253af4f68a497John McCall 2071f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 20729d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2073f85e193739c953358c865005855253af4f68a497John McCall/// or 20749d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// %retain = call i8* \@objc_retainBlock(i8* %value) 20759d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %retain) 2076f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2077f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2078f85e193739c953358c865005855253af4f68a497John McCall if (!type->isBlockPointerType()) 2079f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutoreleaseNonBlock(value); 2080f85e193739c953358c865005855253af4f68a497John McCall 2081f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 2082f85e193739c953358c865005855253af4f68a497John McCall 20832acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 2084f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2085348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ true); 2086f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2087f85e193739c953358c865005855253af4f68a497John McCall return Builder.CreateBitCast(value, origType); 2088f85e193739c953358c865005855253af4f68a497John McCall} 2089f85e193739c953358c865005855253af4f68a497John McCall 2090f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 20919d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2092f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2093f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2094f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2095f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutorelease, 2096f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutorelease"); 2097f85e193739c953358c865005855253af4f68a497John McCall} 2098f85e193739c953358c865005855253af4f68a497John McCall 20999d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeak(i8** %addr) 2100f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 2101f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { 2102f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2103f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeak, 2104f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeak"); 2105f85e193739c953358c865005855253af4f68a497John McCall} 2106f85e193739c953358c865005855253af4f68a497John McCall 21079d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeakRetained(i8** %addr) 2108f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { 2109f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2110f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeakRetained, 2111f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeakRetained"); 2112f85e193739c953358c865005855253af4f68a497John McCall} 2113f85e193739c953358c865005855253af4f68a497John McCall 21149d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2115f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. 2116f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, 2117f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2118f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2119f85e193739c953358c865005855253af4f68a497John McCall return emitARCStoreOperation(*this, addr, value, 2120f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_storeWeak, 2121f85e193739c953358c865005855253af4f68a497John McCall "objc_storeWeak", ignored); 2122f85e193739c953358c865005855253af4f68a497John McCall} 2123f85e193739c953358c865005855253af4f68a497John McCall 21249d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_initWeak(i8** %addr, i8* %value) 2125f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. %addr is known to not have a current weak entry. 2126f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to: 2127f85e193739c953358c865005855253af4f68a497John McCall/// *addr = nil; objc_storeWeak(addr, value); 2128f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { 2129f85e193739c953358c865005855253af4f68a497John McCall // If we're initializing to null, just write null to memory; no need 2130f85e193739c953358c865005855253af4f68a497John McCall // to get the runtime involved. But don't do this if optimization 2131f85e193739c953358c865005855253af4f68a497John McCall // is enabled, because accounting for this would make the optimizer 2132f85e193739c953358c865005855253af4f68a497John McCall // much more complicated. 2133f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value) && 2134f85e193739c953358c865005855253af4f68a497John McCall CGM.getCodeGenOpts().OptimizationLevel == 0) { 2135f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(value, addr); 2136f85e193739c953358c865005855253af4f68a497John McCall return; 2137f85e193739c953358c865005855253af4f68a497John McCall } 2138f85e193739c953358c865005855253af4f68a497John McCall 2139f85e193739c953358c865005855253af4f68a497John McCall emitARCStoreOperation(*this, addr, value, 2140f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_initWeak, 2141f85e193739c953358c865005855253af4f68a497John McCall "objc_initWeak", /*ignored*/ true); 2142f85e193739c953358c865005855253af4f68a497John McCall} 2143f85e193739c953358c865005855253af4f68a497John McCall 21449d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_destroyWeak(i8** %addr) 2145f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil). 2146f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { 2147f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; 2148f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 21492acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 215076ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); 2151f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); 2152f85e193739c953358c865005855253af4f68a497John McCall } 2153f85e193739c953358c865005855253af4f68a497John McCall 2154f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 2155f85e193739c953358c865005855253af4f68a497John McCall addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2156f85e193739c953358c865005855253af4f68a497John McCall 2157bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, addr); 2158f85e193739c953358c865005855253af4f68a497John McCall} 2159f85e193739c953358c865005855253af4f68a497John McCall 21609d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_moveWeak(i8** %dest, i8** %src) 2161f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Leaves %src pointing to nothing. 2162f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 2163f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { 2164f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2165f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_moveWeak, 2166f85e193739c953358c865005855253af4f68a497John McCall "objc_moveWeak"); 2167f85e193739c953358c865005855253af4f68a497John McCall} 2168f85e193739c953358c865005855253af4f68a497John McCall 21699d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_copyWeak(i8** %dest, i8** %src) 2170f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Essentially 2171f85e193739c953358c865005855253af4f68a497John McCall/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 2172f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { 2173f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2174f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_copyWeak, 2175f85e193739c953358c865005855253af4f68a497John McCall "objc_copyWeak"); 2176f85e193739c953358c865005855253af4f68a497John McCall} 2177f85e193739c953358c865005855253af4f68a497John McCall 2178f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push. 21799d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleasePoolPush(void) 2180f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 2181f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; 2182f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 21832acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 2184f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType::get(Int8PtrTy, false); 2185f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); 2186f85e193739c953358c865005855253af4f68a497John McCall } 2187f85e193739c953358c865005855253af4f68a497John McCall 2188bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return EmitNounwindRuntimeCall(fn); 2189f85e193739c953358c865005855253af4f68a497John McCall} 2190f85e193739c953358c865005855253af4f68a497John McCall 2191f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 21929d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_autoreleasePoolPop(i8* %ptr) 2193f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2194f85e193739c953358c865005855253af4f68a497John McCall assert(value->getType() == Int8PtrTy); 2195f85e193739c953358c865005855253af4f68a497John McCall 2196f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; 2197f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 21982acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 219976ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2200f85e193739c953358c865005855253af4f68a497John McCall 2201f85e193739c953358c865005855253af4f68a497John McCall // We don't want to use a weak import here; instead we should not 2202f85e193739c953358c865005855253af4f68a497John McCall // fall into this path. 2203f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); 2204f85e193739c953358c865005855253af4f68a497John McCall } 2205f85e193739c953358c865005855253af4f68a497John McCall 2206bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, value); 2207f85e193739c953358c865005855253af4f68a497John McCall} 2208f85e193739c953358c865005855253af4f68a497John McCall 2209f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push. 2210f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init]; 2211f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2212f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class. 2213f85e193739c953358c865005855253af4f68a497John McCall/// 2214f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2215f85e193739c953358c865005855253af4f68a497John McCall CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2216bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2217f85e193739c953358c865005855253af4f68a497John McCall // [NSAutoreleasePool alloc] 2218f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2219f85e193739c953358c865005855253af4f68a497John McCall Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2220f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2221f85e193739c953358c865005855253af4f68a497John McCall RValue AllocRV = 2222f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2223f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2224f85e193739c953358c865005855253af4f68a497John McCall AllocSel, Receiver, Args); 2225f85e193739c953358c865005855253af4f68a497John McCall 2226f85e193739c953358c865005855253af4f68a497John McCall // [Receiver init] 2227f85e193739c953358c865005855253af4f68a497John McCall Receiver = AllocRV.getScalarVal(); 2228f85e193739c953358c865005855253af4f68a497John McCall II = &CGM.getContext().Idents.get("init"); 2229f85e193739c953358c865005855253af4f68a497John McCall Selector InitSel = getContext().Selectors.getSelector(0, &II); 2230f85e193739c953358c865005855253af4f68a497John McCall RValue InitRV = 2231f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2232f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2233f85e193739c953358c865005855253af4f68a497John McCall InitSel, Receiver, Args); 2234f85e193739c953358c865005855253af4f68a497John McCall return InitRV.getScalarVal(); 2235f85e193739c953358c865005855253af4f68a497John McCall} 2236f85e193739c953358c865005855253af4f68a497John McCall 2237f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 2238f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain]; 2239f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2240f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2241f85e193739c953358c865005855253af4f68a497John McCall Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2242f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2243f85e193739c953358c865005855253af4f68a497John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2244f85e193739c953358c865005855253af4f68a497John McCall getContext().VoidTy, DrainSel, Arg, Args); 2245f85e193739c953358c865005855253af4f68a497John McCall} 2246f85e193739c953358c865005855253af4f68a497John McCall 2247bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 2248bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2249bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2250015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall CGF.EmitARCDestroyStrong(addr, /*precise*/ true); 2251bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2252bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2253bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 2254bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2255bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2256015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall CGF.EmitARCDestroyStrong(addr, /*precise*/ false); 2257bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2258bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2259bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 2260bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2261bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2262bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall CGF.EmitARCDestroyWeak(addr); 2263bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2264bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2265f85e193739c953358c865005855253af4f68a497John McCallnamespace { 2266f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { 2267f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2268f85e193739c953358c865005855253af4f68a497John McCall 2269f85e193739c953358c865005855253af4f68a497John McCall CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2270f85e193739c953358c865005855253af4f68a497John McCall 2271ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 2272f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCAutoreleasePoolPop(Token); 2273f85e193739c953358c865005855253af4f68a497John McCall } 2274f85e193739c953358c865005855253af4f68a497John McCall }; 2275f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { 2276f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2277f85e193739c953358c865005855253af4f68a497John McCall 2278f85e193739c953358c865005855253af4f68a497John McCall CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2279f85e193739c953358c865005855253af4f68a497John McCall 2280ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 2281f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2282f85e193739c953358c865005855253af4f68a497John McCall } 2283f85e193739c953358c865005855253af4f68a497John McCall }; 2284f85e193739c953358c865005855253af4f68a497John McCall} 2285f85e193739c953358c865005855253af4f68a497John McCall 2286f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 22874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 2288f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2289f85e193739c953358c865005855253af4f68a497John McCall else 2290f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2291f85e193739c953358c865005855253af4f68a497John McCall} 2292f85e193739c953358c865005855253af4f68a497John McCall 2293f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2294f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2295f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2296f85e193739c953358c865005855253af4f68a497John McCall switch (type.getObjCLifetime()) { 2297f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 2298f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 2299f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 2300f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 2301545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(), 2302f85e193739c953358c865005855253af4f68a497John McCall false); 2303f85e193739c953358c865005855253af4f68a497John McCall 2304f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 2305f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), 2306f85e193739c953358c865005855253af4f68a497John McCall true); 2307f85e193739c953358c865005855253af4f68a497John McCall } 2308f85e193739c953358c865005855253af4f68a497John McCall 2309f85e193739c953358c865005855253af4f68a497John McCall llvm_unreachable("impossible lifetime!"); 2310f85e193739c953358c865005855253af4f68a497John McCall} 2311f85e193739c953358c865005855253af4f68a497John McCall 2312f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2313f85e193739c953358c865005855253af4f68a497John McCall const Expr *e) { 2314f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2315f85e193739c953358c865005855253af4f68a497John McCall QualType type = e->getType(); 2316f85e193739c953358c865005855253af4f68a497John McCall 23172148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // If we're loading retained from a __strong xvalue, we can avoid 23182148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // an extra retain/release pair by zeroing out the source of this 23192148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // "move" operation. 23202148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall if (e->isXValue() && 23212148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall !type.isConstQualified() && 23222148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall type.getObjCLifetime() == Qualifiers::OCL_Strong) { 23232148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Emit the lvalue. 23242148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall LValue lv = CGF.EmitLValue(e); 23252148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23262148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Load the object pointer. 23272148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal(); 23282148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23292148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Set the source pointer to NULL. 23302148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); 23312148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23322148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall return TryEmitResult(result, true); 23332148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall } 23342148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 2335f85e193739c953358c865005855253af4f68a497John McCall // As a very special optimization, in ARC++, if the l-value is the 2336f85e193739c953358c865005855253af4f68a497John McCall // result of a non-volatile assignment, do a simple retain of the 2337f85e193739c953358c865005855253af4f68a497John McCall // result of the call to objc_storeWeak instead of reloading. 23384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGF.getLangOpts().CPlusPlus && 2339f85e193739c953358c865005855253af4f68a497John McCall !type.isVolatileQualified() && 2340f85e193739c953358c865005855253af4f68a497John McCall type.getObjCLifetime() == Qualifiers::OCL_Weak && 2341f85e193739c953358c865005855253af4f68a497John McCall isa<BinaryOperator>(e) && 2342f85e193739c953358c865005855253af4f68a497John McCall cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2343f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitScalarExpr(e), false); 2344f85e193739c953358c865005855253af4f68a497John McCall 2345f85e193739c953358c865005855253af4f68a497John McCall return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2346f85e193739c953358c865005855253af4f68a497John McCall} 2347f85e193739c953358c865005855253af4f68a497John McCall 2348f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2349f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value); 2350f85e193739c953358c865005855253af4f68a497John McCall 2351f85e193739c953358c865005855253af4f68a497John McCall/// Given that the given expression is some sort of call (which does 2352f85e193739c953358c865005855253af4f68a497John McCall/// not return retained), emit a retain following it. 2353f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { 2354f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = CGF.EmitScalarExpr(e); 2355f85e193739c953358c865005855253af4f68a497John McCall return emitARCRetainAfterCall(CGF, value); 2356f85e193739c953358c865005855253af4f68a497John McCall} 2357f85e193739c953358c865005855253af4f68a497John McCall 2358f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2359f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2360f85e193739c953358c865005855253af4f68a497John McCall if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2361f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2362f85e193739c953358c865005855253af4f68a497John McCall 2363f85e193739c953358c865005855253af4f68a497John McCall // Place the retain immediately following the call. 2364f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(call->getParent(), 2365f85e193739c953358c865005855253af4f68a497John McCall ++llvm::BasicBlock::iterator(call)); 2366f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2367f85e193739c953358c865005855253af4f68a497John McCall 2368f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2369f85e193739c953358c865005855253af4f68a497John McCall return value; 2370f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2371f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2372f85e193739c953358c865005855253af4f68a497John McCall 2373f85e193739c953358c865005855253af4f68a497John McCall // Place the retain at the beginning of the normal destination block. 2374f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *BB = invoke->getNormalDest(); 2375f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(BB, BB->begin()); 2376f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2377f85e193739c953358c865005855253af4f68a497John McCall 2378f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2379f85e193739c953358c865005855253af4f68a497John McCall return value; 2380f85e193739c953358c865005855253af4f68a497John McCall 2381f85e193739c953358c865005855253af4f68a497John McCall // Bitcasts can arise because of related-result returns. Rewrite 2382f85e193739c953358c865005855253af4f68a497John McCall // the operand. 2383f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2384f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *operand = bitcast->getOperand(0); 2385f85e193739c953358c865005855253af4f68a497John McCall operand = emitARCRetainAfterCall(CGF, operand); 2386f85e193739c953358c865005855253af4f68a497John McCall bitcast->setOperand(0, operand); 2387f85e193739c953358c865005855253af4f68a497John McCall return bitcast; 2388f85e193739c953358c865005855253af4f68a497John McCall 2389f85e193739c953358c865005855253af4f68a497John McCall // Generic fall-back case. 2390f85e193739c953358c865005855253af4f68a497John McCall } else { 2391f85e193739c953358c865005855253af4f68a497John McCall // Retain using the non-block variant: we never need to do a copy 2392f85e193739c953358c865005855253af4f68a497John McCall // of a block that's been returned to us. 2393f85e193739c953358c865005855253af4f68a497John McCall return CGF.EmitARCRetainNonBlock(value); 2394f85e193739c953358c865005855253af4f68a497John McCall } 2395f85e193739c953358c865005855253af4f68a497John McCall} 2396f85e193739c953358c865005855253af4f68a497John McCall 2397dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate 2398dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or 2399dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context. 2400dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) { 2401dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall assert(e->getType()->isBlockPointerType()); 2402dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall e = e->IgnoreParens(); 2403dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2404dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // For future goodness, emit block expressions directly in +1 2405dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // contexts if we can. 2406dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (isa<BlockExpr>(e)) 2407dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2408dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2409dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2410dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall switch (cast->getCastKind()) { 2411dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Emitting these operations in +1 contexts is goodness. 2412dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_LValueToRValue: 241333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 241433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 241533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 2416dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2417dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2418dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations preserve a block type. 2419dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_NoOp: 2420dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_BitCast: 2421dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2422dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2423dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations are known to be bad (or haven't been considered). 2424dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_AnyPointerToBlockPointerCast: 2425dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall default: 2426dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2427dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2428dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2429dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2430dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2431dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall} 2432dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 24334b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// Try to emit a PseudoObjectExpr at +1. 24344b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// 24354b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// This massively duplicates emitPseudoObjectRValue. 24364b9c2d235fb9449e249d74f48ecfec601650de93John McCallstatic TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, 24374b9c2d235fb9449e249d74f48ecfec601650de93John McCall const PseudoObjectExpr *E) { 2438cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 24394b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24404b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Find the result expression. 24414b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *resultExpr = E->getResultExpr(); 24424b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(resultExpr); 24434b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result; 24444b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24454b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (PseudoObjectExpr::const_semantics_iterator 24464b9c2d235fb9449e249d74f48ecfec601650de93John McCall i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 24474b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *semantic = *i; 24484b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24494b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic expression is an opaque value, bind it 24504b9c2d235fb9449e249d74f48ecfec601650de93John McCall // to the result of its source expression. 24514b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 24524b9c2d235fb9449e249d74f48ecfec601650de93John McCall typedef CodeGenFunction::OpaqueValueMappingData OVMA; 24534b9c2d235fb9449e249d74f48ecfec601650de93John McCall OVMA opaqueData; 24544b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24554b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic is the result of the pseudo-object 24564b9c2d235fb9449e249d74f48ecfec601650de93John McCall // expression, try to evaluate the source as +1. 24574b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (ov == resultExpr) { 24584b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(!OVMA::shouldBindAsLValue(ov)); 24594b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); 24604b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); 24614b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24624b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, just bind it. 24634b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 24644b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 24654b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 24664b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques.push_back(opaqueData); 24674b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24684b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, if the expression is the result, evaluate it 24694b9c2d235fb9449e249d74f48ecfec601650de93John McCall // and remember the result. 24704b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (semantic == resultExpr) { 24714b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, semantic); 24724b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24734b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, evaluate the expression in an ignored context. 24744b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 24754b9c2d235fb9449e249d74f48ecfec601650de93John McCall CGF.EmitIgnoredExpr(semantic); 24764b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 24774b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 24784b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24794b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Unbind all the opaques now. 24804b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (unsigned i = 0, e = opaques.size(); i != e; ++i) 24814b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques[i].unbind(CGF); 24824b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24834b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 24844b9c2d235fb9449e249d74f48ecfec601650de93John McCall} 24854b9c2d235fb9449e249d74f48ecfec601650de93John McCall 2486f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 2487f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 248872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // We should *never* see a nested full-expression here, because if 248972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // we fail to emit at +1, our caller must not retain after we close 249072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // out the full-expression. 249172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall assert(!isa<ExprWithCleanups>(e)); 2492990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 2493f85e193739c953358c865005855253af4f68a497John McCall // The desired result type, if it differs from the type of the 2494f85e193739c953358c865005855253af4f68a497John McCall // ultimate opaque expression. 24952acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *resultType = 0; 2496f85e193739c953358c865005855253af4f68a497John McCall 2497f85e193739c953358c865005855253af4f68a497John McCall while (true) { 2498f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2499f85e193739c953358c865005855253af4f68a497John McCall 2500f85e193739c953358c865005855253af4f68a497John McCall // There's a break at the end of this if-chain; anything 2501f85e193739c953358c865005855253af4f68a497John McCall // that wants to keep looping has to explicitly continue. 2502f85e193739c953358c865005855253af4f68a497John McCall if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 2503f85e193739c953358c865005855253af4f68a497John McCall switch (ce->getCastKind()) { 2504f85e193739c953358c865005855253af4f68a497John McCall // No-op casts don't change the type, so we just ignore them. 2505f85e193739c953358c865005855253af4f68a497John McCall case CK_NoOp: 2506f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2507f85e193739c953358c865005855253af4f68a497John McCall continue; 2508f85e193739c953358c865005855253af4f68a497John McCall 2509f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueToRValue: { 2510f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult loadResult 2511f85e193739c953358c865005855253af4f68a497John McCall = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); 2512f85e193739c953358c865005855253af4f68a497John McCall if (resultType) { 2513f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = loadResult.getPointer(); 2514f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, resultType); 2515f85e193739c953358c865005855253af4f68a497John McCall loadResult.setPointer(value); 2516f85e193739c953358c865005855253af4f68a497John McCall } 2517f85e193739c953358c865005855253af4f68a497John McCall return loadResult; 2518f85e193739c953358c865005855253af4f68a497John McCall } 2519f85e193739c953358c865005855253af4f68a497John McCall 2520f85e193739c953358c865005855253af4f68a497John McCall // These casts can change the type, so remember that and 2521f85e193739c953358c865005855253af4f68a497John McCall // soldier on. We only need to remember the outermost such 2522f85e193739c953358c865005855253af4f68a497John McCall // cast, though. 25231d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 25241d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 2525f85e193739c953358c865005855253af4f68a497John McCall case CK_AnyPointerToBlockPointerCast: 2526f85e193739c953358c865005855253af4f68a497John McCall case CK_BitCast: 2527f85e193739c953358c865005855253af4f68a497John McCall if (!resultType) 2528f85e193739c953358c865005855253af4f68a497John McCall resultType = CGF.ConvertType(ce->getType()); 2529f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2530f85e193739c953358c865005855253af4f68a497John McCall assert(e->getType()->hasPointerRepresentation()); 2531f85e193739c953358c865005855253af4f68a497John McCall continue; 2532f85e193739c953358c865005855253af4f68a497John McCall 2533f85e193739c953358c865005855253af4f68a497John McCall // For consumptions, just emit the subexpression and thus elide 2534f85e193739c953358c865005855253af4f68a497John McCall // the retain/release pair. 253533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: { 2536f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); 2537f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2538f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 2539f85e193739c953358c865005855253af4f68a497John McCall } 2540f85e193739c953358c865005855253af4f68a497John McCall 2541dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Block extends are net +0. Naively, we could just recurse on 2542dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // the subexpression, but actually we need to ensure that the 2543dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // value is copied as a block, so there's a little filter here. 254433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: { 2545dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall llvm::Value *result; // will be a +0 value 2546dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2547dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If we can't safely assume the sub-expression will produce a 2548dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // block-copied value, emit the sub-expression at +0. 2549dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { 2550dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.EmitScalarExpr(ce->getSubExpr()); 2551dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2552dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise, try to emit the sub-expression at +1 recursively. 2553dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } else { 2554dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall TryEmitResult subresult 2555dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); 2556dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = subresult.getPointer(); 2557dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2558dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If that produced a retained value, just use that, 2559dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // possibly casting down. 2560dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (subresult.getInt()) { 2561dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) 2562dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.Builder.CreateBitCast(result, resultType); 2563dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2564dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2565dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2566dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise it's +0. 2567dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2568dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2569dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Retain the object as a block, then cast down. 2570348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 2571dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2572dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2573dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2574dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 25757e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // For reclaims, emit the subexpression as a retained call and 25767e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // skip the consumption. 257733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: { 25787e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); 25797e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 25807e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall return TryEmitResult(result, true); 25817e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall } 25827e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 2583f85e193739c953358c865005855253af4f68a497John McCall default: 2584f85e193739c953358c865005855253af4f68a497John McCall break; 2585f85e193739c953358c865005855253af4f68a497John McCall } 2586f85e193739c953358c865005855253af4f68a497John McCall 2587f85e193739c953358c865005855253af4f68a497John McCall // Skip __extension__. 2588f85e193739c953358c865005855253af4f68a497John McCall } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { 2589f85e193739c953358c865005855253af4f68a497John McCall if (op->getOpcode() == UO_Extension) { 2590f85e193739c953358c865005855253af4f68a497John McCall e = op->getSubExpr(); 2591f85e193739c953358c865005855253af4f68a497John McCall continue; 2592f85e193739c953358c865005855253af4f68a497John McCall } 2593f85e193739c953358c865005855253af4f68a497John McCall 2594f85e193739c953358c865005855253af4f68a497John McCall // For calls and message sends, use the retained-call logic. 2595f85e193739c953358c865005855253af4f68a497John McCall // Delegate inits are a special case in that they're the only 2596f85e193739c953358c865005855253af4f68a497John McCall // returns-retained expression that *isn't* surrounded by 2597f85e193739c953358c865005855253af4f68a497John McCall // a consume. 2598f85e193739c953358c865005855253af4f68a497John McCall } else if (isa<CallExpr>(e) || 2599f85e193739c953358c865005855253af4f68a497John McCall (isa<ObjCMessageExpr>(e) && 2600f85e193739c953358c865005855253af4f68a497John McCall !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 2601f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = emitARCRetainCall(CGF, e); 2602f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2603f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 26044b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26054b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Look through pseudo-object expressions. 26064b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 26074b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result 26084b9c2d235fb9449e249d74f48ecfec601650de93John McCall = tryEmitARCRetainPseudoObject(CGF, pseudo); 26094b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (resultType) { 26104b9c2d235fb9449e249d74f48ecfec601650de93John McCall llvm::Value *value = result.getPointer(); 26114b9c2d235fb9449e249d74f48ecfec601650de93John McCall value = CGF.Builder.CreateBitCast(value, resultType); 26124b9c2d235fb9449e249d74f48ecfec601650de93John McCall result.setPointer(value); 26134b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26144b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 2615f85e193739c953358c865005855253af4f68a497John McCall } 2616f85e193739c953358c865005855253af4f68a497John McCall 2617f85e193739c953358c865005855253af4f68a497John McCall // Conservatively halt the search at any other expression kind. 2618f85e193739c953358c865005855253af4f68a497John McCall break; 2619f85e193739c953358c865005855253af4f68a497John McCall } 2620f85e193739c953358c865005855253af4f68a497John McCall 2621f85e193739c953358c865005855253af4f68a497John McCall // We didn't find an obvious production, so emit what we've got and 2622f85e193739c953358c865005855253af4f68a497John McCall // tell the caller that we didn't manage to retain. 2623f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(e); 2624f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2625f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, false); 2626f85e193739c953358c865005855253af4f68a497John McCall} 2627f85e193739c953358c865005855253af4f68a497John McCall 2628f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2629f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2630f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2631f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 2632f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2633f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2634f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetain(type, value); 2635f85e193739c953358c865005855253af4f68a497John McCall return value; 2636f85e193739c953358c865005855253af4f68a497John McCall} 2637f85e193739c953358c865005855253af4f68a497John McCall 2638f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to 2639f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 2640f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce 2641f85e193739c953358c865005855253af4f68a497John McCall/// retained objects. 2642f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 264372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 264472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 264572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 264672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 264772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 264872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 264972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2650f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2651f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2652f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2653f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetain(e->getType(), value); 2654f85e193739c953358c865005855253af4f68a497John McCall return value; 2655f85e193739c953358c865005855253af4f68a497John McCall} 2656f85e193739c953358c865005855253af4f68a497John McCall 2657f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2658f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 265972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 266072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 266172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 266272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 266372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 266472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 266572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2666f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2667f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2668f85e193739c953358c865005855253af4f68a497John McCall if (result.getInt()) 2669f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2670f85e193739c953358c865005855253af4f68a497John McCall else 2671f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetainAutorelease(e->getType(), value); 2672f85e193739c953358c865005855253af4f68a497John McCall return value; 2673f85e193739c953358c865005855253af4f68a497John McCall} 2674f85e193739c953358c865005855253af4f68a497John McCall 2675348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 2676348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result; 2677348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool doRetain; 2678348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2679348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (shouldEmitSeparateBlockRetain(e)) { 2680348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitScalarExpr(e); 2681348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = true; 2682348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } else { 2683348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 2684348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = subresult.getPointer(); 2685348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = !subresult.getInt(); 2686348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 2687348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2688348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (doRetain) 2689348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitARCRetainBlock(result, /*mandatory*/ true); 2690348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitObjCConsumeObject(e->getType(), result); 2691348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall} 2692348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 26932b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 26942b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // In ARC, retain and autorelease the expression. 26954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 26962b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Do so before running any cleanups for the full-expression. 269772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // EmitARCRetainAutoreleaseScalarExpr does this for us. 26982b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitARCRetainAutoreleaseScalarExpr(expr); 26992b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall } 27002b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 27012b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Otherwise, use the normal scalar-expression emission. The 27022b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception machinery doesn't do anything special with the 27032b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception like retaining it, so there's no safety associated with 27042b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // only running cleanups after the throw has started, and when it 27052b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // matters it tends to be substantially inferior code. 27062b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitScalarExpr(expr); 27072b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall} 27082b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 2709f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2710f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 2711f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2712f85e193739c953358c865005855253af4f68a497John McCall // Evaluate the RHS first. 2713f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 2714f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2715f85e193739c953358c865005855253af4f68a497John McCall 2716fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall bool hasImmediateRetain = result.getInt(); 2717fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2718fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // If we didn't emit a retained object, and the l-value is of block 2719fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // type, then we need to emit the block-retain immediately in case 2720fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // it invalidates the l-value. 2721fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 2722348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ false); 2723fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall hasImmediateRetain = true; 2724fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall } 2725fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2726f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2727f85e193739c953358c865005855253af4f68a497John McCall 2728f85e193739c953358c865005855253af4f68a497John McCall // If the RHS was emitted retained, expand this. 2729fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (hasImmediateRetain) { 2730f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *oldValue = 27316da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitLoadOfScalar(lvalue); 27326da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 2733f85e193739c953358c865005855253af4f68a497John McCall EmitARCRelease(oldValue, /*precise*/ false); 2734f85e193739c953358c865005855253af4f68a497John McCall } else { 2735545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall value = EmitARCStoreStrong(lvalue, value, ignored); 2736f85e193739c953358c865005855253af4f68a497John McCall } 2737f85e193739c953358c865005855253af4f68a497John McCall 2738f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2739f85e193739c953358c865005855253af4f68a497John McCall} 2740f85e193739c953358c865005855253af4f68a497John McCall 2741f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2742f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 2743f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 2744f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2745f85e193739c953358c865005855253af4f68a497John McCall 27466da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 2747f85e193739c953358c865005855253af4f68a497John McCall 2748f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2749f85e193739c953358c865005855253af4f68a497John McCall} 2750f85e193739c953358c865005855253af4f68a497John McCall 2751f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt( 275216098f366097305c348b356a44638d6c959c6e60Eric Christopher const ObjCAutoreleasePoolStmt &ARPS) { 2753f85e193739c953358c865005855253af4f68a497John McCall const Stmt *subStmt = ARPS.getSubStmt(); 2754f85e193739c953358c865005855253af4f68a497John McCall const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 2755f85e193739c953358c865005855253af4f68a497John McCall 2756f85e193739c953358c865005855253af4f68a497John McCall CGDebugInfo *DI = getDebugInfo(); 275773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 275873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 2759f85e193739c953358c865005855253af4f68a497John McCall 2760f85e193739c953358c865005855253af4f68a497John McCall // Keep track of the current cleanup stack depth. 2761f85e193739c953358c865005855253af4f68a497John McCall RunCleanupsScope Scope(*this); 27620a7dd788dbef975f35f273c7ab913f480f7edd60John McCall if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 2763f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCAutoreleasePoolPush(); 2764f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 2765f85e193739c953358c865005855253af4f68a497John McCall } else { 2766f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 2767f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 2768f85e193739c953358c865005855253af4f68a497John McCall } 2769f85e193739c953358c865005855253af4f68a497John McCall 2770f85e193739c953358c865005855253af4f68a497John McCall for (CompoundStmt::const_body_iterator I = S.body_begin(), 2771f85e193739c953358c865005855253af4f68a497John McCall E = S.body_end(); I != E; ++I) 2772f85e193739c953358c865005855253af4f68a497John McCall EmitStmt(*I); 2773f85e193739c953358c865005855253af4f68a497John McCall 277473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 277573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 2776f85e193739c953358c865005855253af4f68a497John McCall} 27770c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 27780c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 27790c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point. 27800c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 27810c24c808e4dce43e88abf2d5f98546b901bd4315John McCall // We just use an inline assembly. 27820c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::FunctionType *extenderType 2783de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 27840c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::Value *extender 27850c24c808e4dce43e88abf2d5f98546b901bd4315John McCall = llvm::InlineAsm::get(extenderType, 27860c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* assembly */ "", 27870c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* constraints */ "r", 27880c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* side effects */ true); 27890c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 27900c24c808e4dce43e88abf2d5f98546b901bd4315John McCall object = Builder.CreateBitCast(object, VoidPtrTy); 2791bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(extender, object); 27920c24c808e4dce43e88abf2d5f98546b901bd4315John McCall} 27930c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 279420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 279584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// non-trivial copy assignment function, produce following helper function. 279684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 279784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// 279884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanianllvm::Constant * 279920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 280020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2801260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 280290f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 280384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 280484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType Ty = PID->getPropertyIvarDecl()->getType(); 280584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian if (!Ty->isRecordType()) 280684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 280784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 280820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 280984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 2810b08cfb3c5909752c0e501a4e5c31a507a4cc1f72Fariborz Jahanian llvm::Constant * HelperFn = 0; 281120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialSetExpr(PID)) 281220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 281320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 281420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 281520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 281684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 281784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ASTContext &C = getContext(); 281884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian IdentifierInfo *II 281920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 282084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 282184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian C.getTranslationUnitDecl(), 282284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), 282384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 282484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SC_Static, 282584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SC_None, 282684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian false, 2827b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 282884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 282984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 283084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType SrcTy = Ty; 283184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy.addConst(); 283284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 283384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 283484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionArgList args; 283584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 283684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&dstDecl); 283784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 283884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&srcDecl); 283984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 284084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const CGFunctionInfo &FI = 2841de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2842de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionType::ExtInfo(), 2843de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs::All); 284484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2845de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 284684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 284784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function *Fn = 284884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 284916098f366097305c348b356a44638d6c959c6e60Eric Christopher "__assign_helper_atomic_property_", 285016098f366097305c348b356a44638d6c959c6e60Eric Christopher &CGM.getModule()); 285184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2852a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov // Initialize debug info if needed. 2853a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov maybeInitializeDebugInfo(); 285484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 285584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 285684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2857f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2858f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2859f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 2860f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 286184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2862f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 2863f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2864f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2865f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 286684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2867f4b88a45902af1802a1cb42ba48b1c474474f228John McCall Expr *Args[2] = { &DST, &SRC }; 286820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 2869f4b88a45902af1802a1cb42ba48b1c474474f228John McCall CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), 28703b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer Args, DestTy->getPointeeType(), 2871be9af1288881110e406b87914162eaa59f1e5918Lang Hames VK_LValue, SourceLocation(), false); 2872f4b88a45902af1802a1cb42ba48b1c474474f228John McCall 2873f4b88a45902af1802a1cb42ba48b1c474474f228John McCall EmitStmt(&TheCall); 287484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 287584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FinishFunction(); 2876cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 287720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 2878cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian return HelperFn; 287920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 288020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 288120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianllvm::Constant * 288220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 288320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2884260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 288590f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 288620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 288720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 288820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType Ty = PD->getType(); 288920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!Ty->isRecordType()) 289020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 289120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 289220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 289320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant * HelperFn = 0; 289420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 289520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialGetExpr(PID)) 289620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 289720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 289820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 289920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 290020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 290120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 290220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ASTContext &C = getContext(); 290320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian IdentifierInfo *II 290420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 290520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 290620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian C.getTranslationUnitDecl(), 290720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), 290820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 290920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SC_Static, 291020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SC_None, 291120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian false, 2912b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 291320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 291420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 291520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType SrcTy = Ty; 291620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy.addConst(); 291720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 291820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 291920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionArgList args; 292020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 292120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&dstDecl); 292220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 292320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&srcDecl); 292420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 292520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const CGFunctionInfo &FI = 2926de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2927de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionType::ExtInfo(), 2928de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs::All); 292920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2930de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 293120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 293220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function *Fn = 293320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 293420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian "__copy_helper_atomic_property_", &CGM.getModule()); 293584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2936a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov // Initialize debug info if needed. 2937a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov maybeInitializeDebugInfo(); 293820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 293920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 294020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2941f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 294220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian VK_RValue, SourceLocation()); 294320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2944f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2945f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 294620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 294720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *CXXConstExpr = 294820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 294920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SmallVector<Expr*, 4> ConstructorArgs; 2951f4b88a45902af1802a1cb42ba48b1c474474f228John McCall ConstructorArgs.push_back(&SRC); 295220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin(); 295320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ++A; 295420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end(); 295620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian A != AEnd; ++A) 295720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ConstructorArgs.push_back(*A); 295820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *TheCXXConstructExpr = 296020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::Create(C, Ty, SourceLocation(), 296120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->getConstructor(), 296220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->isElidable(), 29633b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer ConstructorArgs, 29645b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->hadMultipleCandidates(), 29655b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->isListInitialization(), 296620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->requiresZeroInitialization(), 296716098f366097305c348b356a44638d6c959c6e60Eric Christopher CXXConstExpr->getConstructionKind(), 296816098f366097305c348b356a44638d6c959c6e60Eric Christopher SourceRange()); 296920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2970f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2971f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 297220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2973f4b88a45902af1802a1cb42ba48b1c474474f228John McCall RValue DV = EmitAnyExpr(&DstExpr); 297416098f366097305c348b356a44638d6c959c6e60Eric Christopher CharUnits Alignment 297516098f366097305c348b356a44638d6c959c6e60Eric Christopher = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 297620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitAggExpr(TheCXXConstructExpr, 297720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(), 297820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::IsDestructed, 297920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::DoesNotNeedGCBarriers, 2980649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 298120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 298220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FinishFunction(); 298320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 298420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 298520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 298684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian} 298784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2988cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedmanllvm::Value * 2989cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli FriedmanCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 2990cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Get selectors for retain/autorelease. 29918c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 29928c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Selector CopySelector = 29938c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman getContext().Selectors.getNullarySelector(CopyID); 2994cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 2995cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Selector AutoreleaseSelector = 2996cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman getContext().Selectors.getNullarySelector(AutoreleaseID); 2997cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 2998cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Emit calls to retain/autorelease. 2999cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3000cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman llvm::Value *Val = Block; 3001cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman RValue Result; 3002cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 30038c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Ty, CopySelector, 3004cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3005cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3006cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3007cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Ty, AutoreleaseSelector, 3008cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3009cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3010cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman return Val; 3011cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman} 3012cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 301384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 30142979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {} 3015